-
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.
gameEvents: created CHALLENGE_UNO event.
- Created `challengeUno.ts`. - Checked if a player can be challenged. - Player can be challenged only if `vulnerableToUNO` is equal to that player. - If the challenge is successful, the player must draw two cards from the deck. - Created tests for the challenge_uno event and its corner cases. fixes: #145
- Loading branch information
1 parent
cc97d5e
commit 14aa71c
Showing
3 changed files
with
127 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
import assert from 'assert'; | ||
import { EventResult, GameEvent, Player } from '../../types'; | ||
import { GameEngine } from '../engine'; | ||
import { getPlayer } from './eventHandlerUtils'; | ||
|
||
export function canChallengeUNO(game: GameEngine, player: Player): EventResult { | ||
if (game.runningEvents.vulnerableToUNO !== player) { | ||
return { | ||
type: 'ERROR', | ||
message: 'Cannot challenge this player', | ||
}; | ||
} | ||
|
||
return { type: 'SUCCESS', message: 'Can challenge UNO' }; | ||
} | ||
|
||
export function challengeUNO(game: GameEngine, event: GameEvent): EventResult { | ||
assert(event.type === 'CHALLENGE_UNO', 'Invalid event type'); | ||
const player = getPlayer(game, event.playerId); | ||
if (!player) { | ||
return { type: 'ERROR', message: 'Player not found' }; | ||
} | ||
|
||
const canChallenge = canChallengeUNO(game, player); | ||
if (canChallenge.type === 'ERROR') { | ||
return canChallenge; | ||
} | ||
|
||
// draw two cards as penalty | ||
// drawCardFromDeck() will set vulnerableToUNO to null | ||
const drawPenaltyCards = game.drawCardFromDeck(player, 2); | ||
if (drawPenaltyCards.type === 'ERROR') { | ||
return drawPenaltyCards; | ||
} | ||
|
||
return { type: 'SUCCESS', message: 'UNO challenged successfully' }; | ||
} |
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