package snake package web import scala.scalajs.js import scala.scalajs.js.Dynamic.global // Four programs, one runtime: build-web.sh picks the entry point with --main-class. private def canvas() = global.document.getElementById("game") /** scala/index.html: Snake. */ object SnakeMain: def main(args: Array[String]): Unit = Runtime.runApp(App.main, BrowserPlatform(canvas())) /** scala/pong.html: Pong (m11). */ object PongMain: def main(args: Array[String]): Unit = Runtime.runApp(pong.PongApp.pongMain, BrowserPlatform(canvas())) /** * scala/replay.html: the replay viewer (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, scala/replay.html). * The JSON is decoded by hand with js.Dynamic: `{seed, actions}`, each action `{"tag":"Tick"}` or * `{"tag":"Turn","dir":"Up"}`. */ object ReplayMain: private def action(a: js.Dynamic): Action = a.tag.asInstanceOf[String] match case "Tick" => Action.Tick case "Turn" => Action.Turn(Direction.valueOf(a.dir.asInstanceOf[String])) case other => throw new IllegalArgumentException(s"a recording holds only Tick and Turn, not $other") private def recording(json: js.Dynamic): Recording = Recording( json.seed.asInstanceOf[Double].toLong, json.actions.asInstanceOf[js.Array[js.Dynamic]].toList.map(action) ) def main(args: Array[String]): Unit = val loaded = global.fetch("../fixtures/replay-sample.json").`then`((r: js.Dynamic) => r.json()) loaded.`then`((json: js.Dynamic) => Runtime.runApp(Replay.replayMain(recording(json)), BrowserPlatform(canvas()))) /** scala/life.html: Conway's Life with the mouse (m13). */ object LifeMain: def main(args: Array[String]): Unit = Runtime.runApp(life.LifeApp.lifeMain, BrowserPlatform(canvas()))