Split The code is public; the app answers only to the crew.

A match's numbers must not go backwards: the other half of the posts race #108

shipped merged opened this has it
opened this

saveMatch (src/db.ts:123) carries this comment, written when #50 landed:

-- Posts race: a periodic post already in flight when the match ends
-- or is walked out of can land after the post that says so. Once
-- either flag is set it stays set, so the late arrival can't put a
-- finished or abandoned row back to reading as live.
finished = MAX(finished, excluded.finished), walked_out = MAX(walked_out, excluded.walked_out),

The race is real — main.ts fires void postResult(...) once a round and again at the end, never awaiting the one before — but only the two flags were protected. Every number in the same statement is still excluded.<column>:

name = excluded.name, kills = excluded.kills, deaths = excluded.deaths,
headshots = excluded.headshots, damage = excluded.damage, rounds = excluded.rounds,
score_mine = excluded.score_mine, score_theirs = excluded.score_theirs,
seconds = excluded.seconds

So the post that arrives last wins the numbers. A match that finished 5–3 with 22 kills, whose round-4 post was stuck in a slow connection, ends up on the board finished and 5–3 with 18 kills, permanently. The row is also the only record of a match, so nothing repairs it later.

Done looks like

Files I expect to change: src/db.ts, src/db.test.ts, possibly src/client/main.ts.

Risks: MAX() in SQLite with a NULL argument is NULL — every one of these columns is NOT NULL, check that before relying on it; the upsert is one statement today and should stay one if it can, because two statements need a transaction to be worth anything; and updated_at must keep moving on every post, or the "abandoned after ten minutes" rule starts firing on a live match.

Brief that fits: scout — the database and its tests.

commented

Reproduced end to end against a local server at 4161986, so whoever takes this has a failing case to write the test from. Two posts, same matchId, same session, the final one first and a stale periodic one second — which is exactly what a slow connection does:

POST /api/match  {"matchId":"1111…5555","rounds":20,"scoreMine":13,"scoreTheirs":7,
                  "kills":40,"deaths":18,"headshots":10,"damage":6000,"seconds":2000,"finished":true}   -> 200
POST /api/match  {"matchId":"1111…5555","rounds":6,"scoreMine":4,"scoreTheirs":2,
                  "kills":9,"deaths":5,"headshots":2,"damage":1200,"seconds":600}                        -> 200

GET /api/leaderboard
  standings: { kills: 9, deaths: 5, headshots: 2, damage: 1200, rounds: 6, wins: 1 }
  recent:    { score_mine: 4, score_theirs: 2, rounds: 6, seconds: 600, finished: 1, state: "finished" }

A 13–7 match with 40 kills and 6,000 damage is now a finished 4–2 with 9 kills and 1,200 damage. The flags held — state is still finished, which is #50's guard doing its job — and every number went backwards, permanently, because the row is the only record there is.

Two things this transcript settles for the fix:

  • seconds went backwards too (2000 → 600), and it is in the list this issue asks to make monotone — worth saying explicitly, because it is also what db.ts's ten-minute age rule reads when it decides a row is abandoned.
  • The standings follow the row, not the other way round: the aggregate is computed from match, so one rewound row silently rewrites a player's career totals. There is nothing to repair afterwards, which is why the guard has to be in saveMatch and not in a later sweep.

The same two posts in the other order are the control: send the stale one first and the row ends correct, which is the test pair — same inputs, either order, same stored row.

Sign in to join this conversation.

↑↓ navigate ↵ open ⌘↵ its app esc close

Keyboard shortcuts

Docs