-
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.
- Loading branch information
Showing
5 changed files
with
145 additions
and
9 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
// TODO: Currently the test fails, because | ||
// we should have a way to generate the set of cards, | ||
// and add them to the game. | ||
|
||
// import { createGame, retrieveGame } from '../../gameStore'; | ||
// import { handleEvent } from "../gameEvents"; | ||
|
||
// function CreateUNOTestGame() { | ||
// const gameId = createGame(); | ||
// const game = retrieveGame(gameId); | ||
// if (!game) { | ||
// throw new Error('Game not found'); | ||
// } | ||
|
||
// game.addPlayer({ id: '1', cards: [] }); | ||
// game.addPlayer({ id: '2', cards: [] }); | ||
// game.startGame(); | ||
// game.allotCards(); | ||
// return game; | ||
// } | ||
|
||
// test('Throw a card', () => { | ||
// const game = CreateUNOTestGame(); | ||
// const player = game.players[0]; | ||
// const card = player.cards[0]; | ||
// const event: GameEvent = { | ||
// type: 'THROW_CARD', | ||
// playerId: player.id, | ||
// data: { | ||
// card, | ||
// }, | ||
// }; | ||
// const result = handleEvent(game, event); | ||
// expect(result.type).toBe('SUCCESS'); | ||
// expect(player.cards.length).toBe(6); | ||
// expect(game.thrownCards.length).toBe(1); | ||
// expect(game.lastThrownCard).toBe(card); | ||
// }); |
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); |