-
-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(game-play): can be skipped field for current game play (#583)
- Loading branch information
1 parent
830ac85
commit a5908ef
Showing
35 changed files
with
18,794 additions
and
15,774 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 |
---|---|---|
@@ -1,12 +1,20 @@ | ||
import { plainToInstance } from "class-transformer"; | ||
|
||
import { GameWithCurrentPlay } from "@/modules/game/types/game-with-current-play"; | ||
import { Game } from "@/modules/game/schemas/game.schema"; | ||
|
||
import { toJSON } from "@/shared/misc/helpers/object.helper"; | ||
import { PLAIN_TO_INSTANCE_DEFAULT_OPTIONS } from "@/shared/validation/constants/validation.constant"; | ||
|
||
function createGameWithCurrentGamePlay(gameWithCurrentPlay: GameWithCurrentPlay): GameWithCurrentPlay { | ||
return plainToInstance(GameWithCurrentPlay, toJSON(gameWithCurrentPlay), PLAIN_TO_INSTANCE_DEFAULT_OPTIONS); | ||
} | ||
|
||
function createGame(game: Game): Game { | ||
return plainToInstance(Game, toJSON(game), PLAIN_TO_INSTANCE_DEFAULT_OPTIONS); | ||
} | ||
|
||
export { createGame }; | ||
export { | ||
createGameWithCurrentGamePlay, | ||
createGame, | ||
}; |
66 changes: 66 additions & 0 deletions
66
src/modules/game/providers/services/game-play/game-play-augmenter.service.ts
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,66 @@ | ||
import { Injectable } from "@nestjs/common"; | ||
|
||
import { WEREWOLF_ROLES } from "@/modules/role/constants/role.constant"; | ||
import { GamePlayActions, GamePlayCauses } from "@/modules/game/enums/game-play.enum"; | ||
import { PlayerGroups } from "@/modules/game/enums/player.enum"; | ||
import { createGamePlay } from "@/modules/game/helpers/game-play/game-play.factory"; | ||
import { getLeftToEatByWerewolvesPlayers } from "@/modules/game/helpers/game.helper"; | ||
import type { GamePlay } from "@/modules/game/schemas/game-play/game-play.schema"; | ||
import type { Game } from "@/modules/game/schemas/game.schema"; | ||
import type { GamePlaySourceName } from "@/modules/game/types/game-play.type"; | ||
import { RoleNames } from "@/modules/role/enums/role.enum"; | ||
|
||
@Injectable() | ||
export class GamePlayAugmenterService { | ||
private readonly canBeSkippedPlayMethods: Partial<Record<GamePlaySourceName, (gamePlay: GamePlay, game: Game) => boolean>> = { | ||
[PlayerGroups.CHARMED]: () => true, | ||
[PlayerGroups.LOVERS]: () => true, | ||
[PlayerGroups.SURVIVORS]: (gamePlay: GamePlay, game: Game) => this.canSurvivorsSkipGamePlay(gamePlay, game), | ||
[RoleNames.BIG_BAD_WOLF]: (gamePlay: GamePlay, game: Game) => this.canBigBadWolfSkipGamePlay(game), | ||
[RoleNames.FOX]: () => true, | ||
[RoleNames.RAVEN]: () => true, | ||
[RoleNames.SCAPEGOAT]: () => true, | ||
[RoleNames.THIEF]: (gamePlay: GamePlay, game: Game) => this.canThiefSkipGamePlay(game), | ||
[RoleNames.TWO_SISTERS]: () => true, | ||
[RoleNames.THREE_BROTHERS]: () => true, | ||
[RoleNames.WHITE_WEREWOLF]: () => true, | ||
[RoleNames.WITCH]: () => true, | ||
}; | ||
|
||
public setGamePlayCanBeSkipped(gamePlay: GamePlay, game: Game): GamePlay { | ||
const clonedGamePlay = createGamePlay(gamePlay); | ||
clonedGamePlay.canBeSkipped = this.canGamePlayBeSkipped(gamePlay, game); | ||
return clonedGamePlay; | ||
} | ||
|
||
private canSurvivorsSkipGamePlay(gamePlay: GamePlay, game: Game): boolean { | ||
const { canBeSkipped } = game.options.votes; | ||
const isGamePlayVoteCauseAngelPresence = gamePlay.action === GamePlayActions.VOTE && gamePlay.cause === GamePlayCauses.ANGEL_PRESENCE; | ||
if (gamePlay.action === GamePlayActions.ELECT_SHERIFF || isGamePlayVoteCauseAngelPresence) { | ||
return false; | ||
} | ||
return canBeSkipped; | ||
} | ||
|
||
private canBigBadWolfSkipGamePlay(game: Game): boolean { | ||
const leftToEatByWerewolvesPlayers = getLeftToEatByWerewolvesPlayers(game); | ||
return leftToEatByWerewolvesPlayers.length === 0; | ||
} | ||
|
||
private canThiefSkipGamePlay(game: Game): boolean { | ||
const { mustChooseBetweenWerewolves } = game.options.roles.thief; | ||
if (game.additionalCards === undefined || game.additionalCards.length === 0) { | ||
return true; | ||
} | ||
const areAllAdditionalCardsWerewolves = game.additionalCards.every(({ roleName }) => WEREWOLF_ROLES.find(role => role.name === roleName)); | ||
return !areAllAdditionalCardsWerewolves || !mustChooseBetweenWerewolves; | ||
} | ||
|
||
private canGamePlayBeSkipped(gamePlay: GamePlay, game: Game): boolean { | ||
const canBeSkippedGamePlayMethod = this.canBeSkippedPlayMethods[gamePlay.source.name]; | ||
if (!canBeSkippedGamePlayMethod) { | ||
return false; | ||
} | ||
return canBeSkippedGamePlayMethod(gamePlay, game); | ||
} | ||
} |
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.