-
-
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(player-attribute): decreasing and outcomes (#279)
- Loading branch information
1 parent
0e00ea0
commit a5c8b6b
Showing
14 changed files
with
17,386 additions
and
15,257 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
10 changes: 10 additions & 0 deletions
10
src/modules/game/helpers/player/player-attribute/player-attribute.helper.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,10 @@ | ||
import { GAME_PHASES } from "../../../enums/game.enum"; | ||
import type { Game } from "../../../schemas/game.schema"; | ||
import type { PlayerAttribute } from "../../../schemas/player/player-attribute/player-attribute.schema"; | ||
|
||
function isPlayerAttributeActive({ activeAt }: PlayerAttribute, game: Game): boolean { | ||
return activeAt === undefined || activeAt.turn < game.turn || | ||
activeAt.turn === game.turn && (activeAt.phase === game.phase || game.phase === GAME_PHASES.DAY); | ||
} | ||
|
||
export { isPlayerAttributeActive }; |
57 changes: 57 additions & 0 deletions
57
src/modules/game/providers/services/player/player-attribute.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,57 @@ | ||
import { Injectable } from "@nestjs/common"; | ||
import { cloneDeep } from "lodash"; | ||
import { isPlayerAttributeActive } from "../../../helpers/player/player-attribute/player-attribute.helper"; | ||
import { createPlayerDiseaseByRustySwordKnightDeath, createPlayerDeathPotionByWitchDeath, createPlayerEatenByWerewolvesDeath } from "../../../helpers/player/player-death/player-death.factory"; | ||
import type { Game } from "../../../schemas/game.schema"; | ||
import type { PlayerAttribute } from "../../../schemas/player/player-attribute/player-attribute.schema"; | ||
import type { Player } from "../../../schemas/player/player.schema"; | ||
import { PlayerKillerService } from "./player-killer.service"; | ||
|
||
@Injectable() | ||
export class PlayerAttributeService { | ||
public constructor(private readonly playerKillerService: PlayerKillerService) {} | ||
|
||
public async applyEatenAttributeOutcomes(player: Player, game: Game, attribute: PlayerAttribute): Promise<Game> { | ||
const death = createPlayerEatenByWerewolvesDeath({ source: attribute.source }); | ||
return this.playerKillerService.killOrRevealPlayer(player._id, game, death); | ||
} | ||
|
||
public async applyDrankDeathPotionAttributeOutcomes(player: Player, game: Game): Promise<Game> { | ||
const death = createPlayerDeathPotionByWitchDeath(); | ||
return this.playerKillerService.killOrRevealPlayer(player._id, game, death); | ||
} | ||
|
||
public async applyContaminatedAttributeOutcomes(player: Player, game: Game): Promise<Game> { | ||
const death = createPlayerDiseaseByRustySwordKnightDeath(); | ||
return this.playerKillerService.killOrRevealPlayer(player._id, game, death); | ||
} | ||
|
||
public decreaseRemainingPhasesAndRemoveObsoletePlayerAttributes(game: Game): Game { | ||
const clonedGame = cloneDeep(game); | ||
clonedGame.players = clonedGame.players.map(player => this.decreaseRemainingPhasesAndRemoveObsoleteAttributes(player, clonedGame)); | ||
return clonedGame; | ||
} | ||
|
||
private decreaseAttributeRemainingPhase(attribute: PlayerAttribute, game: Game): PlayerAttribute { | ||
const clonedAttribute = cloneDeep(attribute); | ||
if (clonedAttribute.remainingPhases !== undefined && isPlayerAttributeActive(clonedAttribute, game)) { | ||
clonedAttribute.remainingPhases--; | ||
} | ||
return clonedAttribute; | ||
} | ||
|
||
private decreaseRemainingPhasesAndRemoveObsoleteAttributes(player: Player, game: Game): Player { | ||
const clonedPlayer = cloneDeep(player); | ||
if (!clonedPlayer.isAlive) { | ||
return clonedPlayer; | ||
} | ||
clonedPlayer.attributes = player.attributes.reduce<PlayerAttribute[]>((acc, attribute) => { | ||
const decreasedAttribute = this.decreaseAttributeRemainingPhase(attribute, game); | ||
if (decreasedAttribute.remainingPhases === undefined || decreasedAttribute.remainingPhases > 0) { | ||
return [...acc, decreasedAttribute]; | ||
} | ||
return acc; | ||
}, []); | ||
return clonedPlayer; | ||
} | ||
} |
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.