Skip to content

feat(site/src/pages/CoderCupPage): add 5-second rewind to the Codernauts game - #31613

Merged
bartekgatzcoder merged 7 commits into
mainfrom
codernauts-rewind
Nov 10, 2026
Merged

bartekgatzcoder merged 7 commits into
mainfrom
codernauts-rewind

Conversation

@bartekgatzcoder

@bartekgatzcoder bartekgatzcoder commented Nov 1, 2026 •

Copy link
Copy Markdown
Collaborator

Stacked on #31612.

Adds a 5-second rewind to the Codernauts game. Pressing R plays the last five seconds back in reverse at 2.5x speed under a VHS rewind effect and a "REWINDING..." label, then resumes play from that moment. It works mid-flight and after a crash, so a crashed lander can be saved. The start screen and the canvas's hidden controls description mention the R key, a polite live region announces "Rewinding 5 seconds" to screen readers, and players who prefer reduced motion get the label without the VHS effect.

Implementation notes
  • The simulation step moves out of the frame loop into updateGame(dt, limits), unchanged apart from taking the limits as a parameter. Each frame either advances the game and records a GameSnapshot, or, while rewinding, steps a cursor backwards through history and restores the newest snapshot at or before it.
  • History holds every frame snapshot from the last five seconds, plus one older snapshot so a full rewind lands exactly five seconds back. Arrays are deep-copied with structuredClone; terrain is shared because it is only ever replaced, never mutated.
  • Rewind covers lander physics, fuel, the explosion, Codernauts boarding, disembarking, and saved status, and the round-complete timer. It is ignored on the start screen, the victory screen, while already rewinding, and on key repeat. A resize regenerates the terrain, so it clears the history.
  • drawVhsRewindEffect redraws the finished frame through red and cyan tinted offscreen copies with per-band sideways shifts and occasional hard tears, then adds a rolling tracking-noise band, static, scanlines, and a blue cast. The offscreen buffers are only resized when the canvas size changes. drawRewindLabel draws the rewind symbol and "REWINDING...".
  • Tests use userEvent and cover the label and announcement appearing during a rewind and clearing after, the lander returning to an earlier, slower state, and rewinding after a crash to bring the lander back before the explosion would respawn it (mutation-checked against a no-op restore). Ran pnpm check, pnpm lint, tsc, and the CoderCupPage Vitest suite locally.

Generated by Coder Agents on behalf of @bartekgatzcoder.

…nauts game

Replace the intro countdown with a start screen that waits for the player
to choose Easy, Moderate, or Hard by clicking a button or pressing E, M,
or H. Hard keeps the original landing limits (speed 15, yaw 10 degrees);
Moderate allows 30 and 20 degrees; Easy allows 50 and 30 degrees. The HUD
velocity and yaw warnings use the chosen level's limits.
…uts game

Pressing R replays the last five seconds of play backwards at 2.5x speed
with a VHS rewind effect (color bleed, tearing, tracking noise, static,
and scanlines) under a REWINDING... label, then resumes play from that
point. It works mid-flight and after a crash. The game records a snapshot
of its state every frame and keeps five seconds of history.
@bartekgatzcoder

bartekgatzcoder commented Nov 9, 2026 •

Copy link
Copy Markdown
Collaborator Author

@DanielleMaywood @jeremyruppel could one of you review this as well? It is stacked on #31612 (difficulty levels) and adds a 5-second rewind with a VHS effect to the Codernauts easter egg game. Please merge #31612 first. CI is green; the doc-check bot is advisory and the game has no user docs.


Generated by Coder Agents on behalf of @bartekgatzcoder.

@jeremyruppel jeremyruppel left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Reviewed the rewind change against #31612. Rewind logic looks correct: the updateGame extraction is behavior-preserving (moving updateDisembarks into it is right), the snapshot covers every gameplay variable updateGame mutates, all snapshotted types are plain data so structuredClone is safe, per-snapshot terrain handles rewinding across a round boundary, and the history trim keeps exactly one pre-window snapshot so chained rewinds cannot exceed 5 s. CoderCupPage Vitest suite (7/7) and biome pass locally.

