Fixes #15 #36

Merged
Persson-dev merged 13 commits from multiplayer into master 2025-02-01 12:43:14 +00:00
6 changed files with 20 additions and 17 deletions
Showing only changes of commit f22debdf5f - Show all commits

View File

@@ -37,10 +37,10 @@ public class Game {
leaderboard.add(player); leaderboard.add(player);
} }
public void setPlayerScore(Player player, int newScore) { public void setPlayerRemainingCells(Player player, int newScore) {
player.setScore(newScore); player.setRemainingCells(newScore);
Collections.sort(this.leaderboard, Collections.sort(this.leaderboard,
(player1, player2) -> Integer.compare(player1.getScore(), player2.getScore())); (player1, player2) -> Integer.compare(player1.getRemainingCells(), player2.getRemainingCells()));
} }
public void removePlayer(int id) { public void removePlayer(int id) {

View File

@@ -16,11 +16,11 @@ public class Player implements Serializable {
this.score = 0; this.score = 0;
} }
public int getScore() { public int getRemainingCells() {
return score; return score;
} }
void setScore(int score) { void setRemainingCells(int score) {
this.score = score; this.score = score;
} }

View File

@@ -22,9 +22,12 @@ public class LeaderboardRenderer {
private final ImVec4 cellColorEnemy = new ImVec4(1.0f, 0.0f, 0.0f, 0.5f); private final ImVec4 cellColorEnemy = new ImVec4(1.0f, 0.0f, 0.0f, 0.5f);
private final int maxPlayersShowed = 2; private final int maxPlayersShowed = 2;
private final int emptyCellCount;
public LeaderboardRenderer(Game game, Player player) { public LeaderboardRenderer(Game game, Player player) {
this.game = game; this.game = game;
this.currentPlayer = player; this.currentPlayer = player;
this.emptyCellCount = game.getDoku().getEmptyCells().size();
} }
private void renderRank(int rank) { private void renderRank(int rank) {
@@ -48,7 +51,7 @@ public class LeaderboardRenderer {
ImGui.sameLine(); ImGui.sameLine();
renderName(player.getPseudo()); renderName(player.getPseudo());
ImGui.sameLine(); ImGui.sameLine();
renderScore(player.getScore()); renderScore(emptyCellCount - player.getRemainingCells());
ImGui.endChild(); ImGui.endChild();
ImGui.popStyleColor(3); ImGui.popStyleColor(3);
} }

View File

@@ -21,8 +21,8 @@ public class MultiPlayerCompleteProgress {
public void render() { public void render() {
Player firstPlayer = game.getLeaderboard().get(0); Player firstPlayer = game.getLeaderboard().get(0);
ImGui.setCursorPosX(ImGui.getIO().getDisplaySizeX() / 2.0f - progressSize.x / 2.0f); ImGui.setCursorPosX(ImGui.getIO().getDisplaySizeX() / 2.0f - progressSize.x / 2.0f);
String progressText = firstPlayer.getPseudo() + " - " + firstPlayer.getScore() + "/" + emptyCellCount; String progressText = firstPlayer.getPseudo() + " - " + (emptyCellCount - firstPlayer.getRemainingCells()) + "/" + emptyCellCount;
this.progressBar.render(progressText, progressSize, 1.0f - firstPlayer.getScore() / (float) emptyCellCount); this.progressBar.render(progressText, progressSize, 1.0f - firstPlayer.getRemainingCells() / (float) emptyCellCount);
} }
} }

View File

@@ -86,7 +86,7 @@ public class ClientConnexion extends Connexion {
public void visitPacket(UpdatePlayerScorePacket packet) { public void visitPacket(UpdatePlayerScorePacket packet) {
Player player = this.client.getGame().getPlayerById(packet.getPlayerId()); Player player = this.client.getGame().getPlayerById(packet.getPlayerId());
assert (player != null); assert (player != null);
this.client.getGame().setPlayerScore(player, packet.getCellsLeft()); this.client.getGame().setPlayerRemainingCells(player, packet.getCellsLeft());
} }
@Override @Override

View File

@@ -63,7 +63,7 @@ public class ServerConnexion extends Connexion {
for (Player p : this.server.getGame().getPlayers().values()) { for (Player p : this.server.getGame().getPlayers().values()) {
if (p.getId() != player.getId()) { if (p.getId() != player.getId()) {
sendPacket(new PlayerJoinPacket(p)); sendPacket(new PlayerJoinPacket(p));
sendPacket(new UpdatePlayerScorePacket(p.getId(), p.getScore())); sendPacket(new UpdatePlayerScorePacket(p.getId(), p.getRemainingCells()));
} }
} }
@@ -139,20 +139,20 @@ public class ServerConnexion extends Connexion {
} }
if (cell.getSymbolIndex() != Cell.NOSYMBOL && packet.getNewValue() == Cell.NOSYMBOL) { if (cell.getSymbolIndex() != Cell.NOSYMBOL && packet.getNewValue() == Cell.NOSYMBOL) {
cell.empty(); cell.empty();
this.server.getGame().setPlayerScore(player, player.getScore() + 1); this.server.getGame().setPlayerRemainingCells(player, player.getRemainingCells() + 1);
this.server.broadcastPacket(new UpdatePlayerScorePacket(player.getId(), player.getScore())); this.server.broadcastPacket(new UpdatePlayerScorePacket(player.getId(), player.getRemainingCells()));
return; return;
} }
// on rajoute un chiffre à la grille // on rajoute un chiffre à la grille
if (cell.trySetValue(packet.getNewValue())) { if (cell.trySetValue(packet.getNewValue())) {
this.server.getGame().setPlayerScore(player, player.getScore() - 1); this.server.getGame().setPlayerRemainingCells(player, player.getRemainingCells() - 1);
this.server.broadcastPacket(new UpdatePlayerScorePacket(player.getId(), player.getScore())); this.server.broadcastPacket(new UpdatePlayerScorePacket(player.getId(), player.getRemainingCells()));
} }
checkWin(); checkWin();
} }
private void checkWin() { private void checkWin() {
if (this.player.getScore() == 0) { if (this.player.getRemainingCells() == 0) {
// we don't need to specify the winner since it has to be the first // we don't need to specify the winner since it has to be the first
this.server.broadcastPacket(new EndGamePacket()); this.server.broadcastPacket(new EndGamePacket());
this.server.getGame().stopGame(); this.server.getGame().stopGame();
@@ -162,8 +162,8 @@ public class ServerConnexion extends Connexion {
public void setSudoku(MultiDoku doku) { public void setSudoku(MultiDoku doku) {
this.doku = doku; this.doku = doku;
assert (player != null); assert (player != null);
this.server.getGame().setPlayerScore(player, this.doku.getEmptyCells().size()); this.server.getGame().setPlayerRemainingCells(player, this.doku.getEmptyCells().size());
this.server.broadcastPacket(new UpdatePlayerScorePacket(player.getId(), player.getScore())); this.server.broadcastPacket(new UpdatePlayerScorePacket(player.getId(), player.getRemainingCells()));
} }
} }