다시 보기: 컴퓨터와 한 판·점수 문구 이전 게임도 다시 볼 수 있게(결과 비교를 순위·점수·이유·요약으로)

운영 DB 44판 중 21판이 '다시 볼 수 없어요'였던 원인: 저장 결과의 practice 표시와 새 scoreText 때문에 JSON 전체 비교가 실패.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
EJClaw
2026-10-11 03:18:54 +09:00
parent 0fe7e27eb2
commit 65a0f34d93
3 changed files with 38 additions and 3 deletions

View File

@@ -10,7 +10,7 @@ import { GAMES } from '@bg/games';
import { openDb } from '../db';
import { Room, type Conn, type RoomConfig } from './room';
import { RoomStore } from './store';
import { Replays, rebuildGame, type ReplayInput } from './replay';
import { Replays, rebuildGame, sameOutcome, type ReplayInput } from './replay';
const json = (v: unknown) => JSON.parse(JSON.stringify(v));
@@ -262,6 +262,24 @@ describe('replay: access and edge cases', () => {
expect(await new Replays(db, GAMES, users).get(pk, 'A')).toMatchObject({ ok: false, status: 410 });
});
test('practice games (marked "practice") and games recorded before final scores existed are replayable', async () => {
const { db, room, users } = omokRoom();
room.act('A', 1, { type: 'place', x: 7, y: 7 });
room.leave('B');
const pk = room.game!.gamePk;
const stored = JSON.parse(db.query<{ result_json: string }, [string]>('SELECT result_json FROM games WHERE id = ?').get(pk)!.result_json);
// As the store writes a game played with computers.
db.query('UPDATE games SET result_json = ? WHERE id = ?').run(JSON.stringify({ ...stored, practice: true }), pk);
const practice = await new Replays(db, GAMES, users).get(pk, 'A');
expect(practice.ok).toBe(true);
// A scored game recorded without scoreText gets it from the rebuilt game; a changed outcome still fails.
const scored = { ...GAMES, omok: { ...GAMES.omok!, result: (st: unknown) => { const r = GAMES.omok!.result(st); return r && { ...r, scoreText: { A: '1점', B: '0점' } }; } } };
const old = await new Replays(db, scored, users).get(pk, 'A');
if (!old.ok) throw new Error(old.error);
expect((JSON.parse(old.body) as ReplayData).result.scoreText).toEqual({ A: '1점', B: '0점' });
expect(sameOutcome({ ranking: [['A'], ['B']], summary: 'x', reason: 'normal' }, { ranking: [['B'], ['A']], summary: 'x', reason: 'normal' })).toBe(false);
});
test('my games list: newest first, paged, with ranks and nicknames', async () => {
const { db, room, clock, users } = omokRoom();
room.act('A', 1, { type: 'place', x: 7, y: 7 });

View File

@@ -41,6 +41,20 @@ function* replaySteps(def: AnyGameDefinition, input: ReplayInput): Generator<Ste
}
}
/**
* Did the rebuilt game end the way it was recorded? Compares the outcome (ranking, scores, reason, summary), not
* labels added around it: the store marks practice games (`practice`), and `scoreText` is display wording that games
* recorded before 2026-10-11 don't have. (Comparing the raw JSON made every practice game and every older scored
* game "not replayable".)
*/
export function sameOutcome(rebuilt: GameResult, stored: GameResult): boolean {
const core = (r: GameResult) => {
const { practice: _p, scoreText: _t, ...rest } = r as GameResult & { practice?: boolean };
return JSON.stringify(rest);
};
return core(rebuilt) === core(stored);
}
/** Final state of a rebuilt game. */
export function rebuildGame(def: AnyGameDefinition, input: ReplayInput): unknown {
let state: unknown;
@@ -233,10 +247,12 @@ export class Replays {
return { ok: false, status: 410, error: '이 게임은 다시 볼 수 없어요.' };
}
// The rebuilt game must end exactly as recorded; otherwise the replay would show a different game.
const result = JSON.parse(g.result_json) as GameResult;
const stored = JSON.parse(g.result_json) as GameResult;
const rebuilt = def.result(built.final);
const matches = g.end_reason === 'void' ? rebuilt === null : JSON.stringify(rebuilt) === g.result_json;
const matches = g.end_reason === 'void' ? rebuilt === null : !!rebuilt && sameOutcome(rebuilt, stored);
if (!matches) return { ok: false, status: 410, error: '이 게임은 다시 볼 수 없어요.' };
// Games recorded before the result screen showed final scores get them from the rebuilt game.
const result: GameResult = rebuilt?.scoreText && !stored.scoreText ? { ...stored, scoreText: rebuilt.scoreText } : stored;
const ids = this.idMap(start.players, seats.map((s) => s.user_id));
const players: Record<string, PublicUser> = {};