feat: make timer stop game (Fixes #15)
All checks were successful
Linux arm64 / Build (push) Successful in 29s

This commit is contained in:
2025-02-01 13:41:13 +01:00
parent f22debdf5f
commit 352aee49e4
11 changed files with 109 additions and 11 deletions

View File

@@ -8,7 +8,9 @@ import java.util.List;
import game.Game;
import game.Player;
import game.Game.GameState;
import network.protocol.Packet;
import network.protocol.packets.EndGamePacket;
import network.protocol.packets.StartGamePacket;
import sudoku.io.SudokuSerializer;
import sudoku.structure.MultiDoku;
@@ -38,7 +40,16 @@ public class Server {
}
}
public void update() {
private void checkTimer() {
if (getGame() == null || getGame().getGameState() != GameState.GameGoing)
return;
long now = Instant.now().getEpochSecond();
long end = getGame().getStartTime().getEpochSecond() + getGame().getGameDuration();
if (now > end)
stopGame();
}
private void checkConnexions() {
for (var it = connexions.iterator(); it.hasNext();) {
ServerConnexion connexion = it.next();
if (!connexion.update()) {
@@ -49,6 +60,11 @@ public class Server {
}
}
public void update() {
checkTimer();
checkConnexions();
}
public void stop() {
this.acceptThread.cancel();
this.logicThread.cancel();
@@ -78,4 +94,10 @@ public class Server {
broadcastPacket(new StartGamePacket(SudokuSerializer.serializeSudoku(doku).toString(), now, gameDuration));
}
public void stopGame() {
// we don't need to specify the winner since it has to be the first
broadcastPacket(new EndGamePacket());
getGame().stopGame();
}
}

View File

@@ -153,9 +153,7 @@ public class ServerConnexion extends Connexion {
private void checkWin() {
if (this.player.getRemainingCells() == 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();
this.server.stopGame();
}
}

View File

@@ -19,7 +19,7 @@ public class ServerLogicThread extends Thread {
try {
Thread.sleep(50);
} catch (InterruptedException e) {
// e.printStackTrace();
e.printStackTrace();
break;
}
}