import { useGameStore } from '@/stores/game' import { wait } from '@/common/helpers' import type { MatchSessionState, SocketEvent } from '@/common/interfaces' import { storeToRefs } from 'pinia' import { useEventBusStore } from '@/stores/eventBus' export class SocketIoEventManager { gameStore: any = useGameStore() eventBus = useEventBusStore() handleGameEvent(gameEvent: SocketEvent) { const { event, data } = gameEvent switch (event) { case 'session-created': this.updateSessionState(data) break case 'game-finished': default: this.eventBus.publish(event, data) break } } handleGameEventAck(gameEvent: SocketEvent) { const { event, data } = gameEvent try { switch (event) { case 'update-match-session-state': this.updateSessionState(data) break case 'update-game-state': this.updateGameState(data) break case 'update-player-state': this.updatePlayerState(data) break case 'ask-client-for-move': return this.handleCanMakeMoveEvent(data) default: this.eventBus.publish(event, data) break } return { status: 'ok' } } catch (error) { return { status: 'error', error } } } private updateSessionState(data: MatchSessionState) { const { updateSessionState } = this.gameStore updateSessionState(data) return { status: 'ok' } } private updateGameState(data: any) { const { updateGameState } = this.gameStore updateGameState(data) return { status: 'ok' } } private updatePlayerState(data: any) { const { updatePlayerState } = this.gameStore updatePlayerState(data) return { status: 'ok' } } private async handleCanMakeMoveEvent(data: any) { const { canMakeMove, moveToMake } = storeToRefs(this.gameStore) const { updateCanMakeMove, setIncomingFreeEnds } = this.gameStore setIncomingFreeEnds(data.freeHands) updateCanMakeMove(true) while (canMakeMove.value) { await wait(500) } return { status: 'ok', ...moveToMake.value } } private async handleCanSelectTileEvent() { const { canSelectTile } = storeToRefs(this.gameStore) const { updateCanSelectTile } = this.gameStore updateCanSelectTile(true) while (canSelectTile.value) { await wait(500) } return { status: 'ok' } } }