diff --git a/backend/uno-game-engine/engine.ts b/backend/uno-game-engine/engine.ts index 2af035d..17bf2fe 100644 --- a/backend/uno-game-engine/engine.ts +++ b/backend/uno-game-engine/engine.ts @@ -1,4 +1,5 @@ import { getShuffledCardDeck } from './deck'; +import { handleEvent } from './gameEvents'; const NUM_CARDS_PER_PLAYER = 7; @@ -52,4 +53,8 @@ export class GameEngine { .find((p: Player) => p.id === player.id) .cards.push(this.cardDeck.pop()); } + dispatchEvent(event: GameEvent) { + // handle different types of events based on event.type + handleEvent(this, event); + } } diff --git a/backend/uno-game-engine/gameEvents.ts b/backend/uno-game-engine/gameEvents.ts new file mode 100644 index 0000000..e8126b8 --- /dev/null +++ b/backend/uno-game-engine/gameEvents.ts @@ -0,0 +1,22 @@ +// this module houses the handlers for various game events. + +import { type GameEngine } from './engine'; + +type GameEventHandler = (game: GameEngine, event: GameEvent) => EventResult; + +const map = new Map(); + +export function registerEventHandler( + eventType: GameEventType, + handler: GameEventHandler +) { + map.set(eventType, handler); +} + +export function handleEvent(game: GameEngine, event: GameEvent): EventResult { + const handler = map.get(event.type); + if (!handler) { + return { type: 'ERROR', message: 'Invalid event type' }; + } + return handler(game, event); +}