Skip to content

Commit

Permalink
Merge pull request #59 from milospi/58-feature-move-responsibility-fr…
Browse files Browse the repository at this point in the history
…om-roundmanager-to-answersubmission

Move responsibility from RoundManager to AnswerSubmission
  • Loading branch information
odaakj authored Apr 21, 2026
2 parents 5fbf7c3 + c4d8731 commit 20b3950
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 5 deletions.
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

0 comments on commit 20b3950

Please sign in to comment.