Skip to content

Commit

Permalink
feat(additional-cards): remove additional cards count (#1272)
Browse files Browse the repository at this point in the history
  • Loading branch information
antoinezanardi authored Sep 5, 2024
1 parent b85bbb4 commit fc9be84
Show file tree
Hide file tree
Showing 21 changed files with 20,345 additions and 20,412 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
import type { RoleName } from "@/modules/role/types/role.types";

const MAX_ADDITIONAL_CARDS_COUNT_FOR_RECIPIENT = 5;

const GAME_ADDITIONAL_CARDS_RECIPIENTS = ["thief", "actor"] as const satisfies readonly RoleName[];

export { GAME_ADDITIONAL_CARDS_RECIPIENTS };
export {
MAX_ADDITIONAL_CARDS_COUNT_FOR_RECIPIENT,
GAME_ADDITIONAL_CARDS_RECIPIENTS,
};
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@ const DEFAULT_GAME_OPTIONS: ReadonlyDeep<GameOptions> = {
thief: {
mustChooseBetweenWerewolves: true,
isChosenCardRevealed: false,
additionalCardsCount: 2,
},
piedPiper: {
charmedPeopleCountPerNight: 2,
Expand All @@ -63,7 +62,6 @@ const DEFAULT_GAME_OPTIONS: ReadonlyDeep<GameOptions> = {
prejudicedManipulator: { isPowerlessOnWerewolvesSide: true },
actor: {
isPowerlessOnWerewolvesSide: true,
additionalCardsCount: 3,
},
},
};
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { MAX_ADDITIONAL_CARDS_COUNT_FOR_RECIPIENT } from "@/modules/game/constants/game-additional-card/game-additional-card.constants";
import type { ValidationArguments, ValidationOptions } from "class-validator";
import { registerDecorator } from "class-validator";
import { has } from "lodash";

import type { CreateGameDto } from "@/modules/game/dto/create-game/create-game.dto";

function isAdditionalCardsForActorSizeRespected(value: unknown, validationArguments: ValidationArguments): boolean {
const { players, options } = validationArguments.object as CreateGameDto;
const { additionalCardsCount } = options.roles.actor;
const { players } = validationArguments.object as CreateGameDto;
if (value === undefined || !players.some(player => player.role.name === "actor")) {
return true;
}
Expand All @@ -16,11 +16,11 @@ function isAdditionalCardsForActorSizeRespected(value: unknown, validationArgume
const cards = value as { recipient: string }[];
const actorAdditionalCards = cards.filter(card => card.recipient === "actor");

return actorAdditionalCards.length === additionalCardsCount;
return actorAdditionalCards.length !== 0 && actorAdditionalCards.length <= MAX_ADDITIONAL_CARDS_COUNT_FOR_RECIPIENT;
}

function getAdditionalCardsForActorSizeDefaultMessage(): string {
return "additionalCards length for actor must be equal to options.roles.actor.additionalCardsCount";
return `additionalCards length for actor must be between 1 and ${MAX_ADDITIONAL_CARDS_COUNT_FOR_RECIPIENT}`;
}

function AdditionalCardsForActorSize(validationOptions?: ValidationOptions) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import { MAX_ADDITIONAL_CARDS_COUNT_FOR_RECIPIENT } from "@/modules/game/constants/game-additional-card/game-additional-card.constants";
import type { ValidationArguments, ValidationOptions } from "class-validator";
import { registerDecorator } from "class-validator";
import { has } from "lodash";

import type { CreateGameDto } from "@/modules/game/dto/create-game/create-game.dto";

function isAdditionalCardsForThiefSizeRespected(value: unknown, validationArguments: ValidationArguments): boolean {
const { players, options } = validationArguments.object as CreateGameDto;
const { players } = validationArguments.object as CreateGameDto;
if (value === undefined || !players.some(player => player.role.name === "thief")) {
return true;
}
Expand All @@ -15,11 +16,11 @@ function isAdditionalCardsForThiefSizeRespected(value: unknown, validationArgume
const cards = value as { recipient: string }[];
const thiefAdditionalCards = cards.filter(card => card.recipient === "thief");

return thiefAdditionalCards.length === options.roles.thief.additionalCardsCount;
return thiefAdditionalCards.length !== 0 && thiefAdditionalCards.length <= MAX_ADDITIONAL_CARDS_COUNT_FOR_RECIPIENT;
}

function getAdditionalCardsForThiefSizeDefaultMessage(): string {
return "additionalCards length for thief must be equal to options.roles.thief.additionalCardsCount";
return `additionalCards length for thief must be between 1 and ${MAX_ADDITIONAL_CARDS_COUNT_FOR_RECIPIENT}`;
}

function AdditionalCardsForThiefSize(validationOptions?: ValidationOptions) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import { ACTOR_GAME_OPTIONS_API_PROPERTIES, ACTOR_GAME_OPTIONS_FIELDS_SPECS } from "@/modules/game/schemas/game-options/roles-game-options/actor-game-options/actor-game-options.schema.constants";
import type { ApiPropertyOptions } from "@nestjs/swagger";
import { ApiProperty } from "@nestjs/swagger";
import { IsBoolean, IsInt, IsOptional, Max, Min } from "class-validator";

import { ACTOR_GAME_OPTIONS_API_PROPERTIES, ACTOR_GAME_OPTIONS_FIELDS_SPECS } from "@/modules/game/schemas/game-options/roles-game-options/actor-game-options/actor-game-options.schema.constants";
import { IsBoolean, IsOptional } from "class-validator";

class CreateActorGameOptionsDto {
@ApiProperty({
Expand All @@ -12,16 +11,6 @@ class CreateActorGameOptionsDto {
@IsOptional()
@IsBoolean()
public isPowerlessOnWerewolvesSide: boolean = ACTOR_GAME_OPTIONS_FIELDS_SPECS.isPowerlessOnWerewolvesSide.default;

@ApiProperty({
...ACTOR_GAME_OPTIONS_API_PROPERTIES.additionalCardsCount,
required: false,
} as ApiPropertyOptions)
@IsOptional()
@IsInt()
@Min(ACTOR_GAME_OPTIONS_FIELDS_SPECS.additionalCardsCount.min)
@Max(ACTOR_GAME_OPTIONS_FIELDS_SPECS.additionalCardsCount.max)
public additionalCardsCount: number = ACTOR_GAME_OPTIONS_FIELDS_SPECS.additionalCardsCount.default;
}

export { CreateActorGameOptionsDto };
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import { THIEF_GAME_OPTIONS_API_PROPERTIES, THIEF_GAME_OPTIONS_FIELDS_SPECS } from "@/modules/game/schemas/game-options/roles-game-options/thief-game-options/thief-game-options.schema.constants";
import type { ApiPropertyOptions } from "@nestjs/swagger";
import { ApiProperty } from "@nestjs/swagger";
import { IsBoolean, IsInt, IsOptional, Max, Min } from "class-validator";

import { THIEF_GAME_OPTIONS_API_PROPERTIES, THIEF_GAME_OPTIONS_FIELDS_SPECS } from "@/modules/game/schemas/game-options/roles-game-options/thief-game-options/thief-game-options.schema.constants";
import { IsBoolean, IsOptional } from "class-validator";

class CreateThiefGameOptionsDto {
@ApiProperty({
Expand All @@ -20,16 +19,6 @@ class CreateThiefGameOptionsDto {
@IsOptional()
@IsBoolean()
public isChosenCardRevealed: boolean = THIEF_GAME_OPTIONS_FIELDS_SPECS.isChosenCardRevealed.default;

@ApiProperty({
...THIEF_GAME_OPTIONS_API_PROPERTIES.additionalCardsCount,
required: false,
} as ApiPropertyOptions)
@IsOptional()
@IsInt()
@Min(THIEF_GAME_OPTIONS_FIELDS_SPECS.additionalCardsCount.min)
@Max(THIEF_GAME_OPTIONS_FIELDS_SPECS.additionalCardsCount.max)
public additionalCardsCount: number = THIEF_GAME_OPTIONS_FIELDS_SPECS.additionalCardsCount.default;
}

export { CreateThiefGameOptionsDto };
Original file line number Diff line number Diff line change
Expand Up @@ -12,23 +12,13 @@ const ACTOR_GAME_OPTIONS_FIELDS_SPECS = {
required: true,
default: DEFAULT_GAME_OPTIONS.roles.actor.isPowerlessOnWerewolvesSide,
},
additionalCardsCount: {
required: true,
default: DEFAULT_GAME_OPTIONS.roles.actor.additionalCardsCount,
min: 1,
max: 5,
},
} as const satisfies Record<keyof ActorGameOptions, MongoosePropOptions>;

const ACTOR_GAME_OPTIONS_API_PROPERTIES: ReadonlyDeep<Record<keyof ActorGameOptions, ApiPropertyOptions>> = {
isPowerlessOnWerewolvesSide: {
description: "If set to `true`, the actor becomes powerless if he joins the werewolves side.",
...convertMongoosePropOptionsToApiPropertyOptions(ACTOR_GAME_OPTIONS_FIELDS_SPECS.isPowerlessOnWerewolvesSide),
},
additionalCardsCount: {
description: "Number of additional cards for the `actor` at the beginning of the game.",
...convertMongoosePropOptionsToApiPropertyOptions(ACTOR_GAME_OPTIONS_FIELDS_SPECS.additionalCardsCount),
},
};

export {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,6 @@ class ActorGameOptions {
@Prop(ACTOR_GAME_OPTIONS_FIELDS_SPECS.isPowerlessOnWerewolvesSide)
@Expose()
public isPowerlessOnWerewolvesSide: boolean;

@ApiProperty(ACTOR_GAME_OPTIONS_API_PROPERTIES.additionalCardsCount as ApiPropertyOptions)
@Prop(ACTOR_GAME_OPTIONS_FIELDS_SPECS.additionalCardsCount)
@Expose()
public additionalCardsCount: number;
}

const ACTOR_GAME_OPTIONS_SCHEMA = SchemaFactory.createForClass(ActorGameOptions);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,6 @@ const THIEF_GAME_OPTIONS_FIELDS_SPECS = {
required: true,
default: DEFAULT_GAME_OPTIONS.roles.thief.isChosenCardRevealed,
},
additionalCardsCount: {
required: true,
default: DEFAULT_GAME_OPTIONS.roles.thief.additionalCardsCount,
min: 1,
max: 5,
},
} as const satisfies Record<keyof ThiefGameOptions, MongoosePropOptions>;

const THIEF_GAME_OPTIONS_API_PROPERTIES: ReadonlyDeep<Record<keyof ThiefGameOptions, ApiPropertyOptions>> = {
Expand All @@ -33,10 +27,6 @@ const THIEF_GAME_OPTIONS_API_PROPERTIES: ReadonlyDeep<Record<keyof ThiefGameOpti
description: "If set to `true`, the `thief` chosen card is revealed to every other players",
...convertMongoosePropOptionsToApiPropertyOptions(THIEF_GAME_OPTIONS_FIELDS_SPECS.isChosenCardRevealed),
},
additionalCardsCount: {
description: "Number of additional cards for the `thief` at the beginning of the game",
...convertMongoosePropOptionsToApiPropertyOptions(THIEF_GAME_OPTIONS_FIELDS_SPECS.additionalCardsCount),
},
};

export {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,6 @@ class ThiefGameOptions {
@Prop(THIEF_GAME_OPTIONS_FIELDS_SPECS.isChosenCardRevealed)
@Expose()
public isChosenCardRevealed: boolean;

@ApiProperty(THIEF_GAME_OPTIONS_API_PROPERTIES.additionalCardsCount as ApiPropertyOptions)
@Prop(THIEF_GAME_OPTIONS_FIELDS_SPECS.additionalCardsCount)
@Expose()
public additionalCardsCount: number;
}

const THIEF_GAME_OPTIONS_SCHEMA = SchemaFactory.createForClass(ThiefGameOptions);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
[
{
"roleName": "seer",
"recipient": "actor"
},
{
"roleName": "witch",
"recipient": "actor"
},
{
"roleName": "hunter",
"recipient": "actor"
},
{
"roleName": "elder",
"recipient": "actor"
},
{
"roleName": "wild-child",
"recipient": "actor"
},
{
"roleName": "little-girl",
"recipient": "actor"
}
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
[
{
"roleName": "white-werewolf",
"recipient": "thief"
},
{
"roleName": "werewolf",
"recipient": "thief"
},
{
"roleName": "villager-villager",
"recipient": "thief"
},
{
"roleName": "idiot",
"recipient": "thief"
},
{
"roleName": "wolf-hound",
"recipient": "thief"
},
{
"roleName": "big-bad-wolf",
"recipient": "thief"
}
]

This file was deleted.

40 changes: 6 additions & 34 deletions tests/acceptance/features/game/features/crud/game-creation.feature
Original file line number Diff line number Diff line change
Expand Up @@ -407,22 +407,8 @@ Feature: 🎲 Game Creation
| message |
| additionalCards must be set if there is a player with one of the following roles : thief,actor |

Scenario: 🎲 Game can't be created if there are too less additional cards for thief
Given a created game with additional cards described in file one-additional-card-for-thief.json and with the following players
| name | role |
| Antoine | villager |
| Olivia | thief |
| JB | idiot |
| Thomas | werewolf |
Then the request should have failed with status code 400
And the request exception status code should be 400
And the request exception error should be "Bad Request"
And the request exception messages should be
| message |
| additionalCards length for thief must be equal to options.roles.thief.additionalCardsCount |

Scenario: 🎲 Game can't be created if there are too much additional cards for thief
Given a created game with additional cards described in file five-additional-cards-for-thief.json and with the following players
Given a created game with additional cards described in file six-additional-cards-for-thief.json and with the following players
| name | role |
| Antoine | villager |
| Olivia | thief |
Expand All @@ -432,8 +418,8 @@ Feature: 🎲 Game Creation
And the request exception status code should be 400
And the request exception error should be "Bad Request"
And the request exception messages should be
| message |
| additionalCards length for thief must be equal to options.roles.thief.additionalCardsCount |
| message |
| additionalCards length for thief must be between 1 and 5 |

Scenario: 🎲 Game can't be created if one additional card can't be given to thief
Given a created game with additional cards described in file invalid-additional-cards-for-thief.json and with the following players
Expand Down Expand Up @@ -692,22 +678,8 @@ Feature: 🎲 Game Creation
| message |
| additionalCards must be set if there is a player with one of the following roles : thief,actor |

Scenario: 🎲 Game can't be created if there are too less additional cards for actor
Given a created game with additional cards described in file one-additional-card-for-actor.json and with the following players
| name | role |
| Antoine | villager |
| Olivia | actor |
| JB | idiot |
| Thomas | werewolf |
Then the request should have failed with status code 400
And the request exception status code should be 400
And the request exception error should be "Bad Request"
And the request exception messages should be
| message |
| additionalCards length for actor must be equal to options.roles.actor.additionalCardsCount |

Scenario: 🎲 Game can't be created if there are too much additional cards for actor
Given a created game with additional cards described in file five-additional-cards-for-actor.json and with the following players
Given a created game with additional cards described in file six-additional-cards-for-actor.json and with the following players
| name | role |
| Antoine | villager |
| Olivia | actor |
Expand All @@ -717,8 +689,8 @@ Feature: 🎲 Game Creation
And the request exception status code should be 400
And the request exception error should be "Bad Request"
And the request exception messages should be
| message |
| additionalCards length for actor must be equal to options.roles.actor.additionalCardsCount |
| message |
| additionalCards length for actor must be between 1 and 5 |

Scenario: 🎲 Game can't be created if one additional card can't be given to actor
Given a created game with additional cards described in file invalid-additional-cards-for-actor.json and with the following players
Expand Down
4 changes: 2 additions & 2 deletions tests/acceptance/features/game/features/role/thief.feature
Original file line number Diff line number Diff line change
Expand Up @@ -131,8 +131,8 @@ Feature: 👺 Thief role
When the player or group skips his turn
Then the game's current play should be werewolves to eat

Scenario: 👺 Thief can choose between more than two cards with good options
Given a created game with additional cards described in file five-additional-cards-for-thief.json and with options described in file no-sheriff-option.json, thief-has-five-additional-cards-option.json and with the following players
Scenario: 👺 Thief can choose between more than two cards
Given a created game with additional cards described in file five-additional-cards-for-thief.json and with options described in file no-sheriff-option.json and with the following players
| name | role |
| Antoine | thief |
| Olivia | villager |
Expand Down
Loading

0 comments on commit fc9be84

Please sign in to comment.