/// 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
[]
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