diff --git a/framework/src/main/java/org/toop/framework/networking/server/OnlineTurnBasedGame.java b/framework/src/main/java/org/toop/framework/networking/server/OnlineTurnBasedGame.java index 9367c9a..dd29447 100644 --- a/framework/src/main/java/org/toop/framework/networking/server/OnlineTurnBasedGame.java +++ b/framework/src/main/java/org/toop/framework/networking/server/OnlineTurnBasedGame.java @@ -5,18 +5,21 @@ import org.toop.framework.gameFramework.GameState; import org.toop.framework.gameFramework.model.game.TurnBasedGame; import org.toop.framework.networking.server.client.NettyClient; +import java.util.ArrayList; +import java.util.Arrays; import java.util.concurrent.CompletableFuture; public class OnlineTurnBasedGame implements OnlineGame { private long id; private NettyClient[] clients; + private NettyClient[] admins; private TurnBasedGame game; private ServerThreadBehaviour gameThread; - private CompletableFuture futureOrNull = null; + private final CompletableFuture futureOrNull; - public OnlineTurnBasedGame(TurnBasedGame game, CompletableFuture futureOrNull, NettyClient... clients) { + public OnlineTurnBasedGame(NettyClient[] admins, TurnBasedGame game, CompletableFuture futureOrNull, NettyClient... clients) { this.game = game; this.gameThread = new ServerThreadBehaviour( game, @@ -25,37 +28,40 @@ public class OnlineTurnBasedGame implements OnlineGame { ); this.futureOrNull = futureOrNull; this.clients = clients; + this.admins = admins; } - public OnlineTurnBasedGame(TurnBasedGame game, NettyClient... clients) { - this.game = game; - this.gameThread = new ServerThreadBehaviour( - game, - (pair) -> notifyMoveMade(pair.getLeft(), pair.getRight()), - (pair) -> notifyGameEnd(pair.getLeft(), pair.getRight()) - ); - this.clients = clients; + public OnlineTurnBasedGame(NettyClient[] admins, TurnBasedGame game, NettyClient... clients) { + this(admins, game, null, clients); } private void notifyMoveMade(String speler, int move){ + for (NettyClient admin : admins) { + admin.send(String.format("SVR GAME MOVE {PLAYER: \"%s\", MOVE: \"%s\", DETAILS: \"\"}", speler, move)); + } for (NettyClient client : clients) { - client.send(String.format("SVR GAME MOVE {PLAYER: \"%s\", MOVE: \"%s\", DETAILS: \"\"}\n", speler, move)); + client.send(String.format("SVR GAME MOVE {PLAYER: \"%s\", MOVE: \"%s\", DETAILS: \"\"}", speler, move)); } } private void notifyGameEnd(GameState state, int winner){ - if (state == GameState.DRAW){ + if (state == GameState.DRAW) { + Arrays.stream(admins).forEach(a -> a.send( + String.format("SVR GAME END") + )); + for (NettyClient client : clients) { - client.send(String.format("SVR GAME DRAW {PLAYERONESCORE: \"\", PLAYERTWOSCORE: \"\", COMMENT: \"\"}\n")); + client.send(String.format("SVR GAME DRAW {PLAYERONESCORE: \"\", PLAYERTWOSCORE: \"\", COMMENT: \"\"}")); } - } - else{ - clients[winner].send(String.format("SVR GAME WIN {PLAYERONESCORE: \"\", PLAYERTWOSCORE: \"\", COMMENT: \"\"}\n")); - clients[(winner + 1)%2].send(String.format("SVR GAME LOSS {PLAYERONESCORE: \"\", PLAYERTWOSCORE: \"\", COMMENT: \"\"}\n")); + } else { + Arrays.stream(admins).forEach(a -> a.send("SVR GAME END")); + clients[winner].send(String.format("SVR GAME WIN {PLAYERONESCORE: \"\", PLAYERTWOSCORE: \"\", COMMENT: \"\"}")); + clients[1-winner].send(String.format("SVR GAME LOSS {PLAYERONESCORE: \"\", PLAYERTWOSCORE: \"\", COMMENT: \"\"}")); } - // Remove game fromt clients - for(NettyClient client : clients) { + // Remove game from clients + for (NettyClient client : clients) { + admins = null; client.clearGame(); } diff --git a/framework/src/main/java/org/toop/framework/networking/server/Server.java b/framework/src/main/java/org/toop/framework/networking/server/Server.java index 0ff3699..88d18c4 100644 --- a/framework/src/main/java/org/toop/framework/networking/server/Server.java +++ b/framework/src/main/java/org/toop/framework/networking/server/Server.java @@ -137,7 +137,7 @@ public class Server implements GameServer { try { ServerPlayer[] players = new ServerPlayer[clients.length]; - var game = new OnlineTurnBasedGame(gameTypesStore.create(gameType), futureOrNull, clients); + var game = new OnlineTurnBasedGame(getAdmins().toArray(NettyClient[]::new), gameTypesStore.create(gameType), futureOrNull, clients); for (int i = 0; i < clients.length; i++) { players[i] = new ServerPlayer(clients[i]);