Skip to content

Commit a939776

Browse files
committed
fix server side color logic
1 parent 6ce84ad commit a939776

File tree

3 files changed

+21
-22
lines changed

3 files changed

+21
-22
lines changed

src/main/client/src/Game.jsx

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,10 @@ import {
88
} from "react"
99
import {
1010
useParams,
11+
useNavigate,
1112
} from "react-router-dom"
1213
import {
14+
base,
1315
StompContext,
1416
BLACK,
1517
TERRITORY,
@@ -39,6 +41,7 @@ export const Game = () => {
3941
let [cursor_y, setCursor_y] = useState(-1)
4042
let [zoom, setZoom] = useState(0)
4143
let {gameId} = useParams()
44+
let navigate = useNavigate()
4245
let stompClient = useContext(StompContext)
4346
let auth = useAuthStore(state => state.auth)
4447
let lastMove = useGameStore(state => state.lastMove)
@@ -208,7 +211,7 @@ export const Game = () => {
208211
},
209212
})
210213
setGameState(game)
211-
})
214+
}, () => navigate(base + "/lobby"))
212215
}, [setGameState, queueStatus, auth, gameId])
213216

214217
useEffect(() => {

src/main/client/src/feature/GamePanel.jsx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,9 +174,11 @@ function Panel({zoom, setZoom}) {
174174
</div>
175175
</div>
176176
)}
177+
{/*
177178
<div className="absolute bottom-10 pr-2">
178179
<GameChat />
179180
</div>
181+
*/}
180182
</>
181183
)
182184
}

src/main/java/com/bernd/GameController.java

Lines changed: 15 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -66,50 +66,44 @@ public void action(Move move, Principal p) {
6666
if (p == null || game == null) {
6767
return;
6868
}
69-
int color = getCurrentColor(game, getPrincipal(p));
70-
if (color == 0) {
69+
int principalColor = getColorFromPrincipal(game, getPrincipal(p));
70+
int color = getColorFromGameState(game);
71+
if (color == 0
72+
|| principalColor == 0
73+
|| color != principalColor && !game.counting() && !game.isSelfPlay()) {
7174
return;
7275
}
73-
Move updatedMove = move.withColor(color).withMoveNumber(game.moves().size());
76+
Move updatedMove = move
77+
.withColor(game.isSelfPlay() ? color : principalColor)
78+
.withMoveNumber(game.moves().size());
7479
Game updated = game.update(updatedMove);
7580
games.put(updated);
7681
GameMove lastMove = game.getLastMove();
7782
operations.convertAndSend("/topic/move/" + game.id(), lastMove);
7883
}
7984

80-
private int getCurrentColor(Game game, String principal) {
85+
private int getColorFromGameState(Game game) {
8186
if (game.gameHasEnded()) {
8287
return 0;
8388
}
84-
if (game.remainingHandicap() != 0) {
89+
if (game.remainingHandicap() > 0) {
8590
return Board.B;
8691
}
87-
int color = getColor(game, principal);
88-
if (color == 0) {
89-
return 0;
90-
}
9192
MoveList moves = game.moves();
9293
if (moves.isEmpty()) {
9394
return Board.B;
9495
}
9596
return moves.get(moves.size() - 1).color() ^ COLORS;
9697
}
9798

98-
private static int getColor(Game game, String principal) {
99-
if (!(game.isBlack(principal) || game.isWhite(principal))) {
100-
return 0;
101-
}
102-
if (game.remainingHandicap() > 0) {
103-
if (!game.isBlack(principal)) {
104-
return 0;
105-
}
99+
private static int getColorFromPrincipal(Game game, String principal) {
100+
if (game.isBlack(principal)) {
106101
return Board.B;
107102
}
108-
if (game.isSelfPlay()) {
109-
return game.moves().size() + game.remainingHandicap() % 2 == 0 ?
110-
Board.B : Board.W;
103+
if (game.isWhite(principal)) {
104+
return Board.W;
111105
}
112-
return game.isBlack(principal) ? Board.B : Board.W;
106+
return 0;
113107
}
114108

115109
@ResponseBody

0 commit comments

Comments
 (0)