Skip to content

fix: redirect players if host leaves #48

Closed
wants to merge 1 commit into from
Closed
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 @@ -346,7 +346,10 @@ public void listenToSessionState(
listener.onFailure(error);
return;
}
if (snapshot == null || !snapshot.exists()) return;
if (snapshot == null || !snapshot.exists()) {
listener.onSessionDeleted();
return;
}
String state = snapshot.getString("state");
Long roundLong = snapshot.getLong("currentRound");
int currentRound = roundLong != null ? roundLong.intValue() : 0;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,15 @@ private void launchNextRound() {
}

public void onBackToMenu() {
if (game.isLocalHost()) {
String sessionId = game.getLocalSessionId();
if (sessionId != null && !sessionId.isBlank()) {
game.getFirebaseGateway().deleteSession(sessionId, new FirebaseGateway.DeleteSessionCallback() {
@Override public void onSuccess() {}
@Override public void onFailure(Exception e) {}
});
}
}
StateManager.getInstance().setState(new StartState(game, new LobbyController(game)));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
import java.util.List;

import group07.beatbattle.BeatBattle;
import group07.beatbattle.ecs.Engine;
import group07.beatbattle.ecs.systems.AudioSystem;
import group07.beatbattle.firebase.FirebaseGateway;
import group07.beatbattle.model.GameMode;
import group07.beatbattle.model.GameSession;
Expand Down Expand Up @@ -289,6 +291,15 @@ public void onStateChanged(String state, int currentRound) {
}
}

@Override
public void onSessionDeleted() {
Gdx.app.postRunnable(() -> {
AudioSystem.getInstance().stopAll();
Engine.getInstance().reset();
StateManager.getInstance().setState(new StartState(game, new LobbyController(game)));
});
}

@Override
public void onFailure(Exception exception) {
Gdx.app.error("LobbyController",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,16 @@ public void onRoundExpired() {
public void onLeaveSession() {
AudioSystem.getInstance().stop(roundEntity);
Engine.getInstance().removeEntity(roundEntity);
if (game.isLocalHost()) {
String sessionId = game.getLocalSessionId();
if (sessionId != null && !sessionId.isBlank()) {
game.getFirebaseGateway().deleteSession(sessionId, new FirebaseGateway.DeleteSessionCallback() {
@Override public void onSuccess() {}
@Override public void onFailure(Exception e) {}
});
}
}

StateManager.getInstance().setState(new StartState(game, new LobbyController(game)));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,15 @@ public boolean isMuted() {
return muted;
}

/**
* Stops any currently playing audio without needing a specific entity reference.
* Used when a joiner is redirected to the start screen mid-round because the
* host left
*/
public void stopAll() {
if (audioPlayer != null) audioPlayer.stop();
}

public void dispose() {
if (audioPlayer != null) audioPlayer.dispose();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,10 @@ interface SessionStateListener {
* @param currentRound current round index stored in Firebase */
void onStateChanged(String state, int currentRound);
void onFailure(Exception exception);
/**
* Called when the session document is deleted from Firestore. If host leaves.
*/
default void onSessionDeleted() {}
}

interface AnswerCountCallback {
Expand Down
11 changes: 11 additions & 0 deletions core/src/main/java/group07/beatbattle/view/LobbyView.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@
import group07.beatbattle.model.GameMode;
import group07.beatbattle.model.Player;
import group07.beatbattle.model.services.LobbyService;
import group07.beatbattle.states.StartState;
import group07.beatbattle.states.StateManager;
import group07.beatbattle.ui.components.BackButton;
import group07.beatbattle.ui.components.RoundSelector;
import group07.beatbattle.ui.components.SettingsButton;
Expand Down Expand Up @@ -165,6 +167,15 @@ public void onFailure(Exception exception) {
);
}
}

@Override
public void onSessionDeleted() {
Gdx.app.postRunnable(() -> {
if (!leavingLobby && !gameStarted) {
StateManager.getInstance().setState(new StartState(game, new LobbyController(game)));
}
});
}

@Override
public void onFailure(Exception exception) {
Expand Down
Loading