Created
June 23, 2015 21:16
-
-
Save marinoluck/b8d5e704c994dadf16ec to your computer and use it in GitHub Desktop.
Game State
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Represents the snapshot of the quiz in a determined moment. | |
* With tis information Quiz presenter is able to redraw the quiz screen and continue playing | |
* (if there are time for it) | |
* | |
* @author Lucas Marino | |
*/ | |
public class GameState implements Parcelable { | |
//the current loop I'm running | |
private String currentLoop; | |
private boolean opponentAnswered; | |
private boolean userAnswered; | |
private long loopAccumulatedTime = 0; | |
private int currentPromptId; | |
private ArrayList<QuizResponse> userAnswers; | |
private ArrayList<QuizResponse> opponentAnswers; | |
long quizId; // to ask the quiz if it was deleted for the SO | |
long timeStamp; // exact time of this snapshot | |
public String getCurrentLoop() { | |
return currentLoop; | |
} | |
public void setCurrentLoop(String currentLoop) { | |
this.currentLoop = currentLoop; | |
} | |
public boolean isOpponentAnswered() { | |
return opponentAnswered; | |
} | |
public void setOpponentAnswered(boolean opponentAnswered) { | |
this.opponentAnswered = opponentAnswered; | |
} | |
public boolean isUserAnswered() { | |
return userAnswered; | |
} | |
public void setUserAnswered(boolean userAnswered) { | |
this.userAnswered = userAnswered; | |
} | |
public long getLoopAccumulatedTime() { | |
return loopAccumulatedTime; | |
} | |
public void setLoopAccumulatedTime(long loopAccumulatedTime) { | |
this.loopAccumulatedTime = loopAccumulatedTime; | |
} | |
public long getCurrentPromptId() { | |
return currentPromptId; | |
} | |
public void setCurrentPromptId(int currentPromptId) { | |
this.currentPromptId = currentPromptId; | |
} | |
public long getQuizId() { | |
return quizId; | |
} | |
public void setQuizId(long quizId) { | |
this.quizId = quizId; | |
} | |
public long getTimeStamp() { | |
return timeStamp; | |
} | |
public void setTimeStamp(long timeStamp) { | |
this.timeStamp = timeStamp; | |
} | |
// if it is previous to current prompt should do full fast forward | |
public boolean shouldFullFastForward(Prompt prompt) { | |
if (prompt.getPosition() < getCurrentPromptId()) { | |
return true; | |
} | |
return false; | |
} | |
public long getLeftTimeForPromptInSeconds(Prompt prompt) { | |
long leftTime = 0; | |
if (QuizConstats.START_LOOP.equals(getCurrentLoop())) { | |
leftTime = QuizConstats.START_LOOP_TIME_SECONDS - loopAccumulatedTime + | |
prompt.getTimeout() + QuizConstats.END_LOOP_TIME_SECONDS; | |
} else if (QuizConstats.PLAY_LOOP.equals(getCurrentLoop())) { | |
leftTime = prompt.getTimeout() - loopAccumulatedTime + | |
QuizConstats.END_LOOP_TIME_SECONDS; | |
} else if (QuizConstats.END_LOOP.equals(getCurrentLoop())) { | |
leftTime = QuizConstats.END_LOOP_TIME_SECONDS - loopAccumulatedTime; | |
} | |
return leftTime; | |
} | |
public long getAccumulatedTime(Prompt prompt) { | |
long accumulated = 0; | |
if (QuizConstats.START_LOOP.equals(getCurrentLoop())) { | |
accumulated = loopAccumulatedTime; | |
} else if (QuizConstats.PLAY_LOOP.equals(getCurrentLoop())) { | |
accumulated = QuizConstats.START_LOOP_TIME_SECONDS + loopAccumulatedTime; | |
} else if (QuizConstats.END_LOOP.equals(getCurrentLoop())) { | |
accumulated = QuizConstats.START_LOOP_TIME_SECONDS + prompt.getTimeout() + loopAccumulatedTime; | |
} | |
return accumulated; | |
} | |
public GameState() { | |
} | |
public List<QuizResponse> getUserAnswers() { | |
return userAnswers; | |
} | |
public void setUserAnswers(ArrayList<QuizResponse> userAnswers) { | |
this.userAnswers = userAnswers; | |
} | |
public List<QuizResponse> getOpponentAnswers() { | |
return opponentAnswers; | |
} | |
public void setOpponentAnswers(ArrayList<QuizResponse> opponentAnswers) { | |
this.opponentAnswers = opponentAnswers; | |
} | |
@Override | |
public int describeContents() { | |
return 0; | |
} | |
@Override | |
public void writeToParcel(Parcel dest, int flags) { | |
dest.writeString(this.currentLoop); | |
dest.writeByte(opponentAnswered ? (byte) 1 : (byte) 0); | |
dest.writeByte(userAnswered ? (byte) 1 : (byte) 0); | |
dest.writeLong(this.loopAccumulatedTime); | |
dest.writeLong(this.currentPromptId); | |
dest.writeSerializable(this.userAnswers); | |
dest.writeSerializable(this.opponentAnswers); | |
dest.writeLong(this.quizId); | |
dest.writeLong(this.timeStamp); | |
} | |
private GameState(Parcel in) { | |
this.currentLoop = in.readString(); | |
this.opponentAnswered = in.readByte() != 0; | |
this.userAnswered = in.readByte() != 0; | |
this.loopAccumulatedTime = in.readLong(); | |
this.currentPromptId = in.readInt(); | |
this.userAnswers = (ArrayList<QuizResponse>) in.readSerializable(); | |
this.opponentAnswers = (ArrayList<QuizResponse>) in.readSerializable(); | |
this.quizId = in.readLong(); | |
this.timeStamp = in.readLong(); | |
} | |
public static final Creator<GameState> CREATOR = new Creator<GameState>() { | |
public GameState createFromParcel(Parcel source) { | |
return new GameState(source); | |
} | |
public GameState[] newArray(int size) { | |
return new GameState[size]; | |
} | |
}; | |
public void forwadTo(long timeInMillis, Quiz quiz) { | |
long timeHasPassedInSeconds = (timeInMillis - getTimeStamp()) / 1000; | |
// iterates the next prompts until spend all the time or not more prompts | |
for (int i = currentPromptId; i <= quiz.getQuizScript().getPrompts().size(); i++) { | |
// update the prompt Id | |
currentPromptId = i; | |
// continue the last played prompt | |
Prompt p = quiz.getScript().getPrompts().get(i); | |
// forward loop by loop untill spend the full time | |
//TODO this 3 loops are similar make an abstraction giving params | |
if (QuizConstats.START_LOOP.equals(currentLoop)) { | |
if (QuizConstats.START_LOOP_TIME_SECONDS < timeHasPassedInSeconds) { | |
timeHasPassedInSeconds -= QuizConstats.START_LOOP_TIME_SECONDS; | |
currentLoop = QuizConstats.PLAY_LOOP; | |
} else { | |
// continues with the loop the full time was spent | |
loopAccumulatedTime += timeHasPassedInSeconds; | |
return; | |
} | |
} | |
if (QuizConstats.PLAY_LOOP.equals(currentLoop)) { | |
if (p.getTimeout() < timeHasPassedInSeconds) { | |
timeHasPassedInSeconds -= p.getTimeout(); | |
currentLoop = QuizConstats.END_LOOP; | |
} else { | |
// continues with the loop the full time was spent | |
loopAccumulatedTime += timeHasPassedInSeconds; | |
return; | |
} | |
} | |
if (QuizConstats.END_LOOP.equals(currentLoop)) { | |
if (QuizConstats.END_LOOP_TIME_SECONDS < timeHasPassedInSeconds) { | |
timeHasPassedInSeconds -= QuizConstats.END_LOOP_TIME_SECONDS; | |
currentLoop = QuizConstats.START_LOOP; | |
} else { | |
// continues with the loop the full time was spent | |
loopAccumulatedTime += timeHasPassedInSeconds; | |
return; | |
} | |
} | |
} | |
} | |
public void addUserResponse(QuizResponse r) { | |
if (userAnswers == null) | |
userAnswers = new ArrayList<>(); | |
userAnswers.add(r); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment