Skip to content

Commit bfa8d9b

Browse files
committed
save game id in user object
1 parent e7def85 commit bfa8d9b

21 files changed

+143
-83
lines changed

src/main/client/src/Game.jsx

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -37,12 +37,11 @@ const TAU = 2 * Math.PI
3737
export const Game = () => {
3838
let [cursor_x, setCursor_x] = useState(-1)
3939
let [cursor_y, setCursor_y] = useState(-1)
40-
let lastStoneXref = useRef(-1)
41-
let lastStoneYref = useRef(-1)
4240
let [zoom, setZoom] = useState(0)
4341
let {gameId} = useParams()
4442
let stompClient = useContext(StompContext)
4543
let auth = useAuthStore(state => state.auth)
44+
let lastMove = useGameStore(state => state.lastMove)
4645
let setGameState = useGameStore(state => state.setGameState)
4746
let queueStatus = useGameStore(state => state.queueStatus)
4847
let addMove = useGameStore(state => state.addMove)
@@ -110,8 +109,6 @@ export const Game = () => {
110109
stoneRadius: getRadius(step * 0.475),
111110
territoryRadius: getRadius(step * 0.125),
112111
hoshiRadius: getRadius(step * 0.0625),
113-
lastStoneXref: lastStoneXref,
114-
lastStoneYref: lastStoneYref,
115112
}
116113
}, [board.length, canvasRef, zoom])
117114

