Skip to content

Commit 7234baf

Browse files
committed
server side refactoring, add field color to move
1 parent 794a329 commit 7234baf

File tree

11 files changed

+176
-83
lines changed

11 files changed

+176
-83
lines changed

src/main/client/src/Game.jsx

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import {
1212
import {
1313
StompContext,
1414
BLACK,
15+
WHITE,
1516
TERRITORY,
1617
TERRITORY_B,
1718
REMOVED_B,
@@ -43,6 +44,7 @@ export const Game = () => {
4344
let {gameId} = useParams()
4445
let stompClient = useContext(StompContext)
4546
let auth = useAuthStore(state => state.auth)
47+
let black = useGameStore(state => state.black)
4648
let setGameState = useGameStore(state => state.setGameState)
4749
let queueStatus = useGameStore(state => state.queueStatus)
4850
let queueLength = useGameStore(state => state.queueLength)
@@ -167,6 +169,7 @@ export const Game = () => {
167169
body: JSON.stringify({
168170
id: gameId,
169171
n: queueLength(),
172+
color: black.name === auth.name ? BLACK : WHITE,
170173
x: cursor_x,
171174
y: cursor_y,
172175
}),

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

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,9 +51,11 @@ function Panel({zoom, setZoom}) {
5151
let queueLength = useGameStore(state => state.queueLength)
5252
let counting = useGameStore(state => state.counting)
5353
let countingComplete = useGameStore(state => state.countingComplete)
54+
let currentColor = useGameStore(state => state.currentColor)
5455
let currentPlayer = useGameStore(state => state.currentPlayer)
5556
let { board, gameHasEnded } = useGameStore(state => state.gameState)
5657
let navigate = useNavigate()
58+
let myColor = black.name === auth.name ? BLACK : WHITE
5759
let onExit = useCallback(() => {
5860
navigate(base + "/lobby")
5961
}, [navigate])
@@ -62,6 +64,7 @@ function Panel({zoom, setZoom}) {
6264
destination: "/app/game/move",
6365
body: JSON.stringify({
6466
id: gameId,
67+
color: myColor,
6568
n: queueLength(),
6669
pass: true,
6770
}),
@@ -72,21 +75,23 @@ function Panel({zoom, setZoom}) {
7275
destination: "/app/game/move",
7376
body: JSON.stringify({
7477
id: gameId,
78+
color: myColor,
7579
n: queueLength(),
7680
resetCounting: true,
7781
}),
7882
})
79-
}, [stompClient, gameId, queueLength])
83+
}, [stompClient, gameId, queueLength, currentColor])
8084
let onCountingAgree = useCallback(() => {
8185
stompClient.publish({
8286
destination: "/app/game/move",
8387
body: JSON.stringify({
8488
id: gameId,
89+
color: myColor,
8590
n: queueLength(),
8691
agreeCounting: true,
8792
}),
8893
})
89-
}, [stompClient, gameId, queueLength])
94+
}, [stompClient, gameId, queueLength, currentColor])
9095
if (!board.length) {
9196
return <span>Loading...</span>
9297
}

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

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,7 @@ public void action(Move move, Principal principal) {
5757
return;
5858
}
5959
int moveNumber = game.moves().size();
60-
int color = game.currentColor();
61-
String currentPlayer = game.currentColor() == Board.B ? game.black().name() : game.white().name();
60+
String currentPlayer = move.color() == Board.B ? game.black().name() : game.white().name();
6261
if (!principal.getName().equals(currentPlayer)) {
6362
return; // discard
6463
}
@@ -68,9 +67,9 @@ public void action(Move move, Principal principal) {
6867
Game updated = game.update(move);
6968
games.put(updated);
7069
if (updated.gameHasEnded()) {
71-
operations.convertAndSend("/topic/move/" + game.id(), move.gameEnd(color, updated.counting()));
70+
operations.convertAndSend("/topic/move/" + game.id(), move.gameEnd(updated.counting()));
7271
} else if (!move.agreeCounting()) {
73-
operations.convertAndSend("/topic/move/" + game.id(), move.toView(color, updated.counting()));
72+
operations.convertAndSend("/topic/move/" + game.id(), move.toView(updated.counting()));
7473
}
7574
}
7675

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

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,6 @@
1818
import org.springframework.web.bind.annotation.RequestBody;
1919
import org.springframework.web.bind.annotation.ResponseBody;
2020