Findings inline: reduced motion for the VHS effect, per-frame offscreen canvas resizing, and a test assertion that cannot fail.


Generated by Coder Agents on behalf of @jeremyruppel.

* played backwards: color bleed, horizontal tearing, a rolling
* tracking-noise band, static, and scanlines. Works in device pixels.
*/
function drawVhsRewindEffect(

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

should-fix (a11y, motion): The VHS effect (random tears, red/cyan band shifts, full-screen static re-randomized every frame) ignores prefers-reduced-motion. Suggest skipping drawVhsRewindEffect when window.matchMedia("(prefers-reduced-motion: reduce)").matches and keeping only drawRewindLabel.

Related note, not verified: per-frame static and saturated red tears are a WCAG 2.3.1 (three flashes) risk. Likely under the general flash threshold since bright areas are small, but the reduced-motion path would cover sensitive users either way.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Fixed in 95bf088: with prefers-reduced-motion: reduce the VHS effect is skipped and only the label is drawn. On WCAG 2.3.1: the static dots are 2x1 px and cover well under 1% of the frame, the tears shift thin line art on black, and nothing changes overall frame brightness, so I don't think it hits the general flash threshold. Happy to throttle the noise if you see it differently.

Coder Agents on behalf of @bartekgatzcoder.

source: HTMLCanvasElement,
color: string,
) {
target.width = source.width;

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

should-fix (perf): Assigning width/height, even to the same value, resets the canvas and clears its backing store. This runs for red and cyan every rewind frame, plus buffers.frame at line 2001, all at device-pixel size. Guard with a size check, e.g. if (target.width !== source.width || target.height !== source.height) { ... }, and clear explicitly if needed.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Fixed in 95bf088: a matchSize helper only assigns width/height when they differ. Each buffer is then overwritten by an opaque full-frame drawImage, so no explicit clear is needed.

Coder Agents on behalf of @bartekgatzcoder.

}

// The game state needed to resume play from a moment in the past.
type GameSnapshot = {

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

nit (FE3): the gameplay field list now lives in four places: the let declarations, this type, recordSnapshot, and restoreSnapshot. Any state added later that is not added to all four silently breaks rewind. Consider holding gameplay state in a single object so a snapshot is one structuredClone(state) (with terrain shared). Fine to defer if that refactor is out of scope.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Agreed this is a real maintenance risk. I'd like to defer it: moving the gameplay state into one object touches most of the loop, and that refactor reads better as its own PR. Leaving it as is here.

Coder Agents on behalf of @bartekgatzcoder.

}

/** Draw the VCR on-screen display shown while rewinding. */
function drawRewindLabel(ctx: CanvasRenderingContext2D) {

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

should-fix (a11y, ARIA rule 5): the R control and the "REWINDING..." status exist only as canvas pixels, and the canvas accessible name from #31612 does not mention controls. Whatever fix lands for the start-screen finding there (DOM controls or an aria-describedby hint) should mention R too. A visually hidden aria-live="polite" region that announces "Rewinding" would cover the status.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Fixed in 95bf088: the hidden controls description now ends with "Press R to rewind the last 5 seconds.", and a visually hidden aria-live="polite" region announces "Rewinding 5 seconds" while the replay runs. Tests assert the announcement appears and clears.

Coder Agents on behalf of @bartekgatzcoder.

runFrame(now);
};

// R does nothing before a difficulty is chosen.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

nit (FE1): this assertion cannot fail. On the start screen the loop returns before the rewind draw, and history is empty so startRewind bails on history.length < 2 anyway; removing the !limits guard would still pass. Either drop it, or cover a guard that matters (victory screen, or key repeat via { key: "r", repeat: true } during play).

The headline case, rewinding after a crash to save the lander, is also not covered.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Fixed in 95bf088: dropped that assertion and added the headline case, "brings a crashed lander back when R is pressed during the explosion". It waits for the crash, presses R, and checks the lander is drawn again before the explosion could have respawned it. Mutation-checked: making restoreSnapshot a no-op fails it.

