From a939776384ec04c16cc1cd061a1a8a149e829a58 Mon Sep 17 00:00:00 2001 From: jbock Date: Fri, 9 Aug 2024 12:45:18 +0200 Subject: [PATCH] fix server side color logic --- src/main/client/src/Game.jsx | 5 ++- src/main/client/src/feature/GamePanel.jsx | 2 ++ src/main/java/com/bernd/GameController.java | 36 +++++++++------------ 3 files changed, 21 insertions(+), 22 deletions(-) diff --git a/src/main/client/src/Game.jsx b/src/main/client/src/Game.jsx index e1cb49f..14f1424 100644 --- a/src/main/client/src/Game.jsx +++ b/src/main/client/src/Game.jsx @@ -8,8 +8,10 @@ import { } from "react" import { useParams, + useNavigate, } from "react-router-dom" import { + base, StompContext, BLACK, TERRITORY, @@ -39,6 +41,7 @@ export const Game = () => { let [cursor_y, setCursor_y] = useState(-1) let [zoom, setZoom] = useState(0) let {gameId} = useParams() + let navigate = useNavigate() let stompClient = useContext(StompContext) let auth = useAuthStore(state => state.auth) let lastMove = useGameStore(state => state.lastMove) @@ -208,7 +211,7 @@ export const Game = () => { }, }) setGameState(game) - }) + }, () => navigate(base + "/lobby")) }, [setGameState, queueStatus, auth, gameId]) useEffect(() => { diff --git a/src/main/client/src/feature/GamePanel.jsx b/src/main/client/src/feature/GamePanel.jsx index 2ccfebf..a0ed055 100644 --- a/src/main/client/src/feature/GamePanel.jsx +++ b/src/main/client/src/feature/GamePanel.jsx @@ -174,9 +174,11 @@ function Panel({zoom, setZoom}) { )} + {/*
+ */} ) } diff --git a/src/main/java/com/bernd/GameController.java b/src/main/java/com/bernd/GameController.java index 422a834..0e3e6f3 100644 --- a/src/main/java/com/bernd/GameController.java +++ b/src/main/java/com/bernd/GameController.java @@ -66,28 +66,29 @@ public void action(Move move, Principal p) { if (p == null || game == null) { return; } - int color = getCurrentColor(game, getPrincipal(p)); - if (color == 0) { + int principalColor = getColorFromPrincipal(game, getPrincipal(p)); + int color = getColorFromGameState(game); + if (color == 0 + || principalColor == 0 + || color != principalColor && !game.counting() && !game.isSelfPlay()) { return; } - Move updatedMove = move.withColor(color).withMoveNumber(game.moves().size()); + Move updatedMove = move + .withColor(game.isSelfPlay() ? color : principalColor) + .withMoveNumber(game.moves().size()); Game updated = game.update(updatedMove); games.put(updated); GameMove lastMove = game.getLastMove(); operations.convertAndSend("/topic/move/" + game.id(), lastMove); } - private int getCurrentColor(Game game, String principal) { + private int getColorFromGameState(Game game) { if (game.gameHasEnded()) { return 0; } - if (game.remainingHandicap() != 0) { + if (game.remainingHandicap() > 0) { return Board.B; } - int color = getColor(game, principal); - if (color == 0) { - return 0; - } MoveList moves = game.moves(); if (moves.isEmpty()) { return Board.B; @@ -95,21 +96,14 @@ private int getCurrentColor(Game game, String principal) { return moves.get(moves.size() - 1).color() ^ COLORS; } - private static int getColor(Game game, String principal) { - if (!(game.isBlack(principal) || game.isWhite(principal))) { - return 0; - } - if (game.remainingHandicap() > 0) { - if (!game.isBlack(principal)) { - return 0; - } + private static int getColorFromPrincipal(Game game, String principal) { + if (game.isBlack(principal)) { return Board.B; } - if (game.isSelfPlay()) { - return game.moves().size() + game.remainingHandicap() % 2 == 0 ? - Board.B : Board.W; + if (game.isWhite(principal)) { + return Board.W; } - return game.isBlack(principal) ? Board.B : Board.W; + return 0; } @ResponseBody