feat: multi synced player scores
All checks were successful
Linux arm64 / Build (push) Successful in 31s

This commit is contained in:
2025-01-30 22:16:29 +01:00
parent bcded60fbe
commit 25c2270a37
19 changed files with 264 additions and 22 deletions

View File

@@ -6,14 +6,19 @@ import java.net.Socket;
import game.Player;
import game.Game.GameState;
import network.Connexion;
import network.protocol.packets.ChangeCellPacket;
import network.protocol.packets.ConnexionInfoPacket;
import network.protocol.packets.DisconnectPacket;
import network.protocol.packets.EndGamePacket;
import network.protocol.packets.KeepAlivePacket;
import network.protocol.packets.LoginPacket;
import network.protocol.packets.PlayerJoinPacket;
import network.protocol.packets.PlayerLeavePacket;
import network.protocol.packets.StartGamePacket;
import network.protocol.packets.UpdatePlayerScorePacket;
import sudoku.io.SudokuSerializer;
import sudoku.structure.Cell;
import sudoku.structure.MultiDoku;
public class ServerConnexion extends Connexion {
@@ -21,6 +26,7 @@ public class ServerConnexion extends Connexion {
private final KeepAliveHandler keepAliveHandler;
private boolean shouldClose = false;
private Player player = null;
private MultiDoku doku;
public ServerConnexion(Socket socket, Server server) throws IOException {
super(socket);
@@ -44,7 +50,7 @@ public class ServerConnexion extends Connexion {
@Override
public synchronized void close() {
if(shouldClose)
if (shouldClose)
return;
super.close();
shouldClose = true;
@@ -54,13 +60,19 @@ public class ServerConnexion extends Connexion {
private void finishLogin() {
// send players that have already joined (excluding this one)
for (Player p : this.server.getGame().getPlayers().values()) {
if (p.getId() != player.getId())
if (p.getId() != player.getId()) {
sendPacket(new PlayerJoinPacket(p));
sendPacket(new UpdatePlayerScorePacket(p.getId(), p.getScore()));
}
}
this.server.broadcastPacket(new PlayerJoinPacket(player));
sendPacket(new ConnexionInfoPacket(player.getId()));
if (this.server.getGame().getGameState() == GameState.GameGoing) {
sendPacket(new StartGamePacket(SudokuSerializer.serializeSudoku(this.server.getGame().getDoku()).toString()));
setSudoku(this.server.getGame().getDoku());
sendPacket(
new StartGamePacket(SudokuSerializer.serializeSudoku(this.server.getGame().getDoku()).toString()));
}
}
@@ -102,4 +114,43 @@ public class ServerConnexion extends Connexion {
throw new UnsupportedOperationException("Unimplemented method 'visitPacketStartGame'");
}
@Override
public void visitPacket(EndGamePacket packet) {
throw new UnsupportedOperationException("Unimplemented method 'visitPacket'");
}
@Override
public void visitPacket(UpdatePlayerScorePacket packet) {
throw new UnsupportedOperationException("Unimplemented method 'visitPacket'");
}
@Override
public void visitPacket(ChangeCellPacket packet) {
Cell cell = this.doku.getSubGrid(packet.getSudokuIndex()).getCell(packet.getCellIndex());
if (cell.getSymbolIndex() == Cell.NOSYMBOL && packet.getNewValue() == Cell.NOSYMBOL)
return;
if (cell.getSymbolIndex() != Cell.NOSYMBOL && packet.getNewValue() != Cell.NOSYMBOL) {
cell.trySetValue(packet.getNewValue());
return;
}
if (cell.getSymbolIndex() != Cell.NOSYMBOL && packet.getNewValue() == Cell.NOSYMBOL) {
cell.trySetValue(Cell.NOSYMBOL);
player.setScore(player.getScore() + 1);
sendPacket(new UpdatePlayerScorePacket(player.getId(), player.getScore()));
return;
}
// on rajoute un chiffre à la grille
if (cell.trySetValue(packet.getNewValue())) {
player.setScore(player.getScore() - 1);
sendPacket(new UpdatePlayerScorePacket(player.getId(), player.getScore()));
}
}
public void setSudoku(MultiDoku doku) {
this.doku = doku;
assert (player != null);
player.setScore(this.doku.getEmptyCells().size());
sendPacket(new UpdatePlayerScorePacket(player.getId(), player.getScore()));
}
}