Skip to content

Commit 6df8daf

Browse files
committed
some js refactoring
1 parent a422985 commit 6df8daf

File tree

7 files changed

+43
-37
lines changed

7 files changed

+43
-37
lines changed

src/main/client/src/Lobby.jsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,10 +73,10 @@ export function Lobby() {
7373
return (
7474
<div className="m-4">
7575
<div>
76-
<button type="Button"
76+
<Button type="Button"
7777
onClick={() => matchRequest(true)}>
7878
Create
79-
</button>
79+
</Button>
8080
</div>
8181
<div className="mt-2">
8282
<button type="button"

src/main/client/src/Play.jsx

Lines changed: 26 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,9 @@ import {
1515
BLACK,
1616
TERRITORY,
1717
REMOVED,
18-
hasColor,
1918
hasBlack,
19+
hasStone,
20+
getColorClassName,
2021
} from "./util.js"
2122
import {
2223
Button,
@@ -103,7 +104,6 @@ export const Play = () => {
103104
board.map((row, y) => (
104105
row.map((check, x) => (
105106
<Tile
106-
disabled={!counting && currentPlayer !== auth.name}
107107
counting={counting}
108108
key={y + "_" + x}
109109
onClick={() => onClick(x, y)}
@@ -139,48 +139,46 @@ function GridTile() {
139139
return <div className={gridTileClasses} />
140140
}
141141

142-
function Tile({check, onClick, disabled, counting}) {
143-
if (counting && hasColor(check) && (check & TERRITORY) === 0) {
144-
return (
145-
<CountingActive check={check} onClick={onClick} />
146-
)
142+
function Tile({check, onClick, counting}) {
143+
if (!hasStone(check)) {
144+
return <EmptyTile onClick={onClick} check={check} />
147145
}
148-
if (!hasColor(check) && !counting) {
149-
if (disabled) {
150-
return <div className={twJoin(tileClasses, "text-transparent")} />
151-
}
146+
if (counting) {
152147
return (
153-
<EmptyActive onClick={onClick} />
148+
<CountingActive check={check} onClick={onClick} />
154149
)
155150
}
156-
if (!hasColor(check)) {
157-
return <div className={twJoin(tileClasses, "text-transparent")} />
158-
}
159151
return (
160152
<div className={tileClasses}>
161-
<IconContext.Provider value={getStyle(check)}>
153+
<IconContext.Provider value={{ color: getColorClassName(check), size: "2.75em" }}>
162154
<FaCircle />
163155
</IconContext.Provider>
164156
</div>
165157
)
166158
}
167159

168-
function getStyle(check) {
169-
let size = (check & TERRITORY) !== 0 ? "1em" : "2.75em"
170-
if ((check & BLACK) !== 0) {
171-
return { color: "black", size: size }
172-
}
173-
return { color: "white", size: size }
174-
}
175-
176-
function EmptyActive({ onClick }) {
160+
function EmptyTile({ onClick, check }) {
161+
let { counting, currentPlayer } = useGameStore(state => state.gameState)
162+
let auth = useAuthStore(state => state.auth)
177163
let currentColor = useGameStore(state => state.gameState.currentColor)
178-
let hovercolor = currentColor === BLACK ? "hover:text-black" : "hover:text-white"
164+
if ((check & TERRITORY) !== 0) {
165+
return (
166+
<div className={tileClasses}>
167+
<IconContext.Provider value={{ color: getColorClassName(check), size: "1em" }}>
168+
<FaCircle />
169+
</IconContext.Provider>
170+
</div>
171+
)
172+
}
173+
if (counting || currentPlayer !== auth.name) {
174+
return <div className={tileClasses} />
175+
}
179176
let classes = twJoin(
180177
tileClasses,
178+
"cursor-pointer",
181179
"text-transparent",
182-
"cursor-pointer opacity-25",
183-
hovercolor,
180+
"opacity-25",
181+
currentColor === BLACK ? "hover:text-black" : "hover:text-white",
184182
)
185183
return (
186184
<div className={classes} onClick={onClick}>

src/main/client/src/util.js

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@ export const WHITE = 64
1010
export const TERRITORY = 2
1111
export const REMOVED = 4
1212

13+
const MARKERS = ~BLACK & ~WHITE;
14+
const COLORS = BLACK | WHITE;
15+
1316
export async function tfetch(url, options) {
1417
let response
1518
try {
@@ -51,8 +54,8 @@ function dec2hex(dec) {
5154
return dec.toString(16).padStart(2, "0")
5255
}
5356

54-
export function hasColor(color) {
55-
return hasBlack(color) || hasWhite(color)
57+
export function hasStone(color) {
58+
return (color & COLORS) != 0 && (color & MARKERS) == 0;
5659
}
5760

5861
export function hasBlack(color) {
@@ -62,3 +65,7 @@ export function hasBlack(color) {
6265
export function hasWhite(color) {
6366
return (color & WHITE) !== 0
6467
}
68+
69+
export function getColorClassName(color) {
70+
return hasWhite(color) ? "white" : "black"
71+
}

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ public void matchAction(MatchRequest request, Principal principal) {
5555
false,
5656
createEmptyBoard(request)));
5757
operations.convertAndSend("/topic/lobby/gamestart", game);
58+
return;
5859
}
5960
if (lookingForMatch == null) {
6061
lookingForMatch = user;

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
import static com.bernd.game.Board.B;
77
import static com.bernd.game.Board.TERRITORY;
88
import static com.bernd.game.Board.W;
9-
import static com.bernd.util.Util.isPlainColor;
9+
import static com.bernd.util.Util.isStone;
1010
import static com.bernd.util.Util.keepMarkers;
1111

1212
public class Count {
@@ -15,7 +15,7 @@ static int getImpliedColor(
1515
int[][] board,
1616
int xx,
1717
int yy) {
18-
if (isPlainColor(board[yy][xx])) {
18+
if (isStone(board[yy][xx])) {
1919
return board[yy][xx];
2020
}
2121
int dim = board.length;
@@ -27,7 +27,7 @@ static int getImpliedColor(
2727
int ptId = pointsToCheck.poll();
2828
int y = ptId / dim;
2929
int x = ptId % dim;
30-
if (isPlainColor(board[y][x])) {
30+
if (isStone(board[y][x])) {
3131
return board[y][x] | TERRITORY;
3232
}
3333
if (y > 0 && !pointsChecked.has(x, y - 1)) {

src/main/java/com/bernd/util/Util.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ public static int divUp(int i, int div) {
3636
return i % div == 0 ? i / div : (i / div) + 1;
3737
}
3838

39-
public static boolean isPlainColor(int color) {
39+
public static boolean isStone(int color) {
4040
return (color & COLORS) != 0 && (color & MARKERS) == 0;
4141
}
4242
}

src/test/java/com/bernd/game/CountTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -231,7 +231,7 @@ static int[][] createEmptyBoard(int dim) {
231231
return board;
232232
}
233233

234-
static int[][] createAccWithoutRemovedStones(int[][] board) {
234+
private static int[][] createAccWithoutRemovedStones(int[][] board) {
235235
int[][] result = new int[board.length][];
236236
for (int y = 0; y < board.length; y++) {
237237
result[y] = Arrays.copyOf(board[y], board[y].length);

0 commit comments

Comments
 (0)