Skip to content

Commit ec6b7b1

Browse files
committed
stop transmitting move color
1 parent a3c99c2 commit ec6b7b1

File tree

9 files changed

+58
-18
lines changed

9 files changed

+58
-18
lines changed

README.md

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,20 @@
1-
https://www.jessym.com/articles/bundling-react-vite-with-spring-boot
1+
This is a go / baduk server.
2+
3+
### Hacking
4+
5+
First, start the backend server.
26

37
```
48
make run
59
```
610

7-
http://localhost:8080/app/
11+
Now, in a new terminal window or tab, start vite:
12+
13+
```
14+
make dev
15+
```
16+
17+
The app is running at [http://localhost:3006](http://localhost:3006/app).
18+
19+
Try editing a file in `src/main/client/src`.
20+
The change should immediately be visible in the browser.

src/main/client/src/store.js

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import {
99
} from "zustand/middleware"
1010
import {
1111
BLACK,
12-
WHITE,
12+
COLORS,
1313
} from "./util.js"
1414
import {
1515
rehydrate,
@@ -98,7 +98,7 @@ export const useGameStore = create((set, get) => ({
9898
if (!moves.length) {
9999
return BLACK
100100
}
101-
return moves[moves.length - 1].color ^ (BLACK | WHITE)
101+
return moves[moves.length - 1].color ^ COLORS
102102
},
103103
counting: false,
104104
gameState: {
@@ -127,7 +127,7 @@ export const useGameStore = create((set, get) => ({
127127
if (!counting) {
128128
state.queueLength = get().queueLength + 1
129129
}
130-
let [storedMove, updated, forbidden] = createMoveData(baseBoard, moves, move, counting)
130+
let [storedMove, updated, forbidden] = createMoveData(baseBoard, moves, move, counting, get().handicap)
131131
state.moves.push(storedMove)
132132
state.lastMove = move.pass ? undefined : move
133133
state.baseBoard = updated
@@ -167,7 +167,7 @@ export const useGameStore = create((set, get) => ({
167167
} else {
168168
passes = 0
169169
}
170-
let [storedMove, updated, newForbidden] = createMoveData(baseBoard, moves, move, counting)
170+
let [storedMove, updated, newForbidden] = createMoveData(baseBoard, moves, move, counting, game.handicap)
171171
moves.push(storedMove)
172172
forbidden = newForbidden
173173
baseBoard = updated
@@ -189,7 +189,8 @@ export const useGameStore = create((set, get) => ({
189189
},
190190
}))
191191

192-
function createMoveData(baseBoard, moves, move, counting) {
192+
function createMoveData(baseBoard, moves, colorlessMove, counting, handicap) {
193+
let move = {...colorlessMove, color: nextMoveColor(moves, handicap)}
193194
if (move.pass && moves.length && moves[moves.length - 1].pass) {
194195
return [move, count(baseBoard), [-1, -1]]
195196
}
@@ -204,3 +205,14 @@ function createMoveData(baseBoard, moves, move, counting) {
204205
let forbidden = getForbidden(baseBoard, updated, storedMove)
205206
return [storedMove, updated, forbidden]
206207
}
208+
209+
function nextMoveColor(moves, handicap) {
210+
let remainingHandicap = Math.max(0, handicap - moves.length)
211+
if (remainingHandicap) {
212+
return BLACK
213+
}
214+
if (!moves.length) {
215+
return BLACK
216+
}
217+
return moves[moves.length - 1].color ^ COLORS
218+
}

src/main/client/src/util.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ export const TOGGLE_STUFF = TOGGLE_B | TOGGLE_W
2121
export const TERRITORY = TERRITORY_W | TERRITORY_B
2222
export const ANY_REMOVED = REMOVED_W | REMOVED_B
2323

24-
export const COLORS = BLACK | WHITE;
24+
export const COLORS = BLACK | WHITE
2525

2626
export async function tfetch(url, options) {
2727
let response

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

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import com.bernd.model.OpenGame;
1111
import com.bernd.model.ViewGame;
1212
import com.bernd.util.RandomString;
13+
import java.security.Principal;
1314
import org.springframework.http.HttpStatus;
1415
import org.springframework.http.ResponseEntity;
1516
import org.springframework.messaging.core.MessageSendingOperations;
@@ -22,8 +23,6 @@
2223
import org.springframework.web.bind.annotation.ResponseBody;
2324
import org.springframework.web.server.ResponseStatusException;
2425

25-
import java.security.Principal;
26-
2726
import static com.bernd.util.Auth.getPrincipal;
2827
import static com.bernd.util.Util.COLORS;
2928

@@ -82,7 +81,7 @@ public void action(Move move, Principal p) {
8281
Game updated = game.update(updatedMove);
8382
games.put(updated);
8483
GameMove lastMove = game.getLastMove();
85-
operations.convertAndSend("/topic/move/" + game.id(), lastMove);
84+
operations.convertAndSend("/topic/move/" + game.id(), lastMove.removeColor());
8685
}
8786

8887
private int getColorFromGameState(Game game) {

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22

33
import com.bernd.model.GameMove;
44
import com.bernd.model.Move;
5-
65
import java.util.ArrayList;
76
import java.util.List;
7+
import java.util.stream.Stream;
88

99
public final class MoveList {
1010

@@ -42,6 +42,10 @@ public List<GameMove> asList() {
4242
return List.copyOf(moves);
4343
}
4444

45+
public Stream<GameMove> asStream() {
46+
return moves.stream();
47+
}
48+
4549
@Override
4650
public String toString() {
4751
return moves.toString();
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package com.bernd.model;
2+
3+
public record ColorlessMove(
4+
int n,
5+
boolean pass,
6+
int x,
7+
int y,
8+
boolean resetCounting,
9+
boolean agreeCounting,
10+
boolean end) {
11+
}

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,9 @@ public record GameMove(
99
boolean resetCounting,
1010
boolean agreeCounting,
1111
boolean end) {
12+
13+
public ColorlessMove removeColor() {
14+
return new ColorlessMove(
15+
n, pass, x, y, resetCounting, agreeCounting, end);
16+
}
1217
}

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

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,6 @@ public GameMove toGameMove() {
1313
return new GameMove(n, color, pass, x, y, resetCounting, agreeCounting, false);
1414
}
1515

16-
public GameMove gameEnd() {
17-
return new GameMove(n, color, pass, x, y, resetCounting, agreeCounting, true);
18-
}
19-
2016
public Move withColor(int color) {
2117
return new Move(color, n, pass, resetCounting, agreeCounting, x, y);
2218
}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ public record ViewGame(
99
int dim,
1010
int handicap,
1111
int remainingHandicap,
12-
List<GameMove> moves
12+
List<ColorlessMove> moves
1313
) {
1414

1515
static ViewGame fromGame(Game game) {
@@ -20,6 +20,6 @@ static ViewGame fromGame(Game game) {
2020
game.dim(),
2121
game.handicap(),
2222
game.remainingHandicap(),
23-
game.moves().asList());
23+
game.moves().asStream().map(GameMove::removeColor).toList());
2424
}
2525
}

0 commit comments

Comments
 (0)