Quick fix so more than one game can be played in succession

This commit is contained in:
2025-12-14 11:36:51 +01:00
parent 8867d5a1ea
commit 2d9b34b7f6
4 changed files with 12 additions and 9 deletions

View File

@@ -31,14 +31,18 @@ public class OnlineTurnBasedGame implements OnlineGame<TurnBasedGame> {
private void notifyGameEnd(GameState state, int winner){
if (state == GameState.DRAW){
for (NettyClient client : clients) {
client.send(String.format("SVR GAME DRAW {PLAYERONESCORE: \"<score speler1>\", PLAYERTWOSCORE: \"<score speler2>\", COMMENT: \"NettyClient disconnected\"}\n"));
client.send(String.format("SVR GAME DRAW {PLAYERONESCORE: \"<score speler1>\", PLAYERTWOSCORE: \"<score speler2>\", COMMENT: \"<comment>\"}\n"));
}
}
else{
clients[winner].send(String.format("SVR GAME WIN {PLAYERONESCORE: \"<score speler1>\", PLAYERTWOSCORE: \"<score speler2>\", COMMENT: \"NettyClient disconnected\"}\n"));
clients[(winner + 1)%2].send(String.format("SVR GAME LOSS {PLAYERONESCORE: \"<score speler1>\", PLAYERTWOSCORE: \"<score speler2>\", COMMENT: \"NettyClient disconnected\"}\n"));
clients[winner].send(String.format("SVR GAME WIN {PLAYERONESCORE: \"<score speler1>\", PLAYERTWOSCORE: \"<score speler2>\", COMMENT: \"<comment>\"}\n"));
clients[(winner + 1)%2].send(String.format("SVR GAME LOSS {PLAYERONESCORE: \"<score speler1>\", PLAYERTWOSCORE: \"<score speler2>\", COMMENT: \"<comment>\"}\n"));
}
// Remove game fromt clients
for(NettyClient client : clients) {
client.clearGame();
}
}
@Override

View File

@@ -142,7 +142,7 @@ public class Server implements GameServer<TurnBasedGame, NettyClient, Long> {
for (int i = 0; i < clients.length; i++) {
players[i] = new ServerPlayer(clients[i]);
clients[i].addGame(new ImmutablePair<>(game, players[i]));
clients[i].setGame(new ImmutablePair<>(game, players[i]));
}
System.out.println("Starting OnlineTurnBasedGame");
@@ -207,6 +207,7 @@ public class Server implements GameServer<TurnBasedGame, NettyClient, Long> {
if (userNames.size() < 2) continue;
while (userNames.size() > 1) {
// TODO: Make sure users aren't currently in a game.
int left = ran.nextInt(userNames.size());
int right;
do {

View File

@@ -12,7 +12,7 @@ public interface Client<G, P> {
OnlineTurnBasedGame game();
P player();
void addGame(Pair<G, P> gamePair);
void setGame(Pair<G, P> gamePair);
void clearGame();
void send(String message);

View File

@@ -27,10 +27,8 @@ public class NettyClient implements Client<OnlineTurnBasedGame, ServerPlayer> {
}
@Override
public void addGame(Pair<OnlineTurnBasedGame, ServerPlayer> gamePair) {
if (this.gamePair == null) {
this.gamePair = gamePair;
}
public void setGame(Pair<OnlineTurnBasedGame, ServerPlayer> gamePair) {
this.gamePair = gamePair;
}
@Override