Skip to content

Commit 727b348

Browse files
committed
prevent agree to count twice
1 parent 73a1e92 commit 727b348

File tree

15 files changed

+83
-87
lines changed

15 files changed

+83
-87
lines changed

src/main/client/src/Game.jsx

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -47,12 +47,12 @@ export const Game = () => {
4747
let lastMove = useGameStore(state => state.lastMove)
4848
let setGameState = useGameStore(state => state.setGameState)
4949
let queueStatus = useGameStore(state => state.queueStatus)
50+
let movesLength = useGameStore(state => state.moves.length)
5051
let addMove = useGameStore(state => state.addMove)
5152
let currentPlayer = useGameStore(state => state.currentPlayer)
5253
let counting = useGameStore(state => state.counting)
5354
let currentColor = useGameStore(state => state.currentColor)
5455
let {board, forbidden: [forbidden_x, forbidden_y], gameHasEnded} = useGameStore(state => state.gameState)
55-
let initialized = useRef()
5656
let canvasRef = useRef()
5757
let countingGroup = !gameHasEnded && counting ? getCountingGroup(board, cursor_x, cursor_y) : undefined
5858

@@ -161,7 +161,7 @@ export const Game = () => {
161161
stompClient.publish({
162162
destination: "/app/game/move",
163163
body: JSON.stringify({
164-
id: gameId,
164+
n: movesLength,
165165
x: cursor_x,
166166
y: cursor_y,
167167
}),
@@ -215,16 +215,12 @@ export const Game = () => {
215215
}, [setGameState, queueStatus, auth, gameId, navigate])
216216

217217
useEffect(() => {
218-
if (initialized.current) {
219-
return
220-
}
221-
initialized.current = true
222218
let sub = stompClient.subscribe("/topic/move/" + gameId, (message) => {
223219
let move = JSON.parse(message.body)
224220
addMove(move)
225221
})
226222
return sub.unsubscribe
227-
}, [setGameState, addMove, initialized, stompClient, gameId, auth])
223+
}, [setGameState, addMove, stompClient, gameId, auth])
228224

229225
if (!board.length) {
230226
return <div>Loading...</div>

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

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,10 @@ function Panel({zoom, setZoom}) {
5252
let black = useGameStore(state => state.black)
5353
let white = useGameStore(state => state.white)
5454
let queueLength = useGameStore(state => state.queueLength)
55+
let movesLength = useGameStore(state => state.moves.length)
5556
let counting = useGameStore(state => state.counting)
57+
let agreeCounting = useGameStore(state => state.agreeCounting)
58+
let setAgreeCounting = useGameStore(state => state.setAgreeCounting)
5659
let countingComplete = useGameStore(state => state.countingComplete)
5760
let currentPlayer = useGameStore(state => state.currentPlayer)
5861
let { board, gameHasEnded } = useGameStore(state => state.gameState)
@@ -64,29 +67,30 @@ function Panel({zoom, setZoom}) {
6467
stompClient.publish({
6568
destination: "/app/game/move",
6669
body: JSON.stringify({
67-
id: gameId,
68-
pass: true,
70+
n: movesLength,
71+
action: "pass",
6972
}),
7073
})
71-
}, [stompClient, gameId])
74+
}, [stompClient, gameId, movesLength])
7275
let onResetCounting = useCallback(() => {
7376
stompClient.publish({
7477
destination: "/app/game/move",
7578
body: JSON.stringify({
76-
id: gameId,
77-
resetCounting: true,
79+
n: movesLength,
80+
action: "resetCounting",
7881
}),
7982
})
80-
}, [stompClient, gameId])
83+
}, [stompClient, gameId, movesLength])
8184
let onCountingAgree = useCallback(() => {
85+
setAgreeCounting(true)
8286
stompClient.publish({
8387
destination: "/app/game/move",
8488
body: JSON.stringify({
85-
id: gameId,
86-
agreeCounting: true,
89+
n: movesLength,
90+
action: "agreeCounting",
8791
}),
8892
})
89-
}, [stompClient, gameId])
93+
}, [stompClient, gameId, movesLength])
9094
if (!board.length) {
9195
return <span>Loading...</span>
9296
}
@@ -158,7 +162,7 @@ function Panel({zoom, setZoom}) {
158162
</div>
159163
<div className="flex-none">
160164
<Button
161-
disabled={gameHasEnded || !countingComplete()}
165+
disabled={agreeCounting || gameHasEnded || !countingComplete()}
162166
className="py-1 px-4"
163167
onClick={onCountingAgree}>
164168
OK

src/main/client/src/model/base.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,10 @@ import {
1313
} from "../util.js"
1414

1515
export function updateBoard(board, move) {
16-
let {pass, x, y, color} = move
17-
if (pass) {
16+
if (move.action === "pass") {
1817
return [PointList.empty(), board]
1918
}
19+
let {x, y, color} = move
2020
board = applyMove(board, move)
2121
let oppositeColor = color ^ (WHITE | BLACK)
2222
let dead = new PointList(board.length)

src/main/client/src/store.js

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -101,18 +101,28 @@ export const useGameStore = create((set, get) => ({
101101
return moves[moves.length - 1].color ^ COLORS
102102
},
103103
counting: false,
104+
agreeCounting: false,
105+
setAgreeCounting: (agree) => {
106+
set(produce(state => {
107+
state.agreeCounting = agree
108+
}))
109+
},
104110
gameState: {
105111
board: [],
106112
forbidden: [-1, -1],
107113
gameHasEnded: false,
108114
},
109115
addMove: (move) => {
110116
set(produce(state => {
111-
if (move.end) {
117+
if (move.action === "end") {
112118
state.moves.push(move)
113119
state.gameState.gameHasEnded = true
114120
return
115121
}
122+
if (move.action === "agreeCounting") {
123+
return
124+
}
125+
state.agreeCounting = false
116126
let moves = get().moves
117127
let baseBoard = get().baseBoard
118128
if (move.n < moves.length) {
@@ -129,11 +139,11 @@ export const useGameStore = create((set, get) => ({
129139
}
130140
let [storedMove, updated, forbidden] = createMoveData(baseBoard, moves, move, counting, get().handicap)
131141
state.moves.push(storedMove)
132-
state.lastMove = move.pass ? undefined : move
142+
state.lastMove = move.action === "pass" ? undefined : move
133143
state.baseBoard = updated
134144
state.gameState.board = rehydrate(updated)
135145
state.gameState.forbidden = forbidden
136-
if (move.pass && moves.length && moves[moves.length - 1].pass) {
146+
if (move.action === "pass" && moves.length && moves[moves.length - 1].action === "pass") {
137147
state.counting = true
138148
}
139149
}))
@@ -152,7 +162,7 @@ export const useGameStore = create((set, get) => ({
152162
let counting = false
153163
let queueLength = 0
154164
for (let move of game.moves) {
155-
if (move.end) {
165+
if (move.action === "end") {
156166
moves.push(move)
157167
state.gameState.gameHasEnded = true
158168
break
@@ -174,7 +184,7 @@ export const useGameStore = create((set, get) => ({
174184
}
175185
if (game.moves.length) {
176186
let move = game.moves[game.moves.length - 1]
177-
state.lastMove = move.pass ? undefined : move
187+
state.lastMove = move.action === "pass" ? undefined : move
178188
}
179189
state.counting = counting
180190
state.dim = game.dim
@@ -191,11 +201,11 @@ export const useGameStore = create((set, get) => ({
191201

192202
function createMoveData(baseBoard, moves, colorlessMove, counting, handicap) {
193203
let move = {...colorlessMove, color: nextMoveColor(moves, handicap)}
194-
if (move.pass && moves.length && moves[moves.length - 1].pass) {
204+
if (move.action === "pass" && moves.length && moves[moves.length - 1].action === "pass") {
195205
return [move, count(baseBoard), [-1, -1]]
196206
}
197207
if (counting) {
198-
let updated = move.resetCounting ?
208+
let updated = move.action === "resetCounting" ?
199209
resetCounting(baseBoard) :
200210
toggleStonesAt(baseBoard, move.x, move.y)
201211
return [move, count(updated), [-1, -1]]

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

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
import com.bernd.model.AcceptRequest;
66
import com.bernd.model.ActiveGame;
77
import com.bernd.model.Game;
8-
import com.bernd.model.GameMove;
98
import com.bernd.model.Move;
109
import com.bernd.model.OpenGame;
1110
import com.bernd.model.ViewGame;
@@ -76,11 +75,10 @@ public void action(Move move, Principal p) {
7675
return;
7776
}
7877
Move updatedMove = move
79-
.withColor(game.isSelfPlay() ? color : principalColor)
80-
.withMoveNumber(game.moves().size());
78+
.withColor(game.isSelfPlay() ? color : principalColor);
8179
Game updated = game.update(updatedMove);
8280
games.put(updated);
83-
GameMove lastMove = game.getLastMove();
81+
Move lastMove = game.getLastMove();
8482
operations.convertAndSend("/topic/move/" + game.id(), lastMove.removeColor());
8583
}
8684

src/main/java/com/bernd/LobbyController.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,6 @@ public ViewGame startEdit(@RequestBody MatchRequest request) {
7272
principal,
7373
principal,
7474
false,
75-
0,
7675
createEmptyBoard(request.dim()),
7776
request.dim(),
7877
request.handicap(),

src/main/java/com/bernd/game/MoveList.java

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,15 @@
11
package com.bernd.game;
22

3-
import com.bernd.model.GameMove;
43
import com.bernd.model.Move;
54
import java.util.ArrayList;
65
import java.util.List;
76
import java.util.stream.Stream;
87

98
public final class MoveList {
109

11-
private final List<GameMove> moves;
10+
private final List<Move> moves;
1211

13-
private MoveList(List<GameMove> moves) {
12+
private MoveList(List<Move> moves) {
1413
this.moves = moves;
1514
}
1615

@@ -19,14 +18,14 @@ public static MoveList create(int size) {
1918
}
2019

2120
public void addGameEndMarker() {
22-
moves.add(new GameMove(moves.size(), 0, true, -1, -1, false, false, true));
21+
moves.add(new Move(0, moves.size(), "end", -1, -1));
2322
}
2423

2524
public void add(Move move) {
2625
moves.add(move.toGameMove());
2726
}
2827

29-
public GameMove get(int i) {
28+
public Move get(int i) {
3029
return moves.get(i);
3130
}
3231

@@ -38,11 +37,7 @@ public int size() {
3837
return moves.size();
3938
}
4039

41-
public List<GameMove> asList() {
42-
return List.copyOf(moves);
43-
}
44-
45-
public Stream<GameMove> asStream() {
40+
public Stream<Move> asStream() {
4641
return moves.stream();
4742
}
4843

src/main/java/com/bernd/model/ColorlessMove.java

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,7 @@
22

33
public record ColorlessMove(
44
int n,
5-
boolean pass,
5+
String action,
66
int x,
7-
int y,
8-
boolean resetCounting,
9-
boolean agreeCounting,
10-
boolean end) {
7+
int y) {
118
}

src/main/java/com/bernd/model/Game.java

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ public record Game(
1717
String black,
1818
String white,
1919
boolean counting,
20-
int countingAgreed,
2120
int[][] board,
2221
int dim,
2322
int handicap,
@@ -38,14 +37,13 @@ public Game update(Move move) {
3837
}
3938

4039
private Game updateInternal(Move move) {
41-
if (move.agreeCounting() && (countingAgreed | move.color()) == COLORS) {
40+
if (move.agreeCounting() && (countingAgreed() | move.color()) == COLORS) {
4241
moves.addGameEndMarker();
4342
} else {
4443
moves.add(move);
4544
}
4645
if (move.agreeCounting()) {
4746
return toBuilder()
48-
.withCountingAgreed(countingAgreed | move.color())
4947
.build();
5048
}
5149
if (counting) {
@@ -144,7 +142,10 @@ public ViewGame toView() {
144142
}
145143

146144
public boolean gameHasEnded() {
147-
return countingAgreed == COLORS;
145+
if (moves.isEmpty()) {
146+
return false;
147+
}
148+
return getLastMove().end();
148149
}
149150

150151
public GameBuilder toBuilder() {
@@ -163,7 +164,7 @@ public boolean isBlack(String name) {
163164
return black.equals(name);
164165
}
165166

166-
public GameMove getLastMove() {
167+
public Move getLastMove() {
167168
return moves.get(moves.size() - 1);
168169
}
169170

@@ -174,6 +175,14 @@ boolean opponentPassed() {
174175
return getLastMove().pass();
175176
}
176177

178+
public int countingAgreed() {
179+
if (moves.isEmpty()) {
180+
return 0;
181+
}
182+
Move lastMove = getLastMove();
183+
return lastMove.agreeCounting() ? lastMove.color() : 0;
184+
}
185+
177186
public int remainingHandicap() {
178187
return Math.max(0, handicap - moves.size());
179188
}

src/main/java/com/bernd/model/GameBuilder.java

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ public final class GameBuilder {
44
private final Game game;
55

66
private boolean counting;
7-
private int countingAgreed;
87
private int[][] board;
98
private int[] forbidden;
109

@@ -17,11 +16,6 @@ public GameBuilder withCounting(boolean counting) {
1716
return this;
1817
}
1918

20-
public GameBuilder withCountingAgreed(int countingAgreed) {
21-
this.countingAgreed = countingAgreed;
22-
return this;
23-
}
24-
2519
public GameBuilder withBoard(int[][] board) {
2620
this.board = board;
2721
return this;
@@ -39,7 +33,6 @@ public GameBuilder withForbidden(int x, int y) {
3933
static GameBuilder builder(Game game) {
4034
GameBuilder builder = new GameBuilder(game);
4135
builder.counting = game.counting();
42-
builder.countingAgreed = game.countingAgreed();
4336
builder.board = game.board();
4437
builder.forbidden = game.forbidden();
4538
return builder;
@@ -51,7 +44,6 @@ public Game build() {
5144
game.black(),
5245
game.white(),
5346
counting,
54-
countingAgreed,
5547
board,
5648
game.dim(),
5749
game.handicap(),

src/main/java/com/bernd/model/GameMove.java

Lines changed: 0 additions & 17 deletions
This file was deleted.

0 commit comments

Comments
 (0)