-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsideStackerGame.tsx
45 lines (42 loc) · 1.38 KB
/
sideStackerGame.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
'use server'
import { cookies } from 'next/headers'
import {
getMovesInGame,
joinGameOrNewGame,
loadGameForPlayer,
} from '@/lib/sqliteDb'
import { PlayStates } from '@/types/gameStateTypes'
import { SESSION_ID_COOKIE_NAME } from '@/utils/cookieUtils'
import {
buildBoardState,
getInitialGameState,
initializeBoard,
} from '@/utils/gameUtils'
import GameBoardWrapper from '@/components/gameBoardWrapper'
export default async function SideStackerGame() {
// First check for an existing session ID (player ID).
const cookieStore = cookies()
const sessionId = cookieStore.get(SESSION_ID_COOKIE_NAME)
// Initialize Board.
let gameBoardState
let playState = PlayStates.waiting
let initialBoard = initializeBoard()
// Do we have a session ID? Load an existing game & join it or start a new one.
if (sessionId?.value) {
gameBoardState = await loadGameForPlayer(sessionId.value)
if (gameBoardState?.id === -1) {
gameBoardState = await joinGameOrNewGame(sessionId.value)
}
// Now build the initial Board & set up Board & Play States.
const moves = await getMovesInGame(gameBoardState!.id)
initialBoard = buildBoardState(moves, gameBoardState!)
playState = getInitialGameState(gameBoardState!, moves)
}
return (
<GameBoardWrapper
gameBoard={initialBoard}
initialGameState={gameBoardState!}
playState={playState}
/>
)
}