-
Notifications
You must be signed in to change notification settings - Fork 42
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
gameEvent: Implement Throw Card Event
Adds the throw card event handler. Fixes #44 Signed-off-by: Sagnik Mandal <[email protected]>
- Loading branch information
Showing
5 changed files
with
109 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
import { registerEventHandler } from '../gameEvents'; | ||
import { GameEngine } from '../engine'; | ||
|
||
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); | ||
} | ||
|
||
export function throwCard(game: GameEngine, event: GameEvent): EventResult { | ||
const { currentPlayerIndex, players } = game; | ||
const currentPlayer = players[currentPlayerIndex]; | ||
|
||
// check if the player is the current player | ||
if (currentPlayer.id !== event.playerId) { | ||
return { type: 'ERROR', message: 'It is not your turn' }; | ||
} | ||
|
||
// check if the card is present in the player's hand | ||
const player = findPlayer(game, event.playerId); | ||
if (!player) { | ||
return { type: 'ERROR', message: 'Player not found' }; | ||
} | ||
const card = findCard(player, event.data.card.id); | ||
if (!card) { | ||
return { type: 'ERROR', message: 'Card not found' }; | ||
} | ||
|
||
// check if the card can be thrown | ||
if (game.lastThrownCard && !canThrowCard(game.lastThrownCard, card)) { | ||
return { type: 'ERROR', message: 'Cannot throw this card' }; | ||
} | ||
|
||
player.cards = player.cards.filter((c) => c.id !== card.id); | ||
|
||
game.thrownCards.push(card); | ||
game.lastThrownCard = card; | ||
|
||
game.nextPlayer(); | ||
|
||
// handle special cards | ||
if (card.type === 'special') { | ||
handleSpecialCard(game, card); | ||
} | ||
|
||
return { type: 'SUCCESS', message: 'Card thrown successfully' }; | ||
} | ||
|
||
function canThrowCard(lastThrownCard: UNOCard, card: UNOCard): boolean { | ||
return ( | ||
lastThrownCard.color === card.color || | ||
lastThrownCard.value === card.value || | ||
card.color === 'wild' | ||
); | ||
} | ||
|
||
function handleSpecialCard(game: GameEngine, card: UNOCard) { | ||
switch (card.value) { | ||
case 'skip': | ||
game.nextPlayer(); | ||
break; | ||
case 'reverse': | ||
game.direction *= -1; | ||
break; | ||
case 'draw2': | ||
{ | ||
const nextPlayer = game.players[game.currentPlayerIndex]; | ||
game.drawCardFromDeck(nextPlayer, 2); | ||
} | ||
break; | ||
case 'draw4': | ||
{ | ||
const currentPlayer = game.players[game.currentPlayerIndex]; | ||
game.drawCardFromDeck(currentPlayer, 4); | ||
} | ||
break; | ||
default: | ||
break; | ||
} | ||
} | ||
|
||
registerEventHandler('THROW_CARD', throwCard); |