Skip to content

refactor(game-states): make checkGameStateForWin side-effect free (#1325) - #1491

Open
pburatini wants to merge 1 commit into
cuttle-cards:mainfrom
pburatini:feature/1325-consolidate-win-check
Open

pburatini wants to merge 1 commit into
cuttle-cards:mainfrom
pburatini:feature/1325-consolidate-win-check

Conversation

@pburatini

@pburatini pburatini commented Nov 25, 2026 •

Copy link
Copy Markdown

Issue number

Relevant issue number

Please check the following

  • Do the tests still pass? (see Run the Tests)
  • Is the code formatted properly? (see Linting (Formatting))
  • For New Features:
    • Have tests been added to cover any new features or fixes?
    • Has the documentation been updated accordingly? (no docs reference these helpers)

Please describe additional details for testing this change

What changed

  • check-game-state-for-win-sync.js is merged into check-game-state-for-win.js, which is now the single synchronous checkGameStateForWin() with no side effects. The issue calls these checkWinGameState() / checkWinGameStateSync(); I kept the existing shorter helper name.
  • The cleanup moved to the move and ai-move endpoints. After saving the new game state, they check for a win and, only if the game ended, call a new finishGame helper (marks the game finished, sets the winner, adds it to its match). It's a helper so both endpoints share it.
  • createSocketEvents no longer writes anything. The socket event still needs the match for the game-over dialog, so:
    • the endpoints pass the match returned by finishGame through publishGameState (new optional currentMatch input);
    • other callers (get-game, spectate) get it from a new read-only getGameMatch helper. addGameToMatch now reuses it instead of duplicating that lookup.
  • The pass counting that lived inside createSocketEvents is now a countPasses helper, since the endpoints need it too.
  • tests/unit/fixtures/gameStates/points.js: the fixture game was FINISHED with no winner and relied on createSocketEvents to fill it in; it now has its winner recorded.

Testing

  • npm run lint, client unit tests and sails unit tests pass.
  • New tests/unit/specs/sails/game-over.spec.js covers: winning moves record the winner (casual) and add the game to its match (ranked), a non-winning move leaves the game in progress, and createSocketEvents on a winning state no longer touches the database (this last test fails on main).
  • I compared old and new code for a winning move (casual and ranked), an AI winning move, and re-opening a finished game: the socket payloads and the resulting Game/Match records are identical.
  • I haven't run the Cypress suite locally; relying on CI for it.

RetriggerView in GreptileConfidence Score: 4/5

Fix the missing retry path for failed match writes before merging.

Findings

  1. P1 Ranked wins can go uncounted ▶

Summary

Makes checkGameStateForWin synchronous and moves end-of-game writes into finishGame.

  • Player and AI moves record a game’s result before publishing it.
  • Socket events show match details without recording game results.
  • One synchronous win check serves move handling and AI search.

Reviews (1) · Last reviewed commit: "refactor(game-states): make checkGameSta..." · Reviewed by Greptile

…ttle-cards#1325)

Merge check-game-state-for-win and check-game-state-for-win-sync into one synchronous checkGameStateForWin with no side effects. The move and ai-move endpoints now record the end of a game (status, winner, match) through a new finishGame helper.

Co-authored-by: Arthur Amorim Ruschel <aruschel@usp.br>
Co-authored-by: Gabriel Meirelles Cury <g.cury@usp.br>
Comment on lines +34 to +41
if (game.status === GameStatus.STARTED) {
await Game.updateOne({ id: game.id }).set(gameUpdates);
}

const currentMatch = await sails.helpers.addGameToMatch({
...game,
...gameUpdates,
});

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Ranked wins can go uncounted

If addGameToMatch fails after Game.updateOne succeeds, the game stays finished without being added to its match. Retrying the move is rejected because the game is finished. Previously, loading its winning state retried addGameToMatch; now getGameMatch returns null when the link is missing, so the ranked result stays uncounted.

Make the writes succeed together, or keep a reliable retry path for the missing match write.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[DevEx]: Refactor to consolidate check-game-state-for-win and check-game-state-for-win-sync

1 participant