Regression: a game still ends through four separate code paths

オープン
#173 コメント 0 件 リアクション 0 件 担当者 0 名 GitHub で見る

メンテナーはふだん 1 日以内に返信

まだ誰も着手していません。

評価

難易度
4/5
見積もり時間
3〜5日
初心者へのやさしさ
45/100
issue の種類
リファクタリング
明瞭さ
明確に書かれている
活発さ
活発
技術スタック
javascript
領域
backend, testing

調査の方向性

Read the listed ending paths in js/game.js, the save inference in js/saved-game.js, and the related tests tests/unit/review-result.test.js and tests/unit/draws-promotion.test.js. Start by running npm test and tracing how each ending updates state, stops the clock, and starts scoring. Done means end-state fields are assigned in one function and the acceptance tests cover each listed ending and its expected state.

索引モデルが issue の本文から書いたものです。

説明

help wanted severity: medium tech-debt

Summary

#140 moved the rule decision into gameOutcome, but the end state of a game is still written in four places, and they already behave differently. A related hidden coupling: updateStatusDisplay, which is a render call, also records the players and starts scoring the finished game. Every game ending therefore depends on a redraw happening.

Where

  • js/game.js:725-745 (endWith)
  • js/game.js:856-881 (endGameWithoutMove)
  • js/game.js:1686-1694 (importPGN sets the end-state fields inline)
  • js/game.js:799-802 (Undo reopens a game by hand)
  • js/game.js:637-638, js/game.js:835-836, js/game.js:1760 (each caller of playMove must remember endIfAutomaticDraw)
  • js/saved-game.js:114-122 (result inference for old saves)
  • js/game.js:1576-1584 (updateStatusDisplay calls recordPlayers() and scoreFinishedGame())

Details

importPGN writes the end state itself instead of using either helper:

if (ending) {
    isGameOver = true;
    gameResult = ending.result;
    endedWithoutMove = true;
    currentStatus = ending.status;
}

playMove itself decides only checkmate and stalemate. Each caller then has to call endIfAutomaticDraw for repetition, the fifty-move rule and insufficient material.

Impact

The paths already differ: an imported ending plays no sound, and its clock handling differs from endGameWithoutMove. Anything new that must happen when a game ends, such as #160's results record, has to be added to every path. A future caller of playMove that forgets endIfAutomaticDraw would let a game continue past threefold repetition. If a state change ever skips updateStatusDisplay, scoring and player recording are skipped without any error.

Suggested fix

  1. Add markEnded(result, status, { withoutMove }). Have endWith, endGameWithoutMove and importPGN all set the end state through it.
  2. Add playAndJudge(from, to, promo) (that is, playMove followed by endIfAutomaticDraw) and use it in makeMove and redoMove. Keep importPGN on plain playMove, with a comment explaining why.
  3. Call recordPlayers() and scoreFinishedGame() from these transition helpers, and remove them from updateStatusDisplay in a separate commit.
  4. Once old saves without a result can be dropped, remove the inference in saved-game.js.

Acceptance criteria

  • Before starting: one end-to-end test per ending (mate, stalemate, threefold via redo, fifty-move via redo, resignation, agreed draw, flag fall, PGN result tag). Each asserts gameResult, endedWithoutMove, the status text, that the clock timer stopped, and that scoring started.
  • The end-state fields are assigned in one function.

Getting started

  • Relevant files: js/game.js, js/saved-game.js, tests/unit/review-result.test.js, tests/unit/draws-promotion.test.js
  • How to run tests: npm test

Regression of #140.


Filed by repo-audit/full-audit at 01c6a35. Severity: medium. Effort: M. audit-key: tech-debt|js/game.js|endWith

主要言語
JavaScript
スター
4
フォーク
2
平均マージ
22時間 16分
マージ済み PR(30日)
9

環境構築

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

TMHSDigital/Static_Chess のほかの issue

TMHSDigital/Static_Chess の issue をすべて見る

似ている issue

JavaScript の issue をもっと見る

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。