@@ -158,8 +155,6 @@ export const Game = () => {
158155
return
159156
}
160157
}
161-
lastStoneXref.current = cursor_x
162-
lastStoneYref.current = cursor_y
163158
stompClient.publish({
164159
destination: "/app/game/move",
165160
body: JSON.stringify({
@@ -179,7 +174,7 @@ export const Game = () => {
179174
paintStonesCounting(context, board, countingGroup)
180175
return
181176
} else {
182-
paintStones(context, board)
177+
paintStones(context, board, lastMove)
183178
}
184179
if (currentPlayer() !== auth.name) {
185180
return
@@ -200,7 +195,7 @@ export const Game = () => {
200195
"rgba(0,0,0,0.25)" :
201196
"rgba(255,255,255,0.25)"
202197
showShadow(context, cursor_x, cursor_y, style)
203-
}, [cursor_x, cursor_y, context, canvasRef, auth, currentColor, board, currentPlayer, counting, countingGroup, forbidden_x, forbidden_y])
198+
}, [cursor_x, cursor_y, context, canvasRef, auth, currentColor, board, currentPlayer, counting, countingGroup, forbidden_x, forbidden_y, lastMove])
204199

205200
useEffect(() => {
206201
if (queueStatus === "up_to_date") {
@@ -266,13 +261,14 @@ function showStone({ canvasRef, grid, stoneRadius }, grid_x, grid_y, style) {
266261
ctx.fill()
267262
}
268263

269-
function paintLastMove({ isCursorInBounds, canvasRef, grid, stoneRadius, lastStoneXref, lastStoneYref }, board) {
270-
let grid_x = lastStoneXref.current
271-
let grid_y = lastStoneYref.current
264+
function paintLastMove({isCursorInBounds, canvasRef, grid, stoneRadius}, lastMove) {
265+
if (!lastMove) {
266+
return
267+
}
268+
let {x: grid_x, y: grid_y, color} = lastMove
272269
if (!isCursorInBounds(grid_x, grid_y)) {
273270
return
274271
}
275-
let { color } = board[grid_y][grid_x]
276272
let style = color === BLACK ?
277273
"rgba(255,255,255)" :
278274
"rgba(0,0,0)"
@@ -334,7 +330,7 @@ function getRadius(radius) {
334330
return diameter / 2
335331
}
336332

337-
function paintStones(context, board) {
333+
function paintStones(context, board, lastMove) {
338334
for (let grid_y = 0; grid_y < board.length; grid_y++) {
339335
for (let grid_x = 0; grid_x < board.length; grid_x++) {
340336
let { hasStone, color } = board[grid_y][grid_x]
@@ -346,7 +342,7 @@ function paintStones(context, board) {
346342
}
347343
}
348344
}
349-
paintLastMove(context, board)
345+
paintLastMove(context, lastMove)
350346
}
351347

352348
function paintStonesCounting(context, board, countingGroup) {

src/main/client/src/feature/ActiveGames.jsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,8 +81,8 @@ function ActiveGame({game}) {
8181
}}
8282
className={classes}
8383
key={game.id}>
84-
<div className="pl-3 pr-1 rounded-l-lg">{game.white.name}</div>
85-
<div className="">{game.black.name}</div>
84+
<div className="pl-3 pr-1 rounded-l-lg">{game.white}</div>
85+
<div className="">{game.black}</div>
8686
<div className={dimClasses}>
8787
{game.dim}x{game.dim}
8888
</div>

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -127,11 +127,11 @@ function Panel({zoom, setZoom}) {
127127
</IconContext.Provider>
128128
</button>
129129
<div className="flex gap-x-1">
130-
<div>{white.name}</div>
130+
<div>{white}</div>
131131
<div>vs</div>
132-
<div>{black.name}</div>
132+
<div>{black}</div>
133133
</div>
134-
<div>Move {queueLength()}</div>
134+
<div>Move {queueLength}</div>
135135
<div className="mt-2">
136136
<Button
137137
onClick={onPass}

src/main/client/src/feature/LobbyPanel.jsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,8 @@ function Panel() {
5252
</div>
5353
<div className="pl-2 mt-2">
5454
{users.map(user => (
55-
<div key={user.name}>
56-
{user.name}
55+
<div key={user}>
56+
{user}
5757
</div>
5858
))}
5959
</div>

src/main/client/src/feature/OpenGames.jsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ export function OpenGames() {
101101
function OpenGame({game, acceptableGame, setAcceptableGame}) {
102102
let dimRef = useRef()
103103
let auth = useAuthStore(state => state.auth)
104-
let disabled = auth.name === game.user.name
104+
let disabled = auth.name === game.user
105105
let classes = twJoin(
106106
"contents",
107107
"*:py-3",
@@ -130,7 +130,7 @@ function OpenGame({game, acceptableGame, setAcceptableGame}) {
130130
}}
131131
className={classes}
132132
key={game.id}>
133-
<div className="pl-3 pr-1 rounded-l-lg">{game.user.name}</div>
133+
<div className="pl-3 pr-1 rounded-l-lg">{game.user}</div>
134134
<div ref={dimRef} className={dimClasses}>
135135
{game.dim}x{game.dim}
136136
</div>
@@ -159,7 +159,7 @@ function AcceptDialog({acceptableGame, onAccept}) {
159159
<div className="text-black">
160160
<button type="button" className="inline-flex" onClick={() => setFlip(!isFlip)}>
161161
<BabyStone color={isFlip ? "white": "black"} />
162-
{acceptableGame.game.user.name}
162+
{acceptableGame.game.user}
163163
</button>
164164
</div>
165165
<div className="text-black">

src/main/client/src/store.js

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -57,12 +57,9 @@ export const useGameStore = create((set, get) => ({
5757
dim: 0,
5858
handicap: 0,
5959
queueStatus: "behind",
60-
black: {
61-
name: "",
62-
},
63-
white: {
64-
name: "",
65-
},
60+
black: "",
61+
white: "",
62+
lastMove: undefined,
6663
countingComplete: () => {
6764
if (!get().counting) {
6865
return false
@@ -84,16 +81,14 @@ export const useGameStore = create((set, get) => ({
8481
let black = get().black
8582
let handicap = get().handicap
8683
if (handicap > moves.length) {
87-
return black.name
84+
return black
8885
}
8986
if (!moves.length) {
90-
return black.name
87+
return black
9188
}
92-
return moves[moves.length - 1].color === BLACK ? white.name : black.name
93-
},
94-
queueLength: () => {
95-
return get().moves.length
89+
return moves[moves.length - 1].color === BLACK ? white : black
9690
},
91+
queueLength: 0,
9792
currentColor: () => {
9893
let moves = get().moves
9994
let handicap = get().handicap
@@ -129,8 +124,12 @@ export const useGameStore = create((set, get) => ({
129124
}
130125
state.queueStatus = "up_to_date"
131126
let counting = get().counting
127+
if (!counting) {
128+
state.queueLength = get().queueLength + 1
129+
}
132130
let [storedMove, updated, forbidden] = createMoveData(baseBoard, moves, move, counting)
133131
state.moves.push(storedMove)
132+
state.lastMove = move.pass ? undefined : move
134133
state.baseBoard = updated
135134
state.gameState.board = rehydrate(updated)
136135
state.gameState.forbidden = forbidden
@@ -151,6 +150,7 @@ export const useGameStore = create((set, get) => ({
151150
let forbidden = [-1, -1]
152151
let passes = 0
153152
let counting = false
153+
let queueLength = 0
154154
for (let move of game.moves) {
155155
if (move.end) {
156156
moves.push(move)
@@ -160,6 +160,7 @@ export const useGameStore = create((set, get) => ({
160160
if (move.pass) {
161161
if (passes) {
162162
counting = true
163+
queueLength = move.n
163164
} else {
164165
passes = 1
165166
}
@@ -171,13 +172,18 @@ export const useGameStore = create((set, get) => ({
171172
forbidden = newForbidden
172173
baseBoard = updated
173174
}
175+
if (game.moves.length) {
176+
let move = game.moves[game.moves.length - 1]
177+
state.lastMove = move.pass ? undefined : move
178+
}
174179
state.counting = counting
175180
state.dim = game.dim
176181
state.baseBoard = baseBoard
177182
state.moves = moves
178183
state.gameState.board = rehydrate(baseBoard)
179184
state.gameState.forbidden = forbidden
180185
state.handicap = game.handicap
186+
state.queueLength = queueLength
181187
state.queueStatus = "up_to_date"
182188
}))
183189
},

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

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
import com.bernd.model.Move;
1010
import com.bernd.model.OpenGame;
1111
import com.bernd.model.ViewGame;
12-
import com.bernd.util.Auth;
1312
import com.bernd.util.RandomString;
1413
import org.springframework.http.HttpStatus;
1514
import org.springframework.http.ResponseEntity;
@@ -24,32 +23,36 @@
2423
import org.springframework.web.server.ResponseStatusException;
2524

2625
import java.security.Principal;
27-
import java.util.Objects;
2826

27+
import static com.bernd.util.Auth.getPrincipal;
2928
import static com.bernd.util.Util.COLORS;
3029

3130
@Controller
3231
public class GameController {
3332

3433
private final MessageSendingOperations<String> operations;
34+
private final Users users;
3535
private final Games games;
3636
private final OpenGames openGames;
3737
private final ActiveGames activeGames;
3838

3939
GameController(
4040
MessageSendingOperations<String> operations,
41+
Users users,
4142
Games games,
4243
OpenGames openGames,
4344
ActiveGames activeGames) {
4445
this.operations = operations;
46+
this.users = users;
4547
this.games = games;
4648
this.openGames = openGames;
4749
this.activeGames = activeGames;
4850
}
4951

5052
@ResponseBody
5153
@GetMapping(value = "/api/game/{id}")
52-
public ViewGame getGame(@PathVariable String id) {
54+
public ViewGame getGame(@PathVariable String id, Principal p) {
55+
users.get(p).setCurrentGame(id);
5356
Game game = games.get(id);
5457
if (game == null) {
5558
throw new ResponseStatusException(HttpStatus.NOT_FOUND, "no such game");
@@ -59,11 +62,11 @@ public ViewGame getGame(@PathVariable String id) {
5962

6063
@MessageMapping("/game/move")
6164
public void action(Move move, Principal p) {
62-
Game game = games.get(move.id());
65+
Game game = games.get(users.get(p).currentGame());
6366
if (p == null || game == null) {
6467
return;
6568
}
66-
int color = getCurrentColor(game, Objects.toString(p.getName(), ""));
69+
int color = getCurrentColor(game, getPrincipal(p));
6770
if (color == 0) {
6871
return;
6972
}
@@ -112,7 +115,7 @@ private static int getColor(Game game, String principal) {
112115
@ResponseBody
113116
@PostMapping(value = "/api/create", consumes = "application/json")
114117
public OpenGame newGame(@RequestBody OpenGame game) {
115-
String principal = Auth.getPrincipal();
118+
String principal = getPrincipal();
116119
OpenGame result = openGames.put(game.withUser(principal)
117120
.withId(RandomString.get()));
118121
operations.convertAndSend("/topic/lobby/open_games", openGames.games());
@@ -121,9 +124,9 @@ public OpenGame newGame(@RequestBody OpenGame game) {
121124

122125
@PostMapping(value = "/api/accept", consumes = "application/json")
123126
public ResponseEntity<?> accept(@RequestBody AcceptRequest acceptRequest) {
124-
String principal = Auth.getPrincipal();
127+
String principal = getPrincipal();
125128
openGames.remove(principal);
126-
OpenGame openGame = openGames.remove(acceptRequest.game().user().name());
129+
OpenGame openGame = openGames.remove(acceptRequest.game().user());
127130
Game fullGame = games.put(openGame.accept(principal, acceptRequest));
128131
activeGames.put(ActiveGame.fromGame(fullGame));
129132
operations.convertAndSend("/topic/game/" + fullGame.id(), fullGame.toView());

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

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
import com.bernd.model.Game;
77
import com.bernd.model.MatchRequest;
88
import com.bernd.model.OpenGameList;
9-
import com.bernd.model.User;
109
import com.bernd.model.ViewGame;
1110
import com.bernd.util.Auth;
1211
import com.bernd.util.RandomString;
@@ -67,12 +66,11 @@ public OpenGameList getOpenGames() {
6766
public ViewGame startEdit(@RequestBody MatchRequest request) {
6867
String principal = Auth.getPrincipal();
6968
lobbyUsers.remove(principal);
70-
User user = new User(principal);
7169
operations.convertAndSend("/topic/lobby/users", lobbyUsers.users());
7270
Game game = games.put(new Game(
7371
RandomString.get(),
74-
user,
75-
user,
72+
principal,
73+
principal,
7674
false,
7775
0,
7876
createEmptyBoard(request.dim()),
Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
package com.bernd;
22

3-
import com.bernd.model.User;
43
import com.bernd.model.UserList;
54
import org.springframework.stereotype.Component;
65

@@ -11,22 +10,22 @@
1110

1211
@Component
1312
public class LobbyUsers {
14-
private final Map<String, User> map = new LinkedHashMap<>();
13+
private final Map<String, String> map = new LinkedHashMap<>();
1514

1615
void add(String name) {
17-
map.put(name, new User(name));
16+
map.put(name, name);
1817
}
1918

20-
User get(String name) {
19+
String get(String name) {
2120
return map.get(name);
2221
}
2322

24-
User remove(String principal) {
23+
String remove(String principal) {
2524
return map.remove(principal);
2625
}
2726

2827
UserList users() {
29-
Collection<User> values = map.values();
28+
Collection<String> values = map.values();
3029
return new UserList(List.copyOf(values));
3130
}
3231
}

src/main/java/com/bernd/OpenGames.java

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

33
import com.bernd.model.OpenGame;
44
import com.bernd.model.OpenGameList;
5+
import org.springframework.stereotype.Component;
6+
57
import java.util.LinkedHashMap;
68
import java.util.List;
79
import java.util.Map;
8-
import org.springframework.stereotype.Component;
910

1011
@Component
1112
public class OpenGames {
1213
private final Map<String, OpenGame> map = new LinkedHashMap<>();
1314

1415
OpenGame put(OpenGame game) {
15-
map.put(game.user().name(), game);
16+
map.put(game.user(), game);
1617
return game;
1718
}
1819

0 commit comments

Comments
 (0)