Skip to content

Move responsibility from RoundManager to AnswerSubmission #59

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@
import group07.beatbattle.BeatBattle;
import group07.beatbattle.ecs.Engine;
import group07.beatbattle.firebase.FirebaseGateway;
import group07.beatbattle.model.domain.AnswerSubmission;
import group07.beatbattle.model.domain.GameSession;
import group07.beatbattle.model.domain.Player;
import group07.beatbattle.model.domain.Question;
import group07.beatbattle.utils.ScoreCalculator;
import group07.beatbattle.ecs.RoundManager;
import group07.beatbattle.states.LeaderboardState;
import group07.beatbattle.states.StartState;
Expand Down Expand Up @@ -124,12 +124,10 @@ public void onFailure(Exception exception) {

public void onAnswerSubmitted(String answer) {
float answerTime = getTotalTime() - getTimeRemaining();
boolean correct = question.isCorrect(answer);
int points = ScoreCalculator.calculateScore(correct, answerTime);

Player localPlayer = findLocalPlayer();
if (localPlayer != null) {
localPlayer.addScore(points);
AnswerSubmission submission = AnswerSubmission.create(localPlayer.getId(), answer, answerTime, question);
localPlayer.addScore(submission.getPointsAwarded());
submitScoreToFirebase(localPlayer.getId(), localPlayer.getScore());
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package group07.beatbattle.model.domain;

import group07.beatbattle.utils.ScoreCalculator;

/**
* Immutable record of a player's answer for a single round.
* Stores what they answered, when they answered, whether it was correct, and how many points were awarded.
Expand All @@ -21,6 +23,13 @@ public AnswerSubmission(String playerId, String selectedAnswer, long timestamp,
this.pointsAwarded = pointsAwarded;
}

/** Creates an AnswerSubmission by evaluating correctness and calculating score from the given question and answer time. */
public static AnswerSubmission create(String playerId, String selectedAnswer, float answerTime, Question question) {
boolean correct = question.isCorrect(selectedAnswer);
int points = ScoreCalculator.calculateScore(correct, answerTime);
return new AnswerSubmission(playerId, selectedAnswer, System.currentTimeMillis(), correct, points);
}

public String getPlayerId() { return playerId; }
public String getSelectedAnswer() { return selectedAnswer; }
public long getTimestamp() { return timestamp; }
Expand Down
Loading