Skip to content

Commit

Permalink
server:Implement the START_GAME event handler in the engine
Browse files Browse the repository at this point in the history
This commit adds the START_GAME event handler in the engine.The event
handler can be found in event folder.This event handler starts the game
and distributes the cards among players

fixes #148
  • Loading branch information
ritwik-69 committed Jun 23, 2024
1 parent cc97d5e commit ec7dac8
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 29 deletions.
6 changes: 6 additions & 0 deletions backend/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ export enum GameEventTypes {
LEAVE_GAME = 'LEAVE_GAME',
ANNOUNCE_UNO = 'ANNOUNCE_UNO',
STATE_SYNC = 'STATE_SYNC',
START_GAME = 'START_GAME',
}
export type GameEvent =
| {
Expand Down Expand Up @@ -92,6 +93,11 @@ export type GameEvent =
currentTurn: number;
lastThrownCard: string;
};
}
| {
type: GameEventTypes.START_GAME;
playerId: string;
data?: null;
};

export enum ChatEventTypes {
Expand Down
17 changes: 17 additions & 0 deletions backend/src/uno-game-engine/events/startGame.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import assert from 'assert';
import { GameEngine } from '../engine';
import { EventResult, GameEvent } from '../../types';

export function startGame(game: GameEngine, event: GameEvent): EventResult {
assert(event.type === 'START_GAME', 'Invalid event type');
if (event.playerId === game.players[0].id) {
game.status = 'STARTED';
game.allotCards();
return { type: 'SUCCESS', message: 'Game started successfully' };
} else {
return {
type: 'ERROR',
message: 'Only the Host can start the game',
};
}
}
2 changes: 2 additions & 0 deletions backend/src/uno-game-engine/gameEvents.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { announceUNO } from './events/announceUno';
import { drawCard } from './events/drawCard';
import { joinGame } from './events/joinGame';
import { leaveGame } from './events/leaveGame';
import { startGame } from './events/startGame';
import { throwCard } from './events/throwCard';

type GameEventHandler = (game: GameEngine, event: GameEvent) => EventResult;
Expand All @@ -32,3 +33,4 @@ registerEventHandler(GameEventTypes.LEAVE_GAME, leaveGame);
registerEventHandler(GameEventTypes.DRAW_CARD, drawCard);
registerEventHandler(GameEventTypes.THROW_CARD, throwCard);
registerEventHandler(GameEventTypes.ANNOUNCE_UNO, announceUNO);
registerEventHandler(GameEventTypes.START_GAME, startGame);
58 changes: 29 additions & 29 deletions frontend/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit ec7dac8

Please sign in to comment.