This commit is contained in:
@@ -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();
|
||||
}
|
||||
|
||||
|
||||
@@ -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) {
|
||||
|
||||
@@ -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);
|
||||
|
||||
@@ -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));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -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) {
|
||||
|
||||
Reference in New Issue
Block a user