Skip to content

Commit

Permalink
set winner by time
Browse files Browse the repository at this point in the history
  • Loading branch information
h908714124 committed Sep 10, 2024
1 parent 81e5898 commit 623c662
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 13 deletions.
14 changes: 11 additions & 3 deletions src/main/client/src/feature/game/Game.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import {
StompContext,
tfetch,
doTry,
COLORS,
} from "src/util.js"
import {
PointList,
Expand All @@ -42,6 +43,7 @@ import {
paintStones,
paintStonesCounting,
paintLastMove,
paintNumber,
paintMoveNumbers,
} from "./paint.js"
import {
Expand All @@ -58,6 +60,7 @@ import {
createGameState,
isReviewing,
teleport,
setWinnerByTime,
} from "./state.js"
import {
BoardSettings,
Expand Down Expand Up @@ -85,6 +88,8 @@ function Board({gameState, setGameState}) {
let setTimeout = useTimeoutStore(state => state.setTimeout)
let timeoutRef = useRef()
timeoutRef.current = timeout
let gameStateRef = useRef()
gameStateRef.current = gameState
let [ctrlKeyDown, setCtrlKeyDown] = useState(false)
let zoom = useViewStateStore(state => state.zoom)
let {gameId} = useParams()
Expand Down Expand Up @@ -120,10 +125,13 @@ function Board({gameState, setGameState}) {
setTimeout(t)
if (t <= 0) {
clearInterval(intervalIdRef.current)
let gameState = gameStateRef.current
let updated = setWinnerByTime(gameState, currentColor(gameState) ^ COLORS)
setGameState(updated)
}
}, 1000)

}, [setTimeout, fulltime])
}, [setTimeout, fulltime, setGameState])

useEffect(() => {
resetCountdown()
Expand Down Expand Up @@ -349,8 +357,8 @@ function Board({gameState, setGameState}) {
paintMoveNumbers(context, board)
} else if (!counting && !end) {
paintLastMove(context, lastMove, timeoutRef.current)
} else {
paintLastMove(context, lastMove)
} else if (lastMove) {
paintNumber(context, lastMove.x, lastMove.y, lastMove.n + 1, lastMove.color)
}
if (currentPlayer(gameState) !== auth.name) {
return
Expand Down
20 changes: 15 additions & 5 deletions src/main/client/src/feature/game/paint.js
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,20 @@ function paintMoveNumber({cursorXref, cursorYref, stoneRadius, canvasRef, grid},
ctx.fillText(historyEntry.n + 1, x, y)
}

export function paintNumber(context, grid_x, grid_y, n, color) {
let {canvasRef, grid, stoneRadius} = context
let ctx = canvasRef.current.getContext("2d")
let [x, y] = grid[grid_y][grid_x]
let style = color === BLACK ?
"rgba(255,255,255)" :
"rgba(0,0,0)"
ctx.font = "bold " + Math.trunc(stoneRadius * 1.125) + "px sans-serif"
ctx.textBaseline = "middle"
ctx.textAlign = "center"
ctx.fillStyle = style
ctx.fillText(n, x, y)
}

export function paintLastMove(context, lastMove, countdown) {
if (!lastMove) {
return
Expand All @@ -198,11 +212,7 @@ export function paintLastMove(context, lastMove, countdown) {
let [x, y] = grid[grid_y][grid_x]
let ctx = canvasRef.current.getContext("2d")
if (countdown && countdown < 10 && countdown > 0) {
ctx.font = "bold " + Math.trunc(stoneRadius * 1.125) + "px sans-serif"
ctx.textBaseline = "middle"
ctx.textAlign = "center"
ctx.fillStyle = style
ctx.fillText(countdown, x, y)
paintNumber(context, grid_x, grid_y, countdown, color)
return
}
let length = stoneRadius * 0.875
Expand Down
22 changes: 17 additions & 5 deletions src/main/client/src/feature/game/state.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ export function initialState() {
baseBoard: [],
historyBoard: [],
viewPos: Number.NaN,
winnerByTime: 0,
dim: 0,
handicap: 0,
queueStatus: "behind",
Expand Down Expand Up @@ -97,7 +98,10 @@ export function countingAgreed({moves, myColor}) {
return move.color === myColor && move.action === "agreeCounting"
}

export function gameHasEnded({moves}) {
export function gameHasEnded({winnerByTime, moves}) {
if (winnerByTime) {
return true
}
if (!moves.length) {
return false
}
Expand Down Expand Up @@ -161,15 +165,17 @@ function goToEnd(baseState) {
let queueLength = baseState.queueLength
let baseBoard = baseState.baseBoard
let historyBoard = baseState.historyBoard
for (let i = queueLength; i < moves.length; i++) {
for (let i = baseState.viewPos; i < moves.length; i++) {
let move = moves[i]
let previousMove = getMove(moves, i - 1)
let [, updated] = updateBoardState(baseBoard, previousMove, move, true)
let [, updated] = updateBoardState(baseBoard, previousMove, move, !baseState.winnerByTime)
baseBoard = updated
}
let board = rehydrate(baseBoard, historyBoard)
return produce(baseState, (draft) => {
draft.board = rehydrate(baseBoard, historyBoard)
draft.viewPos = baseState.queueLength
draft.board = board
draft.viewPos = queueLength
draft.lastMove = getMove(moves, queueLength - 1)
})
}

Expand Down Expand Up @@ -210,6 +216,12 @@ export function addMove(baseState, move) {
})
}

export function setWinnerByTime(baseState, winnerByTime) {
return produce(baseState, (draft) => {
draft.winnerByTime = winnerByTime
})
}

export function createGameState(game, auth) {
let baseBoard = Array(game.dim)
let historyBoard = Array(game.dim)
Expand Down

0 comments on commit 623c662

Please sign in to comment.