59 lines
2.4 KiB
Java
59 lines
2.4 KiB
Java
|
|
package app.playmancala.game;
|
||
|
|
|
||
|
|
import java.util.ArrayList;
|
||
|
|
import java.util.List;
|
||
|
|
import java.util.Map;
|
||
|
|
|
||
|
|
import app.playmancala.engine.MancalaGame;
|
||
|
|
import app.playmancala.model.GameClock;
|
||
|
|
import app.playmancala.model.GameUsersConnectionInfo;
|
||
|
|
import app.playmancala.model.PlayerSummary;
|
||
|
|
|
||
|
|
// A live game and the server-side state around it. Only touched on the game loop.
|
||
|
|
public class BackendGame {
|
||
|
|
public final String id;
|
||
|
|
public final MancalaGame mancalaGame;
|
||
|
|
public GameUsersConnectionInfo gameUsersConnectionInfo;
|
||
|
|
// Users watching right now.
|
||
|
|
public final List<String> spectatorIds = new ArrayList<>();
|
||
|
|
// Everyone who watched at some point, and the most at once.
|
||
|
|
public final List<String> allSpectatorIds;
|
||
|
|
public int peakSpectators;
|
||
|
|
// Keyed by user id, captured when the game is created.
|
||
|
|
public final Map<String, PlayerSummary> players;
|
||
|
|
public final long startedAt;
|
||
|
|
// Time of each move, aligned with mancalaGame.history.
|
||
|
|
public final List<Long> moveTimes;
|
||
|
|
public GameClock clock;
|
||
|
|
// The mover's clock after each move, aligned with mancalaGame.history; null for moves made before the game clock.
|
||
|
|
public final List<Long> moveRemainingMs;
|
||
|
|
|
||
|
|
public BackendGame(MancalaGame mancalaGame, GameUsersConnectionInfo gameUsersConnectionInfo,
|
||
|
|
List<String> allSpectatorIds, int peakSpectators, Map<String, PlayerSummary> players, long startedAt,
|
||
|
|
List<Long> moveTimes, GameClock clock, List<Long> moveRemainingMs) {
|
||
|
|
this.id = mancalaGame.getId();
|
||
|
|
this.mancalaGame = mancalaGame;
|
||
|
|
this.gameUsersConnectionInfo = gameUsersConnectionInfo;
|
||
|
|
this.allSpectatorIds = new ArrayList<>(allSpectatorIds);
|
||
|
|
this.peakSpectators = peakSpectators;
|
||
|
|
this.players = Map.copyOf(players);
|
||
|
|
this.startedAt = startedAt;
|
||
|
|
this.moveTimes = new ArrayList<>(moveTimes);
|
||
|
|
this.clock = clock;
|
||
|
|
this.moveRemainingMs = new ArrayList<>(moveRemainingMs);
|
||
|
|
}
|
||
|
|
|
||
|
|
public PlayerSummary player(String userId) {
|
||
|
|
PlayerSummary player = players.get(userId);
|
||
|
|
return player != null ? player : PlayerSummary.unknown(userId);
|
||
|
|
}
|
||
|
|
|
||
|
|
public boolean isPlayer(String userId) {
|
||
|
|
return mancalaGame.getPlayer1Id().equals(userId) || mancalaGame.getPlayer2Id().equals(userId);
|
||
|
|
}
|
||
|
|
|
||
|
|
public String opponentOf(String userId) {
|
||
|
|
return userId.equals(mancalaGame.getPlayer1Id()) ? mancalaGame.getPlayer2Id() : mancalaGame.getPlayer1Id();
|
||
|
|
}
|
||
|
|
}
|