-
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: created Announce UNO event
Details: - Created `announced_uno` event. - Added `runningEvents` with `vulnerableToUNO` and `hasAnnouncedUNO` subfields in engine.ts. - Created `getThrowableCards()` in eventHandlerUtils. - Handled corner cases related to the `announced_uno` event. - Created tests for the `announced_uno` event and its corner cases. Fixes: #132
- Loading branch information
1 parent
3a8332e
commit fbb9905
Showing
11 changed files
with
248 additions
and
23 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
// The game continues until a player has one or two card(s) left. | ||
// Once a player has two cards, they can announce "UNO!". | ||
// The announcement of "UNO" needs to be repeated every time the player is left with one or two cards. | ||
import assert from 'assert'; | ||
import { EventResult, GameEvent, GameEventTypes, Player } from '../../types'; | ||
import { GameEngine } from '../engine'; | ||
import { getPlayer, getThrowableCards } from './eventHandlerUtils'; | ||
|
||
export function canAnnounceUNO(game: GameEngine, player: Player): EventResult { | ||
const throwableCards = getThrowableCards(game, player); | ||
if ( | ||
player.cards.length > 2 || | ||
(player.cards.length === 2 && !throwableCards) | ||
) { | ||
return { type: 'ERROR', message: 'Cannot announce UNO' }; | ||
} | ||
|
||
return { type: 'SUCCESS', message: 'Can announce UNO' }; | ||
} | ||
|
||
export function announceUNO(game: GameEngine, event: GameEvent): EventResult { | ||
assert(event.type === GameEventTypes.ANNOUNCE_UNO, 'Invalid event type'); | ||
const player = getPlayer(game, event.playerId); | ||
if (!player) { | ||
return { type: 'ERROR', message: 'Player not found' }; | ||
} | ||
|
||
const canAnnounce = canAnnounceUNO(game, player); | ||
|
||
if (canAnnounce.type === 'ERROR') { | ||
return canAnnounce; | ||
} | ||
|
||
// if the player has announced UNO, they are no longer vulnerable to UNO | ||
game.runningEvents.hasAnnouncedUNO = player; | ||
game.runningEvents.vulnerableToUNO = null; | ||
|
||
return { type: 'SUCCESS', message: 'UNO announced 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
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
Oops, something went wrong.