Skip to content

Commit

Permalink
feat(player-attribute): decreasing and outcomes (#279)
Browse files Browse the repository at this point in the history
  • Loading branch information
antoinezanardi committed Jun 27, 2023
1 parent 0e00ea0 commit a5c8b6b
Show file tree
Hide file tree
Showing 14 changed files with 17,386 additions and 15,257 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@ import type { ApiPropertyOptions } from "@nestjs/swagger";
import type { PlayerAttribute } from "../../../schemas/player/player-attribute/player-attribute.schema";
import { gameSourceValues } from "../../game.constant";

const playerAttributeFieldsSpecs = Object.freeze({ remainingPhases: { minimum: 1 } });
const playerAttributeFieldsSpecs = Object.freeze({
remainingPhases: { minimum: 1 },
doesRemainAfterDeath: { default: false },
});

const playerAttributeApiProperties: Record<keyof PlayerAttribute, ApiPropertyOptions> = Object.freeze({
name: { description: "Attribute's name on the player." },
Expand All @@ -15,6 +18,7 @@ const playerAttributeApiProperties: Record<keyof PlayerAttribute, ApiPropertyOpt
...playerAttributeFieldsSpecs.remainingPhases,
},
activeAt: { description: "When the attribute will become active and will have consequences on players. Used for attributes with delay. If not set, the attribute is immediately active." },
doesRemainAfterDeath: { description: "If the attribute is removed on player's death" },
});

export { playerAttributeApiProperties, playerAttributeFieldsSpecs };
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ function createPowerlessByFoxPlayerAttribute(playerAttribute: Partial<PlayerAttr
return createPlayerAttribute({
name: PLAYER_ATTRIBUTE_NAMES.POWERLESS,
source: ROLE_NAMES.FOX,
doesRemainAfterDeath: true,
...playerAttribute,
});
}
Expand All @@ -65,6 +66,7 @@ function createPowerlessByAncientPlayerAttribute(playerAttribute: Partial<Player
return createPlayerAttribute({
name: PLAYER_ATTRIBUTE_NAMES.POWERLESS,
source: ROLE_NAMES.ANCIENT,
doesRemainAfterDeath: true,
...playerAttribute,
});
}
Expand Down Expand Up @@ -161,6 +163,7 @@ function createSheriffBySheriffPlayerAttribute(playerAttribute: Partial<PlayerAt
return createPlayerAttribute({
name: PLAYER_ATTRIBUTE_NAMES.SHERIFF,
source: PLAYER_ATTRIBUTE_NAMES.SHERIFF,
doesRemainAfterDeath: true,
...playerAttribute,
});
}
Expand All @@ -169,6 +172,7 @@ function createSheriffByAllPlayerAttribute(playerAttribute: Partial<PlayerAttrib
return createPlayerAttribute({
name: PLAYER_ATTRIBUTE_NAMES.SHERIFF,
source: PLAYER_GROUPS.ALL,
doesRemainAfterDeath: true,
...playerAttribute,
});
}
Expand Down
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 };
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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,12 @@ export class PlayerKillerService {
death.cause === PLAYER_DEATH_CAUSES.VOTE;
}

private removePlayerAttributesAfterDeath(player: Player): Player {
const clonedPlayer = cloneDeep(player);
clonedPlayer.attributes = clonedPlayer.attributes.filter(({ doesRemainAfterDeath }) => doesRemainAfterDeath === true);
return clonedPlayer;
}

private async getAncientLivesCountAgainstWerewolves(game: Game): Promise<number> {
const { livesCountAgainstWerewolves } = game.options.roles.ancient;
const werewolvesEatAncientRecords = await this.gameHistoryRecordService.getGameHistoryWerewolvesEatAncientRecords(game._id);
Expand Down Expand Up @@ -230,7 +236,7 @@ export class PlayerKillerService {
clonedPlayerToKill = getPlayerWithIdOrThrow(clonedPlayerToKill._id, clonedGame, cantFindPlayerException);
clonedGame = this.applyPlayerDeathOutcomes(clonedPlayerToKill, clonedGame, death);
clonedPlayerToKill = getPlayerWithIdOrThrow(clonedPlayerToKill._id, clonedGame, cantFindPlayerException);
return updatePlayerInGame(clonedPlayerToKill._id, { attributes: [] }, clonedGame);
return updatePlayerInGame(clonedPlayerToKill._id, this.removePlayerAttributesAfterDeath(clonedPlayerToKill), clonedGame);
}

private getPlayerToKillInGame(playerId: Types.ObjectId, game: Game): Player {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@ class PlayerAttribute {
@Type(() => PlayerAttributeActivation)
@Expose()
public activeAt?: PlayerAttributeActivation;

@ApiProperty(playerAttributeApiProperties.doesRemainAfterDeath)
@Expose()
public doesRemainAfterDeath?: boolean;
}

const PlayerAttributeSchema = SchemaFactory.createForClass(PlayerAttribute);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ function createFakeSheriffBySheriffPlayerAttribute(attribute: Partial<PlayerAttr
return createFakePlayerAttribute({
name: PLAYER_ATTRIBUTE_NAMES.SHERIFF,
source: PLAYER_ATTRIBUTE_NAMES.SHERIFF,
doesRemainAfterDeath: true,
...attribute,
}, override);
}
Expand All @@ -22,6 +23,7 @@ function createFakeSheriffByAllPlayerAttribute(attribute: Partial<PlayerAttribut
return createFakePlayerAttribute({
name: PLAYER_ATTRIBUTE_NAMES.SHERIFF,
source: PLAYER_GROUPS.ALL,
doesRemainAfterDeath: true,
...attribute,
}, override);
}
Expand Down Expand Up @@ -118,6 +120,7 @@ function createFakePowerlessByFoxPlayerAttribute(attribute: Partial<PlayerAttrib
return createFakePlayerAttribute({
name: PLAYER_ATTRIBUTE_NAMES.POWERLESS,
source: ROLE_NAMES.FOX,
doesRemainAfterDeath: true,
...attribute,
}, override);
}
Expand All @@ -126,6 +129,7 @@ function createFakePowerlessByAncientPlayerAttribute(attribute: Partial<PlayerAt
return createFakePlayerAttribute({
name: PLAYER_ATTRIBUTE_NAMES.POWERLESS,
source: ROLE_NAMES.ANCIENT,
doesRemainAfterDeath: true,
...attribute,
}, override);
}
Expand Down Expand Up @@ -191,6 +195,7 @@ function createFakePlayerAttribute(attribute: Partial<PlayerAttribute> = {}, ove
source: attribute.source ?? faker.helpers.arrayElement(gameSourceValues),
remainingPhases: attribute.remainingPhases ?? undefined,
activeAt: attribute.activeAt ?? undefined,
doesRemainAfterDeath: attribute.doesRemainAfterDeath ?? undefined,
...override,
}, plainToInstanceDefaultOptions);
}
Expand Down

0 comments on commit a5c8b6b

Please sign in to comment.