21-
import static com.bernd.game.Board.B;
22-
2321
@Controller
2422
public class LobbyController {
2523

@@ -77,8 +75,6 @@ public ViewGame startEdit(@RequestBody MatchRequest request) {
7775
user,
7876
false,
7977
0,
80-
principal,
81-
B,
8278
false,
8379
createEmptyBoard(request.dim()),
8480
request.dim(),

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

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

33
import com.bernd.model.GameMove;
44
import com.bernd.model.Move;
5+
56
import java.util.ArrayList;
67
import java.util.Arrays;
78
import java.util.List;
@@ -45,15 +46,15 @@ public void addGameEndMarker() {
4546
pos++;
4647
}
4748

48-
public void add(int color, Move move, boolean counting) {
49+
public void add(Move move, boolean counting) {
4950
ensureCapacity();
5051
int ptId;
5152
if (move.pass()) {
5253
ptId = PASS;
5354
} else {
5455
ptId = dim * move.y() + move.x();
5556
}
56-
if (color == Board.W) {
57+
if (move.color() == Board.W) {
5758
ptId |= WHITE;
5859
}
5960
if (counting) {

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

Lines changed: 35 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,6 @@
99
import org.apache.logging.log4j.LogManager;
1010
import org.apache.logging.log4j.Logger;
1111

12-
import static com.bernd.game.Board.B;
13-
import static com.bernd.game.Board.W;
1412
import static com.bernd.game.Board.removeDeadStonesAround;
1513
import static com.bernd.util.Util.COLORS;
1614

@@ -20,8 +18,6 @@ public record Game(
2018
User white,
2119
boolean counting,
2220
int countingAgreed,
23-
String currentPlayer,
24-
int currentColor,
2521
boolean opponentPassed,
2622
int[][] board,
2723
int dim,
@@ -44,36 +40,57 @@ public Game update(Move move) {
4440

4541
private Game updateInternal(Move move) {
4642
if (move.agreeCounting()) {
47-
if ((countingAgreed | currentColor()) == COLORS) {
43+
if ((countingAgreed | move.color()) == COLORS) {
4844
moves.addGameEndMarker();
4945
}
50-
return countingAgreed(countingAgreed | currentColor());
46+
return toBuilder()
47+
.withCountingAgreed(countingAgreed | move.color())
48+
.build();
5149
}
52-
moves.add(currentColor, move, counting);
50+
moves.add(move, counting);
5351
if (counting) {
5452
if (move.resetCounting()) {
5553
int[][] resetted = Toggle.resetCounting(board);
56-
return game(Count.count(resetted), NOT_FORBIDDEN);
54+
return toBuilder()
55+
.withBoard(Count.count(resetted))
56+
.withForbidden(NOT_FORBIDDEN)
57+
.build();
5758
}
5859
int[][] toggled = Toggle.toggleStonesAt(board, move.x(), move.y());
59-
return game(Count.count(toggled), NOT_FORBIDDEN);
60+
return toBuilder()
61+
.withBoard(Count.count(toggled))
62+
.withForbidden(NOT_FORBIDDEN)
63+
.build();
6064
}
6165
if (move.pass()) {
6266
if (opponentPassed) {
63-
return startCounting();
67+
return toBuilder()
68+
.withBoard(Count.count(board))
69+
.withCounting(true)
70+
.withForbidden(NOT_FORBIDDEN)
71+
.build();
6472
}
65-
return game(board, counting, 0, true, NOT_FORBIDDEN);
73+
return toBuilder()
74+
.withOpponentPassed(true)
75+
.withForbidden(NOT_FORBIDDEN)
76+
.build();
6677
}
6778
int x = move.x();
6879
int y = move.y();
69-
int color = currentColor();
80+
int color = move.color();
7081
int[][] updated = BoardUpdateImpl.create(board.length, x, y, color).apply(board);
7182
int[][] result = removeDeadStonesAround(updated, x, y);
7283
Direction direction = getDirection(x, y, result, updated);
7384
if (isKo(x, y, color, result, direction)) {
74-
return game(result, new int[]{direction.moveX(x), direction.moveY(y)});
85+
return toBuilder()
86+
.withBoard(result)
87+
.withForbidden(direction.moveX(x), direction.moveY(y))
88+
.build();
7589
}
76-
return game(result, NOT_FORBIDDEN);
90+
return toBuilder()
91+
.withBoard(result)
92+
.withForbidden(NOT_FORBIDDEN)
93+
.build();
7794
}
7895

7996
private boolean isKo(
@@ -128,56 +145,15 @@ private Direction getDirection(
128145
return Direction.from(xx, yy, col, row);
129146
}
130147

131-
private Game game(
132-
int[][] board,
133-
boolean counting,
134-
int countingAgreed,
135-
boolean opponentPassed,
136-
int[] forbidden) {
137-
return new Game(
138-
id,
139-
black,
140-
white,
141-
counting,
142-
countingAgreed,
143-
nextPlayer(),
144-
nextColor(),
145-
opponentPassed,
146-
board,
147-
dim,
148-
Math.max(0, handicap - 1),
149-
forbidden,
150-
moves);
151-
}
152-
153-
private Game game(int[][] board, int[] forbidden) {
154-
return game(board, counting, 0, false, forbidden);
155-
}
156-
157-
private Game startCounting() {
158-
return game(Count.count(board), true, 0, true, NOT_FORBIDDEN);
159-
}
160-
161-
private String nextPlayer() {
162-
return currentPlayer.equals(black.name()) ? white().name() : black().name();
163-
}
164-
165-
private int nextColor() {
166-
if (handicap > 0) {
167-
return currentColor;
168-
}
169-
return currentColor == B ? W : B;
170-
}
171-
172148
public ViewGame toView() {
173149
return ViewGame.fromGame(this);
174150
}
175151

176-
private Game countingAgreed(int countingAgreed) {
177-
return game(board, counting, countingAgreed, opponentPassed, forbidden);
178-
}
179-
180152
public boolean gameHasEnded() {
181153
return countingAgreed == COLORS;
182154
}
155+
156+
public GameBuilder toBuilder() {
157+
return GameBuilder.builder(this);
158+
}
183159
}
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
package com.bernd.model;
2+
3+
public final class GameBuilder {
4+
private final Game game;
5+
6+
private boolean counting;
7+
private int countingAgreed;
8+
private boolean opponentPassed;
9+
private int[][] board;
10+
private int dim;
11+
private int handicap;
12+
private int[] forbidden;
13+
14+
private GameBuilder(Game game) {
15+
this.game = game;
16+
}
17+
18+
public GameBuilder withCounting(boolean counting) {
19+
this.counting = counting;
20+
return this;
21+
}
22+
23+
public GameBuilder withCountingAgreed(int countingAgreed) {
24+
this.countingAgreed = countingAgreed;
25+
return this;
26+
}
27+
28+
public GameBuilder withOpponentPassed(boolean opponentPassed) {
29+
this.opponentPassed = opponentPassed;
30+
return this;
31+
}
32+
33+
public GameBuilder withBoard(int[][] board) {
34+
this.board = board;
35+
return this;
36+
}
37+
38+
public GameBuilder withDim(int dim) {
39+
this.dim = dim;
40+
return this;
41+
}
42+
43+
public GameBuilder withHandicap(int handicap) {
44+
this.handicap = handicap;
45+
return this;
46+
}
47+
48+
public GameBuilder withForbidden(int[] forbidden) {
49+
this.forbidden = forbidden;
50+
return this;
51+
}
52+
53+
public GameBuilder withForbidden(int x, int y) {
54+
return withForbidden(new int[]{x, y});
55+
}
56+
57+
static GameBuilder builder(Game game) {
58+
GameBuilder builder = new GameBuilder(game);
59+
builder.counting = game.counting();
60+
builder.countingAgreed = game.countingAgreed();
61+
builder.opponentPassed = game.opponentPassed();
62+
builder.board = game.board();
63+
builder.dim = game.dim();
64+
builder.handicap = game.handicap();
65+
builder.forbidden = game.forbidden();
66+
return builder;
67+
}
68+
69+
public Game build() {
70+
return new Game(
71+
game.id(),
72+
game.black(),
73+
game.white(),
74+
counting,
75+
countingAgreed,
76+
opponentPassed,
77+
board,
78+
dim,
79+
handicap,
80+
forbidden,
81+
game.moves()
82+
);
83+
}
84+
}

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,19 @@
22

33
public record Move(
44
String id,
5+
int color,
56
int n,
67
boolean pass,
78
boolean resetCounting,
89
boolean agreeCounting,
910
int x,
1011
int y) {
1112

12-
public GameMove toView(int color, boolean counting) {
13+
public GameMove toView(boolean counting) {
1314
return new GameMove(n, color, pass, x, y, counting, resetCounting, false);
1415
}
1516

16-
public GameMove gameEnd(int color, boolean counting) {
17+
public GameMove gameEnd(boolean counting) {
1718
return new GameMove(n, color, pass, x, y, counting, resetCounting, true);
1819
}
1920
}

0 commit comments

Comments
 (0)