Skip to content

Commit

Permalink
engine: Minor amendments.
Browse files Browse the repository at this point in the history
- Modified the GameEvent types to accept cardId in data.
- Fixed bug in nextPlayer logic.
- Refactored some utility functions into gameEvents, to be
used by the event handlers.
- Fixed typescript errors in throwCard.ts
  • Loading branch information
kuv2707 committed Jun 12, 2024
1 parent 460edce commit ee3bf21
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 15 deletions.
4 changes: 2 additions & 2 deletions backend/src/types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,14 @@ type GameEvent =
type: 'DRAW_CARD';
playerId: string;
data: {
card: UNOCard;
cardId: string;
};
}
| {
type: 'THROW_CARD';
playerId: string;
data: {
card: UNOCard;
cardId: string;
};
};

Expand Down
3 changes: 2 additions & 1 deletion backend/src/uno-game-engine/engine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,8 @@ export class GameEngine {
}
nextPlayer() {
this.currentPlayerIndex =
(this.currentPlayerIndex + this.direction) % this.players.length;
(this.players.length + this.currentPlayerIndex + this.direction) %
this.players.length;
}
drawCardFromDeck(player: Player, numCards = 1): EventResult {
try {
Expand Down
23 changes: 11 additions & 12 deletions backend/src/uno-game-engine/events/throwCard.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,12 @@
import { registerEventHandler } from '../gameEvents';
import { getPlayer, getPlayerCard, registerEventHandler } from '../gameEvents';
import { GameEngine } from '../engine';
import assert from 'assert';

function findPlayer(game: GameEngine, playerId: string) {
return game.players.find((p) => p.id === playerId);
}

function findCard(player, cardId: string) {
return player.cards.find((c) => c.id === cardId);
}

function canThrowCard(game: GameEngine, player, card): EventResult {
function canThrowCard(
game: GameEngine,
player: Player,
card: UNOCard
): EventResult {
const { currentPlayerIndex, players } = game;
const currentPlayer = players[currentPlayerIndex];

Expand Down Expand Up @@ -39,12 +36,14 @@ function canThrowCard(game: GameEngine, player, card): EventResult {
}

export function throwCard(game: GameEngine, event: GameEvent): EventResult {
const player = findPlayer(game, event.playerId);
// validate the event so that typescript knows that event is of type 'THROW_CARD'
assert(event.type === 'THROW_CARD', 'Invalid event type');
const player = getPlayer(game, event.playerId);
if (!player) {
return { type: 'ERROR', message: 'Player not found' };
}

const card = findCard(player, event.data.card.id);
const card = getPlayerCard(player, event.data.cardId);
if (!card) {
return { type: 'ERROR', message: 'Card not found' };
}
Expand Down
10 changes: 10 additions & 0 deletions backend/src/uno-game-engine/gameEvents.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,13 @@ export function handleEvent(game: GameEngine, event: GameEvent): EventResult {
}
return handler(game, event);
}

// some utility functions shared by event handlers

export function getPlayer(game: GameEngine, playerId: string) {
return game.players.find((p) => p.id === playerId);
}

export function getPlayerCard(player: Player, cardId: string) {
return player.cards.find((c) => c.id === cardId);
}

0 comments on commit ee3bf21

Please sign in to comment.