feat(course): audit fixes and twelve extensions (Modules 7b, 12, 13)
Audit pass: - Frame rule counts frames since the last move, so every step lasts exactly movesEvery(score) frames, also right after a speed-up - clampTime + MAX_GAP_MS: a hidden tab no longer replays the whole gap - Pong Restart keeps held keys - Fact corrections (mulberry32 output coverage, Haskell IO, uniform) - New fixture cases from a mutation analysis Extensions (all three tracks, shared fixtures, course text): - Turn queue (Exercise 10.6), persisted high score (10.7), replay viewer (10.8), play() end-to-end helper (9.5) - Optional State (4.7) and optional Module 7b, stack-safe IO - Module 12, property-based testing with generators, shrinking, invariants and a model-based oracle - Module 13, Conway's Life with a Click event - JS type checking via jsconfig, JSDoc and types.d.ts - Maintainer tools: scripts/mutate.mjs, scripts/typecheck-js.sh, Playwright end-to-end suite (e2e/, scripts/e2e.sh) Docs: docs/contract.md is the canonical contract; specs and build log under docs/superpowers. Verified: JS 728, Scala 689, F# 691 unit tests; stubs and progression M01-M13 and M07b; 176 end-to-end tests on Chromium and WebKit. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
This commit is contained in:
1 parent
876f5aabb1
commit
edd57f0148
253 files changed
+21753
-503
No files matched your search
@@ -41,7 +41,48 @@ let private randomSeed () : uint32 =
|
||||
crypto?getRandomValues (buf)
|
||||
buf[0]
|
||||
|
||||
/// A Platform drawing on `canvas` (an HTML canvas element) and listening to the window's keys and frames.
|
||||
// m10: where the best score survives a reload.
|
||||
let private bestKey = "pure-snake-best"
|
||||
|
||||
// Storage can be missing, blocked (privacy settings; some browsers throw as soon as localStorage is touched) or hold
|
||||
// anything: all of that reads as 0, and nothing throws. Only a non-negative whole number counts.
|
||||
let private loadBest () : int =
|
||||
try
|
||||
let raw: string = window?localStorage?getItem (bestKey)
|
||||
|
||||
if isNull raw then
|
||||
0
|
||||
else
|
||||
// Only what saveBest writes counts: decimal digits, at most 2147483647 (so all three tracks read alike).
|
||||
let digits = raw.Length >= 1 && raw.Length <= 10 && Seq.forall (fun c -> c >= '0' && c <= '9') raw
|
||||
|
||||
if digits && float raw <= 2147483647.0 then int (float raw) else 0
|
||||
with _ ->
|
||||
0
|
||||
|
||||
// A record that cannot be stored is lost; the game goes on.
|
||||
let private saveBest (score: int) : unit =
|
||||
try
|
||||
window?localStorage?setItem (bestKey, string score)
|
||||
with _ ->
|
||||
()
|
||||
|
||||
// m13: a click on the canvas, in canvas pixels. The page may show the canvas larger or smaller than its width and
|
||||
// height (CSS, zoom) and draws a border round it, so the window coordinates of the mouse are made relative to the
|
||||
// drawing area (inside the border) and then scaled. The listener is added when the runtime asks, not before.
|
||||
let private onClick (canvas: obj) (cb: int -> int -> unit) : unit =
|
||||
canvas?addEventListener ("click",
|
||||
fun (e: obj) ->
|
||||
let r = canvas?getBoundingClientRect ()
|
||||
let scale (client: float) (start: float) (border: float) (size: float) (shown: float) =
|
||||
int (System.Math.Floor((client - start - border) * size / shown))
|
||||
|
||||
cb
|
||||
(scale e?clientX r?left canvas?clientLeft canvas?width canvas?clientWidth)
|
||||
(scale e?clientY r?top canvas?clientTop canvas?height canvas?clientHeight))
|
||||
|
||||
/// A Platform drawing on `canvas` (an HTML canvas element) and listening to the window's keys and frames, and from
|
||||
/// m13 to clicks on the canvas.
|
||||
let create (canvas: obj) : Platform =
|
||||
let ctx = canvas?getContext ("2d")
|
||||
|
||||
@@ -56,9 +97,12 @@ let create (canvas: obj) : Platform =
|
||||
ctx?textAlign <- align
|
||||
ctx?fillText (text, x, y)
|
||||
RandomSeed = randomSeed
|
||||
LoadBest = loadBest
|
||||
SaveBest = saveBest
|
||||
StartClock = startClock
|
||||
OnKeyDown = listen "keydown"
|
||||
OnKeyUp = listen "keyup" }
|
||||
OnKeyUp = listen "keyup"
|
||||
OnClick = onClick canvas }
|
||||
|
||||
/// The page's `<canvas id="game">`.
|
||||
let gameCanvas () : obj = window?document?getElementById ("game")
|
||||
@@ -0,0 +1,7 @@
|
||||
/// fsharp/life.html loads this module (web/dist/LifeMain.js): one call runs Conway's Life (m13).
|
||||
module Snake.Web.LifeMain
|
||||
|
||||
open Snake
|
||||
|
||||
// The same Platform as Snake's; Life listens to its clicks, and never asks for LoadBest or SaveBest.
|
||||
Runtime.runApp (BrowserPlatform.create (BrowserPlatform.gameCanvas ())) (Life.App.lifeMain ())
|
||||
@@ -3,4 +3,5 @@ module Snake.Web.PongMain
|
||||
|
||||
open Snake
|
||||
|
||||
// The same Platform as Snake's; pongMain () never asks for LoadBest or SaveBest, so the stored best is untouched.
|
||||
Runtime.runApp (BrowserPlatform.create (BrowserPlatform.gameCanvas ())) (Pong.App.pongMain ())
|
||||
@@ -0,0 +1,39 @@
|
||||
/// fsharp/replay.html loads this module (web/dist/ReplayMain.js): the replay viewer of m10.
|
||||
/// The recording is plain JSON next to the test fixtures; the page is served from the repository root by
|
||||
/// scripts/serve.mjs, so it can be fetched (relative to the page, fsharp/replay.html). It is decoded with plain
|
||||
/// JavaScript interop, without a JSON library, and the viewer starts once it has arrived.
|
||||
module Snake.Web.ReplayMain
|
||||
|
||||
open Fable.Core
|
||||
open Fable.Core.JsInterop
|
||||
open Snake
|
||||
open Snake.Model
|
||||
open Snake.Replay
|
||||
|
||||
[<Global>]
|
||||
let private window: obj = jsNative
|
||||
|
||||
let private direction (dir: string) : Direction =
|
||||
match dir with
|
||||
| "Up" -> Up
|
||||
| "Down" -> Down
|
||||
| "Left" -> Left
|
||||
| "Right" -> Right
|
||||
| other -> failwith $"unknown direction in the recording: {other}"
|
||||
|
||||
// A recording holds only Tick and Turn: {"tag":"Tick"} or {"tag":"Turn","dir":"Up"}.
|
||||
let private action (a: obj) : Action =
|
||||
match (a?tag: string) with
|
||||
| "Tick" -> Tick
|
||||
| "Turn" -> Turn(direction a?dir)
|
||||
| other -> failwith $"unknown action in the recording: {other}"
|
||||
|
||||
let private recording (data: obj) : Recording =
|
||||
{ Seed = uint32 (data?seed: float)
|
||||
Actions = (data?actions: obj[]) |> Array.map action |> List.ofArray }
|
||||
|
||||
let private start (data: obj) : unit =
|
||||
Runtime.runApp (BrowserPlatform.create (BrowserPlatform.gameCanvas ())) (replayMain (recording data))
|
||||
|
||||
window?fetch("../fixtures/replay-sample.json")?``then``(fun (response: obj) -> response?json ())?``then``(start)
|
||||
|> ignore
|
||||
@@ -19,13 +19,20 @@
|
||||
<Compile Include="../src/Input.fs" />
|
||||
<Compile Include="../src/App.fs" />
|
||||
<Compile Include="../src/Runtime.fs" />
|
||||
<Compile Include="../src/Replay.fs" />
|
||||
<Compile Include="../src/Pong/Model.fs" />
|
||||
<Compile Include="../src/Pong/Rules.fs" />
|
||||
<Compile Include="../src/Pong/View.fs" />
|
||||
<Compile Include="../src/Pong/App.fs" />
|
||||
<Compile Include="../src/Life/Model.fs" />
|
||||
<Compile Include="../src/Life/Rules.fs" />
|
||||
<Compile Include="../src/Life/View.fs" />
|
||||
<Compile Include="../src/Life/App.fs" />
|
||||
<Compile Include="BrowserPlatform.fs" />
|
||||
<Compile Include="SnakeMain.fs" />
|
||||
<Compile Include="PongMain.fs" />
|
||||
<Compile Include="ReplayMain.fs" />
|
||||
<Compile Include="LifeMain.fs" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Fable.Core" Version="5.6.0" />
|
||||
|
||||
Reference in new issue
Block a user