Skip to content

Commit 350f0f3

Browse files
committed
hover counting groups
1 parent 561705f commit 350f0f3

File tree

5 files changed

+57
-14
lines changed

5 files changed

+57
-14
lines changed

src/main/client/src/Play.jsx

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ export const Play = () => {
5353
initialized.current = true
5454
let sub1 = stompClient.subscribe("/topic/game/" + gameId, (message) => {
5555
let game = JSON.parse(message.body)
56-
setGameState(game, auth)
56+
setGameState(game)
5757
})
5858
stompClient.publish({
5959
destination: "/app/game/hello",
@@ -161,18 +161,22 @@ function Tile({ groupInfo, onClick }) {
161161
function EmptyTile({ groupInfo, onClick }) {
162162
let { board, counting, currentPlayer, currentColor } = useGameStore(state => state.gameState)
163163
let auth = useAuthStore(state => state.auth)
164+
let setIsInCountingGroup = useGameStore(state => state.setIsInCountingGroup)
165+
let setNoCountingGroup = useCallback(() => {
166+
setIsInCountingGroup(undefined)
167+
}, [setIsInCountingGroup])
164168
let { color } = groupInfo
165169
if ((color & TERRITORY) !== 0) {
166170
return (
167-
<div className={tileClasses}>
171+
<div onMouseEnter={setNoCountingGroup} className={tileClasses}>
168172
<IconContext.Provider value={{ color: getColorClassName(color), size: "1em" }}>
169173
<FaCircle />
170174
</IconContext.Provider>
171175
</div>
172176
)
173177
}
174178
if (counting || currentPlayer !== auth.name || isForbidden(board, groupInfo, currentColor)) {
175-
return <div className={tileClasses} />
179+
return <div onMouseEnter={setNoCountingGroup} className={tileClasses} />
176180
}
177181
let classes = twJoin(
178182
tileClasses,
@@ -191,15 +195,20 @@ function EmptyTile({ groupInfo, onClick }) {
191195
}
192196

193197
function CountingTile({ groupInfo, onClick }) {
194-
let { color } = groupInfo
198+
let { color, has, x, y } = groupInfo
199+
let isInCountingGroup = useGameStore(state => state.isInCountingGroup)
200+
let setIsInCountingGroup = useGameStore(state => state.setIsInCountingGroup)
201+
let setCountingGroup = useCallback(() => {
202+
setIsInCountingGroup(has)
203+
}, [has, setIsInCountingGroup])
195204
let classes = twJoin(
196205
tileClasses,
197206
"cursor-pointer",
198207
color === BLACK ? "text-black" : "text-white",
199-
"hover:opacity-25",
208+
isInCountingGroup && isInCountingGroup(x, y) && "opacity-25",
200209
)
201210
return (
202-
<div className={classes} onClick={onClick}>
211+
<div onMouseEnter={setCountingGroup} className={classes} onClick={onClick}>
203212
<IconContext.Provider value={{ size: "2.75em" }}>
204213
<FaCircle />
205214
</IconContext.Provider>

src/main/client/src/model/PointList.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
import {
2+
PointSet,
3+
} from "./PointSet.js"
4+
15
export class PointList {
26

37
static LO = 0xffff
@@ -50,6 +54,12 @@ export class PointList {
5054
return this.pos
5155
}
5256

57+
toSet() {
58+
let result = new PointSet(this.dim)
59+
this.forEach((x, y) => result.add(x, y))
60+
return result
61+
}
62+
5363
forEach(consumer) {
5464
for (let i = 0; i < this.pos; i++) {
5565
consumer(this.x(i), this.y(i))

src/main/client/src/model/board.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import {
1111
hasStone,
1212
} from "../util.js"
1313

14-
function getGroup(board, xx, yy) {
14+
export function getGroup(board, xx, yy) {
1515
let color = board[yy][xx]
1616
let dim = board.length
1717
if (!hasStone(color)) {
@@ -76,14 +76,15 @@ function getGroup(board, xx, yy) {
7676
}
7777
}
7878
}
79+
let set = points.toSet()
7980
return {
8081
x: xx,
8182
y: yy,
8283
ptId: yy * dim + xx,
8384
color: color,
8485
hasStone: true,
8586
liberties: liberties,
86-
has: pointsChecked.has,
87+
has: (x, y) => set.has(x, y),
8788
points: points,
8889
}
8990
}

src/main/client/src/model/board.test.js

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,14 @@ import {
33
test,
44
} from "vitest"
55
import {
6+
getGroup,
67
createBoardWithGroups,
78
} from "./board.js"
89
import {
910
BLACK,
1011
WHITE,
1112
} from "../util.js"
1213

13-
1414
test("liberties", () => {
1515
let b = BLACK
1616
let w = WHITE
@@ -23,3 +23,24 @@ test("liberties", () => {
2323
let result = createBoardWithGroups(board)
2424
expect(result[0][1].liberties).toBe(1)
2525
})
26+
27+
test("has", () => {
28+
let b = BLACK
29+
let w = WHITE
30+
let board = [
31+
[0, b, w, 0],
32+
[b, b, w, 0],
33+
[w, w, w, 0],
34+
[0, 0, 0, 0],
35+
]
36+
let { has } = getGroup(board, 0, 1)
37+
expect(has(0, 0)).toBe(false)
38+
expect(has(0, 1)).toBe(true)
39+
expect(has(1, 0)).toBe(true)
40+
expect(has(1, 1)).toBe(true)
41+
expect(has(2, 0)).toBe(false)
42+
expect(has(2, 1)).toBe(false)
43+
expect(has(2, 2)).toBe(false)
44+
expect(has(1, 2)).toBe(false)
45+
expect(has(0, 2)).toBe(false)
46+
})

src/main/client/src/store.js

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import {
66
} from "immer"
77
import {
88
BLACK,
9-
WHITE,
109
} from "./util.js"
1110
import {
1211
createBoardWithGroups,
@@ -33,20 +32,25 @@ export const useAuthStore = create((set) => ({
3332
}))
3433

3534
export const useGameStore = create((set) => ({
36-
symbol: 0,
3735
editMode: false,
3836
black: {
3937
name: "",
4038
},
4139
white: {
4240
name: "",
4341
},
42+
isInCountingGroup: undefined,
43+
setIsInCountingGroup: (has) => {
44+
set(produce(state => {
45+
state.isInCountingGroup = has
46+
}))
47+
},
4448
gameState: {
4549
currentPlayer: undefined,
4650
currentColor: BLACK,
4751
counting: false,
4852
},
49-
setGameState: (game, auth) => {
53+
setGameState: (game) => {
5054
set(produce(state => {
5155
state.black = game.black
5256
state.white = game.white
@@ -55,8 +59,6 @@ export const useGameStore = create((set) => ({
5559
state.gameState.currentPlayer = game.currentPlayer
5660
state.gameState.currentColor = game.currentColor
5761
state.gameState.counting = game.counting
58-
let symbol = game.black.name === auth.name? BLACK : WHITE
59-
state.symbol = symbol
6062
}))
6163
},
6264
}))

0 commit comments

Comments
 (0)