2026-10-16 11:23:35 +03:00
|
|
|
package app.playmancala.game;
|
|
|
|
|
|
|
|
|
|
import java.util.ArrayList;
|
|
|
|
|
import java.util.List;
|
|
|
|
|
import java.util.Map;
|
|
|
|
|
|
2026-10-16 14:18:45 +03:00
|
|
|
import app.playmancala.bot.BotSettings;
|
2026-10-16 11:23:35 +03:00
|
|
|
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;
|
2026-10-16 14:18:45 +03:00
|
|
|
// How the bot plays in a game against the bot; null in games between people.
|
|
|
|
|
public final BotSettings bot;
|
2026-10-16 11:23:35 +03:00
|
|
|
|
|
|
|
|
public BackendGame(MancalaGame mancalaGame, GameUsersConnectionInfo gameUsersConnectionInfo,
|
|
|
|
|
List<String> allSpectatorIds, int peakSpectators, Map<String, PlayerSummary> players, long startedAt,
|
2026-10-16 14:18:45 +03:00
|
|
|
List<Long> moveTimes, GameClock clock, List<Long> moveRemainingMs, BotSettings bot) {
|
2026-10-16 11:23:35 +03:00
|
|
|
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);
|
2026-10-16 14:18:45 +03:00
|
|
|
this.bot = bot;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public boolean isBotTurn() {
|
|
|
|
|
return bot != null && bot.botId().equals(mancalaGame.getTurnPlayerId());
|
2026-10-16 11:23:35 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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();
|
|
|
|
|
}
|
|
|
|
|
}
|