Skip to content

Commit

Permalink
fix server side color logic
Browse files Browse the repository at this point in the history
  • Loading branch information
h908714124 committed Aug 9, 2024
1 parent 6ce84ad commit a939776
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 22 deletions.
5 changes: 4 additions & 1 deletion src/main/client/src/Game.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,10 @@ import {
} from "react"
import {
useParams,
useNavigate,
} from "react-router-dom"
import {
base,
StompContext,
BLACK,
TERRITORY,
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -208,7 +211,7 @@ export const Game = () => {
},
})
setGameState(game)
})
}, () => navigate(base + "/lobby"))
}, [setGameState, queueStatus, auth, gameId])

useEffect(() => {
Expand Down
2 changes: 2 additions & 0 deletions src/main/client/src/feature/GamePanel.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -174,9 +174,11 @@ function Panel({zoom, setZoom}) {
</div>
</div>
)}
{/*
<div className="absolute bottom-10 pr-2">
<GameChat />
</div>
*/}
</>
)
}
Expand Down
36 changes: 15 additions & 21 deletions src/main/java/com/bernd/GameController.java
Original file line number Diff line number Diff line change
Expand Up @@ -66,50 +66,44 @@ 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;
}
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
Expand Down

0 comments on commit a939776

Please sign in to comment.