From 49724892ea34d8eee909474a5748e17a6fd6d1d4 Mon Sep 17 00:00:00 2001 From: jbock Date: Tue, 6 Aug 2024 14:29:07 +0200 Subject: [PATCH] server-side color --- src/main/client/src/Game.jsx | 3 -- src/main/client/src/feature/GamePanel.jsx | 9 +---- src/main/java/com/bernd/GameController.java | 38 ++++++++++++++----- src/main/java/com/bernd/game/MoveList.java | 4 ++ .../java/com/bernd/model/GameBuilder.java | 18 +-------- src/main/java/com/bernd/model/Move.java | 4 ++ 6 files changed, 40 insertions(+), 36 deletions(-) diff --git a/src/main/client/src/Game.jsx b/src/main/client/src/Game.jsx index 0b99875..5a64fe9 100644 --- a/src/main/client/src/Game.jsx +++ b/src/main/client/src/Game.jsx @@ -12,7 +12,6 @@ import { import { StompContext, BLACK, - WHITE, TERRITORY, TERRITORY_B, REMOVED_B, @@ -44,7 +43,6 @@ export const Game = () => { let {gameId} = useParams() let stompClient = useContext(StompContext) let auth = useAuthStore(state => state.auth) - let black = useGameStore(state => state.black) let setGameState = useGameStore(state => state.setGameState) let queueStatus = useGameStore(state => state.queueStatus) let queueLength = useGameStore(state => state.queueLength) @@ -169,7 +167,6 @@ export const Game = () => { body: JSON.stringify({ id: gameId, n: queueLength(), - color: black.name === auth.name ? BLACK : WHITE, x: cursor_x, y: cursor_y, }), diff --git a/src/main/client/src/feature/GamePanel.jsx b/src/main/client/src/feature/GamePanel.jsx index f4a535a..e4ba171 100644 --- a/src/main/client/src/feature/GamePanel.jsx +++ b/src/main/client/src/feature/GamePanel.jsx @@ -51,11 +51,9 @@ function Panel({zoom, setZoom}) { let queueLength = useGameStore(state => state.queueLength) let counting = useGameStore(state => state.counting) let countingComplete = useGameStore(state => state.countingComplete) - let currentColor = useGameStore(state => state.currentColor) let currentPlayer = useGameStore(state => state.currentPlayer) let { board, gameHasEnded } = useGameStore(state => state.gameState) let navigate = useNavigate() - let myColor = black.name === auth.name ? BLACK : WHITE let onExit = useCallback(() => { navigate(base + "/lobby") }, [navigate]) @@ -64,7 +62,6 @@ function Panel({zoom, setZoom}) { destination: "/app/game/move", body: JSON.stringify({ id: gameId, - color: myColor, n: queueLength(), pass: true, }), @@ -75,23 +72,21 @@ function Panel({zoom, setZoom}) { destination: "/app/game/move", body: JSON.stringify({ id: gameId, - color: myColor, n: queueLength(), resetCounting: true, }), }) - }, [stompClient, gameId, queueLength, currentColor]) + }, [stompClient, gameId, queueLength]) let onCountingAgree = useCallback(() => { stompClient.publish({ destination: "/app/game/move", body: JSON.stringify({ id: gameId, - color: myColor, n: queueLength(), agreeCounting: true, }), }) - }, [stompClient, gameId, queueLength, currentColor]) + }, [stompClient, gameId, queueLength]) if (!board.length) { return Loading... } diff --git a/src/main/java/com/bernd/GameController.java b/src/main/java/com/bernd/GameController.java index 45056e1..79e28e4 100644 --- a/src/main/java/com/bernd/GameController.java +++ b/src/main/java/com/bernd/GameController.java @@ -1,9 +1,11 @@ package com.bernd; import com.bernd.game.Board; +import com.bernd.game.MoveList; import com.bernd.model.AcceptRequest; import com.bernd.model.ActiveGame; import com.bernd.model.Game; +import com.bernd.model.GameMove; import com.bernd.model.Move; import com.bernd.model.OpenGame; import com.bernd.model.ViewGame; @@ -51,25 +53,41 @@ public ViewGame getGame(@PathVariable String id) { } @MessageMapping("/game/move") - public void action(Move move, Principal principal) { + public void action(Move move, Principal p) { Game game = games.get(move.id()); if (game == null) { return; } - int moveNumber = game.moves().size(); - String currentPlayer = move.color() == Board.B ? game.black().name() : game.white().name(); - if (!principal.getName().equals(currentPlayer)) { - return; // discard - } - if (move.n() != moveNumber) { + String principal = p.getName(); + MoveList moves = game.moves(); + int moveNumber = moves.size(); + int color = principal.equals(game.black().name()) ? Board.B : + principal.equals(game.white().name()) ? Board.W : 0; + if (color == 0) { return; } - Game updated = game.update(move); + if (!game.counting()) { + if (moves.isEmpty()) { + if (color == Board.W) { + return; + } + } else { + GameMove lastMove = moves.get(moves.size() - 1); + if (lastMove.color() == color) { + return; + } + } + if (move.n() != moveNumber) { + return; + } + } + Move updatedMove = move.withColor(color); + Game updated = game.update(updatedMove); games.put(updated); if (updated.gameHasEnded()) { - operations.convertAndSend("/topic/move/" + game.id(), move.gameEnd(updated.counting())); + operations.convertAndSend("/topic/move/" + game.id(), updatedMove.gameEnd(updated.counting())); } else if (!move.agreeCounting()) { - operations.convertAndSend("/topic/move/" + game.id(), move.toView(updated.counting())); + operations.convertAndSend("/topic/move/" + game.id(), updatedMove.toView(updated.counting())); } } diff --git a/src/main/java/com/bernd/game/MoveList.java b/src/main/java/com/bernd/game/MoveList.java index 10d0ff2..411ef60 100644 --- a/src/main/java/com/bernd/game/MoveList.java +++ b/src/main/java/com/bernd/game/MoveList.java @@ -91,6 +91,10 @@ public GameMove get(int i) { } } + public boolean isEmpty() { + return pos == 0; + } + private void set(int ptId) { int i = pos / 2; if (pos % 2 == 0) { diff --git a/src/main/java/com/bernd/model/GameBuilder.java b/src/main/java/com/bernd/model/GameBuilder.java index 0f28548..03611c8 100644 --- a/src/main/java/com/bernd/model/GameBuilder.java +++ b/src/main/java/com/bernd/model/GameBuilder.java @@ -7,8 +7,6 @@ public final class GameBuilder { private int countingAgreed; private boolean opponentPassed; private int[][] board; - private int dim; - private int handicap; private int[] forbidden; private GameBuilder(Game game) { @@ -35,16 +33,6 @@ public GameBuilder withBoard(int[][] board) { return this; } - public GameBuilder withDim(int dim) { - this.dim = dim; - return this; - } - - public GameBuilder withHandicap(int handicap) { - this.handicap = handicap; - return this; - } - public GameBuilder withForbidden(int[] forbidden) { this.forbidden = forbidden; return this; @@ -60,8 +48,6 @@ static GameBuilder builder(Game game) { builder.countingAgreed = game.countingAgreed(); builder.opponentPassed = game.opponentPassed(); builder.board = game.board(); - builder.dim = game.dim(); - builder.handicap = game.handicap(); builder.forbidden = game.forbidden(); return builder; } @@ -75,8 +61,8 @@ public Game build() { countingAgreed, opponentPassed, board, - dim, - handicap, + game.dim(), + game.handicap(), forbidden, game.moves() ); diff --git a/src/main/java/com/bernd/model/Move.java b/src/main/java/com/bernd/model/Move.java index f90910d..99b9f14 100644 --- a/src/main/java/com/bernd/model/Move.java +++ b/src/main/java/com/bernd/model/Move.java @@ -17,4 +17,8 @@ public GameMove toView(boolean counting) { public GameMove gameEnd(boolean counting) { return new GameMove(n, color, pass, x, y, counting, resetCounting, true); } + + public Move withColor(int color) { + return new Move(id, color, n, pass, resetCounting, agreeCounting, x, y); + } }