Skip to content

Commit

Permalink
game end
Browse files Browse the repository at this point in the history
  • Loading branch information
h908714124 committed Aug 6, 2024
1 parent 6df9887 commit 794a329
Show file tree
Hide file tree
Showing 13 changed files with 137 additions and 58 deletions.
16 changes: 11 additions & 5 deletions src/main/client/src/Game.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ export const Game = () => {
let lastStoneXref = useRef(-1)
let lastStoneYref = useRef(-1)
let [zoom, setZoom] = useState(0)
let { gameId } = useParams()
let {gameId} = useParams()
let stompClient = useContext(StompContext)
let auth = useAuthStore(state => state.auth)
let setGameState = useGameStore(state => state.setGameState)
Expand All @@ -50,11 +50,11 @@ export const Game = () => {
let currentPlayer = useGameStore(state => state.currentPlayer)
let counting = useGameStore(state => state.counting)
let currentColor = useGameStore(state => state.currentColor)
let { board, forbidden } = useGameStore(state => state.gameState)
let {board, forbidden, gameHasEnded} = useGameStore(state => state.gameState)
let [forbidden_x, forbidden_y] = forbidden
let initialized = useRef()
let canvasRef = useRef()
let countingGroup = counting() ? getCountingGroup(board, cursor_x, cursor_y) : undefined
let countingGroup = !gameHasEnded && counting() ? getCountingGroup(board, cursor_x, cursor_y) : undefined

let context = useMemo(() => {
let dim = board.length
Expand Down Expand Up @@ -118,6 +118,9 @@ export const Game = () => {
}, [board.length, canvasRef, zoom])

let onMouseMove = useCallback((e) => {
if (gameHasEnded) {
return
}
if (!board.length) {
return
}
Expand All @@ -128,9 +131,12 @@ export const Game = () => {
let cursor_y = Math.round((e.nativeEvent.offsetY - context.margin) / context.step)
setCursor_x(cursor_x + 0)
setCursor_y(cursor_y + 0)
}, [context, currentPlayer, auth, board.length, counting])
}, [context, currentPlayer, auth, board.length, counting, gameHasEnded])

let onClick = useCallback((e) => {
if (gameHasEnded) {
return
}
if (!board.length) {
return
}
Expand Down Expand Up @@ -165,7 +171,7 @@ export const Game = () => {
y: cursor_y,
}),
})
}, [context, currentPlayer, currentColor, auth, board, gameId, stompClient, counting, forbidden_x, forbidden_y, queueLength])
}, [context, currentPlayer, currentColor, auth, board, gameId, stompClient, counting, forbidden_x, forbidden_y, queueLength, gameHasEnded])

