2021-09-24 06:51:17 -04:00
|
|
|
import React from 'react'
|
|
|
|
|
|
|
|
import assertNever from '~/common/assertNever.ts'
|
|
|
|
|
|
|
|
import Loading from '../components/Loading.tsx'
|
|
|
|
|
|
|
|
import {
|
|
|
|
AsyncHandle,
|
2021-09-24 22:51:26 -04:00
|
|
|
Card,
|
2021-09-24 06:51:17 -04:00
|
|
|
FighterArea,
|
|
|
|
GameState,
|
|
|
|
GameAction,
|
|
|
|
GameCommandAPI,
|
|
|
|
} from './types.ts'
|
|
|
|
import useServerSocket from './useServerSocket.ts'
|
|
|
|
import Game from './Game.tsx'
|
|
|
|
|
2021-09-24 22:51:26 -04:00
|
|
|
interface GameClientState extends GameState {
|
|
|
|
isDrawing: boolean
|
|
|
|
}
|
|
|
|
|
|
|
|
type GameClientAction = GameAction
|
|
|
|
|
|
|
|
function reducer(state: GameClientState, action: GameClientAction): GameClientState {
|
2021-09-24 06:51:17 -04:00
|
|
|
switch (action.type) {
|
|
|
|
case 'update-state': {
|
2021-09-24 22:51:26 -04:00
|
|
|
return {...state, ...action.state}
|
2021-09-24 06:51:17 -04:00
|
|
|
}
|
2021-09-24 22:51:26 -04:00
|
|
|
case 'set-player-team': {
|
2021-09-24 06:51:17 -04:00
|
|
|
return {...state, team: action.team}
|
|
|
|
}
|
2021-09-24 22:51:26 -04:00
|
|
|
case 'play-card': {
|
|
|
|
const nextHand = [
|
|
|
|
...state.player.hand.slice(0, action.handIndex),
|
|
|
|
...state.player.hand.slice(action.handIndex + 1),
|
|
|
|
]
|
|
|
|
return {
|
|
|
|
...state,
|
|
|
|
player: { ...state.player, hand: nextHand },
|
|
|
|
}
|
|
|
|
}
|
|
|
|
case 'receive-cards': {
|
|
|
|
// first, i can draw and i am not yet drawing
|
|
|
|
// scry N cards from the deck
|
|
|
|
if (state.canDraw && !state.isDrawing) {
|
|
|
|
return {
|
|
|
|
...state,
|
|
|
|
isDrawing: true,
|
|
|
|
drawChoices: action.cards,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// then, i am drawing, i can still draw, and I haven't yet drawn.
|
|
|
|
// i draw a card into my hand
|
|
|
|
if (state.canDraw && state.isDrawing && !state.hasDrawn) {
|
|
|
|
return {
|
|
|
|
...state,
|
|
|
|
drawChoices: [],
|
|
|
|
player: {
|
|
|
|
...state.player,
|
|
|
|
hand: action.cards
|
|
|
|
},
|
|
|
|
isDrawing: false,
|
|
|
|
hasDrawn: true,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// then, i am no longer drawing and cannot draw. this is the board.
|
|
|
|
const team = state.team === 1 ? 'sentinal' : 'scourge' as const
|
|
|
|
return {
|
|
|
|
...state,
|
|
|
|
board: {
|
|
|
|
...state.board,
|
|
|
|
[team]: action.cards,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
2021-09-24 06:51:17 -04:00
|
|
|
default: return state
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-24 22:51:26 -04:00
|
|
|
const initialState: GameClientState = {
|
2021-09-24 06:51:17 -04:00
|
|
|
board: {
|
|
|
|
sentinal: Array(4).fill(undefined) as FighterArea,
|
|
|
|
scourge: Array(4).fill(undefined) as FighterArea,
|
|
|
|
},
|
|
|
|
player: {
|
|
|
|
name: '',
|
|
|
|
id: 0,
|
|
|
|
hand: [],
|
|
|
|
life: 0,
|
|
|
|
ready: false,
|
|
|
|
},
|
2021-09-24 22:51:26 -04:00
|
|
|
deck: {
|
|
|
|
cards: []
|
|
|
|
},
|
2021-09-24 06:51:17 -04:00
|
|
|
team: 1,
|
|
|
|
enemyLife: 0,
|
|
|
|
enemyDeckSize: 0,
|
|
|
|
enemyHandSize: 0,
|
|
|
|
currentTurn: 1,
|
|
|
|
canDraw: false,
|
|
|
|
hasDrawn: false,
|
|
|
|
gameStatus: 0,
|
2021-09-24 22:51:26 -04:00
|
|
|
isDrawing: false,
|
|
|
|
drawChoices: [],
|
2021-09-24 06:51:17 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
export default function GameClient(): JSX.Element {
|
|
|
|
const [state, dispatch] = React.useReducer(reducer, initialState)
|
|
|
|
|
2021-09-24 22:51:26 -04:00
|
|
|
const handleGameUpdate = React.useCallback((action: GameAction) => {
|
|
|
|
dispatch(action)
|
2021-09-24 06:51:17 -04:00
|
|
|
}, [])
|
|
|
|
|
|
|
|
const socketHandle = useServerSocket(handleGameUpdate)
|
|
|
|
|
2021-09-24 22:51:26 -04:00
|
|
|
const {team} = state
|
2021-09-24 06:51:17 -04:00
|
|
|
const gameHandle = React.useMemo<AsyncHandle<GameCommandAPI>>(
|
|
|
|
() => {
|
|
|
|
if (socketHandle.status !== 'connected') return socketHandle
|
|
|
|
return {
|
|
|
|
status: 'connected',
|
|
|
|
handle: {
|
|
|
|
readyPlayer: () => {
|
|
|
|
if (socketHandle.status !== 'connected') return
|
|
|
|
socketHandle.handle.sendGameCommand({
|
|
|
|
type: 's',
|
|
|
|
cmd: 'b',
|
2021-09-24 22:51:26 -04:00
|
|
|
player_id: team,
|
2021-09-24 06:51:17 -04:00
|
|
|
})
|
|
|
|
},
|
|
|
|
startTurn: () => {
|
|
|
|
if (socketHandle.status !== 'connected') return
|
|
|
|
socketHandle.handle.sendGameCommand({
|
|
|
|
type: 's',
|
|
|
|
cmd: 's',
|
2021-09-24 22:51:26 -04:00
|
|
|
player_id: team,
|
2021-09-24 06:51:17 -04:00
|
|
|
})
|
|
|
|
},
|
|
|
|
endTurn: () => {
|
|
|
|
if (socketHandle.status !== 'connected') return
|
|
|
|
socketHandle.handle.sendGameCommand({
|
|
|
|
type: 's',
|
|
|
|
cmd: 'e',
|
2021-09-24 22:51:26 -04:00
|
|
|
player_id: team,
|
2021-09-24 06:51:17 -04:00
|
|
|
})
|
|
|
|
},
|
|
|
|
getView: () => {
|
|
|
|
if (socketHandle.status !== 'connected') return
|
|
|
|
socketHandle.handle.sendGameCommand({
|
|
|
|
type: 's',
|
|
|
|
cmd: 'g',
|
2021-09-24 22:51:26 -04:00
|
|
|
player_id: team,
|
2021-09-24 06:51:17 -04:00
|
|
|
})
|
|
|
|
},
|
|
|
|
startDraw: () => {
|
|
|
|
if (socketHandle.status !== 'connected') return
|
|
|
|
socketHandle.handle.sendGameCommand({
|
|
|
|
type: 'a',
|
|
|
|
cmd: 's',
|
2021-09-24 22:51:26 -04:00
|
|
|
player_id: team,
|
2021-09-24 06:51:17 -04:00
|
|
|
})
|
|
|
|
},
|
|
|
|
commitDraw: (cardIndex: number) => {
|
|
|
|
if (socketHandle.status !== 'connected') return
|
2021-09-24 22:51:26 -04:00
|
|
|
// dispatch({type})
|
2021-09-24 06:51:17 -04:00
|
|
|
socketHandle.handle.sendGameCommand({
|
|
|
|
type: 'a',
|
|
|
|
cmd: `d ${cardIndex}`,
|
2021-09-24 22:51:26 -04:00
|
|
|
player_id: team,
|
2021-09-24 06:51:17 -04:00
|
|
|
})
|
|
|
|
},
|
|
|
|
playCard: (handIndex: number, positionIndex: number) => {
|
|
|
|
if (socketHandle.status !== 'connected') return
|
2021-09-24 22:51:26 -04:00
|
|
|
dispatch({ type: 'play-card', handIndex })
|
2021-09-24 06:51:17 -04:00
|
|
|
socketHandle.handle.sendGameCommand({
|
|
|
|
type: 'a',
|
|
|
|
cmd: `p ${handIndex} ${positionIndex}`,
|
2021-09-24 22:51:26 -04:00
|
|
|
player_id: team,
|
2021-09-24 06:51:17 -04:00
|
|
|
})
|
|
|
|
},
|
|
|
|
moveCard: (positionFrom: number, positionTo: number) => {
|
|
|
|
if (socketHandle.status !== 'connected') return
|
|
|
|
socketHandle.handle.sendGameCommand({
|
|
|
|
type: 'a',
|
|
|
|
cmd: `m ${positionFrom} ${positionTo}`,
|
2021-09-24 22:51:26 -04:00
|
|
|
player_id: team,
|
2021-09-24 06:51:17 -04:00
|
|
|
})
|
|
|
|
},
|
|
|
|
attackCard: (positionFrom: number, positionTo: number) => {
|
|
|
|
if (socketHandle.status !== 'connected') return
|
|
|
|
socketHandle.handle.sendGameCommand({
|
|
|
|
type: 'a',
|
|
|
|
cmd: `a ${positionFrom} ${positionTo}`,
|
2021-09-24 22:51:26 -04:00
|
|
|
player_id: team,
|
2021-09-24 06:51:17 -04:00
|
|
|
})
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
},
|
2021-09-24 22:51:26 -04:00
|
|
|
[socketHandle, team],
|
2021-09-24 06:51:17 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
React.useEffect(() => {
|
|
|
|
if (gameHandle.status !== 'connected') return
|
|
|
|
gameHandle.handle.readyPlayer()
|
2021-09-24 22:59:49 -04:00
|
|
|
gameHandle.handle.readyPlayer()
|
2021-09-24 22:51:26 -04:00
|
|
|
}, [gameHandle, team])
|
2021-09-24 06:51:17 -04:00
|
|
|
|
|
|
|
switch (gameHandle.status) {
|
|
|
|
case 'connected': {
|
|
|
|
return <Game {...state} {...gameHandle.handle} />
|
|
|
|
}
|
|
|
|
case 'not-connected':
|
|
|
|
case 'connecting': {
|
|
|
|
return <Loading />
|
|
|
|
}
|
|
|
|
default: return assertNever(gameHandle)
|
|
|
|
}
|
|
|
|
}
|