-
-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(thief): thief additional cards validation on game creation (#415)
- Loading branch information
1 parent
b6c4ca9
commit b4bf9de
Showing
89 changed files
with
68,237 additions
and
56,984 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
2 changes: 1 addition & 1 deletion
2
src/modules/game/constants/game-play/game-play-source.constant.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
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
37 changes: 37 additions & 0 deletions
37
...s/game/dto/base/decorators/additional-cards/additional-cards-for-thief-roles.decorator.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,37 @@ | ||
import type { ValidationOptions } from "class-validator"; | ||
import { registerDecorator } from "class-validator"; | ||
import { gameAdditionalCardsThiefRoleNames } from "../../../../constants/game-additional-card/game-additional-card.constant"; | ||
import type { CreateGameAdditionalCardDto } from "../../../create-game/create-game-additional-card/create-game-additional-card.dto"; | ||
|
||
function areAdditionalCardsForThiefRolesRespected(value: unknown): boolean { | ||
if (value === undefined) { | ||
return true; | ||
} | ||
const thiefAdditionalCards = value as CreateGameAdditionalCardDto[]; | ||
return thiefAdditionalCards.every(({ roleName }) => gameAdditionalCardsThiefRoleNames.includes(roleName)); | ||
} | ||
|
||
function getAdditionalCardsForThiefRolesDefaultMessage(): string { | ||
return `additionalCards.roleName must be one of the following values: ${gameAdditionalCardsThiefRoleNames.toString()}`; | ||
} | ||
|
||
function AdditionalCardsForThiefRoles(validationOptions?: ValidationOptions) { | ||
return (object: object, propertyName: string): void => { | ||
registerDecorator({ | ||
name: "AdditionalCardsForThiefRoles", | ||
target: object.constructor, | ||
propertyName, | ||
options: validationOptions, | ||
validator: { | ||
validate: areAdditionalCardsForThiefRolesRespected, | ||
defaultMessage: getAdditionalCardsForThiefRolesDefaultMessage, | ||
}, | ||
}); | ||
}; | ||
} | ||
|
||
export { | ||
AdditionalCardsForThiefRoles, | ||
getAdditionalCardsForThiefRolesDefaultMessage, | ||
areAdditionalCardsForThiefRolesRespected, | ||
}; |
39 changes: 39 additions & 0 deletions
39
...es/game/dto/base/decorators/additional-cards/additional-cards-for-thief-size.decorator.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,39 @@ | ||
import type { ValidationArguments, ValidationOptions } from "class-validator"; | ||
import { registerDecorator } from "class-validator"; | ||
import type { CreateGameDto } from "../../../create-game/create-game.dto"; | ||
|
||
function isAdditionalCardsForThiefSizeRespected(value: unknown, validationArguments: ValidationArguments): boolean { | ||
const { options } = validationArguments.object as CreateGameDto; | ||
if (value === undefined) { | ||
return true; | ||
} | ||
if (!Array.isArray(value)) { | ||
return false; | ||
} | ||
return options.roles.thief.additionalCardsCount === value.length; | ||
} | ||
|
||
function getAdditionalCardsForThiefSizeDefaultMessage(): string { | ||
return "additionalCards length must be equal to options.roles.thief.additionalCardsCount"; | ||
} | ||
|
||
function AdditionalCardsForThiefSize(validationOptions?: ValidationOptions) { | ||
return (object: object, propertyName: string): void => { | ||
registerDecorator({ | ||
name: "AdditionalCardsForThiefSize", | ||
target: object.constructor, | ||
propertyName, | ||
options: validationOptions, | ||
validator: { | ||
validate: isAdditionalCardsForThiefSizeRespected, | ||
defaultMessage: getAdditionalCardsForThiefSizeDefaultMessage, | ||
}, | ||
}); | ||
}; | ||
} | ||
|
||
export { | ||
AdditionalCardsForThiefSize, | ||
getAdditionalCardsForThiefSizeDefaultMessage, | ||
isAdditionalCardsForThiefSizeRespected, | ||
}; |
38 changes: 38 additions & 0 deletions
38
src/modules/game/dto/base/decorators/additional-cards/additional-cards-presence.decorator.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,38 @@ | ||
import type { ValidationArguments, ValidationOptions } from "class-validator"; | ||
import { registerDecorator } from "class-validator"; | ||
import { ROLE_NAMES } from "../../../../../role/enums/role.enum"; | ||
import type { CreateGameDto } from "../../../create-game/create-game.dto"; | ||
|
||
function isAdditionalCardsPresenceRespected(value: unknown, validationArguments: ValidationArguments): boolean { | ||
const { players } = validationArguments.object as Partial<CreateGameDto>; | ||
const doSomePlayersNeedAdditionalCards = players?.some(player => player.role.name === ROLE_NAMES.THIEF) === true; | ||
return doSomePlayersNeedAdditionalCards ? Array.isArray(value) : value === undefined; | ||
} | ||
|
||
function getAdditionalCardsPresenceDefaultMessage(validationArguments: ValidationArguments): string { | ||
if (!Array.isArray(validationArguments.value)) { | ||
return "additionalCards must be set if there is a player with role `thief`"; | ||
} | ||
return "additionalCards can't be set if there is no player with role `thief`"; | ||
} | ||
|
||
function AdditionalCardsPresence(validationOptions?: ValidationOptions) { | ||
return (object: object, propertyName: string): void => { | ||
registerDecorator({ | ||
name: "AdditionalCardsForThiefSize", | ||
target: object.constructor, | ||
propertyName, | ||
options: validationOptions, | ||
validator: { | ||
validate: isAdditionalCardsPresenceRespected, | ||
defaultMessage: getAdditionalCardsPresenceDefaultMessage, | ||
}, | ||
}); | ||
}; | ||
} | ||
|
||
export { | ||
isAdditionalCardsPresenceRespected, | ||
getAdditionalCardsPresenceDefaultMessage, | ||
AdditionalCardsPresence, | ||
}; |
46 changes: 46 additions & 0 deletions
46
...game/dto/base/decorators/additional-cards/additional-cards-roles-max-in-game.decorator.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,46 @@ | ||
import { registerDecorator } from "class-validator"; | ||
import type { ValidationArguments, ValidationOptions } from "class-validator"; | ||
import { roles } from "../../../../../role/constants/role.constant"; | ||
import type { CreateGameAdditionalCardDto } from "../../../create-game/create-game-additional-card/create-game-additional-card.dto"; | ||
import type { CreateGameDto } from "../../../create-game/create-game.dto"; | ||
|
||
function areAdditionalCardsRolesMaxInGameRespected(value: unknown, validationArguments: ValidationArguments): boolean { | ||
if (value === undefined) { | ||
return true; | ||
} | ||
const { players } = validationArguments.object as Partial<CreateGameDto>; | ||
if (players === undefined) { | ||
return false; | ||
} | ||
const additionalCards = value as CreateGameAdditionalCardDto[]; | ||
return roles.every(role => { | ||
const playersRoleCount = players.filter(player => player.role.name === role.name).length; | ||
const additionalCardsRoleCount = additionalCards.filter(additionalCard => additionalCard.roleName === role.name).length; | ||
return playersRoleCount + additionalCardsRoleCount <= role.maxInGame; | ||
}); | ||
} | ||
|
||
function getAdditionalCardsRolesMaxInGameDefaultMessage(): string { | ||
return "additionalCards.roleName can't exceed role maximum occurrences in game. Please check `maxInGame` property of roles"; | ||
} | ||
|
||
function AdditionalCardsRolesMaxInGame(validationOptions?: ValidationOptions) { | ||
return (object: object, propertyName: string): void => { | ||
registerDecorator({ | ||
name: "AdditionalCardsRolesMaxInGame", | ||
target: object.constructor, | ||
propertyName, | ||
options: validationOptions, | ||
validator: { | ||
validate: areAdditionalCardsRolesMaxInGameRespected, | ||
defaultMessage: getAdditionalCardsRolesMaxInGameDefaultMessage, | ||
}, | ||
}); | ||
}; | ||
} | ||
|
||
export { | ||
AdditionalCardsRolesMaxInGame, | ||
areAdditionalCardsRolesMaxInGameRespected, | ||
getAdditionalCardsRolesMaxInGameDefaultMessage, | ||
}; |
2 changes: 1 addition & 1 deletion
2
...ecorators/composition-bounds.decorator.ts → ...mposition/composition-bounds.decorator.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
Oops, something went wrong.