Created
June 24, 2015 21:29
-
-
Save marinoluck/3df2aa7f4270cce3a60e to your computer and use it in GitHub Desktop.
Game state a bit smarter
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<ChoiceResponse> userAnswers; | |
private ArrayList<ChoiceResponse> 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; | |
} else if (prompt.getPosition() == getCurrentPromptId() | |
&& QuizConstats.END_LOOP.equals(getCurrentLoop())) { | |
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<ChoiceResponse> getUserAnswers() { | |
return userAnswers; | |
} | |
public void setUserAnswers(ArrayList<ChoiceResponse> userAnswers) { | |
this.userAnswers = userAnswers; | |
} | |
public List<ChoiceResponse> getOpponentAnswers() { | |
return opponentAnswers; | |
} | |
public void setOpponentAnswers(ArrayList<ChoiceResponse> opponentAnswers) { | |
this.opponentAnswers = opponentAnswers; | |
} | |
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; | |
if (currentPromptId != quiz.getQuizScript().getPrompts().size()) { | |
// prepare it for the nex loop, otherwise, we have to simulate the full game | |
currentLoop = QuizConstats.START_LOOP; | |
} | |
} else { | |
// continues with the loop the full time was spent | |
loopAccumulatedTime += timeHasPassedInSeconds; | |
return; | |
} | |
} | |
} | |
} | |
public void addUserResponse(ChoiceResponse r) { | |
if (userAnswers == null) | |
userAnswers = new ArrayList<>(); | |
userAnswers.add(r); | |
} | |
@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.writeInt(this.currentPromptId); | |
dest.writeTypedList(this.userAnswers); | |
dest.writeTypedList(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(); | |
userAnswers = new ArrayList<>(); | |
opponentAnswers = new ArrayList<>(); | |
in.readTypedList(this.userAnswers, ChoiceResponse.CREATOR); | |
in.readTypedList(this.opponentAnswers, ChoiceResponse.CREATOR); | |
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 incrementLoopAccumulatedTime() { | |
loopAccumulatedTime++; | |
} | |
public boolean isInStartLoop() { | |
return currentLoop.equals(QuizConstats.START_LOOP); | |
} | |
public boolean isInPlayLoop() { | |
return currentLoop.equals(QuizConstats.PLAY_LOOP); | |
} | |
public boolean isInEndLoop() { | |
return currentLoop.equals(QuizConstats.END_LOOP); | |
} | |
/** | |
* check if the current loop hit this time | |
* @param timeToCheck | |
* @return | |
*/ | |
public boolean hitThisTime(long timeToCheck) { | |
return loopAccumulatedTime == timeToCheck; | |
} | |
/** | |
* cleans the prompt state, and leaves it ready to start to play next prompt | |
*/ | |
public void prepareForNextPrompt() { | |
userAnswered = false; | |
opponentAnswered = false; | |
loopAccumulatedTime = 0; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment