Skip to content

Commit

Permalink
test(acceptance): scapegoat acceptance tests (#411)
Browse files Browse the repository at this point in the history
  • Loading branch information
antoinezanardi committed Aug 19, 2023
1 parent 8163c39 commit 864e4e6
Show file tree
Hide file tree
Showing 36 changed files with 14,846 additions and 13,296 deletions.
6 changes: 3 additions & 3 deletions src/modules/game/helpers/game-play/game-play.helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ function getVotesWithRelationsFromMakeGamePlayDto(makeGamePlayDto: MakeGamePlayD
return;
}
return makeGamePlayDto.votes.reduce<MakeGamePlayVoteWithRelationsDto[]>((acc, vote) => {
const source = getPlayerWithId(game.players, vote.sourceId);
const target = getPlayerWithId(game.players, vote.targetId);
const source = getPlayerWithId(game, vote.sourceId);
const target = getPlayerWithId(game, vote.targetId);
if (source === undefined) {
throw new ResourceNotFoundException(API_RESOURCES.PLAYERS, vote.sourceId.toString(), RESOURCE_NOT_FOUND_REASONS.UNMATCHED_GAME_PLAY_PLAYER_VOTE_SOURCE);
}
Expand All @@ -37,7 +37,7 @@ function getTargetsWithRelationsFromMakeGamePlayDto(makeGamePlayDto: MakeGamePla
return;
}
return makeGamePlayDto.targets.reduce<MakeGamePlayTargetWithRelationsDto[]>((acc, target) => {
const player = getPlayerWithId(game.players, target.playerId);
const player = getPlayerWithId(game, target.playerId);
if (player === undefined) {
throw new ResourceNotFoundException(API_RESOURCES.PLAYERS, target.playerId.toString(), RESOURCE_NOT_FOUND_REASONS.UNMATCHED_GAME_PLAY_PLAYER_TARGET);
}
Expand Down
72 changes: 35 additions & 37 deletions src/modules/game/helpers/game-victory/game-victory.helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,88 +7,86 @@ import { GAME_VICTORY_TYPES } from "../../enums/game-victory.enum";
import { PLAYER_ATTRIBUTE_NAMES, PLAYER_DEATH_CAUSES } from "../../enums/player.enum";
import { GameVictory } from "../../schemas/game-victory/game-victory.schema";
import type { Game } from "../../schemas/game.schema";
import type { Player } from "../../schemas/player/player.schema";
import { areAllPlayersDead, getLeftToCharmByPiedPiperPlayers, getPlayersWithAttribute, getPlayersWithCurrentSide, getPlayerWithCurrentRole } from "../game.helper";
import { doesPlayerHaveAttributeWithName } from "../player/player-attribute/player-attribute.helper";
import { isPlayerAliveAndPowerful } from "../player/player.helper";
import { areAllPlayersDead, getLeftToCharmByPiedPiperPlayers, getPlayersWithActiveAttributeName, getPlayersWithCurrentSide, getPlayerWithCurrentRole } from "../game.helper";
import { doesPlayerHaveActiveAttributeWithName } from "../player/player-attribute/player-attribute.helper";
import { isPlayerAliveAndPowerful, isPlayerPowerful } from "../player/player.helper";

function doWerewolvesWin(players: Player[]): boolean {
const werewolvesSidedPlayers = getPlayersWithCurrentSide(players, ROLE_SIDES.WEREWOLVES);
return werewolvesSidedPlayers.length > 0 && !players.some(({ side, isAlive }) => side.current === ROLE_SIDES.VILLAGERS && isAlive);
function doWerewolvesWin(game: Game): boolean {
const werewolvesSidedPlayers = getPlayersWithCurrentSide(game, ROLE_SIDES.WEREWOLVES);
return werewolvesSidedPlayers.length > 0 && !game.players.some(({ side, isAlive }) => side.current === ROLE_SIDES.VILLAGERS && isAlive);
}

function doVillagersWin(players: Player[]): boolean {
const villagersSidedPlayers = getPlayersWithCurrentSide(players, ROLE_SIDES.VILLAGERS);
return villagersSidedPlayers.length > 0 && !players.some(({ side, isAlive }) => side.current === ROLE_SIDES.WEREWOLVES && isAlive);
function doVillagersWin(game: Game): boolean {
const villagersSidedPlayers = getPlayersWithCurrentSide(game, ROLE_SIDES.VILLAGERS);
return villagersSidedPlayers.length > 0 && !game.players.some(({ side, isAlive }) => side.current === ROLE_SIDES.WEREWOLVES && isAlive);
}

function doLoversWin(players: Player[]): boolean {
const lovers = getPlayersWithAttribute(players, PLAYER_ATTRIBUTE_NAMES.IN_LOVE);
return lovers.length > 0 && players.every(player => {
const isPlayerInLove = doesPlayerHaveAttributeWithName(player, PLAYER_ATTRIBUTE_NAMES.IN_LOVE);
function doLoversWin(game: Game): boolean {
const lovers = getPlayersWithActiveAttributeName(game, PLAYER_ATTRIBUTE_NAMES.IN_LOVE);
return lovers.length > 0 && game.players.every(player => {
const isPlayerInLove = doesPlayerHaveActiveAttributeWithName(player, PLAYER_ATTRIBUTE_NAMES.IN_LOVE, game);
return isPlayerInLove && player.isAlive || !isPlayerInLove && !player.isAlive;
});
}

function doesWhiteWerewolfWin(players: Player[]): boolean {
const whiteWerewolfPlayer = getPlayerWithCurrentRole(players, ROLE_NAMES.WHITE_WEREWOLF);
return !!whiteWerewolfPlayer && players.every(({ role, isAlive }) =>
function doesWhiteWerewolfWin(game: Game): boolean {
const whiteWerewolfPlayer = getPlayerWithCurrentRole(game, ROLE_NAMES.WHITE_WEREWOLF);
return !!whiteWerewolfPlayer && game.players.every(({ role, isAlive }) =>
role.current === ROLE_NAMES.WHITE_WEREWOLF && isAlive || role.current !== ROLE_NAMES.WHITE_WEREWOLF && !isAlive);
}

function doesPiedPiperWin(game: Game): boolean {
const { isPowerlessIfInfected } = game.options.roles.piedPiper;
const piedPiperPlayer = getPlayerWithCurrentRole(game.players, ROLE_NAMES.PIED_PIPER);
const leftToCharmPlayers = getLeftToCharmByPiedPiperPlayers(game.players);
return !!piedPiperPlayer && isPlayerAliveAndPowerful(piedPiperPlayer) && !leftToCharmPlayers.length &&
const piedPiperPlayer = getPlayerWithCurrentRole(game, ROLE_NAMES.PIED_PIPER);
const leftToCharmPlayers = getLeftToCharmByPiedPiperPlayers(game);
return !!piedPiperPlayer && isPlayerAliveAndPowerful(piedPiperPlayer, game) && !leftToCharmPlayers.length &&
(!isPowerlessIfInfected || piedPiperPlayer.side.current === ROLE_SIDES.VILLAGERS);
}

function doesAngelWin(game: Game): boolean {
const angelPlayer = getPlayerWithCurrentRole(game.players, ROLE_NAMES.ANGEL);
if (!angelPlayer?.death || angelPlayer.isAlive || doesPlayerHaveAttributeWithName(angelPlayer, PLAYER_ATTRIBUTE_NAMES.POWERLESS) || game.turn > 1) {
const angelPlayer = getPlayerWithCurrentRole(game, ROLE_NAMES.ANGEL);
if (!angelPlayer?.death || angelPlayer.isAlive || !isPlayerPowerful(angelPlayer, game) || game.turn > 1) {
return false;
}
return [PLAYER_DEATH_CAUSES.VOTE, PLAYER_DEATH_CAUSES.EATEN].includes(angelPlayer.death.cause);
}

function isGameOver(game: Game): boolean {
const { players, upcomingPlays, currentPlay } = game;
const { upcomingPlays, currentPlay } = game;
if (!currentPlay) {
throw createNoCurrentGamePlayUnexpectedException("isGameOver", { gameId: game._id });
}
const isShootPlayIncoming = !!upcomingPlays.find(({ action, source }) => action === GAME_PLAY_ACTIONS.SHOOT && source.name === ROLE_NAMES.HUNTER);
return areAllPlayersDead(players) || currentPlay.action !== GAME_PLAY_ACTIONS.SHOOT && !isShootPlayIncoming &&
(doWerewolvesWin(players) || doVillagersWin(players) ||
doLoversWin(players) || doesWhiteWerewolfWin(players) || doesPiedPiperWin(game) || doesAngelWin(game));
return areAllPlayersDead(game) || currentPlay.action !== GAME_PLAY_ACTIONS.SHOOT && !isShootPlayIncoming &&
(doWerewolvesWin(game) || doVillagersWin(game) || doLoversWin(game) || doesWhiteWerewolfWin(game) || doesPiedPiperWin(game) || doesAngelWin(game));
}

function generateGameVictoryData(game: Game): GameVictory | undefined {
if (areAllPlayersDead(game.players)) {
if (areAllPlayersDead(game)) {
return plainToInstance(GameVictory, { type: GAME_VICTORY_TYPES.NONE }, plainToInstanceDefaultOptions);
}
if (doesAngelWin(game)) {
const angelPlayer = getPlayerWithCurrentRole(game.players, ROLE_NAMES.ANGEL);
const angelPlayer = getPlayerWithCurrentRole(game, ROLE_NAMES.ANGEL);
return plainToInstance(GameVictory, { type: GAME_VICTORY_TYPES.ANGEL, winners: [angelPlayer] }, plainToInstanceDefaultOptions);
}
if (doLoversWin(game.players)) {
const inLovePlayers = getPlayersWithAttribute(game.players, PLAYER_ATTRIBUTE_NAMES.IN_LOVE);
if (doLoversWin(game)) {
const inLovePlayers = getPlayersWithActiveAttributeName(game, PLAYER_ATTRIBUTE_NAMES.IN_LOVE);
return plainToInstance(GameVictory, { type: GAME_VICTORY_TYPES.LOVERS, winners: inLovePlayers }, plainToInstanceDefaultOptions);
}
if (doesPiedPiperWin(game)) {
const piedPiperPlayer = getPlayerWithCurrentRole(game.players, ROLE_NAMES.PIED_PIPER);
const piedPiperPlayer = getPlayerWithCurrentRole(game, ROLE_NAMES.PIED_PIPER);
return plainToInstance(GameVictory, { type: GAME_VICTORY_TYPES.PIED_PIPER, winners: [piedPiperPlayer] }, plainToInstanceDefaultOptions);
}
if (doesWhiteWerewolfWin(game.players)) {
const whiteWerewolfPlayer = getPlayerWithCurrentRole(game.players, ROLE_NAMES.WHITE_WEREWOLF);
if (doesWhiteWerewolfWin(game)) {
const whiteWerewolfPlayer = getPlayerWithCurrentRole(game, ROLE_NAMES.WHITE_WEREWOLF);
return plainToInstance(GameVictory, { type: GAME_VICTORY_TYPES.WHITE_WEREWOLF, winners: [whiteWerewolfPlayer] }, plainToInstanceDefaultOptions);
}
if (doWerewolvesWin(game.players)) {
const werewolvesSidePlayers = getPlayersWithCurrentSide(game.players, ROLE_SIDES.WEREWOLVES);
if (doWerewolvesWin(game)) {
const werewolvesSidePlayers = getPlayersWithCurrentSide(game, ROLE_SIDES.WEREWOLVES);
return plainToInstance(GameVictory, { type: GAME_VICTORY_TYPES.WEREWOLVES, winners: werewolvesSidePlayers }, plainToInstanceDefaultOptions);
}
if (doVillagersWin(game.players)) {
const villagersSidePlayers = getPlayersWithCurrentSide(game.players, ROLE_SIDES.VILLAGERS);
if (doVillagersWin(game)) {
const villagersSidePlayers = getPlayersWithCurrentSide(game, ROLE_SIDES.VILLAGERS);
return plainToInstance(GameVictory, { type: GAME_VICTORY_TYPES.VILLAGERS, winners: villagersSidePlayers }, plainToInstanceDefaultOptions);
}
}
Expand Down

0 comments on commit 864e4e6

Please sign in to comment.