Skip to content

Commit

Permalink
engine: Implement structure for game event handling.
Browse files Browse the repository at this point in the history
Created a module to deal with executing events
on the game object.
  • Loading branch information
kuv2707 committed Jun 1, 2024
1 parent 09b7759 commit 193329e
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 0 deletions.
5 changes: 5 additions & 0 deletions backend/uno-game-engine/engine.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { getShuffledCardDeck } from './deck';
import { handleEvent } from './gameEvents';

const NUM_CARDS_PER_PLAYER = 7;

Expand Down Expand Up @@ -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);
}
}
22 changes: 22 additions & 0 deletions backend/uno-game-engine/gameEvents.ts
Original file line number Diff line number Diff line change
@@ -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<GameEventType, GameEventHandler>();

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);
}

0 comments on commit 193329e

Please sign in to comment.