Skip to content

Commit

Permalink
Revert "Merge branch 'main' of git.ntnu.no:milospi/TDT4240gr07-BeatBa…
Browse files Browse the repository at this point in the history
…ttle"

This reverts commit 3a93d9d, reversing
changes made to 6f60727.
  • Loading branch information
milospi committed Apr 13, 2026
1 parent 3a93d9d commit 580dbd4
Show file tree
Hide file tree
Showing 22 changed files with 69 additions and 245 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -99,8 +99,8 @@ public void listenToSessionState(String sessionId, SessionStateListener listener
}

@Override
public void updatePlayerScore(String sessionId, String playerId, int score, int roundScore, SimpleCallback callback) {
sessionRepository.updatePlayerScore(sessionId, playerId, score, roundScore, callback);
public void updatePlayerScore(String sessionId, String playerId, int score, SimpleCallback callback) {
sessionRepository.updatePlayerScore(sessionId, playerId, score, callback);
}

@Override
Expand All @@ -117,9 +117,4 @@ public void submitAnswer(String sessionId, int roundIndex, String playerId, Simp
public void listenToRoundAnswerCount(String sessionId, int roundIndex, AnswerCountCallback callback) {
sessionRepository.listenToRoundAnswerCount(sessionId, roundIndex, callback);
}

@Override
public void resetSessionForNewGame(String sessionId, SimpleCallback callback) {
sessionRepository.resetSessionForNewGame(sessionId, callback);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -159,11 +159,6 @@ public ListenerRegistration listenToPlayers(
player.setScore(scoreValue.intValue());
}

Long roundScoreValue = document.getLong("roundScore");
if (roundScoreValue != null) {
player.setLastRoundScore(roundScoreValue.intValue());
}

players.add(player);
});

Expand All @@ -189,8 +184,6 @@ public void fetchPlayers(
Player player = new Player(playerId, name, isHost);
Long scoreValue = document.getLong("score");
if (scoreValue != null) player.setScore(scoreValue.intValue());
Long roundScoreValue = document.getLong("roundScore");
if (roundScoreValue != null) player.setLastRoundScore(roundScoreValue.intValue());
players.add(player);
}
callback.onPlayersChanged(players);
Expand Down Expand Up @@ -350,10 +343,7 @@ public void listenToSessionState(
listener.onFailure(error);
return;
}
if (snapshot == null || !snapshot.exists()) {
listener.onStateChanged("deleted", 0);
return;
}
if (snapshot == null || !snapshot.exists()) return;
String state = snapshot.getString("state");
Long roundLong = snapshot.getLong("currentRound");
int currentRound = roundLong != null ? roundLong.intValue() : 0;
Expand Down Expand Up @@ -422,12 +412,10 @@ public void updatePlayerScore(
String sessionId,
String playerId,
int score,
int roundScore,
FirebaseGateway.SimpleCallback callback
) {
Map<String, Object> update = new HashMap<>();
update.put("score", score);
update.put("roundScore", roundScore);

firestore.collection(SESSIONS)
.document(sessionId)
Expand All @@ -437,51 +425,4 @@ public void updatePlayerScore(
.addOnSuccessListener(unused -> callback.onSuccess())
.addOnFailureListener(callback::onFailure);
}

public void resetSessionForNewGame(
String sessionId,
FirebaseGateway.SimpleCallback callback
) {
// Delete all Answers first
firestore.collection(SESSIONS)
.document(sessionId)
.collection("Answers")
.get()
.addOnSuccessListener(answersSnap -> {
WriteBatch batch = firestore.batch();

for (DocumentSnapshot doc : answersSnap.getDocuments()) {
batch.delete(doc.getReference());
}

// Reset all player scores to 0 in the same batch
firestore.collection(SESSIONS)
.document(sessionId)
.collection(PLAYERS)
.get()
.addOnSuccessListener(playersSnap -> {
Map<String, Object> scoreReset = new HashMap<>();
scoreReset.put("score", 0);
scoreReset.put("roundScore", 0);
for (DocumentSnapshot p : playersSnap.getDocuments()) {
batch.update(p.getReference(), scoreReset);
}

// Reset session state + currentRound
Map<String, Object> sessionReset = new HashMap<>();
sessionReset.put("state", "lobby");
sessionReset.put("currentRound", 0);
batch.update(
firestore.collection(SESSIONS).document(sessionId),
sessionReset
);

batch.commit()
.addOnSuccessListener(u -> callback.onSuccess())
.addOnFailureListener(callback::onFailure);
})
.addOnFailureListener(callback::onFailure);
})
.addOnFailureListener(callback::onFailure);
}
}
5 changes: 1 addition & 4 deletions core/src/main/java/group07/beatbattle/BeatBattle.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import group07.beatbattle.audio.AudioPlayer;
import group07.beatbattle.firebase.FirebaseGateway;
import group07.beatbattle.controller.LobbyController;
import group07.beatbattle.utils.Strings;
import group07.beatbattle.i18n.Strings;
import group07.beatbattle.ecs.Engine;
import group07.beatbattle.model.GameSession;
import group07.beatbattle.service.MusicService;
Expand Down Expand Up @@ -37,7 +37,6 @@ public BeatBattle(FirebaseGateway firebaseGateway) {
private String localPlayerId;
private String localPlayerName;
private String localSessionId;
private String localGamePin;
private boolean localIsHost;

public void setServices(MusicService musicService, AudioPlayer audioPlayer) {
Expand All @@ -61,8 +60,6 @@ public void setLocalSession(String sessionId, boolean isHost) {
this.localIsHost = isHost;
}

public void setLocalGamePin(String gamePin) { this.localGamePin = gamePin; }
public String getLocalGamePin() { return localGamePin; }
public String getLocalSessionId() { return localSessionId; }
public boolean isLocalHost() { return localIsHost; }

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,8 @@
import group07.beatbattle.model.GameSession;
import group07.beatbattle.model.Leaderboard;
import group07.beatbattle.model.LeaderboardEntry;
import group07.beatbattle.model.GameMode;
import group07.beatbattle.states.GameOverState;
import group07.beatbattle.states.InRoundState;
import group07.beatbattle.states.LobbyState;
import group07.beatbattle.states.StartState;
import group07.beatbattle.states.StateManager;

Expand Down Expand Up @@ -95,33 +93,6 @@ public void onBackToMenu() {
StateManager.getInstance().setState(new StartState(game, new LobbyController(game)));
}

public void onPlayAgain() {
String sessionId = game.getLocalSessionId();
// Reset answers, scores, and state in Firebase, then go to lobby
game.getFirebaseGateway().resetSessionForNewGame(
sessionId,
new FirebaseGateway.SimpleCallback() {
@Override public void onSuccess() { goToLobby(); }
@Override public void onFailure(Exception e) { goToLobby(); }
}
);
}

private void goToLobby() {
Gdx.app.postRunnable(() ->
StateManager.getInstance().setState(new LobbyState(
game,
GameMode.CREATE,
game.getLocalSessionId(),
game.getLocalGamePin(),
game.getLocalPlayerId(),
game.getLocalPlayerName(),
true,
new LobbyController(game)
))
);
}

public void onGameOver() {
String sessionId = game.getLocalSessionId();
if (game.isLocalHost() && sessionId != null && !sessionId.isBlank()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,6 @@ public void onSuccess(SessionCreationResult result) {
Gdx.app.postRunnable(() -> {
game.setLocalPlayer(result.getHostId(), result.getHostName());
game.setLocalSession(result.getSessionId(), true);
game.setLocalGamePin(result.getGamePin());
view.setLoadingState(false);
view.setStatusMessage("");
StateManager.getInstance().setState(
Expand Down Expand Up @@ -115,7 +114,6 @@ public void onSuccess(SessionJoinResult result) {
Gdx.app.postRunnable(() -> {
game.setLocalPlayer(result.getPlayerId(), result.getPlayerName());
game.setLocalSession(result.getSessionId(), false);
game.setLocalGamePin(result.getGamePin());
view.setLoadingState(false);
view.setStatusMessage("");
StateManager.getInstance().setState(
Expand Down Expand Up @@ -274,7 +272,6 @@ private void startJoinerSyncListener(String sessionId, GameSession session) {
new FirebaseGateway.SessionStateListener() {
@Override
public void onStateChanged(String state, int currentRound) {
if ("STOPPED".equals(lastHandled[0])) return;
String key = state + ":" + currentRound;
if (key.equals(lastHandled[0])) return;
lastHandled[0] = key;
Expand All @@ -289,27 +286,6 @@ public void onStateChanged(String state, int currentRound) {
LeaderboardController lc = new LeaderboardController(game, session, lb);
StateManager.getInstance().setState(new GameOverState(game, lc));
});
} else if ("lobby".equals(state)) {
// Host pressed Play Again — kill this listener and return to lobby
lastHandled[0] = "STOPPED";
Gdx.app.postRunnable(() ->
StateManager.getInstance().setState(new LobbyState(
game,
GameMode.JOIN,
sessionId,
game.getLocalGamePin(),
game.getLocalPlayerId(),
game.getLocalPlayerName(),
false,
new LobbyController(game)
))
);
} else if ("deleted".equals(state)) {
// Host deleted the session — send everyone back to start
lastHandled[0] = "STOPPED";
Gdx.app.postRunnable(() ->
StateManager.getInstance().setState(new StartState(game, new LobbyController(game)))
);
}
}

Expand All @@ -331,10 +307,7 @@ public void onPlayersChanged(List<Player> updated) {
Gdx.app.postRunnable(() -> {
for (Player u : updated) {
for (Player local : session.getPlayers()) {
if (local.getId().equals(u.getId())) {
local.setScore(u.getScore());
local.setLastRoundScore(u.getLastRoundScore());
}
if (local.getId().equals(u.getId())) local.setScore(u.getScore());
}
}
Leaderboard lb = new Leaderboard(session.getPlayers());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ public class RoundController {
private final Entity roundEntity;
private boolean playerAnswered = false;
private boolean transitioned = false;
private boolean allAnswered = false;

public RoundController(BeatBattle game, Question question, GameSession session) {
this.game = game;
Expand All @@ -50,19 +49,6 @@ public Question getQuestion() {
return question;
}

/** True once the Firebase answer count has reached totalPlayers. */
public boolean isAllAnswered() {
return allAnswered;
}

/** Called by the view when a player's timer expires without them clicking an answer. */
public void onPlayerTimedOut() {
if (!playerAnswered) {
playerAnswered = true;
recordAnswerInFirebase();
}
}

public float getTimeRemaining() {
TimerComponent timer = roundEntity.getComponent(TimerComponent.class);
return timer != null ? timer.timeRemaining : 0f;
Expand All @@ -81,7 +67,7 @@ public void onAnswerSubmitted(String answer) {
Player localPlayer = findLocalPlayer();
if (localPlayer != null) {
localPlayer.addScore(points);
submitScoreToFirebase(localPlayer.getId(), localPlayer.getScore(), localPlayer.getLastRoundScore());
submitScoreToFirebase(localPlayer.getId(), localPlayer.getScore());
}

playerAnswered = true;
Expand All @@ -93,7 +79,7 @@ public void onRoundExpired() {
if (!playerAnswered) {
Player localPlayer = findLocalPlayer();
if (localPlayer != null) {
submitScoreToFirebase(localPlayer.getId(), localPlayer.getScore(), localPlayer.getLastRoundScore());
submitScoreToFirebase(localPlayer.getId(), localPlayer.getScore());
}
recordAnswerInFirebase(); // count as answered so listener is consistent
}
Expand Down Expand Up @@ -126,7 +112,7 @@ private void startAllAnsweredListener() {
@Override
public void onCountChanged(int count) {
if (count >= totalPlayers) {
Gdx.app.postRunnable(() -> allAnswered = true);
Gdx.app.postRunnable(() -> transitionToLeaderboard());
}
}

Expand Down Expand Up @@ -170,15 +156,14 @@ private Player findLocalPlayer() {
return null;
}

private void submitScoreToFirebase(String playerId, int score, int roundScore) {
private void submitScoreToFirebase(String playerId, int score) {
String sessionId = game.getLocalSessionId();
if (sessionId == null || sessionId.isBlank()) return;

game.getFirebaseGateway().updatePlayerScore(
sessionId,
playerId,
score,
roundScore,
new FirebaseGateway.SimpleCallback() {
@Override public void onSuccess() {}
@Override public void onFailure(Exception exception) {
Expand Down Expand Up @@ -221,7 +206,6 @@ public void onPlayersChanged(List<Player> updatedPlayers) {
for (Player local : session.getPlayers()) {
if (local.getId().equals(updated.getId())) {
local.setScore(updated.getScore());
local.setLastRoundScore(updated.getLastRoundScore());
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Preferences;
import group07.beatbattle.BeatBattle;
import group07.beatbattle.utils.Strings;
import group07.beatbattle.i18n.Strings;
import group07.beatbattle.model.GameMode;
import group07.beatbattle.states.StartState;
import group07.beatbattle.states.StateManager;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,17 +67,14 @@ void deleteSession(

void listenToSessionState(String sessionId, SessionStateListener listener);

void updatePlayerScore(String sessionId, String playerId, int score, int roundScore, SimpleCallback callback);
void updatePlayerScore(String sessionId, String playerId, int score, SimpleCallback callback);

/** Records that a player has submitted an answer for the given round. */
void submitAnswer(String sessionId, int roundIndex, String playerId, SimpleCallback callback);

/** Fires with the current answer count whenever a new answer is submitted for a round. */
void listenToRoundAnswerCount(String sessionId, int roundIndex, AnswerCountCallback callback);

/** Clears Answers, resets all player scores to 0, and sets currentRound=0. Use before Play Again. */
void resetSessionForNewGame(String sessionId, SimpleCallback callback);

// --- Callbacks ---

interface GamePinExistsCallback {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ public void listenToSessionState(String sessionId, SessionStateListener listener
}

@Override
public void updatePlayerScore(String sessionId, String playerId, int score, int roundScore, SimpleCallback callback) {
public void updatePlayerScore(String sessionId, String playerId, int score, SimpleCallback callback) {
callback.onFailure(new UnsupportedOperationException("Firebase is not available on this platform"));
}

Expand All @@ -112,9 +112,4 @@ public void submitAnswer(String sessionId, int roundIndex, String playerId, Simp
public void listenToRoundAnswerCount(String sessionId, int roundIndex, AnswerCountCallback callback) {
callback.onFailure(new UnsupportedOperationException("Firebase is not available on this platform"));
}

@Override
public void resetSessionForNewGame(String sessionId, SimpleCallback callback) {
callback.onFailure(new UnsupportedOperationException("Firebase is not available on this platform"));
}
}
Loading

0 comments on commit 580dbd4

Please sign in to comment.