tome-web/client/game/useSocket.ts

134 lines
3.2 KiB
TypeScript
Raw Normal View History

2021-09-23 22:37:47 -04:00
import React from 'react'
import useWebSocket from 'react-use-websocket'
import assertNever from '~/common/assertNever.ts'
import { useUser } from '../user.tsx'
function shouldReconnect() {
console.log('Reconnecting...')
return true
}
type AsyncHandle<T> =
| { status: 'not-connected' }
| { status: 'connecting' }
| { status: 'connected'; handle: T}
interface SessionCommandAPI {
query: () => void
join: () => void
leave: () => void
play: () => void
poll: () => void
}
interface GameCommandAPI {
act: () => void,
getState: () => void,
debug: () => void,
}
// TODO
type GameCommandEnum = 'a' | 's' | 'd'
interface SocketMessage {
playerId?: string
matchId?: string
command: keyof SessionCommandAPI
gameCommand?: {
playerId: string
type: GameCommandEnum,
cmd: unknown
}
}
2021-09-23 22:37:47 -04:00
type State =
| { status: 'not-connected' }
| { status: 'connecting' }
| { status: 'finding-game' }
| { status: 'in-game'; playerId: string; matchId: string }
type Action =
| { type: 'open' }
| { type: 'close' }
| { type: 'error' }
| { type: 'game-found'; matchId: string; playerId: string }
2021-09-23 22:37:47 -04:00
function reducer(_state: State, action: Action): State {
switch (action.type) {
case 'open': return { status: 'finding-game' }
case 'game-found': return { status: 'in-game', matchId: action.matchId, playerId: action.playerId }
2021-09-23 22:37:47 -04:00
case 'close': return { status: 'not-connected' }
case 'error': return { status: 'connecting' }
default: return assertNever(action)
}
}
const initialState: State = {status: 'not-connected'}
export default function useSocket(): AsyncHandle<GameCommandAPI> {
const profile = useUser()
2021-09-23 22:37:47 -04:00
const [state, dispatch] = React.useReducer(reducer, initialState)
const onMessage = React.useCallback((message: WebSocketEventMap['message']) => {
const data = JSON.parse(message.data)
dispatch({ type: 'update', ...data })
}, [])
const onOpen = React.useCallback(() => {
console.log('socket opened')
dispatch({type: 'open'})
2021-09-23 22:37:47 -04:00
}, [])
const onError = React.useCallback((event: WebSocketEventMap['error']) => {
console.error(event)
dispatch({type: 'error'})
2021-09-23 22:37:47 -04:00
}, [])
const onClose = React.useCallback((_event: WebSocketEventMap['close']) => {
console.log('socket closed')
dispatch({type: 'close'})
2021-09-23 22:37:47 -04:00
}, [])
const url = React.useMemo(
// () => `ws://arcade.saintnet.tech:7636/ws?name=${profile.displayName}`,
() => `ws://arcade.saintnet.tech:7636/ws`,
[profile],
2021-09-23 22:37:47 -04:00
)
const socket = useWebSocket(
url,
{onMessage, onOpen, onError, onClose, shouldReconnect},
)
const sendJson = React.useCallback((message: SocketMessage) => {
socket.send(JSON.stringify(message))
}, [socket])
2021-09-23 22:37:47 -04:00
const handle = React.useMemo(() => ({
// session commands
query: () => {},
join: () => {},
leave: () => {},
play: () => {},
poll: () => {},
// game commands
act: () => {},
getState: () => {},
debug: () => {},
}), [sendJson])
2021-09-23 22:37:47 -04:00
switch (state.status) {
case 'in-game': {
2021-09-23 22:37:47 -04:00
return {status: 'connected', handle}
}
case 'finding-game': {
return {status: 'connecting'}
}
2021-09-23 22:37:47 -04:00
case 'connecting':
case 'not-connected':
return state
default: return assertNever(state)
}
}