/* ZimiGames: the shell every game shares. Our own code, GPL-3.0. No network, no modules. One bar (back, title, one stat, Undo or Pause, Menu) and one Menu sheet with the same rows in every game: New game, Settings, How to play, Stats, About this game (Watch and learn and History sit before Stats in the games that have them). The rules are docs/design/games.md. It loads synchronously in
(scripts/games_shell.py writes the tag), so the settings are on before the first paint; the bar and the sheet are built when the page is ready. A game's own buttons are not rewritten: the shell moves them into the Menu and clicks the originals, so every game's own code keeps working as it was. A game may tell the shell more (all optional): ZimiShell.game({ stats: fn -> text or [text], sound: true, hints: true, inProgress: fn, about: fn -> text, settings: fn -> [nodes] (the game's own rows in Settings, built each time the pane opens; use .gs-row, .gs-seg), menu: fn -> { options: [nodes] }, newGame: fn, moveTimer: true, watch: { script, start(ctl), stop(kind) } }) and reads the settings with ZimiShell.settings.get('anim'), etc. What a game calls (every string goes through T, see text.js): ZimiShell.sheet({ title, text, body: [nodes], actions: [{ label, primary, onclick }], onClose }) One sheet anatomy for every start sheet and dialog: the pebble and the title, one line of text, option rows, actions last with the primary last. Focus lands on the sheet itself; Esc closes. Use it only when the choice changes the game (players, seats, mode); a game with no real choice opens ready to play with one line ("Tap to start"). Returns { el, close }. ZimiShell.endSheet({ result: 'win' | 'lose' | 'draw' | 'over', title, name, team, stats: [[label, value]], extra: [nodes under the chips], after, onNew, onClose, newLabel }) The end of a game, the same in every game. The title is the result ("You win", "You lose", "Draw"; `name` makes it "Name wins" for games with several people; 'over' is "Game over", for solo score games with no win state). At most two stat chips, then Close and New game in amber. It appears `after` ms from now (default 600: call it when the last animation ends) and ignores keys for its first 600 ms, so a player still pressing Space or Enter cannot dismiss or restart it unseen. onNew defaults to the game's own New game; a loss looks like a win. Closing the sheet leaves the result line and a large New game over the bottom of the board until a new game starts, so no game is a dead end. `newLabel` (T('shell.nextLevel')) names the amber button for a game that goes on to a next level rather than a new game. ZimiShell.status(el) makes el the game's one status line (role=status, aria-live=polite); returns { say(text), reason(text, ms) } where reason shows for 2 s, then goes back to the line before it. ZimiShell.shake(el) the illegal-move shake (120 ms; an outline flash under reduced motion). ZimiShell.reducedMotion() the one answer: Animation is Off, which is the default where the device asks for less motion and gives way to a choice. ZimiShell.settings, .anim(), .ms(n), .sound(), .stripClock(text) as before. ZimiShell.persist({ save, load }) Never lose a game: leaving a game and coming back puts the same position on the screen. `save()` returns the game's state as plain JSON (null when there is nothing worth keeping: no move made yet). The shell calls it when the page is hidden or closed and shortly after every tap or key, and keeps the result in localStorage under the page's name. `load(data)` is called at once if something is kept; return false (or throw) when it no longer fits and the shell forgets it. A game that builds its own state first can skip `load` and ask ZimiShell.saved() for the data instead. A finished game is saved as finished (so it comes back to its end sheet). Only an explicit New game or a puzzle's day rolling over discards a save: the game then returns null from save(), or calls ZimiShell.forget(). A kept save is never written over or removed until the game has read it (ZimiShell.saved()) or forgotten it, so a library game that reads it once the library answers loses nothing to a tap or a pagehide before then. Arcade games restore paused. ZimiShell.saved() the kept data or null. ZimiShell.saveNow() save right now. ZimiShell.forget() discard. Where a game overrides a shared rule it says why in a comment where it happens. */ (function (root) { 'use strict'; // FNV-1a over UTF-8: ZimiGames' one hash (ZimiShell.hash), for the day's seeds and picks; zimi/gamelinks.py _fnv1a is the same in // Python (a test holds them together). A seed is a promise to everyone who played that day: never change it. function fnv1a(text) { var bytes = unescape(encodeURIComponent(String(text))), hash = 0x811c9dc5; for (var i = 0; i < bytes.length; i++) { hash ^= bytes.charCodeAt(i); hash = Math.imul(hash, 0x01000193) >>> 0; } return hash >>> 0; } // Node (the tests, recent.js and librarykit.js there) asks for the hash alone. if (!root.document && typeof module !== 'undefined' && module.exports) { module.exports = { hash: fnv1a }; return; } var d = root.document; // ZimiText first (text.js beside this file): it must exist before anything below draws a string. A page // that did not load it writes the tag here, synchronously, so the strings are ready before the first paint. if (!root.ZimiText && d && d.currentScript && d.currentScript.src && d.readyState === 'loading') { d.write('