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}
|
|
|
|
|
2021-09-24 00:47:45 -04:00
|
|
|
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' }
|
2021-09-24 00:47:45 -04:00
|
|
|
| { 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 {
|
2021-09-24 00:47:45 -04:00
|
|
|
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'}
|
|
|
|
|
2021-09-24 00:47:45 -04:00
|
|
|
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')
|
2021-09-24 00:47:45 -04:00
|
|
|
dispatch({type: 'open'})
|
2021-09-23 22:37:47 -04:00
|
|
|
}, [])
|
|
|
|
|
|
|
|
const onError = React.useCallback((event: WebSocketEventMap['error']) => {
|
|
|
|
console.error(event)
|
2021-09-24 00:47:45 -04:00
|
|
|
dispatch({type: 'error'})
|
2021-09-23 22:37:47 -04:00
|
|
|
}, [])
|
|
|
|
|
|
|
|
const onClose = React.useCallback((_event: WebSocketEventMap['close']) => {
|
|
|
|
console.log('socket closed')
|
2021-09-24 00:47:45 -04:00
|
|
|
dispatch({type: 'close'})
|
2021-09-23 22:37:47 -04:00
|
|
|
}, [])
|
|
|
|
|
2021-09-24 00:47:45 -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},
|
|
|
|
)
|
|
|
|
|
2021-09-24 00:47:45 -04:00
|
|
|
const sendJson = React.useCallback((message: SocketMessage) => {
|
|
|
|
socket.send(JSON.stringify(message))
|
|
|
|
}, [socket])
|
|
|
|
|
2021-09-23 22:37:47 -04:00
|
|
|
const handle = React.useMemo(() => ({
|
2021-09-24 00:47:45 -04:00
|
|
|
// session commands
|
|
|
|
query: () => {},
|
|
|
|
join: () => {},
|
|
|
|
leave: () => {},
|
|
|
|
play: () => {},
|
|
|
|
poll: () => {},
|
|
|
|
// game commands
|
|
|
|
act: () => {},
|
|
|
|
getState: () => {},
|
|
|
|
debug: () => {},
|
|
|
|
}), [sendJson])
|
2021-09-23 22:37:47 -04:00
|
|
|
|
|
|
|
switch (state.status) {
|
2021-09-24 00:47:45 -04:00
|
|
|
case 'in-game': {
|
2021-09-23 22:37:47 -04:00
|
|
|
return {status: 'connected', handle}
|
|
|
|
}
|
2021-09-24 00:47:45 -04:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|