-
-
Notifications
You must be signed in to change notification settings - Fork 138
Feat/hand limit rework #1359
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Feat/hand limit rework #1359
Changes from all commits
f6cf4da
08ce877
6e0ed94
25d6d16
5d2e6fc
e08261a
fbf96e9
1efd565
1f34c9f
1e2a35c
08c33b7
0c6d5ed
d2cf03a
e253033
8386e72
b5432db
9acc559
35c4d28
6857521
ff4fd1c
f506796
4a4370b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| --- | ||
| paths: | ||
| - "src/translations/*.json" | ||
| --- | ||
|
|
||
| ## Translation File Parity | ||
|
|
||
| All JSON files in this directory must have identical keys in identical order at all times. | ||
|
|
||
| Before editing any translation file, read all other files in this directory to understand the current key set and order. | ||
|
|
||
| When adding a key: add it at the same position in every file, with the value translated into each file's language (e.g. English in en.json, French in fr.json). | ||
|
|
||
| When deleting a key: delete it from every file. | ||
|
|
||
| When changing a value: update the corresponding translation in every other file so the meaning stays in sync across languages. | ||
|
|
||
| This applies whether translation changes are the primary task or incidental to a larger change. |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -31,6 +31,9 @@ module.exports = { | |
| case GamePhase.RESOLVING_FOUR: | ||
| return exits.success((currentState.turn + 1) % 2); | ||
|
|
||
| case GamePhase.DISCARDING_TO_HAND_LIMIT: | ||
| return exits.success(currentState.p0.hand.length > 8 ? 0 : 1); | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Refactor to use turn instead |
||
|
|
||
| case GamePhase.CONSIDERING_STALEMATE: | ||
| return exits.success((currentState.playedBy + 1) % 2); | ||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -120,6 +120,11 @@ module.exports = { | |
| discardedCards.length > 1 ? `and the ${getFullCardName(discardedCards[1])}.` : '.' | ||
| }`; | ||
|
|
||
| case MoveType.DISCARD_TO_HAND_LIMIT: | ||
| return `${player} discarded the ${getFullCardName(discardedCards[0])} ${ | ||
| discardedCards.length > 1 ? `and the ${getFullCardName(discardedCards[1])} ` : '' | ||
| }to the hand limit.`; | ||
|
Comment on lines
+124
to
+126
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Generalize to n cards |
||
|
|
||
| case MoveType.RESOLVE_FIVE: | ||
| if (discardedCards.length) { | ||
| return `${player} discarded the ${getFullCardName( | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,65 @@ | ||
| const GamePhase = require('../../../../../utils/GamePhase.json'); | ||
|
|
||
| module.exports = { | ||
| friendlyName: 'Discard to hand limit', | ||
|
|
||
| description: 'Returns new GameState resulting from a player discarding excess cards to reach the hand limit', | ||
|
|
||
| inputs: { | ||
| currentState: { | ||
| type: 'ref', | ||
| description: 'The latest GameState before the requesting player discards excess cards', | ||
| required: true, | ||
| }, | ||
| /** | ||
| * @param { Object } requestedMove - Object describing the request to discard to hand limit | ||
| * @param { string[] } requestedMove.discardedCards - IDs of cards to discard (exactly hand.length - 8) | ||
| * @param { MoveType.DISCARD_TO_HAND_LIMIT } requestedMove.moveType | ||
| */ | ||
| requestedMove: { | ||
| type: 'ref', | ||
| description: 'The move being requested.', | ||
| }, | ||
| playedBy: { | ||
| type: 'number', | ||
| description: 'Player number of player requesting move.', | ||
| }, | ||
| priorStates: { | ||
| type: 'ref', | ||
| description: 'List of packed gameStateRows for this game\'s prior states', | ||
| required: true, | ||
| } | ||
| }, | ||
| sync: true, | ||
| fn: ({ currentState, requestedMove, playedBy }, exits) => { | ||
| const { discardedCards: discardIds } = requestedMove; | ||
| let result = _.cloneDeep(currentState); | ||
|
|
||
| const player = playedBy ? result.p1 : result.p0; | ||
| const discardedCards = []; | ||
|
|
||
| for (const cardId of discardIds) { | ||
| const cardIndex = player.hand.findIndex(({ id }) => id === cardId); | ||
| if (cardIndex !== -1) { | ||
| discardedCards.push(player.hand[cardIndex]); | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should we be this defensive. Given that validate runs first we could take discarded cards as-is. But this further ensures we sanity check which isn't crazy. Syntactically if we're constructing discarded cards from the ones we find, I'd prefer this in three lines, splice then two pushes |
||
| result.scrap.push(...player.hand.splice(cardIndex, 1)); | ||
| } | ||
| } | ||
|
|
||
| result.turn++; | ||
|
|
||
| result = { | ||
| ...result, | ||
| ...requestedMove, | ||
| phase: GamePhase.MAIN, | ||
| playedBy, | ||
| playedCard: null, | ||
| targetCard: null, | ||
| discardedCards, | ||
| resolved: null, | ||
| oneOff: null, | ||
| }; | ||
|
|
||
| return exits.success(result); | ||
| }, | ||
| }; | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,72 @@ | ||
| const GamePhase = require('../../../../../utils/GamePhase.json'); | ||
| const BadRequestError = require('../../../../errors/badRequestError'); | ||
|
|
||
| module.exports = { | ||
| friendlyName: 'Validate request to discard to hand limit', | ||
|
|
||
| description: 'Verifies whether a request to discard excess cards is legal, throwing explanatory error if not.', | ||
|
|
||
| inputs: { | ||
| currentState: { | ||
| type: 'ref', | ||
| descriptions: 'Object containing the current game state', | ||
| required: true, | ||
| }, | ||
| /** | ||
| * @param { Object } requestedMove - Object describing the request to discard to hand limit | ||
| * @param { string[] } requestedMove.discardedCards - IDs of cards to discard (must equal hand.length - 8) | ||
| * @param { MoveType.DISCARD_TO_HAND_LIMIT } requestedMove.moveType | ||
| */ | ||
| requestedMove: { | ||
| type: 'ref', | ||
| description: 'Object containing data needed for current move', | ||
| required: true, | ||
| }, | ||
| playedBy: { | ||
| type: 'number', | ||
| description: 'Player number of player requesting move', | ||
| required: true, | ||
| }, | ||
| priorStates: { | ||
| type: 'ref', | ||
| description: 'List of packed gameStateRows for this game\'s prior states', | ||
| required: true, | ||
| } | ||
| }, | ||
| sync: true, | ||
| fn: ({ requestedMove, currentState, playedBy }, exits) => { | ||
| try { | ||
| const activePlayerPNum = currentState.p0.hand.length > 8 ? 0 : 1; | ||
| if (playedBy !== activePlayerPNum) { | ||
| throw new BadRequestError('game.snackbar.global.notYourTurn'); | ||
| } | ||
|
|
||
| if (currentState.phase !== GamePhase.DISCARDING_TO_HAND_LIMIT) { | ||
|
itsalaidbacklife marked this conversation as resolved.
|
||
| throw new BadRequestError('game.snackbar.oneOffs.discardToHandLimit.notDiscardingPhase'); | ||
| } | ||
|
|
||
|
|
||
| const player = playedBy ? currentState.p1 : currentState.p0; | ||
| const overflowCount = player.hand.length - 8; | ||
|
|
||
| if (overflowCount <= 0) { | ||
| throw new BadRequestError('game.snackbar.oneOffs.discardToHandLimit.handNotOverLimit'); | ||
| } | ||
|
|
||
| const { discardedCards } = requestedMove; | ||
|
|
||
| if (!Array.isArray(discardedCards) || discardedCards.length !== overflowCount) { | ||
| throw new BadRequestError('game.snackbar.oneOffs.discardToHandLimit.mustSelectCards'); | ||
| } | ||
|
|
||
| const allInHand = discardedCards.every(id => player.hand.some(card => card.id === id)); | ||
| if (!allInHand) { | ||
| throw new BadRequestError('game.snackbar.oneOffs.discardToHandLimit.mustSelectCards'); | ||
| } | ||
|
|
||
| return exits.success(); | ||
| } catch (err) { | ||
| return exits.error(err); | ||
| } | ||
| }, | ||
| }; | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -47,12 +47,6 @@ module.exports = { | |
| throw new BadRequestError('game.snackbar.global.notYourTurn'); | ||
| } | ||
|
|
||
| // Must be under hand limit of 8 | ||
| const player = playedBy ? currentState.p1 : currentState.p0; | ||
| if (player.hand.length >= 8) { | ||
| throw new BadRequestError('game.snackbar.draw.handLimit'); | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ensure this is removed from translation keys |
||
| } | ||
|
|
||
| // Deck must have cards | ||
| if (!currentState.deck.length) { | ||
| throw new BadRequestError('game.snackbar.draw.deckIsEmpty'); | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should investigate this from a performance perspective. Recursion smells and I could see this blowing up