feat: select game duration
All checks were successful
Linux arm64 / Build (push) Successful in 28s

This commit is contained in:
2025-02-01 12:27:50 +01:00
parent e98199e1ec
commit 02089c649b
9 changed files with 52 additions and 27 deletions

View File

@@ -15,13 +15,12 @@ public class Game {
GameNotStarted, GameGoing, GameEnd
}
public static final int GAME_DURATION = 10 * 60;
private final Map<Integer, Player> players;
private final List<Player> leaderboard;
private GameState gameState;
private MultiDoku doku;
private Instant startTime = null;
private long gameDuration;
public Game() {
this.players = new HashMap<>();
@@ -53,10 +52,15 @@ public class Game {
return players;
}
public void startGame(MultiDoku doku, Instant startTime) {
public void startGame(MultiDoku doku, Instant startTime, long gameDuration) {
this.doku = doku;
this.gameState = GameState.GameGoing;
this.startTime = startTime;
this.gameDuration = gameDuration;
}
public void stopGame() {
this.gameState = GameState.GameEnd;
}
public GameState getGameState() {
@@ -75,4 +79,8 @@ public class Game {
return startTime;
}
public long getGameDuration() {
return gameDuration;
}
}

View File

@@ -27,7 +27,7 @@ public class MultiPlayerDokuView extends BaseView {
this.leaderboardRenderer = new LeaderboardRenderer(client.getGame(), client.getPlayer());
this.sudokuRenderer.onCellChange.connect(this::onCellChange);
this.client.onDisconnect.connect(this::onDisconnect);
this.timerRenderer = new TimerRenderer(this.client.getGame().getStartTime(), Game.GAME_DURATION);
this.timerRenderer = new TimerRenderer(this.client.getGame().getStartTime(), this.client.getGame().getGameDuration());
this.completeProgress = new MultiPlayerCompleteProgress(this.client.getGame());
}

View File

@@ -3,6 +3,7 @@ package gui.menu;
import game.Player;
import gui.widget.SudokuSelector;
import imgui.ImGui;
import imgui.type.ImInt;
import network.client.Client;
import network.server.Server;
import sudoku.structure.MultiDoku;
@@ -14,6 +15,8 @@ public class MultiPlayerView extends BaseView {
private final SudokuSelector selector;
private ImInt gameDurationMinutes = new ImInt(10);
private MultiDoku doku = null;
public MultiPlayerView(StateMachine stateMachine, Client client, Server server) {
@@ -45,25 +48,32 @@ public class MultiPlayerView extends BaseView {
if (this.server == null) {
ImGui.text("En attente de l'administrateur du serveur ...");
} else {
if (this.doku == null)
ImGui.beginDisabled();
renderTimer();
ImGui.beginDisabled(this.doku == null);
if (ImGui.button("Démarrer")) {
this.server.startGame(this.doku);
this.server.startGame(this.doku, this.gameDurationMinutes.get() * 60);
}
if (this.doku == null)
ImGui.endDisabled();
selector.render();
}
}
@Override
public void render() {
private void renderPlayers() {
ImGui.text("Joueurs :");
{
for (Player player : this.client.getGame().getPlayers().values()) {
ImGui.bulletText(player.getPseudo());
}
}
}
private void renderTimer() {
ImGui.inputInt("Temps de la partie (minutes)", gameDurationMinutes);
}
@Override
public void render() {
renderPlayers();
renderGameStatus();
}

View File

@@ -8,7 +8,7 @@ public class TimerRenderer {
private final long endTime;
public TimerRenderer(Instant startTime, int duration) {
public TimerRenderer(Instant startTime, long duration) {
this.endTime = startTime.getEpochSecond() + duration;
}

View File

@@ -72,7 +72,7 @@ public class ClientConnexion extends Connexion {
@Override
public void visitPacket(StartGamePacket packet) {
this.client.getGame().startGame(SudokuSerializer.deserializeSudoku(packet.getSerializedSudoku()),
packet.getInstant());
packet.getInstant(), packet.getGameDuration());
this.client.onGameStarted.emit();
}

View File

@@ -8,15 +8,7 @@ public class EndGamePacket extends Packet {
static private final long serialVersionUID = Packets.EndGame.ordinal();
private final int winnerId;
public EndGamePacket(int winnerId) {
this.winnerId = winnerId;
}
public int getWinnerId() {
return winnerId;
}
public EndGamePacket() { }
@Override
public void accept(PacketVisitor packetVisitor) {

View File

@@ -13,10 +13,12 @@ public class StartGamePacket extends Packet {
private final String serializedSudoku;
// used to resume game
private final Instant instant;
private final long gameDuration;
public StartGamePacket(String serializedSudoku, Instant instant) {
public StartGamePacket(String serializedSudoku, Instant instant, long gameDuration) {
this.serializedSudoku = serializedSudoku;
this.instant = instant;
this.gameDuration = gameDuration;
}
public String getSerializedSudoku() {
@@ -27,6 +29,10 @@ public class StartGamePacket extends Packet {
return instant;
}
public long getGameDuration() {
return gameDuration;
}
@Override
public void accept(PacketVisitor packetVisitor) {
packetVisitor.visitPacket(this);

View File

@@ -69,13 +69,13 @@ public class Server {
return game;
}
public void startGame(MultiDoku doku) {
public void startGame(MultiDoku doku, long gameDuration) {
Instant now = Instant.now();
this.game.startGame(doku, now);
this.game.startGame(doku, now, gameDuration);
for (ServerConnexion connexion : this.connexions) {
connexion.setSudoku(doku.clone());
}
broadcastPacket(new StartGamePacket(SudokuSerializer.serializeSudoku(doku).toString(), now));
broadcastPacket(new StartGamePacket(SudokuSerializer.serializeSudoku(doku).toString(), now, gameDuration));
}
}

View File

@@ -76,7 +76,7 @@ public class ServerConnexion extends Connexion {
setSudoku(game.getDoku().clone());
sendPacket(
new StartGamePacket(SudokuSerializer.serializeSudoku(game.getDoku()).toString(),
game.getStartTime()));
game.getStartTime(), game.getGameDuration()));
}
}
@@ -148,6 +148,15 @@ public class ServerConnexion extends Connexion {
this.server.getGame().setPlayerScore(player, player.getScore() - 1);
this.server.broadcastPacket(new UpdatePlayerScorePacket(player.getId(), player.getScore()));
}
checkWin();
}
private void checkWin() {
if (this.player.getScore() == 0) {
// we don't need to specify the winner since it has to be the first
this.server.broadcastPacket(new EndGamePacket());
this.server.getGame().stopGame();
}
}
public void setSudoku(MultiDoku doku) {