Coder Agents on behalf of @bartekgatzcoder.

advance(16);
expect(drew("⚠ WARNING")).toBe(true);

fireEvent.keyDown(window, { key: "R" });

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

should-fix (FE1): same as #31612, prefer userEvent (await user.keyboard("R")) over fireEvent here and at lines 155 and 160.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Fixed in 95bf088: fireEvent is gone from the file.

Coder Agents on behalf of @bartekgatzcoder.

…-levels

# Conflicts:
#	site/src/pages/CoderCupPage/LunarLander.tsx
… picker

Address review feedback: render Easy, Moderate, and Hard as shared
Button components over the canvas instead of canvas-drawn pixels with a
hand-written hit test, so they are focusable and announced to screen
readers. Describe the flight controls to assistive technology, switch
the tests to userEvent and role queries, and reword comments that
described history.
# Conflicts:
#	site/src/pages/CoderCupPage/LunarLander.tsx
Address review feedback: skip the VHS effect when the player prefers
reduced motion, only resize the offscreen buffers when the canvas size
changes, describe the R control to assistive technology, and announce
the rewind in a polite live region. Switch the rewind tests to
userEvent, drop an assertion that could not fail, and cover rewinding
after a crash.
@bartekgatzcoder

Copy link
Copy Markdown
Collaborator Author

@jeremyruppel the review items here are addressed too, with replies in each thread:

  • Reduced motion: with prefers-reduced-motion: reduce, only the label is drawn and the VHS effect is skipped.
  • Offscreen canvases: they're only resized when the canvas size changes.
  • Screen readers: the R control is in the hidden controls description, and a polite live region announces "Rewinding 5 seconds".
  • Tests: they use userEvent. I dropped the assertion that couldn't fail and added a test for rewinding after a crash. A no-op restore makes that test fail.
  • Single game-state object: I'd like to defer this to its own PR, as explained in that thread.

The branch includes the updated #31612 and current main.


Generated by Coder Agents on behalf of @bartekgatzcoder.

@jeremyruppel jeremyruppel left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Earlier feedback is addressed in 95bf088. Reduced motion now skips the VHS effect, the offscreen buffers only resize when the canvas size changes, R is in the hidden controls description, and a polite live region announces the rewind. The tests use userEvent, and the new crash-rewind test covers the headline case. Deferring the single game-state object to its own PR is fine. CoderCupPage Vitest (8/8), biome, lint:design-system, and tsc pass locally. The only failing check is the advisory AI doc-check, which timed out.

Optional nits, fine as follow-ups:

  • The announcement always says "Rewinding 5 seconds", even when less than 5 seconds of play has been recorded.
  • The crash test infers that the lander was drawn from context.rotate calls, which only holds while drawLander is the sole caller.

Generated by Coder Agents on behalf of @jeremyruppel.

Base automatically changed from codernauts-difficulty-levels to main November 10, 2026 18:01
@bartekgatzcoder
bartekgatzcoder temporarily deployed to main November 10, 2026 18:01 — with GitHub Actions Inactive
# Conflicts:
#	site/src/pages/CoderCupPage/LunarLander.test.tsx
#	site/src/pages/CoderCupPage/LunarLander.tsx
@bartekgatzcoder
bartekgatzcoder temporarily deployed to main November 10, 2026 18:02 — with GitHub Actions Inactive
@bartekgatzcoder
bartekgatzcoder merged commit 0fbe705 into main Nov 10, 2026
27 checks passed
@bartekgatzcoder
bartekgatzcoder deleted the codernauts-rewind branch November 10, 2026 18:14
@bartekgatzcoder
bartekgatzcoder temporarily deployed to main November 10, 2026 18:14 — with GitHub Actions Inactive
@github-actions github-actions Bot locked and limited conversation to collaborators Nov 10, 2026

This branch was previously deployed

1 inactive deployment
main — f78b47f1 Deployed Nov 10, 2026 by bartekgatzcoder via cla #253645
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants