From 248ea82573b2f39e0541814236aa8bcf7a7b1d90 Mon Sep 17 00:00:00 2001 From: Zach H Date: Sat, 24 Aug 2024 17:31:20 -0700 Subject: [PATCH] Support Game Events (#5087) --- webclient/src/types/game.ts | 7 ++++ webclient/src/websocket/events/game/index.ts | 36 +++++++++++++++++++ .../src/websocket/events/game/joinGame.ts | 6 ++++ .../src/websocket/events/game/leaveGame.ts | 7 ++++ webclient/src/websocket/events/index.ts | 1 + .../websocket/persistence/GamePersistence.ts | 12 +++++++ .../persistence/SessionPersistence.ts | 2 +- webclient/src/websocket/persistence/index.ts | 1 + .../src/websocket/services/ProtobufService.ts | 8 +++-- 9 files changed, 77 insertions(+), 3 deletions(-) create mode 100644 webclient/src/websocket/events/game/index.ts create mode 100644 webclient/src/websocket/events/game/joinGame.ts create mode 100644 webclient/src/websocket/events/game/leaveGame.ts create mode 100644 webclient/src/websocket/persistence/GamePersistence.ts diff --git a/webclient/src/types/game.ts b/webclient/src/types/game.ts index 746c119e5..b9fcc1dc2 100644 --- a/webclient/src/types/game.ts +++ b/webclient/src/types/game.ts @@ -33,3 +33,10 @@ export interface JoinGameParams { overrideRestrictions: boolean; joinAsJudge: boolean; } + +export enum LeaveGameReason { + OTHER = 1, + USER_KICKED = 2, + USER_LEFT = 3, + USER_DISCONNECTED = 4 +} diff --git a/webclient/src/websocket/events/game/index.ts b/webclient/src/websocket/events/game/index.ts new file mode 100644 index 000000000..895eadfef --- /dev/null +++ b/webclient/src/websocket/events/game/index.ts @@ -0,0 +1,36 @@ +import { ProtobufEvents } from '../../services/ProtobufService'; +import { joinGame } from './joinGame'; +import { leaveGame } from './leaveGame'; + + +export const GameEvents: ProtobufEvents = { + '.Event_Join.ext': () => joinGame, + '.Event_Leave.ext': () => leaveGame, + '.Event_GameClosed.ext': () => console.log('Event_GameClosed.ext'), + '.Event_GameHostChanged.ext': () => console.log('Event_GameHostChanged.ext'), + '.Event_Kicked.ext': () => console.log('Event_Kicked.ext'), + '.Event_GameStateChanged.ext': () => console.log('Event_GameStateChanged.ext'), + // '.Event_PlayerPropertiesChanged.ext': () => console.log("Event_PlayerProperties.ext"), + '.Event_GameSay.ext': () => console.log('Event_GameSay.ext'), + '.Event_CreateArrow.ext': () => console.log('Event_CreateArrow.ext'), + '.Event_DeleteArrow.ext': () => console.log('Event_DeleteArrow.ext'), + '.Event_CreateCounter.ext': () => console.log('Event_CreateCounter.ext'), + '.Event_SetCounter.ext': () => console.log('Event_SetCounter.ext'), + '.Event_DelCounter.ext': () => console.log('Event_DelCounter.ext'), + '.Event_DrawCards.ext': () => console.log('Event_DrawCards.ext'), + '.Event_RevealCards.ext': () => console.log('Event_RevealCards.ext'), + '.Event_Shuffle.ext': () => console.log('Event_Shuffle.ext'), + '.Event_RollDie.ext': () => console.log('Event_Roll.ext'), + '.Event_MoveCard.ext': () => console.log('Event_MoveCard.ext'), + '.Event_FlipCard.ext': () => console.log('Event_FlipCard.ext'), + '.Event_DestroyCard.ext': () => console.log('Event_DestroyCard.ext'), + '.Event_AttachCard.ext': () => console.log('Event_AttachCard.ext'), + '.Event_CreateToken.ext': () => console.log('Event_CreateToken.ext'), + '.Event_SetCardAttribute.ext': () => console.log('Event_SetCardAttribute.ext'), + '.Event_SetCardCounter.ext': () => console.log('Event_SetCardCounter.ext'), + '.Event_SetActivePlayer.ext': () => console.log('Event_SetActivePlayer.ext'), + '.Event_SetActivePhase.ext': () => console.log('Event_SetActivePhase.ext'), + '.Event_DumpZone.ext': () => console.log('Event_DumpZone.ext'), + '.Event_ChangeZoneProperties.ext': () => console.log('Event_ChangeZoneProperties.ext'), + '.Event_ReverseTurn.ext': () => console.log('Event_ReverseTurn.ext'), +}; diff --git a/webclient/src/websocket/events/game/joinGame.ts b/webclient/src/websocket/events/game/joinGame.ts new file mode 100644 index 000000000..30cef87dc --- /dev/null +++ b/webclient/src/websocket/events/game/joinGame.ts @@ -0,0 +1,6 @@ +import { GamePersistence } from '../../persistence'; +import { PlayerGamePropertiesData } from '../session/interfaces'; + +export function joinGame(playerGamePropertiesData: PlayerGamePropertiesData): void { + GamePersistence.joinGame(playerGamePropertiesData); +} diff --git a/webclient/src/websocket/events/game/leaveGame.ts b/webclient/src/websocket/events/game/leaveGame.ts new file mode 100644 index 000000000..74a4dc6c5 --- /dev/null +++ b/webclient/src/websocket/events/game/leaveGame.ts @@ -0,0 +1,7 @@ +import { LeaveGameReason } from 'types'; +import { GamePersistence } from '../../persistence'; + + +export function leaveGame(reason: LeaveGameReason): void { + GamePersistence.leaveGame(reason); +} diff --git a/webclient/src/websocket/events/index.ts b/webclient/src/websocket/events/index.ts index 00d2e4629..ed74f9b93 100644 --- a/webclient/src/websocket/events/index.ts +++ b/webclient/src/websocket/events/index.ts @@ -1,3 +1,4 @@ export * from './common'; export * from './room'; export * from './session'; +export * from './game'; diff --git a/webclient/src/websocket/persistence/GamePersistence.ts b/webclient/src/websocket/persistence/GamePersistence.ts new file mode 100644 index 000000000..e39c1b5a9 --- /dev/null +++ b/webclient/src/websocket/persistence/GamePersistence.ts @@ -0,0 +1,12 @@ +import { PlayerGamePropertiesData } from '../events/session/interfaces'; +import { LeaveGameReason } from '../../types'; + +export class GamePersistence { + static joinGame(playerGamePropertiesData: PlayerGamePropertiesData) { + console.log('joinGame', playerGamePropertiesData); + } + + static leaveGame(reason: LeaveGameReason) { + console.log('leaveGame', reason); + } +} diff --git a/webclient/src/websocket/persistence/SessionPersistence.ts b/webclient/src/websocket/persistence/SessionPersistence.ts index 62c21268d..c9248745c 100644 --- a/webclient/src/websocket/persistence/SessionPersistence.ts +++ b/webclient/src/websocket/persistence/SessionPersistence.ts @@ -10,7 +10,7 @@ import { UserMessageData } from '../events/session/interfaces'; import NormalizeService from '../utils/NormalizeService'; -import { DeckList } from '../../types/deckList'; +import { DeckList } from 'types'; import { common } from 'protobufjs'; import IBytesValue = common.IBytesValue; diff --git a/webclient/src/websocket/persistence/index.ts b/webclient/src/websocket/persistence/index.ts index 2235d505e..f4df52645 100644 --- a/webclient/src/websocket/persistence/index.ts +++ b/webclient/src/websocket/persistence/index.ts @@ -2,3 +2,4 @@ export { AdminPersistence } from './AdminPresistence'; export { RoomPersistence } from './RoomPersistence'; export { SessionPersistence } from './SessionPersistence'; export { ModeratorPersistence } from './ModeratorPresistence'; +export { GamePersistence } from './GamePersistence'; diff --git a/webclient/src/websocket/services/ProtobufService.ts b/webclient/src/websocket/services/ProtobufService.ts index 8ade17513..341bbeae3 100644 --- a/webclient/src/websocket/services/ProtobufService.ts +++ b/webclient/src/websocket/services/ProtobufService.ts @@ -1,6 +1,6 @@ import protobuf from 'protobufjs'; -import { CommonEvents, RoomEvents, SessionEvents } from '../events'; +import { CommonEvents, GameEvents, RoomEvents, SessionEvents } from '../events'; import { SessionPersistence } from '../persistence'; import { WebClient } from '../WebClient'; import { SessionCommands } from 'websocket'; @@ -95,7 +95,7 @@ export class ProtobufService { this.processSessionEvent(msg.sessionEvent, msg); break; case this.controller.ServerMessage.MessageType.GAME_EVENT_CONTAINER: - console.log(msg); + this.processGameEvent(msg.gameEvent, msg); break; default: console.log(msg); @@ -128,6 +128,10 @@ export class ProtobufService { this.processEvent(response, SessionEvents, raw); } + private processGameEvent(response: any, raw: any): void { + this.processEvent(response, GameEvents, raw); + } + private processEvent(response: any, events: ProtobufEvents, raw: any) { for (const event in events) { const payload = response[event];