3 Commits

Author SHA1 Message Date
lieght
6e2ea82a32 UI fixes after game end 2025-12-14 13:30:21 +01:00
lieght
34c85ec472 Removed user from subscription if in a game 2025-12-14 13:11:55 +01:00
2d9b34b7f6 Quick fix so more than one game can be played in succession 2025-12-14 11:36:51 +01:00
5 changed files with 29 additions and 17 deletions

View File

@@ -184,7 +184,7 @@ public final class Server {
gameController = null;
//if (!isPolling) return;
// if (!isPolling) return;
String gameType = extractQuotedValue(response.gameType());
if (response.clientId() == clientId) {
@@ -235,8 +235,9 @@ public final class Server {
}
if (gameController != null) {
primary.reEnableButton();
primary.reEnableButton(); // Re enable subscribe button
gameController.start();
isPolling = true; // Fixes server getting stuck
}
}
}

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

@@ -128,21 +128,15 @@ public class Server implements GameServer<TurnBasedGame, NettyClient, Long> {
@Override
public void startGame(String gameType, NettyClient... clients) {
IO.println("------------------------------------------");
IO.println("USERS: " + clients.length + " " + Arrays.stream(clients).toList().toString());
if (!gameTypesStore.all().containsKey(gameType)) return;
IO.println("------------------------------------------");
try {
ServerPlayer[] players = new ServerPlayer[clients.length];
var game = new OnlineTurnBasedGame(gameTypesStore.create(gameType), clients);
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");
@@ -216,8 +210,23 @@ public class Server implements GameServer<TurnBasedGame, NettyClient, Long> {
String userLeft = userNames.get(left);
String userRight = userNames.get(right);
// Remove user if he is in a game
boolean userInGame = false;
if (getUser(userLeft).game() != null) {
userNames.remove(userLeft);
userInGame = true;
} else if (getUser(userRight).game() != null) {
userNames.remove(userRight);
userInGame = true;
}
if (userInGame) { continue; }
//
int first = Math.max(left, right);
int second = Math.min(left, right);
userNames.remove(first);
userNames.remove(second);

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