useEffect(() => {
if (!board.length) {
Expand Down
4 changes: 2 additions & 2 deletions src/main/client/src/component/Button.jsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import {
twJoin,
twMerge,
} from "tailwind-merge"

export const Button = ({ type, children, disabled, className, onClick, ...rest }) => {
let classes = twJoin(
let classes = twMerge(
"border-2 border-slate-600 rounded-lg px-8 py-2",
disabled && "text-slate-500 bg-slate-200 border-slate-400 border-2",
!disabled && "hover:text-white text-slate-200 hover:border-sky-700",
Expand Down
38 changes: 28 additions & 10 deletions src/main/client/src/feature/GamePanel.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,9 @@ function Panel({zoom, setZoom}) {
let white = useGameStore(state => state.white)
let queueLength = useGameStore(state => state.queueLength)
let counting = useGameStore(state => state.counting)
let countingComplete = useGameStore(state => state.countingComplete)
let currentPlayer = useGameStore(state => state.currentPlayer)
let { board } = useGameStore(state => state.gameState)
let { board, gameHasEnded } = useGameStore(state => state.gameState)
let navigate = useNavigate()
let onExit = useCallback(() => {
navigate(base + "/lobby")
Expand All @@ -76,10 +77,20 @@ function Panel({zoom, setZoom}) {
}),
})
}, [stompClient, gameId, queueLength])
let onCountingAgree = useCallback(() => {
stompClient.publish({
destination: "/app/game/move",
body: JSON.stringify({
id: gameId,
n: queueLength(),
agreeCounting: true,
}),
})
}, [stompClient, gameId, queueLength])
if (!board.length) {
return <span>Loading...</span>
}
let result = counting() ? getScore(board) : undefined
let result = gameHasEnded ? getScore(board) : undefined
return (
<>
<div className="inline-flex gap-x-2">
Expand Down Expand Up @@ -123,21 +134,33 @@ function Panel({zoom, setZoom}) {
<div>vs</div>
<div>{black.name}</div>
</div>
<div>Move {queueLength()}</div>
<div className="mt-2">
<Button
onClick={onPass}
disabled={counting() || currentPlayer() !== auth.name}>
className="py-1 px-4"
disabled={gameHasEnded || counting() || currentPlayer() !== auth.name}>
Pass
</Button>
</div>
{counting() && (
{counting() && <>
<div className="mt-2">
<Button
className="py-1 px-4"
disabled={gameHasEnded}
onClick={onResetCounting}>
Reset Counting
</Button>
</div>
)}
<div className="mt-2">
<Button
disabled={gameHasEnded || !countingComplete()}
className="py-1 px-4"
onClick={onCountingAgree}>
OK
</Button>
</div>
</>}
{result && (
<div className="mt-4">
<div>
Expand All @@ -151,11 +174,6 @@ function Panel({zoom, setZoom}) {
</div>
</div>
)}
{!counting() && (
<div className="mt-2">
{currentPlayer() + " ist dran..."}
</div>
)}
</>
)
}
Expand Down
78 changes: 54 additions & 24 deletions src/main/client/src/store.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ import {
import {
produce,
} from "immer"
import {
persist,
} from "zustand/middleware"
import {
BLACK,
WHITE,
Expand All @@ -23,41 +26,56 @@ import {
resetCounting,
} from "./model/count.js"

export const useAuthStore = create((set) => ({
auth: {
name: "",
state: "anonymous",
token: "",
},
setAuth: (payload) => {
set(produce(state => {
state.auth.name = payload.name
state.auth.state = "authenticated"
state.auth.token = payload.token
}))
},
setPending: (b) => {
set(produce(state => {
state.auth.state = b ? "pending" : "anonymous"
}), true)
},
}))
export const useAuthStore = create(
persist(
(set) => ({
auth: {
name: "",
state: "anonymous",
token: "",
},
setAuth: (payload) => {
set(produce(state => {
state.auth.name = payload.name
state.auth.state = "authenticated"
state.auth.token = payload.token
}))
},
setPending: (b) => {
set(produce(state => {
state.auth.state = b ? "pending" : "anonymous"
}), true)
},
}),
{ name: "auth-storage" },
),
)

export const useGameStore = create((set, get) => ({
moves: [],
baseBoard: [],
dim: 0,
queueStatus: "behind",
black: {
name: "",
},
white: {
name: "",
},
isInCountingGroup: undefined,
setIsInCountingGroup: (has) => {
set(produce(state => {
state.isInCountingGroup = has
}))
countingComplete: () => {
if (!get().counting()) {
return false
}
let baseBoard = get().baseBoard
let dim = get().dim
for (let y = 0; y < dim; y++) {
for (let x = 0; x < dim; x++) {
if (!baseBoard[y][x]) {
return false
}
}
}
return true
},
currentPlayer: () => {
let moves = get().moves
Expand Down Expand Up @@ -88,9 +106,15 @@ export const useGameStore = create((set, get) => ({
gameState: {
board: [],
forbidden: [-1, -1],
gameHasEnded: false,
},
addMove: (move) => {
set(produce(state => {
if (move.end) {
state.moves.push(move)
state.gameState.gameHasEnded = true
return
}
let moves = get().moves
let baseBoard = get().baseBoard
if (move.n < moves.length) {
Expand Down Expand Up @@ -119,11 +143,17 @@ export const useGameStore = create((set, get) => ({
let moves = []
let forbidden = [-1, -1]
for (let move of game.moves) {
if (move.end) {
moves.push(move)
state.gameState.gameHasEnded = true
break
}
let [storedMove, updated, newForbidden] = createMoveData(baseBoard, moves, move)
moves.push(storedMove)
forbidden = newForbidden
baseBoard = updated
}
state.dim = game.dim
state.baseBoard = baseBoard
state.moves = moves
state.gameState.board = rehydrate(baseBoard)
Expand Down
7 changes: 5 additions & 2 deletions src/main/java/com/bernd/GameController.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import com.bernd.game.Board;
import com.bernd.model.AcceptRequest;
import com.bernd.model.ActiveGame;
import com.bernd.model.CountingMove;
import com.bernd.model.Game;
import com.bernd.model.Move;
import com.bernd.model.OpenGame;
Expand Down Expand Up @@ -68,7 +67,11 @@ public void action(Move move, Principal principal) {
}
Game updated = game.update(move);
games.put(updated);
operations.convertAndSend("/topic/move/" + game.id(), move.toView(color, updated.counting()));
if (updated.gameHasEnded()) {
operations.convertAndSend("/topic/move/" + game.id(), move.gameEnd(color, updated.counting()));
} else if (!move.agreeCounting()) {
operations.convertAndSend("/topic/move/" + game.id(), move.toView(color, updated.counting()));
}
}

@ResponseBody
Expand Down
1 change: 1 addition & 0 deletions src/main/java/com/bernd/LobbyController.java
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ public ViewGame startEdit(@RequestBody MatchRequest request) {
user,
user,
false,
0,
principal,
B,
false,
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/com/bernd/game/MoveList.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public static MoveList create(int dim) {
return new MoveList(dim, new int[16]);
}

public void gameEnd() {
public void addGameEndMarker() {
ensureCapacity();
set(GAME_END);
pos++;
Expand Down
23 changes: 20 additions & 3 deletions src/main/java/com/bernd/model/Game.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ public record Game(
User black,
User white,
boolean counting,
int countingAgreed,
String currentPlayer,
int currentColor,
boolean opponentPassed,
Expand All @@ -42,6 +43,12 @@ public Game update(Move move) {
}

private Game updateInternal(Move move) {
if (move.agreeCounting()) {
if ((countingAgreed | currentColor()) == COLORS) {
moves.addGameEndMarker();
}
return countingAgreed(countingAgreed | currentColor());
}
moves.add(currentColor, move, counting);
if (counting) {
if (move.resetCounting()) {
Expand All @@ -55,7 +62,7 @@ private Game updateInternal(Move move) {
if (opponentPassed) {
return startCounting();
}
return game(board, counting, true, NOT_FORBIDDEN);
return game(board, counting, 0, true, NOT_FORBIDDEN);
}
int x = move.x();
int y = move.y();
Expand Down Expand Up @@ -124,13 +131,15 @@ private Direction getDirection(
private Game game(
int[][] board,
boolean counting,
int countingAgreed,
boolean opponentPassed,
int[] forbidden) {
return new Game(
id,
black,
white,
counting,
countingAgreed,
nextPlayer(),
nextColor(),
opponentPassed,
Expand All @@ -142,11 +151,11 @@ private Game game(
}

private Game game(int[][] board, int[] forbidden) {
return game(board, counting, false, forbidden);
return game(board, counting, 0, false, forbidden);
}

private Game startCounting() {
return game(Count.count(board), true, true, NOT_FORBIDDEN);
return game(Count.count(board), true, 0, true, NOT_FORBIDDEN);
}

private String nextPlayer() {
Expand All @@ -163,4 +172,12 @@ private int nextColor() {
public ViewGame toView() {
return ViewGame.fromGame(this);
}

private Game countingAgreed(int countingAgreed) {
return game(board, counting, countingAgreed, opponentPassed, forbidden);
}

public boolean gameHasEnded() {
return countingAgreed == COLORS;
}
}
8 changes: 8 additions & 0 deletions src/main/java/com/bernd/model/GameEndMove.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package com.bernd.model;

public record GameEndMove(boolean gameHasEnded) {

public static GameEndMove create() {
return new GameEndMove(true);
}
}
Loading

0 comments on commit 794a329

Please sign in to comment.