@caisual/cli 0.13.0 → 0.14.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. package/dist/caisual.mjs +67 -7
  2. package/package.json +1 -1
package/dist/caisual.mjs CHANGED
@@ -1046,10 +1046,10 @@ import { tmpdir } from "node:os";
1046
1046
  import { basename as basename2, dirname as dirname3, extname as extname2, join as join4, resolve as resolve2 } from "node:path";
1047
1047
 
1048
1048
  // ../../docs/publish.md
1049
- var publish_default = '# Publish a game on Caisual\n\nCaisual hosts browser games supplied as folders. Each publish creates an immutable version and moves the game\'s stable link to that version.\nThe publishing flow supports both single-player and multiplayer games and does not require changes in the Caisual dashboard. Player identity, rooms, cloud saves, leaderboards, and the daily challenge come from the game kit, documented in [kit.md](./kit.md).\n\nA game published with `"overlay": { "version": 1 }` is a standard game: it runs full screen and Caisual draws the menu, the lobby, invitations, friends, matchmaking, spectators, leaderboards, voice, the end of a match and Play again on top of it. Write the field, the HUD and the settings; declare the rest in the manifest. See [Sessions and the standard overlay](./kit.md#sessions-and-the-standard-overlay).\n\n## Game folder\n\nUse this structure:\n\n```text\nmy-game/\n caisual.json\n server.js # optional, required only for multiplayer rooms\n client/\n index.html\n i18n/\n en.json\n it.json\n ...\n```\n\n`caisual.json` and `client/index.html` are required. Put every file used by the game under `client/`.\n\nRun `npx @caisual/cli init my-game` to create a minimal single-player folder with the standard overlay and one local mode. Run `npx @caisual/cli init --multiplayer my-game` to add a room mode with matchmaking and a `server.js`. Both templates include `client/i18n/en.json` used by the example client through `const t = await c.text()`. They are full screen and use `c.session` and `c.overlay`; neither draws a menu or a lobby of its own.\n\nThe whole CLI is:\n\n```text\ncaisual init [--multiplayer | --arcade] [folder]\ncaisual dev [folder] [--port 8790] [--day YYYY-MM-DD] [--latency ms [--jitter ms] [--loss percent]]\ncaisual check [folder] [--json]\ncaisual publish [folder]\ncaisual unlist [folder|id]\ncaisual relist [folder|id]\ncaisual delete [folder|id] --yes\ncaisual skill\ncaisual --help\ncaisual --version\n```\n\n`caisual init --arcade my-arena` creates a complete online action starter in English and Italian. It includes a shared fixed-step simulation, an authoritative `server.js`, numbered and acknowledged controls, local prediction with reconciliation, remote interpolation, safe HUD placement, standings and fast rematches. All `init` variants also generate `package.json` with `dev`, `test` and optional `test:browser` scripts. See [Responsive action games](./kit.md#responsive-action-games) and [Testing with a browser](/docs/local-development#testing-with-a-browser).\n\n`caisual check [folder] [--json]` runs every local check used by publish: the manifest, game texts, client files, the `server.js` bundle, the three required images, and screenshots. It needs no key and uploads nothing. It validates files without launching or playing the game in a browser. With `--json` it prints a report for tools and agents. It exits with code 2 when the report has errors.\n\n`caisual skill` writes this guide and [kit.md](./kit.md) into `.claude/skills/caisual/SKILL.md` in the current folder and adds a `## Caisual` section to `AGENTS.md`, so an agent working in that repository reads the rules before it starts.\n\n## caisual.json\n\nThe file must contain one JSON object. Unknown fields are rejected. This is a complete single-player example:\n\n```json\n{\n "manifest": 1,\n "id": "my-game",\n "name": "My Game",\n "description": "A short description of the game.",\n "cover": "cover.png",\n "card": "card.png",\n "icon": "icon.png",\n "screenshots": ["screenshots/level-one.png"],\n "tags": ["puzzle"],\n "languages": ["en", "it"],\n "platform": "both",\n "overlay": { "version": 1, "accent": "#397e83" },\n "orientation": "landscape",\n "input": ["keyboard", "mouse", "touch"],\n "visibility": "public",\n "network": [],\n "isolated": false,\n "requires": { "webgl2": false, "webgpu": false, "wasm": false, "threads": false, "memoryMb": null, "performance": "light" },\n "players": { "min": 1, "max": 1 },\n "lobby": false,\n "persistent": false,\n "spectators": true,\n "boards": { "main": { "source": "client", "label": "Best run", "periods": ["daily", "all-time"] } },\n "roles": [],\n "teams": null,\n "voice": "none",\n "modes": [\n { "id": "solo", "execution": "local", "label": "Solo", "instructions": "One run against the clock." }\n ]\n}\n```\n\n- `manifest` is required and must be `1`.\n- `overlay` is optional and defaults to absent. Set `{ "version": 1 }` to publish a standard game and get the whole overlay. `accent` is optional and must be a six-digit `#RRGGBB` colour; no other CSS is accepted. A game without `overlay` keeps its historical flow and draws its own menus, and nothing in this guide changes for it.\n- `id` is required. Use 3 to 32 lowercase ASCII letters or digits, with single hyphens only between groups. The ID becomes the URL slug. Choose it carefully because it cannot be renamed or reused after deletion.\n- `name` is required and must contain 1 to 60 characters.\n- `description` is optional, defaults to an empty string, and can contain at most 500 characters.\n- `cover`, `card` and `icon` are required, distinct relative file paths inside `client/`. Use PNG, JPEG or WebP, at most 2 MB (2,000,000 bytes) per image. Do not include a query, fragment, empty segment or parent segment. See the exact dimensions below.\n- `screenshots` is optional and defaults to `[]`. It accepts up to 8 relative paths inside `client/`.\n- `tags` is optional and defaults to `[]`. It accepts up to 10 values. Each value uses 1 to 24 lowercase letters, digits, or hyphens.\n- `languages` is required: a non-empty array of distinct BCP 47 tags that includes `en`, such as `["it", "en", "pt-BR"]`. English is always required alongside the game\'s own languages. The first entry remains the default and may be a language other than English. Tags are normalized to canonical casing. The catalog and standard menu show the available languages.\n- `platform` is required. Use `desktop` when the game needs a keyboard, mouse, large display, or desktop performance. Use `mobile` when it is designed only for touch and small screens. Use `both` only after checking that layout, performance, and controls work on both.\n- `orientation` is optional and defaults to `landscape`. Use `landscape` or `portrait` to describe the intended mobile layout. The device may not honor an orientation request.\n- `input` is optional and defaults to `[]`. Include every supported input from `keyboard`, `mouse`, `touch`, and `gamepad`. Do not claim an input until the game is usable with it.\n- `visibility` is optional and defaults to `public`. Use `public` for catalog eligibility or `unlisted` for access by direct link only.\n- `network` is optional and defaults to `[]`. List every external host contacted or loaded by the game, without scheme, port, path, query, or fragment, for example `api.example.com`. If an external host is missing, the browser blocks the request. Keep the array empty when the game uses only its own files and Caisual services.\n- `isolated` is optional and defaults to `false`. Use `true` only when the game requires shared memory or threaded WebAssembly. Every external host in `network` must then send headers compatible with cross-origin isolation.\n- `requires` is optional and defaults to `{ "webgl2": false, "webgpu": false, "wasm": false, "threads": false, "memoryMb": null, "performance": "light" }`. Declare the minimum capabilities the game truly needs to run. For example, a game with a WebGPU renderer and a WebGL2 fallback declares only `webgl2`. Set `threads` together with `isolated: true`. `memoryMb` accepts `null` or a multiple of 256 from 512 to 32768. Use `light`, `medium`, or `heavy` for the expected performance load.\n- `players` is optional and defaults to `{ "min": 1, "max": 1 }`. Both values are integers from 1 to 24 and `max` must be at least `min`. Set the range that a room needs before play can start.\n- `lobby` is optional and defaults to `false`. Use `true` when players must choose roles or teams, mark themselves ready, and wait for the host to start. With `false`, play starts when the first player enters and later players may join in progress. A room mode with resolved `players.max === 1` always starts on entry and bypasses the lobby, including an inherited `lobby: true`.\n- `persistent` is optional and defaults to `false`. Use `true` when room members must be able to return with the same code after disconnecting, including while the game is already playing. Persistent rooms expire after 30 days without activity.\n- `spectators` is optional and defaults to `{ "delayMs": 3000 }`. Use `false` to disable watching, `true` for the default three-second delay, or `{ "delayMs": N }` to choose an integer delay from 0 to 30000 milliseconds.\n- `boards` is optional and defaults to `{}`. Each key is a leaderboard id. Use `{ "source": "server" }` to accept only `room.board.submit`, or `{ "source": "client" }` to allow browser submissions. Boards not listed use `client`. A manifest may list up to 32 boards. `label` is optional text or a language-to-text object, 1 to 48 characters on one line per translation, and names the board in the overlay; without it the overlay shows the id. `periods` is optional and defaults to `["all-time"]`: list `daily`, `all-time` or both, without duplicates. `all-time` means the best score with no day attached, not a sum of days. `periods` only chooses what the overlay offers; it does not change what the score APIs accept. `day` is `submit` by default, or `start` for a server board. With `start`, submit a daily score from the room server with `{ day: room.daily.day }` within 10 minutes of its next UTC midnight. See [Daily challenge](./kit.md#daily-challenge).\n- `roles` is optional and defaults to `[]`. Each entry has an `id` of 1 to 32 lowercase letters, digits, or internal hyphens, a `min` integer from 0 to 24, and an optional `max` in the same range. Rooms enforce these capacities in the lobby. `label` is optional text or a language-to-text object, 1 to 32 characters on one line per translation, and names the role in the overlay lobby; without it the overlay shows the id.\n- `teams` is optional and defaults to `null`. An object has `min` and `max` integers from 2 to 24, with `max` at least `min`. Rooms balance players who do not choose a team.\n- `voice` is optional and defaults to `none`. Use `room` so everyone in the room can hear each other, `team` to restrict voice to teammates, or `proximity` when `server.js` sets the gain between player pairs. Use `none` to disable voice.\n- `modes` is optional and defaults to `[]`. A mode has a unique `id` using 1 to 32 lowercase letters, digits, or internal hyphens. It may have `matchmaking` with `key`, an array of 1 to 8 unique field names, and `timeoutMs`, an integer from 1,000 to 300,000. Each field name uses 1 to 32 lowercase letters, digits, or hyphens and starts with a letter or digit. A mode may also define `players: { min, max }` (both integers from 1 to 24, max at least min) and `lobby` (boolean). Each supplied field replaces its root counterpart for creation, joining and matchmaking, including filling an open room; omitted fields inherit the root value. `players` is replaced as a whole, not merged. `mode: null` uses the root configuration. Roles, teams, voice and persistence remain game-wide. Catalog labels consider the resolved modes, or the root range when there are no modes: Single player, Multiplayer, or Solo + Multiplayer.\n- A standard game declares at least one mode, and every mode of a standard game needs `execution`: `local` for a run inside the browser, `room` for a room. A `local` mode resolves to exactly one player with `lobby` false and no matchmaking; it is not a room of one, and the create and match APIs refuse it. A `room` mode requires `server.js`, checked by the CLI and again when the version is published.\n- `label` is optional text or a language-to-text object, 1 to 48 characters on one line per translation, and names the mode in the standard menu; without it the menu shows the id. `instructions` is optional text or a language-to-text object, 1 to 160 characters on one line per translation, and adds a line under the label. Both are text, never HTML, and resolve to the player\'s language with the fallback described below.\n- `matchmaking.defaults` is required when the overlay is expected to start a search on its own. It holds exactly the fields listed in `key`, with safe integers or strings of 1 to 64 characters from letters, digits, `_ . : -`. Without it a search must come from the game\'s own `c.room.match()` call.\n\nThe CLI prints every manifest error in one run. Fix every listed field and rule before retrying.\n\n`requires` is also available to the game itself through `c.device` in the kit, so the game can show its own warning or pick a lighter renderer. The portal does not gate the Play link on it.\n\n## Game translations\n\nUse this convention for every new game, including games with only one language:\n\n1. Put the supported languages in `languages`, always including `en`, with the default first.\n2. Put game UI strings in `client/i18n/<lang>.json`, with canonical filenames such as `en.json`, `it.json` and `pt-BR.json`. Dictionaries are flat objects with identical keys and string values, including placeholders such as `{n}`.\n3. Call `const t = await c.text()` after `caisual.connect()` and before `c.session.ready()`. Render with `t(\'score\', { n: 3 })`. Use `c.player.language` when formatting dates or numbers.\n4. Translate the mode labels and instructions in the manifest. Role and leaderboard labels support the same objects. `name`, `description` and `tags` are not localized fields.\n\nFor example, a mode can contain:\n\n```json\n{\n "id": "solo",\n "execution": "local",\n "label": { "en": "Solo", "it": "Da solo" },\n "instructions": { "en": "Light up three lights.", "it": "Accendi tre luci." }\n}\n```\n\nA dictionary at `client/i18n/en.json` can contain:\n\n```json\n{ "score": "Lights: {n} / 3", "done": "All lit up!" }\n```\n\nSee [Game language and strings](./kit.md#game-language-and-strings) for a complete manifest, two dictionaries and a working client.\n\nThe kit makes one request to the game\'s own origin. The kit first resolves the player\'s ordered preferences against declared game languages, using exact tags, parent tags, then the game\'s default. `c.player.language` is that declared game language; `c.player.uiLanguage` is the overlay\'s locale. Caisual resolves each text key from the selected game language, its parent language tags, then the manifest\'s default language, then the key itself. For `pt-BR` with English as default, that is `pt-BR`, `pt`, `en`, key. The same chain selects localized manifest text from `uiLanguage` in the overlay; a missing label falls back to its id and missing instructions are omitted. A string continues to appear as written. Translation objects must be non-empty, contain valid language tags and satisfy the original text limits for every value. Empty strings are allowed in game dictionaries, but not in manifest labels or instructions.\n\n`caisual dev` at startup and `caisual publish` before any upload check `client/i18n/`. No folder is required for an existing game. If the folder exists, the default language file must exist as a regular file or the command fails. All other dictionary issues produce warnings: invalid JSON, non-string values, non-canonical filenames, missing dictionaries for declared languages, and differing keys. Missing-key warnings compare the union of keys across every usable file, including keys absent from the default. Invalid dictionaries are ignored at runtime. Fix the warnings before sharing the game; they do not block development or publication. Restart dev after changing the manifest to reload its language list and repeat the checks.\n\nDeclare `languages: ["it", "en"]` to keep Italian as the default. Add English game strings and translate the manifest labels and instructions too. Declaring English does not create translations. A manifest without `languages`, or without `en`, is rejected.\n\n## client/index.html\n\n`index.html` must be at the root of `client/`. Use relative URLs such as `./game.js` or `assets/sprite.png`. Do not use root-relative URLs such as `/game.js`, and do not use parent paths that leave the published `client/` tree.\n\nTo use player identity, saves, and leaderboards, import the kit from `/__caisual/kit/v1.js` as shown in [kit.md](./kit.md). The path `/__caisual/` is reserved: do not put game files under it.\n\nA standard game fills the window: `html`, `body` and the game surface are 100% of the viewport, with no maximum width, no header, no footer and no editorial frame, and the document must not scroll at 1366x768 or at 390x844 with safe areas applied. Aim for the playable field to cover at least 70% of the visible area on desktop and 60% on mobile; a board with a fixed aspect ratio uses the geometric exception described in [kit.md](./kit.md#full-screen).\n\nCaisual draws its own controls on top: a pill in the top-right corner, about 44 pixels tall and wider when it carries an invitation, and a compact bar at the end of a match. Exit lives in that pill. The exact positions arrive in the game as `reservedRects` on `c.overlay.onChange`, in CSS pixels of the game viewport, so place the game\'s own HUD outside them rather than guessing a corner. While a panel is open `inputBlocked` is `true`: release held keys and stop reading input, but keep simulating, because a panel never pauses a room.\n\nA game published without `overlay` keeps the historical control instead: a small round Exit button over the top-right corner, 36 pixels, inside the safe area. Keep that corner free of controls.\n\nDo not register a service worker. The game runs in an iframe on its own origin inside `caisual.com`. Test it without assuming access to the parent page, parent cookies, or files outside `client/`.\n\nWhen `voice` is not `none`, the portal grants microphone access to the game iframe. The browser still asks the player for permission when the game calls `room.voice.join()`. Call it from a button click or another user gesture, not automatically when the page loads.\n\n## Multiplayer server\n\nAdd `server.js` beside `caisual.json` when the game uses rooms. It is the ESM entry point and must have an `export default`. It may import local files such as `./logic/ships.js`, including `.js`, `.ts`, and `.json` files, and npm packages installed in the game folder. The CLI bundles these imports into one file both when publishing and when starting `caisual dev`.\n\nA minimal relay server looks like this:\n\n```js\nimport { defineGame } from \'@caisual/kit/server\';\n\nexport default defineGame({\n tickRate: 0,\n onMessage(room, player, message) {\n room.broadcast(message);\n },\n});\n```\n\n`tickRate` is required and must be an integer from 0 to 60; `defineGame` throws without it. Use `0` for a server that runs only in response to events. Every callback is optional.\n\nThe file may define the optional room callbacks documented in [kit.md](./kit.md). Server code runs without Node.js APIs or network access. Dynamic `import()`, `require()`, and CommonJS exports are not supported. Use only pure JavaScript packages, such as a noise or vector library. A package that needs an HTTP client is not suitable. The `network` field in `caisual.json` controls only requests made by the browser client.\n\nThe bundled `server.js` may be at most 1,000,000 bytes. Room state must remain plain JSON and may be at most 256 KB when serialized. Each incoming game message may be at most 16 KB. Game messages are limited to 20 per second per connection. Excess messages are dropped; `room.onError` receives `rate_limited` at most once per second across both budgets. Protocol service messages, including ping, lobby actions, state requests and voice signaling, have a separate 20/s budget with the same drop policy. More than 100 attempts in each of three consecutive one-second windows in either budget closes the connection with 4008 `rate_limited`; the kit does not reconnect automatically after this abuse closure. Abuse windows start with the first message on the connection; a normal or empty window resets the sequence. Oversized game frames close with 4009 `message_too_large`. Room save values may be at most 128 KB.\n\nPublish a multiplayer game with the same `npx @caisual/cli publish` command. When imports need bundling, the CLI prints `Bundling server.js (N KB).` The uploaded file is the bundle: the CLI validates it, declares its size and SHA-256 digest, and uploads it separately from browser files. The portal validates the stored bundle again before making the new game version current.\n\nIf the portal finds an invalid `server.js`, the command prints `The multiplayer server could not be published.` followed by diagnostic hints. The failed version is kept for diagnosis but never becomes current. If the game already has a working version, players continue to receive that version. Fix the reported problem and publish again to create a new version.\n\n## Test locally\n\nRun the local preview from the game folder before publishing:\n\n```sh\nnpx @caisual/cli dev\n```\n\nYou can pass a game folder and choose another port:\n\n```sh\nnpx @caisual/cli dev ./my-game --port 8790 --day 2026-09-04\n```\n\nThe optional `--day YYYY-MM-DD` flag pins the UTC date for daily seeds and local daily leaderboards, including room scores. Invalid dates are usage errors; omitting the flag uses today in UTC. Scores persist by day in `.caisual-dev/`, so restarting with another date switches boards without erasing earlier scores. Saves, identities and rooms stay shared; queued room scores keep their assigned day across restarts. Room daily contexts stay fixed at creation, including on restoration; new rooms follow the simulated day. Daily `expiresAt` stays on the next real UTC midnight, so it can be compared with the unchanged server clock. Reload the game after restarting dev. Real clocks and timers are unchanged.\n\nThe command prints a portal URL and a game URL. Open the portal URL. It loads the game in an iframe with the same handshake used after publishing, so `c.connected` is `true`, and it mounts the same standard overlay when the manifest declares one. Player identity, saves, leaderboards, daily data, invitations, and rooms all use local data. Add `?lang=` with any game language to test the manifest resolution, including regional tags such as `pt-BR`. For `?lang=ja` with Japanese declared, `c.player.language` is `ja` and `c.player.uiLanguage` is `en`: the overlay supports en, it, es, fr, de and pt. Without the parameter, the game uses the browser\'s ordered preferences. Friends and parties are marked unavailable locally. Each new browser tab gets a different guest identity, while reloading one tab keeps that tab\'s identity.\n\nOpen `/__caisual/players?n=4` on the printed portal URL for independent guest frames with the full overlay. Each has Drop and Spectate controls. Use `dev --latency 120 --jitter 40 --loss 2` to delay room WebSocket messages in each direction, vary the delay and discard 2% of messages for recovery testing. Latency and jitter accept integer milliseconds from 0 to 60000, loss accepts 0 to 100 percent; jitter and loss require latency. HTTP, matchmaking and audio are unaffected. See [Local dev](/docs/local-development) for precise semantics and frame sizes.\n\nReload after client edits; restart dev after editing `caisual.json`, `server.js`, or server imports, including shared physics. Rooms restore from `.caisual-dev/`, so incompatible state changes may need a new room. There is no automatic reload. If a port is busy, dev suggests a command using a currently available port.\n\nWhen `server.js` exists, room data is stored as JSON under `.caisual-dev/` in the game folder. Without `server.js`, the game remains single player and attempts to create a room return `no_server`.\n\nPress Ctrl+C in the terminal to stop the preview. No account or publish key is required.\n\n## Limits\n\n- At most 2,000 files per version.\n- At most 50,000,000 bytes per file.\n- At most 200,000,000 bytes for all files in one version.\n- At most 1,000,000 bytes for `server.js`.\n- At most 60 versions per publishing key in any 24-hour window. Beyond that the portal answers `publish_rate_limit`.\n- Dotfiles, dot-directories, and directories named `node_modules` are ignored.\n- Symbolic links and other non-regular files are rejected.\n\nReduce or split files that exceed the per-file limit. Remove generated files that the browser does not need.\n\n## Publish\n\nUse the key supplied by the creator. Set it in the environment so it does not enter shell history as a command-line flag:\n\n```sh\nnpx @caisual/cli check\nexport CAISUAL_KEY=\'ck_...\'\nnpx @caisual/cli publish\n```\n\nRun the command from the game folder, or pass the folder path after `publish`. For local portal development only, set `CAISUAL_ORIGIN` to the local HTTP origin.\n\nThe CLI validates the folder, computes every file size and SHA-256 digest, creates a new version, uploads the files, completes the version, and prints the game URL. The stable URL is `https://caisual.com/g/<id>`.\n\nBefore contacting the portal, the CLI scans browser files for common WebGL2, WebGPU, WebAssembly, and shared-memory signatures. A possible mismatch is printed to stderr with a `Warning:` prefix and never blocks publishing. Correct an accurate warning by declaring the minimum matching `requires` field, and set `isolated: true` for shared memory. If the signature belongs to unused code, remove that code from the published client bundle.\n\nThe first games from a new creator are reviewed before they can appear in the public catalog. Their stable links still work while review is pending.\n\n## Update, unlist, or delete\n\nTo update a game, change its files without changing `id`, then run `npx @caisual/cli publish` again. This creates a new version and keeps the same stable game URL.\n\nTo remove the current game from the catalog without publishing a new version, run:\n\n```sh\nCAISUAL_KEY=\'ck_...\' npx @caisual/cli unlist\n```\n\nRestore its public visibility with:\n\n```sh\nCAISUAL_KEY=\'ck_...\' npx @caisual/cli relist\n```\n\nDelete it permanently only when you are certain:\n\n```sh\nCAISUAL_KEY=\'ck_...\' npx @caisual/cli delete --yes\n```\n\nEach command reads the `id` from `caisual.json` in the current folder. You may instead pass a game folder or an ID directly, for example `npx @caisual/cli unlist ./my-game` or `npx @caisual/cli relist my-game`. The publishing key always comes from `CAISUAL_KEY`, never from a flag. Deletion has no interactive prompt, is permanent, removes the stored game files, and never frees the ID for reuse.\n\n## Common errors\n\n- `CAISUAL_KEY is required`: export the creator\'s key in the same shell before publishing or managing a game.\n- `The publish API key is not valid`: create a new key in the account dashboard if the old key expired or was revoked.\n- `game_not_found`: check that the game ID is correct and belongs to the creator represented by `CAISUAL_KEY`; deleted games return the same error.\n- `caisual.json is not valid`: read every reported field and rule, fix all of them, then retry.\n- `client/index.html: file not found`: place `index.html` directly under `client/`, not in a nested build folder.\n- `referenced file not found`: make sure `cover`, `card`, `icon` and every screenshot path match a file under `client/`, including letter case.\n- `file is larger than 50 MB`: compress, reduce, or split the asset and update its references.\n- `upload failed` or a temporary portal error: keep the files unchanged and retry the same publish command. The CLI retries temporary upload failures automatically.\n- `publish_rate_limit`: this key has already created 60 versions in the last 24 hours. Wait until the oldest one leaves the window.\n- `burst_rate_limit`: too many publishing or management requests arrived at once. Wait briefly and retry.\n- `The multiplayer server could not be published`: read every diagnostic hint, fix `server.js`, and publish again. The failed version does not replace the current one.\n- An external browser request works locally but fails after publishing: add its host to `network` and publish a new version. Server code cannot make outbound network requests.\n- A threaded WebAssembly game fails to start: set `isolated` to `true` and verify that every declared external host supports cross-origin isolation.\n\n\n## Required game images\n\nEvery manifest must declare three different files inside `client/`. All three are required PNG, JPEG or WebP images, at most 2 MB (2,000,000 bytes) each:\n\n| Field | Exact size | Use |\n| --- | --- | --- |\n| `cover` | 1536x1024, 3:2 | Featured home card, game page and overlay boot background, social preview, mobile invitation |\n| `card` | 1024x1024, 1:1 | Secondary home cards and square invitation artwork |\n| `icon` | 1024x1024, 1:1 | Game window favicon, overlay, friends and parties |\n\n**No text inside any image:** no title, slogan, letters or numbers. The portal displays the game name. Keep the icon simple enough to read at a small size. The files must be different, including `card` and `icon`. Screenshots keep their existing rules.\n\nThe 3:2 and 1:1 ratios match native formats of widely used image generators. Export at the exact sizes above: Caisual does not crop, stretch or accept a size range. `caisual check` and publish validate the format from the file headers, exact dimensions and weight. The portal repeats these checks for direct publication. The absence of text is an editorial requirement, not an automatic check.\n\nAll three `caisual init` variants include small, real PNG examples at these sizes. Replace them with artwork for your game before sharing it. English (`en`) is always required in `languages`; the first language remains the default, for example `["it", "en"]`.\n';
1049
+ var publish_default = '# Publish a game on Caisual\n\nCaisual hosts browser games supplied as folders. Each publish creates an immutable version. It becomes current unless another version was activated after the upload opened.\nThe publishing flow supports both single-player and multiplayer games and does not require changes in the Caisual dashboard. Player identity, rooms, cloud saves, leaderboards, and the daily challenge come from the game kit, documented in [kit.md](./kit.md).\n\nA game published with `"overlay": { "version": 1 }` is a standard game: it runs full screen and Caisual draws the menu, the lobby, invitations, friends, matchmaking, spectators, leaderboards, voice, the end of a match and Play again on top of it. Write the field, the HUD and the settings; declare the rest in the manifest. See [Sessions and the standard overlay](./kit.md#sessions-and-the-standard-overlay).\n\n## Game folder\n\nUse this structure:\n\n```text\nmy-game/\n caisual.json\n server.js # optional, required only for multiplayer rooms\n client/\n index.html\n i18n/\n en.json\n it.json\n ...\n```\n\n`caisual.json` and `client/index.html` are required. Put every file used by the game under `client/`.\n\nRun `npx @caisual/cli init my-game` to create a minimal single-player folder with the standard overlay and one local mode. Run `npx @caisual/cli init --multiplayer my-game` to add a room mode with matchmaking and a `server.js`. Both templates include `client/i18n/en.json` used by the example client through `const t = await c.text()`. They are full screen and use `c.session` and `c.overlay`; neither draws a menu or a lobby of its own.\n\nThe whole CLI is:\n\n```text\ncaisual init [--multiplayer | --arcade] [folder]\ncaisual dev [folder] [--port 8790] [--day YYYY-MM-DD] [--latency ms [--jitter ms] [--loss percent]]\ncaisual check [folder] [--json]\ncaisual publish [folder]\ncaisual versions [folder|id]\ncaisual rollback [folder|id] --to <n>\ncaisual unlist [folder|id]\ncaisual relist [folder|id]\ncaisual delete [folder|id] --yes\ncaisual skill\ncaisual --help\ncaisual --version\n```\n\n`caisual init --arcade my-arena` creates a complete online action starter in English and Italian. It includes a shared fixed-step simulation, an authoritative `server.js`, numbered and acknowledged controls, local prediction with reconciliation, remote interpolation, safe HUD placement, standings and fast rematches. All `init` variants also generate `package.json` with `dev`, `test` and optional `test:browser` scripts. See [Responsive action games](./kit.md#responsive-action-games) and [Testing with a browser](/docs/local-development#testing-with-a-browser).\n\n`caisual check [folder] [--json]` runs every local check used by publish: the manifest, game texts, client files, the `server.js` bundle, the three required images, and screenshots. It needs no key and uploads nothing. It validates files without launching or playing the game in a browser. With `--json` it prints a report for tools and agents. It exits with code 2 when the report has errors.\n\n`caisual skill` writes this guide and [kit.md](./kit.md) into `.claude/skills/caisual/SKILL.md` in the current folder and adds a `## Caisual` section to `AGENTS.md`, so an agent working in that repository reads the rules before it starts.\n\n## caisual.json\n\nThe file must contain one JSON object. Unknown fields are rejected. This is a complete single-player example:\n\n```json\n{\n "manifest": 1,\n "id": "my-game",\n "name": "My Game",\n "description": "A short description of the game.",\n "cover": "cover.png",\n "card": "card.png",\n "icon": "icon.png",\n "screenshots": ["screenshots/level-one.png"],\n "tags": ["puzzle"],\n "languages": ["en", "it"],\n "platform": "both",\n "overlay": { "version": 1, "accent": "#397e83" },\n "orientation": "landscape",\n "input": ["keyboard", "mouse", "touch"],\n "visibility": "public",\n "network": [],\n "isolated": false,\n "requires": { "webgl2": false, "webgpu": false, "wasm": false, "threads": false, "memoryMb": null, "performance": "light" },\n "players": { "min": 1, "max": 1 },\n "lobby": false,\n "persistent": false,\n "spectators": true,\n "boards": { "main": { "source": "client", "label": "Best run", "periods": ["daily", "all-time"] } },\n "roles": [],\n "teams": null,\n "voice": "none",\n "modes": [\n { "id": "solo", "execution": "local", "label": "Solo", "instructions": "One run against the clock." }\n ]\n}\n```\n\n- `manifest` is required and must be `1`.\n- `overlay` is optional and defaults to absent. Set `{ "version": 1 }` to publish a standard game and get the whole overlay. `accent` is optional and must be a six-digit `#RRGGBB` colour; no other CSS is accepted. A game without `overlay` keeps its historical flow and draws its own menus, and nothing in this guide changes for it.\n- `id` is required. Use 3 to 32 lowercase ASCII letters or digits, with single hyphens only between groups. The ID becomes the URL slug. Choose it carefully because it cannot be renamed or reused after deletion.\n- `name` is required and must contain 1 to 60 characters.\n- `description` is optional, defaults to an empty string, and can contain at most 500 characters.\n- `cover`, `card` and `icon` are required, distinct relative file paths inside `client/`. Use PNG, JPEG or WebP, at most 2 MB (2,000,000 bytes) per image. Do not include a query, fragment, empty segment or parent segment. See the exact dimensions below.\n- `screenshots` is optional and defaults to `[]`. It accepts up to 8 relative paths inside `client/`.\n- `tags` is optional and defaults to `[]`. It accepts up to 10 values. Each value uses 1 to 24 lowercase letters, digits, or hyphens.\n- `languages` is required: a non-empty array of distinct BCP 47 tags that includes `en`, such as `["it", "en", "pt-BR"]`. English is always required alongside the game\'s own languages. The first entry remains the default and may be a language other than English. Tags are normalized to canonical casing. The catalog and standard menu show the available languages.\n- `platform` is required. Use `desktop` when the game needs a keyboard, mouse, large display, or desktop performance. Use `mobile` when it is designed only for touch and small screens. Use `both` only after checking that layout, performance, and controls work on both.\n- `orientation` is optional and defaults to `landscape`. Use `landscape` or `portrait` to describe the intended mobile layout. The device may not honor an orientation request.\n- `input` is optional and defaults to `[]`. Include every supported input from `keyboard`, `mouse`, `touch`, and `gamepad`. Do not claim an input until the game is usable with it.\n- `visibility` is optional and defaults to `public`. Use `public` for catalog eligibility or `unlisted` for access by direct link only.\n- `network` is optional and defaults to `[]`. List every external host contacted or loaded by the game, without scheme, port, path, query, or fragment, for example `api.example.com`. If an external host is missing, the browser blocks the request. Keep the array empty when the game uses only its own files and Caisual services.\n- `isolated` is optional and defaults to `false`. Use `true` only when the game requires shared memory or threaded WebAssembly. Every external host in `network` must then send headers compatible with cross-origin isolation.\n- `requires` is optional and defaults to `{ "webgl2": false, "webgpu": false, "wasm": false, "threads": false, "memoryMb": null, "performance": "light" }`. Declare the minimum capabilities the game truly needs to run. For example, a game with a WebGPU renderer and a WebGL2 fallback declares only `webgl2`. Set `threads` together with `isolated: true`. `memoryMb` accepts `null` or a multiple of 256 from 512 to 32768. Use `light`, `medium`, or `heavy` for the expected performance load.\n- `players` is optional and defaults to `{ "min": 1, "max": 1 }`. Both values are integers from 1 to 24 and `max` must be at least `min`. Set the range that a room needs before play can start.\n- `lobby` is optional and defaults to `false`. Use `true` when players must choose roles or teams, mark themselves ready, and wait for the host to start. With `false`, play starts when the first player enters and later players may join in progress. A room mode with resolved `players.max === 1` always starts on entry and bypasses the lobby, including an inherited `lobby: true`.\n- `persistent` is optional and defaults to `false`. Use `true` when room members must be able to return with the same code after disconnecting, including while the game is already playing. Persistent rooms expire after 30 days without activity.\n- `spectators` is optional and defaults to `{ "delayMs": 3000 }`. Use `false` to disable watching, `true` for the default three-second delay, or `{ "delayMs": N }` to choose an integer delay from 0 to 30000 milliseconds.\n- `boards` is optional and defaults to `{}`. Each key is a leaderboard id. Use `{ "source": "server" }` to accept only `room.board.submit`, or `{ "source": "client" }` to allow browser submissions. Boards not listed use `client`. A manifest may list up to 32 boards. `label` is optional text or a language-to-text object, 1 to 48 characters on one line per translation, and names the board in the overlay; without it the overlay shows the id. `periods` is optional and defaults to `["all-time"]`: list `daily`, `all-time` or both, without duplicates. `all-time` means the best score with no day attached, not a sum of days. `periods` only chooses what the overlay offers; it does not change what the score APIs accept. `day` is `submit` by default, or `start` for a server board. With `start`, submit a daily score from the room server with `{ day: room.daily.day }` within 10 minutes of its next UTC midnight. See [Daily challenge](./kit.md#daily-challenge).\n- `roles` is optional and defaults to `[]`. Each entry has an `id` of 1 to 32 lowercase letters, digits, or internal hyphens, a `min` integer from 0 to 24, and an optional `max` in the same range. Rooms enforce these capacities in the lobby. `label` is optional text or a language-to-text object, 1 to 32 characters on one line per translation, and names the role in the overlay lobby; without it the overlay shows the id.\n- `teams` is optional and defaults to `null`. An object has `min` and `max` integers from 2 to 24, with `max` at least `min`. Rooms balance players who do not choose a team.\n- `voice` is optional and defaults to `none`. Use `room` so everyone in the room can hear each other, `team` to restrict voice to teammates, or `proximity` when `server.js` sets the gain between player pairs. Use `none` to disable voice.\n- `modes` is optional and defaults to `[]`. A mode has a unique `id` using 1 to 32 lowercase letters, digits, or internal hyphens. It may have `matchmaking` with `key`, an array of 1 to 8 unique field names, and `timeoutMs`, an integer from 1,000 to 300,000. Each field name uses 1 to 32 lowercase letters, digits, or hyphens and starts with a letter or digit. A mode may also define `players: { min, max }` (both integers from 1 to 24, max at least min) and `lobby` (boolean). Each supplied field replaces its root counterpart for creation, joining and matchmaking, including filling an open room; omitted fields inherit the root value. `players` is replaced as a whole, not merged. `mode: null` uses the root configuration. Roles, teams, voice and persistence remain game-wide. Catalog labels consider the resolved modes, or the root range when there are no modes: Single player, Multiplayer, or Solo + Multiplayer.\n- A standard game declares at least one mode, and every mode of a standard game needs `execution`: `local` for a run inside the browser, `room` for a room. A `local` mode resolves to exactly one player with `lobby` false and no matchmaking; it is not a room of one, and the create and match APIs refuse it. A `room` mode requires `server.js`, checked by the CLI and again when the version is published.\n- `label` is optional text or a language-to-text object, 1 to 48 characters on one line per translation, and names the mode in the standard menu; without it the menu shows the id. `instructions` is optional text or a language-to-text object, 1 to 160 characters on one line per translation, and adds a line under the label. Both are text, never HTML, and resolve to the player\'s language with the fallback described below.\n- `matchmaking.defaults` is required when the overlay is expected to start a search on its own. It holds exactly the fields listed in `key`, with safe integers or strings of 1 to 64 characters from letters, digits, `_ . : -`. Without it a search must come from the game\'s own `c.room.match()` call.\n\nThe CLI prints every manifest error in one run. Fix every listed field and rule before retrying.\n\n`requires` is also available to the game itself through `c.device` in the kit, so the game can show its own warning or pick a lighter renderer. The portal does not gate the Play link on it.\n\n## Game translations\n\nUse this convention for every new game, including games with only one language:\n\n1. Put the supported languages in `languages`, always including `en`, with the default first.\n2. Put game UI strings in `client/i18n/<lang>.json`, with canonical filenames such as `en.json`, `it.json` and `pt-BR.json`. Dictionaries are flat objects with identical keys and string values, including placeholders such as `{n}`.\n3. Call `const t = await c.text()` after `caisual.connect()` and before `c.session.ready()`. Render with `t(\'score\', { n: 3 })`. Use `c.player.language` when formatting dates or numbers.\n4. Translate the mode labels and instructions in the manifest. Role and leaderboard labels support the same objects. `name`, `description` and `tags` are not localized fields.\n\nFor example, a mode can contain:\n\n```json\n{\n "id": "solo",\n "execution": "local",\n "label": { "en": "Solo", "it": "Da solo" },\n "instructions": { "en": "Light up three lights.", "it": "Accendi tre luci." }\n}\n```\n\nA dictionary at `client/i18n/en.json` can contain:\n\n```json\n{ "score": "Lights: {n} / 3", "done": "All lit up!" }\n```\n\nSee [Game language and strings](./kit.md#game-language-and-strings) for a complete manifest, two dictionaries and a working client.\n\nThe kit makes one request to the game\'s own origin. The kit first resolves the player\'s ordered preferences against declared game languages, using exact tags, parent tags, then the game\'s default. `c.player.language` is that declared game language; `c.player.uiLanguage` is the overlay\'s locale. Caisual resolves each text key from the selected game language, its parent language tags, then the manifest\'s default language, then the key itself. For `pt-BR` with English as default, that is `pt-BR`, `pt`, `en`, key. The same chain selects localized manifest text from `uiLanguage` in the overlay; a missing label falls back to its id and missing instructions are omitted. A string continues to appear as written. Translation objects must be non-empty, contain valid language tags and satisfy the original text limits for every value. Empty strings are allowed in game dictionaries, but not in manifest labels or instructions.\n\n`caisual dev` at startup and `caisual publish` before any upload check `client/i18n/`. No folder is required for an existing game. If the folder exists, the default language file must exist as a regular file or the command fails. All other dictionary issues produce warnings: invalid JSON, non-string values, non-canonical filenames, missing dictionaries for declared languages, and differing keys. Missing-key warnings compare the union of keys across every usable file, including keys absent from the default. Invalid dictionaries are ignored at runtime. Fix the warnings before sharing the game; they do not block development or publication. Restart dev after changing the manifest to reload its language list and repeat the checks.\n\nDeclare `languages: ["it", "en"]` to keep Italian as the default. Add English game strings and translate the manifest labels and instructions too. Declaring English does not create translations. A manifest without `languages`, or without `en`, is rejected.\n\n## client/index.html\n\n`index.html` must be at the root of `client/`. Use relative URLs such as `./game.js` or `assets/sprite.png`. Do not use root-relative URLs such as `/game.js`, and do not use parent paths that leave the published `client/` tree.\n\nTo use player identity, saves, and leaderboards, import the kit from `/__caisual/kit/v1.js` as shown in [kit.md](./kit.md). The path `/__caisual/` is reserved: do not put game files under it.\n\nA standard game fills the window: `html`, `body` and the game surface are 100% of the viewport, with no maximum width, no header, no footer and no editorial frame, and the document must not scroll at 1366x768 or at 390x844 with safe areas applied. Aim for the playable field to cover at least 70% of the visible area on desktop and 60% on mobile; a board with a fixed aspect ratio uses the geometric exception described in [kit.md](./kit.md#full-screen).\n\nCaisual draws its own controls on top: a pill in the top-right corner, about 44 pixels tall and wider when it carries an invitation, and a compact bar at the end of a match. Exit lives in that pill. The exact positions arrive in the game as `reservedRects` on `c.overlay.onChange`, in CSS pixels of the game viewport, so place the game\'s own HUD outside them rather than guessing a corner. While a panel is open `inputBlocked` is `true`: release held keys and stop reading input, but keep simulating, because a panel never pauses a room.\n\nA game published without `overlay` keeps the historical control instead: a small round Exit button over the top-right corner, 36 pixels, inside the safe area. Keep that corner free of controls.\n\nDo not register a service worker. The game runs in an iframe on its own origin inside `caisual.com`. Test it without assuming access to the parent page, parent cookies, or files outside `client/`.\n\nWhen `voice` is not `none`, the portal grants microphone access to the game iframe. The browser still asks the player for permission when the game calls `room.voice.join()`. Call it from a button click or another user gesture, not automatically when the page loads.\n\n## Multiplayer server\n\nAdd `server.js` beside `caisual.json` when the game uses rooms. It is the ESM entry point and must have an `export default`. It may import local files such as `./logic/ships.js`, including `.js`, `.ts`, and `.json` files, and npm packages installed in the game folder. The CLI bundles these imports into one file both when publishing and when starting `caisual dev`.\n\nA minimal relay server looks like this:\n\n```js\nimport { defineGame } from \'@caisual/kit/server\';\n\nexport default defineGame({\n tickRate: 0,\n onMessage(room, player, message) {\n room.broadcast(message);\n },\n});\n```\n\n`tickRate` is required and must be an integer from 0 to 60; `defineGame` throws without it. Use `0` for a server that runs only in response to events. Every callback is optional.\n\nThe file may define the optional room callbacks documented in [kit.md](./kit.md). Server code runs without Node.js APIs or network access. Dynamic `import()`, `require()`, and CommonJS exports are not supported. Use only pure JavaScript packages, such as a noise or vector library. A package that needs an HTTP client is not suitable. The `network` field in `caisual.json` controls only requests made by the browser client.\n\nThe bundled `server.js` may be at most 1,000,000 bytes. Room state must remain plain JSON and may be at most 256 KB when serialized. Each incoming game message may be at most 16 KB. Game messages are limited to 20 per second per connection. Excess messages are dropped; `room.onError` receives `rate_limited` at most once per second across both budgets. Protocol service messages, including ping, lobby actions, state requests and voice signaling, have a separate 20/s budget with the same drop policy. More than 100 attempts in each of three consecutive one-second windows in either budget closes the connection with 4008 `rate_limited`; the kit does not reconnect automatically after this abuse closure. Abuse windows start with the first message on the connection; a normal or empty window resets the sequence. Oversized game frames close with 4009 `message_too_large`. Room save values may be at most 128 KB.\n\nPublish a multiplayer game with the same `npx @caisual/cli publish` command. When imports need bundling, the CLI prints `Bundling server.js (N KB).` The uploaded file is the bundle: the CLI validates it, declares its size and SHA-256 digest, and uploads it separately from browser files. The portal validates the stored bundle again before making the new game version current.\n\nIf the portal finds an invalid `server.js`, the command prints `The multiplayer server could not be published.` followed by diagnostic hints. The failed version is kept for diagnosis but never becomes current. If the game already has a working version, players continue to receive that version. Fix the reported problem and publish again to create a new version.\n\n## Test locally\n\nRun the local preview from the game folder before publishing:\n\n```sh\nnpx @caisual/cli dev\n```\n\nYou can pass a game folder and choose another port:\n\n```sh\nnpx @caisual/cli dev ./my-game --port 8790 --day 2026-09-04\n```\n\nThe optional `--day YYYY-MM-DD` flag pins the UTC date for daily seeds and local daily leaderboards, including room scores. Invalid dates are usage errors; omitting the flag uses today in UTC. Scores persist by day in `.caisual-dev/`, so restarting with another date switches boards without erasing earlier scores. Saves, identities and rooms stay shared; queued room scores keep their assigned day across restarts. Room daily contexts stay fixed at creation, including on restoration; new rooms follow the simulated day. Daily `expiresAt` stays on the next real UTC midnight, so it can be compared with the unchanged server clock. Reload the game after restarting dev. Real clocks and timers are unchanged.\n\nThe command prints a portal URL and a game URL. Open the portal URL. It loads the game in an iframe with the same handshake used after publishing, so `c.connected` is `true`, and it mounts the same standard overlay when the manifest declares one. Player identity, saves, leaderboards, daily data, invitations, and rooms all use local data. Add `?lang=` with any game language to test the manifest resolution, including regional tags such as `pt-BR`. For `?lang=ja` with Japanese declared, `c.player.language` is `ja` and `c.player.uiLanguage` is `en`: the overlay supports en, it, es, fr, de and pt. Without the parameter, the game uses the browser\'s ordered preferences. Friends and parties are marked unavailable locally. Each new browser tab gets a different guest identity, while reloading one tab keeps that tab\'s identity.\n\nOpen `/__caisual/players?n=4` on the printed portal URL for independent guest frames with the full overlay. Each has Drop and Spectate controls. Use `dev --latency 120 --jitter 40 --loss 2` to delay room WebSocket messages in each direction, vary the delay and discard 2% of messages for recovery testing. Latency and jitter accept integer milliseconds from 0 to 60000, loss accepts 0 to 100 percent; jitter and loss require latency. HTTP, matchmaking and audio are unaffected. See [Local dev](/docs/local-development) for precise semantics and frame sizes.\n\nReload after client edits; restart dev after editing `caisual.json`, `server.js`, or server imports, including shared physics. Rooms restore from `.caisual-dev/`, so incompatible state changes may need a new room. There is no automatic reload. If a port is busy, dev suggests a command using a currently available port.\n\nWhen `server.js` exists, room data is stored as JSON under `.caisual-dev/` in the game folder. Without `server.js`, the game remains single player and attempts to create a room return `no_server`.\n\nPress Ctrl+C in the terminal to stop the preview. No account or publish key is required.\n\n## Limits\n\n- At most 2,000 files per version.\n- At most 50,000,000 bytes per file.\n- At most 200,000,000 bytes for all files in one version.\n- At most 1,000,000 bytes for `server.js`.\n- At most 60 versions per publishing key in any 24-hour window. Beyond that the portal answers `publish_rate_limit`.\n- Dotfiles, dot-directories, and directories named `node_modules` are ignored.\n- Symbolic links and other non-regular files are rejected.\n\nReduce or split files that exceed the per-file limit. Remove generated files that the browser does not need.\n\n## Publish\n\nUse the key supplied by the creator. Set it in the environment so it does not enter shell history as a command-line flag:\n\n```sh\nnpx @caisual/cli check\nexport CAISUAL_KEY=\'ck_...\'\nnpx @caisual/cli publish\n```\n\nRun the command from the game folder, or pass the folder path after `publish`. For local portal development only, set `CAISUAL_ORIGIN` to the local HTTP origin.\n\nThe CLI validates the folder, computes every file size and SHA-256 digest, creates a new version, uploads the files, completes the version, and prints the game URL. The stable URL is `https://caisual.com/g/<id>`.\n\nBefore contacting the portal, the CLI scans browser files for common WebGL2, WebGPU, WebAssembly, and shared-memory signatures. A possible mismatch is printed to stderr with a `Warning:` prefix and never blocks publishing. Correct an accurate warning by declaring the minimum matching `requires` field, and set `isolated: true` for shared memory. If the signature belongs to unused code, remove that code from the published client bundle.\n\nThe first games from a new creator are reviewed before they can appear in the public catalog. Their stable links still work while review is pending.\n\n## Versions and rollback\n\nNew games start on the current version. An existing room stays on its own version for its entire life, including players arriving later through an invitation, a typed code, friends, spectators or Resume. Its client, manifest, overlay, iframe permissions and images all come from that version.\n\nEvery `publish` creates a new immutable version, even for a tiny correction. The permanent address stays `/g/<id>`. The home page sorts games by the creation date of the current version; publishing can move a game up and a rollback can move it down.\n\n## Updates while a page is open\n\nCreating a room or starting a matchmaking search from an outdated page fails with `version_outdated` and `currentVersion`. The standard overlay shows **This game was updated** and a **Reload game** button that reloads the portal page on the current version.\n\nJoining or watching an existing room from another version fails with `version_mismatch` and `roomVersion`. For a typed code or Resume, the standard overlay reloads the portal with the invitation so it can load the room\'s version. Spectators keep their watch intent. A room whose version is no longer ready, or whose game was deleted, shows **This room is no longer available**, with a link to the current game.\n\nMatchmaking keeps versions separate. A reservation already accepted before an update can finish on its original version. Existing rooms are not restarted by an update or rollback.\n\nGames with their own menus receive these errors both as rejected room operations and through `c.room.onError(listener)`. Call `c.room.reload()` after either error to load the current game or the room referenced by the failed join or watch:\n\n```js\nc.room.onError((error) => {\n if (error.code === \'version_outdated\' || error.code === \'version_mismatch\') {\n showReloadButton(() => c.room.reload());\n }\n});\n```\n\n## List and restore versions\n\nUse the same `CAISUAL_KEY` environment variable as for publishing:\n\n```sh\ncaisual versions\ncaisual versions ./my-game\ncaisual rollback --to 7\ncaisual rollback my-game --to 7\n```\n\nThe target defaults to the current folder, or accepts a folder or game ID. `versions` lists the number, date, state and size and marks the current version. The account page also marks it.\n\nRollback selects an existing ready version of that game. It copies no files, creates no new number and does not consume the 60-version daily allowance. Visibility and moderation stay unchanged. **Rollback restores code, not saved data or scores.**\n\nIf another version was activated while a publish was uploading, the upload becomes ready but does not replace it. The CLI reports:\n\n```text\nVersion N is ready but not current: version M was activated in the meantime. Run caisual rollback --to N to activate it.\n```\n\nA manifest\'s visibility takes effect only when activation succeeds. An `unlist` or `relist` issued during an upload wins over that upload\'s manifest visibility.\n\n## Game versions and data formats\n\nSaves, leaderboards and `room.shared` belong to the game, not to a game version. The creator is responsible for data compatibility. A game version and a data format version are separate things.\n\nFor a compatible change, keep the same keys. For an incompatible change, use a new key such as `progress_v2`. Import from `progress` only when `progress_v2` is missing. Keep the original, identify the format inside each value and never overwrite a format you do not recognize. Use the same convention for `room.shared`. Progress written later by an old version is not automatically merged into the new key.\n\nFor leaderboards, keep the same ID while results remain comparable. Use a new ID such as `main_s2` when rules, scoring scale or trust rules change. Old rooms continue writing to their old board. Keep each board ID\'s rules consistent across game versions. Scores record the originating game version for diagnostics; this does not automatically split the leaderboard.\n\nThe daily seed does not depend on the game version. Do not change the generator or rules halfway through a UTC day without changing the board ID.\n\n## Retention and local development\n\nGood versions stay. Old ready versions are retained for rooms and rollback, without automatic age-based deletion. Failed uploads are cleaned from both client and server storage; uploads left open for more than 24 hours fail with a note and are cleaned. Version numbers are never reused. Deleting a game removes its client and server files.\n\n`caisual dev` always uses game version 1. It does not simulate publishing, version changes or rollback.\n\n## Update, unlist, or delete\n\nTo update a game, change its files without changing `id`, then run `npx @caisual/cli publish` again. This creates a new version and keeps the same stable game URL.\n\nTo remove the current game from the catalog without publishing a new version, run:\n\n```sh\nCAISUAL_KEY=\'ck_...\' npx @caisual/cli unlist\n```\n\nRestore its public visibility with:\n\n```sh\nCAISUAL_KEY=\'ck_...\' npx @caisual/cli relist\n```\n\nDelete it permanently only when you are certain:\n\n```sh\nCAISUAL_KEY=\'ck_...\' npx @caisual/cli delete --yes\n```\n\nEach command reads the `id` from `caisual.json` in the current folder. You may instead pass a game folder or an ID directly, for example `npx @caisual/cli unlist ./my-game` or `npx @caisual/cli relist my-game`. The publishing key always comes from `CAISUAL_KEY`, never from a flag. Deletion has no interactive prompt, is permanent, removes the stored game files, and never frees the ID for reuse.\n\n## Common errors\n\n- `CAISUAL_KEY is required`: export the creator\'s key in the same shell before publishing or managing a game.\n- `The publish API key is not valid`: create a new key in the account dashboard if the old key expired or was revoked.\n- `game_not_found`: check that the game ID is correct and belongs to the creator represented by `CAISUAL_KEY`; deleted games return the same error.\n- `caisual.json is not valid`: read every reported field and rule, fix all of them, then retry.\n- `client/index.html: file not found`: place `index.html` directly under `client/`, not in a nested build folder.\n- `referenced file not found`: make sure `cover`, `card`, `icon` and every screenshot path match a file under `client/`, including letter case.\n- `file is larger than 50 MB`: compress, reduce, or split the asset and update its references.\n- `upload failed` or a temporary portal error: keep the files unchanged and retry the same publish command. The CLI retries temporary upload failures automatically.\n- `publish_rate_limit`: this key has already created 60 versions in the last 24 hours. Wait until the oldest one leaves the window.\n- `burst_rate_limit`: too many publishing or management requests arrived at once. Wait briefly and retry.\n- `The multiplayer server could not be published`: read every diagnostic hint, fix `server.js`, and publish again. The failed version does not replace the current one.\n- An external browser request works locally but fails after publishing: add its host to `network` and publish a new version. Server code cannot make outbound network requests.\n- A threaded WebAssembly game fails to start: set `isolated` to `true` and verify that every declared external host supports cross-origin isolation.\n\n\n## Required game images\n\nEvery manifest must declare three different files inside `client/`. All three are required PNG, JPEG or WebP images, at most 2 MB (2,000,000 bytes) each:\n\n| Field | Exact size | Use |\n| --- | --- | --- |\n| `cover` | 1536x1024, 3:2 | Featured home card, game page and overlay boot background, social preview, mobile invitation |\n| `card` | 1024x1024, 1:1 | Secondary home cards and square invitation artwork |\n| `icon` | 1024x1024, 1:1 | Game window favicon, overlay, friends and parties |\n\n**No text inside any image:** no title, slogan, letters or numbers. The portal displays the game name. Keep the icon simple enough to read at a small size. The files must be different, including `card` and `icon`. Screenshots keep their existing rules.\n\nThe 3:2 and 1:1 ratios match native formats of widely used image generators. Export at the exact sizes above: Caisual does not crop, stretch or accept a size range. `caisual check` and publish validate the format from the file headers, exact dimensions and weight. The portal repeats these checks for direct publication. The absence of text is an editorial requirement, not an automatic check.\n\nAll three `caisual init` variants include small, real PNG examples at these sizes. Replace them with artwork for your game before sharing it. English (`en`) is always required in `languages`; the first language remains the default, for example `["it", "en"]`.\n';
1050
1050
 
1051
1051
  // ../../docs/kit.md
1052
- var kit_default = '# Caisual game kit\n\nThe kit gives a published game a stable player identity, cloud saves, leaderboards, a daily challenge seed, and multiplayer rooms with server-owned state.\n\nThe kit is a single JavaScript module with no dependencies. It never touches the page: the game keeps its own rendering, input, and UI.\n\n## Load the kit\n\nEvery published game can import the kit from its own origin, without a bundler and without an npm install:\n\n```html\n<script type="module">\n import { caisual } from \'/__caisual/kit/v1.js\';\n\n const c = await caisual.connect();\n console.log(c.player.name);\n</script>\n```\n\nGames built with a bundler can install the same module from npm:\n\n```sh\nnpm install @caisual/kit\n```\n\n```js\nimport { caisual } from \'@caisual/kit\';\n```\n\nBoth forms expose the same API. The module also sets `globalThis.caisual` for classic scripts that load it first.\n\nThe path `/__caisual/` is reserved on every game origin. Do not put game files under it.\n\n## Connect\n\n```js\nconst c = await caisual.connect();\n```\n\n`connect()` completes when the game is running inside caisual.com and has received its player identity, or after a short timeout when it is not. Calling it again returns the same promise.\n\n- `c.connected` is `true` inside caisual.com and `false` when the game runs on its own, for example from a local folder during development or when its files are copied elsewhere.\n- `c.player` is `{ id, name, guest, language, uiLanguage }`. `id` is stable for the player across sessions and across every version of the game. `name` is the account username, or a stable `Guest-XXXX` name derived from the player id. The four-character suffix uses `ABCDEFGHJKLMNPQRSTUVWXYZ23456789`, excluding I, O, 0 and 1; it helps distinguish guests but is not a unique identifier. `guest` is `true` for players without an account. `language` selects game strings; `uiLanguage` is the overlay locale. On first sign-in to an account without a player identity, the browser guest is adopted with its existing id, saves and scores. An account that already has a player identity uses that identity instead; guest data is not merged.\n- When not connected, `c.player` has `id: "local"`, `name: "Guest"`, `guest: true`, plus both language fields. If the host answered, its language information is kept; without a handshake, `language` is the normalized `navigator.language` (or `en`) and `uiLanguage` is its overlay fallback.\n\nDo not store the ticket or reimplement the handshake. The kit handles identity, renewal, and retries.\n\n## Game language and strings\n\n`c.player.language` is the game\'s language; `c.player.uiLanguage` is the overlay\'s locale. Use the former for game strings and formatting, or the latter when intentionally aligning text with the overlay.\n\nThe kit resolves the player\'s explicit portal language choice, otherwise `navigator.languages` in order, against `manifest.languages`. For each preference it tries the exact tag and then its parent tags; if none of the preferences match, it uses the game\'s first declared language. The result is always a normalized declared tag. For example, `ja-JP` with `languages: ["en", "ja"]` selects `ja`, while `uiLanguage` is `en`. The overlay supports English, Italian, Spanish, French, German and Portuguese; it keeps regional tags in those families, such as `pt-BR`, and falls back to `en` for other languages. Games may declare languages outside these six.\n\nA localized portal URL counts as a language choice. The language selector remembers explicit choices, including English; without either, the browser\'s ordered preferences apply. The handshake keeps its legacy `language` field for existing kits, and also sends `uiLanguage`, `languagePreferences` and `gameLanguages`. The kit resolves the game language, including when player services fail after a successful handshake.\n\nWithout a handshake, no manifest is available: `language` is the raw preference from `navigator.language`, normalized as a BCP 47 tag, or `en` if invalid or unavailable. It is not restricted to declared game languages. `uiLanguage` uses the overlay fallback. In `caisual dev`, `?lang=ja` selects `ja` when the manifest declares it, while the overlay stays in English. Without `?lang=`, dev uses `navigator.languages` in order for the game.\n\nPut all game UI strings in flat JSON dictionaries named `client/i18n/<lang>.json`. Use canonical BCP 47 filenames, for example `en.json`, `it.json`, `pt.json`, `pt-BR.json`. Every value is a string; keys are identical across dictionaries. Text can contain named placeholders such as `{n}`.\n\nDeclare supported languages in `caisual.json`, always including `en`. English is required alongside the game\'s own languages; the first entry is the default and may be another language. This complete manifest supports a local game:\n\n```json\n{\n "manifest": 1,\n "cover": "cover.png",\n "card": "card.png",\n "icon": "icon.png",\n "id": "three-lights",\n "name": "Three Lights",\n "platform": "both",\n "languages": ["en", "it"],\n "overlay": { "version": 1 },\n "modes": [{\n "id": "solo",\n "execution": "local",\n "label": { "en": "Solo", "it": "Da solo" },\n "instructions": { "en": "Light up three lights.", "it": "Accendi tre luci." }\n }]\n}\n```\n\n`client/i18n/en.json`:\n\n```json\n{ "score": "Lights: {n} / 3", "light": "Light up", "done": "All lit up!" }\n```\n\n`client/i18n/it.json`:\n\n```json\n{ "score": "Luci: {n} / 3", "light": "Accendi", "done": "Tutte accese!" }\n```\n\nLoad once during setup, before `c.session.ready()`. The returned function is synchronous and can be used in every draw call:\n\n```html\n<!doctype html>\n<html>\n<head><meta charset="utf-8"><title>Three Lights</title></head>\n<body style="margin:0;min-height:100dvh;display:grid;place-content:center">\n <p id="score"></p>\n <button id="light" disabled></button>\n <script type="module">\n import { caisual } from \'/__caisual/kit/v1.js\';\n const c = await caisual.connect();\n const t = await c.text();\n document.documentElement.lang = c.player.language;\n const score = document.querySelector(\'#score\');\n const light = document.querySelector(\'#light\');\n let n = 0, blocked = false, active = !c.session.capabilities.overlay;\n function draw() {\n score.textContent = n === 3 ? t(\'done\') : t(\'score\', { n });\n light.textContent = t(\'light\');\n light.disabled = blocked || !active || n === 3;\n }\n c.overlay.onChange((view) => { blocked = view.inputBlocked; draw(); });\n c.session.onChange((session) => {\n if (session.kind === \'local\' && session.status === \'playing\') n = 0;\n active = !c.session.capabilities.overlay || (session.kind === \'local\' && session.status === \'playing\');\n draw();\n });\n light.onclick = () => {\n n += 1;\n if (n === 3 && c.session.current.kind === \'local\') c.session.finish();\n draw();\n };\n draw();\n c.session.ready();\n </script>\n</body>\n</html>\n```\n\n`c.text(): Promise<Text>` makes one request to the game\'s own origin, tied to the version currently open. Caisual and `caisual dev` read the matching files and merge them per key: `pt-BR` then `pt` then the manifest\'s default language. Longer tags fall back through their parent tags, such as `zh-Hant-TW`, `zh-Hant`, `zh`. The default file is tried once. If no file contains a key, `t` returns that key. Empty strings are valid translations.\n\nConcurrent and later `c.text()` calls share the same promise and translator for that connection. There are no dependencies, eager downloads, or per-call network requests. Missing files, invalid dictionaries and network failures do not prevent startup. If the Caisual text service is unavailable, for example on a plain static host outside Caisual, the translator returns keys; it does not probe other URLs. Use `caisual dev` to preview the complete convention.\n\n`t(\'score\', { n: 3 })` replaces named placeholders with strings or numbers. An omitted placeholder stays unchanged, such as `{n}`. The result is plain text, with no HTML processing, plural rules or automatic translation. Set `textContent` or draw it on the canvas; do not insert it as HTML. A new document gets the host\'s current language and a fresh translator.\n\nMode `label` and `instructions`, role `label`, and leaderboard `label` accept a string or a language-to-text object, and resolve from `uiLanguage` in the overlay using the same fallback chain. `name`, `description` and `tags` keep their existing forms. Existing single-string labels stay unchanged. See [manifest languages and validation](./publish.md#game-translations) for CLI checks and migration from `language`.\n\n## Sessions and the standard overlay\n\nA game that declares `overlay` in `caisual.json` is a standard game: it runs full screen and Caisual draws everything around it. The opening menu, the mode choice, the lobby with roles, teams and ready, invitations, friends and parties, matchmaking, spectators, leaderboards, voice, the end of a match and Play again belong to the platform. The game keeps the field, its own HUD and its own settings.\n\n```json\n{ "overlay": { "version": 1, "accent": "#397e83" } }\n```\n\nTwo objects appear on the connection. `c.session` says which session the game is in, `c.overlay` says when the platform is on top of it.\n\n```js\nconst c = await caisual.connect();\n\nconst stopSession = c.session.onChange((session) => {\n detachGameListeners();\n if (session.kind === \'idle\') return showAttractScene();\n if (session.kind === \'local\') return showLocalRun(session.mode, session.status);\n attachGameListeners(session.room, session.kind === \'watch\');\n draw(session.room.state);\n});\n\nconst stopOverlay = c.overlay.onChange(({ inputBlocked, reservedRects }) => {\n clearHeldKeys();\n setInputEnabled(!inputBlocked);\n placeHudOutside(reservedRects);\n});\n\nawait loadAssetsAndChosenView();\nc.session.ready();\n```\n\n### The session\n\n`c.session.current` reads the session at once. `onChange` repeats the current value immediately to every new listener, returns a function that removes it, and then reports attaches, detaches and the end of a local run. It never fires for a move or a roster change: those stay on the room listeners.\n\n- `{ kind: \'idle\' }`: no session. Show an attract scene, not a menu.\n- `{ kind: \'local\', id, mode, status }`: a run of a mode declared with `"execution": "local"`. `status` is `playing` or `ended`.\n- `{ kind: \'room\', id, room }`: `room` is the `Room` documented below, already attached.\n- `{ kind: \'watch\', id, room }`: `room` is a `Spectate`. Draw it read only.\n\n`id` changes on every attach, so a second local run is distinguishable from the first.\n\n`c.session.ready()` says the game has loaded its assets and installed its listeners. Call it once, at the end of setup: until then the overlay waits instead of starting a session under a game that is still downloading. It is idempotent.\n\n`c.session.finish()` ends a local run and shows the compact end bar with a Play again action. It applies only to `kind: \'local\'`: on a room it fails with `not_local` and on idle it does nothing. The result of an online match comes from the server, never from `finish()`.\n\n`c.session.capabilities` is `{ local, rooms, overlay, requestRole }`. Outside caisual.com `overlay` and `rooms` are `false` while `local` stays `true`, which is the signal to run the game\'s own offline fallback. No fake room is created.\n\nTwo front ends of the same game share one session. Switching view does not call `ready()` again and does not detach the room: the new renderer reads `c.session.current` and draws.\n\n### The overlay on top\n\n`c.overlay.onChange` repeats the current geometry immediately, then on every change:\n\n- `inputBlocked` is `true` while a panel is open. Release held keys and stop reading input, but keep simulating: opening a panel never pauses a room.\n- `reservedRects` is an array of up to eight `{ x, y, width, height }` rectangles in CSS pixels of the game viewport. Keep the game\'s own HUD out of them, and nothing else: never resize, move, or letterbox the field because of them. The overlay sits on top of the game and the game must not shift when a bar or a pill appears. The field under a closed overlay stays visible and clickable.\n\nThe complete `OverlayView` value is `{ inputBlocked, reservedRects, safeArea?: { top, right, bottom, left }, shortcutEnabled? }`.\n\nThe host measures `safeArea: { top, right, bottom, left }` in CSS pixels of the game viewport, accounting for the iframe\'s position, borders and scale. It updates after viewport changes, including rotation. Use these values for HUD margins: `env(safe-area-inset-*)` inside the iframe usually reads zero. The kit also sets `--caisual-safe-top`, `--caisual-safe-right`, `--caisual-safe-bottom` and `--caisual-safe-left` on the game document root:\n\n```css\n.hud {\n top: max(16px, var(--caisual-safe-top, 0px));\n left: max(16px, var(--caisual-safe-left, 0px));\n}\n```\n\n`safeArea` is optional for compatibility with older hosts; treat a missing value as four zeros. Older kits keep receiving their supported geometry fields. During the opening screen, `inputBlocked` is true from the first view, including before the first host measurement.\n\n\n`c.overlay.open(panel)` asks the platform to open one of `home`, `room`, `invite`, `friends`, `voice`, `boards`. It is a request, not a permission: it creates no room and grants nothing. Outside caisual.com it does nothing.\n\nShift+Tab from the field opens the menu and Escape closes it. Text fields inside the game keep their own shortcut.\n\n### What a standard game no longer builds\n\nRemove these and let the overlay do them:\n\n- a start menu with Create, Join or a code field;\n- invitation links, copy buttons and share sheets;\n- the lobby: roster, ready, role and team pickers, the Start button;\n- a matchmaking screen with its cancel button;\n- a friends or party list;\n- voice buttons;\n- a leaderboard screen;\n- an Exit or Back to Caisual button;\n- a Play again button after a match.\n\nThe end bar recognizes optional standings and winners; the game may draw additional result details inside the field. `c.room.create`, `join`, `match` and `watch` stay available for a game that wants its own entry point: under a standard overlay the room they return becomes the current session and the standard controls follow it. Do not attach a second room controller from a second renderer.\n\n### Full screen\n\nA standard game fills the window. `html`, `body` and the game surface are 100% of the viewport: no maximum width, no header, no footer, no editorial frame, and no document scrolling at 1366x768 or 390x844, safe areas included. Only the HUD and compact controls sit over the scene.\n\nAim for the playable field to cover at least 70% of the visible area on desktop and 60% on mobile, counting only what shows or controls the game. A board with a fixed aspect ratio cannot always reach that: a square board on a 1366x768 window tops out near 56% before any HUD. That is the declared geometric exception: the board must then fill at least 90% of the largest rectangle that fits the area left free, and the HUD must have a stated ceiling, typically 48 to 64 pixels on desktop and about 160 pixels of controls on a phone.\n\n### Resume\n\nCaisual keeps one resume reference per game and per player, in a save key it owns. Leaving through the overlay with Leave for now stores the room code and detaches without giving up the seat; the standard menu then offers Resume, which rejoins from that code. Leave room removes the reference and gives up the seat, and so does a terminal room end. A `finished` room waiting for a rematch keeps its resume reference. A network drop keeps it.\n\nA game does not read or write that key, and does not build its own Resume button. A reference that is no longer valid returns the service error and the overlay explains it: it does not retry forever. Resume carries the room code, not a promise to reopen the same published version of the game.\n\n### Voice and leaderboards\n\nIn a standard game the overlay\'s voice panel carries Join, Leave, Mute, and the list of who is in the call, with the click the browser requires. A standard game does not draw its own voice buttons. The `room.voice` API below remains for games without the standard overlay and for server-side gain and proximity rules.\n\nLeaderboards are read by the overlay from the published manifest, using the boards and periods declared there. The overlay reads the official verified scores after a submission and offers Refresh: a game does not need a board screen. Scores are still submitted by the game or, better, by `server.js`.\n\n### Known gaps\n\nThree things are deliberately not in this version, and a game should not work around them:\n\n- resolving the original game version behind a persistent Resume;\n- inviting one friend straight into a room, as opposed to a party;\n- matchmaking for a whole group at once.\n\n## Daily challenge\n\n`c.daily` describes the day selected when the connection opened. `day`, `seed` and `expiresAt` stay fixed for that connection, including across UTC midnight. `expiresAt` is the next UTC midnight in milliseconds on the server clock.\n\n```js\nc.daily.day;\nc.daily.seed;\nc.daily.expiresAt;\nconst random = c.daily.rng();\nconst stop = c.daily.onChange(({ day, seed, expiresAt }) => {\n offerNextDailyRun({ day, seed, expiresAt });\n});\n```\n\n`onChange` reports a new `{ day, seed, expiresAt }` when the current UTC day changes. It returns an unsubscribe function and does not immediately replay the initial value. A suspended browser or a connection failure can delay the notification; the kit retries and reports the latest day when it can. The event does not mutate `c.daily` or reseed either generator. `rng()` always creates a fresh generator from the connection\'s original seed, and `random()` keeps advancing the original shared generator. Use the event\'s context explicitly for a new daily run, or open a new document. Repeated `connect()` calls return the same connection.\n\n`day` is a UTC date such as `"2026-09-04"`; `seed` is an unsigned 32-bit integer shared by all players of that game on that day. Both generators produce values in [0, 1). `c.time.now()` is in milliseconds aligned with the portal clock. Without a connection, daily data uses the local clock and hostname, with the same listener and generator behavior.\n\nOn the server, `room.daily` is `{ day, seed, expiresAt }`, fixed at **room creation**, persisted across sleep and rematches. Its seed matches a client connection opened for that game on the creation day. A client connected yesterday can therefore have a different `c.daily` from a room created today: send the room\'s daily context through game state when rendering its course. To switch a persistent or rematched room to a new daily course, create a new room.\n\nBoards accept `"day": "submit" | "start"` in the manifest. Omission means `submit`, preserving submission-day scoring. With `start`, which requires `source: "server"`, submit `room.board.submit(player, \'run\', score, { day: room.daily.day })`. An explicit `day` implies a daily score. It must equal the room\'s creation day and arrive no later than **10 minutes after `room.daily.expiresAt`**, including the exact boundary. A run started at 23:58 and submitted at 00:04 is attributed to its starting day. The all-time score is a separate submission without `day` or `daily`.\n\n| Error code | Meaning |\n| --- | --- |\n| `board_day_required` | A daily score on a start-day board omitted `day`. |\n| `board_day_mismatch` | `day` differs from `room.daily.day`. |\n| `board_day_expired` | More than 10 minutes have elapsed after the room\'s daily expiry. |\n| `board_day_policy` | An explicit day was used on a submit-day board or with `daily: false`. |\n\nThese server calls throw synchronously; handle expected errors inside the callback if the game should continue. Accepted scores keep their assigned day and submission timestamp across delayed writes and retries. Client scores continue to use the day when the portal receives them and cannot choose a day.\n\n`caisual dev --day YYYY-MM-DD` fixes the simulated day and seed for new connections and rooms. Clocks stay real: `expiresAt` is the next **real** UTC midnight at connection or room creation, so comparing it with `c.time.now()` or `room.time.now()` remains valid even for a simulated date. Restored rooms keep their original daily context. Restart dev and reload to change the flag; create a new room for the newly selected day.\n\n## Saves\n\nEach player has up to 32 saves per game. A save is any JSON value up to 256 KB when serialized.\n\n```js\nawait c.save.set(\'slot1\', { level: 3, coins: 120 }); // -> { key, bytes, updatedAt }\nconst data = await c.save.get(\'slot1\'); // -> the value, or null\nawait c.save.remove(\'slot1\');\nconst saves = await c.save.list(); // -> [{ key, bytes, updatedAt }]\n```\n\n- Keys use 1 to 32 characters: lowercase letters, digits, `_` or `-`, starting with a letter or digit.\n- `updatedAt` is a millisecond timestamp.\n- Saves are per player and per game. Another game cannot read them.\n- When not connected, saves go to the browser\'s local storage on the game origin.\n\nErrors reject the promise with an `Error` whose `code` is one of `invalid_request`, `not_found`, `save_limit`, `payload_too_large`, `rate_limited`, `invalid_ticket`, `internal_error`, or `offline`.\n\n## Leaderboards\n\nA leaderboard is identified by a board id chosen by the game. Scores are non-negative integers and higher is better. Each player keeps one entry per board, and one per board per day for daily boards: the best score is kept.\n\n```js\nconst result = await c.board.submit(\'main\', 1234);\n// -> { accepted: true, best: 1234, rank: 7, day: null, verified: false }\n\nconst daily = await c.board.submit(\'main\', 1234, { daily: true });\n// -> { accepted: true, best: 1234, rank: 7, day: "2026-09-04", verified: false }\n\nconst top = await c.board.top(\'main\', { daily: true, limit: 10 });\n// -> { day: "2026-09-04", entries: [{ rank, name, score, guest, me, verified }], me: { rank, score, verified } | null }\n```\n\n- Board ids use the same format as save keys.\n- `submit` never rejects because of connectivity. When the game is not connected it resolves `{ accepted: false, reason: "offline" }`.\n- `best` is the score kept for this player after the submission, which can be higher than the submitted one.\n- `rank` counts players with a strictly higher score. Ties are ordered by who reached the score first.\n- Accounts and guests are ranked separately. `top()` returns account players by default; pass `guests: true` to list guests instead. `me` always refers to the current player within their own category, even beyond `limit`.\n- `limit` is 1 to 100 and defaults to 10.\n- Pass `day: "2026-09-06"` to `top()` to read exactly that UTC day, even after midnight. A day implies the daily filter. A date that is not a real `YYYY-MM-DD` is rejected.\n- `verified` is `true` when the kept score came from the room server. Browser scores cannot replace a verified score.\n- Add `"boards": { "main": { "source": "server" } }` to `caisual.json` for a server-only board. It accepts scores only from `room.board.submit` in `server.js`.\n- An omitted board has `source: "client"`. Browser submissions keep working for existing games.\n- A browser submission to a server-only board rejects with `board_server_only`.\n\n### Verified scores in a single-player game\n\nA verified solo game uses a `room` mode with one place. The server validates each move and computes the score. This complete example accepts each of three cells once; a client cannot submit a score or claim the same cell twice. The standard overlay provides Play again. The optional client button below calls the same `restart()` API when no overlay is available.\n\n`caisual.json`:\n\n```json\n{\n "manifest": 1,\n "cover": "cover.png",\n "card": "card.png",\n "icon": "icon.png", "id": "verified-cells", "name": "Verified Cells", "platform": "both",\n "languages": ["en"], "overlay": { "version": 1 },\n "modes": [{ "id": "solo", "execution": "room", "players": { "min": 1, "max": 1 }, "lobby": false }],\n "boards": { "run": { "source": "server", "day": "start", "label": "Cells", "periods": ["daily", "all-time"] } }\n}\n```\n\n`server.js`:\n\n```js\nimport { defineGame } from \'@caisual/kit/server\';\n\nfunction reset(room) {\n room.state = { cells: [false, false, false], score: 0, daily: { ...room.daily } };\n}\nexport default defineGame({\n tickRate: 0,\n onCreate: reset,\n onRestart: reset,\n onMessage(room, player, message) {\n if (room.status !== \'playing\' || player.role === \'spectator\') return;\n const cell = message?.cell;\n if (!Number.isInteger(cell) || cell < 0 || cell > 2 || room.state.cells[cell]) return;\n room.state.cells[cell] = true;\n room.state.score += 1;\n if (room.state.score !== 3) return;\n const result = { standings: [{ playerId: player.id, score: room.state.score }], winners: [player.id], unit: \'points\' };\n room.board.submit(player, \'run\', room.state.score);\n if (room.time.now() > room.daily.expiresAt + 10 * 60_000) {\n // La corsa scaduta conserva il record assoluto; una stanza nuova scegliera\' il nuovo giorno.\n room.end({ ...result, data: { dailyExpired: true } });\n return;\n }\n room.board.submit(player, \'run\', room.state.score, { day: room.daily.day });\n room.end(result, { rematch: true });\n },\n});\n```\n\n`client/index.html`:\n\n```html\n<!doctype html>\n<html lang="en">\n<meta charset="utf-8">\n<meta name="viewport" content="width=device-width, initial-scale=1, viewport-fit=cover">\n<title>Verified Cells</title>\n<style>body{margin:0;min-height:100dvh;display:grid;place-content:center;gap:12px;background:#12252b;color:#f2faf3;font:16px system-ui}button{min-width:48px;min-height:48px}</style>\n<p id="score"></p><div id="cells"></div><button id="again" hidden>Play again</button>\n<script type="module">\n import { caisual } from \'/__caisual/kit/v1.js\';\n const c = await caisual.connect();\n const score = document.querySelector(\'#score\'), cells = document.querySelector(\'#cells\'), again = document.querySelector(\'#again\');\n let room = null, blocked = false, stops = [];\n function draw() {\n cells.replaceChildren();\n if (!room) { score.textContent = \'Choose Play to start.\'; again.hidden = true; return; }\n score.textContent = `${room.state.score} / 3`;\n room.state.cells.forEach((claimed, cell) => {\n const button = document.createElement(\'button\'); button.textContent = claimed ? \'\u2713\' : String(cell + 1);\n button.disabled = blocked || room.connection !== \'connected\' || room.status !== \'playing\' || claimed;\n button.onclick = () => room.send({ cell }); cells.append(button);\n });\n again.hidden = c.session.capabilities.overlay || room.status !== \'finished\';\n again.disabled = blocked || room.connection !== \'connected\';\n }\n again.onclick = () => { again.disabled = true; room.restart(); };\n c.overlay.onChange((view) => { blocked = view.inputBlocked; draw(); });\n function attach(next) {\n stops.forEach((stop) => stop()); stops = []; room = next;\n if (room) stops.push(room.onState(draw), room.onStatus(draw), room.onConnection(draw));\n draw();\n }\n c.session.onChange((session) => attach(session.kind === \'room\' ? session.room : null));\n c.session.ready();\n if (!c.session.capabilities.overlay && c.connected) attach(await c.room.create({ mode: \'solo\' }));\n</script>\n</html>\n```\n\n`room.board.submit(\'run\', score, { day: room.daily.day })` is also accepted in a solo room: the sole non-spectator member is implicit. In multiplayer, always pass `player` or its id first; omitting it throws `board_player_required`. `room.board.submit` queues the score; use `room.onScoreQueued` and the overlay\'s board refresh to observe it, not a state transition as a storage receipt. A rematch keeps the room\'s creation day; a new room selects the current day.\n\n## Device\n\n`c.device` contains the browser and device report collected while `connect()` runs:\n\n```ts\ninterface DeviceReport {\n webgl2: boolean;\n webgpu: boolean;\n wasm: boolean;\n threads: boolean;\n isolated: boolean;\n gpu: \'hardware\' | \'software\' | \'none\';\n memoryMb: number | null;\n cores: number | null;\n mobile: boolean;\n tier: \'low\' | \'mid\' | \'high\';\n}\n```\n\nUse capability fields to choose a renderer, then use `tier` to reduce pixel ratio and quality on smaller devices:\n\n```js\nconst renderer = c.device.webgpu\n ? createWebGpuRenderer()\n : createWebGl2Renderer();\n\nconst pixelRatio = c.device.tier === \'high\' ? devicePixelRatio : 1;\nconst quality = c.device.tier === \'low\' ? \'low\' : \'high\';\nrenderer.configure({ pixelRatio, quality });\n```\n\nThe probe takes at most 1.5 seconds. `memoryMb` and `cores` are `null` when the browser does not expose them. The report stays in the browser and is not saved or sent to Caisual.\n\n### Two front ends, one game\n\nKeep one `client/index.html`, one game ID and one server. With `platform: "both"`, choose separate front ends in that entry without navigating or adding another iframe:\n```js\nimport { caisual } from \'/__caisual/kit/v1.js\';\nconst c = await caisual.connect();\nlet preference = null;\ntry { preference = localStorage.getItem(\'layout\'); } catch {}\nconst touch = preference === \'touch\' || (preference !== \'desktop\' && (c.device.mobile || matchMedia(\'(pointer: coarse)\').matches));\nconst screen = touch ? await import(\'./touch/main.js\') : await import(\'./desktop/main.js\');\nscreen.mount({ c, root: document.querySelector(\'#app\') });\n```\nOffer a manual layout choice, persist it when storage is available, and keep rules and room connections shared. Both front ends use relative asset paths inside `client/`.\n\n## Rooms\n\nA room brings players into the same running game. Creating and joining require a published `server.js`; single-player games can ignore `c.room`.\n\nIn a standard game the overlay creates, joins, matches and watches on the player\'s behalf, and hands the game the room through `c.session`. The calls below stay available, and their result becomes the current session. Read them for what the room object offers; do not rebuild the entry screens around them. An optional [MatchResult](#recognized-match-results) in `room.end` lets the overlay display winners and standings while `room.result` keeps the original JSON.\n\n```js\nconst c = await caisual.connect();\n\nc.room.invited; // invitation code from the game page, or null\n\nconst room = await c.room.create({ mode: null });\n// Or join the invitation that opened the game:\nconst invitedRoom = await c.room.join();\n// Or enter a code supplied by the player:\nconst codedRoom = await c.room.join(\'ABC234\');\n\nroom.code;\nroom.seed; // unsigned 32-bit integer fixed for this room\nroom.tickRate;\nroom.latency;\nroom.invite(); // { code: "ABC234", url: "https://caisual.com/r/ABC234" }\n```\n\nPass a mode id from the manifest to `create({ mode })`, or `null` to use the root configuration. Optional `players` and `lobby` on that mode replace the root values; joining keeps the configuration of the room being joined. `join()` uses `c.room.invited`; without an invitation, pass the six-character code explicitly. A standard game does not need `invite()`: the overlay owns the invitation panel and the copy action.\n\n### Crew\n\nThe kit automatically reports the player\'s current room to the Caisual portal, so the player\'s friends can join with one click. The game does not need to send or handle anything for this. There is no `c.crew` API in this version. In a standard game the friends and party list is a panel of the overlay, so there is nothing to draw either.\n\n### Matchmaking\n\nUse `c.room.match()` to find players who requested the same mode and key. The key must contain exactly the fields declared by that mode\'s `matchmaking.key` in `caisual.json`.\n\n```js\nconst room = await c.room.match({\n mode: \'daily\',\n key: { day: c.daily.day, stage: 3 },\n onWaiting({ players, min, max }) {\n showQueue(`${players}/${max} players, ${min} required`);\n },\n});\n```\n\nMatchmaking uses the selected mode\'s resolved `players` and `lobby`, and that mode\'s `matchmaking.timeoutMs`. A room opens as soon as the queue reaches the resolved `players.max`. When `matchmaking.timeoutMs` expires, it also opens if at least `players.min` players are waiting. Otherwise the promise rejects with `no_match`, and the game should offer the player another option. A new search first tries to fill a matching room that is already open and can still accept players.\n\nPass an `AbortSignal` as `signal` to let the player cancel a search. Cancellation rejects with `cancelled`. In a standard game the search screen, its Cancel button and the lobby that follows are the overlay\'s: declare `matchmaking.defaults` in the mode and the player can start a search from the standard menu without the game passing a key.\n\nRoom status is one of:\n\n- `lobby`: players are joining and choosing their setup.\n- `countdown`: the lobby has accepted `start()` and play begins at the announced server time.\n- `playing`: the game server is running the match.\n- `finished`: the match is over and the room is waiting for a rematch, with sockets open and `room.result` available.\n- `ended`: the match or connection has ended. `room.result` contains the result last reported by the room. A definitive connection closure uses `{ closed: 4003 }` when the player was kicked, `{ closed: 4004 }` when the room ended, `{ closed: 4005 }` when the published version closed, or `{ closed: 4006 }` when the same player opened the room in another tab.\n\nThe current lobby data is available directly:\n\n```js\nroom.players; // [{ id, name, guest, role, team, ready, connected }]\nroom.you; // this player\'s id\nroom.host; // the current host\'s id, or null\n\nroom.ready(true);\nroom.setRole(\'captain\');\nroom.setTeam(1);\n\nif (room.you === room.host) room.start();\n```\n\nIn a standard game the overlay calls these four for the player: read `room.players` to draw the field, not to build a roster panel. `ready`, role, team, and `start()` are lobby actions. Starting requires the host, every connected player to be ready, and the player, role, and team minimums from the manifest. Calling `start()` begins a three-second countdown. A role or team change clears that player\'s ready state. The built-in `spectator` role is still a player slot for setups such as a shared screen with phone controllers. Use `watch()` for someone who only observes and does not occupy a player slot.\n\n### Rematch in the same room\n\nThe server opts in per match with `room.end(result, { rematch: true })`. The room becomes `finished`, keeps its code, members, state and open connections, and exposes the result through `room.result` and `onStatus(\'finished\', result, at)`. Voice and spectator streams remain connected. `room.end(result)` or `{ rematch: false }` still ends the room permanently with status `ended` and close code 4004.\n\nWith `rematch: true` in a multiplayer room, each connected player calls `room.restart()` once to become ready for the rematch. In `finished`, `room.players[].ready` means rematch readiness. Once every connected non-spectator player is ready and the mode\'s `players.min` is met, the host calls `room.restart()` again to confirm. The first host call only registers readiness. A host with the built-in `spectator` role does not register readiness and can confirm once the players are ready. Repeated non-host calls do nothing. Calling outside `finished` fails with `rematch_unavailable`; an early host confirmation reports `players_not_ready` through `room.onError`.\n\nThe standard overlay handles these calls with Play again, the readiness count and names, and Start rematch for the host. Games without the overlay can call `restart()` directly. No member is removed for declining. With `rematch: true`, there is no automatic start after a departure: the current host must confirm.\n\nAt confirmation, the kit clears the result to `null` and all readiness flags, then calls optional `onRestart(room)` with status `lobby` when the mode has a lobby, or `playing` otherwise. **The kit does not reset `room.state`.** Reset match data in `onRestart`, keeping series scores or other data as needed. With a lobby, players choose their setup and get ready again before the normal countdown and `onStart`. Without a lobby, `onStart` follows `onRestart` immediately. Clients receive the new state and an `onStatus` transition with a null result. The room identity, seed and tick sequence are retained; `session.onChange` does not create a new session for a rematch. Listen to `room.onState` and `room.onStatus`.\n\n### Fast rematches and solo rooms\n\nThe existing `rematch: true` flow above is unchanged for multiplayer rooms. To shorten it, use:\n\n```js\nroom.end(result, { rematch: { keepSetup: true, autoStart: true } });\n```\n\nBoth flags default to `false`. `keepSetup` restores the previous readiness flags, keeps roles and teams, and skips the lobby: `onRestart` runs with status `countdown`, followed by the usual three-second countdown and `onStart`. The result clears before `onRestart`. Even a mode without a lobby gets this countdown. Readiness during `finished` is still consent for the next match, separate from the previous setup. Player, role and team minimums are rechecked during the countdown; if they fail, the room returns to the lobby so the setup can be repaired.\n\n`autoStart` starts the rematch when all connected non-spectator players have accepted and the mode\'s minimum is met, including after a departure leaves that condition satisfied. The overlay shows the readiness count and names without a host confirmation button or wait-for-host text. With `autoStart`, the host can also call `restart()` again to confirm early once at least `players.min` connected non-spectator players have accepted. Members who have not accepted remain in the room and join the next phase; they are not removed. With `autoStart` alone, the next phase is the usual lobby or immediate play; combine it with `keepSetup` to skip another ready/start cycle.\n\nA room whose resolved `players.max` is `1` is a solo room, including when it inherits `lobby: true`: it starts on entry and shows no lobby, invitation, code or wait-for-host prompt. In `finished`, one `room.restart()` immediately runs `onRestart`, then `onStart`, without consent or host confirmation or a countdown. The standard Play again button makes that one call. After a terminal end it creates a new solo room without opening or copying an invite. No additional manifest field is needed; `caisual init` without `--multiplayer` still creates a local game.\n\nThe waiting rules are:\n\n- All pending `room.schedule` handlers are cancelled when the match finishes, including handlers already due in the same batch. Scheduling during `finished` has no effect. Schedule new work in `onRestart` or `onStart`. Tick callbacks stop immediately; game input during the wait is discarded, and queued continuous input is cleared on the client.\n- `onEnd` runs once for the completed match, with status `finished` and its result. A subsequent timeout only closes the room and does not call `onEnd` again. Restart does not resubmit or clear pending board scores: each submission is drained once from the existing score queue. The game must not submit the previous match\'s scores again in `onRestart`.\n- Disconnecting clears that member\'s readiness and transfers the host to the oldest connected member. Disconnected players do not count toward readiness or the minimum. The usual 60-second reconnection grace applies; persistent members keep their seats after it. Rejoining requires a new readiness call. Connection and departure callbacks continue during the wait.\n- New members may join by invitation during the wait, up to the resolved `players.max`, and start unready. A lobby mode therefore reopens admission while `finished`; without a lobby, admission continues as during play. Disconnected members still occupy seats until removed. Roles and teams retain their existing capacity rules.\n- `watch()` spectators do not vote or occupy seats and keep following the same delayed stream across matches. Members with role `spectator` occupy a seat but do not vote or count toward the rematch minimum.\n- The room closes with 4004 exactly two minutes after entering `finished` if no restart is confirmed, retaining the last result. Readiness, joins and pings do not extend this deadline. It also applies to persistent rooms and survives sleep or restoration; it does not depend on active ticks.\n\n`finished` is a new status, not a terminal connection state. Older room clients receive the unfamiliar status and keep their sockets open instead of taking their `ended` cleanup path; they have no `restart()` control, and older overlays may retain their previous screen. Use an updated kit for games opting into rematches, and explicitly handle `finished` in game status listeners.\n\nThe server owns room state. Read it and react to updates, but do not assign to it or mutate nested values from the browser:\n\n```js\ndraw(room.state);\n\nconst stopState = room.onState((state, tick, serverTime) => {\n draw(state);\n});\n\nconst stopPlayers = room.onPlayers((players) => updateLobby(players));\nconst stopStatus = room.onStatus((status, result, at) => showStatus(status, result, at));\n\nstopState();\nstopPlayers();\nstopStatus();\n```\n\n`room.tick` identifies the latest state. The kit applies structural updates in order for both tick-based and event-only servers. Full state is sent on entry, reconnection or resync when an update does not match the current tick; normal updates, including every hundredth tick, remain diffs. `room.serverTime()` returns milliseconds aligned with the room clock and is kept current by a ping every five seconds.\n\n`room.tickRate` is the current effective server frequency, including reductions caused by the CPU budget; zero means event-only. Updates arrive with state diffs and snapshots, including an empty diff when only the frequency changes. `room.latency` is the smoothed round-trip time in milliseconds, or `null` before the first pong and after a dropped connection until the next pong. Each pong uses 20% of the new RTT and 80% of the previous estimate, with the first sample used directly. Read the property when drawing network status; there is no `onLatency` listener. Spectators expose the same properties; their tick rate follows the delayed state stream.\n\nSend JSON input to `onMessage` in the server definition, and receive JSON sent or broadcast by the server:\n\n```js\nroom.send({ type: \'fire\', target: 3 });\n\nconst stopMessages = room.onMessage((message) => {\n showEvent(message);\n});\n```\n\nThe kit numbers outgoing inputs in increasing order. It automatically reconnects temporary failures with delays of 1, 2, 4, then 8 seconds, for at most the room\'s 60-second grace period. Each attempt gets a fresh room token. A successful reconnect replaces local state with a full server state. `room.send` calls while reconnecting throw an error with `code: "offline"`.\n\nUse `room.input(value)` for continuous controls, including calls from every animation frame:\n\n```js\nroom.input({ type: \'move\', x: axisX, y: axisY });\nroom.onError(({ code }) => {\n if (code === \'rate_limited\') showInputWarning();\n});\n```\n\n`input` copies and keeps only the latest JSON value in one slot. It coalesces updates and sends at most 20 times per second, or at the effective `room.tickRate` when that is lower and positive. With `tickRate: 0`, it still sends at most 20/s. It also waits for budget used by `send`. Values with the same JSON serialization are not resent on the same connection. Combine independent controls into that one value; there is no channel option. On the server it is an ordinary message passed unchanged to `onMessage`, exactly like `send`, with no extra envelope.\n\nDuring reconnection, `input` accepts updates without throwing `offline`. After the new welcome it sends only the latest value, even if it was sent on the previous connection. It never replays intermediate values or old commands. Use `send` for individual actions such as firing or confirming a turn; `send` still throws `offline` during reconnection. Invalid JSON input can throw `invalid_request`. Input stops after leaving, disconnecting intentionally or ending the room.\n\nGame messages are limited to 20 per second per connection. Excess messages are dropped; `room.onError` receives `rate_limited` at most once per second across both budgets. Protocol service messages, including ping, lobby actions, state requests and voice signaling, have a separate 20/s budget with the same drop policy. More than 100 attempts in each of three consecutive one-second windows in either budget closes the connection with 4008 `rate_limited`; the kit does not reconnect automatically after this abuse closure. Abuse windows start with the first message on the connection; a normal or empty window resets the sequence. Oversized game frames close with 4009 `message_too_large`, reported through `room.onError`; malformed frames use 4009 `bad_message`. Neither closure is retried automatically.\n\nCall `room.leave()` for an intentional departure. The kit does not reconnect after leaving, being kicked, the room ending, the published version closing, or the same player opening the room in another tab.\n\n`room.disconnect()` is the other departure: it stops the transport, the retries and voice without sending a leave, so the server keeps the seat under its own persistence and grace rules. It is not reversible on the same object; returning means entering again from the code. The overlay uses it for Leave for now, together with the resume reference.\n\nA room also exposes `room.mode`, `room.countdownAt`, `room.connection`, `room.metadata`, and the `onMetadata` and `onConnection` listeners. `connection` is one of `connecting`, `connected`, `reconnecting`, `disconnected`, `ended`, `closed`, or `replaced`, where `replaced` means the same player opened the room in another tab. Unlike `session.onChange` and `overlay.onChange`, these listeners do not repeat the current value: read the getter first.\n\n`room.onError(listener)` reports technical protocol errors of the room as `{ code, message }`; it is not the place where a game reads its own result.\n\n`room.onScoreQueued(listener)` and `room.queuedScores` cover scores submitted for this player by `server.js`. Each entry is `{ board, player, score, day, submittedAt }`, only the owner\'s connection receives it, and the last 32 are kept. It is a technical notice that the server accepted the score, not a receipt that it is already on the board: read the board back with `c.board.top()` for that.\n\n`await room.requestRole(\'scout\')` asks the server for a role change during a match. It works only while the room is playing, only for a role declared in the manifest, and only when `server.js` defines `onRoleRequest(room, player, role)`; the server approves by calling `room.setRole`. Without that callback nothing changes, and the capability shows as `false` in `c.session.capabilities`. It is not a shortcut for changing roles from the browser.\n\nRoom creation, joining, and matchmaking reject with an `Error` carrying a stable `code`. Common codes are `invalid_request`, `no_server`, `no_match`, `cancelled`, `room_not_found`, `room_full`, `room_playing`, `room_ended`, `rate_limited`, `invalid_ticket`, `internal_error`, and `offline`. `no_server` means the published game has no multiplayer server. When `c.connected` is `false`, `create`, `join`, and `match` reject with `offline`.\n\n- `invalid_role`: a requested role id is malformed or is not declared in the manifest. Request a declared role id.\n- `role_change_unavailable`: the room is disconnected, is not playing, or `server.js` has no `onRoleRequest`. Wait for a connected playing state and provide that callback before offering the action.\n- `role_change_refused`: `onRoleRequest` returned without assigning the requested role. Leave the current role in place, or have the server approve with `room.setRole`.\n- `version_closed`: the room connection ended because its published version closed. Reopen the current game version and enter a current room.\n\nEvery listener call on a room returns a function that removes that listener: `onState`, `onPlayers`, `onStatus`, `onMessage`, `onMetadata`, `onConnection`, `onError`, and `onScoreQueued`.\n\n### Responsive action games\n\n1. Start with `npx @caisual/cli init --arcade my-arena`, a complete English/Italian canvas game with shared rules and tests.\n2. Accumulate `deltaSeconds` on the server and advance shared physics at a fixed 1/60 second step.\n3. Keep positions, collision or range checks, cooldowns and scores authoritative in `server.js`.\n4. Use `room.input()` for continuous controls and `room.send()` for discrete actions such as the starter\'s pulse.\n5. Number commands in the game and publish each player\'s last **applied** `ack` in `room.state`; transport sequence numbers are separate.\n6. Because `input()` coalesces values, send a bounded batch of unacknowledged commands; deduplicate on the server and consume at most one per simulation step.\n7. Predict your own entity with the shared step, replace it with each authoritative snapshot, then replay only commands after `ack`; ease corrections in the drawing only.\n8. Buffer remote samples by the `onState` timestamp and render behind `room.serverTime()`, accounting for RTT in `room.latency`, effective `room.tickRate`, and spectator `delayMs`.\n9. Clear pending controls on reconnection or a new round; keep the HUD inside safe areas and outside `reservedRects`, and end with `standings` plus `keepSetup`/`autoStart` rematches.\n10. Run the generated browser fixture and `/__caisual/players?n=2` with `dev --latency 120 --jitter 40 --loss 2`; see [Testing with a browser](/docs/local-development#testing-with-a-browser).\n\n### Spectators\n\nIn a standard game the overlay offers watching from the menu and the session arrives as `{ kind: \'watch\' }`. Use `c.room.watch(code)` to observe a room without joining it as a player:\n\n```js\nconst view = await c.room.watch(\'ABC234\');\n\ndraw(view.state);\nview.onState((state) => draw(state));\nview.onPlayers((players) => updateRoster(players));\nview.onStatus((status, result) => showStatus(status, result));\nview.onMessage((message) => showEvent(message));\n\nview.leave();\n```\n\nThe returned `Spectate` object exposes `state`, `tick`, `seed`, `status`, `players`, `host`, `code`, `result`, `delayMs`, the four listeners shown above, `serverTime()`, and `leave()`. It receives the room\'s public state, snapshots and updates, player list, status, and messages broadcast by `server.js`. The kit repairs a missed update automatically and reconnects temporary failures for the same 60-second grace period used by players.\n\nPublic room events are delayed by `delayMs`, which defaults to 3000 milliseconds. A game can set `"spectators": { "delayMs": N }` in `caisual.json`, where `N` is from 0 to 30000, or set `"spectators": false` to disable watching.\n\nA spectator has no `you`, `invite()`, `send()`, or voice API. Watching does not add anyone to `room.players`, does not affect roles, teams, player minimums, the host, or room lifetime, and is not visible to `server.js`. `watch()` can reject with `room_not_found`, `room_ended`, `spectators_disabled`, `spectators_full`, `rate_limited`, `offline`, or `invalid_request`.\n\n## Voice\n\nEvery room has a `room.voice` object. Voice is disabled by default and is enabled with the manifest\'s `voice` field.\n\nIn a standard game the overlay\'s voice panel carries Join, Leave, Mute, and the list of who is in the call, with the click the browser requires. A standard game does not draw its own voice buttons. The `room.voice` API below remains for games without the standard overlay and for server-side gain and proximity rules.\n\nA game with its own controls must offer an explicit one, because `join()` must be called from a click or another user gesture so the browser can start audio and, when publishing, request microphone permission.\n\n```js\nconst micButton = document.querySelector(\'#mic\');\nconst voiceList = document.querySelector(\'#voice-list\');\n\nfunction renderVoice(peers = room.voice.peers) {\n voiceList.replaceChildren(...peers.map((peer) => {\n const item = document.createElement(\'li\');\n const player = room.players.find((entry) => entry.id === peer.id);\n item.textContent = `${player?.name ?? peer.id}: ${\n peer.speaking ? \'speaking\' : peer.muted ? \'muted\' : \'quiet\'\n }`;\n return item;\n }));\n micButton.textContent = room.voice.state === \'off\'\n ? \'Join voice\'\n : !room.voice.mic ? \'Listening\' : room.voice.muted ? \'Unmute\' : \'Mute\';\n}\n\nmicButton.addEventListener(\'click\', async () => {\n if (room.voice.state === \'off\') await room.voice.join();\n else if (room.voice.mic) room.voice.mute(!room.voice.muted);\n renderVoice();\n});\n\nroom.voice.onPeers(renderVoice);\nroom.voice.onState(() => renderVoice());\nrenderVoice();\n```\n\n`room.voice.mode` is `none`, `room`, `team`, or `proximity`. In `room` mode, every participant in voice can hear every other participant. In `team` mode, players hear only their team. In `proximity` mode, the room server controls the gain between participants. Call `room.voice.join({ mic: false })` to listen without opening or publishing a microphone. Spectators join in listening mode when they call `join()` without options. A spectator that calls `join({ mic: true })` receives the `spectator` error.\n\nThe room server authorizes every voice track by team and gain. Listening that is no longer allowed is refused or closed.\n\n`room.voice.state` is `off`, `joining`, `on`, or `reconnecting`. `room.voice.mic` is `true` while the local player is publishing. `room.voice.muted` and `room.voice.speaking` describe the local microphone. `room.voice.peers` contains the other voice participants as `{ id, mic, muted, speaking, volume, gain }`. A listening participant has `mic: false`, `muted: true`, and `speaking: false`. `volume` is the local setting and `gain` is the value from the room server. Use `room.voice.setVolume(playerId, volume)` with a value from 0 to 1 to change only local playback.\n\n`room.voice.onPeers(listener)` runs when participants, microphone state, mute state, speaking state, volume, or gain changes. `room.voice.onState(listener)` reports connection state changes. Both return a function that removes the listener.\n\nCall `room.voice.leave()` to stop publishing or listening without leaving the room. `room.voice.mute()` requires an active published microphone and otherwise throws `not_publishing`. `room.leave()` and the end of the room stop voice automatically.\n\n`join()` rejects with an `Error` carrying one of these stable codes: `voice_disabled`, `permission_denied`, `unsupported`, `spectator`, `offline`, or `voice_error`. Voice can reconnect after a temporary room or media connection failure. The state becomes `reconnecting` while the kit retries.\n\n## Server\n\nPut `server.js` next to `caisual.json` and publish it with the game. See [publish.md](./publish.md#multiplayer-server) for the file rules, validation, and publishing flow.\n\n```js\nimport { defineGame } from \'@caisual/kit/server\';\n\nexport default defineGame({\n tickRate: 20, // required, an integer from 0 to 60; 0 runs only in response to events\n onCreate(room) {},\n onStart(room) {},\n onJoin(room, player) {},\n onConnection(room, player, connected) {},\n onLeave(room, player, reason) {}, // "left", "timeout", or "kicked"\n onRoleRequest(room, player, role) {}, // approve with room.setRole\n onMessage(room, player, message) {},\n onTick(room, deltaSeconds) {},\n onEnd(room) {},\n onRestart(room) {},\n});\n```\n\n`tickRate` is the only required field: `defineGame` throws a `TypeError` when it is missing or is not an integer from 0 to 60. All callbacks are optional. A player is `{ id, name, guest, role, team, connected }`.\n\n### Callback order\n\n- `onCreate` runs once when the room is first created, before any player joins.\n- `onJoin` runs when a player first enters the room, not when that same member reconnects. Without a lobby, the first player\'s `onJoin` is followed by `onStart`.\n- `onStart` runs when the room changes to `playing`. Without a lobby this is the first player entry. With a lobby it is after the host starts, the three-second countdown finishes, and the room still meets its minimums.\n- `onTick` runs for each active game tick when `tickRate` is greater than zero. `onMessage` runs for accepted client game messages, and `onRoleRequest` runs for an in-match role request when that callback exists. These events are processed serially, so their relative order is the order in which the room processes them.\n- `onConnection(room, player, connected)` runs with `false` when an existing member loses their connection and with `true` when that disconnected member returns. Both `player.connected` and `room.players` are already updated. It does not run for the first entry, a socket replacement while the member is still connected, or a permanent removal. Restoring a room reconciles actual connections: members saved as connected whose sockets are gone receive `false`, and their later return receives `true`. Surviving sockets receive no extra callback.\n- `onLeave` runs only when a player is removed with reason `left`, `timeout`, or `kicked`. A dropped connection calls `onConnection` during the grace period and does not call `onLeave`.\n- `onEnd` runs after the callback that requested `room.end`, with the final result and status `ended` or `finished`. Automatic room endings also use it, except closure of an already `finished` match, which must not run it twice.\n- `onRestart` runs when a rematch starts, after clearing the result and selecting the next status. Readiness is reset by default or restored with `keepSetup`. The fast-rematch and solo rules above override the default host-confirmed flow. With a lobby it prepares `lobby`; without one it prepares `playing` and is immediately followed by `onStart`. It is optional; `room.state` is preserved unless the game changes it.\n\nThere is no `onResume` callback. A sleeping room restores its saved state without calling one. When a scheduled time arrives, the room invokes the method named by `room.schedule`.\n\nFor a turn-based game, store the active player\'s id and pause that turn when the player disconnects. This also works with `tickRate: 0`, because callback state changes are published immediately:\n\n```js\nonConnection(room, player, connected) {\n if (room.state.turnPlayerId === player.id) {\n room.state.turnPaused = !connected;\n }\n},\nonMessage(room, player, message) {\n if (room.state.turnPaused || player.id !== room.state.turnPlayerId) return;\n applyTurn(room, player, message);\n},\n```\n\nIf turns have deadlines, save the remaining time when pausing and calculate a new deadline on return. Scheduled handlers must check whether the turn is still paused or current. Decide separately in `onLeave` how to handle a permanent departure. Secret redelivery remains a client responsibility through `room.onConnection`; see [Hidden information](#hidden-information).\n\nThe room object provides:\n\n```js\nroom.id;\nroom.seed;\nroom.mode;\nroom.status;\nroom.tick;\nroom.tickRate;\nroom.result;\nroom.state;\nroom.players;\nroom.host;\n\nroom.broadcast(message);\nroom.send(playerOrId, message);\nroom.kick(playerOrId);\nroom.setRole(playerOrId, role);\nroom.setTeam(playerOrId, team);\nroom.end(result);\nroom.end(result, { rematch: true });\n\nawait room.save(\'round\', value);\nawait room.load(\'round\');\nawait room.shared.get(\'ship_abc\');\nawait room.shared.set(\'ship_abc\', value);\nawait room.shared.delete(\'ship_abc\');\nawait room.shared.list(\'ship_\');\nawait room.shared.increment(\'visits\', 1);\nroom.schedule(milliseconds, \'methodName\', payload);\nroom.board.submit(playerOrId, \'main\', score, { daily: true });\nroom.board.submit(playerOrId, \'run\', score, { day: room.daily.day });\n\nroom.daily.day;\nroom.daily.seed;\nroom.daily.expiresAt;\nroom.time.now();\n\nroom.voice.mode;\nroom.voice.setGain(listener, speaker, 0.25);\nroom.voice.setProximity(playerA, playerB, 0.5);\n```\n\nSet `room.state` in `onCreate`, then mutate it only in server callbacks. It must remain plain JSON and may be at most 256 KB when serialized. `broadcast` sends a JSON message to everyone; `send` targets one player. `end` records a JSON result and closes the room unless rematch is enabled with `true` or an options object. See [Rematch in the same room](#rematch-in-the-same-room) for readiness, callback order, timer cancellation and the waiting deadline. Room saves use keys with the same format as player save keys and values up to 128 KB. `schedule` names a method on the definition so it can run even after a quiet room resumes. Scores submitted through `room.board` are verified. The room fixes their UTC `day` and millisecond `submittedAt` when `submit` is called, so delayed writes and retries do not move them to another day. Older queued scores without these fields retain the write-time day. Boards default to submission-day scoring. Set `day: "start"` and pass `{ day: room.daily.day }` for the starting day with a 10-minute grace period after midnight. See [Daily challenge](#daily-challenge) for expiry, errors and local day simulation.\n\n### Recognized match results\n\n`room.end(result)` still accepts any JSON. The optional `MatchResult` type is exported by the contracts and by `@caisual/kit` and `@caisual/kit/server`:\n\n```ts\ntype MatchResult = {\n standings: Array<{ playerId: string; score?: number; rank?: number }>;\n winners?: string[];\n draw?: boolean;\n unit?: \'points\' | \'time\' | \'distance\' | string;\n data?: JsonValue;\n} & Record<string, JsonValue>;\n```\n\nOrder `standings` from first place onward. Scores must be finite numbers; ranks are positive safe integers. The overlay uses the supplied order, displays player names and optional scores, and uses `rank` when present. `draw: true` takes precedence; otherwise `winners` identifies the winners. Without `winners`, the first row wins, together with rows sharing its explicit rank. Equal scores alone do not imply a draw. An explicit empty `winners` list means nobody won.\n\nThe end bar shows You won, You lost or Draw only for a participating player; spectators and players missing from the standings see Match finished. Outcome labels and the built-in units are translated in six languages. Custom units are shown as plain text; `time` and `distance` do not convert values or imply a measurement scale.\n\nReading is tolerant: unknown fields are ignored by the overlay, unknown or duplicate players are discarded, invalid scores and ranks are omitted, and winners must occur in the retained standings. With no usable standings the generic end bar remains. **`room.result` keeps the original JSON without normalization**, including `data` and any other game fields. Results are public to room members and spectators, so keep secrets elsewhere.\n\n```js\nroom.end({\n standings: [{ playerId: winner.id, score: 12, rank: 1 }, { playerId: other.id, score: 9, rank: 2 }],\n winners: [winner.id], unit: \'points\', data: { rounds: 3 },\n}, { rematch: true });\n```\n\n### Hidden information\n\nEverything in `room.state` reaches every player and every spectator. Never store cards in hand, secret roles, fog of war, or any other private value there.\n\nKeep secrets in room saves through `room.save` and `room.load`, which are server-only, or in module-level variables in `server.js`, keyed by room when needed. Deliver a secret to one player with `room.send(player, { type: \'hand\', cards })`. `onJoin` runs only on the player\'s first entry, not on reconnection, so the client requests its secrets for the current connection and every later reconnection:\n\n```js\nif (room.connection === \'connected\') room.send({ type: \'hand?\' });\nroom.onConnection((state) => {\n if (state === \'connected\') room.send({ type: \'hand?\' });\n});\n```\n\nThe server answers from `onMessage` with `room.send(player, { type: \'hand\', cards })`. The three-second delay of `c.room.watch` does not protect secrets. It only stops a player from watching an opponent\'s live screen in another tab.\n\nThe browser can change its role or team only while the room is in `lobby`. During a match, the server decides when a player changes role or team with `room.setRole` and `room.setTeam`. Both methods accept a player object or id and immediately update `room.players` for every client.\n\n```js\nonMessage(room, player, message) {\n if (message?.swap === \'captain\') {\n room.setRole(player, \'captain\');\n }\n},\n```\n\n`room.daily.seed` is fixed at room creation and shared by rooms created for that game on the same UTC day. `room.seed` is fixed for one room and is identical on the server and clients, so rooms created on the same day can generate different maps.\n\n### Shared game store\n\n`room.shared` is a server-only JSON key/value store shared by every room of the same game. It is useful when one room must leave data for another room, while `room.save` remains private to one room.\n\nThe following server leaves a ship when a room ends, then loads every previously left ship when another room is created. The room id suffix is used because shared-store keys follow the save-key format.\n\n```js\nexport default defineGame({\n tickRate: 0,\n\n async onCreate(room) {\n const keys = await room.shared.list(\'ship_\');\n room.state = {\n ships: await Promise.all(keys.map((key) => room.shared.get(key))),\n };\n },\n\n async onEnd(room) {\n const roomSuffix = room.id.split(\'.\')[1];\n await room.shared.set(\'ship_\' + roomSuffix, {\n position: room.state.position,\n cargo: room.state.cargo,\n });\n },\n});\n```\n\nThe five methods are asynchronous:\n\n```js\nconst value = await room.shared.get(key); // JSON value, or null\nawait room.shared.set(key, value); // last writer wins\nawait room.shared.delete(key);\nconst keys = await room.shared.list(prefix); // sorted, up to 1024\nconst total = await room.shared.increment(key, 1); // atomic, defaults to 1\n```\n\nKeys contain 1 to 32 lowercase letters, numbers, underscores, or hyphens. Values may be up to 64 KB when serialized, and each game may keep up to 1024 keys. Each room may perform up to 120 shared-store operations per minute. `increment` treats a missing key as zero and rejects unless the existing value, amount, and result are safe integers.\n\nUse the store in `onCreate`, `onStart`, `onEnd`, `onMessage`, or a `schedule` handler. Do not call it on every tick: each call waits for a remote operation, and the CPU budget uses elapsed wall-clock time. Browser clients cannot access this store. Send only the data they need with `room.broadcast` or `room.send`.\n\nFailures reject with an `Error` carrying `store_invalid_key`, `store_too_large`, `store_full`, `store_not_integer`, `store_unavailable`, or `store_rate_limited` in `code`.\n\n`room.voice.setGain(listener, speaker, gain)` controls how much one listener hears one speaker. It is directional, limited to the range from 0 to 1, and rounded to two decimal places. For example, the following setup lets the captain hear everyone while each crew member hears only the captain:\n\n```js\nconst captain = room.players.find((player) => player.role === \'captain\');\nconst crew = room.players.filter((player) => player.id !== captain.id);\n\nfor (const speaker of room.players) {\n room.voice.setGain(captain, speaker, 1);\n}\nfor (const listener of crew) {\n for (const speaker of room.players) {\n room.voice.setGain(listener, speaker, speaker.id === captain.id ? 1 : 0);\n }\n}\n```\n\n`room.voice.setProximity(a, b, gain)` is the symmetric shortcut for setting both directions. Both methods work in `room`, `team`, and `proximity` modes, and do nothing in `none`. In `team` mode, gains remain inside the team and cannot make a player hear another team.\n\nFor position-based audio, update the symmetric gain between players from server-owned positions:\n\n```js\nexport default defineGame({\n tickRate: 20,\n onTick(room) {\n for (const a of room.players) {\n for (const b of room.players) {\n if (a.id >= b.id) continue;\n const pa = room.state.positions[a.id];\n const pb = room.state.positions[b.id];\n const distance = Math.hypot(pa.x - pb.x, pa.y - pb.y);\n room.voice.setProximity(a, b, Math.max(0, 1 - distance / 20));\n }\n }\n },\n});\n```\n\n### Sleeping and cost\n\nPrefer `tickRate: 0` for turn based and party games. A room with a tick loop sleeps automatically after 30 seconds without player input or state changes and wakes on the next game message or player joining. Automatic ping and resync messages do not count as player input. A match with no player input for 10 minutes ends with `{ error: \'idle\' }`. Timers set with `schedule` and the countdown keep working while the room sleeps.\n\n### CPU budget\n\nEvery `onTick` and `onMessage` call is measured. Twenty consecutive calls above 100 ms end the room with `{ error: \'cpu_budget\' }`. If the average over 50 ticks is above 20 ms, the effective `tickRate` is halved, down to a minimum of 5, and clients receive an `error` message with code `tick_rate_reduced`. The optional `tickRate` field in `state` and `snapshot` protocol messages updates client `room.tickRate`; older clients ignore the added field. A frequency change sends a state diff even when its patch is empty.\n\n`room.tickRate` starts at the definition\'s `tickRate` and always reports the current effective frequency. `deltaSeconds` follows that frequency, so a fixed-step simulation must accumulate `deltaSeconds` instead of counting ticks. Measurement uses elapsed wall-clock time, so a slow `await` inside a callback also counts. `room.result` is `null` during a match, contains its result in `finished` or `ended` and inside `onEnd`, and returns to `null` before `onRestart`.\n\n### Persistent rooms\n\nSet `"persistent": true` in `caisual.json` for a room that must survive long breaks. It does not use the normal inactivity ending rule and does not end when every player disconnects. Players remain members until they call `room.leave()` or the server removes them with `room.kick()`. They can use the same room code to return while the game is already playing. The code remains valid while the room lives, and absent members remain in `room.players` with `connected: false`.\n\nA persistent room ends when the server calls `room.end(result)` without rematch, when its two-minute rematch wait expires, after 30 days without player input, entry, or a state change with `{ error: \'expired\' }`, or after five minutes without any members. The standard overlay stores the resume reference itself. Only games without it need to store the client `room.code` with `c.save.set()` and offer Resume. A persistent room incurs cost only while it is awake.\n\n## Limits\n\n- 120 requests per minute per player. Beyond that the kit rejects with `rate_limited`; wait and retry.\n- Saves: 32 keys per player per game, 256 KB per value.\n- Scores: safe integers from 0 upward.\n- Room state: 256 KB of plain JSON.\n- Game messages: 16 KB each and 20/s per connection; excess messages are dropped with at most one `rate_limited` error per second. Service messages have a separate 20/s budget. More than 100 attempts/s in either budget for three consecutive one-second windows closes with 4008. Oversized frames close with 4009 `message_too_large`.\n- Spectators: 100 per room, with a configured delay from 0 to 30 seconds.\n- Voice supports audio only and one voice channel per room.\n- Voice control messages: 64 KB each and 30 operations per 10 seconds per connection. Voice signaling also uses the separate service-message budget; audio traffic does not consume either message budget.\n- Room save values: 128 KB each.\n- Shared game store: 64 KB per JSON value, 1024 keys per game, and 120 operations per minute per room.\n\n## Development\n\nRun `npx @caisual/cli dev` from the game folder, then open the printed portal URL. The preview supplies the normal handshake, so `c.connected` is `true` and the game receives a local guest identity. It also mounts the same standard overlay as the portal. `?lang=` chooses the game preference, resolved against the manifest; `c.player.uiLanguage` follows the overlay fallback. For example, `?lang=ja` gives `c.player.language === "ja"` when declared, with the overlay in English. Friends and parties are marked unavailable locally; everything else, including saves, leaderboards, daily data, invitations, and rooms, works on local data. Opening the portal URL in more browser tabs creates more local players, which makes multiplayer testing possible without publishing.\n\nUse `npx @caisual/cli dev --day 2026-09-04` to pin the UTC day used by client and room daily seeds and local daily leaderboards. The flag accepts only a real date in `YYYY-MM-DD` format; without it, dev uses today\'s UTC date. Real clocks and room timers keep running normally. Scores remain in `.caisual-dev/scores.json` under their assigned day: restarting with another `--day` selects that day\'s board, and returning to a previous day restores its scores. Saves, identities and rooms are shared across these dates; queued room scores keep their assigned day when flushed after a restart. Existing rooms retain their creation context; new rooms follow the selected day. `expiresAt` follows the real clock even with `--day`. Use `c.board.top(\'main\', { day: \'2026-09-04\', guests: true })` to inspect a specific local day. A changed flag takes effect after restarting dev and reloading the game.\n\nOpen the printed `/__caisual/players?n=4` URL for 1 to 8 independent guest frames, each including the game and standard overlay. Use the overlay to create a room and join its code in the other frames. Each frame has **Drop** (1 to 60 seconds, default 3) and **Spectate**, which opens a new guest watching that frame\'s room. Phone frames are 390 x 844; the desktop preset is 960 x 640. Add `&lang=it` to test Italian.\n\n`dev --latency 120 --jitter 40 --loss 2` delays room WebSocket messages in both directions for players and spectators. Latency and jitter are integer milliseconds from 0 to 60000; loss is a percentage from 0 to 100 and may be fractional. Jitter and loss require `--latency`, which may be zero. Jitter varies delay uniformly within plus/minus the supplied value, clamped at zero, and preserves message order. Loss discards whole application messages, including protocol messages, to exercise recovery; it is not a model of TCP packet retransmission. HTTP, matchmaking and audio are unaffected. Drop closes the guest\'s room sockets and prevents successful reconnects for the selected duration; the kit\'s retry schedule can make the return later. The spectator stream also retains its configured game delay.\n\nClient files are read on each request: reload the portal or frame after editing, or rebuild into `client/` first if using a bundler. There is no automatic browser reload. Restart dev after changing `caisual.json`, `server.js`, or any server import (including shared client physics). Rooms restore from `.caisual-dev/`; changing the shape of their state may require creating a fresh room. A busy port error suggests a command with a currently available port.\n\nEvery `init` variant generates Node test and optional Playwright browser scripts. Playwright is a development dependency of the generated game, never of the CLI. See [Local dev](/docs/local-development#testing-with-a-browser) for installation and the full browser workflow.\n\nWhen building the client with a bundler, remember that `client/` is served as-is. Configure Vite, esbuild, or another bundler to write into that folder, for example `vite build --outDir client`, and use relative paths such as `base: \'./\'`.\n\nKeep loading the kit from the `<script type="module">` shown at the beginning of this guide, using `/__caisual/kit/v1.js`, when publishing on Caisual. That URL exists only in `caisual dev` and in the published game.\n\nIf the game has `server.js`, room state is handled locally and stored under `.caisual-dev/` in the game folder. If it has no `server.js`, room creation rejects with `no_server` and the single-player APIs still work.\n\nTo run from any other static server, install `@caisual/kit` from npm and import it with a bundler as `import { caisual } from \'@caisual/kit\'`. In that build standalone mode applies: `c.connected` is `false`, saves use local storage, `submit` returns `accepted: false`, leaderboards are empty, the daily seed is local, and room creation and joining reject with `offline`. The rest of the game logic does not need a different code path.\n\nAfter publishing with `npx @caisual/cli publish`, open the game from its caisual.com page: `c.connected` becomes `true` and every call goes to the portal.\n\n## Manifest\n\nDeclare `"overlay": { "version": 1 }` to get the standard overlay, with an optional `accent` colour. A standard game must declare at least one mode, and every mode needs `execution`, either `local` for a single-player run of exactly one player or `room` for a room backed by `server.js`. `label` names the mode in the standard menu and `instructions` adds one line under it. `roles[].label` and `boards[<id>].label` name roles and boards in the same UI, and `boards[<id>].periods` lists `daily`, `all-time` or both. A mode with matchmaking adds `matchmaking.defaults`, one value for every field of its `key`, so the overlay can start a search on its own.\n\n```json\n{\n "overlay": { "version": 1, "accent": "#397e83" },\n "players": { "min": 2, "max": 4 },\n "lobby": true,\n "boards": { "solo": { "source": "server", "label": "Best run", "periods": ["daily", "all-time"] } },\n "modes": [\n { "id": "practice", "execution": "local", "label": "Practice",\n "instructions": "One run against the clock.",\n "players": { "min": 1, "max": 1 }, "lobby": false },\n { "id": "duel", "execution": "room", "label": "Online",\n "matchmaking": { "key": ["pool"], "defaults": { "pool": "v1" }, "timeoutMs": 12000 } }\n ]\n}\n```\n\nA game without `overlay` keeps its historical flow and draws its own menus. Nothing else changes for it.\n\nNo manifest field is required for identity, saves, leaderboards, or the daily challenge. Use `boards` to make selected leaderboards server-only. A mode may override only `players: { min, max }` and `lobby`; omitted fields inherit the root configuration, and `mode: null` uses the root values. Matchmaking thresholds and room admission use this same resolution. For rooms, set `players` to the supported range and use `lobby`, `persistent`, `spectators`, `roles`, `teams`, and `modes` to describe the setup and lifetime. Set `voice` to `room`, `team`, or `proximity` to enable the corresponding voice mode, or omit it for `none`. A single-player game can keep `players` at `{ "min": 1, "max": 1 }`, `lobby` at `false`, and omit `server.js`. See [publish.md](./publish.md#caisualjson) for every field and the publishing steps.\n\n\n## Required game images\n\nEvery manifest must declare three different files inside `client/`. All three are required PNG, JPEG or WebP images, at most 2 MB (2,000,000 bytes) each:\n\n| Field | Exact size | Use |\n| --- | --- | --- |\n| `cover` | 1536x1024, 3:2 | Featured home card, game page and overlay boot background, social preview, mobile invitation |\n| `card` | 1024x1024, 1:1 | Secondary home cards and square invitation artwork |\n| `icon` | 1024x1024, 1:1 | Game window favicon, overlay, friends and parties |\n\n**No text inside any image:** no title, slogan, letters or numbers. The portal displays the game name. Keep the icon simple enough to read at a small size. The files must be different, including `card` and `icon`. Screenshots keep their existing rules.\n\nThe 3:2 and 1:1 ratios match native formats of widely used image generators. Export at the exact sizes above: Caisual does not crop, stretch or accept a size range. `caisual check` and publish validate the format from the file headers, exact dimensions and weight. The portal repeats these checks for direct publication. The absence of text is an editorial requirement, not an automatic check.\n\nAll three `caisual init` variants include small, real PNG examples at these sizes. Replace them with artwork for your game before sharing it. English (`en`) is always required in `languages`; the first language remains the default, for example `["it", "en"]`.\n';
1052
+ var kit_default = '# Caisual game kit\n\nThe kit gives a published game a stable player identity, cloud saves, leaderboards, a daily challenge seed, and multiplayer rooms with server-owned state.\n\nThe kit is a single JavaScript module with no dependencies. It never touches the page: the game keeps its own rendering, input, and UI.\n\n## Load the kit\n\nEvery published game can import the kit from its own origin, without a bundler and without an npm install:\n\n```html\n<script type="module">\n import { caisual } from \'/__caisual/kit/v1.js\';\n\n const c = await caisual.connect();\n console.log(c.player.name);\n</script>\n```\n\nGames built with a bundler can install the same module from npm:\n\n```sh\nnpm install @caisual/kit\n```\n\n```js\nimport { caisual } from \'@caisual/kit\';\n```\n\nBoth forms expose the same API. The module also sets `globalThis.caisual` for classic scripts that load it first.\n\nThe path `/__caisual/` is reserved on every game origin. Do not put game files under it.\n\n## Connect\n\n```js\nconst c = await caisual.connect();\n```\n\n`connect()` completes when the game is running inside caisual.com and has received its player identity, or after a short timeout when it is not. Calling it again returns the same promise.\n\n- `c.connected` is `true` inside caisual.com and `false` when the game runs on its own, for example from a local folder during development or when its files are copied elsewhere.\n- `c.player` is `{ id, name, guest, language, uiLanguage }`. `id` is stable for the player across sessions and across every version of the game. `name` is the account username, or a stable `Guest-XXXX` name derived from the player id. The four-character suffix uses `ABCDEFGHJKLMNPQRSTUVWXYZ23456789`, excluding I, O, 0 and 1; it helps distinguish guests but is not a unique identifier. `guest` is `true` for players without an account. `language` selects game strings; `uiLanguage` is the overlay locale. On first sign-in to an account without a player identity, the browser guest is adopted with its existing id, saves and scores. An account that already has a player identity uses that identity instead; guest data is not merged.\n- When not connected, `c.player` has `id: "local"`, `name: "Guest"`, `guest: true`, plus both language fields. If the host answered, its language information is kept; without a handshake, `language` is the normalized `navigator.language` (or `en`) and `uiLanguage` is its overlay fallback.\n\nDo not store the ticket or reimplement the handshake. The kit handles identity, renewal, and retries.\n\n## Game language and strings\n\n`c.player.language` is the game\'s language; `c.player.uiLanguage` is the overlay\'s locale. Use the former for game strings and formatting, or the latter when intentionally aligning text with the overlay.\n\nThe kit resolves the player\'s explicit portal language choice, otherwise `navigator.languages` in order, against `manifest.languages`. For each preference it tries the exact tag and then its parent tags; if none of the preferences match, it uses the game\'s first declared language. The result is always a normalized declared tag. For example, `ja-JP` with `languages: ["en", "ja"]` selects `ja`, while `uiLanguage` is `en`. The overlay supports English, Italian, Spanish, French, German and Portuguese; it keeps regional tags in those families, such as `pt-BR`, and falls back to `en` for other languages. Games may declare languages outside these six.\n\nA localized portal URL counts as a language choice. The language selector remembers explicit choices, including English; without either, the browser\'s ordered preferences apply. The handshake keeps its legacy `language` field for existing kits, and also sends `uiLanguage`, `languagePreferences` and `gameLanguages`. The kit resolves the game language, including when player services fail after a successful handshake.\n\nWithout a handshake, no manifest is available: `language` is the raw preference from `navigator.language`, normalized as a BCP 47 tag, or `en` if invalid or unavailable. It is not restricted to declared game languages. `uiLanguage` uses the overlay fallback. In `caisual dev`, `?lang=ja` selects `ja` when the manifest declares it, while the overlay stays in English. Without `?lang=`, dev uses `navigator.languages` in order for the game.\n\nPut all game UI strings in flat JSON dictionaries named `client/i18n/<lang>.json`. Use canonical BCP 47 filenames, for example `en.json`, `it.json`, `pt.json`, `pt-BR.json`. Every value is a string; keys are identical across dictionaries. Text can contain named placeholders such as `{n}`.\n\nDeclare supported languages in `caisual.json`, always including `en`. English is required alongside the game\'s own languages; the first entry is the default and may be another language. This complete manifest supports a local game:\n\n```json\n{\n "manifest": 1,\n "cover": "cover.png",\n "card": "card.png",\n "icon": "icon.png",\n "id": "three-lights",\n "name": "Three Lights",\n "platform": "both",\n "languages": ["en", "it"],\n "overlay": { "version": 1 },\n "modes": [{\n "id": "solo",\n "execution": "local",\n "label": { "en": "Solo", "it": "Da solo" },\n "instructions": { "en": "Light up three lights.", "it": "Accendi tre luci." }\n }]\n}\n```\n\n`client/i18n/en.json`:\n\n```json\n{ "score": "Lights: {n} / 3", "light": "Light up", "done": "All lit up!" }\n```\n\n`client/i18n/it.json`:\n\n```json\n{ "score": "Luci: {n} / 3", "light": "Accendi", "done": "Tutte accese!" }\n```\n\nLoad once during setup, before `c.session.ready()`. The returned function is synchronous and can be used in every draw call:\n\n```html\n<!doctype html>\n<html>\n<head><meta charset="utf-8"><title>Three Lights</title></head>\n<body style="margin:0;min-height:100dvh;display:grid;place-content:center">\n <p id="score"></p>\n <button id="light" disabled></button>\n <script type="module">\n import { caisual } from \'/__caisual/kit/v1.js\';\n const c = await caisual.connect();\n const t = await c.text();\n document.documentElement.lang = c.player.language;\n const score = document.querySelector(\'#score\');\n const light = document.querySelector(\'#light\');\n let n = 0, blocked = false, active = !c.session.capabilities.overlay;\n function draw() {\n score.textContent = n === 3 ? t(\'done\') : t(\'score\', { n });\n light.textContent = t(\'light\');\n light.disabled = blocked || !active || n === 3;\n }\n c.overlay.onChange((view) => { blocked = view.inputBlocked; draw(); });\n c.session.onChange((session) => {\n if (session.kind === \'local\' && session.status === \'playing\') n = 0;\n active = !c.session.capabilities.overlay || (session.kind === \'local\' && session.status === \'playing\');\n draw();\n });\n light.onclick = () => {\n n += 1;\n if (n === 3 && c.session.current.kind === \'local\') c.session.finish();\n draw();\n };\n draw();\n c.session.ready();\n </script>\n</body>\n</html>\n```\n\n`c.text(): Promise<Text>` makes one request to the game\'s own origin, tied to the version currently open. Caisual and `caisual dev` read the matching files and merge them per key: `pt-BR` then `pt` then the manifest\'s default language. Longer tags fall back through their parent tags, such as `zh-Hant-TW`, `zh-Hant`, `zh`. The default file is tried once. If no file contains a key, `t` returns that key. Empty strings are valid translations.\n\nConcurrent and later `c.text()` calls share the same promise and translator for that connection. There are no dependencies, eager downloads, or per-call network requests. Missing files, invalid dictionaries and network failures do not prevent startup. If the Caisual text service is unavailable, for example on a plain static host outside Caisual, the translator returns keys; it does not probe other URLs. Use `caisual dev` to preview the complete convention.\n\n`t(\'score\', { n: 3 })` replaces named placeholders with strings or numbers. An omitted placeholder stays unchanged, such as `{n}`. The result is plain text, with no HTML processing, plural rules or automatic translation. Set `textContent` or draw it on the canvas; do not insert it as HTML. A new document gets the host\'s current language and a fresh translator.\n\nMode `label` and `instructions`, role `label`, and leaderboard `label` accept a string or a language-to-text object, and resolve from `uiLanguage` in the overlay using the same fallback chain. `name`, `description` and `tags` keep their existing forms. Existing single-string labels stay unchanged. See [manifest languages and validation](./publish.md#game-translations) for CLI checks and migration from `language`.\n\n## Sessions and the standard overlay\n\nA game that declares `overlay` in `caisual.json` is a standard game: it runs full screen and Caisual draws everything around it. The opening menu, the mode choice, the lobby with roles, teams and ready, invitations, friends and parties, matchmaking, spectators, leaderboards, voice, the end of a match and Play again belong to the platform. The game keeps the field, its own HUD and its own settings.\n\n```json\n{ "overlay": { "version": 1, "accent": "#397e83" } }\n```\n\nTwo objects appear on the connection. `c.session` says which session the game is in, `c.overlay` says when the platform is on top of it.\n\n```js\nconst c = await caisual.connect();\n\nconst stopSession = c.session.onChange((session) => {\n detachGameListeners();\n if (session.kind === \'idle\') return showAttractScene();\n if (session.kind === \'local\') return showLocalRun(session.mode, session.status);\n attachGameListeners(session.room, session.kind === \'watch\');\n draw(session.room.state);\n});\n\nconst stopOverlay = c.overlay.onChange(({ inputBlocked, reservedRects }) => {\n clearHeldKeys();\n setInputEnabled(!inputBlocked);\n placeHudOutside(reservedRects);\n});\n\nawait loadAssetsAndChosenView();\nc.session.ready();\n```\n\n### The session\n\n`c.session.current` reads the session at once. `onChange` repeats the current value immediately to every new listener, returns a function that removes it, and then reports attaches, detaches and the end of a local run. It never fires for a move or a roster change: those stay on the room listeners.\n\n- `{ kind: \'idle\' }`: no session. Show an attract scene, not a menu.\n- `{ kind: \'local\', id, mode, status }`: a run of a mode declared with `"execution": "local"`. `status` is `playing` or `ended`.\n- `{ kind: \'room\', id, room }`: `room` is the `Room` documented below, already attached.\n- `{ kind: \'watch\', id, room }`: `room` is a `Spectate`. Draw it read only.\n\n`id` changes on every attach, so a second local run is distinguishable from the first.\n\n`c.session.ready()` says the game has loaded its assets and installed its listeners. Call it once, at the end of setup: until then the overlay waits instead of starting a session under a game that is still downloading. It is idempotent.\n\n`c.session.finish()` ends a local run and shows the compact end bar with a Play again action. It applies only to `kind: \'local\'`: on a room it fails with `not_local` and on idle it does nothing. The result of an online match comes from the server, never from `finish()`.\n\n`c.session.capabilities` is `{ local, rooms, overlay, requestRole }`. Outside caisual.com `overlay` and `rooms` are `false` while `local` stays `true`, which is the signal to run the game\'s own offline fallback. No fake room is created.\n\nTwo front ends of the same game share one session. Switching view does not call `ready()` again and does not detach the room: the new renderer reads `c.session.current` and draws.\n\n### The overlay on top\n\n`c.overlay.onChange` repeats the current geometry immediately, then on every change:\n\n- `inputBlocked` is `true` while a panel is open. Release held keys and stop reading input, but keep simulating: opening a panel never pauses a room.\n- `reservedRects` is an array of up to eight `{ x, y, width, height }` rectangles in CSS pixels of the game viewport. Keep the game\'s own HUD out of them, and nothing else: never resize, move, or letterbox the field because of them. The overlay sits on top of the game and the game must not shift when a bar or a pill appears. The field under a closed overlay stays visible and clickable.\n\nThe complete `OverlayView` value is `{ inputBlocked, reservedRects, safeArea?: { top, right, bottom, left }, shortcutEnabled? }`.\n\nThe host measures `safeArea: { top, right, bottom, left }` in CSS pixels of the game viewport, accounting for the iframe\'s position, borders and scale. It updates after viewport changes, including rotation. Use these values for HUD margins: `env(safe-area-inset-*)` inside the iframe usually reads zero. The kit also sets `--caisual-safe-top`, `--caisual-safe-right`, `--caisual-safe-bottom` and `--caisual-safe-left` on the game document root:\n\n```css\n.hud {\n top: max(16px, var(--caisual-safe-top, 0px));\n left: max(16px, var(--caisual-safe-left, 0px));\n}\n```\n\n`safeArea` is optional for compatibility with older hosts; treat a missing value as four zeros. Older kits keep receiving their supported geometry fields. During the opening screen, `inputBlocked` is true from the first view, including before the first host measurement.\n\n\n`c.overlay.open(panel)` asks the platform to open one of `home`, `room`, `invite`, `friends`, `voice`, `boards`. It is a request, not a permission: it creates no room and grants nothing. Outside caisual.com it does nothing.\n\nShift+Tab from the field opens the menu and Escape closes it. Text fields inside the game keep their own shortcut.\n\n### What a standard game no longer builds\n\nRemove these and let the overlay do them:\n\n- a start menu with Create, Join or a code field;\n- invitation links, copy buttons and share sheets;\n- the lobby: roster, ready, role and team pickers, the Start button;\n- a matchmaking screen with its cancel button;\n- a friends or party list;\n- voice buttons;\n- a leaderboard screen;\n- an Exit or Back to Caisual button;\n- a Play again button after a match.\n\nThe end bar recognizes optional standings and winners; the game may draw additional result details inside the field. `c.room.create`, `join`, `match` and `watch` stay available for a game that wants its own entry point: under a standard overlay the room they return becomes the current session and the standard controls follow it. Do not attach a second room controller from a second renderer.\n\n### Full screen\n\nA standard game fills the window. `html`, `body` and the game surface are 100% of the viewport: no maximum width, no header, no footer, no editorial frame, and no document scrolling at 1366x768 or 390x844, safe areas included. Only the HUD and compact controls sit over the scene.\n\nAim for the playable field to cover at least 70% of the visible area on desktop and 60% on mobile, counting only what shows or controls the game. A board with a fixed aspect ratio cannot always reach that: a square board on a 1366x768 window tops out near 56% before any HUD. That is the declared geometric exception: the board must then fill at least 90% of the largest rectangle that fits the area left free, and the HUD must have a stated ceiling, typically 48 to 64 pixels on desktop and about 160 pixels of controls on a phone.\n\n### Resume\n\nCaisual keeps one resume reference per game and per player, in a save key it owns. Leaving through the overlay with Leave for now stores the room code and detaches without giving up the seat; the standard menu then offers Resume, which rejoins from that code. Leave room removes the reference and gives up the seat, and so does a terminal room end. A `finished` room waiting for a rematch keeps its resume reference. A network drop keeps it.\n\nA game does not read or write that key, and does not build its own Resume button. A reference that is no longer valid returns the service error and the overlay explains it: it does not retry forever. Resume carries the room code, not a promise to reopen the same published version of the game.\n\n### Voice and leaderboards\n\nIn a standard game the overlay\'s voice panel carries Join, Leave, Mute, and the list of who is in the call, with the click the browser requires. A standard game does not draw its own voice buttons. The `room.voice` API below remains for games without the standard overlay and for server-side gain and proximity rules.\n\nLeaderboards are read by the overlay from the published manifest, using the boards and periods declared there. The overlay reads the official verified scores after a submission and offers Refresh: a game does not need a board screen. Scores are still submitted by the game or, better, by `server.js`.\n\n### Known gaps\n\nThree things are deliberately not in this version, and a game should not work around them:\n\n- resolving the original game version behind a persistent Resume;\n- inviting one friend straight into a room, as opposed to a party;\n- matchmaking for a whole group at once.\n\n## Daily challenge\n\n`c.daily` describes the day selected when the connection opened. `day`, `seed` and `expiresAt` stay fixed for that connection, including across UTC midnight. `expiresAt` is the next UTC midnight in milliseconds on the server clock.\n\n```js\nc.daily.day;\nc.daily.seed;\nc.daily.expiresAt;\nconst random = c.daily.rng();\nconst stop = c.daily.onChange(({ day, seed, expiresAt }) => {\n offerNextDailyRun({ day, seed, expiresAt });\n});\n```\n\n`onChange` reports a new `{ day, seed, expiresAt }` when the current UTC day changes. It returns an unsubscribe function and does not immediately replay the initial value. A suspended browser or a connection failure can delay the notification; the kit retries and reports the latest day when it can. The event does not mutate `c.daily` or reseed either generator. `rng()` always creates a fresh generator from the connection\'s original seed, and `random()` keeps advancing the original shared generator. Use the event\'s context explicitly for a new daily run, or open a new document. Repeated `connect()` calls return the same connection.\n\n`day` is a UTC date such as `"2026-09-04"`; `seed` is an unsigned 32-bit integer shared by all players of that game on that day. Both generators produce values in [0, 1). `c.time.now()` is in milliseconds aligned with the portal clock. Without a connection, daily data uses the local clock and hostname, with the same listener and generator behavior.\n\nOn the server, `room.daily` is `{ day, seed, expiresAt }`, fixed at **room creation**, persisted across sleep and rematches. Its seed matches a client connection opened for that game on the creation day. A client connected yesterday can therefore have a different `c.daily` from a room created today: send the room\'s daily context through game state when rendering its course. To switch a persistent or rematched room to a new daily course, create a new room.\n\nBoards accept `"day": "submit" | "start"` in the manifest. Omission means `submit`, preserving submission-day scoring. With `start`, which requires `source: "server"`, submit `room.board.submit(player, \'run\', score, { day: room.daily.day })`. An explicit `day` implies a daily score. It must equal the room\'s creation day and arrive no later than **10 minutes after `room.daily.expiresAt`**, including the exact boundary. A run started at 23:58 and submitted at 00:04 is attributed to its starting day. The all-time score is a separate submission without `day` or `daily`.\n\n| Error code | Meaning |\n| --- | --- |\n| `board_day_required` | A daily score on a start-day board omitted `day`. |\n| `board_day_mismatch` | `day` differs from `room.daily.day`. |\n| `board_day_expired` | More than 10 minutes have elapsed after the room\'s daily expiry. |\n| `board_day_policy` | An explicit day was used on a submit-day board or with `daily: false`. |\n\nThese server calls throw synchronously; handle expected errors inside the callback if the game should continue. Accepted scores keep their assigned day and submission timestamp across delayed writes and retries. Client scores continue to use the day when the portal receives them and cannot choose a day.\n\n`caisual dev --day YYYY-MM-DD` fixes the simulated day and seed for new connections and rooms. Clocks stay real: `expiresAt` is the next **real** UTC midnight at connection or room creation, so comparing it with `c.time.now()` or `room.time.now()` remains valid even for a simulated date. Restored rooms keep their original daily context. Restart dev and reload to change the flag; create a new room for the newly selected day.\n\n## Saves\n\nEach player has up to 32 saves per game. A save is any JSON value up to 256 KB when serialized.\n\n```js\nawait c.save.set(\'slot1\', { level: 3, coins: 120 }); // -> { key, bytes, updatedAt }\nconst data = await c.save.get(\'slot1\'); // -> the value, or null\nawait c.save.remove(\'slot1\');\nconst saves = await c.save.list(); // -> [{ key, bytes, updatedAt }]\n```\n\n- Keys use 1 to 32 characters: lowercase letters, digits, `_` or `-`, starting with a letter or digit.\n- `updatedAt` is a millisecond timestamp.\n- Saves are per player and per game. Another game cannot read them.\n- When not connected, saves go to the browser\'s local storage on the game origin.\n\nErrors reject the promise with an `Error` whose `code` is one of `invalid_request`, `not_found`, `save_limit`, `payload_too_large`, `rate_limited`, `invalid_ticket`, `internal_error`, or `offline`.\n\n## Leaderboards\n\nA leaderboard is identified by a board id chosen by the game. Scores are non-negative integers and higher is better. Each player keeps one entry per board, and one per board per day for daily boards: the best score is kept.\n\n```js\nconst result = await c.board.submit(\'main\', 1234);\n// -> { accepted: true, best: 1234, rank: 7, day: null, verified: false }\n\nconst daily = await c.board.submit(\'main\', 1234, { daily: true });\n// -> { accepted: true, best: 1234, rank: 7, day: "2026-09-04", verified: false }\n\nconst top = await c.board.top(\'main\', { daily: true, limit: 10 });\n// -> { day: "2026-09-04", entries: [{ rank, name, score, guest, me, verified }], me: { rank, score, verified } | null }\n```\n\n- Board ids use the same format as save keys.\n- `submit` never rejects because of connectivity. When the game is not connected it resolves `{ accepted: false, reason: "offline" }`.\n- `best` is the score kept for this player after the submission, which can be higher than the submitted one.\n- `rank` counts players with a strictly higher score. Ties are ordered by who reached the score first.\n- Accounts and guests are ranked separately. `top()` returns account players by default; pass `guests: true` to list guests instead. `me` always refers to the current player within their own category, even beyond `limit`.\n- `limit` is 1 to 100 and defaults to 10.\n- Pass `day: "2026-09-06"` to `top()` to read exactly that UTC day, even after midnight. A day implies the daily filter. A date that is not a real `YYYY-MM-DD` is rejected.\n- `verified` is `true` when the kept score came from the room server. Browser scores cannot replace a verified score.\n- Add `"boards": { "main": { "source": "server" } }` to `caisual.json` for a server-only board. It accepts scores only from `room.board.submit` in `server.js`.\n- An omitted board has `source: "client"`. Browser submissions keep working for existing games.\n- A browser submission to a server-only board rejects with `board_server_only`.\n\n### Verified scores in a single-player game\n\nA verified solo game uses a `room` mode with one place. The server validates each move and computes the score. This complete example accepts each of three cells once; a client cannot submit a score or claim the same cell twice. The standard overlay provides Play again. The optional client button below calls the same `restart()` API when no overlay is available.\n\n`caisual.json`:\n\n```json\n{\n "manifest": 1,\n "cover": "cover.png",\n "card": "card.png",\n "icon": "icon.png", "id": "verified-cells", "name": "Verified Cells", "platform": "both",\n "languages": ["en"], "overlay": { "version": 1 },\n "modes": [{ "id": "solo", "execution": "room", "players": { "min": 1, "max": 1 }, "lobby": false }],\n "boards": { "run": { "source": "server", "day": "start", "label": "Cells", "periods": ["daily", "all-time"] } }\n}\n```\n\n`server.js`:\n\n```js\nimport { defineGame } from \'@caisual/kit/server\';\n\nfunction reset(room) {\n room.state = { cells: [false, false, false], score: 0, daily: { ...room.daily } };\n}\nexport default defineGame({\n tickRate: 0,\n onCreate: reset,\n onRestart: reset,\n onMessage(room, player, message) {\n if (room.status !== \'playing\' || player.role === \'spectator\') return;\n const cell = message?.cell;\n if (!Number.isInteger(cell) || cell < 0 || cell > 2 || room.state.cells[cell]) return;\n room.state.cells[cell] = true;\n room.state.score += 1;\n if (room.state.score !== 3) return;\n const result = { standings: [{ playerId: player.id, score: room.state.score }], winners: [player.id], unit: \'points\' };\n room.board.submit(player, \'run\', room.state.score);\n if (room.time.now() > room.daily.expiresAt + 10 * 60_000) {\n // La corsa scaduta conserva il record assoluto; una stanza nuova scegliera\' il nuovo giorno.\n room.end({ ...result, data: { dailyExpired: true } });\n return;\n }\n room.board.submit(player, \'run\', room.state.score, { day: room.daily.day });\n room.end(result, { rematch: true });\n },\n});\n```\n\n`client/index.html`:\n\n```html\n<!doctype html>\n<html lang="en">\n<meta charset="utf-8">\n<meta name="viewport" content="width=device-width, initial-scale=1, viewport-fit=cover">\n<title>Verified Cells</title>\n<style>body{margin:0;min-height:100dvh;display:grid;place-content:center;gap:12px;background:#12252b;color:#f2faf3;font:16px system-ui}button{min-width:48px;min-height:48px}</style>\n<p id="score"></p><div id="cells"></div><button id="again" hidden>Play again</button>\n<script type="module">\n import { caisual } from \'/__caisual/kit/v1.js\';\n const c = await caisual.connect();\n const score = document.querySelector(\'#score\'), cells = document.querySelector(\'#cells\'), again = document.querySelector(\'#again\');\n let room = null, blocked = false, stops = [];\n function draw() {\n cells.replaceChildren();\n if (!room) { score.textContent = \'Choose Play to start.\'; again.hidden = true; return; }\n score.textContent = `${room.state.score} / 3`;\n room.state.cells.forEach((claimed, cell) => {\n const button = document.createElement(\'button\'); button.textContent = claimed ? \'\u2713\' : String(cell + 1);\n button.disabled = blocked || room.connection !== \'connected\' || room.status !== \'playing\' || claimed;\n button.onclick = () => room.send({ cell }); cells.append(button);\n });\n again.hidden = c.session.capabilities.overlay || room.status !== \'finished\';\n again.disabled = blocked || room.connection !== \'connected\';\n }\n again.onclick = () => { again.disabled = true; room.restart(); };\n c.overlay.onChange((view) => { blocked = view.inputBlocked; draw(); });\n function attach(next) {\n stops.forEach((stop) => stop()); stops = []; room = next;\n if (room) stops.push(room.onState(draw), room.onStatus(draw), room.onConnection(draw));\n draw();\n }\n c.session.onChange((session) => attach(session.kind === \'room\' ? session.room : null));\n c.session.ready();\n if (!c.session.capabilities.overlay && c.connected) attach(await c.room.create({ mode: \'solo\' }));\n</script>\n</html>\n```\n\n`room.board.submit(\'run\', score, { day: room.daily.day })` is also accepted in a solo room: the sole non-spectator member is implicit. In multiplayer, always pass `player` or its id first; omitting it throws `board_player_required`. `room.board.submit` queues the score; use `room.onScoreQueued` and the overlay\'s board refresh to observe it, not a state transition as a storage receipt. A rematch keeps the room\'s creation day; a new room selects the current day.\n\n## Device\n\n`c.device` contains the browser and device report collected while `connect()` runs:\n\n```ts\ninterface DeviceReport {\n webgl2: boolean;\n webgpu: boolean;\n wasm: boolean;\n threads: boolean;\n isolated: boolean;\n gpu: \'hardware\' | \'software\' | \'none\';\n memoryMb: number | null;\n cores: number | null;\n mobile: boolean;\n tier: \'low\' | \'mid\' | \'high\';\n}\n```\n\nUse capability fields to choose a renderer, then use `tier` to reduce pixel ratio and quality on smaller devices:\n\n```js\nconst renderer = c.device.webgpu\n ? createWebGpuRenderer()\n : createWebGl2Renderer();\n\nconst pixelRatio = c.device.tier === \'high\' ? devicePixelRatio : 1;\nconst quality = c.device.tier === \'low\' ? \'low\' : \'high\';\nrenderer.configure({ pixelRatio, quality });\n```\n\nThe probe takes at most 1.5 seconds. `memoryMb` and `cores` are `null` when the browser does not expose them. The report stays in the browser and is not saved or sent to Caisual.\n\n### Two front ends, one game\n\nKeep one `client/index.html`, one game ID and one server. With `platform: "both"`, choose separate front ends in that entry without navigating or adding another iframe:\n```js\nimport { caisual } from \'/__caisual/kit/v1.js\';\nconst c = await caisual.connect();\nlet preference = null;\ntry { preference = localStorage.getItem(\'layout\'); } catch {}\nconst touch = preference === \'touch\' || (preference !== \'desktop\' && (c.device.mobile || matchMedia(\'(pointer: coarse)\').matches));\nconst screen = touch ? await import(\'./touch/main.js\') : await import(\'./desktop/main.js\');\nscreen.mount({ c, root: document.querySelector(\'#app\') });\n```\nOffer a manual layout choice, persist it when storage is available, and keep rules and room connections shared. Both front ends use relative asset paths inside `client/`.\n\n## Game version changes\n\nNew games use the current version. Existing rooms retain their version for their whole life, including invitations, typed codes, friends, spectators and Resume. Matchmaking never mixes versions; an accepted reservation can finish on its original version. Saves, boards and `room.shared` remain per game, with data compatibility handled by the creator. See [Versions and rollback](./publish.md#versions-and-rollback).\n\nA room create or match operation rejects with `version_outdated` and `currentVersion` when the loaded game is no longer current. A join, watch or reconnect rejects with `version_mismatch` and `roomVersion` when the room uses another version. The standard overlay shows **This game was updated** with **Reload game** for outdated pages; a mismatched code or Resume reloads the portal with the room invitation, preserving watch intent for spectators.\n\nCustom menus can subscribe with `c.room.onError(listener)`, which returns an unsubscribe function. The error has `code`, `message`, and the applicable version number. The operation also rejects with that error. `c.room.reload()` reloads the portal on the current game after `version_outdated`, or on the last mismatched room after `version_mismatch`. Show a button calling it after either error. This works without the standard overlay.\n\n`caisual dev` always uses game version 1 and does not simulate version changes.\n\n## Rooms\n\nA room brings players into the same running game. Creating and joining require a published `server.js`; single-player games can ignore `c.room`.\n\nIn a standard game the overlay creates, joins, matches and watches on the player\'s behalf, and hands the game the room through `c.session`. The calls below stay available, and their result becomes the current session. Read them for what the room object offers; do not rebuild the entry screens around them. An optional [MatchResult](#recognized-match-results) in `room.end` lets the overlay display winners and standings while `room.result` keeps the original JSON.\n\n```js\nconst c = await caisual.connect();\n\nc.room.invited; // invitation code from the game page, or null\n\nconst room = await c.room.create({ mode: null });\n// Or join the invitation that opened the game:\nconst invitedRoom = await c.room.join();\n// Or enter a code supplied by the player:\nconst codedRoom = await c.room.join(\'ABC234\');\n\nroom.code;\nroom.seed; // unsigned 32-bit integer fixed for this room\nroom.tickRate;\nroom.latency;\nroom.invite(); // { code: "ABC234", url: "https://caisual.com/r/ABC234" }\n```\n\nPass a mode id from the manifest to `create({ mode })`, or `null` to use the root configuration. Optional `players` and `lobby` on that mode replace the root values; joining keeps the configuration of the room being joined. `join()` uses `c.room.invited`; without an invitation, pass the six-character code explicitly. A standard game does not need `invite()`: the overlay owns the invitation panel and the copy action.\n\n### Crew\n\nThe kit automatically reports the player\'s current room to the Caisual portal, so the player\'s friends can join with one click. The game does not need to send or handle anything for this. There is no `c.crew` API in this version. In a standard game the friends and party list is a panel of the overlay, so there is nothing to draw either.\n\n### Matchmaking\n\nUse `c.room.match()` to find players who requested the same mode and key. The key must contain exactly the fields declared by that mode\'s `matchmaking.key` in `caisual.json`.\n\n```js\nconst room = await c.room.match({\n mode: \'daily\',\n key: { day: c.daily.day, stage: 3 },\n onWaiting({ players, min, max }) {\n showQueue(`${players}/${max} players, ${min} required`);\n },\n});\n```\n\nMatchmaking uses the selected mode\'s resolved `players` and `lobby`, and that mode\'s `matchmaking.timeoutMs`. A room opens as soon as the queue reaches the resolved `players.max`. When `matchmaking.timeoutMs` expires, it also opens if at least `players.min` players are waiting. Otherwise the promise rejects with `no_match`, and the game should offer the player another option. A new search first tries to fill a matching room that is already open and can still accept players.\n\nPass an `AbortSignal` as `signal` to let the player cancel a search. Cancellation rejects with `cancelled`. In a standard game the search screen, its Cancel button and the lobby that follows are the overlay\'s: declare `matchmaking.defaults` in the mode and the player can start a search from the standard menu without the game passing a key.\n\nRoom status is one of:\n\n- `lobby`: players are joining and choosing their setup.\n- `countdown`: the lobby has accepted `start()` and play begins at the announced server time.\n- `playing`: the game server is running the match.\n- `finished`: the match is over and the room is waiting for a rematch, with sockets open and `room.result` available.\n- `ended`: the match or connection has ended. `room.result` contains the result last reported by the room. A definitive connection closure uses `{ closed: 4003 }` when the player was kicked, `{ closed: 4004 }` when the room ended, `{ closed: 4005 }` when the published version closed, or `{ closed: 4006 }` when the same player opened the room in another tab.\n\nThe current lobby data is available directly:\n\n```js\nroom.players; // [{ id, name, guest, role, team, ready, connected }]\nroom.you; // this player\'s id\nroom.host; // the current host\'s id, or null\n\nroom.ready(true);\nroom.setRole(\'captain\');\nroom.setTeam(1);\n\nif (room.you === room.host) room.start();\n```\n\nIn a standard game the overlay calls these four for the player: read `room.players` to draw the field, not to build a roster panel. `ready`, role, team, and `start()` are lobby actions. Starting requires the host, every connected player to be ready, and the player, role, and team minimums from the manifest. Calling `start()` begins a three-second countdown. A role or team change clears that player\'s ready state. The built-in `spectator` role is still a player slot for setups such as a shared screen with phone controllers. Use `watch()` for someone who only observes and does not occupy a player slot.\n\n### Rematch in the same room\n\nThe server opts in per match with `room.end(result, { rematch: true })`. The room becomes `finished`, keeps its code, members, state and open connections, and exposes the result through `room.result` and `onStatus(\'finished\', result, at)`. Voice and spectator streams remain connected. `room.end(result)` or `{ rematch: false }` still ends the room permanently with status `ended` and close code 4004.\n\nWith `rematch: true` in a multiplayer room, each connected player calls `room.restart()` once to become ready for the rematch. In `finished`, `room.players[].ready` means rematch readiness. Once every connected non-spectator player is ready and the mode\'s `players.min` is met, the host calls `room.restart()` again to confirm. The first host call only registers readiness. A host with the built-in `spectator` role does not register readiness and can confirm once the players are ready. Repeated non-host calls do nothing. Calling outside `finished` fails with `rematch_unavailable`; an early host confirmation reports `players_not_ready` through `room.onError`.\n\nThe standard overlay handles these calls with Play again, the readiness count and names, and Start rematch for the host. Games without the overlay can call `restart()` directly. No member is removed for declining. With `rematch: true`, there is no automatic start after a departure: the current host must confirm.\n\nAt confirmation, the kit clears the result to `null` and all readiness flags, then calls optional `onRestart(room)` with status `lobby` when the mode has a lobby, or `playing` otherwise. **The kit does not reset `room.state`.** Reset match data in `onRestart`, keeping series scores or other data as needed. With a lobby, players choose their setup and get ready again before the normal countdown and `onStart`. Without a lobby, `onStart` follows `onRestart` immediately. Clients receive the new state and an `onStatus` transition with a null result. The room identity, seed and tick sequence are retained; `session.onChange` does not create a new session for a rematch. Listen to `room.onState` and `room.onStatus`.\n\n### Fast rematches and solo rooms\n\nThe existing `rematch: true` flow above is unchanged for multiplayer rooms. To shorten it, use:\n\n```js\nroom.end(result, { rematch: { keepSetup: true, autoStart: true } });\n```\n\nBoth flags default to `false`. `keepSetup` restores the previous readiness flags, keeps roles and teams, and skips the lobby: `onRestart` runs with status `countdown`, followed by the usual three-second countdown and `onStart`. The result clears before `onRestart`. Even a mode without a lobby gets this countdown. Readiness during `finished` is still consent for the next match, separate from the previous setup. Player, role and team minimums are rechecked during the countdown; if they fail, the room returns to the lobby so the setup can be repaired.\n\n`autoStart` starts the rematch when all connected non-spectator players have accepted and the mode\'s minimum is met, including after a departure leaves that condition satisfied. The overlay shows the readiness count and names without a host confirmation button or wait-for-host text. With `autoStart`, the host can also call `restart()` again to confirm early once at least `players.min` connected non-spectator players have accepted. Members who have not accepted remain in the room and join the next phase; they are not removed. With `autoStart` alone, the next phase is the usual lobby or immediate play; combine it with `keepSetup` to skip another ready/start cycle.\n\nA room whose resolved `players.max` is `1` is a solo room, including when it inherits `lobby: true`: it starts on entry and shows no lobby, invitation, code or wait-for-host prompt. In `finished`, one `room.restart()` immediately runs `onRestart`, then `onStart`, without consent or host confirmation or a countdown. The standard Play again button makes that one call. After a terminal end it creates a new solo room without opening or copying an invite. No additional manifest field is needed; `caisual init` without `--multiplayer` still creates a local game.\n\nThe waiting rules are:\n\n- All pending `room.schedule` handlers are cancelled when the match finishes, including handlers already due in the same batch. Scheduling during `finished` has no effect. Schedule new work in `onRestart` or `onStart`. Tick callbacks stop immediately; game input during the wait is discarded, and queued continuous input is cleared on the client.\n- `onEnd` runs once for the completed match, with status `finished` and its result. A subsequent timeout only closes the room and does not call `onEnd` again. Restart does not resubmit or clear pending board scores: each submission is drained once from the existing score queue. The game must not submit the previous match\'s scores again in `onRestart`.\n- Disconnecting clears that member\'s readiness and transfers the host to the oldest connected member. Disconnected players do not count toward readiness or the minimum. The usual 60-second reconnection grace applies; persistent members keep their seats after it. Rejoining requires a new readiness call. Connection and departure callbacks continue during the wait.\n- New members may join by invitation during the wait, up to the resolved `players.max`, and start unready. A lobby mode therefore reopens admission while `finished`; without a lobby, admission continues as during play. Disconnected members still occupy seats until removed. Roles and teams retain their existing capacity rules.\n- `watch()` spectators do not vote or occupy seats and keep following the same delayed stream across matches. Members with role `spectator` occupy a seat but do not vote or count toward the rematch minimum.\n- The room closes with 4004 exactly two minutes after entering `finished` if no restart is confirmed, retaining the last result. Readiness, joins and pings do not extend this deadline. It also applies to persistent rooms and survives sleep or restoration; it does not depend on active ticks.\n\n`finished` is a new status, not a terminal connection state. Older room clients receive the unfamiliar status and keep their sockets open instead of taking their `ended` cleanup path; they have no `restart()` control, and older overlays may retain their previous screen. Use an updated kit for games opting into rematches, and explicitly handle `finished` in game status listeners.\n\nThe server owns room state. Read it and react to updates, but do not assign to it or mutate nested values from the browser:\n\n```js\ndraw(room.state);\n\nconst stopState = room.onState((state, tick, serverTime) => {\n draw(state);\n});\n\nconst stopPlayers = room.onPlayers((players) => updateLobby(players));\nconst stopStatus = room.onStatus((status, result, at) => showStatus(status, result, at));\n\nstopState();\nstopPlayers();\nstopStatus();\n```\n\n`room.tick` identifies the latest state. The kit applies structural updates in order for both tick-based and event-only servers. Full state is sent on entry, reconnection or resync when an update does not match the current tick; normal updates, including every hundredth tick, remain diffs. `room.serverTime()` returns milliseconds aligned with the room clock and is kept current by a ping every five seconds.\n\n`room.tickRate` is the current effective server frequency, including reductions caused by the CPU budget; zero means event-only. Updates arrive with state diffs and snapshots, including an empty diff when only the frequency changes. `room.latency` is the smoothed round-trip time in milliseconds, or `null` before the first pong and after a dropped connection until the next pong. Each pong uses 20% of the new RTT and 80% of the previous estimate, with the first sample used directly. Read the property when drawing network status; there is no `onLatency` listener. Spectators expose the same properties; their tick rate follows the delayed state stream.\n\nSend JSON input to `onMessage` in the server definition, and receive JSON sent or broadcast by the server:\n\n```js\nroom.send({ type: \'fire\', target: 3 });\n\nconst stopMessages = room.onMessage((message) => {\n showEvent(message);\n});\n```\n\nThe kit numbers outgoing inputs in increasing order. It automatically reconnects temporary failures with delays of 1, 2, 4, then 8 seconds, for at most the room\'s 60-second grace period. Each attempt gets a fresh room token. A successful reconnect replaces local state with a full server state. `room.send` calls while reconnecting throw an error with `code: "offline"`.\n\nUse `room.input(value)` for continuous controls, including calls from every animation frame:\n\n```js\nroom.input({ type: \'move\', x: axisX, y: axisY });\nroom.onError(({ code }) => {\n if (code === \'rate_limited\') showInputWarning();\n});\n```\n\n`input` copies and keeps only the latest JSON value in one slot. It coalesces updates and sends at most 20 times per second, or at the effective `room.tickRate` when that is lower and positive. With `tickRate: 0`, it still sends at most 20/s. It also waits for budget used by `send`. Values with the same JSON serialization are not resent on the same connection. Combine independent controls into that one value; there is no channel option. On the server it is an ordinary message passed unchanged to `onMessage`, exactly like `send`, with no extra envelope.\n\nDuring reconnection, `input` accepts updates without throwing `offline`. After the new welcome it sends only the latest value, even if it was sent on the previous connection. It never replays intermediate values or old commands. Use `send` for individual actions such as firing or confirming a turn; `send` still throws `offline` during reconnection. Invalid JSON input can throw `invalid_request`. Input stops after leaving, disconnecting intentionally or ending the room.\n\nGame messages are limited to 20 per second per connection. Excess messages are dropped; `room.onError` receives `rate_limited` at most once per second across both budgets. Protocol service messages, including ping, lobby actions, state requests and voice signaling, have a separate 20/s budget with the same drop policy. More than 100 attempts in each of three consecutive one-second windows in either budget closes the connection with 4008 `rate_limited`; the kit does not reconnect automatically after this abuse closure. Abuse windows start with the first message on the connection; a normal or empty window resets the sequence. Oversized game frames close with 4009 `message_too_large`, reported through `room.onError`; malformed frames use 4009 `bad_message`. Neither closure is retried automatically.\n\nCall `room.leave()` for an intentional departure. The kit does not reconnect after leaving, being kicked, the room ending, the published version closing, or the same player opening the room in another tab.\n\n`room.disconnect()` is the other departure: it stops the transport, the retries and voice without sending a leave, so the server keeps the seat under its own persistence and grace rules. It is not reversible on the same object; returning means entering again from the code. The overlay uses it for Leave for now, together with the resume reference.\n\nA room also exposes `room.mode`, `room.countdownAt`, `room.connection`, `room.metadata`, and the `onMetadata` and `onConnection` listeners. `connection` is one of `connecting`, `connected`, `reconnecting`, `disconnected`, `ended`, `closed`, or `replaced`, where `replaced` means the same player opened the room in another tab. Unlike `session.onChange` and `overlay.onChange`, these listeners do not repeat the current value: read the getter first.\n\n`room.onError(listener)` reports technical protocol errors of the room as `{ code, message }`; it is not the place where a game reads its own result.\n\n`room.onScoreQueued(listener)` and `room.queuedScores` cover scores submitted for this player by `server.js`. Each entry is `{ board, player, score, day, submittedAt }`, only the owner\'s connection receives it, and the last 32 are kept. It is a technical notice that the server accepted the score, not a receipt that it is already on the board: read the board back with `c.board.top()` for that.\n\n`await room.requestRole(\'scout\')` asks the server for a role change during a match. It works only while the room is playing, only for a role declared in the manifest, and only when `server.js` defines `onRoleRequest(room, player, role)`; the server approves by calling `room.setRole`. Without that callback nothing changes, and the capability shows as `false` in `c.session.capabilities`. It is not a shortcut for changing roles from the browser.\n\nRoom creation, joining, and matchmaking reject with an `Error` carrying a stable `code`. Common codes are `invalid_request`, `no_server`, `no_match`, `cancelled`, `room_not_found`, `room_full`, `room_playing`, `room_ended`, `rate_limited`, `invalid_ticket`, `internal_error`, and `offline`. `no_server` means the published game has no multiplayer server. When `c.connected` is `false`, `create`, `join`, and `match` reject with `offline`.\n\n- `invalid_role`: a requested role id is malformed or is not declared in the manifest. Request a declared role id.\n- `role_change_unavailable`: the room is disconnected, is not playing, or `server.js` has no `onRoleRequest`. Wait for a connected playing state and provide that callback before offering the action.\n- `role_change_refused`: `onRoleRequest` returned without assigning the requested role. Leave the current role in place, or have the server approve with `room.setRole`.\n- `version_closed`: the room connection ended because its published version closed. Reopen the current game version and enter a current room.\n\nEvery listener call on a room returns a function that removes that listener: `onState`, `onPlayers`, `onStatus`, `onMessage`, `onMetadata`, `onConnection`, `onError`, and `onScoreQueued`.\n\n### Responsive action games\n\n1. Start with `npx @caisual/cli init --arcade my-arena`, a complete English/Italian canvas game with shared rules and tests.\n2. Accumulate `deltaSeconds` on the server and advance shared physics at a fixed 1/60 second step.\n3. Keep positions, collision or range checks, cooldowns and scores authoritative in `server.js`.\n4. Use `room.input()` for continuous controls and `room.send()` for discrete actions such as the starter\'s pulse.\n5. Number commands in the game and publish each player\'s last **applied** `ack` in `room.state`; transport sequence numbers are separate.\n6. Because `input()` coalesces values, send a bounded batch of unacknowledged commands; deduplicate on the server and consume at most one per simulation step.\n7. Predict your own entity with the shared step, replace it with each authoritative snapshot, then replay only commands after `ack`; ease corrections in the drawing only.\n8. Buffer remote samples by the `onState` timestamp and render behind `room.serverTime()`, accounting for RTT in `room.latency`, effective `room.tickRate`, and spectator `delayMs`.\n9. Clear pending controls on reconnection or a new round; keep the HUD inside safe areas and outside `reservedRects`, and end with `standings` plus `keepSetup`/`autoStart` rematches.\n10. Run the generated browser fixture and `/__caisual/players?n=2` with `dev --latency 120 --jitter 40 --loss 2`; see [Testing with a browser](/docs/local-development#testing-with-a-browser).\n\n### Spectators\n\nIn a standard game the overlay offers watching from the menu and the session arrives as `{ kind: \'watch\' }`. Use `c.room.watch(code)` to observe a room without joining it as a player:\n\n```js\nconst view = await c.room.watch(\'ABC234\');\n\ndraw(view.state);\nview.onState((state) => draw(state));\nview.onPlayers((players) => updateRoster(players));\nview.onStatus((status, result) => showStatus(status, result));\nview.onMessage((message) => showEvent(message));\n\nview.leave();\n```\n\nThe returned `Spectate` object exposes `state`, `tick`, `seed`, `status`, `players`, `host`, `code`, `result`, `delayMs`, the four listeners shown above, `serverTime()`, and `leave()`. It receives the room\'s public state, snapshots and updates, player list, status, and messages broadcast by `server.js`. The kit repairs a missed update automatically and reconnects temporary failures for the same 60-second grace period used by players.\n\nPublic room events are delayed by `delayMs`, which defaults to 3000 milliseconds. A game can set `"spectators": { "delayMs": N }` in `caisual.json`, where `N` is from 0 to 30000, or set `"spectators": false` to disable watching.\n\nA spectator has no `you`, `invite()`, `send()`, or voice API. Watching does not add anyone to `room.players`, does not affect roles, teams, player minimums, the host, or room lifetime, and is not visible to `server.js`. `watch()` can reject with `room_not_found`, `room_ended`, `spectators_disabled`, `spectators_full`, `rate_limited`, `offline`, or `invalid_request`.\n\n## Voice\n\nEvery room has a `room.voice` object. Voice is disabled by default and is enabled with the manifest\'s `voice` field.\n\nIn a standard game the overlay\'s voice panel carries Join, Leave, Mute, and the list of who is in the call, with the click the browser requires. A standard game does not draw its own voice buttons. The `room.voice` API below remains for games without the standard overlay and for server-side gain and proximity rules.\n\nA game with its own controls must offer an explicit one, because `join()` must be called from a click or another user gesture so the browser can start audio and, when publishing, request microphone permission.\n\n```js\nconst micButton = document.querySelector(\'#mic\');\nconst voiceList = document.querySelector(\'#voice-list\');\n\nfunction renderVoice(peers = room.voice.peers) {\n voiceList.replaceChildren(...peers.map((peer) => {\n const item = document.createElement(\'li\');\n const player = room.players.find((entry) => entry.id === peer.id);\n item.textContent = `${player?.name ?? peer.id}: ${\n peer.speaking ? \'speaking\' : peer.muted ? \'muted\' : \'quiet\'\n }`;\n return item;\n }));\n micButton.textContent = room.voice.state === \'off\'\n ? \'Join voice\'\n : !room.voice.mic ? \'Listening\' : room.voice.muted ? \'Unmute\' : \'Mute\';\n}\n\nmicButton.addEventListener(\'click\', async () => {\n if (room.voice.state === \'off\') await room.voice.join();\n else if (room.voice.mic) room.voice.mute(!room.voice.muted);\n renderVoice();\n});\n\nroom.voice.onPeers(renderVoice);\nroom.voice.onState(() => renderVoice());\nrenderVoice();\n```\n\n`room.voice.mode` is `none`, `room`, `team`, or `proximity`. In `room` mode, every participant in voice can hear every other participant. In `team` mode, players hear only their team. In `proximity` mode, the room server controls the gain between participants. Call `room.voice.join({ mic: false })` to listen without opening or publishing a microphone. Spectators join in listening mode when they call `join()` without options. A spectator that calls `join({ mic: true })` receives the `spectator` error.\n\nThe room server authorizes every voice track by team and gain. Listening that is no longer allowed is refused or closed.\n\n`room.voice.state` is `off`, `joining`, `on`, or `reconnecting`. `room.voice.mic` is `true` while the local player is publishing. `room.voice.muted` and `room.voice.speaking` describe the local microphone. `room.voice.peers` contains the other voice participants as `{ id, mic, muted, speaking, volume, gain }`. A listening participant has `mic: false`, `muted: true`, and `speaking: false`. `volume` is the local setting and `gain` is the value from the room server. Use `room.voice.setVolume(playerId, volume)` with a value from 0 to 1 to change only local playback.\n\n`room.voice.onPeers(listener)` runs when participants, microphone state, mute state, speaking state, volume, or gain changes. `room.voice.onState(listener)` reports connection state changes. Both return a function that removes the listener.\n\nCall `room.voice.leave()` to stop publishing or listening without leaving the room. `room.voice.mute()` requires an active published microphone and otherwise throws `not_publishing`. `room.leave()` and the end of the room stop voice automatically.\n\n`join()` rejects with an `Error` carrying one of these stable codes: `voice_disabled`, `permission_denied`, `unsupported`, `spectator`, `offline`, or `voice_error`. Voice can reconnect after a temporary room or media connection failure. The state becomes `reconnecting` while the kit retries.\n\n## Server\n\nPut `server.js` next to `caisual.json` and publish it with the game. See [publish.md](./publish.md#multiplayer-server) for the file rules, validation, and publishing flow.\n\n```js\nimport { defineGame } from \'@caisual/kit/server\';\n\nexport default defineGame({\n tickRate: 20, // required, an integer from 0 to 60; 0 runs only in response to events\n onCreate(room) {},\n onStart(room) {},\n onJoin(room, player) {},\n onConnection(room, player, connected) {},\n onLeave(room, player, reason) {}, // "left", "timeout", or "kicked"\n onRoleRequest(room, player, role) {}, // approve with room.setRole\n onMessage(room, player, message) {},\n onTick(room, deltaSeconds) {},\n onEnd(room) {},\n onRestart(room) {},\n});\n```\n\n`tickRate` is the only required field: `defineGame` throws a `TypeError` when it is missing or is not an integer from 0 to 60. All callbacks are optional. A player is `{ id, name, guest, role, team, connected }`.\n\n### Callback order\n\n- `onCreate` runs once when the room is first created, before any player joins.\n- `onJoin` runs when a player first enters the room, not when that same member reconnects. Without a lobby, the first player\'s `onJoin` is followed by `onStart`.\n- `onStart` runs when the room changes to `playing`. Without a lobby this is the first player entry. With a lobby it is after the host starts, the three-second countdown finishes, and the room still meets its minimums.\n- `onTick` runs for each active game tick when `tickRate` is greater than zero. `onMessage` runs for accepted client game messages, and `onRoleRequest` runs for an in-match role request when that callback exists. These events are processed serially, so their relative order is the order in which the room processes them.\n- `onConnection(room, player, connected)` runs with `false` when an existing member loses their connection and with `true` when that disconnected member returns. Both `player.connected` and `room.players` are already updated. It does not run for the first entry, a socket replacement while the member is still connected, or a permanent removal. Restoring a room reconciles actual connections: members saved as connected whose sockets are gone receive `false`, and their later return receives `true`. Surviving sockets receive no extra callback.\n- `onLeave` runs only when a player is removed with reason `left`, `timeout`, or `kicked`. A dropped connection calls `onConnection` during the grace period and does not call `onLeave`.\n- `onEnd` runs after the callback that requested `room.end`, with the final result and status `ended` or `finished`. Automatic room endings also use it, except closure of an already `finished` match, which must not run it twice.\n- `onRestart` runs when a rematch starts, after clearing the result and selecting the next status. Readiness is reset by default or restored with `keepSetup`. The fast-rematch and solo rules above override the default host-confirmed flow. With a lobby it prepares `lobby`; without one it prepares `playing` and is immediately followed by `onStart`. It is optional; `room.state` is preserved unless the game changes it.\n\nThere is no `onResume` callback. A sleeping room restores its saved state without calling one. When a scheduled time arrives, the room invokes the method named by `room.schedule`.\n\nFor a turn-based game, store the active player\'s id and pause that turn when the player disconnects. This also works with `tickRate: 0`, because callback state changes are published immediately:\n\n```js\nonConnection(room, player, connected) {\n if (room.state.turnPlayerId === player.id) {\n room.state.turnPaused = !connected;\n }\n},\nonMessage(room, player, message) {\n if (room.state.turnPaused || player.id !== room.state.turnPlayerId) return;\n applyTurn(room, player, message);\n},\n```\n\nIf turns have deadlines, save the remaining time when pausing and calculate a new deadline on return. Scheduled handlers must check whether the turn is still paused or current. Decide separately in `onLeave` how to handle a permanent departure. Secret redelivery remains a client responsibility through `room.onConnection`; see [Hidden information](#hidden-information).\n\nThe room object provides:\n\n```js\nroom.id;\nroom.seed;\nroom.mode;\nroom.status;\nroom.tick;\nroom.tickRate;\nroom.result;\nroom.state;\nroom.players;\nroom.host;\n\nroom.broadcast(message);\nroom.send(playerOrId, message);\nroom.kick(playerOrId);\nroom.setRole(playerOrId, role);\nroom.setTeam(playerOrId, team);\nroom.end(result);\nroom.end(result, { rematch: true });\n\nawait room.save(\'round\', value);\nawait room.load(\'round\');\nawait room.shared.get(\'ship_abc\');\nawait room.shared.set(\'ship_abc\', value);\nawait room.shared.delete(\'ship_abc\');\nawait room.shared.list(\'ship_\');\nawait room.shared.increment(\'visits\', 1);\nroom.schedule(milliseconds, \'methodName\', payload);\nroom.board.submit(playerOrId, \'main\', score, { daily: true });\nroom.board.submit(playerOrId, \'run\', score, { day: room.daily.day });\n\nroom.daily.day;\nroom.daily.seed;\nroom.daily.expiresAt;\nroom.time.now();\n\nroom.voice.mode;\nroom.voice.setGain(listener, speaker, 0.25);\nroom.voice.setProximity(playerA, playerB, 0.5);\n```\n\nSet `room.state` in `onCreate`, then mutate it only in server callbacks. It must remain plain JSON and may be at most 256 KB when serialized. `broadcast` sends a JSON message to everyone; `send` targets one player. `end` records a JSON result and closes the room unless rematch is enabled with `true` or an options object. See [Rematch in the same room](#rematch-in-the-same-room) for readiness, callback order, timer cancellation and the waiting deadline. Room saves use keys with the same format as player save keys and values up to 128 KB. `schedule` names a method on the definition so it can run even after a quiet room resumes. Scores submitted through `room.board` are verified. The room fixes their UTC `day` and millisecond `submittedAt` when `submit` is called, so delayed writes and retries do not move them to another day. Older queued scores without these fields retain the write-time day. Boards default to submission-day scoring. Set `day: "start"` and pass `{ day: room.daily.day }` for the starting day with a 10-minute grace period after midnight. See [Daily challenge](#daily-challenge) for expiry, errors and local day simulation.\n\n### Recognized match results\n\n`room.end(result)` still accepts any JSON. The optional `MatchResult` type is exported by the contracts and by `@caisual/kit` and `@caisual/kit/server`:\n\n```ts\ntype MatchResult = {\n standings: Array<{ playerId: string; score?: number; rank?: number }>;\n winners?: string[];\n draw?: boolean;\n unit?: \'points\' | \'time\' | \'distance\' | string;\n data?: JsonValue;\n} & Record<string, JsonValue>;\n```\n\nOrder `standings` from first place onward. Scores must be finite numbers; ranks are positive safe integers. The overlay uses the supplied order, displays player names and optional scores, and uses `rank` when present. `draw: true` takes precedence; otherwise `winners` identifies the winners. Without `winners`, the first row wins, together with rows sharing its explicit rank. Equal scores alone do not imply a draw. An explicit empty `winners` list means nobody won.\n\nThe end bar shows You won, You lost or Draw only for a participating player; spectators and players missing from the standings see Match finished. Outcome labels and the built-in units are translated in six languages. Custom units are shown as plain text; `time` and `distance` do not convert values or imply a measurement scale.\n\nReading is tolerant: unknown fields are ignored by the overlay, unknown or duplicate players are discarded, invalid scores and ranks are omitted, and winners must occur in the retained standings. With no usable standings the generic end bar remains. **`room.result` keeps the original JSON without normalization**, including `data` and any other game fields. Results are public to room members and spectators, so keep secrets elsewhere.\n\n```js\nroom.end({\n standings: [{ playerId: winner.id, score: 12, rank: 1 }, { playerId: other.id, score: 9, rank: 2 }],\n winners: [winner.id], unit: \'points\', data: { rounds: 3 },\n}, { rematch: true });\n```\n\n### Hidden information\n\nEverything in `room.state` reaches every player and every spectator. Never store cards in hand, secret roles, fog of war, or any other private value there.\n\nKeep secrets in room saves through `room.save` and `room.load`, which are server-only, or in module-level variables in `server.js`, keyed by room when needed. Deliver a secret to one player with `room.send(player, { type: \'hand\', cards })`. `onJoin` runs only on the player\'s first entry, not on reconnection, so the client requests its secrets for the current connection and every later reconnection:\n\n```js\nif (room.connection === \'connected\') room.send({ type: \'hand?\' });\nroom.onConnection((state) => {\n if (state === \'connected\') room.send({ type: \'hand?\' });\n});\n```\n\nThe server answers from `onMessage` with `room.send(player, { type: \'hand\', cards })`. The three-second delay of `c.room.watch` does not protect secrets. It only stops a player from watching an opponent\'s live screen in another tab.\n\nThe browser can change its role or team only while the room is in `lobby`. During a match, the server decides when a player changes role or team with `room.setRole` and `room.setTeam`. Both methods accept a player object or id and immediately update `room.players` for every client.\n\n```js\nonMessage(room, player, message) {\n if (message?.swap === \'captain\') {\n room.setRole(player, \'captain\');\n }\n},\n```\n\n`room.daily.seed` is fixed at room creation and shared by rooms created for that game on the same UTC day. `room.seed` is fixed for one room and is identical on the server and clients, so rooms created on the same day can generate different maps.\n\n### Shared game store\n\n`room.shared` is a server-only JSON key/value store shared by every room of the same game. It is useful when one room must leave data for another room, while `room.save` remains private to one room.\n\nThe following server leaves a ship when a room ends, then loads every previously left ship when another room is created. The room id suffix is used because shared-store keys follow the save-key format.\n\n```js\nexport default defineGame({\n tickRate: 0,\n\n async onCreate(room) {\n const keys = await room.shared.list(\'ship_\');\n room.state = {\n ships: await Promise.all(keys.map((key) => room.shared.get(key))),\n };\n },\n\n async onEnd(room) {\n const roomSuffix = room.id.split(\'.\')[1];\n await room.shared.set(\'ship_\' + roomSuffix, {\n position: room.state.position,\n cargo: room.state.cargo,\n });\n },\n});\n```\n\nThe five methods are asynchronous:\n\n```js\nconst value = await room.shared.get(key); // JSON value, or null\nawait room.shared.set(key, value); // last writer wins\nawait room.shared.delete(key);\nconst keys = await room.shared.list(prefix); // sorted, up to 1024\nconst total = await room.shared.increment(key, 1); // atomic, defaults to 1\n```\n\nKeys contain 1 to 32 lowercase letters, numbers, underscores, or hyphens. Values may be up to 64 KB when serialized, and each game may keep up to 1024 keys. Each room may perform up to 120 shared-store operations per minute. `increment` treats a missing key as zero and rejects unless the existing value, amount, and result are safe integers.\n\nUse the store in `onCreate`, `onStart`, `onEnd`, `onMessage`, or a `schedule` handler. Do not call it on every tick: each call waits for a remote operation, and the CPU budget uses elapsed wall-clock time. Browser clients cannot access this store. Send only the data they need with `room.broadcast` or `room.send`.\n\nFailures reject with an `Error` carrying `store_invalid_key`, `store_too_large`, `store_full`, `store_not_integer`, `store_unavailable`, or `store_rate_limited` in `code`.\n\n`room.voice.setGain(listener, speaker, gain)` controls how much one listener hears one speaker. It is directional, limited to the range from 0 to 1, and rounded to two decimal places. For example, the following setup lets the captain hear everyone while each crew member hears only the captain:\n\n```js\nconst captain = room.players.find((player) => player.role === \'captain\');\nconst crew = room.players.filter((player) => player.id !== captain.id);\n\nfor (const speaker of room.players) {\n room.voice.setGain(captain, speaker, 1);\n}\nfor (const listener of crew) {\n for (const speaker of room.players) {\n room.voice.setGain(listener, speaker, speaker.id === captain.id ? 1 : 0);\n }\n}\n```\n\n`room.voice.setProximity(a, b, gain)` is the symmetric shortcut for setting both directions. Both methods work in `room`, `team`, and `proximity` modes, and do nothing in `none`. In `team` mode, gains remain inside the team and cannot make a player hear another team.\n\nFor position-based audio, update the symmetric gain between players from server-owned positions:\n\n```js\nexport default defineGame({\n tickRate: 20,\n onTick(room) {\n for (const a of room.players) {\n for (const b of room.players) {\n if (a.id >= b.id) continue;\n const pa = room.state.positions[a.id];\n const pb = room.state.positions[b.id];\n const distance = Math.hypot(pa.x - pb.x, pa.y - pb.y);\n room.voice.setProximity(a, b, Math.max(0, 1 - distance / 20));\n }\n }\n },\n});\n```\n\n### Sleeping and cost\n\nPrefer `tickRate: 0` for turn based and party games. A room with a tick loop sleeps automatically after 30 seconds without player input or state changes and wakes on the next game message or player joining. Automatic ping and resync messages do not count as player input. A match with no player input for 10 minutes ends with `{ error: \'idle\' }`. Timers set with `schedule` and the countdown keep working while the room sleeps.\n\n### CPU budget\n\nEvery `onTick` and `onMessage` call is measured. Twenty consecutive calls above 100 ms end the room with `{ error: \'cpu_budget\' }`. If the average over 50 ticks is above 20 ms, the effective `tickRate` is halved, down to a minimum of 5, and clients receive an `error` message with code `tick_rate_reduced`. The optional `tickRate` field in `state` and `snapshot` protocol messages updates client `room.tickRate`; older clients ignore the added field. A frequency change sends a state diff even when its patch is empty.\n\n`room.tickRate` starts at the definition\'s `tickRate` and always reports the current effective frequency. `deltaSeconds` follows that frequency, so a fixed-step simulation must accumulate `deltaSeconds` instead of counting ticks. Measurement uses elapsed wall-clock time, so a slow `await` inside a callback also counts. `room.result` is `null` during a match, contains its result in `finished` or `ended` and inside `onEnd`, and returns to `null` before `onRestart`.\n\n### Persistent rooms\n\nSet `"persistent": true` in `caisual.json` for a room that must survive long breaks. It does not use the normal inactivity ending rule and does not end when every player disconnects. Players remain members until they call `room.leave()` or the server removes them with `room.kick()`. They can use the same room code to return while the game is already playing. The code remains valid while the room lives, and absent members remain in `room.players` with `connected: false`.\n\nA persistent room ends when the server calls `room.end(result)` without rematch, when its two-minute rematch wait expires, after 30 days without player input, entry, or a state change with `{ error: \'expired\' }`, or after five minutes without any members. The standard overlay stores the resume reference itself. Only games without it need to store the client `room.code` with `c.save.set()` and offer Resume. A persistent room incurs cost only while it is awake.\n\n## Limits\n\n- 120 requests per minute per player. Beyond that the kit rejects with `rate_limited`; wait and retry.\n- Saves: 32 keys per player per game, 256 KB per value.\n- Scores: safe integers from 0 upward.\n- Room state: 256 KB of plain JSON.\n- Game messages: 16 KB each and 20/s per connection; excess messages are dropped with at most one `rate_limited` error per second. Service messages have a separate 20/s budget. More than 100 attempts/s in either budget for three consecutive one-second windows closes with 4008. Oversized frames close with 4009 `message_too_large`.\n- Spectators: 100 per room, with a configured delay from 0 to 30 seconds.\n- Voice supports audio only and one voice channel per room.\n- Voice control messages: 64 KB each and 30 operations per 10 seconds per connection. Voice signaling also uses the separate service-message budget; audio traffic does not consume either message budget.\n- Room save values: 128 KB each.\n- Shared game store: 64 KB per JSON value, 1024 keys per game, and 120 operations per minute per room.\n\n## Development\n\nRun `npx @caisual/cli dev` from the game folder, then open the printed portal URL. The preview supplies the normal handshake, so `c.connected` is `true` and the game receives a local guest identity. It also mounts the same standard overlay as the portal. `?lang=` chooses the game preference, resolved against the manifest; `c.player.uiLanguage` follows the overlay fallback. For example, `?lang=ja` gives `c.player.language === "ja"` when declared, with the overlay in English. Friends and parties are marked unavailable locally; everything else, including saves, leaderboards, daily data, invitations, and rooms, works on local data. Opening the portal URL in more browser tabs creates more local players, which makes multiplayer testing possible without publishing.\n\nUse `npx @caisual/cli dev --day 2026-09-04` to pin the UTC day used by client and room daily seeds and local daily leaderboards. The flag accepts only a real date in `YYYY-MM-DD` format; without it, dev uses today\'s UTC date. Real clocks and room timers keep running normally. Scores remain in `.caisual-dev/scores.json` under their assigned day: restarting with another `--day` selects that day\'s board, and returning to a previous day restores its scores. Saves, identities and rooms are shared across these dates; queued room scores keep their assigned day when flushed after a restart. Existing rooms retain their creation context; new rooms follow the selected day. `expiresAt` follows the real clock even with `--day`. Use `c.board.top(\'main\', { day: \'2026-09-04\', guests: true })` to inspect a specific local day. A changed flag takes effect after restarting dev and reloading the game.\n\nOpen the printed `/__caisual/players?n=4` URL for 1 to 8 independent guest frames, each including the game and standard overlay. Use the overlay to create a room and join its code in the other frames. Each frame has **Drop** (1 to 60 seconds, default 3) and **Spectate**, which opens a new guest watching that frame\'s room. Phone frames are 390 x 844; the desktop preset is 960 x 640. Add `&lang=it` to test Italian.\n\n`dev --latency 120 --jitter 40 --loss 2` delays room WebSocket messages in both directions for players and spectators. Latency and jitter are integer milliseconds from 0 to 60000; loss is a percentage from 0 to 100 and may be fractional. Jitter and loss require `--latency`, which may be zero. Jitter varies delay uniformly within plus/minus the supplied value, clamped at zero, and preserves message order. Loss discards whole application messages, including protocol messages, to exercise recovery; it is not a model of TCP packet retransmission. HTTP, matchmaking and audio are unaffected. Drop closes the guest\'s room sockets and prevents successful reconnects for the selected duration; the kit\'s retry schedule can make the return later. The spectator stream also retains its configured game delay.\n\nClient files are read on each request: reload the portal or frame after editing, or rebuild into `client/` first if using a bundler. There is no automatic browser reload. Restart dev after changing `caisual.json`, `server.js`, or any server import (including shared client physics). Rooms restore from `.caisual-dev/`; changing the shape of their state may require creating a fresh room. A busy port error suggests a command with a currently available port.\n\nEvery `init` variant generates Node test and optional Playwright browser scripts. Playwright is a development dependency of the generated game, never of the CLI. See [Local dev](/docs/local-development#testing-with-a-browser) for installation and the full browser workflow.\n\nWhen building the client with a bundler, remember that `client/` is served as-is. Configure Vite, esbuild, or another bundler to write into that folder, for example `vite build --outDir client`, and use relative paths such as `base: \'./\'`.\n\nKeep loading the kit from the `<script type="module">` shown at the beginning of this guide, using `/__caisual/kit/v1.js`, when publishing on Caisual. That URL exists only in `caisual dev` and in the published game.\n\nIf the game has `server.js`, room state is handled locally and stored under `.caisual-dev/` in the game folder. If it has no `server.js`, room creation rejects with `no_server` and the single-player APIs still work.\n\nTo run from any other static server, install `@caisual/kit` from npm and import it with a bundler as `import { caisual } from \'@caisual/kit\'`. In that build standalone mode applies: `c.connected` is `false`, saves use local storage, `submit` returns `accepted: false`, leaderboards are empty, the daily seed is local, and room creation and joining reject with `offline`. The rest of the game logic does not need a different code path.\n\nAfter publishing with `npx @caisual/cli publish`, open the game from its caisual.com page: `c.connected` becomes `true` and every call goes to the portal.\n\n## Manifest\n\nDeclare `"overlay": { "version": 1 }` to get the standard overlay, with an optional `accent` colour. A standard game must declare at least one mode, and every mode needs `execution`, either `local` for a single-player run of exactly one player or `room` for a room backed by `server.js`. `label` names the mode in the standard menu and `instructions` adds one line under it. `roles[].label` and `boards[<id>].label` name roles and boards in the same UI, and `boards[<id>].periods` lists `daily`, `all-time` or both. A mode with matchmaking adds `matchmaking.defaults`, one value for every field of its `key`, so the overlay can start a search on its own.\n\n```json\n{\n "overlay": { "version": 1, "accent": "#397e83" },\n "players": { "min": 2, "max": 4 },\n "lobby": true,\n "boards": { "solo": { "source": "server", "label": "Best run", "periods": ["daily", "all-time"] } },\n "modes": [\n { "id": "practice", "execution": "local", "label": "Practice",\n "instructions": "One run against the clock.",\n "players": { "min": 1, "max": 1 }, "lobby": false },\n { "id": "duel", "execution": "room", "label": "Online",\n "matchmaking": { "key": ["pool"], "defaults": { "pool": "v1" }, "timeoutMs": 12000 } }\n ]\n}\n```\n\nA game without `overlay` keeps its historical flow and draws its own menus. Nothing else changes for it.\n\nNo manifest field is required for identity, saves, leaderboards, or the daily challenge. Use `boards` to make selected leaderboards server-only. A mode may override only `players: { min, max }` and `lobby`; omitted fields inherit the root configuration, and `mode: null` uses the root values. Matchmaking thresholds and room admission use this same resolution. For rooms, set `players` to the supported range and use `lobby`, `persistent`, `spectators`, `roles`, `teams`, and `modes` to describe the setup and lifetime. Set `voice` to `room`, `team`, or `proximity` to enable the corresponding voice mode, or omit it for `none`. A single-player game can keep `players` at `{ "min": 1, "max": 1 }`, `lobby` at `false`, and omit `server.js`. See [publish.md](./publish.md#caisualjson) for every field and the publishing steps.\n\n\n## Required game images\n\nEvery manifest must declare three different files inside `client/`. All three are required PNG, JPEG or WebP images, at most 2 MB (2,000,000 bytes) each:\n\n| Field | Exact size | Use |\n| --- | --- | --- |\n| `cover` | 1536x1024, 3:2 | Featured home card, game page and overlay boot background, social preview, mobile invitation |\n| `card` | 1024x1024, 1:1 | Secondary home cards and square invitation artwork |\n| `icon` | 1024x1024, 1:1 | Game window favicon, overlay, friends and parties |\n\n**No text inside any image:** no title, slogan, letters or numbers. The portal displays the game name. Keep the icon simple enough to read at a small size. The files must be different, including `card` and `icon`. Screenshots keep their existing rules.\n\nThe 3:2 and 1:1 ratios match native formats of widely used image generators. Export at the exact sizes above: Caisual does not crop, stretch or accept a size range. `caisual check` and publish validate the format from the file headers, exact dimensions and weight. The portal repeats these checks for direct publication. The absence of text is an editorial requirement, not an automatic check.\n\nAll three `caisual init` variants include small, real PNG examples at these sizes. Replace them with artwork for your game before sharing it. English (`en`) is always required in `languages`; the first language remains the default, for example `["it", "en"]`.\n';
1053
1053
 
1054
1054
  // src/dev.ts
1055
1055
  import { createHash as createHash2, createHmac, randomBytes, randomUUID as randomUUID2, timingSafeEqual } from "node:crypto";
@@ -3721,6 +3721,8 @@ var NOMI_RISERVATI3 = [
3721
3721
  ];
3722
3722
  var RISERVATI3 = new Set(NOMI_RISERVATI3);
3723
3723
  var words = {
3724
+ gameUpdated: ["This game was updated", "Questo gioco \xE8 stato aggiornato", "Este juego se ha actualizado", "Ce jeu a \xE9t\xE9 mis \xE0 jour", "Dieses Spiel wurde aktualisiert", "Este jogo foi atualizado"],
3725
+ reloadGame: ["Reload game", "Ricarica il gioco", "Recargar el juego", "Recharger le jeu", "Spiel neu laden", "Recarregar o jogo"],
3724
3726
  gameLanguages: ["Game languages", "Lingue del gioco", "Idiomas del juego", "Langues du jeu", "Spielsprachen", "Idiomas do jogo"],
3725
3727
  loading: ["Loading game...", "Caricamento...", "Cargando...", "Chargement...", "Spiel wird geladen...", "Carregando..."],
3726
3728
  loadingSlow: ["This game is taking longer than expected. You can wait a little longer or try again.", "Il gioco ci sta mettendo pi\xF9 del previsto. Puoi aspettare ancora un po\u2019 o riprovare.", "El juego est\xE1 tardando m\xE1s de lo esperado. Puedes esperar un poco m\xE1s o volver a intentarlo.", "Le jeu met plus de temps que pr\xE9vu. Vous pouvez patienter encore un peu ou r\xE9essayer.", "Das Spiel braucht l\xE4nger als erwartet. Du kannst noch etwas warten oder es erneut versuchen.", "O jogo est\xE1 demorando mais do que o esperado. Voc\xEA pode esperar mais um pouco ou tentar novamente."],
@@ -4135,6 +4137,7 @@ function serviceTicket(player, game, aud, secret) {
4135
4137
  const iat = currentSeconds();
4136
4138
  return signJwt({
4137
4139
  sub: player.id,
4140
+ n: 1,
4138
4141
  game,
4139
4142
  name: player.name,
4140
4143
  guest: player.guest,
@@ -4387,6 +4390,7 @@ function parentPage(input) {
4387
4390
  const language = overlayLocale(languagePreferences[0]);
4388
4391
  document.documentElement.lang = language;
4389
4392
  const bridge = creaPonteOspite({
4393
+ n: 1, reload: () => window.location.reload(),
4390
4394
  finestra: window, frame, origineGioco: gameOrigin, origineLive: portalOrigin,
4391
4395
  invite, ticket: session.portal, language, languagePreferences,
4392
4396
  configuration,
@@ -5020,7 +5024,7 @@ var DevService = class {
5020
5024
  response.setHeader("Content-Type", "text/javascript; charset=utf-8");
5021
5025
  response.setHeader("Cache-Control", "no-store");
5022
5026
  response.setHeader("X-Content-Type-Options", "nosniff");
5023
- response.end(request.method === "HEAD" ? void 0 : '// Caisual game kit v0.12.0\n\n// ../contracts/src/slug.ts\nvar NOMI_RISERVATI = [\n "www",\n "api",\n "app",\n "play",\n "live",\n "multi",\n "cdn",\n "assets",\n "static",\n "mail",\n "mx",\n "ns1",\n "ns2",\n "autodiscover",\n "_dmarc",\n "admin",\n "login",\n "account",\n "auth",\n "pay",\n "secure",\n "support",\n "help",\n "blog",\n "status",\n "dev",\n "staging",\n "test",\n "caisual",\n "shipz"\n];\nvar RISERVATI = new Set(NOMI_RISERVATI);\nvar SLUG_NUOVO = /^[a-z0-9]+(?:-[a-z0-9]+)*$/;\nvar SLUG_STORICO = /^[0-9a-f]{8}-[0-9a-f]{4}-4[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/;\nfunction isValidSlug(value) {\n return value.length >= 3 && value.length <= 32 && SLUG_NUOVO.test(value) || SLUG_STORICO.test(value);\n}\nfunction isReservedSlug(value) {\n return RISERVATI.has(value);\n}\n\n// ../contracts/src/i18n.ts\nfunction normalizeLanguage(value) {\n if (typeof value !== "string" || value.length > 128) return null;\n try {\n return Intl.getCanonicalLocales(value)[0] ?? null;\n } catch {\n return null;\n }\n}\nfunction manifestLanguages(manifest) {\n return manifest.languages?.length ? [...manifest.languages] : [manifest.language ?? "en"];\n}\nfunction languageFallbacks(language, defaultLanguage = "en") {\n const result = [];\n let tag = normalizeLanguage(language);\n while (tag) {\n result.push(tag);\n const parts = tag.split("-");\n parts.pop();\n if (parts.at(-1)?.length === 1) parts.pop();\n tag = parts.join("-");\n }\n result.push(normalizeLanguage(defaultLanguage) ?? defaultLanguage);\n return [...new Set(result)];\n}\nfunction resolveGameLanguage(preferences, languages2 = []) {\n const declared = languages2.map(normalizeLanguage).filter((tag) => tag !== null);\n const preferred = preferences.map(normalizeLanguage).filter((tag) => tag !== null);\n if (!declared.length) return preferred[0] ?? "en";\n for (const preference of preferred) {\n for (const tag of languageFallbacks(preference, preference)) {\n if (declared.includes(tag)) return tag;\n }\n }\n return declared[0];\n}\nfunction isTextDictionary(value) {\n return typeof value === "object" && value !== null && !Array.isArray(value) && Object.values(value).every((text) => typeof text === "string");\n}\n\n// ../contracts/src/manifest.ts\nfunction risolviModalita(manifest, mode) {\n const scelta = mode === null ? void 0 : manifest.modes.find((voce) => voce.id === mode);\n if (mode !== null && scelta === void 0) throw new Error("The selected game mode does not exist.");\n return { players: { ...scelta?.players ?? manifest.players }, lobby: scelta?.lobby ?? manifest.lobby };\n}\nfunction modalitaLocale(manifest, mode) {\n return mode !== null && manifest.modes.some((voce) => voce.id === mode && voce.execution === "local");\n}\nvar TETTO_GIOCATORI = 24;\nvar RITARDO_SPETTATORI_MS = 3e3;\nvar MASSIMO_CLASSIFICHE = 32;\nvar CAMPI = /* @__PURE__ */ new Set([\n "overlay",\n "manifest",\n "id",\n "name",\n "description",\n "cover",\n "card",\n "icon",\n "screenshots",\n "tags",\n "languages",\n "language",\n "platform",\n "orientation",\n "input",\n "visibility",\n "network",\n "isolated",\n "requires",\n "players",\n "lobby",\n "persistent",\n "spectators",\n "boards",\n "roles",\n "teams",\n "voice",\n "modes"\n]);\nvar INPUT = /* @__PURE__ */ new Set(["keyboard", "mouse", "touch", "gamepad"]);\nvar PLATFORM = /* @__PURE__ */ new Set(["desktop", "mobile", "both"]);\nvar ORIENTATION = /* @__PURE__ */ new Set(["landscape", "portrait"]);\nvar VISIBILITY = /* @__PURE__ */ new Set(["public", "unlisted"]);\nvar VOICE = /* @__PURE__ */ new Set(["none", "room", "team", "proximity"]);\nvar PERFORMANCE = /* @__PURE__ */ new Set(["light", "medium", "heavy"]);\nvar TAG = /^[a-z0-9-]+$/;\nvar ID_INTERNO = /^[a-z0-9]+(?:-[a-z0-9]+)*$/;\nvar CAMPO_MATCHMAKING = /^[a-z0-9][a-z0-9-]{0,31}$/;\nvar ID_CLASSIFICA = /^[a-z0-9][a-z0-9_-]{0,31}$/;\nfunction oggetto(value) {\n if (typeof value !== "object" || value === null || Array.isArray(value)) return null;\n return value;\n}\nfunction percorsoRelativo(value) {\n if (value === "" || value.startsWith("/") || value.includes("\\\\") || value.includes("\\0")) return false;\n if (value.includes("?") || value.includes("#")) return false;\n const parti = value.split("/");\n if (parti.some((parte) => parte === "" || parte === "." || parte === "..")) return false;\n try {\n const decoded = parti.map((parte) => decodeURIComponent(parte));\n return !decoded.some((parte) => parte === "" || parte === "." || parte === ".." || parte.includes("/"));\n } catch {\n return false;\n }\n}\nfunction hostValido(value) {\n if (value.length === 0 || value.length > 253) return false;\n if (value.includes("://") || /[/:?#@]/.test(value)) return false;\n const parti = value.split(".");\n return parti.every(\n (parte) => parte.length >= 1 && parte.length <= 63 && /^[a-z0-9](?:[a-z0-9-]*[a-z0-9])?$/i.test(parte)\n );\n}\nfunction interoTra(value, min, max) {\n return typeof value === "number" && Number.isInteger(value) && value >= min && value <= max;\n}\nfunction stringaDefault(dati, campo, valoreDefault, errori) {\n const value = dati[campo];\n if (value === void 0) return valoreDefault;\n if (typeof value !== "string") {\n errori.push(`${campo}: must be a string.`);\n return valoreDefault;\n }\n return value;\n}\nfunction testoFacoltativo(value, key, max, path, errors) {\n if (value[key] === void 0) return void 0;\n const check = (text2, field2) => {\n if (typeof text2 !== "string" || text2.trim().length === 0 || text2.trim().length > max || /[\\r\\n\\u0000-\\u001f]/.test(text2)) {\n errors.push(`${field2}: must contain 1-${max} characters on one line.`);\n return void 0;\n }\n return text2.trim();\n };\n const text = value[key], field = `${path}.${key}`;\n if (typeof text === "string") return check(text, field);\n const translations = oggetto(text);\n if (!translations || Object.keys(translations).length === 0) {\n errors.push(`${field}: must be a string or a non-empty language-to-text object.`);\n return void 0;\n }\n const result = {};\n for (const [raw, text2] of Object.entries(translations)) {\n const tag = normalizeLanguage(raw);\n if (!tag) {\n errors.push(`${field}.${raw}: must be a BCP 47 language tag.`);\n continue;\n }\n if (Object.hasOwn(result, tag)) errors.push(`${field}.${raw}: duplicate language.`);\n const checked = check(text2, `${field}.${raw}`);\n if (checked !== void 0) result[tag] = checked;\n }\n return result;\n}\nfunction validaManifest(valore) {\n const errori = [];\n const dati = oggetto(valore);\n if (dati === null) return { ok: false, errori: ["manifest: must be a JSON object."] };\n for (const campo of Object.keys(dati)) {\n if (!CAMPI.has(campo)) errori.push(`${campo}: unknown field.`);\n }\n if (dati.manifest === void 0) errori.push("manifest: is required and must be 1.");\n else if (dati.manifest !== 1) errori.push("manifest: must be exactly 1.");\n const id = stringaDefault(dati, "id", "", errori);\n if (dati.id === void 0) errori.push("id: is required.");\n else if (typeof dati.id === "string") {\n if (!isValidSlug(id)) {\n errori.push("id: must be 3-32 lowercase ASCII letters or digits with internal hyphens; historical UUID v4 slugs may be 36 characters.");\n } else if (isReservedSlug(id)) errori.push("id: this slug is reserved.");\n }\n const name = stringaDefault(dati, "name", "", errori);\n if (dati.name === void 0) errori.push("name: is required.");\n else if (typeof dati.name === "string" && (name.trim() === "" || name.length > 60)) {\n errori.push("name: must contain 1-60 characters.");\n }\n const description = stringaDefault(dati, "description", "", errori);\n if (description.length > 500) errori.push("description: must be at most 500 characters.");\n const immagini = { cover: "", card: "", icon: "" };\n const usati = /* @__PURE__ */ new Set();\n for (const campo of ["cover", "card", "icon"]) {\n const path = dati[campo];\n if (path === void 0 || path === null) errori.push(`${campo}: is required.`);\n else if (typeof path !== "string" || !percorsoRelativo(path)) errori.push(`${campo}: must be a relative file path inside client/ without query, fragment, or parent segments.`);\n else {\n if (!/\\.(png|jpe?g|webp)$/i.test(path)) errori.push(`${campo}: must be a PNG, JPEG or WebP file.`);\n const canonical = decodeURIComponent(path);\n if (usati.has(canonical)) errori.push(`${campo}: each image must use a different file; cover, card and icon cannot share a path.`);\n usati.add(canonical);\n immagini[campo] = path;\n }\n }\n const { cover, card, icon } = immagini;\n const screenshots = [];\n if (dati.screenshots !== void 0) {\n if (!Array.isArray(dati.screenshots)) errori.push("screenshots: must be an array of relative file paths.");\n else {\n if (dati.screenshots.length > 8) errori.push("screenshots: must contain at most 8 paths.");\n for (const [indice, value] of dati.screenshots.entries()) {\n if (typeof value !== "string" || !percorsoRelativo(value)) {\n errori.push(`screenshots[${indice}]: must be a relative file path without query, fragment, or parent segments.`);\n } else screenshots.push(value);\n }\n }\n }\n const tags = [];\n if (dati.tags !== void 0) {\n if (!Array.isArray(dati.tags)) errori.push("tags: must be an array.");\n else {\n if (dati.tags.length > 10) errori.push("tags: must contain at most 10 tags.");\n for (const [indice, value] of dati.tags.entries()) {\n if (typeof value !== "string" || value.length > 24 || !TAG.test(value)) {\n errori.push(`tags[${indice}]: must be 1-24 lowercase letters, digits, or hyphens.`);\n } else tags.push(value);\n }\n }\n }\n const legacyLanguage = stringaDefault(dati, "language", "en", errori);\n if (!/^[A-Za-z]{2,8}(?:-[A-Za-z0-9]{1,8})*$/.test(legacyLanguage)) {\n errori.push("language: must be a BCP 47 language tag such as en, it, or pt-BR.");\n }\n const languages2 = [];\n if (!Array.isArray(dati.languages) || dati.languages.length === 0) {\n errori.push("languages: must be a non-empty array of BCP 47 language tags.");\n } else for (const [index, raw] of dati.languages.entries()) {\n const tag = normalizeLanguage(raw);\n if (!tag) errori.push(`languages[${index}]: must be a BCP 47 language tag.`);\n else if (languages2.includes(tag)) errori.push(`languages[${index}]: duplicate language ${tag}.`);\n else languages2.push(tag);\n }\n if (!languages2.includes("en")) errori.push("languages: English is always required alongside the game\'s own languages.");\n const language = languages2[0] ?? legacyLanguage;\n if (dati.language !== void 0 && dati.languages !== void 0 && legacyLanguage.toLowerCase() !== language.toLowerCase()) {\n errori.push("language: must match the first entry in languages when both are present.");\n }\n let platform = "both";\n if (dati.platform === void 0) errori.push("platform: is required.");\n else if (typeof dati.platform !== "string" || !PLATFORM.has(dati.platform)) {\n errori.push("platform: must be desktop, mobile, or both.");\n } else platform = dati.platform;\n let orientation = "landscape";\n if (dati.orientation !== void 0) {\n if (typeof dati.orientation !== "string" || !ORIENTATION.has(dati.orientation)) {\n errori.push("orientation: must be landscape or portrait.");\n } else orientation = dati.orientation;\n }\n const input = [];\n if (dati.input !== void 0) {\n if (!Array.isArray(dati.input)) errori.push("input: must be an array.");\n else for (const [indice, value] of dati.input.entries()) {\n if (typeof value !== "string" || !INPUT.has(value)) {\n errori.push(`input[${indice}]: must be keyboard, mouse, touch, or gamepad.`);\n } else if (input.includes(value)) errori.push(`input[${indice}]: duplicate value ${value}.`);\n else input.push(value);\n }\n }\n let visibility = "public";\n if (dati.visibility !== void 0) {\n if (typeof dati.visibility !== "string" || !VISIBILITY.has(dati.visibility)) {\n errori.push("visibility: must be public or unlisted.");\n } else visibility = dati.visibility;\n }\n const network = [];\n if (dati.network !== void 0) {\n if (!Array.isArray(dati.network)) errori.push("network: must be an array of host names.");\n else for (const [indice, value] of dati.network.entries()) {\n if (typeof value !== "string" || !hostValido(value)) {\n errori.push(`network[${indice}]: must be a host name without scheme, port, path, query, or fragment.`);\n } else if (network.includes(value)) errori.push(`network[${indice}]: duplicate host ${value}.`);\n else network.push(value);\n }\n }\n let isolated = false;\n if (dati.isolated !== void 0) {\n if (typeof dati.isolated !== "boolean") errori.push("isolated: must be a boolean.");\n else isolated = dati.isolated;\n }\n const requires = {\n webgl2: false,\n webgpu: false,\n wasm: false,\n threads: false,\n memoryMb: null,\n performance: "light"\n };\n if (dati.requires !== void 0) {\n const value = oggetto(dati.requires);\n if (value === null) errori.push("requires: must be an object.");\n else {\n for (const campo of Object.keys(value)) {\n if (!["webgl2", "webgpu", "wasm", "threads", "memoryMb", "performance"].includes(campo)) {\n errori.push(`requires.${campo}: unknown field.`);\n }\n }\n for (const campo of ["webgl2", "webgpu", "wasm", "threads"]) {\n if (value[campo] === void 0) continue;\n if (typeof value[campo] !== "boolean") errori.push(`requires.${campo}: must be a boolean.`);\n else requires[campo] = value[campo];\n }\n if (value.memoryMb !== void 0) {\n if (value.memoryMb !== null && (!interoTra(value.memoryMb, 512, 32768) || value.memoryMb % 256 !== 0)) {\n errori.push("requires.memoryMb: must be an integer from 512 to 32768 in steps of 256, or null.");\n } else requires.memoryMb = value.memoryMb;\n }\n if (value.performance !== void 0) {\n if (typeof value.performance !== "string" || !PERFORMANCE.has(value.performance)) {\n errori.push("requires.performance: must be light, medium, or heavy.");\n } else requires.performance = value.performance;\n }\n if (requires.threads && !isolated) errori.push("requires.threads: needs isolated: true.");\n }\n }\n let players = { min: 1, max: 1 };\n if (dati.players !== void 0) {\n const value = oggetto(dati.players);\n if (value === null) errori.push("players: must be an object with min and max.");\n else {\n for (const campo of Object.keys(value)) {\n if (campo !== "min" && campo !== "max") errori.push(`players.${campo}: unknown field.`);\n }\n if (!interoTra(value.min, 1, TETTO_GIOCATORI)) errori.push(`players.min: must be an integer from 1 to ${TETTO_GIOCATORI}.`);\n if (!interoTra(value.max, 1, TETTO_GIOCATORI)) errori.push(`players.max: must be an integer from 1 to ${TETTO_GIOCATORI} in manifest version 1.`);\n if (interoTra(value.min, 1, TETTO_GIOCATORI) && interoTra(value.max, 1, TETTO_GIOCATORI)) {\n if (value.min > value.max) errori.push("players.max: must be greater than or equal to players.min.");\n else players = { min: value.min, max: value.max };\n }\n }\n }\n let lobby = false;\n if (dati.lobby !== void 0) {\n if (typeof dati.lobby !== "boolean") errori.push("lobby: must be a boolean.");\n else lobby = dati.lobby;\n }\n let persistent = false;\n if (dati.persistent !== void 0) {\n if (typeof dati.persistent !== "boolean") errori.push("persistent: must be a boolean.");\n else persistent = dati.persistent;\n }\n let spectators = { delayMs: RITARDO_SPETTATORI_MS };\n if (dati.spectators === false || dati.spectators === null) spectators = null;\n else if (dati.spectators !== void 0 && dati.spectators !== true) {\n const value = oggetto(dati.spectators);\n if (value === null) {\n errori.push("spectators: must be a boolean or an object with delayMs.");\n } else {\n for (const campo of Object.keys(value)) {\n if (campo !== "delayMs") errori.push(`spectators.${campo}: unknown field.`);\n }\n if (!interoTra(value.delayMs, 0, 3e4)) {\n errori.push("spectators.delayMs: must be an integer from 0 to 30000.");\n } else spectators = { delayMs: value.delayMs };\n }\n }\n let overlay = null;\n if (dati.overlay !== void 0 && dati.overlay !== null) {\n const value = oggetto(dati.overlay);\n if (value === null) errori.push("overlay: must be an object or null.");\n else {\n for (const key of Object.keys(value)) if (!["version", "accent"].includes(key)) errori.push(`overlay.${key}: unknown field.`);\n if (value.version !== 1) errori.push("overlay.version: must be exactly 1.");\n if (value.accent !== void 0 && (typeof value.accent !== "string" || !/^#[0-9a-fA-F]{6}$/.test(value.accent))) {\n errori.push("overlay.accent: must be a six-digit hexadecimal color, such as #336699.");\n }\n overlay = { version: 1, ...typeof value.accent === "string" ? { accent: value.accent } : {} };\n }\n }\n const boards = {};\n if (dati.boards !== void 0) {\n const value = oggetto(dati.boards);\n if (value === null) errori.push("boards: must be an object of board ids.");\n else {\n if (Object.keys(value).length > MASSIMO_CLASSIFICHE) {\n errori.push(`boards: at most ${MASSIMO_CLASSIFICHE} boards.`);\n }\n for (const [id2, raw] of Object.entries(value)) {\n let valido = true;\n if (!ID_CLASSIFICA.test(id2)) {\n errori.push(`boards.${id2}: invalid board id.`);\n valido = false;\n }\n const board = oggetto(raw);\n if (board === null) {\n errori.push(`boards.${id2}.source: must be "client" or "server".`);\n continue;\n }\n for (const campo of Object.keys(board)) {\n if (!["source", "label", "periods", "day"].includes(campo)) errori.push(`boards.${id2}.${campo}: unknown field.`);\n }\n if (board.source !== "client" && board.source !== "server") {\n errori.push(`boards.${id2}.source: must be "client" or "server".`);\n valido = false;\n }\n if (board.day !== void 0 && board.day !== "submit" && board.day !== "start") errori.push(`boards.${id2}.day: must be "submit" or "start".`);\n if (board.day === "start" && board.source !== "server") errori.push(`boards.${id2}.day: start requires source "server".`);\n const label = testoFacoltativo(board, "label", 48, `boards.${id2}`, errori);\n let periods = ["all-time"];\n if (board.periods !== void 0) {\n if (!Array.isArray(board.periods) || board.periods.length < 1 || board.periods.length > 2 || board.periods.some((period) => period !== "daily" && period !== "all-time") || new Set(board.periods).size !== board.periods.length) {\n errori.push(`boards.${id2}.periods: must contain daily, all-time, or both without duplicates.`);\n } else periods = [...board.periods];\n }\n if (valido) Object.defineProperty(boards, id2, { value: {\n source: board.source,\n periods,\n ...board.day === void 0 ? {} : { day: board.day },\n ...label === void 0 ? {} : { label }\n }, enumerable: true, configurable: true, writable: true });\n }\n }\n }\n const roles = [];\n if (dati.roles !== void 0) {\n if (!Array.isArray(dati.roles)) errori.push("roles: must be an array.");\n else {\n const ids = /* @__PURE__ */ new Set();\n for (const [indice, raw] of dati.roles.entries()) {\n const value = oggetto(raw);\n if (value === null) {\n errori.push(`roles[${indice}]: must be an object.`);\n continue;\n }\n for (const campo of Object.keys(value)) {\n if (!["id", "min", "max", "label"].includes(campo)) errori.push(`roles[${indice}].${campo}: unknown field.`);\n }\n const idRuolo = value.id;\n const min = value.min;\n const max = value.max;\n let valido = true;\n if (typeof idRuolo !== "string" || idRuolo.length > 32 || !ID_INTERNO.test(idRuolo)) {\n errori.push(`roles[${indice}].id: must be 1-32 lowercase letters, digits, or internal hyphens.`);\n valido = false;\n } else if (ids.has(idRuolo)) {\n errori.push(`roles[${indice}].id: duplicate role ${idRuolo}.`);\n valido = false;\n } else ids.add(idRuolo);\n if (!interoTra(min, 0, TETTO_GIOCATORI)) {\n errori.push(`roles[${indice}].min: must be an integer from 0 to ${TETTO_GIOCATORI}.`);\n valido = false;\n }\n if (max !== void 0 && !interoTra(max, 0, TETTO_GIOCATORI)) {\n errori.push(`roles[${indice}].max: must be an integer from 0 to ${TETTO_GIOCATORI} when present.`);\n valido = false;\n }\n if (typeof min === "number" && typeof max === "number" && min > max) {\n errori.push(`roles[${indice}].max: must be greater than or equal to min.`);\n valido = false;\n }\n const label = testoFacoltativo(value, "label", 32, `roles[${indice}]`, errori);\n if (valido) roles.push({\n id: idRuolo,\n min,\n ...max === void 0 ? {} : { max },\n ...label === void 0 ? {} : { label }\n });\n }\n }\n }\n let teams = null;\n if (dati.teams !== void 0 && dati.teams !== null) {\n const value = oggetto(dati.teams);\n if (value === null) errori.push("teams: must be null or an object with min and max.");\n else {\n for (const campo of Object.keys(value)) {\n if (campo !== "min" && campo !== "max") errori.push(`teams.${campo}: unknown field.`);\n }\n if (!interoTra(value.min, 2, TETTO_GIOCATORI)) errori.push(`teams.min: must be an integer from 2 to ${TETTO_GIOCATORI}.`);\n if (!interoTra(value.max, 2, TETTO_GIOCATORI)) errori.push(`teams.max: must be an integer from 2 to ${TETTO_GIOCATORI}.`);\n if (interoTra(value.min, 2, TETTO_GIOCATORI) && interoTra(value.max, 2, TETTO_GIOCATORI)) {\n if (value.min > value.max) errori.push("teams.max: must be greater than or equal to teams.min.");\n else teams = { min: value.min, max: value.max };\n }\n }\n }\n let voice = "none";\n if (dati.voice !== void 0) {\n if (typeof dati.voice !== "string" || !VOICE.has(dati.voice)) {\n errori.push("voice: must be none, room, team, or proximity.");\n } else voice = dati.voice;\n }\n const modes = [];\n if (dati.modes !== void 0) {\n if (!Array.isArray(dati.modes)) errori.push("modes: must be an array.");\n else {\n const ids = /* @__PURE__ */ new Set();\n for (const [indice, raw] of dati.modes.entries()) {\n const value = oggetto(raw);\n if (value === null) {\n errori.push(`modes[${indice}]: must be an object.`);\n continue;\n }\n for (const campo of Object.keys(value)) {\n if (!["id", "players", "lobby", "matchmaking", "execution", "label", "instructions"].includes(campo)) errori.push(`modes[${indice}].${campo}: unknown field.`);\n }\n if (typeof value.id !== "string" || value.id.length > 32 || !ID_INTERNO.test(value.id)) {\n errori.push(`modes[${indice}].id: must be 1-32 lowercase letters, digits, or internal hyphens.`);\n continue;\n }\n if (ids.has(value.id)) {\n errori.push(`modes[${indice}].id: duplicate mode ${value.id}.`);\n continue;\n }\n ids.add(value.id);\n const modo = { id: value.id };\n for (const [key2, max] of [["label", 48], ["instructions", 160]]) {\n const text = testoFacoltativo(value, key2, max, `modes[${indice}]`, errori);\n if (text !== void 0) modo[key2] = text;\n }\n if (value.execution !== void 0) {\n if (value.execution !== "local" && value.execution !== "room") errori.push(`modes[${indice}].execution: must be local or room.`);\n else modo.execution = value.execution;\n }\n if (overlay !== null && modo.execution === void 0) errori.push(`modes[${indice}].execution: is required with the standard overlay.`);\n if (value.players !== void 0) {\n const campo = `modes[${indice}].players`;\n const range = oggetto(value.players);\n if (range === null) errori.push(`${campo}: must be an object with min and max.`);\n else {\n for (const key2 of Object.keys(range)) {\n if (key2 !== "min" && key2 !== "max") errori.push(`${campo}.${key2}: unknown field.`);\n }\n if (!interoTra(range.min, 1, TETTO_GIOCATORI)) errori.push(`${campo}.min: must be an integer from 1 to ${TETTO_GIOCATORI}.`);\n if (!interoTra(range.max, 1, TETTO_GIOCATORI)) errori.push(`${campo}.max: must be an integer from 1 to ${TETTO_GIOCATORI}.`);\n if (interoTra(range.min, 1, TETTO_GIOCATORI) && interoTra(range.max, 1, TETTO_GIOCATORI)) {\n if (range.min > range.max) errori.push(`${campo}.max: must be greater than or equal to min.`);\n else modo.players = { min: range.min, max: range.max };\n }\n }\n }\n if (value.lobby !== void 0) {\n if (typeof value.lobby !== "boolean") errori.push(`modes[${indice}].lobby: must be a boolean.`);\n else modo.lobby = value.lobby;\n }\n if (modo.execution === "local") {\n const range = modo.players ?? players;\n if (range.min !== 1 || range.max !== 1) errori.push(`modes[${indice}].players: local execution requires min and max to be 1.`);\n if (modo.lobby ?? lobby) errori.push(`modes[${indice}].lobby: local execution requires false.`);\n if (value.matchmaking !== void 0) errori.push(`modes[${indice}].matchmaking: local execution cannot use matchmaking.`);\n }\n if (value.matchmaking === void 0) {\n modes.push(modo);\n continue;\n }\n const matchmaking = oggetto(value.matchmaking);\n if (matchmaking === null) {\n errori.push(`modes[${indice}].matchmaking: must be an object.`);\n continue;\n }\n for (const campo of Object.keys(matchmaking)) {\n if (!["key", "timeoutMs", "defaults"].includes(campo)) {\n errori.push(`modes[${indice}].matchmaking.${campo}: unknown field.`);\n }\n }\n let valido = true;\n const key = [];\n if (!Array.isArray(matchmaking.key) || matchmaking.key.length < 1 || matchmaking.key.length > 8) {\n errori.push(`modes[${indice}].matchmaking.key: must contain from 1 to 8 fields.`);\n valido = false;\n } else for (const [keyIndice, item] of matchmaking.key.entries()) {\n if (typeof item !== "string" || !CAMPO_MATCHMAKING.test(item)) {\n errori.push(`modes[${indice}].matchmaking.key[${keyIndice}]: must be 1-32 lowercase letters, digits, or hyphens and start with a letter or digit.`);\n valido = false;\n } else if (key.includes(item)) {\n errori.push(`modes[${indice}].matchmaking.key[${keyIndice}]: duplicate field ${item}.`);\n valido = false;\n } else key.push(item);\n }\n if (!interoTra(matchmaking.timeoutMs, 1e3, 3e5)) {\n errori.push(`modes[${indice}].matchmaking.timeoutMs: must be an integer from 1000 to 300000.`);\n valido = false;\n }\n let defaults;\n if (matchmaking.defaults !== void 0) {\n const values = oggetto(matchmaking.defaults);\n if (values === null || Object.keys(values).length !== key.length || key.some((field) => !Object.hasOwn(values, field))) {\n errori.push(`modes[${indice}].matchmaking.defaults: must contain exactly the declared key fields.`);\n } else {\n defaults = {};\n for (const [field, value2] of Object.entries(values)) {\n if (!(typeof value2 === "string" && value2.length >= 1 && value2.length <= 64 && /^[A-Za-z0-9_.:-]+$/.test(value2)) && !Number.isSafeInteger(value2)) {\n errori.push(`modes[${indice}].matchmaking.defaults.${field}: must be a string of 1-64 characters or a safe integer.`);\n } else Object.defineProperty(defaults, field, { value: value2, enumerable: true });\n }\n }\n }\n if (valido) modes.push({ ...modo, matchmaking: {\n ...defaults === void 0 ? {} : { defaults },\n key,\n timeoutMs: matchmaking.timeoutMs\n } });\n }\n }\n }\n if (overlay !== null && modes.length === 0) errori.push("modes: at least one explicit mode is required with the standard overlay.");\n if (errori.length > 0) return { ok: false, errori };\n return { ok: true, manifest: {\n manifest: 1,\n overlay,\n id,\n name,\n description,\n cover,\n card,\n icon,\n screenshots,\n tags,\n languages: languages2,\n language,\n platform,\n orientation,\n input,\n visibility,\n network,\n isolated,\n requires,\n players,\n lobby,\n persistent,\n spectators,\n boards,\n roles,\n teams,\n voice,\n modes\n } };\n}\n\n// ../contracts/src/device.ts\nfunction deviceTier(report) {\n if (report.gpu !== "hardware" || report.memoryMb !== null && report.memoryMb <= 2048) return "low";\n if (report.mobile || report.memoryMb !== null && report.memoryMb <= 4096 || report.cores !== null && report.cores <= 4) return "mid";\n return "high";\n}\nfunction perdiContesto(context) {\n try {\n context?.getExtension("WEBGL_lose_context")?.loseContext();\n } catch {\n }\n}\nfunction valoriSincroni(ambiente) {\n let navigator2;\n try {\n navigator2 = ambiente.navigator;\n } catch {\n navigator2 = void 0;\n }\n let memoryMb = null;\n try {\n const memory = navigator2?.deviceMemory;\n const converted = typeof memory === "number" ? memory * 1024 : NaN;\n if (Number.isFinite(converted)) memoryMb = converted;\n } catch {\n memoryMb = null;\n }\n let cores = null;\n try {\n const value = navigator2?.hardwareConcurrency;\n if (typeof value === "number" && Number.isFinite(value)) cores = value;\n } catch {\n cores = null;\n }\n let mobile = false;\n try {\n mobile = typeof navigator2?.userAgentData?.mobile === "boolean" ? navigator2.userAgentData.mobile : /Android|iPhone|iPad|iPod|Mobile/i.test(navigator2?.userAgent ?? "");\n } catch {\n mobile = false;\n }\n let isolated = false;\n try {\n isolated = ambiente.crossOriginIsolated === true;\n } catch {\n isolated = false;\n }\n return {\n webgl2: false,\n webgpu: false,\n wasm: false,\n threads: false,\n isolated,\n gpu: "none",\n memoryMb,\n cores,\n mobile\n };\n}\nasync function probeDevice(globals, timeoutMs = 1500) {\n const ambiente = globals ?? globalThis;\n const report = valoriSincroni(ambiente);\n const webgl = Promise.resolve().then(() => {\n try {\n const canvas = ambiente.document?.createElement("canvas");\n if (canvas === void 0) return;\n const hardware = canvas.getContext("webgl2", { failIfMajorPerformanceCaveat: true });\n if (hardware !== null) {\n report.webgl2 = true;\n report.gpu = "hardware";\n perdiContesto(hardware);\n return;\n }\n const software = canvas.getContext("webgl2");\n if (software !== null) {\n report.webgl2 = true;\n report.gpu = "software";\n perdiContesto(software);\n }\n } catch {\n report.webgl2 = false;\n report.gpu = "none";\n }\n });\n const webgpu = Promise.resolve().then(async () => {\n let device;\n try {\n const gpu = ambiente.navigator?.gpu;\n if (gpu === void 0) return;\n const adapter = await gpu.requestAdapter();\n if (adapter === null) return;\n device = await adapter.requestDevice();\n report.webgpu = true;\n } catch {\n report.webgpu = false;\n } finally {\n try {\n device?.destroy?.();\n } catch {\n }\n }\n });\n const wasm = Promise.resolve().then(() => {\n try {\n report.wasm = ambiente.WebAssembly?.validate(\n new Uint8Array([0, 97, 115, 109, 1, 0, 0, 0])\n ) === true;\n } catch {\n report.wasm = false;\n }\n });\n const threads = Promise.resolve().then(() => {\n try {\n if (ambiente.WebAssembly === void 0) return;\n new ambiente.WebAssembly.Memory({ initial: 1, maximum: 1, shared: true });\n report.threads = true;\n } catch {\n report.threads = false;\n }\n });\n let timer;\n await Promise.race([\n Promise.all([webgl, webgpu, wasm, threads]),\n new Promise((resolve) => {\n timer = setTimeout(resolve, Math.max(0, timeoutMs));\n })\n ]);\n if (timer !== void 0) clearTimeout(timer);\n return { ...report, tier: deviceTier(report) };\n}\n\n// ../contracts/src/overlay.ts\nfunction overlayConfiguration(manifest, coverUrl = null, invite = null, iconUrl = null) {\n const validated = validaManifest(manifest);\n if (!validated.ok) throw new Error("The overlay manifest is invalid.");\n return { manifest: validated.manifest, coverUrl, iconUrl, invite };\n}\nfunction record(value) {\n return typeof value === "object" && value !== null && !Array.isArray(value) ? value : null;\n}\nfunction validOverlayHello(value) {\n const hello = record(value), config = record(hello?.configuration);\n return hello?.v === 1 && typeof hello.epoch === "string" && hello.epoch.length > 0 && hello.epoch.length <= 128 && config !== null && (config.coverUrl === null || typeof config.coverUrl === "string") && (config.iconUrl === null || typeof config.iconUrl === "string") && (config.invite === null || typeof config.invite === "string" && /^[ABCDEFGHJKLMNPQRSTUVWXYZ23456789]{6}$/.test(config.invite)) && validaManifest(config.manifest).ok;\n}\nfunction normalizeOverlayHello(value) {\n if (!validOverlayHello(value)) return null;\n return { v: 1, epoch: value.epoch, configuration: overlayConfiguration(value.configuration.manifest, value.configuration.coverUrl, value.configuration.invite, value.configuration.iconUrl) };\n}\nfunction validSafeArea(value) {\n const area = record(value);\n return area !== null && Object.keys(area).length === 4 && ["top", "right", "bottom", "left"].every((key) => typeof area[key] === "number" && Number.isFinite(area[key]) && Number(area[key]) >= 0 && Number(area[key]) <= 1e5);\n}\nfunction validOverlayView(value) {\n const data = record(value);\n return data !== null && Object.keys(data).every((key) => ["inputBlocked", "reservedRects", "safeArea", "shortcutEnabled"].includes(key)) && (data.safeArea === void 0 || validSafeArea(data.safeArea)) && (data.shortcutEnabled === void 0 || typeof data.shortcutEnabled === "boolean") && typeof data.inputBlocked === "boolean" && Array.isArray(data.reservedRects) && data.reservedRects.length <= 8 && data.reservedRects.every((value2) => {\n const rect = record(value2);\n return rect !== null && Object.keys(rect).length === 4 && ["x", "y", "width", "height"].every((key) => typeof rect[key] === "number" && Number.isFinite(rect[key]) && rect[key] >= 0 && rect[key] <= 1e5);\n });\n}\nfunction validOverlayRequest(value) {\n const message = record(value), args = record(message?.args);\n if (message?.type !== "caisual:overlay" || message.v !== 1 || typeof message.epoch !== "string" || message.epoch.length < 1 || message.epoch.length > 128 || typeof message.requestId !== "string" || !(/^[1-9][0-9]{0,15}$/.test(message.requestId) && Number.isSafeInteger(Number(message.requestId))) || args === null) return false;\n if (Object.keys(message).some((key) => !["type", "v", "epoch", "requestId", "sessionId", "op", "args"].includes(key)) || !(message.sessionId === void 0 || message.sessionId === null || typeof message.sessionId === "string" && /^[1-9][0-9]{0,15}$/.test(message.sessionId))) return false;\n const keys = (...allowed) => Object.keys(args).every((key) => allowed.includes(key));\n const text = (key) => typeof args[key] === "string" && args[key].length >= 1 && args[key].length <= 64;\n switch (message.op) {\n case "local.start":\n return keys("mode") && text("mode");\n case "room.create":\n return keys("mode") && (args.mode === null || text("mode"));\n case "room.join":\n return keys("code") && (args.code === void 0 || text("code"));\n case "room.watch":\n return keys("code") && text("code");\n case "room.match": {\n const key = record(args.key);\n return keys("mode", "key") && text("mode") && (args.key === void 0 || key !== null && Object.keys(key).length <= 8 && Object.values(key).every((v) => typeof v === "string" && v.length >= 1 && v.length <= 64 || typeof v === "number" && Number.isSafeInteger(v)));\n }\n case "room.ready":\n return keys("ready") && typeof args.ready === "boolean";\n case "room.role":\n case "room.requestRole":\n return keys("role") && text("role");\n case "room.team":\n return keys("team") && Number.isInteger(args.team) && args.team >= 1 && args.team <= 24;\n case "room.restart":\n case "room.start":\n case "session.cancel":\n case "session.leave":\n case "session.disconnect":\n case "session.resume":\n return keys();\n case "voice.join":\n case "voice.leave":\n return keys() && typeof message.sessionId === "string";\n case "voice.mute":\n return keys("muted") && typeof args.muted === "boolean" && typeof message.sessionId === "string";\n case "voice.setVolume":\n return keys("playerId", "volume") && typeof message.sessionId === "string" && typeof args.playerId === "string" && args.playerId.length > 0 && args.playerId.length <= 128 && typeof args.volume === "number" && Number.isFinite(args.volume) && args.volume >= 0 && args.volume <= 1;\n case "overlay.view":\n return validOverlayView(args);\n default:\n return false;\n }\n}\nfunction validBoardDay(value) {\n if (typeof value !== "string" || !/^\\d{4}-\\d{2}-\\d{2}$/.test(value)) return false;\n const at = Date.parse(`${value}T00:00:00Z`);\n return Number.isFinite(at) && new Date(at).toISOString().slice(0, 10) === value;\n}\n\n// ../contracts/src/room-limits.ts\nvar MESSAGGI_GIOCO_AL_SECONDO = 20;\n\n// ../contracts/src/match-result.ts\nfunction readMatchResult(value, playerIds) {\n const object = (v) => v !== null && typeof v === "object" && !Array.isArray(v) ? v : null;\n const result = object(value);\n if (!result || !Array.isArray(result.standings)) return null;\n const known = new Set(playerIds), seen = /* @__PURE__ */ new Set();\n const standings = [];\n for (const item of result.standings) {\n const row = object(item);\n if (!row || typeof row.playerId !== "string" || !known.has(row.playerId) || seen.has(row.playerId)) continue;\n seen.add(row.playerId);\n standings.push({\n playerId: row.playerId,\n ...typeof row.score === "number" && Number.isFinite(row.score) ? { score: row.score } : {},\n ...typeof row.rank === "number" && Number.isSafeInteger(row.rank) && row.rank > 0 ? { rank: row.rank } : {}\n });\n }\n if (!standings.length) return null;\n return {\n standings,\n ...Array.isArray(result.winners) ? { winners: [...new Set(result.winners.filter((id) => typeof id === "string" && seen.has(id)))] } : {},\n ...typeof result.draw === "boolean" ? { draw: result.draw } : {},\n ...typeof result.unit === "string" ? { unit: result.unit } : {}\n };\n}\n\n// src/overlay/i18n.ts\nvar languages = ["en", "it", "es", "fr", "de", "pt"];\nvar words = {\n gameLanguages: ["Game languages", "Lingue del gioco", "Idiomas del juego", "Langues du jeu", "Spielsprachen", "Idiomas do jogo"],\n loading: ["Loading game...", "Caricamento...", "Cargando...", "Chargement...", "Spiel wird geladen...", "Carregando..."],\n loadingSlow: ["This game is taking longer than expected. You can wait a little longer or try again.", "Il gioco ci sta mettendo pi\\xF9 del previsto. Puoi aspettare ancora un po\\u2019 o riprovare.", "El juego est\\xE1 tardando m\\xE1s de lo esperado. Puedes esperar un poco m\\xE1s o volver a intentarlo.", "Le jeu met plus de temps que pr\\xE9vu. Vous pouvez patienter encore un peu ou r\\xE9essayer.", "Das Spiel braucht l\\xE4nger als erwartet. Du kannst noch etwas warten oder es erneut versuchen.", "O jogo est\\xE1 demorando mais do que o esperado. Voc\\xEA pode esperar mais um pouco ou tentar novamente."],\n home: ["Play", "Gioca", "Jugar", "Jouer", "Spielen", "Jogar"],\n homeMenu: ["Menu", "Menu", "Men\\xFA", "Menu", "Men\\xFC", "Menu"],\n mode: ["Mode", "Modalit\\xE0", "Modo", "Mode", "Modus", "Modo"],\n play: ["Play", "Gioca", "Jugar", "Jouer", "Spielen", "Jogar"],\n friendsPlay: ["Play with friends", "Gioca con amici", "Jugar con amigos", "Jouer entre amis", "Mit Freunden spielen", "Jogar com amigos"],\n find: ["Find players", "Trova giocatori", "Buscar jugadores", "Trouver des joueurs", "Spieler finden", "Buscar jogadores"],\n join: ["Join with code", "Entra con codice", "Entrar con c\\xF3digo", "Rejoindre avec un code", "Mit Code beitreten", "Entrar com c\\xF3digo"],\n joinInvite: ["Join this room", "Entra in questa stanza", "Entrar en est\\xE1 sala", "Rejoindre cette salle", "Diesem Raum beitreten", "Entrar nest\\xE1 sala"],\n watch: ["Watch a room", "Guarda una stanza", "Observar una sala", "Regarder une salle", "Raum ansehen", "Assistir a uma sala"],\n resume: ["Resume", "Riprendi", "Continuar", "Reprendre", "Fortsetzen", "Continuar"],\n room: ["Room", "Stanza", "Sala", "Salle", "Raum", "Sala"],\n code: ["Room code", "Codice stanza", "C\\xF3digo de sala", "Code de salle", "Raumcode", "C\\xF3digo da sala"],\n copy: ["Copy invite", "Copia invito", "Copiar invitaci\\xF3n", "Copier le lien", "Einladung kopieren", "Copiar convite"],\n copied: ["Invite copied", "Invito copiato", "Invitacion copiada", "Lien copi\\xE9", "Einladung kopiert", "Convite copiado"],\n copyFailed: ["Copy this link:", "Copia questo link:", "Copia este enlace:", "Copiez ce lien :", "Diesen Link kopieren:", "Copie este link:"],\n joining: ["Joining room...", "Ingresso nella stanza...", "Entrando en la sala...", "Connexion \\xE0 la salle...", "Raum wird betreten...", "Entrando na sala..."],\n matching: ["Finding your people...", "Ricerca giocatori...", "Buscando jugadores...", "Recherche de joueurs...", "Spieler werden gesucht...", "Buscando jogadores..."],\n queue: ["{n} / {max} players", "{n} / {max} giocatori", "{n} / {max} jugadores", "{n} / {max} joueurs", "{n} / {max} Spieler", "{n} / {max} jogadores"],\n cancel: ["Cancel", "Annulla", "Cancelar", "Annuler", "Abbrechen", "Cancelar"],\n close: ["Close", "Chiudi", "Cerrar", "Fermer", "Schlie\\xDFen", "Fechar"],\n back: ["Back", "Indietro", "Volver", "Retour", "Zur\\xFCck", "Voltar"],\n ready: ["Ready", "Pronto", "Listo", "Pr\\xEAt", "Bereit", "Pronto"],\n unready: ["Not ready", "Non pronto", "No listo", "Pas pr\\xEAt", "Nicht bereit", "N\\xE3o pronto"],\n start: ["Start", "Inizia", "Empezar", "Commencer", "Starten", "Come\\xE7ar"],\n role: ["Role", "Ruolo", "Rol", "R\\xF4le", "Rolle", "Fun\\xE7\\xE3o"],\n team: ["Team", "Squadra", "Equipo", "\\xC9quipe", "Team", "Equipe"],\n host: ["Host", "Host", "Anfitrion", "H\\xF4te", "Host", "Anfitri\\xE3o"],\n you: ["You", "Tu", "T\\xFA", "Vous", "Du", "Voc\\xEA"],\n away: ["Away", "Assente", "Ausente", "Absent", "Abwesend", "Ausente"],\n needPlayers: ["Waiting for more players", "In attesa di giocatori", "Esperando m\\xE1s jugadores", "En attente de joueurs", "Weitere Spieler fehlen", "Esperando mais jogadores"],\n needReady: ["Everyone needs to be ready", "Tutti devono essere pronti", "Todos deben estar listos", "Tout le monde doit \\xEAtre pr\\xEAt", "Alle m\\xFCssen bereit sein", "Todos precisam estar prontos"],\n needRoles: ["Fill the required roles", "Completa i ruoli richiesti", "Completa los roles", "Compl\\xE9tez les r\\xF4les", "Ben\\xF6tigte Rollen besetzen", "Complete as fun\\xE7\\xF5es"],\n needTeams: ["Choose the required teams", "Scegli le squadre richieste", "Elige los equipos", "Choisissez les \\xE9quipes", "Teams auswahlen", "Escolha as equipes"],\n waitHost: ["Waiting for the host", "In attesa dell\'host", "Esperando al anfitrion", "En attente de l\\u2019h\\xF4te", "Warten auf den Host", "Esperando o anfitri\\xE3o"],\n starting: ["Starting in", "Si inizia tra", "Empieza en", "D\\xE9but dans", "Start in", "Come\\xE7a em"],\n playing: ["Playing", "In partita", "Jugando", "En jeu", "Im Spiel", "Jogando"],\n ended: ["Game finished", "Partita conclusa", "Partida terminada", "Partie termin\\xE9e", "Spiel beendet", "Partida encerrada"],\n rematchReady: ["{n}/{max} ready", "{n}/{max} pronti", "{n}/{max} listos", "{n}/{max} pr\\xEAts", "{n}/{max} bereit", "{n}/{max} prontos"],\n rematchStart: ["Start rematch", "Avvia rivincita", "Iniciar revancha", "Lancer la revanche", "Revanche starten", "Iniciar revanche"],\n won: ["You won", "Hai vinto", "Has ganado", "Vous avez gagn\\xE9", "Du hast gewonnen", "Voc\\xEA venceu"],\n lost: ["You lost", "Hai perso", "Has perdido", "Vous avez perdu", "Du hast verloren", "Voc\\xEA perdeu"],\n draw: ["Draw", "Pareggio", "Empate", "\\xC9galit\\xE9", "Unentschieden", "Empate"],\n standings: ["Standings", "Piazzamenti", "Posiciones", "R\\xE9sultats", "Platzierungen", "Coloca\\xE7\\xF5es"],\n points: ["points", "punti", "puntos", "points", "Punkte", "pontos"],\n time: ["time", "tempo", "tiempo", "temps", "Zeit", "tempo"],\n distance: ["distance", "distanza", "distancia", "distance", "Distanz", "dist\\xE2ncia"],\n again: ["Play again", "Gioca ancora", "Jugar de nuevo", "Rejouer", "Erneut spielen", "Jogar novamente"],\n newRoom: ["New room. Share the new invite.", "Nuova stanza. Condividi il nuovo invito.", "Nueva sala. Comparte la invitaci\\xF3n.", "Nouvelle salle. Partagez le lien.", "Neuer Raum. Neue Einladung teilen.", "Nova sala. Compartilhe o convite."],\n watching: ["Watching", "In osservazione", "Observando", "Spectateur", "Zuschauen", "Assistindo"],\n delay: ["{n}s delay", "Ritardo {n}s", "Retraso de {n}s", "Retard de {n}s", "{n}s Verz\\xF6gerung", "Atraso de {n}s"],\n exit: ["Exit", "Esci", "Salir", "Quitter", "Verlassen", "Sair"],\n leaveNow: ["Leave for now", "Esci per ora", "Salir por ahora", "Quitter pour le moment", "Vor\\xFCbergehend verlassen", "Sair por enquanto"],\n leaveRoom: ["Leave room", "Lascia la stanza", "Abandonar sala", "Abandonner la salle", "Raum verlassen", "Deixar a sala"],\n leaveHint: ["Your room stays available for Resume.", "La stanza resta disponibile con Riprendi.", "Podr\\xE1s volver a est\\xE1 sala.", "Vous pourrez reprendre cette salle.", "Du kannst den Raum fortsetzen.", "Voc\\xEA pode voltar a est\\xE1 sala."],\n temporaryHint: ["The game continues. Rejoining may only be possible briefly.", "La partita continua. Il rientro pu\\xF2 essere disponibile solo per poco.", "La partida continua. Volver puede ser posible solo por poco tiempo.", "La partie continue. Le retour peut \\xEAtre limit\\xE9.", "Das Spiel l\\xE4uft weiter. R\\xFCckkehr nur kurz m\\xF6glich.", "A partida continua. O retorno pode ser limitado."],\n abandonHint: ["Leave room gives up your place.", "Lascia la stanza libera il tuo posto.", "Abandonar libera tu plaza.", "Abandonner lib\\xE8re votre place.", "Raum verlassen gibt deinen Platz frei.", "Deixar a sala libera sua vaga."],\n reconnecting: ["Reconnecting...", "Riconnessione...", "Reconectando...", "Reconnexion...", "Verbindung wird erneuert...", "Reconectando..."],\n replaced: ["Opened in another tab", "Aperta in un\\u2019altra scheda", "Abierta en otra pest\\xE1na", "Ouverte dans un autre onglet", "In anderem Tab ge\\xF6ffnet", "Aberta em outra aba"],\n error: ["Something went wrong. Try again.", "Qualcosa non va. Riprova.", "Algo sali\\xF3 mal. Reintenta.", "Une erreur est survenue. R\\xE9essayez.", "Etwas ist schiefgelaufen. Erneut versuchen.", "Algo deu errado. Tente novamente."],\n noRoom: ["This room is no longer available.", "Questa stanza non \\xE8 pi\\xF9 disponibile.", "Esta sala ya no est\\xE1 disponible.", "Cette salle n\'est plus disponible.", "Dieser Raum ist nicht mehr verf\\xFCgbar.", "Esta sala n\\xE3o est\\xE1 mais disponivel."],\n full: ["This room is full.", "La stanza \\xE8 piena.", "La sala est\\xE1 llena.", "Cette salle est pleine.", "Dieser Raum ist voll.", "Esta sala est\\xE1 cheia."],\n noMatch: ["No match this time. Try again.", "Nessun gruppo trovato. Riprova.", "No hay grupo. Reintenta.", "Aucun groupe trouv\\xE9. R\\xE9essayez.", "Keine Gruppe gefunden. Erneut versuchen.", "Nenhum grupo encontrado. Tente novamente."],\n invalidCode: ["Enter a six-character room code.", "Inserisci un codice di sei caratteri.", "Escribe un c\\xF3digo de seis caracteres.", "Entrez un code de six caracteres.", "Sechsstelligen Raumcode eingeben.", "Digite um c\\xF3digo de seis caracteres."],\n refused: ["The room did not accept that change.", "La stanza ha rifiutato la modifica.", "La sala rechaz\\xF3 el cambio.", "La salle a refus\\xE9 ce changement.", "Der Raum hat die \\xC4nderung abgelehnt.", "A sala recusou a altera\\xE7\\xE3o."],\n unavailable: ["Unavailable right now", "Non disponibile ora", "No disponible ahora", "Indisponible pour le moment", "Derzeit nicht verf\\xFCgbar", "Indisponivel agora"],\n offline: ["Connection unavailable. Try again.", "Connessione non disponibile. Riprova.", "Sin conexi\\xF3n. Reintenta.", "Connexion indisponible. R\\xE9essayez.", "Keine Verbindung. Erneut versuchen.", "Sem conex\\xE3o. Tente novamente."],\n saveFailed: ["Keep the room code. Resume could not be saved.", "Conserva il codice. Riprendi non \\xE8 stato salvato.", "Guarda el c\\xF3digo. No se pudo guardar el regreso.", "Gardez le code. La reprise ne peut pas \\xEAtre enregistr\\xE9e.", "Raumcode aufbewahren. Fortsetzen nicht gespeichert.", "Guarde o c\\xF3digo. O retorno n\\xE3o foi salvo."],\n boards: ["Leaderboard", "Classifica", "Clasificaci\\xF3n", "Classement", "Bestenliste", "Classifica\\xE7\\xE3o"],\n board: ["Board", "Classifica", "Tabla", "Classement", "Bestenliste", "Tabela"],\n daily: ["Daily", "Giornaliera", "Diaria", "Du jour", "T\\xE4glich", "Di\\xE1ria"],\n allTime: ["All time", "Di sempre", "Hist\\xF3rica", "Tous les temps", "Gesamt", "Geral"],\n accounts: ["Accounts", "Account", "Cuentas", "Comptes", "Konten", "Contas"],\n guests: ["Guests", "Ospiti", "Invitados", "Invit\\xE9s", "G\\xE4ste", "Visitantes"],\n category: ["Category", "Categoria", "Categoria", "Cat\\xE9gorie", "Kategorie", "Categoria"],\n period: ["Period", "Periodo", "Per\\xEDodo", "P\\xE9riode", "Zeitraum", "Per\\xEDodo"],\n rank: ["Rank", "Posizione", "Puesto", "Rang", "Platz", "Posicao"],\n score: ["Score", "Punteggio", "Puntos", "Score", "Punkte", "Pontos"],\n verified: ["Verified", "Verificato", "Verificado", "V\\xE9rifi\\xE9", "Verifiziert", "Verificado"],\n own: ["Your best", "Il tuo record", "Tu record", "Votre record", "Dein Rekord", "Seu recorde"],\n empty: ["No scores yet", "Nessun punteggio", "A\\xFAn no hay puntos", "Aucun score", "Noch keine Punkte", "Ainda sem pontos"],\n saving: ["Saving score...", "Salvataggio punteggio...", "Guardando puntos...", "Enregistrement du score...", "Punkte werden gespeichert...", "Salvando pontos..."],\n saved: ["Your best is on the board", "Il tuo record \\xE8 in classifica", "Tu record est\\xE1 en la tabla", "Votre record est au classement", "Dein Rekord ist eingetragen", "Seu recorde est\\xE1 na tabela"],\n bestAlready: ["Your best is already on the board", "Il tuo record era gi\\xE0 in classifica", "Tu record ya estaba en la tabla", "Votre record est d\\xE9j\\xE0 au classement", "Dein Rekord ist bereits eingetragen", "Seu recorde j\\xE1 est\\xE1 na tabela"],\n refresh: ["Refresh", "Aggiorna", "Actualizar", "Actualiser", "Aktualisieren", "Atualizar"],\n refreshHint: ["Score not visible yet. Refresh to check.", "Punteggio non ancora visibile. Aggiorna per controllare.", "Puntos a\\xFAn no visibles. Actualiza.", "Score pas encore visible. Actualisez.", "Punkte noch nicht sichtbar. Aktualisieren.", "Pontos ainda n\\xE3o visiveis. Atualize."],\n friends: ["Friends & party", "Amici e gruppo", "Amigos y grupo", "Amis et groupe", "Freunde & Gruppe", "Amigos e grupo"],\n localCrew: ["Friends and party are unavailable in local preview.", "Amici e gruppo non disponibili in anteprima locale.", "Amigos y grupo no disponibles en la vista local.", "Amis et groupe indisponibles en aper\\xE7u local.", "Freunde und Gruppe in lokaler Vorschau nicht verf\\xFCgbar.", "Amigos e grupo indispon\\xEDveis na pr\\xE9via local."],\n loginCrew: ["Sign in on Caisual to use friends and party.", "Accedi a Caisual per amici e gruppo.", "Inicia sesion para amigos y grupo.", "Connectez-vous pour utiliser amis et groupe.", "F\\xFCr Freunde und Gruppe bei Caisual anmelden.", "Entre no Caisual para amigos e grupo."],\n online: ["Online", "Online", "En linea", "En ligne", "Online", "Online"],\n noFriends: ["No friends online", "Nessun amico online", "Sin amigos en linea", "Aucun ami en ligne", "Keine Freunde online", "Nenhum amigo online"],\n createParty: ["Create party", "Crea gruppo", "Crear grupo", "Cr\\xE9er un groupe", "Gruppe erstellen", "Criar grupo"],\n inviteParty: ["Invite to party", "Invita nel gruppo", "Invitar al grupo", "Inviter au groupe", "In Gruppe einladen", "Convidar para o grupo"],\n leaveParty: ["Leave party", "Lascia gruppo", "Salir del grupo", "Quitter le groupe", "Gruppe verlassen", "Sair do grupo"],\n accept: ["Accept", "Accetta", "Aceptar", "Accepter", "Annehmen", "Aceitar"],\n decline: ["Decline", "Rifiuta", "Rechazar", "Refuser", "Ablehnen", "Recusar"],\n follow: ["Join them", "Raggiungi", "Unirse", "Rejoindre", "Beitreten", "Juntar-se"],\n voice: ["Voice", "Voce", "Voz", "Voix", "Sprache", "Voz"],\n voiceJoin: ["Join voice", "Entra in voce", "Unirse a voz", "Activer la voix", "Sprachchat beitreten", "Entrar na voz"],\n voiceLeave: ["Leave voice", "Esci dalla voce", "Salir de voz", "Quitter la voix", "Sprachchat verlassen", "Sair da voz"],\n voiceMute: ["Mute", "Disattiva microfono", "Silenciar", "Couper le micro", "Stummschalten", "Silenciar"],\n voiceUnmute: ["Unmute", "Attiva microfono", "Activar micr\\xF3fono", "Activer le micro", "Mikrofon aktivieren", "Ativar microfone"],\n voiceOff: ["Voice off", "Voce disattivata", "Voz desactivada", "Voix d\\xE9sactiv\\xE9e", "Sprachchat aus", "Voz desativada"],\n voiceJoining: ["Joining voice...", "Connessione voce...", "Conectando voz...", "Connexion vocale...", "Sprachchat verbindet...", "Conectando voz..."],\n voiceOn: ["Voice connected", "Voce connessa", "Voz conectada", "Voix connect\\xE9e", "Sprachchat verbunden", "Voz conectada"],\n voiceMuted: ["Muted", "Microfono disattivato", "Silenciado", "Micro coup\\xE9", "Stumm", "Silenciado"],\n voiceMic: ["Mic on", "Microfono attivo", "Micr\\xF3fono activo", "Micro actif", "Mikrofon an", "Microfone ativo"],\n voiceListening: ["Listening only", "Solo ascolto", "Solo escucha", "\\xC9coute seule", "Nur zuh\\xF6ren", "Somente ouvindo"],\n voiceSpeaking: ["Speaking", "Sta parlando", "Hablando", "Parle", "Spricht", "Falando"],\n voicePeers: ["Voice participants", "Partecipanti in voce", "Participantes de voz", "Participants vocaux", "Sprachteilnehmer", "Participantes de voz"],\n voiceEmpty: ["No one else in voice yet.", "Nessun altro in voce per ora.", "A\\xFAn no hay nadie m\\xE1s en voz.", "Personne d\\u2019autre en voix pour le moment.", "Noch niemand im Sprachchat.", "Ningu\\xE9m mais na voz ainda."],\n voiceVolume: ["Volume for {name}", "Volume di {name}", "Volumen de {name}", "Volume de {name}", "Lautst\\xE4rke f\\xFCr {name}", "Volume de {name}"],\n voiceUnavailable: ["Join a room with voice to use these controls.", "Entra in una stanza con voce per usare questi controlli.", "Entra en una sala con voz para usar estos controles.", "Rejoignez une salle vocale pour utiliser ces commandes.", "Diese Steuerung braucht einen Raum mit Sprachchat.", "Entre em uma sala com voz para usar estes controles."],\n voiceWatch: ["Voice is unavailable while watching.", "La voce non e\' disponibile in osservazione.", "La voz no est\\xE1 disponible al observar.", "La voix est indisponible en observation.", "Beim Zuschauen ist kein Sprachchat verf\\xFCgbar.", "A voz n\\xE3o est\\xE1 dispon\\xEDvel ao assistir."],\n voiceDenied: ["Microphone permission denied. Allow it in your browser, then try again.", "Permesso microfono negato. Consenti l\'accesso nel browser e riprova.", "Permiso de micr\\xF3fono denegado. Act\\xEDvalo en el navegador e int\\xE9ntalo de nuevo.", "Acc\\xE8s au micro refus\\xE9. Autorisez-le dans le navigateur, puis r\\xE9essayez.", "Mikrofonzugriff verweigert. Im Browser erlauben und erneut versuchen.", "Permiss\\xE3o do microfone negada. Permita no navegador e tente novamente."],\n voiceUnsupported: ["Voice is not supported in this browser.", "Questo browser non supporta la voce.", "Este navegador no admite voz.", "Ce navigateur ne prend pas en charge la voix.", "Dieser Browser unterst\\xFCtzt keinen Sprachchat.", "Este navegador n\\xE3o oferece suporte a voz."],\n voiceFailed: ["Voice could not connect. Try again.", "Connessione voce non riuscita. Riprova.", "No se pudo conectar la voz. Int\\xE9ntalo de nuevo.", "Connexion vocale impossible. R\\xE9essayez.", "Sprachverbindung fehlgeschlagen. Erneut versuchen.", "N\\xE3o foi poss\\xEDvel conectar a voz. Tente novamente."],\n voicePeerGone: ["This participant has left voice.", "Questo partecipante e\' uscito dalla voce.", "Este participante sali\\xF3 de voz.", "Ce participant a quitt\\xE9 la voix.", "Diese Person hat den Sprachchat verlassen.", "Este participante saiu da voz."],\n shortcut: ["Shift+Tab shortcut", "Scorciatoia Shift+Tab", "Atajo Shift+Tab", "Raccourci Maj+Tab", "Umschalt+Tab-Kurzbefehl", "Atalho Shift+Tab"],\n menu: ["Caisual menu", "Menu Caisual", "Menu Caisual", "Menu Caisual", "Caisual-Menu", "Menu Caisual"],\n retry: ["Retry", "Riprova", "Reintentar", "R\\xE9essayer", "Erneut versuchen", "Tentar novamente"]\n};\nvar column = (index) => Object.fromEntries(Object.entries(words).map(([key, row]) => [key, row[index]]));\nvar dictionaries = { en: column(0), it: column(1), es: column(2), fr: column(3), de: column(4), pt: column(5) };\nfunction overlayLocale(raw) {\n const tag = normalizeLanguage(raw);\n return tag && languages.includes(tag.split("-")[0]) ? tag : "en";\n}\n\n// src/text.ts\nfunction createTextLoader(fetcher, language, pathname = "/") {\n let pending;\n const root = pathname.match(/^\\/rt\\/[^/]+\\/[1-9][0-9]*\\//)?.[0] ?? "/";\n return () => pending ?? (pending = (async () => {\n let dictionary = {};\n try {\n const response = await fetcher(`${root}__caisual/text/${encodeURIComponent(language)}.json`);\n if (response.ok) {\n const value = await response.json();\n if (isTextDictionary(value)) dictionary = value;\n }\n } catch {\n }\n return (key, values = {}) => {\n if (!Object.hasOwn(dictionary, key)) return key;\n const text = dictionary[key];\n return text.replace(/\\{([^{}]+)\\}/g, (placeholder, name) => Object.hasOwn(values, name) ? String(values[name]) : placeholder);\n };\n })());\n}\n\n// src/errors.ts\nfunction creaErrore(code, message) {\n return Object.assign(new Error(message), { name: "CaisualError", code });\n}\nfunction erroreOffline() {\n return creaErrore("offline", "Caisual services are unavailable.");\n}\nfunction codiceErrore(valore) {\n return typeof valore === "object" && valore !== null && "code" in valore ? valore.code : null;\n}\n\n// src/session/resume.ts\nvar KEY = "caisual-session-v1";\nfunction resume(value) {\n const data = record(value);\n if (!data || typeof data.code !== "string" || !/^[ABCDEFGHJKLMNPQRSTUVWXYZ23456789]{6}$/.test(data.code) || !(data.mode === void 0 || data.mode === null || typeof data.mode === "string")) return null;\n return {\n version: 1,\n code: data.code,\n mode: typeof data.mode === "string" ? data.mode : null,\n updatedAt: typeof data.updatedAt === "number" && Number.isFinite(data.updatedAt) ? data.updatedAt : 0\n };\n}\nfunction createResume(save, changed) {\n let current = null, error = false, work = Promise.resolve();\n const write = async () => {\n const value = { version: 1, imported: true, resume: current };\n work = work.catch(() => void 0).then(async () => {\n try {\n await save.set(KEY, value);\n error = false;\n } catch (cause) {\n error = true;\n throw cause;\n } finally {\n changed();\n }\n });\n return work;\n };\n const loaded = (async () => {\n try {\n const data = record(await save.get(KEY));\n if (data?.version === 1 && data.imported === true) current = resume(data.resume);\n else {\n current = resume(await save.get("resume"));\n await write();\n }\n } catch {\n error = true;\n }\n changed();\n })();\n return {\n loaded,\n get value() {\n return current === null ? null : { ...current };\n },\n get error() {\n return error;\n },\n async set(value) {\n await loaded;\n current = value;\n changed();\n await write();\n }\n };\n}\n\n// src/session/index.ts\nfunction notify(listeners, value) {\n for (const listener of listeners) {\n try {\n listener(value);\n } catch {\n }\n }\n}\nfunction createSession(base, configuration = null, roomsAvailable = base.connected) {\n const standard = configuration?.manifest.overlay?.version === 1;\n const manifest = configuration?.manifest;\n let current = { kind: "idle" }, ready = false, operation = 0, identifier = 0;\n let pending = null, pendingMode = null;\n let waiting = null, controller = null;\n let stops = [], disposed = false, lastState = "";\n let view = { inputBlocked: standard, reservedRects: [], safeArea: { top: 0, right: 0, bottom: 0, left: 0 } };\n const listeners = /* @__PURE__ */ new Set();\n const viewListeners = /* @__PURE__ */ new Set();\n const stateListeners = /* @__PURE__ */ new Set();\n const openListeners = /* @__PURE__ */ new Set();\n const errorListeners = /* @__PURE__ */ new Set();\n const scoreListeners = /* @__PURE__ */ new Set();\n let resumeStore = null;\n const capabilities = () => ({\n local: true,\n rooms: roomsAvailable,\n overlay: standard,\n requestRole: current.kind === "room" && current.room.metadata.configuration?.requestRole === true\n });\n function voiceSnapshot() {\n if (current.kind !== "room" || !manifest || manifest.voice === "none") return null;\n const room = current.room, voice = room.voice;\n if (!voice || voice.mode === "none" || room.players.find((p) => p.id === room.you)?.role === "spectator") return null;\n return {\n mode: voice.mode,\n state: voice.state,\n mic: voice.mic,\n muted: voice.muted,\n speaking: voice.speaking,\n peers: voice.peers.map(({ id, mic, muted, speaking, volume }) => ({ id, mic, muted, speaking, volume }))\n };\n }\n function snapshot() {\n const attached = current.kind === "room" || current.kind === "watch" ? current.room : null;\n const configured = attached?.metadata.configuration;\n const result = attached ? readMatchResult(attached.result, attached.players.map((p) => p.id)) : null;\n const fallback = manifest && attached && (attached.mode === null || manifest.modes.some((m) => m.id === attached.mode)) ? risolviModalita(manifest, attached.mode) : { players: { min: 1, max: 1 }, lobby: false };\n return {\n kind: pending ?? (current.kind === "idle" ? ready ? "home" : "boot" : current.kind),\n id: current.kind === "idle" ? null : current.id,\n mode: pending ? pendingMode : current.kind === "local" ? current.mode : attached?.mode ?? null,\n localStatus: current.kind === "local" ? current.status : null,\n ready,\n capabilities: capabilities(),\n room: attached ? {\n ...attached.metadata.rematch?.keepSetup || attached.metadata.rematch?.autoStart ? { rematch: attached.metadata.rematch } : {},\n code: attached.code,\n mode: attached.mode,\n status: attached.status,\n host: attached.host,\n you: current.kind === "room" ? current.room.you : null,\n players: attached.players.map((p) => ({ id: p.id, name: p.name, guest: p.guest, role: p.role, team: p.team, ready: p.ready, connected: p.connected })),\n ...result ? { result } : {},\n countdownAt: attached.countdownAt,\n connection: attached.connection,\n closedCode: attached.metadata.closedCode,\n limits: { ...configured?.players ?? fallback.players },\n lobby: configured?.lobby ?? fallback.lobby,\n persistent: configured?.persistent ?? manifest?.persistent ?? false,\n delayMs: current.kind === "watch" ? current.room.delayMs : null,\n requestRole: configured?.requestRole ?? false\n } : null,\n voice: pending ? null : voiceSnapshot(),\n waiting: waiting ? { ...waiting } : null,\n resume: resumeStore?.value ?? null,\n resumeError: resumeStore?.error ?? false\n };\n }\n function emit() {\n if (disposed) return;\n const state = snapshot(), serialized = JSON.stringify(state);\n if (serialized === lastState) return;\n lastState = serialized;\n notify(stateListeners, state);\n }\n function changed() {\n notify(listeners, { ...current });\n emit();\n }\n function active() {\n if (current.kind !== "room") throw creaErrore("no_room", "There is no active player room.");\n return current.room;\n }\n function activeVoice() {\n const room = active();\n if (room.players.find((p) => p.id === room.you)?.role === "spectator") throw creaErrore("spectator", "Spectators cannot use voice controls.");\n if (!manifest || manifest.voice === "none" || room.voice.mode === "none") throw creaErrore("voice_disabled", "Voice is disabled for this room.");\n return room.voice;\n }\n function cancel() {\n operation++;\n controller?.abort();\n controller = null;\n pending = null;\n waiting = null;\n emit();\n }\n function detach(preserve) {\n stops.splice(0).forEach((stop) => stop());\n if (current.kind === "room" || current.kind === "watch") {\n if (preserve) current.room.disconnect();\n else current.room.leave();\n }\n current = { kind: "idle" };\n changed();\n }\n async function clearResume(code) {\n if (resumeStore?.value?.code === code) await resumeStore.set(null).catch(() => void 0);\n }\n async function adopt(next, watch, token) {\n if (token !== operation || disposed) {\n next.leave();\n throw creaErrore("cancelled", "The operation was cancelled.");\n }\n detach(false);\n current = watch ? { kind: "watch", room: next, id: String(++identifier) } : { kind: "room", room: next, id: String(++identifier) };\n const room = next;\n stops = [room.onPlayers(emit), room.onMetadata(() => {\n if (room.connection === "disconnected" && (current.kind === "room" || current.kind === "watch") && current.room === room) {\n stops.splice(0).forEach((stop) => stop());\n if (!watch && room.metadata.closedCode === 1e3) void clearResume(room.code);\n current = { kind: "idle" };\n changed();\n } else emit();\n }), room.onStatus(() => {\n emit();\n if (!watch && room.connection === "ended") void clearResume(room.code);\n })];\n if (!watch) {\n const playerRoom = next;\n const sessionId = current.id;\n if (playerRoom.voice) stops.push(playerRoom.voice.onState(emit), playerRoom.voice.onPeers(emit));\n stops.push(playerRoom.onError((error) => notify(errorListeners, { sessionId, error: { ...error } })));\n stops.push(playerRoom.onScoreQueued((score) => notify(scoreListeners, { ...score })));\n for (const score of playerRoom.queuedScores) notify(scoreListeners, { ...score });\n }\n pending = null;\n waiting = null;\n changed();\n if (!watch && resumeStore && room.connection !== "ended") {\n await resumeStore.set({ version: 1, code: room.code, mode: room.mode, updatedAt: base.time.now() }).catch(() => void 0);\n }\n return next;\n }\n async function run(kind, mode, work, watch = false) {\n cancel();\n const token = operation;\n controller = new AbortController();\n pending = kind;\n pendingMode = mode;\n emit();\n try {\n const next = await work(controller.signal, token);\n await adopt(next, watch, token);\n if (token !== operation || disposed) throw creaErrore("cancelled", "The operation was cancelled.");\n return next;\n } finally {\n if (token === operation) {\n pending = null;\n waiting = null;\n controller = null;\n emit();\n }\n }\n }\n const direct = base.room;\n const rooms = !standard ? direct : {\n invited: direct.invited,\n create(options) {\n if (manifest && modalitaLocale(manifest, options.mode)) return Promise.reject(creaErrore("invalid_request", "Local modes cannot create rooms."));\n return run("attaching", options.mode, () => direct.create(options));\n },\n join(code) {\n return run("attaching", null, () => direct.join(code));\n },\n watch(code) {\n return run("attaching", null, () => direct.watch(code), true);\n },\n match(options) {\n if (manifest && modalitaLocale(manifest, options.mode)) return Promise.reject(creaErrore("invalid_request", "Local modes cannot use matchmaking."));\n return run("matching", options.mode, (signal, token) => {\n const abort = () => {\n if (operation === token) cancel();\n };\n options.signal?.addEventListener("abort", abort, { once: true });\n if (options.signal?.aborted) abort();\n return direct.match({ ...options, signal, onWaiting(value) {\n if (token !== operation) return;\n waiting = { ...value };\n emit();\n options.onWaiting?.(value);\n } }).finally(() => options.signal?.removeEventListener("abort", abort));\n });\n }\n };\n if (standard) resumeStore = createResume(base.save, emit);\n const session = {\n get current() {\n return { ...current };\n },\n get capabilities() {\n return capabilities();\n },\n onChange(listener) {\n listeners.add(listener);\n notify(/* @__PURE__ */ new Set([listener]), { ...current });\n return () => {\n listeners.delete(listener);\n };\n },\n ready() {\n if (disposed || ready) return;\n ready = true;\n emit();\n },\n finish() {\n if (current.kind === "room" || current.kind === "watch") throw creaErrore("not_local", "Only a local session can be finished by the client.");\n if (current.kind === "local") {\n current = { ...current, status: "ended" };\n changed();\n }\n }\n };\n const overlay = {\n open(panel) {\n if (!["home", "room", "invite", "friends", "voice", "boards"].includes(panel)) throw creaErrore("invalid_request", "Unknown overlay panel.");\n if (standard) notify(openListeners, panel);\n },\n onChange(listener) {\n viewListeners.add(listener);\n notify(/* @__PURE__ */ new Set([listener]), structuredClone(view));\n return () => {\n viewListeners.delete(listener);\n };\n }\n };\n return {\n session,\n overlay,\n rooms,\n snapshot,\n serverTime: () => current.kind === "room" || current.kind === "watch" ? current.room.serverTime() : base.time.now(),\n onState(listener) {\n stateListeners.add(listener);\n listener(snapshot());\n return () => {\n stateListeners.delete(listener);\n };\n },\n onOpen(listener) {\n openListeners.add(listener);\n return () => {\n openListeners.delete(listener);\n };\n },\n onError(listener) {\n errorListeners.add(listener);\n return () => {\n errorListeners.delete(listener);\n };\n },\n onScore(listener) {\n scoreListeners.add(listener);\n return () => {\n scoreListeners.delete(listener);\n };\n },\n async execute(request) {\n if (!standard) throw creaErrore("overlay_disabled", "This game uses its own room flow.");\n if (request.op === "overlay.view") {\n if (!validOverlayView(request.args)) throw creaErrore("invalid_request", "The overlay geometry is invalid.");\n view = { ...structuredClone(request.args), safeArea: { top: 0, right: 0, bottom: 0, left: 0, ...request.args.safeArea } };\n if (typeof document !== "undefined") for (const [side, value] of Object.entries(view.safeArea)) {\n document.documentElement.style.setProperty(`--caisual-safe-${side}`, `${value}px`);\n }\n notify(viewListeners, structuredClone(view));\n return;\n }\n if (request.sessionId !== void 0 && request.sessionId !== (current.kind === "idle" ? null : current.id)) throw creaErrore("session_replaced", "The active session changed.");\n if (request.op.startsWith("voice.") && request.sessionId !== (current.kind === "idle" ? null : current.id)) throw creaErrore("session_replaced", "The active session changed.");\n if (!ready) throw creaErrore("game_not_ready", "The game is still loading.");\n switch (request.op) {\n case "local.start": {\n if (!manifest || !modalitaLocale(manifest, request.args.mode)) throw creaErrore("invalid_mode", "This is not a local mode.");\n cancel();\n const token = operation;\n if (current.kind === "room") await clearResume(current.room.code);\n if (token !== operation || disposed) throw creaErrore("cancelled", "The operation was cancelled.");\n detach(false);\n current = { kind: "local", id: String(++identifier), mode: request.args.mode, status: "playing" };\n changed();\n return;\n }\n case "room.create":\n await rooms.create(request.args);\n return;\n case "room.join":\n await rooms.join(request.args.code);\n return;\n case "room.watch":\n await rooms.watch(request.args.code);\n return;\n case "room.match": {\n const mode = manifest?.modes.find((m) => m.id === request.args.mode);\n const key = request.args.key ?? mode?.matchmaking?.defaults;\n if (!key) throw creaErrore("invalid_request", "Matchmaking needs a complete key.");\n await rooms.match({ mode: request.args.mode, key });\n return;\n }\n case "voice.join": {\n const room = active(), voice = activeVoice();\n await voice.join();\n if (current.kind !== "room" || current.room !== room) throw creaErrore("session_replaced", "The active session changed.");\n emit();\n return;\n }\n case "voice.mute":\n activeVoice().mute(request.args.muted);\n emit();\n return;\n case "voice.leave":\n activeVoice().leave();\n emit();\n return;\n case "voice.setVolume": {\n const voice = activeVoice();\n if (!voice.peers.some((peer) => peer.id === request.args.playerId)) throw creaErrore("voice_peer_missing", "This voice participant is no longer available.");\n voice.setVolume(request.args.playerId, request.args.volume);\n emit();\n return;\n }\n case "room.ready":\n active().ready(request.args.ready);\n return;\n case "room.role":\n active().setRole(request.args.role);\n return;\n case "room.requestRole":\n await active().requestRole(request.args.role);\n return;\n case "room.team":\n active().setTeam(request.args.team);\n return;\n case "room.start":\n active().start();\n return;\n case "room.restart":\n active().restart();\n return;\n case "session.cancel":\n cancel();\n return;\n case "session.resume": {\n await run("attaching", null, async (signal) => {\n await resumeStore?.loaded;\n if (signal.aborted) throw creaErrore("cancelled", "The operation was cancelled.");\n if (!resumeStore?.value) throw creaErrore("no_resume", "There is no saved room.");\n return direct.join(resumeStore.value.code);\n });\n return;\n }\n case "session.disconnect": {\n cancel();\n const token = operation;\n if (current.kind === "room" && current.room.connection !== "ended" && resumeStore) await resumeStore.set({ version: 1, code: current.room.code, mode: current.room.mode, updatedAt: base.time.now() });\n if (token !== operation || disposed) throw creaErrore("cancelled", "The operation was cancelled.");\n detach(true);\n return;\n }\n case "session.leave": {\n cancel();\n const token = operation;\n if (current.kind === "room") await clearResume(current.room.code);\n if (token !== operation || disposed) throw creaErrore("cancelled", "The operation was cancelled.");\n detach(false);\n return;\n }\n }\n },\n dispose() {\n cancel();\n detach(true);\n disposed = true;\n listeners.clear();\n viewListeners.clear();\n stateListeners.clear();\n openListeners.clear();\n scoreListeners.clear();\n errorListeners.clear();\n }\n };\n}\n\n// src/overlay/shortcut.ts\nfunction bindOverlayShortcut(target, overlay, open) {\n let enabled = true, blocked = false;\n const stop = overlay.onChange((view) => {\n enabled = view.shortcutEnabled !== false;\n blocked = view.inputBlocked;\n });\n const listener = (event) => {\n const element = event.target;\n if (!enabled || blocked || event.repeat || event.key !== "Tab" || !event.shiftKey || event.ctrlKey || event.altKey || event.metaKey || element?.closest?.(\'input,textarea,select,[contenteditable="true"]\')) return;\n event.preventDefault();\n event.stopImmediatePropagation();\n open();\n };\n target.addEventListener("keydown", listener, true);\n return () => {\n stop();\n target.removeEventListener("keydown", listener, true);\n };\n}\n\n// src/overlay/bridge.ts\nfunction attachKitBridge(port, hello, coordinator) {\n let disposed = false, seq = 0, highestRequest = 0, activeRequests = 0;\n const replies = /* @__PURE__ */ new Map();\n const send = (message) => {\n if (!disposed) try {\n port.postMessage(message);\n } catch {\n }\n };\n const stops = [\n ...hello.configuration.manifest.overlay && typeof window !== "undefined" ? [bindOverlayShortcut(window, coordinator.overlay, () => send({ type: "caisual:overlay-shortcut", v: 1, epoch: hello.epoch }))] : [],\n coordinator.onState((state) => send({ type: "caisual:overlay-state", v: 1, epoch: hello.epoch, seq: ++seq, serverTime: coordinator.serverTime(), state })),\n coordinator.onOpen((panel) => send({ type: "caisual:overlay-open", v: 1, epoch: hello.epoch, panel })),\n coordinator.onError(({ sessionId, error }) => send({ type: "caisual:overlay-error", v: 1, epoch: hello.epoch, sessionId, error })),\n coordinator.onScore((score) => send({ type: "caisual:overlay-score", v: 1, epoch: hello.epoch, score }))\n ];\n const listener = (event) => {\n const raw = record(event.data);\n if (raw?.type !== "caisual:overlay" || raw.epoch !== hello.epoch || disposed) return;\n const reply = { type: "caisual:overlay-response", v: 1, epoch: hello.epoch, requestId: typeof raw.requestId === "string" ? raw.requestId : "" };\n if (!validOverlayRequest(raw)) {\n send({ ...reply, ok: false, error: { code: "invalid_request", message: "The overlay request is invalid." } });\n return;\n }\n const fingerprint = JSON.stringify([raw.op, raw.args, raw.sessionId]);\n const previous = replies.get(raw.requestId);\n if (previous) {\n if (previous.fingerprint !== fingerprint) send({ ...reply, ok: false, error: { code: "duplicate_request", message: "The request id was already used." } });\n else void previous.response.then(send);\n return;\n }\n if (Number(raw.requestId) <= highestRequest || activeRequests >= 32) {\n send({ ...reply, ok: false, error: { code: "stale_request", message: "The request is stale or too many requests are pending." } });\n return;\n }\n highestRequest = Number(raw.requestId);\n activeRequests++;\n const response = Promise.resolve().then(() => coordinator.execute(raw)).then(\n () => ({ ...reply, ok: true }),\n (error) => ({ ...reply, ok: false, error: {\n code: typeof record(error)?.code === "string" ? record(error).code : "internal_error",\n message: error instanceof Error ? error.message : "The operation could not be completed."\n } })\n );\n replies.set(raw.requestId, { fingerprint, response });\n void response.then((value) => {\n activeRequests--;\n send(value);\n if (replies.size > 64) for (const id of replies.keys()) {\n if (Number(id) < highestRequest - 64) replies.delete(id);\n }\n });\n };\n port.addEventListener("message", listener);\n port.start();\n return () => {\n disposed = true;\n port.removeEventListener("message", listener);\n stops.forEach((stop) => stop());\n coordinator.dispose();\n replies.clear();\n };\n}\n\n// src/http.ts\nasync function leggiErrore(response) {\n let corpo = {};\n try {\n corpo = await response.json();\n } catch {\n }\n return creaErrore(\n typeof corpo.error?.code === "string" ? corpo.error.code : response.status === 401 ? "invalid_ticket" : "internal_error",\n typeof corpo.error?.message === "string" ? corpo.error.message : `The request failed with status ${response.status}.`\n );\n}\nfunction creaRichiedente(origin, prefix, fetcher, biglietto) {\n async function manda(path, metodo, ticket, corpo) {\n const headers = new Headers({ Authorization: `Bearer ${ticket}` });\n let body;\n if (corpo !== void 0) {\n headers.set("Content-Type", "application/json");\n try {\n body = JSON.stringify(corpo);\n } catch {\n throw creaErrore("invalid_request", "The value must be valid JSON.");\n }\n }\n try {\n return await fetcher(new URL(prefix + path, origin), {\n method: metodo,\n headers,\n body,\n credentials: "omit"\n });\n } catch {\n throw erroreOffline();\n }\n }\n return async function richiesta(path, metodo, corpo, forzaRinnovo = false) {\n let ticket;\n try {\n ticket = forzaRinnovo ? await biglietto.rinnova() : await biglietto.ottieni();\n } catch {\n throw erroreOffline();\n }\n let response = await manda(path, metodo, ticket, corpo);\n if (response.status === 401) {\n try {\n ticket = await biglietto.rinnova();\n } catch {\n throw erroreOffline();\n }\n response = await manda(path, metodo, ticket, corpo);\n }\n if (!response.ok) throw await leggiErrore(response);\n try {\n return await response.json();\n } catch {\n throw creaErrore("internal_error", "The service returned an invalid response.");\n }\n };\n}\n\n// src/api.ts\nfunction creaClienteApi(appOrigin, fetcher, biglietto) {\n const richiesta = creaRichiedente(appOrigin, "/api/kit", fetcher, biglietto);\n return {\n me: () => richiesta("/me", "GET"),\n saveSet: (key, value) => richiesta(`/saves/${encodeURIComponent(key)}`, "PUT", { value }),\n async saveGet(key) {\n try {\n return (await richiesta(`/saves/${encodeURIComponent(key)}`, "GET")).value;\n } catch (errore) {\n if (codiceErrore(errore) === "not_found") return null;\n throw errore;\n }\n },\n async saveRemove(key) {\n await richiesta(`/saves/${encodeURIComponent(key)}`, "DELETE");\n },\n async saveList() {\n return (await richiesta("/saves", "GET")).saves;\n },\n async boardSubmit(board, score, daily) {\n const risultato = await richiesta("/scores", "POST", { board, score, daily });\n return {\n accepted: true,\n best: risultato.best,\n rank: risultato.rank,\n day: risultato.day,\n verified: risultato.verified\n };\n },\n async boardTop(board, opzioni) {\n if (opzioni.day !== void 0 && (!validBoardDay(opzioni.day) || opzioni.daily === false)) throw creaErrore("invalid_request", "day must be a real UTC date and cannot be combined with daily: false.");\n const query = new URLSearchParams();\n if (opzioni.day !== void 0) query.set("day", opzioni.day);\n if (opzioni.daily) query.set("daily", "1");\n if (opzioni.limit !== void 0) query.set("limit", String(opzioni.limit));\n if (opzioni.guests) query.set("guests", "1");\n const suffisso = query.size === 0 ? "" : `?${query.toString()}`;\n const { day, entries, me } = await richiesta(\n `/scores/${encodeURIComponent(board)}${suffisso}`,\n "GET"\n );\n return { day, entries, me };\n }\n };\n}\n\n// src/daily.ts\nvar DIVISORE_UINT32 = 4294967296;\nfunction prossimaMezzanotteUtc(ora) {\n return (Math.floor(ora / 864e5) + 1) * 864e5;\n}\nfunction creaDaily(initial, now, refresh) {\n const listeners = /* @__PURE__ */ new Set();\n let current = { ...initial }, timer, pending = false;\n function schedule() {\n if (!listeners.size || timer !== void 0 || pending) return;\n timer = setTimeout(check, Math.max(0, Math.min(2147483647, current.expiresAt - now())));\n timer.unref?.();\n }\n async function check() {\n timer = void 0;\n pending = true;\n try {\n const next = await refresh(), changed = next.day !== current.day;\n current = { ...next };\n if (changed) for (const listener of [...listeners]) {\n try {\n listener({ ...next });\n } catch {\n }\n }\n } catch {\n } finally {\n pending = false;\n if (current.expiresAt <= now()) current.expiresAt = now() + 3e4;\n schedule();\n }\n }\n return {\n ...initial,\n random: creaMulberry32(initial.seed),\n rng: () => creaMulberry32(initial.seed),\n onChange(listener) {\n listeners.add(listener);\n schedule();\n return () => {\n listeners.delete(listener);\n if (!listeners.size && timer !== void 0) {\n clearTimeout(timer);\n timer = void 0;\n }\n };\n }\n };\n}\nfunction giornoUtc(ora) {\n return new Date(ora).toISOString().slice(0, 10);\n}\nasync function calcolaSeed(gioco, giorno, subtle) {\n const dati = new TextEncoder().encode(`caisual:${gioco}:${giorno}`);\n const digest = new Uint8Array(await subtle.digest("SHA-256", dati));\n return (digest[0] ?? 0) * 16777216 + ((digest[1] ?? 0) << 16) + ((digest[2] ?? 0) << 8) + (digest[3] ?? 0) >>> 0;\n}\nfunction creaMulberry32(seed) {\n let stato = seed >>> 0;\n return () => {\n stato = stato + 1831565813 >>> 0;\n let valore = stato;\n valore = Math.imul(valore ^ valore >>> 15, valore | 1);\n valore ^= valore + Math.imul(valore ^ valore >>> 7, valore | 61);\n return ((valore ^ valore >>> 14) >>> 0) / DIVISORE_UINT32;\n };\n}\n\n// src/handshake.ts\nfunction record2(valore) {\n return typeof valore === "object" && valore !== null && !Array.isArray(valore) ? valore : null;\n}\nfunction eTipo(valore, tipo) {\n return record2(valore)?.type === tipo;\n}\nfunction leggiOrigine(valore) {\n if (typeof valore !== "string") return null;\n try {\n const url = new URL(valore);\n return url.origin === valore && (url.protocol === "https:" || url.protocol === "http:") ? valore : null;\n } catch {\n return null;\n }\n}\nfunction attendiHandshake(finestra, appOrigin, timeoutMs = 3e3) {\n return new Promise((resolve) => {\n let concluso = false;\n const instance = globalThis.crypto.randomUUID();\n const termina = (esito) => {\n if (concluso) return;\n concluso = true;\n finestra.removeEventListener("message", ascolta);\n finestra.clearTimeout(scadenza);\n resolve(esito);\n };\n const segnalaPronto = () => {\n finestra.parent.postMessage({ type: "caisual:ready", instance, overlayVersion: 1 }, appOrigin);\n };\n const ascolta = (evento) => {\n if (evento.origin !== appOrigin || evento.source !== finestra.parent) return;\n if (eTipo(evento.data, "caisual:ready?")) {\n segnalaPronto();\n return;\n }\n if (!eTipo(evento.data, "caisual:hello")) return;\n const dati = record2(evento.data);\n const porta = evento.ports[0];\n if (typeof dati?.ticket !== "string" || porta === void 0) return;\n porta.start();\n const overlay = normalizeOverlayHello(dati.overlay);\n const tags = (value) => Array.isArray(value) ? value.map(normalizeLanguage).filter((tag) => tag !== null) : void 0;\n termina({\n ...overlay ? { overlay } : {},\n ...normalizeLanguage(dati.language) ? { language: normalizeLanguage(dati.language) } : {},\n uiLanguage: normalizeLanguage(dati.uiLanguage) ?? void 0,\n languagePreferences: tags(dati.languagePreferences),\n gameLanguages: tags(dati.gameLanguages),\n ticket: dati.ticket,\n live: leggiOrigine(dati.live),\n invite: typeof dati.invite === "string" ? dati.invite : null,\n porta\n });\n };\n finestra.addEventListener("message", ascolta);\n const scadenza = finestra.setTimeout(() => termina(null), timeoutMs);\n segnalaPronto();\n });\n}\nfunction scadenzaJwt(ticket) {\n const parte = ticket.split(".")[1];\n if (parte === void 0) return null;\n const base64 = parte.replace(/-/g, "+").replace(/_/g, "/").padEnd(\n Math.ceil(parte.length / 4) * 4,\n "="\n );\n try {\n const payload = record2(JSON.parse(globalThis.atob(base64)));\n return typeof payload?.exp === "number" && Number.isFinite(payload.exp) ? payload.exp * 1e3 : null;\n } catch {\n return null;\n }\n}\nfunction chiediBiglietto(porta, finestra, timeoutMs, aud) {\n return new Promise((resolve, reject) => {\n let concluso = false;\n const termina = (ticket) => {\n if (concluso) return;\n concluso = true;\n porta.removeEventListener("message", ascolta);\n finestra.clearTimeout(scadenza);\n if (ticket === null) reject(new Error("Ticket refresh timed out."));\n else resolve(ticket);\n };\n const ascolta = (evento) => {\n const dati = record2(evento.data);\n const destinatario = dati?.aud === void 0 ? "portal" : dati.aud;\n if (dati?.type === "caisual:ticket" && destinatario === aud && typeof dati.ticket === "string") {\n termina(dati.ticket);\n }\n };\n porta.addEventListener("message", ascolta);\n const scadenza = finestra.setTimeout(() => termina(null), timeoutMs);\n try {\n porta.postMessage(aud === "live" ? { type: "caisual:ticket", aud: "live" } : { type: "caisual:ticket" });\n } catch {\n termina(null);\n }\n });\n}\nfunction creaGestoreBiglietto(ticketIniziale, porta, finestra, ora, timeoutMs = 3e3, aud = "portal") {\n let ticket = ticketIniziale;\n let rinnovo = null;\n const rinnova = () => {\n if (rinnovo !== null) return rinnovo;\n const richiesta = chiediBiglietto(porta, finestra, timeoutMs, aud).then((nuovo) => {\n ticket = nuovo;\n return nuovo;\n });\n const completa = richiesta.finally(() => {\n if (rinnovo === completa) rinnovo = null;\n });\n rinnovo = completa;\n return completa;\n };\n return {\n async ottieni() {\n if (ticket === null) return rinnova();\n const scadenza = scadenzaJwt(ticket);\n return scadenza !== null && scadenza - ora() < 3e4 ? rinnova() : ticket;\n },\n rinnova\n };\n}\n\n// src/voce/index.ts\nvar SOGLIA_AUDIO = 0.02;\nvar DURATA_PARLANTE = 300;\nvar INTERVALLO_AUDIO = 200;\nvar DURATA_ZERO = 3e3;\nvar TIMEOUT_CONNESSIONE = 1e4;\nvar RITARDI_RICONNESSIONE = [1e3, 2e3, 4e3];\nfunction limita(value) {\n return Number.isNaN(value) ? 1 : Math.min(1, Math.max(0, value));\n}\nfunction dipendenzeReali(input) {\n const globali = globalThis;\n const AudioContextClass = globali.AudioContext ?? globali.webkitAudioContext;\n if (typeof RTCPeerConnection === "undefined" || typeof MediaStream === "undefined" || AudioContextClass === void 0 || typeof navigator === "undefined" || navigator.mediaDevices?.getUserMedia === void 0 || typeof document === "undefined") return null;\n return {\n ...input,\n creaPeerConnection: (configuration) => new RTCPeerConnection(configuration),\n getUserMedia: (constraints) => navigator.mediaDevices.getUserMedia(constraints),\n creaAudioContext: () => new AudioContextClass(),\n creaAudioElement: () => document.createElement("audio"),\n creaMediaStream: (tracks) => new MediaStream(tracks)\n };\n}\nvar VoceClient = class {\n constructor(contesto, timer, dipendenze) {\n this.contesto = contesto;\n this.modeCorrente = "none";\n this.stateCorrente = "off";\n this.mutedCorrente = false;\n this.speakingCorrente = false;\n this.roster = [];\n this.gains = /* @__PURE__ */ new Map();\n this.volumi = /* @__PURE__ */ new Map();\n this.speakingPeers = /* @__PURE__ */ new Map();\n this.ultimoAudio = /* @__PURE__ */ new Map();\n this.zeroDa = /* @__PURE__ */ new Map();\n this.timerZero = /* @__PURE__ */ new Map();\n this.ascoltatoriPeers = /* @__PURE__ */ new Set();\n this.ascoltatoriState = /* @__PURE__ */ new Set();\n this.richieste = /* @__PURE__ */ new Map();\n this.riproduzioni = /* @__PURE__ */ new Map();\n this.sfuAttive = /* @__PURE__ */ new Map();\n this.midGiocatori = /* @__PURE__ */ new Map();\n this.negati = /* @__PURE__ */ new Set();\n this.mesh = /* @__PURE__ */ new Map();\n this.stream = null;\n this.tracciaMic = null;\n this.audioContext = null;\n this.analyser = null;\n this.peerSfu = null;\n this.sessioneSfu = null;\n this.connessioneSfuAttesa = false;\n this.trasporto = null;\n this.intervalloAudio = null;\n this.timerConnessione = null;\n this.cancellaAttesaConnessione = null;\n this.timerRiconnessione = null;\n this.ultimoAudioMic = Number.NEGATIVE_INFINITY;\n this.sequenzaRichieste = 0;\n this.generazione = 0;\n this.tentativoRiconnessione = 0;\n this.desiderata = false;\n this.micDesiderato = true;\n this.promessaIngresso = null;\n this.negoziazione = Promise.resolve();\n this.dipendenze = dipendenze ?? dipendenzeReali(timer);\n }\n get mode() {\n return this.modeCorrente;\n }\n get state() {\n return this.stateCorrente;\n }\n get mic() {\n return this.stateCorrente === "on" && this.tracciaMic !== null;\n }\n get muted() {\n return this.mutedCorrente;\n }\n get speaking() {\n return this.speakingCorrente;\n }\n get peers() {\n return this.copiaPeers();\n }\n async join(options = {}) {\n if (this.stateCorrente === "on") return;\n if (this.stateCorrente === "joining") {\n if (this.promessaIngresso !== null) await this.promessaIngresso;\n return;\n }\n if (this.stateCorrente === "reconnecting" && this.desiderata) return;\n const mic = this.scegliMic(options);\n this.verificaIngresso(mic);\n this.micDesiderato = mic;\n this.desiderata = true;\n this.tentativoRiconnessione = 0;\n this.aggiornaState("joining");\n const generazione = ++this.generazione;\n const promessa = this.completaIngresso(generazione);\n this.promessaIngresso = promessa;\n try {\n await promessa;\n } finally {\n if (this.promessaIngresso === promessa) this.promessaIngresso = null;\n }\n }\n async completaIngresso(generazione) {\n try {\n await this.entra(generazione);\n } catch (cause) {\n if (generazione !== this.generazione) return;\n this.desiderata = false;\n this.chiudiRisorse();\n this.aggiornaState("off");\n throw this.mappaErrore(cause);\n }\n }\n leave() {\n const deveFermare = this.desiderata || this.stateCorrente !== "off";\n this.desiderata = false;\n this.generazione++;\n this.fermaRiconnessione();\n if (deveFermare && this.contesto.connessa()) {\n void this.richiedi({ t: "voice", op: "stop" }).catch(() => void 0);\n }\n this.rifiutaRichieste(creaErrore("offline", "Voice has stopped."));\n this.chiudiRisorse();\n this.aggiornaState("off");\n }\n mute(muted = true) {\n if (this.stateCorrente !== "on" || this.tracciaMic === null) {\n throw creaErrore("not_publishing", "Join voice before changing mute.");\n }\n this.mutedCorrente = muted;\n this.tracciaMic.enabled = !muted;\n if (muted) this.speakingCorrente = false;\n this.notificaPeers();\n void this.richiedi({ t: "voice", op: "mute", muted }).catch(() => void 0);\n }\n setVolume(playerId, volume) {\n const valore = limita(volume);\n this.volumi.set(playerId, valore);\n this.aggiornaGuadagno(playerId);\n this.notificaPeers();\n }\n onPeers(listener) {\n this.ascoltatoriPeers.add(listener);\n return () => {\n this.ascoltatoriPeers.delete(listener);\n };\n }\n onState(listener) {\n this.ascoltatoriState.add(listener);\n return () => {\n this.ascoltatoriState.delete(listener);\n };\n }\n ricevi(message) {\n if ("r" in message) {\n const pending = this.richieste.get(message.r);\n if (pending !== void 0) {\n this.richieste.delete(message.r);\n if ("error" in message) {\n pending.reject(creaErrore(message.error.code, message.error.message));\n } else pending.resolve(message);\n }\n return;\n }\n if (message.op === "roster") {\n this.negati.clear();\n this.modeCorrente = message.mode;\n const publisher = new Set(message.peers.map((peer) => peer.id));\n this.roster = [\n ...message.peers.map((peer) => ({ ...peer, mic: true })),\n ...message.listeners.flatMap((id) => publisher.has(id) ? [] : [{ id, mic: false, muted: true }])\n ];\n for (const peer of this.roster) {\n if (peer.muted) this.speakingPeers.set(peer.id, false);\n }\n this.pulisciPeerAssenti();\n this.contesto.rosterPronto();\n this.notificaPeers();\n this.accodaRiconciliazione();\n return;\n }\n if (message.op === "gain") {\n this.negati.clear();\n for (const [playerId, gain] of Object.entries(message.gains)) {\n this.gains.set(playerId, limita(gain));\n this.aggiornaZero(playerId);\n this.aggiornaGuadagno(playerId);\n }\n this.notificaPeers();\n this.accodaRiconciliazione();\n return;\n }\n if (message.op === "closed") {\n for (const mid of message.mids) {\n const playerId = this.midGiocatori.get(mid);\n if (playerId === void 0) continue;\n const attiva = this.sfuAttive.get(playerId);\n if (attiva?.mid === mid && !this.riproduzioni.has(playerId)) attiva.receiver?.track.stop();\n if (attiva?.mid === mid) this.sfuAttive.delete(playerId);\n this.midGiocatori.delete(mid);\n this.scollegaTraccia(playerId);\n this.negati.add(playerId);\n }\n this.notificaPeers();\n return;\n }\n if (message.op === "signal") void this.riceviSegnale(message.from, message.data);\n }\n giocatoriCambiati() {\n this.negati.clear();\n const presenti = new Set(this.contesto.giocatori().map((player) => player.id));\n for (const playerId of this.gains.keys()) {\n if (presenti.has(playerId)) continue;\n this.gains.delete(playerId);\n this.zeroDa.delete(playerId);\n const timer = this.timerZero.get(playerId);\n if (timer !== void 0) this.dipendenze?.clearTimeout(timer);\n this.timerZero.delete(playerId);\n this.aggiornaGuadagno(playerId);\n }\n this.notificaPeers();\n this.accodaRiconciliazione();\n }\n socketDisconnesso() {\n this.sequenzaRichieste = 0;\n this.rifiutaRichieste(creaErrore("offline", "The room is reconnecting."));\n if (!this.desiderata) return;\n this.generazione++;\n this.chiudiRisorse();\n this.tentativoRiconnessione = 0;\n this.aggiornaState("reconnecting");\n }\n socketRiconnesso() {\n this.sequenzaRichieste = 0;\n if (this.desiderata && this.stateCorrente === "reconnecting") this.programmaRiconnessione();\n }\n termina() {\n this.desiderata = false;\n this.generazione++;\n this.fermaRiconnessione();\n this.rifiutaRichieste(creaErrore("offline", "The room connection ended."));\n this.chiudiRisorse();\n this.aggiornaState("off");\n }\n scegliMic(options) {\n if (options.mic !== void 0) return options.mic;\n const you = this.contesto.giocatori().find((player) => player.id === this.contesto.you());\n return you?.role !== "spectator";\n }\n verificaIngresso(mic = this.micDesiderato) {\n if (!this.contesto.connessa()) throw creaErrore("offline", "The room is not connected.");\n if (this.modeCorrente === "none") {\n throw creaErrore("voice_disabled", "Voice is disabled for this room.");\n }\n const you = this.contesto.giocatori().find((player) => player.id === this.contesto.you());\n if (you?.role === "spectator" && mic) {\n throw creaErrore("spectator", "Spectators cannot publish voice.");\n }\n if (this.dipendenze === null) {\n throw creaErrore("unsupported", "Voice is not supported in this browser.");\n }\n }\n async entra(generazione) {\n this.verificaIngresso();\n const dipendenze = this.richiediDipendenze();\n const audioContext = dipendenze.creaAudioContext();\n this.audioContext = audioContext;\n if (this.micDesiderato) {\n let stream;\n try {\n stream = await dipendenze.getUserMedia({ audio: true });\n } catch (cause) {\n if (this.permessoNegato(cause)) {\n throw creaErrore("permission_denied", "Microphone permission was denied.");\n }\n throw creaErrore("voice_error", "The microphone could not be opened.");\n }\n try {\n this.controllaGenerazione(generazione);\n } catch (cause) {\n for (const track of stream.getTracks()) track.stop();\n throw cause;\n }\n const mic = stream.getAudioTracks()[0];\n if (mic === void 0) throw creaErrore("voice_error", "The microphone has no audio track.");\n this.stream = stream;\n this.tracciaMic = mic;\n mic.enabled = !this.mutedCorrente;\n this.preparaAnalizzatore(stream);\n }\n try {\n await audioContext.resume();\n } catch {\n }\n this.controllaGenerazione(generazione);\n const risposta = await this.richiedi({ t: "voice", op: "ice" });\n this.controllaGenerazione(generazione);\n if (risposta.op !== "ice") throw creaErrore("voice_error", "The voice service returned an invalid response.");\n this.modeCorrente = risposta.mode;\n if (risposta.mode === "none") throw creaErrore("voice_disabled", "Voice is disabled for this room.");\n this.trasporto = risposta.transport;\n if (risposta.transport === "sfu") {\n await this.entraSfu(risposta.iceServers, generazione);\n } else {\n await this.richiedi({ t: "voice", op: "publish", mic: this.micDesiderato });\n }\n if (this.micDesiderato && this.mutedCorrente) {\n await this.richiedi({ t: "voice", op: "mute", muted: true });\n }\n this.controllaGenerazione(generazione);\n this.tentativoRiconnessione = 0;\n this.aggiornaState("on");\n this.avviaMisuraAudio();\n for (const playerId of this.gains.keys()) this.aggiornaZero(playerId);\n this.accodaRiconciliazione();\n }\n async entraSfu(iceServers, generazione) {\n const pc = this.richiediDipendenze().creaPeerConnection({\n iceServers,\n bundlePolicy: "max-bundle"\n });\n this.peerSfu = pc;\n pc.ontrack = (event) => {\n const mid = event.transceiver.mid;\n const playerId = mid === null ? void 0 : this.midGiocatori.get(mid);\n if (playerId !== void 0) this.collegaTraccia(playerId, event.track, event.receiver);\n };\n this.osservaCaduta(pc);\n let risposta;\n if (this.micDesiderato) {\n const transceiver = pc.addTransceiver(this.richiediMic(), { direction: "sendonly" });\n const offer = await pc.createOffer();\n await pc.setLocalDescription(offer);\n this.controllaGenerazione(generazione);\n const mid = transceiver.mid;\n const sdp = pc.localDescription?.sdp;\n if (mid === null || sdp === void 0) {\n throw creaErrore("voice_error", "The voice connection could not create an offer.");\n }\n risposta = await this.richiedi({ t: "voice", op: "session", sdp, mid });\n } else {\n risposta = await this.richiedi({ t: "voice", op: "session" });\n }\n if (risposta.op !== "session") {\n throw creaErrore("voice_error", "The voice service returned an invalid response.");\n }\n this.sessioneSfu = risposta.session;\n if (this.micDesiderato) {\n if (risposta.sdp === null) {\n throw creaErrore("voice_error", "The voice service returned an invalid response.");\n }\n await pc.setRemoteDescription({ type: "answer", sdp: risposta.sdp });\n await this.attendiConnessione(pc, generazione);\n this.connessioneSfuAttesa = true;\n return;\n }\n if (risposta.sdp !== null) {\n throw creaErrore("voice_error", "The voice service returned an invalid response.");\n }\n if (this.publisherDesiderati().length > 0) {\n await this.riconciliaSfu();\n }\n }\n attendiConnessione(pc, generazione) {\n if (pc.connectionState === "connected") return Promise.resolve();\n const dipendenze = this.richiediDipendenze();\n return new Promise((resolve, reject) => {\n const pulisci = () => {\n pc.removeEventListener("connectionstatechange", cambiata);\n if (this.timerConnessione !== null) dipendenze.clearTimeout(this.timerConnessione);\n this.timerConnessione = null;\n this.cancellaAttesaConnessione = null;\n };\n const cambiata = () => {\n if (generazione !== this.generazione) {\n pulisci();\n reject(creaErrore("offline", "Voice was stopped."));\n } else if (pc.connectionState === "connected") {\n pulisci();\n resolve();\n } else if (pc.connectionState === "failed" || pc.connectionState === "closed") {\n pulisci();\n reject(creaErrore("voice_error", "The voice connection failed."));\n }\n };\n pc.addEventListener("connectionstatechange", cambiata);\n this.cancellaAttesaConnessione = () => {\n pulisci();\n reject(creaErrore("offline", "Voice was stopped."));\n };\n this.timerConnessione = dipendenze.setTimeout(() => {\n pulisci();\n reject(creaErrore("voice_error", "The voice connection timed out."));\n }, TIMEOUT_CONNESSIONE);\n });\n }\n accodaRiconciliazione() {\n if (this.stateCorrente !== "on") return;\n this.negoziazione = this.negoziazione.then(async () => {\n if (this.stateCorrente !== "on") return;\n if (this.trasporto === "sfu") await this.riconciliaSfu();\n else if (this.trasporto === "mesh") this.riconciliaMesh();\n }).catch(() => this.avviaRiconnessione());\n }\n async riconciliaSfu() {\n const sessione = this.sessioneSfu;\n const pc = this.peerSfu;\n if (sessione === null || pc === null) return;\n const desiderati = new Map(this.publisherDesiderati().map((peer) => [peer.id, peer]));\n const daChiudere = [];\n for (const [playerId, attiva] of this.sfuAttive) {\n const peer = desiderati.get(playerId);\n if (peer !== void 0 && peer.session === attiva.session && peer.track === attiva.track) continue;\n daChiudere.push(attiva);\n if (!this.riproduzioni.has(playerId)) attiva.receiver?.track.stop();\n this.sfuAttive.delete(playerId);\n this.midGiocatori.delete(attiva.mid);\n this.scollegaTraccia(playerId);\n }\n if (daChiudere.length > 0) {\n await this.richiedi({\n t: "voice",\n op: "close",\n session: sessione,\n mids: daChiudere.map((item) => item.mid)\n });\n }\n const nuove = [...desiderati.values()].filter((peer) => !this.sfuAttive.has(peer.id));\n if (nuove.length === 0) return;\n let risposta;\n try {\n risposta = await this.richiedi({\n t: "voice",\n op: "subscribe",\n session: sessione,\n tracks: nuove.map((peer) => ({ session: peer.session, track: peer.track }))\n });\n } catch (cause) {\n if (codiceErrore(cause) !== "not_allowed") throw cause;\n for (const peer of nuove) this.negati.add(peer.id);\n return;\n }\n if (risposta.op !== "subscribe") {\n throw creaErrore("voice_error", "The voice service returned an invalid response.");\n }\n for (const risultato of risposta.tracks) {\n const peer = nuove.find(\n (item) => item.session === risultato.session && item.track === risultato.track\n );\n if (risultato.error === "not_allowed" && peer !== void 0) this.negati.add(peer.id);\n if (risultato?.mid === null || risultato?.mid === void 0 || risultato.error !== null || peer === void 0) continue;\n this.midGiocatori.set(risultato.mid, peer.id);\n this.sfuAttive.set(peer.id, {\n session: peer.session,\n track: peer.track,\n mid: risultato.mid,\n receiver: null\n });\n }\n await pc.setRemoteDescription({ type: "offer", sdp: risposta.sdp });\n const answer = await pc.createAnswer();\n await pc.setLocalDescription(answer);\n const sdp = pc.localDescription?.sdp;\n if (sdp === void 0) throw creaErrore("voice_error", "The voice answer is missing.");\n await this.richiedi({ t: "voice", op: "answer", session: sessione, sdp });\n if (!this.connessioneSfuAttesa) {\n await this.attendiConnessione(pc, this.generazione);\n this.connessioneSfuAttesa = true;\n }\n }\n riconciliaMesh() {\n const desiderati = new Map(this.peerDesiderati().map((peer) => [peer.id, peer]));\n for (const [playerId, item] of this.mesh) {\n if (desiderati.has(playerId)) continue;\n item.pc.close();\n this.mesh.delete(playerId);\n this.scollegaTraccia(playerId);\n }\n for (const peer of desiderati.values()) {\n if (!this.mesh.has(peer.id)) this.creaMesh(peer);\n }\n }\n creaMesh(peer) {\n const playerId = peer.id;\n const pc = this.richiediDipendenze().creaPeerConnection();\n const item = {\n pc,\n makingOffer: false,\n ignoreOffer: false,\n settingRemoteAnswer: false,\n polite: this.contesto.you() > playerId,\n receiver: null\n };\n this.mesh.set(playerId, item);\n pc.onicecandidate = (event) => {\n if (event.candidate === null) return;\n void this.inviaSegnale(playerId, { kind: "candidate", candidate: event.candidate.toJSON() });\n };\n if (!item.polite) pc.onnegotiationneeded = () => {\n void this.offriMesh(playerId, item);\n };\n pc.ontrack = (event) => {\n item.receiver = event.receiver;\n this.collegaTraccia(playerId, event.track, event.receiver);\n };\n this.osservaCaduta(pc);\n if (this.micDesiderato) {\n pc.addTransceiver(this.richiediMic(), {\n direction: peer.mic ? "sendrecv" : "sendonly"\n });\n } else {\n pc.addTransceiver("audio", { direction: "recvonly" });\n }\n }\n async offriMesh(playerId, item) {\n try {\n item.makingOffer = true;\n const offer = await item.pc.createOffer();\n await item.pc.setLocalDescription(offer);\n const sdp = item.pc.localDescription?.sdp;\n if (sdp !== void 0) await this.inviaSegnale(playerId, { kind: "offer", sdp });\n } finally {\n item.makingOffer = false;\n }\n }\n async riceviSegnale(from, data) {\n if (this.trasporto !== "mesh" || this.stateCorrente !== "on") return;\n const peer = this.peerDesiderati().find((item2) => item2.id === from);\n if (peer === void 0) return;\n if (!this.mesh.has(from)) this.creaMesh(peer);\n const item = this.mesh.get(from);\n if (item === void 0 || typeof data !== "object" || data === null || Array.isArray(data)) return;\n const segnale = data;\n try {\n if (segnale.kind === "candidate") {\n if (!item.ignoreOffer) await item.pc.addIceCandidate(segnale.candidate);\n return;\n }\n if (segnale.kind !== "offer" && segnale.kind !== "answer" || typeof segnale.sdp !== "string") return;\n const pronta = !item.makingOffer && (item.pc.signalingState === "stable" || item.settingRemoteAnswer);\n const collisione = segnale.kind === "offer" && !pronta;\n item.ignoreOffer = !item.polite && collisione;\n if (item.ignoreOffer) return;\n item.settingRemoteAnswer = segnale.kind === "answer";\n await item.pc.setRemoteDescription({ type: segnale.kind, sdp: segnale.sdp });\n item.settingRemoteAnswer = false;\n if (segnale.kind === "offer") {\n const answer = await item.pc.createAnswer();\n await item.pc.setLocalDescription(answer);\n const sdp = item.pc.localDescription?.sdp;\n if (sdp !== void 0) await this.inviaSegnale(from, { kind: "answer", sdp });\n }\n } catch {\n this.avviaRiconnessione();\n }\n }\n async inviaSegnale(to, data) {\n try {\n await this.richiedi({ t: "voice", op: "signal", to, data });\n } catch (cause) {\n if (codiceErrore(cause) !== "not_allowed") throw cause;\n const item = this.mesh.get(to);\n item?.pc.close();\n this.mesh.delete(to);\n this.scollegaTraccia(to);\n this.negati.add(to);\n }\n }\n peerDesiderati() {\n const you = this.contesto.you();\n const giocatori = this.contesto.giocatori();\n const player = giocatori.find((item) => item.id === you);\n return this.roster.filter((peer) => {\n if (peer.id === you) return false;\n if (this.negati.has(peer.id)) return false;\n if (!this.micDesiderato && !peer.mic) return false;\n if (this.modeCorrente === "team") {\n const altro = giocatori.find((item) => item.id === peer.id);\n if (player?.role !== "spectator" && altro?.team !== player?.team) return false;\n }\n return true;\n });\n }\n publisherDesiderati() {\n return this.peerDesiderati().filter(\n (peer) => {\n if (!peer.mic) return false;\n const zeroAt = this.zeroDa.get(peer.id);\n return zeroAt === void 0 || this.richiediDipendenze().ora() - zeroAt < DURATA_ZERO;\n }\n );\n }\n aggiornaZero(playerId) {\n const dipendenze = this.dipendenze;\n if (dipendenze === null) return;\n const precedente = this.timerZero.get(playerId);\n if (precedente !== void 0) dipendenze.clearTimeout(precedente);\n this.timerZero.delete(playerId);\n if ((this.gains.get(playerId) ?? 1) > 0) {\n this.zeroDa.delete(playerId);\n return;\n }\n if (!this.zeroDa.has(playerId)) this.zeroDa.set(playerId, dipendenze.ora());\n const trascorso = dipendenze.ora() - (this.zeroDa.get(playerId) ?? dipendenze.ora());\n const timer = dipendenze.setTimeout(() => {\n this.timerZero.delete(playerId);\n this.accodaRiconciliazione();\n }, Math.max(0, DURATA_ZERO - trascorso));\n this.timerZero.set(playerId, timer);\n }\n collegaTraccia(playerId, track, receiver) {\n this.scollegaTraccia(playerId);\n const dipendenze = this.richiediDipendenze();\n const media = dipendenze.creaMediaStream([track]);\n const source = this.richiediAudioContext().createMediaStreamSource(media);\n const gain = this.richiediAudioContext().createGain();\n source.connect(gain);\n gain.connect(this.richiediAudioContext().destination);\n let analyser = null;\n try {\n analyser = this.richiediAudioContext().createAnalyser();\n analyser.fftSize = 256;\n source.connect(analyser);\n } catch {\n analyser = null;\n }\n const audio = dipendenze.creaAudioElement();\n audio.srcObject = media;\n audio.muted = true;\n audio.playsInline = true;\n void audio.play().catch(() => void 0);\n this.riproduzioni.set(playerId, { source, gain, analyser, audio, track, receiver });\n const attiva = this.sfuAttive.get(playerId);\n if (attiva !== void 0) attiva.receiver = receiver;\n this.aggiornaGuadagno(playerId);\n }\n scollegaTraccia(playerId) {\n const riproduzione = this.riproduzioni.get(playerId);\n if (riproduzione === void 0) return;\n riproduzione.source.disconnect();\n riproduzione.gain.disconnect();\n riproduzione.analyser?.disconnect();\n riproduzione.track.stop();\n riproduzione.audio.pause();\n riproduzione.audio.srcObject = null;\n this.riproduzioni.delete(playerId);\n this.speakingPeers.delete(playerId);\n this.ultimoAudio.delete(playerId);\n }\n aggiornaGuadagno(playerId) {\n const riproduzione = this.riproduzioni.get(playerId);\n if (riproduzione !== void 0) {\n riproduzione.gain.gain.value = (this.volumi.get(playerId) ?? 1) * (this.gains.get(playerId) ?? 1);\n }\n }\n preparaAnalizzatore(stream) {\n const context = this.richiediAudioContext();\n const analyser = context.createAnalyser();\n analyser.fftSize = 256;\n context.createMediaStreamSource(stream).connect(analyser);\n this.analyser = analyser;\n }\n avviaMisuraAudio() {\n const dipendenze = this.richiediDipendenze();\n if (this.intervalloAudio !== null) dipendenze.clearInterval(this.intervalloAudio);\n this.intervalloAudio = dipendenze.setInterval(() => this.misuraAudio(), INTERVALLO_AUDIO);\n }\n misuraAudio() {\n const dipendenze = this.dipendenze;\n if (dipendenze === null) return;\n let sopraSoglia = false;\n if (this.analyser !== null) sopraSoglia = this.livelloAnalizzatore(this.analyser) > SOGLIA_AUDIO;\n if (sopraSoglia) this.ultimoAudioMic = dipendenze.ora();\n const parlando = !this.mutedCorrente && dipendenze.ora() - this.ultimoAudioMic <= DURATA_PARLANTE;\n if (parlando !== this.speakingCorrente) {\n this.speakingCorrente = parlando;\n this.notificaPeers();\n }\n let cambiato = false;\n for (const peer of this.copiaPeers()) {\n const riproduzione = this.riproduzioni.get(peer.id);\n if (this.livelloAnalizzatore(riproduzione?.analyser ?? null) > SOGLIA_AUDIO) {\n this.ultimoAudio.set(peer.id, dipendenze.ora());\n } else if (riproduzione?.analyser === null || riproduzione?.analyser === void 0) {\n const sources = riproduzione?.receiver?.getSynchronizationSources?.() ?? [];\n if (sources.some((source) => (source.audioLevel ?? 0) > SOGLIA_AUDIO)) {\n this.ultimoAudio.set(peer.id, dipendenze.ora());\n }\n }\n const speaking = !peer.muted && dipendenze.ora() - (this.ultimoAudio.get(peer.id) ?? 0) <= DURATA_PARLANTE;\n if ((this.speakingPeers.get(peer.id) ?? false) !== speaking) {\n this.speakingPeers.set(peer.id, speaking);\n cambiato = true;\n }\n }\n if (cambiato) this.notificaPeers();\n }\n livelloAnalizzatore(analyser) {\n const nodo = analyser;\n if (nodo?.getFloatTimeDomainData === void 0) return 0;\n const campioni = new Float32Array(nodo.fftSize);\n nodo.getFloatTimeDomainData(campioni);\n return Math.sqrt(campioni.reduce((somma, valore) => somma + valore * valore, 0) / Math.max(1, campioni.length));\n }\n copiaPeers() {\n const you = this.contesto.you();\n const giocatori = this.contesto.giocatori();\n const player = giocatori.find((item) => item.id === you);\n return this.roster.flatMap((peer) => {\n if (peer.id === you) return [];\n if (this.modeCorrente === "team") {\n const altro = giocatori.find((item) => item.id === peer.id);\n if (player?.role !== "spectator" && altro?.team !== player?.team) return [];\n }\n return [{\n id: peer.id,\n mic: peer.mic,\n muted: peer.muted,\n speaking: peer.mic && !peer.muted && (this.speakingPeers.get(peer.id) ?? false),\n volume: this.volumi.get(peer.id) ?? 1,\n gain: this.gains.get(peer.id) ?? 1\n }];\n });\n }\n pulisciPeerAssenti() {\n const presenti = new Set(this.roster.map((peer) => peer.id));\n for (const playerId of this.speakingPeers.keys()) {\n if (!presenti.has(playerId)) this.speakingPeers.delete(playerId);\n }\n for (const playerId of this.zeroDa.keys()) {\n if (presenti.has(playerId)) continue;\n this.zeroDa.delete(playerId);\n const timer = this.timerZero.get(playerId);\n if (timer !== void 0) this.dipendenze?.clearTimeout(timer);\n this.timerZero.delete(playerId);\n }\n }\n osservaCaduta(pc) {\n pc.addEventListener("connectionstatechange", () => {\n if (this.stateCorrente === "on" && (pc.connectionState === "failed" || pc.connectionState === "disconnected")) this.avviaRiconnessione();\n });\n }\n avviaRiconnessione() {\n if (!this.desiderata || this.stateCorrente === "reconnecting") return;\n this.generazione++;\n this.rifiutaRichieste(creaErrore("voice_error", "The voice connection was restarted."));\n this.chiudiRisorse();\n this.tentativoRiconnessione = 0;\n this.aggiornaState("reconnecting");\n this.programmaRiconnessione();\n }\n programmaRiconnessione() {\n if (!this.desiderata || !this.contesto.connessa() || this.timerRiconnessione !== null || this.stateCorrente !== "reconnecting") return;\n const ritardo = RITARDI_RICONNESSIONE[this.tentativoRiconnessione];\n if (ritardo === void 0) {\n this.desiderata = false;\n this.aggiornaState("off");\n return;\n }\n this.tentativoRiconnessione++;\n this.timerRiconnessione = this.richiediDipendenze().setTimeout(() => {\n this.timerRiconnessione = null;\n const generazione = ++this.generazione;\n void this.entra(generazione).catch(() => {\n if (generazione !== this.generazione || !this.desiderata) return;\n this.chiudiRisorse();\n this.aggiornaState("reconnecting");\n this.programmaRiconnessione();\n });\n }, ritardo);\n }\n fermaRiconnessione() {\n if (this.timerRiconnessione === null || this.dipendenze === null) return;\n this.dipendenze.clearTimeout(this.timerRiconnessione);\n this.timerRiconnessione = null;\n }\n chiudiRisorse() {\n const dipendenze = this.dipendenze;\n this.cancellaAttesaConnessione?.();\n this.cancellaAttesaConnessione = null;\n if (dipendenze !== null) {\n if (this.intervalloAudio !== null) dipendenze.clearInterval(this.intervalloAudio);\n if (this.timerConnessione !== null) dipendenze.clearTimeout(this.timerConnessione);\n for (const timer of this.timerZero.values()) dipendenze.clearTimeout(timer);\n }\n this.intervalloAudio = null;\n this.timerConnessione = null;\n this.timerZero.clear();\n for (const playerId of [...this.riproduzioni.keys()]) this.scollegaTraccia(playerId);\n this.peerSfu?.close();\n this.peerSfu = null;\n for (const item of this.mesh.values()) item.pc.close();\n this.mesh.clear();\n this.sfuAttive.clear();\n this.midGiocatori.clear();\n this.negati.clear();\n for (const track of this.stream?.getTracks() ?? []) track.stop();\n this.stream = null;\n this.tracciaMic = null;\n this.analyser = null;\n void this.audioContext?.close().catch(() => void 0);\n this.audioContext = null;\n this.sessioneSfu = null;\n this.connessioneSfuAttesa = false;\n this.trasporto = null;\n this.speakingCorrente = false;\n this.ultimoAudioMic = Number.NEGATIVE_INFINITY;\n this.speakingPeers.clear();\n this.ultimoAudio.clear();\n this.negoziazione = Promise.resolve();\n }\n richiedi(message) {\n if (!this.contesto.connessa()) return Promise.reject(creaErrore("offline", "The room is reconnecting."));\n const r = ++this.sequenzaRichieste;\n return new Promise((resolve, reject) => {\n this.richieste.set(r, { resolve, reject });\n try {\n this.contesto.invia({ ...message, r });\n } catch (cause) {\n this.richieste.delete(r);\n reject(cause);\n }\n });\n }\n rifiutaRichieste(reason) {\n for (const richiesta of this.richieste.values()) richiesta.reject(reason);\n this.richieste.clear();\n }\n aggiornaState(state) {\n if (state === this.stateCorrente) return;\n this.stateCorrente = state;\n for (const listener of this.ascoltatoriState) {\n try {\n listener(state);\n } catch {\n }\n }\n }\n notificaPeers() {\n const peers = this.copiaPeers();\n for (const listener of this.ascoltatoriPeers) {\n try {\n listener(peers);\n } catch {\n }\n }\n }\n controllaGenerazione(generazione) {\n if (generazione !== this.generazione || !this.desiderata) {\n throw creaErrore("offline", "Voice was stopped.");\n }\n }\n richiediDipendenze() {\n if (this.dipendenze === null) throw creaErrore("unsupported", "Voice is not supported.");\n return this.dipendenze;\n }\n richiediMic() {\n if (this.tracciaMic === null) throw creaErrore("voice_error", "The microphone is not ready.");\n return this.tracciaMic;\n }\n richiediAudioContext() {\n if (this.audioContext === null) throw creaErrore("voice_error", "Audio is not ready.");\n return this.audioContext;\n }\n permessoNegato(cause) {\n return typeof cause === "object" && cause !== null && "name" in cause && (cause.name === "NotAllowedError" || cause.name === "SecurityError");\n }\n mappaErrore(cause) {\n if (typeof cause === "object" && cause !== null && "code" in cause) {\n const code = cause.code;\n if (code === "voice_disabled" || code === "permission_denied" || code === "unsupported" || code === "spectator" || code === "offline" || code === "voice_error") return cause;\n return creaErrore("voice_error", "Voice could not be started.");\n }\n return creaErrore("voice_error", "Voice could not be started.");\n }\n};\n\n// src/stanza-client/index.ts\nvar APERTO = 1;\nvar RITARDI_RICONNESSIONE2 = [1e3, 2e3, 4e3, 8e3];\nvar GRAZIA_RICONNESSIONE = 6e4;\nvar INTERVALLO_PING = 5e3;\nvar RITARDO_FLUSH = 500;\nvar ATTESA_ROSTER = 2e3;\nvar CHIUSURE_DEFINITIVE = /* @__PURE__ */ new Set([4003, 4004, 4005, 4006, 4008, 4009]);\nfunction record3(value) {\n return typeof value === "object" && value !== null && !Array.isArray(value) ? value : null;\n}\nfunction ingressoValido(value) {\n const dati = record3(value);\n return dati !== null && typeof dati.roomId === "string" && typeof dati.code === "string" && typeof dati.join === "string" && typeof dati.url === "string";\n}\nfunction visioneValida(value) {\n const dati = record3(value);\n return dati !== null && typeof dati.roomId === "string" && typeof dati.code === "string" && typeof dati.watch === "string" && typeof dati.url === "string";\n}\nfunction rispostaMatchValida(value) {\n const dati = record3(value);\n const players = record3(dati?.players);\n return dati !== null && typeof dati.url === "string" && Number.isInteger(dati.timeoutMs) && dati.timeoutMs >= 1e3 && dati.timeoutMs <= 3e5 && players !== null && Number.isInteger(players.min) && Number.isInteger(players.max) && players.min >= 1 && players.max >= players.min;\n}\nfunction copiaJson(value) {\n return JSON.parse(JSON.stringify(value));\n}\nfunction applicaPatch(state, value) {\n let risultato = copiaJson(state);\n for (const operazione of value) {\n if (operazione.path.length === 0) {\n if (operazione.op !== "set") return { ok: false };\n risultato = copiaJson(operazione.value);\n continue;\n }\n let contenitore = risultato;\n const percorso = operazione.path;\n for (let indice = 0; indice < percorso.length - 1; indice++) {\n const parte = percorso[indice];\n if (Array.isArray(contenitore)) {\n if (typeof parte !== "number" || parte >= contenitore.length) return { ok: false };\n contenitore = contenitore[parte];\n } else {\n const oggetto2 = record3(contenitore);\n if (oggetto2 === null || typeof parte !== "string" || !Object.hasOwn(oggetto2, parte)) {\n return { ok: false };\n }\n contenitore = oggetto2[parte];\n }\n }\n const ultima = percorso.at(-1);\n if (Array.isArray(contenitore)) {\n if (operazione.op !== "set" || typeof ultima !== "number" || ultima >= contenitore.length) return { ok: false };\n contenitore[ultima] = copiaJson(operazione.value);\n } else {\n const oggetto2 = record3(contenitore);\n if (oggetto2 === null || typeof ultima !== "string") return { ok: false };\n if (operazione.op === "del") {\n if (!Object.hasOwn(oggetto2, ultima)) return { ok: false };\n delete oggetto2[ultima];\n } else {\n Object.defineProperty(oggetto2, ultima, {\n configurable: true,\n enumerable: true,\n value: copiaJson(operazione.value),\n writable: true\n });\n }\n }\n }\n return { ok: true, state: risultato };\n}\nfunction creaApiLive(input) {\n const richiesta = creaRichiedente(input.liveOrigin, "", input.fetcher, input.biglietto);\n async function ingresso(path, body, rinnova = false) {\n const value = await richiesta(path, "POST", body, rinnova);\n if (!ingressoValido(value)) {\n throw creaErrore("internal_error", "The room service returned an invalid response.");\n }\n return value;\n }\n async function match(options) {\n const value = await richiesta("/match", "POST", {\n mode: options.mode,\n key: options.key\n });\n if (!rispostaMatchValida(value)) {\n throw creaErrore("internal_error", "The matchmaking service returned an invalid response.");\n }\n return value;\n }\n async function visione(body, rinnova = false) {\n const value = await richiesta("/rooms/watch", "POST", body, rinnova);\n if (!visioneValida(value)) {\n throw creaErrore("internal_error", "The room service returned an invalid response.");\n }\n return value;\n }\n return {\n create: (mode) => ingresso("/rooms", { mode }),\n joinCode: (code) => ingresso("/rooms/join", { code }),\n joinRoom: (roomId) => ingresso("/rooms/join", { roomId }, true),\n watchCode: (code) => visione({ code }),\n watchRoom: (roomId) => visione({ roomId }, true),\n match,\n flush: (roomId) => richiesta(\n `/rooms/${encodeURIComponent(roomId)}/flush`,\n "POST"\n )\n };\n}\nvar StanzaClient = class {\n constructor(roomId, codice, url, dipendenze, api, segnalaStanza, spettatore = false) {\n this.roomId = roomId;\n this.codice = codice;\n this.dipendenze = dipendenze;\n this.api = api;\n this.segnalaStanza = segnalaStanza;\n this.spettatore = spettatore;\n this.meta = { host: null, mode: null, countdownAt: null, configuration: null, connection: "connecting", closedCode: null };\n this.metaListeners = /* @__PURE__ */ new Set();\n this.connectionListeners = /* @__PURE__ */ new Set();\n this.scoreListeners = /* @__PURE__ */ new Set();\n this.scores = [];\n this.errorListeners = /* @__PURE__ */ new Set();\n this.roleId = 0;\n this.roleRequests = /* @__PURE__ */ new Map();\n this.statoPubblico = null;\n this.statoSincronizzato = null;\n this.tickCorrente = 0;\n this.tickRateCorrente = 0;\n this.latenzaCorrente = null;\n this.ultimoInput = null;\n this.inputInviato = null;\n this.timerInput = null;\n this.ultimoInvioGioco = -Infinity;\n this.inviiGioco = [];\n this.seedCorrente = 0;\n this.statusCorrente = "lobby";\n this.giocatoriCorrenti = [];\n this.youCorrente = "";\n this.hostCorrente = null;\n this.resultCorrente = null;\n this.delaySpettatore = 0;\n this.socket = null;\n this.seq = 0;\n this.scartoOrario = 0;\n this.timerPing = null;\n this.timerRiconnessione = null;\n this.timerFlush = null;\n this.flushInCorso = false;\n this.flushRichiesto = false;\n this.ritardoIndice = 0;\n this.tempoRiconnessione = 0;\n this.resyncRichiesto = false;\n this.terminata = false;\n this.lasciata = false;\n this.prontaRisolta = false;\n this.welcomeRicevuto = false;\n this.rosterRicevuto = false;\n this.timerRoster = null;\n this.risolviPronta = () => void 0;\n this.rifiutaPronta = () => void 0;\n this.ascoltatoriStato = /* @__PURE__ */ new Set();\n this.ascoltatoriGiocatori = /* @__PURE__ */ new Set();\n this.ascoltatoriStatus = /* @__PURE__ */ new Set();\n this.ascoltatoriMessaggi = /* @__PURE__ */ new Set();\n this.promessaPronta = new Promise((resolve, reject) => {\n this.risolviPronta = resolve;\n this.rifiutaPronta = reject;\n });\n this.voice = new VoceClient({\n invia: (message) => this.invia(message),\n connessa: () => this.socket?.readyState === APERTO && this.welcomeRicevuto && !this.terminata && !this.lasciata,\n you: () => this.youCorrente,\n giocatori: () => this.copiaGiocatori(),\n rosterPronto: () => {\n this.rosterRicevuto = true;\n this.risolviProntaSePossibile();\n }\n }, dipendenze, dipendenze.voce);\n if (spettatore) this.rosterRicevuto = true;\n this.apri(url);\n }\n get mode() {\n return this.meta.mode;\n }\n get countdownAt() {\n return this.meta.countdownAt;\n }\n get connection() {\n return this.meta.connection;\n }\n get metadata() {\n return structuredClone(this.meta);\n }\n get queuedScores() {\n return structuredClone(this.scores);\n }\n onMetadata(listener) {\n this.metaListeners.add(listener);\n return () => this.metaListeners.delete(listener);\n }\n onConnection(listener) {\n this.connectionListeners.add(listener);\n return () => this.connectionListeners.delete(listener);\n }\n onError(listener) {\n this.errorListeners.add(listener);\n return () => this.errorListeners.delete(listener);\n }\n onScoreQueued(listener) {\n this.scoreListeners.add(listener);\n return () => this.scoreListeners.delete(listener);\n }\n metadataChanged(change) {\n const old = this.meta.connection;\n this.meta = { ...this.meta, ...change };\n this.notifica(this.metaListeners, this.metadata);\n if (old !== this.meta.connection) this.notifica(this.connectionListeners, this.meta.connection);\n }\n initialMetadata(room) {\n this.metadataChanged({\n host: room.host,\n mode: room.mode,\n countdownAt: room.countdownAt ?? null,\n rematch: room.rematch ?? null,\n configuration: room.configuration ?? null,\n connection: "connected",\n closedCode: null\n });\n }\n requestRole(role) {\n if (typeof role !== "string" || role.length < 1 || role.length > 32) return Promise.reject(creaErrore("invalid_role", "The role is not valid."));\n if (this.connection !== "connected" || this.status !== "playing" || !this.meta.configuration?.requestRole) {\n return Promise.reject(creaErrore("role_change_unavailable", "Roles cannot be requested right now."));\n }\n if (this.roleRequests.size >= 8) return Promise.reject(creaErrore("rate_limited", "Too many role requests."));\n const r = ++this.roleId;\n return new Promise((resolve, reject) => {\n const timer = this.dipendenze.setTimeout(() => {\n this.roleRequests.delete(r);\n reject(creaErrore("timeout", "The role request timed out."));\n }, 5e3);\n this.roleRequests.set(r, { resolve, reject, timer });\n try {\n this.invia({ t: "request-role", r, role });\n } catch (error) {\n this.dipendenze.clearTimeout(timer);\n this.roleRequests.delete(r);\n reject(error);\n }\n });\n }\n clearRoleRequests() {\n for (const request of this.roleRequests.values()) {\n this.dipendenze.clearTimeout(request.timer);\n request.reject(creaErrore("offline", "The room connection ended."));\n }\n this.roleRequests.clear();\n }\n disconnect() {\n if (this.lasciata) return;\n this.lasciata = true;\n const socket = this.socket;\n this.socket = null;\n this.voice.termina();\n this.fermaInput();\n this.fermaPing();\n this.fermaRiconnessione();\n this.clearRoleRequests();\n if (this.timerRoster !== null) this.dipendenze.clearTimeout(this.timerRoster);\n socket?.close(1e3);\n this.segnalaStanza(null);\n this.metadataChanged({ connection: "disconnected", closedCode: null });\n if (!this.prontaRisolta) {\n this.prontaRisolta = true;\n this.rifiutaPronta(creaErrore("cancelled", "The room was disconnected."));\n }\n }\n get state() {\n return this.statoPubblico;\n }\n get tick() {\n return this.tickCorrente;\n }\n get tickRate() {\n return this.tickRateCorrente;\n }\n get latency() {\n return this.latenzaCorrente;\n }\n get seed() {\n return this.seedCorrente;\n }\n get status() {\n return this.statusCorrente;\n }\n get players() {\n return this.copiaGiocatori();\n }\n get you() {\n return this.youCorrente;\n }\n get host() {\n return this.hostCorrente;\n }\n get code() {\n return this.codice;\n }\n get result() {\n return this.resultCorrente;\n }\n get delayMs() {\n return this.delaySpettatore;\n }\n pronta() {\n return this.promessaPronta;\n }\n invite() {\n return { code: this.codice, url: new URL(`/r/${this.codice}`, this.dipendenze.appOrigin).href };\n }\n onState(listener) {\n this.ascoltatoriStato.add(listener);\n return () => {\n this.ascoltatoriStato.delete(listener);\n };\n }\n onPlayers(listener) {\n this.ascoltatoriGiocatori.add(listener);\n return () => {\n this.ascoltatoriGiocatori.delete(listener);\n };\n }\n onStatus(listener) {\n this.ascoltatoriStatus.add(listener);\n return () => {\n this.ascoltatoriStatus.delete(listener);\n };\n }\n onMessage(listener) {\n this.ascoltatoriMessaggi.add(listener);\n return () => {\n this.ascoltatoriMessaggi.delete(listener);\n };\n }\n send(message) {\n if (this.statusCorrente === "finished") return;\n const prossimo = this.seq + 1;\n this.invia({ t: "msg", seq: prossimo, m: message });\n this.seq = prossimo;\n this.ultimoInvioGioco = this.dipendenze.ora();\n this.inviiGioco = [...this.inviiGioco.slice(-(MESSAGGI_GIOCO_AL_SECONDO - 1)), this.ultimoInvioGioco];\n }\n input(value) {\n if (this.terminata || this.lasciata || this.statusCorrente === "finished") return;\n try {\n const serializzato = JSON.stringify(value);\n if (serializzato === void 0) throw new TypeError();\n this.ultimoInput = serializzato;\n } catch {\n throw creaErrore("invalid_request", "Room input must be valid JSON.");\n }\n this.programmaInput();\n }\n pulisciInput() {\n this.fermaInput();\n this.ultimoInput = this.inputInviato = null;\n this.ultimoInvioGioco = -Infinity;\n this.inviiGioco = [];\n }\n fermaInput() {\n if (this.timerInput !== null) this.dipendenze.clearTimeout(this.timerInput);\n this.timerInput = null;\n }\n programmaInput() {\n if (this.timerInput !== null || this.ultimoInput === null || this.ultimoInput === this.inputInviato || !this.welcomeRicevuto || this.socket?.readyState !== APERTO || this.terminata || this.lasciata) return;\n const ora = this.dipendenze.ora();\n const frequenza = this.tickRateCorrente > 0 ? Math.min(MESSAGGI_GIOCO_AL_SECONDO, this.tickRateCorrente) : MESSAGGI_GIOCO_AL_SECONDO;\n const periodo = 1e3 / frequenza;\n this.inviiGioco = this.inviiGioco.filter((at) => ora - at < 1e3);\n const spazio = this.inviiGioco.length >= MESSAGGI_GIOCO_AL_SECONDO ? this.inviiGioco[0] + 1e3 : ora;\n const prossimo = Number.isFinite(this.ultimoInvioGioco) ? this.ultimoInvioGioco + periodo : ora + periodo;\n this.timerInput = this.dipendenze.setTimeout(() => {\n this.timerInput = null;\n if (this.ultimoInput === null || this.ultimoInput === this.inputInviato || !this.welcomeRicevuto || this.socket?.readyState !== APERTO || this.terminata || this.lasciata) return;\n const adesso = this.dipendenze.ora();\n if (adesso < this.ultimoInvioGioco + periodo || this.inviiGioco.filter((at) => adesso - at < 1e3).length >= MESSAGGI_GIOCO_AL_SECONDO) {\n this.programmaInput();\n return;\n }\n const valore = this.ultimoInput;\n try {\n this.send(JSON.parse(valore));\n this.inputInviato = valore;\n } catch {\n }\n }, Math.max(0, Math.ceil(Math.max(prossimo, spazio) - ora)));\n }\n aggiornaTickRate(value) {\n if (value === void 0 || !Number.isInteger(value) || value < 0 || value > 60 || value === this.tickRateCorrente) return;\n this.tickRateCorrente = value;\n this.fermaInput();\n this.programmaInput();\n }\n ready(ready) {\n this.invia({ t: "ready", ready });\n }\n setRole(role) {\n this.invia({ t: "role", role });\n }\n setTeam(team) {\n this.invia({ t: "team", team });\n }\n start() {\n this.invia({ t: "start" });\n }\n restart() {\n if (this.statusCorrente !== "finished") throw creaErrore("rematch_unavailable", "This room is not waiting for a rematch.");\n this.invia({ t: "restart" });\n }\n leave() {\n if (this.lasciata) return;\n if (!this.spettatore) this.voice.leave();\n this.lasciata = true;\n this.segnalaStanza(null);\n if (this.socket?.readyState === APERTO) {\n const socket = this.socket;\n this.invia({ t: "leave" });\n if (this.spettatore) socket.close(1e3);\n }\n this.termina(1e3);\n }\n serverTime() {\n return this.dipendenze.ora() + this.scartoOrario;\n }\n copiaGiocatori() {\n return this.giocatoriCorrenti.map((player) => ({ ...player }));\n }\n notifica(listeners, ...args) {\n for (const listener of listeners) {\n try {\n listener(...args);\n } catch {\n }\n }\n }\n invia(message) {\n if (this.socket?.readyState !== APERTO) {\n throw creaErrore("offline", "The room is reconnecting.");\n }\n let frame;\n try {\n frame = JSON.stringify(message);\n } catch {\n throw creaErrore("invalid_request", "Room messages must be valid JSON.");\n }\n this.socket.send(frame);\n }\n apri(url) {\n let socket;\n try {\n socket = this.dipendenze.apriSocket(url);\n } catch {\n this.programmaRiconnessione();\n return;\n }\n this.socket = socket;\n socket.addEventListener("open", () => {\n if (this.socket === socket) this.avviaPing();\n });\n socket.addEventListener("message", (evento) => {\n if (this.socket === socket && typeof evento.data === "string") this.ricevi(evento.data);\n });\n socket.addEventListener("close", (evento) => {\n if (this.socket === socket) this.chiuso(evento.code, evento.reason);\n });\n }\n avviaPing() {\n if (this.timerPing !== null) this.dipendenze.clearInterval(this.timerPing);\n this.timerPing = this.dipendenze.setInterval(() => {\n if (this.socket?.readyState !== APERTO) return;\n try {\n this.invia({ t: "ping", c: this.dipendenze.ora() });\n } catch {\n }\n }, INTERVALLO_PING);\n }\n fermaPing() {\n if (this.timerPing === null) return;\n this.dipendenze.clearInterval(this.timerPing);\n this.timerPing = null;\n }\n ricevi(frame) {\n let dati;\n try {\n const value = JSON.parse(frame);\n const oggetto2 = record3(value);\n if (oggetto2 === null || typeof oggetto2.t !== "string") return;\n dati = oggetto2;\n } catch {\n return;\n }\n try {\n if (dati.t === "watching") this.riceviWatching(dati);\n else if (dati.t === "welcome") this.riceviWelcome(dati);\n else if (dati.t === "players") this.riceviGiocatori(dati.players, dati.host);\n else if (dati.t === "status") this.riceviStatus(dati);\n else if (dati.t === "state") this.riceviDiff(dati);\n else if (dati.t === "snapshot") this.riceviSnapshot(dati);\n else if (dati.t === "msg") this.notifica(this.ascoltatoriMessaggi, copiaJson(dati.m));\n else if (dati.t === "pong") this.riceviPong(dati);\n else if (dati.t === "error") this.notifica(this.errorListeners, { code: dati.code, message: dati.message });\n else if (dati.t === "flush") this.richiediFlush();\n else if (dati.t === "score-queued" && !this.spettatore) {\n this.scores.push(structuredClone(dati.score));\n this.scores = this.scores.slice(-32);\n this.notifica(this.scoreListeners, structuredClone(dati.score));\n } else if (dati.t === "role-result") {\n const request = this.roleRequests.get(dati.r);\n if (request) {\n this.dipendenze.clearTimeout(request.timer);\n this.roleRequests.delete(dati.r);\n if (dati.ok) request.resolve();\n else request.reject(creaErrore(dati.code ?? "role_change_refused", "The role change was not accepted."));\n }\n } else if (dati.t === "voice") this.voice.ricevi(dati);\n } catch {\n if (dati.t === "state" || dati.t === "snapshot") this.chiediResync();\n }\n }\n riceviWatching(dati) {\n const room = dati.room;\n if (!this.spettatore || room.id !== this.roomId) return;\n this.aggiornaTickRate(room.tickRate);\n this.seedCorrente = room.seed;\n this.hostCorrente = room.host;\n this.statusCorrente = room.status;\n this.resultCorrente = copiaJson(room.result ?? null);\n if (room.status === "finished") this.pulisciInput();\n this.giocatoriCorrenti = dati.players.map((player) => ({ ...player }));\n this.delaySpettatore = dati.delayMs;\n this.aggiornaStato(dati.state, room.tick, room.serverTime);\n this.scartoOrario = room.serverTime - this.dipendenze.ora();\n this.resyncRichiesto = false;\n this.welcomeRicevuto = true;\n this.ritardoIndice = 0;\n this.tempoRiconnessione = 0;\n this.notifica(this.ascoltatoriGiocatori, this.copiaGiocatori());\n this.notifica(\n this.ascoltatoriStatus,\n this.statusCorrente,\n this.resultCorrente,\n room.serverTime\n );\n this.initialMetadata(room);\n this.programmaInput();\n this.risolviProntaSePossibile();\n }\n riceviWelcome(dati) {\n const room = dati.room;\n if (room.id !== this.roomId) return;\n this.youCorrente = dati.you;\n this.aggiornaTickRate(room.tickRate);\n this.seedCorrente = room.seed;\n this.hostCorrente = room.host;\n this.statusCorrente = room.status;\n this.resultCorrente = copiaJson(room.result ?? null);\n if (room.status === "finished") this.pulisciInput();\n this.giocatoriCorrenti = dati.players.map((player) => ({ ...player }));\n this.aggiornaStato(dati.state, room.tick, room.serverTime);\n this.scartoOrario = room.serverTime - this.dipendenze.ora();\n this.resyncRichiesto = false;\n this.welcomeRicevuto = true;\n if (!this.rosterRicevuto && this.timerRoster === null) {\n this.timerRoster = this.dipendenze.setTimeout(() => {\n this.timerRoster = null;\n this.rosterRicevuto = true;\n this.risolviProntaSePossibile();\n }, ATTESA_ROSTER);\n }\n this.ritardoIndice = 0;\n this.tempoRiconnessione = 0;\n this.notifica(this.ascoltatoriGiocatori, this.copiaGiocatori());\n this.voice.giocatoriCambiati();\n this.voice.socketRiconnesso();\n this.notifica(\n this.ascoltatoriStatus,\n this.statusCorrente,\n this.resultCorrente,\n room.serverTime\n );\n this.initialMetadata(room);\n this.programmaInput();\n this.risolviProntaSePossibile();\n }\n riceviGiocatori(value, host) {\n this.giocatoriCorrenti = value.map((player) => ({ ...player }));\n if (host !== void 0) this.hostCorrente = host;\n else if (!this.giocatoriCorrenti.some(\n (player) => player.id === this.hostCorrente && player.connected\n )) {\n this.hostCorrente = this.giocatoriCorrenti.find((player) => player.connected)?.id ?? null;\n }\n this.metadataChanged({ host: this.hostCorrente });\n this.notifica(this.ascoltatoriGiocatori, this.copiaGiocatori());\n this.voice.giocatoriCambiati();\n }\n riceviStatus(dati) {\n this.statusCorrente = dati.status;\n if (dati.host !== void 0) this.hostCorrente = dati.host;\n this.resultCorrente = copiaJson(dati.result);\n if (dati.status === "finished") {\n this.pulisciInput();\n this.clearRoleRequests();\n }\n if (dati.status === "ended") {\n this.terminata = true;\n this.clearRoleRequests();\n this.segnalaStanza(null);\n if (!this.spettatore) this.voice.termina();\n this.fermaPing();\n this.fermaRiconnessione();\n this.fermaInput();\n this.ultimoInput = null;\n }\n this.metadataChanged({\n rematch: dati.rematch ?? null,\n host: this.hostCorrente,\n countdownAt: dati.countdownAt ?? (dati.status === "countdown" ? dati.at : null),\n ...dati.status === "ended" ? { connection: "ended", closedCode: 4004 } : {}\n });\n this.notifica(this.ascoltatoriStatus, this.statusCorrente, this.resultCorrente, dati.at);\n }\n riceviDiff(dati) {\n this.aggiornaTickRate(dati.tickRate);\n if (dati.base !== this.tickCorrente) {\n this.chiediResync();\n return;\n }\n const risultato = applicaPatch(this.statoSincronizzato, dati.patch);\n if (!risultato.ok) {\n this.chiediResync();\n return;\n }\n this.resyncRichiesto = false;\n this.aggiornaStato(risultato.state, dati.tick, dati.serverTime);\n }\n riceviSnapshot(dati) {\n if (dati.tick < this.tickCorrente) return;\n this.aggiornaTickRate(dati.tickRate);\n this.resyncRichiesto = false;\n this.aggiornaStato(dati.state, dati.tick, dati.serverTime);\n }\n aggiornaStato(state, tick, serverTime) {\n this.statoSincronizzato = copiaJson(state);\n this.statoPubblico = copiaJson(state);\n this.tickCorrente = tick;\n this.notifica(this.ascoltatoriStato, this.statoPubblico, tick, serverTime);\n }\n chiediResync() {\n if (this.resyncRichiesto || this.socket?.readyState !== APERTO) return;\n this.resyncRichiesto = true;\n try {\n this.invia({ t: "resync" });\n } catch {\n this.resyncRichiesto = false;\n }\n }\n riceviPong(dati) {\n const ora = this.dipendenze.ora();\n if (!Number.isFinite(dati.c) || !Number.isFinite(dati.s) || dati.c > ora) return;\n const rtt = ora - dati.c;\n this.latenzaCorrente = this.latenzaCorrente === null ? rtt : this.latenzaCorrente * 0.8 + rtt * 0.2;\n this.scartoOrario = dati.s - (dati.c + ora) / 2;\n }\n chiuso(code, reason) {\n this.socket = null;\n this.welcomeRicevuto = false;\n this.latenzaCorrente = null;\n this.fermaInput();\n this.inputInviato = null;\n this.ultimoInvioGioco = -Infinity;\n this.inviiGioco = [];\n this.fermaPing();\n if (this.lasciata || this.terminata) return;\n if (CHIUSURE_DEFINITIVE.has(code)) {\n const errore = code === 4009 && reason === "message_too_large" ? "message_too_large" : void 0;\n if (errore) this.notifica(this.errorListeners, { code: errore, message: "The room message is too large." });\n this.termina(code, errore);\n return;\n }\n this.clearRoleRequests();\n if (!this.spettatore) this.voice.socketDisconnesso();\n this.programmaRiconnessione();\n }\n programmaRiconnessione() {\n if (this.terminata || this.lasciata || this.timerRiconnessione !== null) return;\n this.metadataChanged({ connection: "reconnecting" });\n const indice = Math.min(this.ritardoIndice, RITARDI_RICONNESSIONE2.length - 1);\n const ritardo = RITARDI_RICONNESSIONE2[indice];\n if (this.tempoRiconnessione + ritardo > GRAZIA_RICONNESSIONE) {\n this.termina("timeout");\n return;\n }\n this.ritardoIndice++;\n this.tempoRiconnessione += ritardo;\n this.timerRiconnessione = this.dipendenze.setTimeout(() => {\n this.timerRiconnessione = null;\n void this.riconnetti();\n }, ritardo);\n }\n async riconnetti() {\n if (this.terminata || this.lasciata) return;\n try {\n const ingresso = this.spettatore ? await this.api.watchRoom(this.roomId) : await this.api.joinRoom(this.roomId);\n if (this.terminata || this.lasciata) return;\n const codiceCambiato = this.codice !== ingresso.code;\n this.codice = ingresso.code;\n if (codiceCambiato && this.prontaRisolta && !this.terminata && !this.lasciata) {\n this.segnalaStanza({ code: this.codice });\n }\n this.apri(ingresso.url);\n } catch {\n this.programmaRiconnessione();\n }\n }\n fermaRiconnessione() {\n if (this.timerRiconnessione === null) return;\n this.dipendenze.clearTimeout(this.timerRiconnessione);\n this.timerRiconnessione = null;\n }\n termina(code, errore) {\n this.clearRoleRequests();\n this.metadataChanged({ connection: code === 1e3 ? "disconnected" : code === 4006 ? "replaced" : "closed", closedCode: typeof code === "number" ? code : null });\n const risultato = { closed: code };\n const cambiato = this.statusCorrente !== "ended" || JSON.stringify(this.resultCorrente) !== JSON.stringify(risultato);\n this.terminata = true;\n this.fermaInput();\n this.ultimoInput = null;\n this.segnalaStanza(null);\n this.statusCorrente = "ended";\n this.resultCorrente = risultato;\n if (!this.spettatore) this.voice.termina();\n this.fermaPing();\n this.fermaRiconnessione();\n if (cambiato) this.notifica(this.ascoltatoriStatus, "ended", risultato, this.serverTime());\n if (!this.prontaRisolta) {\n this.prontaRisolta = true;\n const codici = {\n 4003: "kicked",\n 4004: "room_ended",\n 4005: "version_closed",\n 4006: "replaced",\n 4008: "rate_limited",\n 4009: "invalid_request"\n };\n const erroreCode = errore ?? (typeof code === "number" ? codici[code] ?? "offline" : "offline");\n this.rifiutaPronta(creaErrore(erroreCode, "The room connection ended."));\n }\n }\n risolviProntaSePossibile() {\n if (this.prontaRisolta || !this.welcomeRicevuto || !this.rosterRicevuto) return;\n if (this.timerRoster !== null) {\n this.dipendenze.clearTimeout(this.timerRoster);\n this.timerRoster = null;\n }\n this.prontaRisolta = true;\n if (!this.spettatore && !this.terminata && !this.lasciata) {\n this.segnalaStanza({ code: this.codice });\n }\n this.risolviPronta();\n }\n richiediFlush() {\n this.flushRichiesto = true;\n if (this.flushInCorso || this.timerFlush !== null) return;\n this.timerFlush = this.dipendenze.setTimeout(() => {\n this.timerFlush = null;\n void this.eseguiFlush();\n }, RITARDO_FLUSH);\n }\n async eseguiFlush() {\n if (this.flushInCorso || !this.flushRichiesto) return;\n this.flushInCorso = true;\n this.flushRichiesto = false;\n try {\n await this.api.flush(this.roomId);\n } catch {\n } finally {\n this.flushInCorso = false;\n if (this.flushRichiesto) this.richiediFlush();\n }\n }\n};\nfunction creaStanzeOffline(invited = null) {\n return {\n invited,\n async create() {\n throw erroreOffline();\n },\n async join() {\n throw erroreOffline();\n },\n async watch() {\n throw erroreOffline();\n },\n async match() {\n throw erroreOffline();\n }\n };\n}\nfunction creaGestoreStanze(input, invited) {\n const api = creaApiLive(input);\n let haSegnalato = false;\n let ultimoCodice = null;\n const segnalaStanza = (room) => {\n const codice = room?.code ?? null;\n if (haSegnalato && codice === ultimoCodice) return;\n haSegnalato = true;\n ultimoCodice = codice;\n input.segnalaStanza?.(room);\n };\n const collega = async (ingresso) => {\n const stanza = new StanzaClient(\n ingresso.roomId,\n ingresso.code,\n ingresso.url,\n input,\n api,\n segnalaStanza\n );\n await stanza.pronta();\n return stanza;\n };\n const guarda = async (ingresso) => {\n const stanza = new StanzaClient(\n ingresso.roomId,\n ingresso.code,\n ingresso.url,\n input,\n api,\n () => void 0,\n true\n );\n await stanza.pronta();\n return {\n get mode() {\n return stanza.mode;\n },\n get countdownAt() {\n return stanza.countdownAt;\n },\n get connection() {\n return stanza.connection;\n },\n get metadata() {\n return stanza.metadata;\n },\n onMetadata: (listener) => stanza.onMetadata(listener),\n onConnection: (listener) => stanza.onConnection(listener),\n disconnect: () => stanza.disconnect(),\n get state() {\n return stanza.state;\n },\n get tick() {\n return stanza.tick;\n },\n get tickRate() {\n return stanza.tickRate;\n },\n get latency() {\n return stanza.latency;\n },\n get seed() {\n return stanza.seed;\n },\n get status() {\n return stanza.status;\n },\n get players() {\n return stanza.players;\n },\n get host() {\n return stanza.host;\n },\n get code() {\n return stanza.code;\n },\n get result() {\n return stanza.result;\n },\n get delayMs() {\n return stanza.delayMs;\n },\n onState: (listener) => stanza.onState(listener),\n onPlayers: (listener) => stanza.onPlayers(listener),\n onStatus: (listener) => stanza.onStatus(listener),\n onMessage: (listener) => stanza.onMessage(listener),\n leave: () => {\n stanza.leave();\n },\n serverTime: () => stanza.serverTime()\n };\n };\n const attendiMatch = (url, options) => new Promise((resolve, reject) => {\n let socket;\n let conclusa = false;\n const pulisci = () => {\n socket.removeEventListener("message", ricevi);\n socket.removeEventListener("close", chiuso);\n socket.removeEventListener("error", caduto);\n options.signal?.removeEventListener("abort", annulla);\n };\n const chiudi = () => {\n try {\n socket.close(1e3);\n } catch {\n }\n };\n const fallisci = (errore, chiudiSocket) => {\n if (conclusa) return;\n conclusa = true;\n pulisci();\n if (chiudiSocket) chiudi();\n reject(errore);\n };\n function annulla() {\n fallisci(\n creaErrore("cancelled", "The matchmaking search was cancelled."),\n true\n );\n }\n function chiuso() {\n fallisci(erroreOffline(), false);\n }\n function caduto() {\n fallisci(erroreOffline(), true);\n }\n function ricevi(evento) {\n let dati = null;\n try {\n dati = typeof evento.data === "string" ? record3(JSON.parse(evento.data)) : null;\n } catch {\n }\n if (dati === null || typeof dati.t !== "string") {\n fallisci(creaErrore("internal_error", "The matchmaking service sent an invalid message."), true);\n return;\n }\n if (dati.t === "waiting") {\n if (!Number.isInteger(dati.players) || !Number.isInteger(dati.min) || !Number.isInteger(dati.max)) {\n fallisci(creaErrore("internal_error", "The matchmaking service sent an invalid message."), true);\n return;\n }\n try {\n options.onWaiting?.({\n players: dati.players,\n min: dati.min,\n max: dati.max\n });\n } catch {\n }\n return;\n }\n if (dati.t === "matched") {\n if (!ingressoValido(dati)) {\n fallisci(creaErrore("internal_error", "The matchmaking service sent an invalid message."), true);\n return;\n }\n conclusa = true;\n pulisci();\n chiudi();\n resolve(dati);\n return;\n }\n if (dati.t === "no_match") {\n fallisci(creaErrore("no_match", "No match was found before the timeout."), true);\n return;\n }\n if (dati.t === "error") {\n fallisci(creaErrore(\n typeof dati.code === "string" ? dati.code : "internal_error",\n typeof dati.message === "string" ? dati.message : "The matchmaking service could not complete the search."\n ), true);\n return;\n }\n if (dati.t !== "pong") {\n fallisci(creaErrore("internal_error", "The matchmaking service sent an invalid message."), true);\n }\n }\n try {\n socket = input.apriSocket(url);\n } catch {\n reject(erroreOffline());\n return;\n }\n socket.addEventListener("message", ricevi);\n socket.addEventListener("close", chiuso);\n socket.addEventListener("error", caduto);\n options.signal?.addEventListener("abort", annulla, { once: true });\n if (options.signal?.aborted === true) annulla();\n });\n return {\n invited,\n async create(options) {\n return collega(await api.create(options.mode));\n },\n async join(code) {\n const scelto = code ?? invited;\n if (scelto === null || scelto === void 0 || scelto.length === 0) {\n throw creaErrore("invalid_request", "A room invitation code is required.");\n }\n return collega(await api.joinCode(scelto));\n },\n async watch(code) {\n if (typeof code !== "string" || code.length === 0) {\n throw creaErrore("invalid_request", "A room invitation code is required.");\n }\n return guarda(await api.watchCode(code));\n },\n async match(options) {\n const annullata = () => options.signal?.aborted === true;\n if (annullata()) {\n throw creaErrore("cancelled", "The matchmaking search was cancelled.");\n }\n const risposta = await api.match(options);\n if (annullata()) {\n throw creaErrore("cancelled", "The matchmaking search was cancelled.");\n }\n return collega(await attendiMatch(risposta.url, options));\n }\n };\n}\n\n// src/standalone.ts\nvar PREFISSO = "caisual:save:";\nvar CHIAVE_VALIDA = /^[a-z0-9][a-z0-9_-]{0,31}$/;\nfunction verificaChiave(key) {\n if (!CHIAVE_VALIDA.test(key)) {\n throw creaErrore("invalid_request", "Save keys must use lowercase letters, numbers, underscores, or hyphens.");\n }\n}\nfunction leggiSalvataggio(testo) {\n if (testo === null) return null;\n try {\n return JSON.parse(testo);\n } catch {\n return null;\n }\n}\nfunction chiavi(archivio) {\n const risultato = [];\n for (let indice = 0; indice < archivio.length; indice++) {\n const key = archivio.key(indice);\n if (key?.startsWith(PREFISSO)) risultato.push(key.slice(PREFISSO.length));\n }\n return risultato;\n}\nfunction creaSave(archivio, ora) {\n const disponibile = () => {\n if (archivio === null) throw erroreOffline();\n return archivio;\n };\n return {\n async set(key, value) {\n verificaChiave(key);\n const locale = disponibile();\n const corpo = JSON.stringify({ value });\n const bytes = new TextEncoder().encode(corpo).byteLength;\n if (bytes > 262144) {\n throw creaErrore("payload_too_large", "The save is larger than 262144 bytes.");\n }\n if (locale.getItem(PREFISSO + key) === null && chiavi(locale).length >= 32) {\n throw creaErrore("save_limit", "A game can store at most 32 save keys.");\n }\n const voce = { value, bytes, updatedAt: ora() };\n locale.setItem(PREFISSO + key, JSON.stringify(voce));\n return { key, bytes, updatedAt: voce.updatedAt };\n },\n async get(key) {\n verificaChiave(key);\n return leggiSalvataggio(disponibile().getItem(PREFISSO + key))?.value ?? null;\n },\n async remove(key) {\n verificaChiave(key);\n disponibile().removeItem(PREFISSO + key);\n },\n async list() {\n const locale = disponibile();\n return chiavi(locale).flatMap((key) => {\n const voce = leggiSalvataggio(locale.getItem(PREFISSO + key));\n return voce === null ? [] : [{ key, bytes: voce.bytes, updatedAt: voce.updatedAt }];\n }).sort((a, b) => a.key.localeCompare(b.key));\n }\n };\n}\nasync function creaStandalone(input, invited = null) {\n const startedAt = input.ora(), day = giornoUtc(startedAt);\n const seed = await calcolaSeed(input.hostname, day, input.subtle);\n return {\n connected: false,\n player: { id: "local", name: "Guest", guest: true },\n daily: creaDaily({ day, seed, expiresAt: prossimaMezzanotteUtc(startedAt) }, input.ora, async () => {\n const now = input.ora(), day2 = giornoUtc(now);\n return { day: day2, seed: await calcolaSeed(input.hostname, day2, input.subtle), expiresAt: prossimaMezzanotteUtc(now) };\n }),\n time: { now: input.ora },\n save: creaSave(input.archivio, input.ora),\n board: {\n async submit() {\n return { accepted: false, reason: "offline", verified: false };\n },\n async top(_board, opzioni = {}) {\n if (opzioni.day !== void 0 && (!validBoardDay(opzioni.day) || opzioni.daily === false)) throw creaErrore("invalid_request", "day must be a real UTC date and cannot be combined with daily: false.");\n return { day: opzioni.day ?? (opzioni.daily ? day : null), entries: [], me: null };\n }\n },\n room: creaStanzeOffline(invited)\n };\n}\n\n// src/kit.ts\nfunction leggiAppOrigin(documento) {\n const valore = documento?.querySelector(\'meta[name="caisual-app"]\')?.getAttribute("content");\n if (valore === null || valore === void 0) return null;\n try {\n const url = new URL(valore);\n return url.origin === valore && (url.protocol === "https:" || url.protocol === "http:") ? valore : null;\n } catch {\n return null;\n }\n}\nfunction archivioReale() {\n try {\n return typeof localStorage === "undefined" ? null : localStorage;\n } catch {\n return null;\n }\n}\nfunction dipendenzeReali2() {\n return {\n finestra: typeof window === "undefined" ? null : window,\n documento: typeof document === "undefined" ? null : document,\n fetcher: (input, init) => globalThis.fetch(input, init),\n archivio: archivioReale(),\n language: typeof navigator === "undefined" ? "en" : navigator.language,\n pathname: typeof location === "undefined" ? "/" : location.pathname,\n hostname: typeof location === "undefined" ? "" : location.hostname,\n subtle: globalThis.crypto.subtle,\n ora: Date.now,\n sonda: () => probeDevice()\n };\n}\nasync function connetti(input) {\n const appOrigin = leggiAppOrigin(input.documento);\n const senzaPadre = input.finestra === null || input.finestra.parent === input.finestra;\n if (appOrigin === null || senzaPadre) {\n return localConnection(input);\n }\n const handshake = await attendiHandshake(\n input.finestra,\n appOrigin,\n input.timeoutHandshake\n );\n if (handshake === null) return localConnection(input);\n const biglietto = creaGestoreBiglietto(\n handshake.ticket,\n handshake.porta,\n input.finestra,\n input.ora,\n input.timeoutRinnovo,\n "portal"\n );\n const api = creaClienteApi(appOrigin, input.fetcher, biglietto);\n const prima = input.ora();\n let me;\n try {\n me = await api.me();\n } catch {\n const base2 = await creaStandalone(input, handshake.invite);\n return installSession(base2, handshake, input);\n }\n const dopo = input.ora();\n const scartoOrario = me.serverTime - (prima + dopo) / 2;\n const room = handshake.live === null ? creaStanzeOffline(handshake.invite) : creaGestoreStanze({\n appOrigin,\n liveOrigin: handshake.live,\n fetcher: input.fetcher,\n biglietto: creaGestoreBiglietto(\n null,\n handshake.porta,\n input.finestra,\n input.ora,\n input.timeoutRinnovo,\n "live"\n ),\n apriSocket(url) {\n if (input.apriSocket !== void 0) return input.apriSocket(url);\n if (typeof WebSocket === "undefined") throw erroreOffline();\n return new WebSocket(url);\n },\n ora: input.ora,\n setTimeout: (handler, timeout) => globalThis.setTimeout(handler, timeout),\n clearTimeout: (id) => globalThis.clearTimeout(id),\n setInterval: (handler, timeout) => globalThis.setInterval(handler, timeout),\n clearInterval: (id) => globalThis.clearInterval(id),\n voce: input.voce,\n segnalaStanza(room2) {\n try {\n handshake.porta.postMessage({ type: "caisual:room", room: room2 });\n } catch {\n }\n }\n }, handshake.invite);\n const base = {\n connected: true,\n player: me.player,\n daily: creaDaily(\n { day: me.day, seed: me.seed, expiresAt: me.expiresAt ?? prossimaMezzanotteUtc(me.serverTime) },\n () => input.ora() + scartoOrario,\n async () => {\n const next = await api.me();\n return { day: next.day, seed: next.seed, expiresAt: next.expiresAt ?? prossimaMezzanotteUtc(next.serverTime) };\n }\n ),\n time: { now: () => input.ora() + scartoOrario },\n save: {\n set: (key, value) => api.saveSet(key, value),\n get: (key) => api.saveGet(key),\n remove: (key) => api.saveRemove(key),\n list: () => api.saveList()\n },\n board: {\n async submit(board, score, opzioni = {}) {\n try {\n return await api.boardSubmit(board, score, opzioni.daily === true);\n } catch (errore) {\n if (typeof errore === "object" && errore !== null && "code" in errore && errore.code === "offline") return { accepted: false, reason: "offline", verified: false };\n throw errore;\n }\n },\n top: (board, opzioni = {}) => api.boardTop(board, opzioni)\n },\n room\n };\n return installSession(base, handshake, input);\n}\nfunction installSession(base, handshake, input) {\n const coordinator = createSession(base, handshake?.overlay?.configuration ?? null, base.connected && handshake?.live != null);\n if (handshake?.overlay) {\n const dispose = attachKitBridge(handshake.porta, handshake.overlay, coordinator);\n if (coordinator.session.capabilities.overlay && typeof window !== "undefined" && input?.finestra === window) window.addEventListener("pagehide", dispose, { once: true });\n }\n const preferences = handshake?.languagePreferences?.length ? handshake.languagePreferences : [handshake?.language ?? input?.language ?? "en"];\n const language = resolveGameLanguage(preferences, handshake?.gameLanguages ?? (handshake?.overlay ? manifestLanguages(handshake.overlay.configuration.manifest) : void 0));\n const uiLanguage = overlayLocale(handshake?.uiLanguage ?? handshake?.language ?? input?.language);\n return {\n ...base,\n player: { ...base.player, language, uiLanguage },\n text: createTextLoader(input?.fetcher ?? globalThis.fetch, language, input?.pathname),\n room: coordinator.rooms,\n session: coordinator.session,\n overlay: coordinator.overlay\n };\n}\nasync function localConnection(input) {\n return installSession(await creaStandalone(input), void 0, input);\n}\nfunction dispositivoSconosciuto() {\n return {\n webgl2: false,\n webgpu: false,\n wasm: false,\n threads: false,\n isolated: false,\n gpu: "none",\n memoryMb: null,\n cores: null,\n mobile: false,\n tier: "low"\n };\n}\nasync function attendiSonda(sonda) {\n let timer;\n try {\n return await Promise.race([\n Promise.resolve().then(sonda).catch(() => dispositivoSconosciuto()),\n new Promise((resolve) => {\n timer = globalThis.setTimeout(() => resolve(dispositivoSconosciuto()), 1500);\n })\n ]);\n } finally {\n if (timer !== void 0) globalThis.clearTimeout(timer);\n }\n}\nfunction creaKit(input = dipendenzeReali2()) {\n let promessa = null;\n return {\n connect() {\n promessa ?? (promessa = Promise.all([connetti(input), attendiSonda(input.sonda)]).then(([connessione, device]) => ({ ...connessione, device })));\n return promessa;\n }\n };\n}\n\n// src/index.ts\nvar caisual = creaKit();\nglobalThis.caisual = caisual;\nvar index_default = caisual;\nexport {\n caisual,\n index_default as default\n};\n');
5027
+ response.end(request.method === "HEAD" ? void 0 : '// Caisual game kit v0.13.0\n\n// ../contracts/src/slug.ts\nvar NOMI_RISERVATI = [\n "www",\n "api",\n "app",\n "play",\n "live",\n "multi",\n "cdn",\n "assets",\n "static",\n "mail",\n "mx",\n "ns1",\n "ns2",\n "autodiscover",\n "_dmarc",\n "admin",\n "login",\n "account",\n "auth",\n "pay",\n "secure",\n "support",\n "help",\n "blog",\n "status",\n "dev",\n "staging",\n "test",\n "caisual",\n "shipz"\n];\nvar RISERVATI = new Set(NOMI_RISERVATI);\nvar SLUG_NUOVO = /^[a-z0-9]+(?:-[a-z0-9]+)*$/;\nvar SLUG_STORICO = /^[0-9a-f]{8}-[0-9a-f]{4}-4[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/;\nfunction isValidSlug(value) {\n return value.length >= 3 && value.length <= 32 && SLUG_NUOVO.test(value) || SLUG_STORICO.test(value);\n}\nfunction isReservedSlug(value) {\n return RISERVATI.has(value);\n}\n\n// ../contracts/src/i18n.ts\nfunction normalizeLanguage(value) {\n if (typeof value !== "string" || value.length > 128) return null;\n try {\n return Intl.getCanonicalLocales(value)[0] ?? null;\n } catch {\n return null;\n }\n}\nfunction manifestLanguages(manifest) {\n return manifest.languages?.length ? [...manifest.languages] : [manifest.language ?? "en"];\n}\nfunction languageFallbacks(language, defaultLanguage = "en") {\n const result = [];\n let tag = normalizeLanguage(language);\n while (tag) {\n result.push(tag);\n const parts = tag.split("-");\n parts.pop();\n if (parts.at(-1)?.length === 1) parts.pop();\n tag = parts.join("-");\n }\n result.push(normalizeLanguage(defaultLanguage) ?? defaultLanguage);\n return [...new Set(result)];\n}\nfunction resolveGameLanguage(preferences, languages2 = []) {\n const declared = languages2.map(normalizeLanguage).filter((tag) => tag !== null);\n const preferred = preferences.map(normalizeLanguage).filter((tag) => tag !== null);\n if (!declared.length) return preferred[0] ?? "en";\n for (const preference of preferred) {\n for (const tag of languageFallbacks(preference, preference)) {\n if (declared.includes(tag)) return tag;\n }\n }\n return declared[0];\n}\nfunction isTextDictionary(value) {\n return typeof value === "object" && value !== null && !Array.isArray(value) && Object.values(value).every((text) => typeof text === "string");\n}\n\n// ../contracts/src/manifest.ts\nfunction risolviModalita(manifest, mode) {\n const scelta = mode === null ? void 0 : manifest.modes.find((voce) => voce.id === mode);\n if (mode !== null && scelta === void 0) throw new Error("The selected game mode does not exist.");\n return { players: { ...scelta?.players ?? manifest.players }, lobby: scelta?.lobby ?? manifest.lobby };\n}\nfunction modalitaLocale(manifest, mode) {\n return mode !== null && manifest.modes.some((voce) => voce.id === mode && voce.execution === "local");\n}\nvar TETTO_GIOCATORI = 24;\nvar RITARDO_SPETTATORI_MS = 3e3;\nvar MASSIMO_CLASSIFICHE = 32;\nvar CAMPI = /* @__PURE__ */ new Set([\n "overlay",\n "manifest",\n "id",\n "name",\n "description",\n "cover",\n "card",\n "icon",\n "screenshots",\n "tags",\n "languages",\n "language",\n "platform",\n "orientation",\n "input",\n "visibility",\n "network",\n "isolated",\n "requires",\n "players",\n "lobby",\n "persistent",\n "spectators",\n "boards",\n "roles",\n "teams",\n "voice",\n "modes"\n]);\nvar INPUT = /* @__PURE__ */ new Set(["keyboard", "mouse", "touch", "gamepad"]);\nvar PLATFORM = /* @__PURE__ */ new Set(["desktop", "mobile", "both"]);\nvar ORIENTATION = /* @__PURE__ */ new Set(["landscape", "portrait"]);\nvar VISIBILITY = /* @__PURE__ */ new Set(["public", "unlisted"]);\nvar VOICE = /* @__PURE__ */ new Set(["none", "room", "team", "proximity"]);\nvar PERFORMANCE = /* @__PURE__ */ new Set(["light", "medium", "heavy"]);\nvar TAG = /^[a-z0-9-]+$/;\nvar ID_INTERNO = /^[a-z0-9]+(?:-[a-z0-9]+)*$/;\nvar CAMPO_MATCHMAKING = /^[a-z0-9][a-z0-9-]{0,31}$/;\nvar ID_CLASSIFICA = /^[a-z0-9][a-z0-9_-]{0,31}$/;\nfunction oggetto(value) {\n if (typeof value !== "object" || value === null || Array.isArray(value)) return null;\n return value;\n}\nfunction percorsoRelativo(value) {\n if (value === "" || value.startsWith("/") || value.includes("\\\\") || value.includes("\\0")) return false;\n if (value.includes("?") || value.includes("#")) return false;\n const parti = value.split("/");\n if (parti.some((parte) => parte === "" || parte === "." || parte === "..")) return false;\n try {\n const decoded = parti.map((parte) => decodeURIComponent(parte));\n return !decoded.some((parte) => parte === "" || parte === "." || parte === ".." || parte.includes("/"));\n } catch {\n return false;\n }\n}\nfunction hostValido(value) {\n if (value.length === 0 || value.length > 253) return false;\n if (value.includes("://") || /[/:?#@]/.test(value)) return false;\n const parti = value.split(".");\n return parti.every(\n (parte) => parte.length >= 1 && parte.length <= 63 && /^[a-z0-9](?:[a-z0-9-]*[a-z0-9])?$/i.test(parte)\n );\n}\nfunction interoTra(value, min, max) {\n return typeof value === "number" && Number.isInteger(value) && value >= min && value <= max;\n}\nfunction stringaDefault(dati, campo, valoreDefault, errori) {\n const value = dati[campo];\n if (value === void 0) return valoreDefault;\n if (typeof value !== "string") {\n errori.push(`${campo}: must be a string.`);\n return valoreDefault;\n }\n return value;\n}\nfunction testoFacoltativo(value, key, max, path, errors) {\n if (value[key] === void 0) return void 0;\n const check = (text2, field2) => {\n if (typeof text2 !== "string" || text2.trim().length === 0 || text2.trim().length > max || /[\\r\\n\\u0000-\\u001f]/.test(text2)) {\n errors.push(`${field2}: must contain 1-${max} characters on one line.`);\n return void 0;\n }\n return text2.trim();\n };\n const text = value[key], field = `${path}.${key}`;\n if (typeof text === "string") return check(text, field);\n const translations = oggetto(text);\n if (!translations || Object.keys(translations).length === 0) {\n errors.push(`${field}: must be a string or a non-empty language-to-text object.`);\n return void 0;\n }\n const result = {};\n for (const [raw, text2] of Object.entries(translations)) {\n const tag = normalizeLanguage(raw);\n if (!tag) {\n errors.push(`${field}.${raw}: must be a BCP 47 language tag.`);\n continue;\n }\n if (Object.hasOwn(result, tag)) errors.push(`${field}.${raw}: duplicate language.`);\n const checked = check(text2, `${field}.${raw}`);\n if (checked !== void 0) result[tag] = checked;\n }\n return result;\n}\nfunction validaManifest(valore) {\n const errori = [];\n const dati = oggetto(valore);\n if (dati === null) return { ok: false, errori: ["manifest: must be a JSON object."] };\n for (const campo of Object.keys(dati)) {\n if (!CAMPI.has(campo)) errori.push(`${campo}: unknown field.`);\n }\n if (dati.manifest === void 0) errori.push("manifest: is required and must be 1.");\n else if (dati.manifest !== 1) errori.push("manifest: must be exactly 1.");\n const id = stringaDefault(dati, "id", "", errori);\n if (dati.id === void 0) errori.push("id: is required.");\n else if (typeof dati.id === "string") {\n if (!isValidSlug(id)) {\n errori.push("id: must be 3-32 lowercase ASCII letters or digits with internal hyphens; historical UUID v4 slugs may be 36 characters.");\n } else if (isReservedSlug(id)) errori.push("id: this slug is reserved.");\n }\n const name = stringaDefault(dati, "name", "", errori);\n if (dati.name === void 0) errori.push("name: is required.");\n else if (typeof dati.name === "string" && (name.trim() === "" || name.length > 60)) {\n errori.push("name: must contain 1-60 characters.");\n }\n const description = stringaDefault(dati, "description", "", errori);\n if (description.length > 500) errori.push("description: must be at most 500 characters.");\n const immagini = { cover: "", card: "", icon: "" };\n const usati = /* @__PURE__ */ new Set();\n for (const campo of ["cover", "card", "icon"]) {\n const path = dati[campo];\n if (path === void 0 || path === null) errori.push(`${campo}: is required.`);\n else if (typeof path !== "string" || !percorsoRelativo(path)) errori.push(`${campo}: must be a relative file path inside client/ without query, fragment, or parent segments.`);\n else {\n if (!/\\.(png|jpe?g|webp)$/i.test(path)) errori.push(`${campo}: must be a PNG, JPEG or WebP file.`);\n const canonical = decodeURIComponent(path);\n if (usati.has(canonical)) errori.push(`${campo}: each image must use a different file; cover, card and icon cannot share a path.`);\n usati.add(canonical);\n immagini[campo] = path;\n }\n }\n const { cover, card, icon } = immagini;\n const screenshots = [];\n if (dati.screenshots !== void 0) {\n if (!Array.isArray(dati.screenshots)) errori.push("screenshots: must be an array of relative file paths.");\n else {\n if (dati.screenshots.length > 8) errori.push("screenshots: must contain at most 8 paths.");\n for (const [indice, value] of dati.screenshots.entries()) {\n if (typeof value !== "string" || !percorsoRelativo(value)) {\n errori.push(`screenshots[${indice}]: must be a relative file path without query, fragment, or parent segments.`);\n } else screenshots.push(value);\n }\n }\n }\n const tags = [];\n if (dati.tags !== void 0) {\n if (!Array.isArray(dati.tags)) errori.push("tags: must be an array.");\n else {\n if (dati.tags.length > 10) errori.push("tags: must contain at most 10 tags.");\n for (const [indice, value] of dati.tags.entries()) {\n if (typeof value !== "string" || value.length > 24 || !TAG.test(value)) {\n errori.push(`tags[${indice}]: must be 1-24 lowercase letters, digits, or hyphens.`);\n } else tags.push(value);\n }\n }\n }\n const legacyLanguage = stringaDefault(dati, "language", "en", errori);\n if (!/^[A-Za-z]{2,8}(?:-[A-Za-z0-9]{1,8})*$/.test(legacyLanguage)) {\n errori.push("language: must be a BCP 47 language tag such as en, it, or pt-BR.");\n }\n const languages2 = [];\n if (!Array.isArray(dati.languages) || dati.languages.length === 0) {\n errori.push("languages: must be a non-empty array of BCP 47 language tags.");\n } else for (const [index, raw] of dati.languages.entries()) {\n const tag = normalizeLanguage(raw);\n if (!tag) errori.push(`languages[${index}]: must be a BCP 47 language tag.`);\n else if (languages2.includes(tag)) errori.push(`languages[${index}]: duplicate language ${tag}.`);\n else languages2.push(tag);\n }\n if (!languages2.includes("en")) errori.push("languages: English is always required alongside the game\'s own languages.");\n const language = languages2[0] ?? legacyLanguage;\n if (dati.language !== void 0 && dati.languages !== void 0 && legacyLanguage.toLowerCase() !== language.toLowerCase()) {\n errori.push("language: must match the first entry in languages when both are present.");\n }\n let platform = "both";\n if (dati.platform === void 0) errori.push("platform: is required.");\n else if (typeof dati.platform !== "string" || !PLATFORM.has(dati.platform)) {\n errori.push("platform: must be desktop, mobile, or both.");\n } else platform = dati.platform;\n let orientation = "landscape";\n if (dati.orientation !== void 0) {\n if (typeof dati.orientation !== "string" || !ORIENTATION.has(dati.orientation)) {\n errori.push("orientation: must be landscape or portrait.");\n } else orientation = dati.orientation;\n }\n const input = [];\n if (dati.input !== void 0) {\n if (!Array.isArray(dati.input)) errori.push("input: must be an array.");\n else for (const [indice, value] of dati.input.entries()) {\n if (typeof value !== "string" || !INPUT.has(value)) {\n errori.push(`input[${indice}]: must be keyboard, mouse, touch, or gamepad.`);\n } else if (input.includes(value)) errori.push(`input[${indice}]: duplicate value ${value}.`);\n else input.push(value);\n }\n }\n let visibility = "public";\n if (dati.visibility !== void 0) {\n if (typeof dati.visibility !== "string" || !VISIBILITY.has(dati.visibility)) {\n errori.push("visibility: must be public or unlisted.");\n } else visibility = dati.visibility;\n }\n const network = [];\n if (dati.network !== void 0) {\n if (!Array.isArray(dati.network)) errori.push("network: must be an array of host names.");\n else for (const [indice, value] of dati.network.entries()) {\n if (typeof value !== "string" || !hostValido(value)) {\n errori.push(`network[${indice}]: must be a host name without scheme, port, path, query, or fragment.`);\n } else if (network.includes(value)) errori.push(`network[${indice}]: duplicate host ${value}.`);\n else network.push(value);\n }\n }\n let isolated = false;\n if (dati.isolated !== void 0) {\n if (typeof dati.isolated !== "boolean") errori.push("isolated: must be a boolean.");\n else isolated = dati.isolated;\n }\n const requires = {\n webgl2: false,\n webgpu: false,\n wasm: false,\n threads: false,\n memoryMb: null,\n performance: "light"\n };\n if (dati.requires !== void 0) {\n const value = oggetto(dati.requires);\n if (value === null) errori.push("requires: must be an object.");\n else {\n for (const campo of Object.keys(value)) {\n if (!["webgl2", "webgpu", "wasm", "threads", "memoryMb", "performance"].includes(campo)) {\n errori.push(`requires.${campo}: unknown field.`);\n }\n }\n for (const campo of ["webgl2", "webgpu", "wasm", "threads"]) {\n if (value[campo] === void 0) continue;\n if (typeof value[campo] !== "boolean") errori.push(`requires.${campo}: must be a boolean.`);\n else requires[campo] = value[campo];\n }\n if (value.memoryMb !== void 0) {\n if (value.memoryMb !== null && (!interoTra(value.memoryMb, 512, 32768) || value.memoryMb % 256 !== 0)) {\n errori.push("requires.memoryMb: must be an integer from 512 to 32768 in steps of 256, or null.");\n } else requires.memoryMb = value.memoryMb;\n }\n if (value.performance !== void 0) {\n if (typeof value.performance !== "string" || !PERFORMANCE.has(value.performance)) {\n errori.push("requires.performance: must be light, medium, or heavy.");\n } else requires.performance = value.performance;\n }\n if (requires.threads && !isolated) errori.push("requires.threads: needs isolated: true.");\n }\n }\n let players = { min: 1, max: 1 };\n if (dati.players !== void 0) {\n const value = oggetto(dati.players);\n if (value === null) errori.push("players: must be an object with min and max.");\n else {\n for (const campo of Object.keys(value)) {\n if (campo !== "min" && campo !== "max") errori.push(`players.${campo}: unknown field.`);\n }\n if (!interoTra(value.min, 1, TETTO_GIOCATORI)) errori.push(`players.min: must be an integer from 1 to ${TETTO_GIOCATORI}.`);\n if (!interoTra(value.max, 1, TETTO_GIOCATORI)) errori.push(`players.max: must be an integer from 1 to ${TETTO_GIOCATORI} in manifest version 1.`);\n if (interoTra(value.min, 1, TETTO_GIOCATORI) && interoTra(value.max, 1, TETTO_GIOCATORI)) {\n if (value.min > value.max) errori.push("players.max: must be greater than or equal to players.min.");\n else players = { min: value.min, max: value.max };\n }\n }\n }\n let lobby = false;\n if (dati.lobby !== void 0) {\n if (typeof dati.lobby !== "boolean") errori.push("lobby: must be a boolean.");\n else lobby = dati.lobby;\n }\n let persistent = false;\n if (dati.persistent !== void 0) {\n if (typeof dati.persistent !== "boolean") errori.push("persistent: must be a boolean.");\n else persistent = dati.persistent;\n }\n let spectators = { delayMs: RITARDO_SPETTATORI_MS };\n if (dati.spectators === false || dati.spectators === null) spectators = null;\n else if (dati.spectators !== void 0 && dati.spectators !== true) {\n const value = oggetto(dati.spectators);\n if (value === null) {\n errori.push("spectators: must be a boolean or an object with delayMs.");\n } else {\n for (const campo of Object.keys(value)) {\n if (campo !== "delayMs") errori.push(`spectators.${campo}: unknown field.`);\n }\n if (!interoTra(value.delayMs, 0, 3e4)) {\n errori.push("spectators.delayMs: must be an integer from 0 to 30000.");\n } else spectators = { delayMs: value.delayMs };\n }\n }\n let overlay = null;\n if (dati.overlay !== void 0 && dati.overlay !== null) {\n const value = oggetto(dati.overlay);\n if (value === null) errori.push("overlay: must be an object or null.");\n else {\n for (const key of Object.keys(value)) if (!["version", "accent"].includes(key)) errori.push(`overlay.${key}: unknown field.`);\n if (value.version !== 1) errori.push("overlay.version: must be exactly 1.");\n if (value.accent !== void 0 && (typeof value.accent !== "string" || !/^#[0-9a-fA-F]{6}$/.test(value.accent))) {\n errori.push("overlay.accent: must be a six-digit hexadecimal color, such as #336699.");\n }\n overlay = { version: 1, ...typeof value.accent === "string" ? { accent: value.accent } : {} };\n }\n }\n const boards = {};\n if (dati.boards !== void 0) {\n const value = oggetto(dati.boards);\n if (value === null) errori.push("boards: must be an object of board ids.");\n else {\n if (Object.keys(value).length > MASSIMO_CLASSIFICHE) {\n errori.push(`boards: at most ${MASSIMO_CLASSIFICHE} boards.`);\n }\n for (const [id2, raw] of Object.entries(value)) {\n let valido = true;\n if (!ID_CLASSIFICA.test(id2)) {\n errori.push(`boards.${id2}: invalid board id.`);\n valido = false;\n }\n const board = oggetto(raw);\n if (board === null) {\n errori.push(`boards.${id2}.source: must be "client" or "server".`);\n continue;\n }\n for (const campo of Object.keys(board)) {\n if (!["source", "label", "periods", "day"].includes(campo)) errori.push(`boards.${id2}.${campo}: unknown field.`);\n }\n if (board.source !== "client" && board.source !== "server") {\n errori.push(`boards.${id2}.source: must be "client" or "server".`);\n valido = false;\n }\n if (board.day !== void 0 && board.day !== "submit" && board.day !== "start") errori.push(`boards.${id2}.day: must be "submit" or "start".`);\n if (board.day === "start" && board.source !== "server") errori.push(`boards.${id2}.day: start requires source "server".`);\n const label = testoFacoltativo(board, "label", 48, `boards.${id2}`, errori);\n let periods = ["all-time"];\n if (board.periods !== void 0) {\n if (!Array.isArray(board.periods) || board.periods.length < 1 || board.periods.length > 2 || board.periods.some((period) => period !== "daily" && period !== "all-time") || new Set(board.periods).size !== board.periods.length) {\n errori.push(`boards.${id2}.periods: must contain daily, all-time, or both without duplicates.`);\n } else periods = [...board.periods];\n }\n if (valido) Object.defineProperty(boards, id2, { value: {\n source: board.source,\n periods,\n ...board.day === void 0 ? {} : { day: board.day },\n ...label === void 0 ? {} : { label }\n }, enumerable: true, configurable: true, writable: true });\n }\n }\n }\n const roles = [];\n if (dati.roles !== void 0) {\n if (!Array.isArray(dati.roles)) errori.push("roles: must be an array.");\n else {\n const ids = /* @__PURE__ */ new Set();\n for (const [indice, raw] of dati.roles.entries()) {\n const value = oggetto(raw);\n if (value === null) {\n errori.push(`roles[${indice}]: must be an object.`);\n continue;\n }\n for (const campo of Object.keys(value)) {\n if (!["id", "min", "max", "label"].includes(campo)) errori.push(`roles[${indice}].${campo}: unknown field.`);\n }\n const idRuolo = value.id;\n const min = value.min;\n const max = value.max;\n let valido = true;\n if (typeof idRuolo !== "string" || idRuolo.length > 32 || !ID_INTERNO.test(idRuolo)) {\n errori.push(`roles[${indice}].id: must be 1-32 lowercase letters, digits, or internal hyphens.`);\n valido = false;\n } else if (ids.has(idRuolo)) {\n errori.push(`roles[${indice}].id: duplicate role ${idRuolo}.`);\n valido = false;\n } else ids.add(idRuolo);\n if (!interoTra(min, 0, TETTO_GIOCATORI)) {\n errori.push(`roles[${indice}].min: must be an integer from 0 to ${TETTO_GIOCATORI}.`);\n valido = false;\n }\n if (max !== void 0 && !interoTra(max, 0, TETTO_GIOCATORI)) {\n errori.push(`roles[${indice}].max: must be an integer from 0 to ${TETTO_GIOCATORI} when present.`);\n valido = false;\n }\n if (typeof min === "number" && typeof max === "number" && min > max) {\n errori.push(`roles[${indice}].max: must be greater than or equal to min.`);\n valido = false;\n }\n const label = testoFacoltativo(value, "label", 32, `roles[${indice}]`, errori);\n if (valido) roles.push({\n id: idRuolo,\n min,\n ...max === void 0 ? {} : { max },\n ...label === void 0 ? {} : { label }\n });\n }\n }\n }\n let teams = null;\n if (dati.teams !== void 0 && dati.teams !== null) {\n const value = oggetto(dati.teams);\n if (value === null) errori.push("teams: must be null or an object with min and max.");\n else {\n for (const campo of Object.keys(value)) {\n if (campo !== "min" && campo !== "max") errori.push(`teams.${campo}: unknown field.`);\n }\n if (!interoTra(value.min, 2, TETTO_GIOCATORI)) errori.push(`teams.min: must be an integer from 2 to ${TETTO_GIOCATORI}.`);\n if (!interoTra(value.max, 2, TETTO_GIOCATORI)) errori.push(`teams.max: must be an integer from 2 to ${TETTO_GIOCATORI}.`);\n if (interoTra(value.min, 2, TETTO_GIOCATORI) && interoTra(value.max, 2, TETTO_GIOCATORI)) {\n if (value.min > value.max) errori.push("teams.max: must be greater than or equal to teams.min.");\n else teams = { min: value.min, max: value.max };\n }\n }\n }\n let voice = "none";\n if (dati.voice !== void 0) {\n if (typeof dati.voice !== "string" || !VOICE.has(dati.voice)) {\n errori.push("voice: must be none, room, team, or proximity.");\n } else voice = dati.voice;\n }\n const modes = [];\n if (dati.modes !== void 0) {\n if (!Array.isArray(dati.modes)) errori.push("modes: must be an array.");\n else {\n const ids = /* @__PURE__ */ new Set();\n for (const [indice, raw] of dati.modes.entries()) {\n const value = oggetto(raw);\n if (value === null) {\n errori.push(`modes[${indice}]: must be an object.`);\n continue;\n }\n for (const campo of Object.keys(value)) {\n if (!["id", "players", "lobby", "matchmaking", "execution", "label", "instructions"].includes(campo)) errori.push(`modes[${indice}].${campo}: unknown field.`);\n }\n if (typeof value.id !== "string" || value.id.length > 32 || !ID_INTERNO.test(value.id)) {\n errori.push(`modes[${indice}].id: must be 1-32 lowercase letters, digits, or internal hyphens.`);\n continue;\n }\n if (ids.has(value.id)) {\n errori.push(`modes[${indice}].id: duplicate mode ${value.id}.`);\n continue;\n }\n ids.add(value.id);\n const modo = { id: value.id };\n for (const [key2, max] of [["label", 48], ["instructions", 160]]) {\n const text = testoFacoltativo(value, key2, max, `modes[${indice}]`, errori);\n if (text !== void 0) modo[key2] = text;\n }\n if (value.execution !== void 0) {\n if (value.execution !== "local" && value.execution !== "room") errori.push(`modes[${indice}].execution: must be local or room.`);\n else modo.execution = value.execution;\n }\n if (overlay !== null && modo.execution === void 0) errori.push(`modes[${indice}].execution: is required with the standard overlay.`);\n if (value.players !== void 0) {\n const campo = `modes[${indice}].players`;\n const range = oggetto(value.players);\n if (range === null) errori.push(`${campo}: must be an object with min and max.`);\n else {\n for (const key2 of Object.keys(range)) {\n if (key2 !== "min" && key2 !== "max") errori.push(`${campo}.${key2}: unknown field.`);\n }\n if (!interoTra(range.min, 1, TETTO_GIOCATORI)) errori.push(`${campo}.min: must be an integer from 1 to ${TETTO_GIOCATORI}.`);\n if (!interoTra(range.max, 1, TETTO_GIOCATORI)) errori.push(`${campo}.max: must be an integer from 1 to ${TETTO_GIOCATORI}.`);\n if (interoTra(range.min, 1, TETTO_GIOCATORI) && interoTra(range.max, 1, TETTO_GIOCATORI)) {\n if (range.min > range.max) errori.push(`${campo}.max: must be greater than or equal to min.`);\n else modo.players = { min: range.min, max: range.max };\n }\n }\n }\n if (value.lobby !== void 0) {\n if (typeof value.lobby !== "boolean") errori.push(`modes[${indice}].lobby: must be a boolean.`);\n else modo.lobby = value.lobby;\n }\n if (modo.execution === "local") {\n const range = modo.players ?? players;\n if (range.min !== 1 || range.max !== 1) errori.push(`modes[${indice}].players: local execution requires min and max to be 1.`);\n if (modo.lobby ?? lobby) errori.push(`modes[${indice}].lobby: local execution requires false.`);\n if (value.matchmaking !== void 0) errori.push(`modes[${indice}].matchmaking: local execution cannot use matchmaking.`);\n }\n if (value.matchmaking === void 0) {\n modes.push(modo);\n continue;\n }\n const matchmaking = oggetto(value.matchmaking);\n if (matchmaking === null) {\n errori.push(`modes[${indice}].matchmaking: must be an object.`);\n continue;\n }\n for (const campo of Object.keys(matchmaking)) {\n if (!["key", "timeoutMs", "defaults"].includes(campo)) {\n errori.push(`modes[${indice}].matchmaking.${campo}: unknown field.`);\n }\n }\n let valido = true;\n const key = [];\n if (!Array.isArray(matchmaking.key) || matchmaking.key.length < 1 || matchmaking.key.length > 8) {\n errori.push(`modes[${indice}].matchmaking.key: must contain from 1 to 8 fields.`);\n valido = false;\n } else for (const [keyIndice, item] of matchmaking.key.entries()) {\n if (typeof item !== "string" || !CAMPO_MATCHMAKING.test(item)) {\n errori.push(`modes[${indice}].matchmaking.key[${keyIndice}]: must be 1-32 lowercase letters, digits, or hyphens and start with a letter or digit.`);\n valido = false;\n } else if (key.includes(item)) {\n errori.push(`modes[${indice}].matchmaking.key[${keyIndice}]: duplicate field ${item}.`);\n valido = false;\n } else key.push(item);\n }\n if (!interoTra(matchmaking.timeoutMs, 1e3, 3e5)) {\n errori.push(`modes[${indice}].matchmaking.timeoutMs: must be an integer from 1000 to 300000.`);\n valido = false;\n }\n let defaults;\n if (matchmaking.defaults !== void 0) {\n const values = oggetto(matchmaking.defaults);\n if (values === null || Object.keys(values).length !== key.length || key.some((field) => !Object.hasOwn(values, field))) {\n errori.push(`modes[${indice}].matchmaking.defaults: must contain exactly the declared key fields.`);\n } else {\n defaults = {};\n for (const [field, value2] of Object.entries(values)) {\n if (!(typeof value2 === "string" && value2.length >= 1 && value2.length <= 64 && /^[A-Za-z0-9_.:-]+$/.test(value2)) && !Number.isSafeInteger(value2)) {\n errori.push(`modes[${indice}].matchmaking.defaults.${field}: must be a string of 1-64 characters or a safe integer.`);\n } else Object.defineProperty(defaults, field, { value: value2, enumerable: true });\n }\n }\n }\n if (valido) modes.push({ ...modo, matchmaking: {\n ...defaults === void 0 ? {} : { defaults },\n key,\n timeoutMs: matchmaking.timeoutMs\n } });\n }\n }\n }\n if (overlay !== null && modes.length === 0) errori.push("modes: at least one explicit mode is required with the standard overlay.");\n if (errori.length > 0) return { ok: false, errori };\n return { ok: true, manifest: {\n manifest: 1,\n overlay,\n id,\n name,\n description,\n cover,\n card,\n icon,\n screenshots,\n tags,\n languages: languages2,\n language,\n platform,\n orientation,\n input,\n visibility,\n network,\n isolated,\n requires,\n players,\n lobby,\n persistent,\n spectators,\n boards,\n roles,\n teams,\n voice,\n modes\n } };\n}\n\n// ../contracts/src/device.ts\nfunction deviceTier(report) {\n if (report.gpu !== "hardware" || report.memoryMb !== null && report.memoryMb <= 2048) return "low";\n if (report.mobile || report.memoryMb !== null && report.memoryMb <= 4096 || report.cores !== null && report.cores <= 4) return "mid";\n return "high";\n}\nfunction perdiContesto(context) {\n try {\n context?.getExtension("WEBGL_lose_context")?.loseContext();\n } catch {\n }\n}\nfunction valoriSincroni(ambiente) {\n let navigator2;\n try {\n navigator2 = ambiente.navigator;\n } catch {\n navigator2 = void 0;\n }\n let memoryMb = null;\n try {\n const memory = navigator2?.deviceMemory;\n const converted = typeof memory === "number" ? memory * 1024 : NaN;\n if (Number.isFinite(converted)) memoryMb = converted;\n } catch {\n memoryMb = null;\n }\n let cores = null;\n try {\n const value = navigator2?.hardwareConcurrency;\n if (typeof value === "number" && Number.isFinite(value)) cores = value;\n } catch {\n cores = null;\n }\n let mobile = false;\n try {\n mobile = typeof navigator2?.userAgentData?.mobile === "boolean" ? navigator2.userAgentData.mobile : /Android|iPhone|iPad|iPod|Mobile/i.test(navigator2?.userAgent ?? "");\n } catch {\n mobile = false;\n }\n let isolated = false;\n try {\n isolated = ambiente.crossOriginIsolated === true;\n } catch {\n isolated = false;\n }\n return {\n webgl2: false,\n webgpu: false,\n wasm: false,\n threads: false,\n isolated,\n gpu: "none",\n memoryMb,\n cores,\n mobile\n };\n}\nasync function probeDevice(globals, timeoutMs = 1500) {\n const ambiente = globals ?? globalThis;\n const report = valoriSincroni(ambiente);\n const webgl = Promise.resolve().then(() => {\n try {\n const canvas = ambiente.document?.createElement("canvas");\n if (canvas === void 0) return;\n const hardware = canvas.getContext("webgl2", { failIfMajorPerformanceCaveat: true });\n if (hardware !== null) {\n report.webgl2 = true;\n report.gpu = "hardware";\n perdiContesto(hardware);\n return;\n }\n const software = canvas.getContext("webgl2");\n if (software !== null) {\n report.webgl2 = true;\n report.gpu = "software";\n perdiContesto(software);\n }\n } catch {\n report.webgl2 = false;\n report.gpu = "none";\n }\n });\n const webgpu = Promise.resolve().then(async () => {\n let device;\n try {\n const gpu = ambiente.navigator?.gpu;\n if (gpu === void 0) return;\n const adapter = await gpu.requestAdapter();\n if (adapter === null) return;\n device = await adapter.requestDevice();\n report.webgpu = true;\n } catch {\n report.webgpu = false;\n } finally {\n try {\n device?.destroy?.();\n } catch {\n }\n }\n });\n const wasm = Promise.resolve().then(() => {\n try {\n report.wasm = ambiente.WebAssembly?.validate(\n new Uint8Array([0, 97, 115, 109, 1, 0, 0, 0])\n ) === true;\n } catch {\n report.wasm = false;\n }\n });\n const threads = Promise.resolve().then(() => {\n try {\n if (ambiente.WebAssembly === void 0) return;\n new ambiente.WebAssembly.Memory({ initial: 1, maximum: 1, shared: true });\n report.threads = true;\n } catch {\n report.threads = false;\n }\n });\n let timer;\n await Promise.race([\n Promise.all([webgl, webgpu, wasm, threads]),\n new Promise((resolve) => {\n timer = setTimeout(resolve, Math.max(0, timeoutMs));\n })\n ]);\n if (timer !== void 0) clearTimeout(timer);\n return { ...report, tier: deviceTier(report) };\n}\n\n// ../contracts/src/versioni.ts\nfunction numeroVersione(value) {\n return typeof value === "number" && Number.isSafeInteger(value) && value > 0;\n}\n\n// ../contracts/src/overlay.ts\nfunction overlayConfiguration(manifest, coverUrl = null, invite = null, iconUrl = null) {\n const validated = validaManifest(manifest);\n if (!validated.ok) throw new Error("The overlay manifest is invalid.");\n return { manifest: validated.manifest, coverUrl, iconUrl, invite };\n}\nfunction record(value) {\n return typeof value === "object" && value !== null && !Array.isArray(value) ? value : null;\n}\nfunction validOverlayHello(value) {\n const hello = record(value), config = record(hello?.configuration);\n return hello?.v === 1 && typeof hello.epoch === "string" && hello.epoch.length > 0 && hello.epoch.length <= 128 && config !== null && (config.coverUrl === null || typeof config.coverUrl === "string") && (config.iconUrl === null || typeof config.iconUrl === "string") && (config.invite === null || typeof config.invite === "string" && /^[ABCDEFGHJKLMNPQRSTUVWXYZ23456789]{6}$/.test(config.invite)) && validaManifest(config.manifest).ok;\n}\nfunction normalizeOverlayHello(value) {\n if (!validOverlayHello(value)) return null;\n return { v: 1, epoch: value.epoch, configuration: overlayConfiguration(value.configuration.manifest, value.configuration.coverUrl, value.configuration.invite, value.configuration.iconUrl) };\n}\nfunction validSafeArea(value) {\n const area = record(value);\n return area !== null && Object.keys(area).length === 4 && ["top", "right", "bottom", "left"].every((key) => typeof area[key] === "number" && Number.isFinite(area[key]) && Number(area[key]) >= 0 && Number(area[key]) <= 1e5);\n}\nfunction validOverlayView(value) {\n const data = record(value);\n return data !== null && Object.keys(data).every((key) => ["inputBlocked", "reservedRects", "safeArea", "shortcutEnabled"].includes(key)) && (data.safeArea === void 0 || validSafeArea(data.safeArea)) && (data.shortcutEnabled === void 0 || typeof data.shortcutEnabled === "boolean") && typeof data.inputBlocked === "boolean" && Array.isArray(data.reservedRects) && data.reservedRects.length <= 8 && data.reservedRects.every((value2) => {\n const rect = record(value2);\n return rect !== null && Object.keys(rect).length === 4 && ["x", "y", "width", "height"].every((key) => typeof rect[key] === "number" && Number.isFinite(rect[key]) && rect[key] >= 0 && rect[key] <= 1e5);\n });\n}\nfunction validOverlayRequest(value) {\n const message = record(value), args = record(message?.args);\n if (message?.type !== "caisual:overlay" || message.v !== 1 || typeof message.epoch !== "string" || message.epoch.length < 1 || message.epoch.length > 128 || typeof message.requestId !== "string" || !(/^[1-9][0-9]{0,15}$/.test(message.requestId) && Number.isSafeInteger(Number(message.requestId))) || args === null) return false;\n if (Object.keys(message).some((key) => !["type", "v", "epoch", "requestId", "sessionId", "op", "args"].includes(key)) || !(message.sessionId === void 0 || message.sessionId === null || typeof message.sessionId === "string" && /^[1-9][0-9]{0,15}$/.test(message.sessionId))) return false;\n const keys = (...allowed) => Object.keys(args).every((key) => allowed.includes(key));\n const text = (key) => typeof args[key] === "string" && args[key].length >= 1 && args[key].length <= 64;\n switch (message.op) {\n case "local.start":\n return keys("mode") && text("mode");\n case "room.create":\n return keys("mode") && (args.mode === null || text("mode"));\n case "room.join":\n return keys("code") && (args.code === void 0 || text("code"));\n case "room.watch":\n return keys("code") && text("code");\n case "room.match": {\n const key = record(args.key);\n return keys("mode", "key") && text("mode") && (args.key === void 0 || key !== null && Object.keys(key).length <= 8 && Object.values(key).every((v) => typeof v === "string" && v.length >= 1 && v.length <= 64 || typeof v === "number" && Number.isSafeInteger(v)));\n }\n case "room.ready":\n return keys("ready") && typeof args.ready === "boolean";\n case "room.role":\n case "room.requestRole":\n return keys("role") && text("role");\n case "room.team":\n return keys("team") && Number.isInteger(args.team) && args.team >= 1 && args.team <= 24;\n case "room.restart":\n case "room.start":\n case "session.cancel":\n case "session.leave":\n case "session.disconnect":\n case "session.resume":\n return keys();\n case "voice.join":\n case "voice.leave":\n return keys() && typeof message.sessionId === "string";\n case "voice.mute":\n return keys("muted") && typeof args.muted === "boolean" && typeof message.sessionId === "string";\n case "voice.setVolume":\n return keys("playerId", "volume") && typeof message.sessionId === "string" && typeof args.playerId === "string" && args.playerId.length > 0 && args.playerId.length <= 128 && typeof args.volume === "number" && Number.isFinite(args.volume) && args.volume >= 0 && args.volume <= 1;\n case "overlay.view":\n return validOverlayView(args);\n default:\n return false;\n }\n}\nfunction validBoardDay(value) {\n if (typeof value !== "string" || !/^\\d{4}-\\d{2}-\\d{2}$/.test(value)) return false;\n const at = Date.parse(`${value}T00:00:00Z`);\n return Number.isFinite(at) && new Date(at).toISOString().slice(0, 10) === value;\n}\n\n// ../contracts/src/room-limits.ts\nvar MESSAGGI_GIOCO_AL_SECONDO = 20;\n\n// ../contracts/src/match-result.ts\nfunction readMatchResult(value, playerIds) {\n const object = (v) => v !== null && typeof v === "object" && !Array.isArray(v) ? v : null;\n const result = object(value);\n if (!result || !Array.isArray(result.standings)) return null;\n const known = new Set(playerIds), seen = /* @__PURE__ */ new Set();\n const standings = [];\n for (const item of result.standings) {\n const row = object(item);\n if (!row || typeof row.playerId !== "string" || !known.has(row.playerId) || seen.has(row.playerId)) continue;\n seen.add(row.playerId);\n standings.push({\n playerId: row.playerId,\n ...typeof row.score === "number" && Number.isFinite(row.score) ? { score: row.score } : {},\n ...typeof row.rank === "number" && Number.isSafeInteger(row.rank) && row.rank > 0 ? { rank: row.rank } : {}\n });\n }\n if (!standings.length) return null;\n return {\n standings,\n ...Array.isArray(result.winners) ? { winners: [...new Set(result.winners.filter((id) => typeof id === "string" && seen.has(id)))] } : {},\n ...typeof result.draw === "boolean" ? { draw: result.draw } : {},\n ...typeof result.unit === "string" ? { unit: result.unit } : {}\n };\n}\n\n// src/overlay/i18n.ts\nvar languages = ["en", "it", "es", "fr", "de", "pt"];\nvar words = {\n gameUpdated: ["This game was updated", "Questo gioco \\xE8 stato aggiornato", "Este juego se ha actualizado", "Ce jeu a \\xE9t\\xE9 mis \\xE0 jour", "Dieses Spiel wurde aktualisiert", "Este jogo foi atualizado"],\n reloadGame: ["Reload game", "Ricarica il gioco", "Recargar el juego", "Recharger le jeu", "Spiel neu laden", "Recarregar o jogo"],\n gameLanguages: ["Game languages", "Lingue del gioco", "Idiomas del juego", "Langues du jeu", "Spielsprachen", "Idiomas do jogo"],\n loading: ["Loading game...", "Caricamento...", "Cargando...", "Chargement...", "Spiel wird geladen...", "Carregando..."],\n loadingSlow: ["This game is taking longer than expected. You can wait a little longer or try again.", "Il gioco ci sta mettendo pi\\xF9 del previsto. Puoi aspettare ancora un po\\u2019 o riprovare.", "El juego est\\xE1 tardando m\\xE1s de lo esperado. Puedes esperar un poco m\\xE1s o volver a intentarlo.", "Le jeu met plus de temps que pr\\xE9vu. Vous pouvez patienter encore un peu ou r\\xE9essayer.", "Das Spiel braucht l\\xE4nger als erwartet. Du kannst noch etwas warten oder es erneut versuchen.", "O jogo est\\xE1 demorando mais do que o esperado. Voc\\xEA pode esperar mais um pouco ou tentar novamente."],\n home: ["Play", "Gioca", "Jugar", "Jouer", "Spielen", "Jogar"],\n homeMenu: ["Menu", "Menu", "Men\\xFA", "Menu", "Men\\xFC", "Menu"],\n mode: ["Mode", "Modalit\\xE0", "Modo", "Mode", "Modus", "Modo"],\n play: ["Play", "Gioca", "Jugar", "Jouer", "Spielen", "Jogar"],\n friendsPlay: ["Play with friends", "Gioca con amici", "Jugar con amigos", "Jouer entre amis", "Mit Freunden spielen", "Jogar com amigos"],\n find: ["Find players", "Trova giocatori", "Buscar jugadores", "Trouver des joueurs", "Spieler finden", "Buscar jogadores"],\n join: ["Join with code", "Entra con codice", "Entrar con c\\xF3digo", "Rejoindre avec un code", "Mit Code beitreten", "Entrar com c\\xF3digo"],\n joinInvite: ["Join this room", "Entra in questa stanza", "Entrar en est\\xE1 sala", "Rejoindre cette salle", "Diesem Raum beitreten", "Entrar nest\\xE1 sala"],\n watch: ["Watch a room", "Guarda una stanza", "Observar una sala", "Regarder une salle", "Raum ansehen", "Assistir a uma sala"],\n resume: ["Resume", "Riprendi", "Continuar", "Reprendre", "Fortsetzen", "Continuar"],\n room: ["Room", "Stanza", "Sala", "Salle", "Raum", "Sala"],\n code: ["Room code", "Codice stanza", "C\\xF3digo de sala", "Code de salle", "Raumcode", "C\\xF3digo da sala"],\n copy: ["Copy invite", "Copia invito", "Copiar invitaci\\xF3n", "Copier le lien", "Einladung kopieren", "Copiar convite"],\n copied: ["Invite copied", "Invito copiato", "Invitacion copiada", "Lien copi\\xE9", "Einladung kopiert", "Convite copiado"],\n copyFailed: ["Copy this link:", "Copia questo link:", "Copia este enlace:", "Copiez ce lien :", "Diesen Link kopieren:", "Copie este link:"],\n joining: ["Joining room...", "Ingresso nella stanza...", "Entrando en la sala...", "Connexion \\xE0 la salle...", "Raum wird betreten...", "Entrando na sala..."],\n matching: ["Finding your people...", "Ricerca giocatori...", "Buscando jugadores...", "Recherche de joueurs...", "Spieler werden gesucht...", "Buscando jogadores..."],\n queue: ["{n} / {max} players", "{n} / {max} giocatori", "{n} / {max} jugadores", "{n} / {max} joueurs", "{n} / {max} Spieler", "{n} / {max} jogadores"],\n cancel: ["Cancel", "Annulla", "Cancelar", "Annuler", "Abbrechen", "Cancelar"],\n close: ["Close", "Chiudi", "Cerrar", "Fermer", "Schlie\\xDFen", "Fechar"],\n back: ["Back", "Indietro", "Volver", "Retour", "Zur\\xFCck", "Voltar"],\n ready: ["Ready", "Pronto", "Listo", "Pr\\xEAt", "Bereit", "Pronto"],\n unready: ["Not ready", "Non pronto", "No listo", "Pas pr\\xEAt", "Nicht bereit", "N\\xE3o pronto"],\n start: ["Start", "Inizia", "Empezar", "Commencer", "Starten", "Come\\xE7ar"],\n role: ["Role", "Ruolo", "Rol", "R\\xF4le", "Rolle", "Fun\\xE7\\xE3o"],\n team: ["Team", "Squadra", "Equipo", "\\xC9quipe", "Team", "Equipe"],\n host: ["Host", "Host", "Anfitrion", "H\\xF4te", "Host", "Anfitri\\xE3o"],\n you: ["You", "Tu", "T\\xFA", "Vous", "Du", "Voc\\xEA"],\n away: ["Away", "Assente", "Ausente", "Absent", "Abwesend", "Ausente"],\n needPlayers: ["Waiting for more players", "In attesa di giocatori", "Esperando m\\xE1s jugadores", "En attente de joueurs", "Weitere Spieler fehlen", "Esperando mais jogadores"],\n needReady: ["Everyone needs to be ready", "Tutti devono essere pronti", "Todos deben estar listos", "Tout le monde doit \\xEAtre pr\\xEAt", "Alle m\\xFCssen bereit sein", "Todos precisam estar prontos"],\n needRoles: ["Fill the required roles", "Completa i ruoli richiesti", "Completa los roles", "Compl\\xE9tez les r\\xF4les", "Ben\\xF6tigte Rollen besetzen", "Complete as fun\\xE7\\xF5es"],\n needTeams: ["Choose the required teams", "Scegli le squadre richieste", "Elige los equipos", "Choisissez les \\xE9quipes", "Teams auswahlen", "Escolha as equipes"],\n waitHost: ["Waiting for the host", "In attesa dell\'host", "Esperando al anfitrion", "En attente de l\\u2019h\\xF4te", "Warten auf den Host", "Esperando o anfitri\\xE3o"],\n starting: ["Starting in", "Si inizia tra", "Empieza en", "D\\xE9but dans", "Start in", "Come\\xE7a em"],\n playing: ["Playing", "In partita", "Jugando", "En jeu", "Im Spiel", "Jogando"],\n ended: ["Game finished", "Partita conclusa", "Partida terminada", "Partie termin\\xE9e", "Spiel beendet", "Partida encerrada"],\n rematchReady: ["{n}/{max} ready", "{n}/{max} pronti", "{n}/{max} listos", "{n}/{max} pr\\xEAts", "{n}/{max} bereit", "{n}/{max} prontos"],\n rematchStart: ["Start rematch", "Avvia rivincita", "Iniciar revancha", "Lancer la revanche", "Revanche starten", "Iniciar revanche"],\n won: ["You won", "Hai vinto", "Has ganado", "Vous avez gagn\\xE9", "Du hast gewonnen", "Voc\\xEA venceu"],\n lost: ["You lost", "Hai perso", "Has perdido", "Vous avez perdu", "Du hast verloren", "Voc\\xEA perdeu"],\n draw: ["Draw", "Pareggio", "Empate", "\\xC9galit\\xE9", "Unentschieden", "Empate"],\n standings: ["Standings", "Piazzamenti", "Posiciones", "R\\xE9sultats", "Platzierungen", "Coloca\\xE7\\xF5es"],\n points: ["points", "punti", "puntos", "points", "Punkte", "pontos"],\n time: ["time", "tempo", "tiempo", "temps", "Zeit", "tempo"],\n distance: ["distance", "distanza", "distancia", "distance", "Distanz", "dist\\xE2ncia"],\n again: ["Play again", "Gioca ancora", "Jugar de nuevo", "Rejouer", "Erneut spielen", "Jogar novamente"],\n newRoom: ["New room. Share the new invite.", "Nuova stanza. Condividi il nuovo invito.", "Nueva sala. Comparte la invitaci\\xF3n.", "Nouvelle salle. Partagez le lien.", "Neuer Raum. Neue Einladung teilen.", "Nova sala. Compartilhe o convite."],\n watching: ["Watching", "In osservazione", "Observando", "Spectateur", "Zuschauen", "Assistindo"],\n delay: ["{n}s delay", "Ritardo {n}s", "Retraso de {n}s", "Retard de {n}s", "{n}s Verz\\xF6gerung", "Atraso de {n}s"],\n exit: ["Exit", "Esci", "Salir", "Quitter", "Verlassen", "Sair"],\n leaveNow: ["Leave for now", "Esci per ora", "Salir por ahora", "Quitter pour le moment", "Vor\\xFCbergehend verlassen", "Sair por enquanto"],\n leaveRoom: ["Leave room", "Lascia la stanza", "Abandonar sala", "Abandonner la salle", "Raum verlassen", "Deixar a sala"],\n leaveHint: ["Your room stays available for Resume.", "La stanza resta disponibile con Riprendi.", "Podr\\xE1s volver a est\\xE1 sala.", "Vous pourrez reprendre cette salle.", "Du kannst den Raum fortsetzen.", "Voc\\xEA pode voltar a est\\xE1 sala."],\n temporaryHint: ["The game continues. Rejoining may only be possible briefly.", "La partita continua. Il rientro pu\\xF2 essere disponibile solo per poco.", "La partida continua. Volver puede ser posible solo por poco tiempo.", "La partie continue. Le retour peut \\xEAtre limit\\xE9.", "Das Spiel l\\xE4uft weiter. R\\xFCckkehr nur kurz m\\xF6glich.", "A partida continua. O retorno pode ser limitado."],\n abandonHint: ["Leave room gives up your place.", "Lascia la stanza libera il tuo posto.", "Abandonar libera tu plaza.", "Abandonner lib\\xE8re votre place.", "Raum verlassen gibt deinen Platz frei.", "Deixar a sala libera sua vaga."],\n reconnecting: ["Reconnecting...", "Riconnessione...", "Reconectando...", "Reconnexion...", "Verbindung wird erneuert...", "Reconectando..."],\n replaced: ["Opened in another tab", "Aperta in un\\u2019altra scheda", "Abierta en otra pest\\xE1na", "Ouverte dans un autre onglet", "In anderem Tab ge\\xF6ffnet", "Aberta em outra aba"],\n error: ["Something went wrong. Try again.", "Qualcosa non va. Riprova.", "Algo sali\\xF3 mal. Reintenta.", "Une erreur est survenue. R\\xE9essayez.", "Etwas ist schiefgelaufen. Erneut versuchen.", "Algo deu errado. Tente novamente."],\n noRoom: ["This room is no longer available.", "Questa stanza non \\xE8 pi\\xF9 disponibile.", "Esta sala ya no est\\xE1 disponible.", "Cette salle n\'est plus disponible.", "Dieser Raum ist nicht mehr verf\\xFCgbar.", "Esta sala n\\xE3o est\\xE1 mais disponivel."],\n full: ["This room is full.", "La stanza \\xE8 piena.", "La sala est\\xE1 llena.", "Cette salle est pleine.", "Dieser Raum ist voll.", "Esta sala est\\xE1 cheia."],\n noMatch: ["No match this time. Try again.", "Nessun gruppo trovato. Riprova.", "No hay grupo. Reintenta.", "Aucun groupe trouv\\xE9. R\\xE9essayez.", "Keine Gruppe gefunden. Erneut versuchen.", "Nenhum grupo encontrado. Tente novamente."],\n invalidCode: ["Enter a six-character room code.", "Inserisci un codice di sei caratteri.", "Escribe un c\\xF3digo de seis caracteres.", "Entrez un code de six caracteres.", "Sechsstelligen Raumcode eingeben.", "Digite um c\\xF3digo de seis caracteres."],\n refused: ["The room did not accept that change.", "La stanza ha rifiutato la modifica.", "La sala rechaz\\xF3 el cambio.", "La salle a refus\\xE9 ce changement.", "Der Raum hat die \\xC4nderung abgelehnt.", "A sala recusou a altera\\xE7\\xE3o."],\n unavailable: ["Unavailable right now", "Non disponibile ora", "No disponible ahora", "Indisponible pour le moment", "Derzeit nicht verf\\xFCgbar", "Indisponivel agora"],\n offline: ["Connection unavailable. Try again.", "Connessione non disponibile. Riprova.", "Sin conexi\\xF3n. Reintenta.", "Connexion indisponible. R\\xE9essayez.", "Keine Verbindung. Erneut versuchen.", "Sem conex\\xE3o. Tente novamente."],\n saveFailed: ["Keep the room code. Resume could not be saved.", "Conserva il codice. Riprendi non \\xE8 stato salvato.", "Guarda el c\\xF3digo. No se pudo guardar el regreso.", "Gardez le code. La reprise ne peut pas \\xEAtre enregistr\\xE9e.", "Raumcode aufbewahren. Fortsetzen nicht gespeichert.", "Guarde o c\\xF3digo. O retorno n\\xE3o foi salvo."],\n boards: ["Leaderboard", "Classifica", "Clasificaci\\xF3n", "Classement", "Bestenliste", "Classifica\\xE7\\xE3o"],\n board: ["Board", "Classifica", "Tabla", "Classement", "Bestenliste", "Tabela"],\n daily: ["Daily", "Giornaliera", "Diaria", "Du jour", "T\\xE4glich", "Di\\xE1ria"],\n allTime: ["All time", "Di sempre", "Hist\\xF3rica", "Tous les temps", "Gesamt", "Geral"],\n accounts: ["Accounts", "Account", "Cuentas", "Comptes", "Konten", "Contas"],\n guests: ["Guests", "Ospiti", "Invitados", "Invit\\xE9s", "G\\xE4ste", "Visitantes"],\n category: ["Category", "Categoria", "Categoria", "Cat\\xE9gorie", "Kategorie", "Categoria"],\n period: ["Period", "Periodo", "Per\\xEDodo", "P\\xE9riode", "Zeitraum", "Per\\xEDodo"],\n rank: ["Rank", "Posizione", "Puesto", "Rang", "Platz", "Posicao"],\n score: ["Score", "Punteggio", "Puntos", "Score", "Punkte", "Pontos"],\n verified: ["Verified", "Verificato", "Verificado", "V\\xE9rifi\\xE9", "Verifiziert", "Verificado"],\n own: ["Your best", "Il tuo record", "Tu record", "Votre record", "Dein Rekord", "Seu recorde"],\n empty: ["No scores yet", "Nessun punteggio", "A\\xFAn no hay puntos", "Aucun score", "Noch keine Punkte", "Ainda sem pontos"],\n saving: ["Saving score...", "Salvataggio punteggio...", "Guardando puntos...", "Enregistrement du score...", "Punkte werden gespeichert...", "Salvando pontos..."],\n saved: ["Your best is on the board", "Il tuo record \\xE8 in classifica", "Tu record est\\xE1 en la tabla", "Votre record est au classement", "Dein Rekord ist eingetragen", "Seu recorde est\\xE1 na tabela"],\n bestAlready: ["Your best is already on the board", "Il tuo record era gi\\xE0 in classifica", "Tu record ya estaba en la tabla", "Votre record est d\\xE9j\\xE0 au classement", "Dein Rekord ist bereits eingetragen", "Seu recorde j\\xE1 est\\xE1 na tabela"],\n refresh: ["Refresh", "Aggiorna", "Actualizar", "Actualiser", "Aktualisieren", "Atualizar"],\n refreshHint: ["Score not visible yet. Refresh to check.", "Punteggio non ancora visibile. Aggiorna per controllare.", "Puntos a\\xFAn no visibles. Actualiza.", "Score pas encore visible. Actualisez.", "Punkte noch nicht sichtbar. Aktualisieren.", "Pontos ainda n\\xE3o visiveis. Atualize."],\n friends: ["Friends & party", "Amici e gruppo", "Amigos y grupo", "Amis et groupe", "Freunde & Gruppe", "Amigos e grupo"],\n localCrew: ["Friends and party are unavailable in local preview.", "Amici e gruppo non disponibili in anteprima locale.", "Amigos y grupo no disponibles en la vista local.", "Amis et groupe indisponibles en aper\\xE7u local.", "Freunde und Gruppe in lokaler Vorschau nicht verf\\xFCgbar.", "Amigos e grupo indispon\\xEDveis na pr\\xE9via local."],\n loginCrew: ["Sign in on Caisual to use friends and party.", "Accedi a Caisual per amici e gruppo.", "Inicia sesion para amigos y grupo.", "Connectez-vous pour utiliser amis et groupe.", "F\\xFCr Freunde und Gruppe bei Caisual anmelden.", "Entre no Caisual para amigos e grupo."],\n online: ["Online", "Online", "En linea", "En ligne", "Online", "Online"],\n noFriends: ["No friends online", "Nessun amico online", "Sin amigos en linea", "Aucun ami en ligne", "Keine Freunde online", "Nenhum amigo online"],\n createParty: ["Create party", "Crea gruppo", "Crear grupo", "Cr\\xE9er un groupe", "Gruppe erstellen", "Criar grupo"],\n inviteParty: ["Invite to party", "Invita nel gruppo", "Invitar al grupo", "Inviter au groupe", "In Gruppe einladen", "Convidar para o grupo"],\n leaveParty: ["Leave party", "Lascia gruppo", "Salir del grupo", "Quitter le groupe", "Gruppe verlassen", "Sair do grupo"],\n accept: ["Accept", "Accetta", "Aceptar", "Accepter", "Annehmen", "Aceitar"],\n decline: ["Decline", "Rifiuta", "Rechazar", "Refuser", "Ablehnen", "Recusar"],\n follow: ["Join them", "Raggiungi", "Unirse", "Rejoindre", "Beitreten", "Juntar-se"],\n voice: ["Voice", "Voce", "Voz", "Voix", "Sprache", "Voz"],\n voiceJoin: ["Join voice", "Entra in voce", "Unirse a voz", "Activer la voix", "Sprachchat beitreten", "Entrar na voz"],\n voiceLeave: ["Leave voice", "Esci dalla voce", "Salir de voz", "Quitter la voix", "Sprachchat verlassen", "Sair da voz"],\n voiceMute: ["Mute", "Disattiva microfono", "Silenciar", "Couper le micro", "Stummschalten", "Silenciar"],\n voiceUnmute: ["Unmute", "Attiva microfono", "Activar micr\\xF3fono", "Activer le micro", "Mikrofon aktivieren", "Ativar microfone"],\n voiceOff: ["Voice off", "Voce disattivata", "Voz desactivada", "Voix d\\xE9sactiv\\xE9e", "Sprachchat aus", "Voz desativada"],\n voiceJoining: ["Joining voice...", "Connessione voce...", "Conectando voz...", "Connexion vocale...", "Sprachchat verbindet...", "Conectando voz..."],\n voiceOn: ["Voice connected", "Voce connessa", "Voz conectada", "Voix connect\\xE9e", "Sprachchat verbunden", "Voz conectada"],\n voiceMuted: ["Muted", "Microfono disattivato", "Silenciado", "Micro coup\\xE9", "Stumm", "Silenciado"],\n voiceMic: ["Mic on", "Microfono attivo", "Micr\\xF3fono activo", "Micro actif", "Mikrofon an", "Microfone ativo"],\n voiceListening: ["Listening only", "Solo ascolto", "Solo escucha", "\\xC9coute seule", "Nur zuh\\xF6ren", "Somente ouvindo"],\n voiceSpeaking: ["Speaking", "Sta parlando", "Hablando", "Parle", "Spricht", "Falando"],\n voicePeers: ["Voice participants", "Partecipanti in voce", "Participantes de voz", "Participants vocaux", "Sprachteilnehmer", "Participantes de voz"],\n voiceEmpty: ["No one else in voice yet.", "Nessun altro in voce per ora.", "A\\xFAn no hay nadie m\\xE1s en voz.", "Personne d\\u2019autre en voix pour le moment.", "Noch niemand im Sprachchat.", "Ningu\\xE9m mais na voz ainda."],\n voiceVolume: ["Volume for {name}", "Volume di {name}", "Volumen de {name}", "Volume de {name}", "Lautst\\xE4rke f\\xFCr {name}", "Volume de {name}"],\n voiceUnavailable: ["Join a room with voice to use these controls.", "Entra in una stanza con voce per usare questi controlli.", "Entra en una sala con voz para usar estos controles.", "Rejoignez une salle vocale pour utiliser ces commandes.", "Diese Steuerung braucht einen Raum mit Sprachchat.", "Entre em uma sala com voz para usar estes controles."],\n voiceWatch: ["Voice is unavailable while watching.", "La voce non e\' disponibile in osservazione.", "La voz no est\\xE1 disponible al observar.", "La voix est indisponible en observation.", "Beim Zuschauen ist kein Sprachchat verf\\xFCgbar.", "A voz n\\xE3o est\\xE1 dispon\\xEDvel ao assistir."],\n voiceDenied: ["Microphone permission denied. Allow it in your browser, then try again.", "Permesso microfono negato. Consenti l\'accesso nel browser e riprova.", "Permiso de micr\\xF3fono denegado. Act\\xEDvalo en el navegador e int\\xE9ntalo de nuevo.", "Acc\\xE8s au micro refus\\xE9. Autorisez-le dans le navigateur, puis r\\xE9essayez.", "Mikrofonzugriff verweigert. Im Browser erlauben und erneut versuchen.", "Permiss\\xE3o do microfone negada. Permita no navegador e tente novamente."],\n voiceUnsupported: ["Voice is not supported in this browser.", "Questo browser non supporta la voce.", "Este navegador no admite voz.", "Ce navigateur ne prend pas en charge la voix.", "Dieser Browser unterst\\xFCtzt keinen Sprachchat.", "Este navegador n\\xE3o oferece suporte a voz."],\n voiceFailed: ["Voice could not connect. Try again.", "Connessione voce non riuscita. Riprova.", "No se pudo conectar la voz. Int\\xE9ntalo de nuevo.", "Connexion vocale impossible. R\\xE9essayez.", "Sprachverbindung fehlgeschlagen. Erneut versuchen.", "N\\xE3o foi poss\\xEDvel conectar a voz. Tente novamente."],\n voicePeerGone: ["This participant has left voice.", "Questo partecipante e\' uscito dalla voce.", "Este participante sali\\xF3 de voz.", "Ce participant a quitt\\xE9 la voix.", "Diese Person hat den Sprachchat verlassen.", "Este participante saiu da voz."],\n shortcut: ["Shift+Tab shortcut", "Scorciatoia Shift+Tab", "Atajo Shift+Tab", "Raccourci Maj+Tab", "Umschalt+Tab-Kurzbefehl", "Atalho Shift+Tab"],\n menu: ["Caisual menu", "Menu Caisual", "Menu Caisual", "Menu Caisual", "Caisual-Menu", "Menu Caisual"],\n retry: ["Retry", "Riprova", "Reintentar", "R\\xE9essayer", "Erneut versuchen", "Tentar novamente"]\n};\nvar column = (index) => Object.fromEntries(Object.entries(words).map(([key, row]) => [key, row[index]]));\nvar dictionaries = { en: column(0), it: column(1), es: column(2), fr: column(3), de: column(4), pt: column(5) };\nfunction overlayLocale(raw) {\n const tag = normalizeLanguage(raw);\n return tag && languages.includes(tag.split("-")[0]) ? tag : "en";\n}\n\n// src/text.ts\nfunction createTextLoader(fetcher, language, pathname = "/") {\n let pending;\n const root = pathname.match(/^\\/rt\\/[^/]+\\/[1-9][0-9]*\\//)?.[0] ?? "/";\n return () => pending ?? (pending = (async () => {\n let dictionary = {};\n try {\n const response = await fetcher(`${root}__caisual/text/${encodeURIComponent(language)}.json`);\n if (response.ok) {\n const value = await response.json();\n if (isTextDictionary(value)) dictionary = value;\n }\n } catch {\n }\n return (key, values = {}) => {\n if (!Object.hasOwn(dictionary, key)) return key;\n const text = dictionary[key];\n return text.replace(/\\{([^{}]+)\\}/g, (placeholder, name) => Object.hasOwn(values, name) ? String(values[name]) : placeholder);\n };\n })());\n}\n\n// src/errors.ts\nfunction creaErrore(code, message, version = {}) {\n return Object.assign(new Error(message), { name: "CaisualError", code, ...version });\n}\nfunction erroreOffline() {\n return creaErrore("offline", "Caisual services are unavailable.");\n}\nfunction codiceErrore(valore) {\n return typeof valore === "object" && valore !== null && "code" in valore ? valore.code : null;\n}\n\n// src/session/resume.ts\nvar KEY = "caisual-session-v1";\nfunction resume(value) {\n const data = record(value);\n if (!data || typeof data.code !== "string" || !/^[ABCDEFGHJKLMNPQRSTUVWXYZ23456789]{6}$/.test(data.code) || !(data.mode === void 0 || data.mode === null || typeof data.mode === "string")) return null;\n return {\n version: 1,\n code: data.code,\n mode: typeof data.mode === "string" ? data.mode : null,\n updatedAt: typeof data.updatedAt === "number" && Number.isFinite(data.updatedAt) ? data.updatedAt : 0\n };\n}\nfunction createResume(save, changed) {\n let current = null, error = false, work = Promise.resolve();\n const write = async () => {\n const value = { version: 1, imported: true, resume: current };\n work = work.catch(() => void 0).then(async () => {\n try {\n await save.set(KEY, value);\n error = false;\n } catch (cause) {\n error = true;\n throw cause;\n } finally {\n changed();\n }\n });\n return work;\n };\n const loaded = (async () => {\n try {\n const data = record(await save.get(KEY));\n if (data?.version === 1 && data.imported === true) current = resume(data.resume);\n else {\n current = resume(await save.get("resume"));\n await write();\n }\n } catch {\n error = true;\n }\n changed();\n })();\n return {\n loaded,\n get value() {\n return current === null ? null : { ...current };\n },\n get error() {\n return error;\n },\n async set(value) {\n await loaded;\n current = value;\n changed();\n await write();\n }\n };\n}\n\n// src/session/index.ts\nfunction notify(listeners, value) {\n for (const listener of listeners) {\n try {\n listener(value);\n } catch {\n }\n }\n}\nfunction createSession(base, configuration = null, roomsAvailable = base.connected) {\n const standard = configuration?.manifest.overlay?.version === 1;\n const manifest = configuration?.manifest;\n let current = { kind: "idle" }, ready = false, operation = 0, identifier = 0;\n let pending = null, pendingMode = null;\n let waiting = null, controller = null;\n let stops = [], disposed = false, lastState = "";\n let view = { inputBlocked: standard, reservedRects: [], safeArea: { top: 0, right: 0, bottom: 0, left: 0 } };\n const listeners = /* @__PURE__ */ new Set();\n const viewListeners = /* @__PURE__ */ new Set();\n const stateListeners = /* @__PURE__ */ new Set();\n const openListeners = /* @__PURE__ */ new Set();\n const errorListeners = /* @__PURE__ */ new Set();\n const scoreListeners = /* @__PURE__ */ new Set();\n let resumeStore = null;\n const capabilities = () => ({\n local: true,\n rooms: roomsAvailable,\n overlay: standard,\n requestRole: current.kind === "room" && current.room.metadata.configuration?.requestRole === true\n });\n function voiceSnapshot() {\n if (current.kind !== "room" || !manifest || manifest.voice === "none") return null;\n const room = current.room, voice = room.voice;\n if (!voice || voice.mode === "none" || room.players.find((p) => p.id === room.you)?.role === "spectator") return null;\n return {\n mode: voice.mode,\n state: voice.state,\n mic: voice.mic,\n muted: voice.muted,\n speaking: voice.speaking,\n peers: voice.peers.map(({ id, mic, muted, speaking, volume }) => ({ id, mic, muted, speaking, volume }))\n };\n }\n function snapshot() {\n const attached = current.kind === "room" || current.kind === "watch" ? current.room : null;\n const configured = attached?.metadata.configuration;\n const result = attached ? readMatchResult(attached.result, attached.players.map((p) => p.id)) : null;\n const fallback = manifest && attached && (attached.mode === null || manifest.modes.some((m) => m.id === attached.mode)) ? risolviModalita(manifest, attached.mode) : { players: { min: 1, max: 1 }, lobby: false };\n return {\n kind: pending ?? (current.kind === "idle" ? ready ? "home" : "boot" : current.kind),\n id: current.kind === "idle" ? null : current.id,\n mode: pending ? pendingMode : current.kind === "local" ? current.mode : attached?.mode ?? null,\n localStatus: current.kind === "local" ? current.status : null,\n ready,\n capabilities: capabilities(),\n room: attached ? {\n ...attached.metadata.rematch?.keepSetup || attached.metadata.rematch?.autoStart ? { rematch: attached.metadata.rematch } : {},\n code: attached.code,\n mode: attached.mode,\n status: attached.status,\n host: attached.host,\n you: current.kind === "room" ? current.room.you : null,\n players: attached.players.map((p) => ({ id: p.id, name: p.name, guest: p.guest, role: p.role, team: p.team, ready: p.ready, connected: p.connected })),\n ...result ? { result } : {},\n countdownAt: attached.countdownAt,\n connection: attached.connection,\n closedCode: attached.metadata.closedCode,\n limits: { ...configured?.players ?? fallback.players },\n lobby: configured?.lobby ?? fallback.lobby,\n persistent: configured?.persistent ?? manifest?.persistent ?? false,\n delayMs: current.kind === "watch" ? current.room.delayMs : null,\n requestRole: configured?.requestRole ?? false\n } : null,\n voice: pending ? null : voiceSnapshot(),\n waiting: waiting ? { ...waiting } : null,\n resume: resumeStore?.value ?? null,\n resumeError: resumeStore?.error ?? false\n };\n }\n function emit() {\n if (disposed) return;\n const state = snapshot(), serialized = JSON.stringify(state);\n if (serialized === lastState) return;\n lastState = serialized;\n notify(stateListeners, state);\n }\n function changed() {\n notify(listeners, { ...current });\n emit();\n }\n function active() {\n if (current.kind !== "room") throw creaErrore("no_room", "There is no active player room.");\n return current.room;\n }\n function activeVoice() {\n const room = active();\n if (room.players.find((p) => p.id === room.you)?.role === "spectator") throw creaErrore("spectator", "Spectators cannot use voice controls.");\n if (!manifest || manifest.voice === "none" || room.voice.mode === "none") throw creaErrore("voice_disabled", "Voice is disabled for this room.");\n return room.voice;\n }\n function cancel() {\n operation++;\n controller?.abort();\n controller = null;\n pending = null;\n waiting = null;\n emit();\n }\n function detach(preserve) {\n stops.splice(0).forEach((stop) => stop());\n if (current.kind === "room" || current.kind === "watch") {\n if (preserve) current.room.disconnect();\n else current.room.leave();\n }\n current = { kind: "idle" };\n changed();\n }\n async function clearResume(code) {\n if (resumeStore?.value?.code === code) await resumeStore.set(null).catch(() => void 0);\n }\n async function adopt(next, watch, token) {\n if (token !== operation || disposed) {\n next.leave();\n throw creaErrore("cancelled", "The operation was cancelled.");\n }\n detach(false);\n current = watch ? { kind: "watch", room: next, id: String(++identifier) } : { kind: "room", room: next, id: String(++identifier) };\n const room = next;\n stops = [room.onPlayers(emit), room.onMetadata(() => {\n if (room.connection === "disconnected" && (current.kind === "room" || current.kind === "watch") && current.room === room) {\n stops.splice(0).forEach((stop) => stop());\n if (!watch && room.metadata.closedCode === 1e3) void clearResume(room.code);\n current = { kind: "idle" };\n changed();\n } else emit();\n }), room.onStatus(() => {\n emit();\n if (!watch && room.connection === "ended") void clearResume(room.code);\n })];\n if (!watch) {\n const playerRoom = next;\n const sessionId = current.id;\n if (playerRoom.voice) stops.push(playerRoom.voice.onState(emit), playerRoom.voice.onPeers(emit));\n stops.push(playerRoom.onError((error) => notify(errorListeners, { sessionId, error: { ...error } })));\n stops.push(playerRoom.onScoreQueued((score) => notify(scoreListeners, { ...score })));\n for (const score of playerRoom.queuedScores) notify(scoreListeners, { ...score });\n }\n pending = null;\n waiting = null;\n changed();\n if (!watch && resumeStore && room.connection !== "ended") {\n await resumeStore.set({ version: 1, code: room.code, mode: room.mode, updatedAt: base.time.now() }).catch(() => void 0);\n }\n return next;\n }\n async function run(kind, mode, work, watch = false) {\n cancel();\n const token = operation;\n controller = new AbortController();\n pending = kind;\n pendingMode = mode;\n emit();\n try {\n const next = await work(controller.signal, token);\n await adopt(next, watch, token);\n if (token !== operation || disposed) throw creaErrore("cancelled", "The operation was cancelled.");\n return next;\n } finally {\n if (token === operation) {\n pending = null;\n waiting = null;\n controller = null;\n emit();\n }\n }\n }\n const direct = base.room;\n const stopVersionErrors = direct.onError((error) => {\n if (!standard || disposed) return;\n if (error.code === "version_mismatch") direct.reload();\n else if (error.code === "version_outdated") notify(errorListeners, { sessionId: current.kind === "idle" ? null : current.id, error });\n });\n const rooms = !standard ? direct : {\n invited: direct.invited,\n reload: () => direct.reload(),\n onError: (listener) => direct.onError(listener),\n create(options) {\n if (manifest && modalitaLocale(manifest, options.mode)) return Promise.reject(creaErrore("invalid_request", "Local modes cannot create rooms."));\n return run("attaching", options.mode, () => direct.create(options));\n },\n join(code) {\n return run("attaching", null, () => direct.join(code));\n },\n watch(code) {\n return run("attaching", null, () => direct.watch(code), true);\n },\n match(options) {\n if (manifest && modalitaLocale(manifest, options.mode)) return Promise.reject(creaErrore("invalid_request", "Local modes cannot use matchmaking."));\n return run("matching", options.mode, (signal, token) => {\n const abort = () => {\n if (operation === token) cancel();\n };\n options.signal?.addEventListener("abort", abort, { once: true });\n if (options.signal?.aborted) abort();\n return direct.match({ ...options, signal, onWaiting(value) {\n if (token !== operation) return;\n waiting = { ...value };\n emit();\n options.onWaiting?.(value);\n } }).finally(() => options.signal?.removeEventListener("abort", abort));\n });\n }\n };\n if (standard) resumeStore = createResume(base.save, emit);\n const session = {\n get current() {\n return { ...current };\n },\n get capabilities() {\n return capabilities();\n },\n onChange(listener) {\n listeners.add(listener);\n notify(/* @__PURE__ */ new Set([listener]), { ...current });\n return () => {\n listeners.delete(listener);\n };\n },\n ready() {\n if (disposed || ready) return;\n ready = true;\n emit();\n },\n finish() {\n if (current.kind === "room" || current.kind === "watch") throw creaErrore("not_local", "Only a local session can be finished by the client.");\n if (current.kind === "local") {\n current = { ...current, status: "ended" };\n changed();\n }\n }\n };\n const overlay = {\n open(panel) {\n if (!["home", "room", "invite", "friends", "voice", "boards"].includes(panel)) throw creaErrore("invalid_request", "Unknown overlay panel.");\n if (standard) notify(openListeners, panel);\n },\n onChange(listener) {\n viewListeners.add(listener);\n notify(/* @__PURE__ */ new Set([listener]), structuredClone(view));\n return () => {\n viewListeners.delete(listener);\n };\n }\n };\n return {\n session,\n overlay,\n rooms,\n snapshot,\n serverTime: () => current.kind === "room" || current.kind === "watch" ? current.room.serverTime() : base.time.now(),\n onState(listener) {\n stateListeners.add(listener);\n listener(snapshot());\n return () => {\n stateListeners.delete(listener);\n };\n },\n onOpen(listener) {\n openListeners.add(listener);\n return () => {\n openListeners.delete(listener);\n };\n },\n onError(listener) {\n errorListeners.add(listener);\n return () => {\n errorListeners.delete(listener);\n };\n },\n onScore(listener) {\n scoreListeners.add(listener);\n return () => {\n scoreListeners.delete(listener);\n };\n },\n async execute(request) {\n if (!standard) throw creaErrore("overlay_disabled", "This game uses its own room flow.");\n if (request.op === "overlay.view") {\n if (!validOverlayView(request.args)) throw creaErrore("invalid_request", "The overlay geometry is invalid.");\n view = { ...structuredClone(request.args), safeArea: { top: 0, right: 0, bottom: 0, left: 0, ...request.args.safeArea } };\n if (typeof document !== "undefined") for (const [side, value] of Object.entries(view.safeArea)) {\n document.documentElement.style.setProperty(`--caisual-safe-${side}`, `${value}px`);\n }\n notify(viewListeners, structuredClone(view));\n return;\n }\n if (request.sessionId !== void 0 && request.sessionId !== (current.kind === "idle" ? null : current.id)) throw creaErrore("session_replaced", "The active session changed.");\n if (request.op.startsWith("voice.") && request.sessionId !== (current.kind === "idle" ? null : current.id)) throw creaErrore("session_replaced", "The active session changed.");\n if (!ready) throw creaErrore("game_not_ready", "The game is still loading.");\n switch (request.op) {\n case "local.start": {\n if (!manifest || !modalitaLocale(manifest, request.args.mode)) throw creaErrore("invalid_mode", "This is not a local mode.");\n cancel();\n const token = operation;\n if (current.kind === "room") await clearResume(current.room.code);\n if (token !== operation || disposed) throw creaErrore("cancelled", "The operation was cancelled.");\n detach(false);\n current = { kind: "local", id: String(++identifier), mode: request.args.mode, status: "playing" };\n changed();\n return;\n }\n case "room.create":\n await rooms.create(request.args);\n return;\n case "room.join":\n await rooms.join(request.args.code);\n return;\n case "room.watch":\n await rooms.watch(request.args.code);\n return;\n case "room.match": {\n const mode = manifest?.modes.find((m) => m.id === request.args.mode);\n const key = request.args.key ?? mode?.matchmaking?.defaults;\n if (!key) throw creaErrore("invalid_request", "Matchmaking needs a complete key.");\n await rooms.match({ mode: request.args.mode, key });\n return;\n }\n case "voice.join": {\n const room = active(), voice = activeVoice();\n await voice.join();\n if (current.kind !== "room" || current.room !== room) throw creaErrore("session_replaced", "The active session changed.");\n emit();\n return;\n }\n case "voice.mute":\n activeVoice().mute(request.args.muted);\n emit();\n return;\n case "voice.leave":\n activeVoice().leave();\n emit();\n return;\n case "voice.setVolume": {\n const voice = activeVoice();\n if (!voice.peers.some((peer) => peer.id === request.args.playerId)) throw creaErrore("voice_peer_missing", "This voice participant is no longer available.");\n voice.setVolume(request.args.playerId, request.args.volume);\n emit();\n return;\n }\n case "room.ready":\n active().ready(request.args.ready);\n return;\n case "room.role":\n active().setRole(request.args.role);\n return;\n case "room.requestRole":\n await active().requestRole(request.args.role);\n return;\n case "room.team":\n active().setTeam(request.args.team);\n return;\n case "room.start":\n active().start();\n return;\n case "room.restart":\n active().restart();\n return;\n case "session.cancel":\n cancel();\n return;\n case "session.resume": {\n await run("attaching", null, async (signal) => {\n await resumeStore?.loaded;\n if (signal.aborted) throw creaErrore("cancelled", "The operation was cancelled.");\n if (!resumeStore?.value) throw creaErrore("no_resume", "There is no saved room.");\n return direct.join(resumeStore.value.code);\n });\n return;\n }\n case "session.disconnect": {\n cancel();\n const token = operation;\n if (current.kind === "room" && current.room.connection !== "ended" && resumeStore) await resumeStore.set({ version: 1, code: current.room.code, mode: current.room.mode, updatedAt: base.time.now() });\n if (token !== operation || disposed) throw creaErrore("cancelled", "The operation was cancelled.");\n detach(true);\n return;\n }\n case "session.leave": {\n cancel();\n const token = operation;\n if (current.kind === "room") await clearResume(current.room.code);\n if (token !== operation || disposed) throw creaErrore("cancelled", "The operation was cancelled.");\n detach(false);\n return;\n }\n }\n },\n dispose() {\n stopVersionErrors();\n cancel();\n detach(true);\n disposed = true;\n listeners.clear();\n viewListeners.clear();\n stateListeners.clear();\n openListeners.clear();\n scoreListeners.clear();\n errorListeners.clear();\n }\n };\n}\n\n// src/overlay/shortcut.ts\nfunction bindOverlayShortcut(target, overlay, open) {\n let enabled = true, blocked = false;\n const stop = overlay.onChange((view) => {\n enabled = view.shortcutEnabled !== false;\n blocked = view.inputBlocked;\n });\n const listener = (event) => {\n const element = event.target;\n if (!enabled || blocked || event.repeat || event.key !== "Tab" || !event.shiftKey || event.ctrlKey || event.altKey || event.metaKey || element?.closest?.(\'input,textarea,select,[contenteditable="true"]\')) return;\n event.preventDefault();\n event.stopImmediatePropagation();\n open();\n };\n target.addEventListener("keydown", listener, true);\n return () => {\n stop();\n target.removeEventListener("keydown", listener, true);\n };\n}\n\n// src/overlay/bridge.ts\nfunction attachKitBridge(port, hello, coordinator) {\n let disposed = false, seq = 0, highestRequest = 0, activeRequests = 0;\n const replies = /* @__PURE__ */ new Map();\n const send = (message) => {\n if (!disposed) try {\n port.postMessage(message);\n } catch {\n }\n };\n const stops = [\n ...hello.configuration.manifest.overlay && typeof window !== "undefined" ? [bindOverlayShortcut(window, coordinator.overlay, () => send({ type: "caisual:overlay-shortcut", v: 1, epoch: hello.epoch }))] : [],\n coordinator.onState((state) => send({ type: "caisual:overlay-state", v: 1, epoch: hello.epoch, seq: ++seq, serverTime: coordinator.serverTime(), state })),\n coordinator.onOpen((panel) => send({ type: "caisual:overlay-open", v: 1, epoch: hello.epoch, panel })),\n coordinator.onError(({ sessionId, error }) => send({ type: "caisual:overlay-error", v: 1, epoch: hello.epoch, sessionId, error })),\n coordinator.onScore((score) => send({ type: "caisual:overlay-score", v: 1, epoch: hello.epoch, score }))\n ];\n const listener = (event) => {\n const raw = record(event.data);\n if (raw?.type !== "caisual:overlay" || raw.epoch !== hello.epoch || disposed) return;\n const reply = { type: "caisual:overlay-response", v: 1, epoch: hello.epoch, requestId: typeof raw.requestId === "string" ? raw.requestId : "" };\n if (!validOverlayRequest(raw)) {\n send({ ...reply, ok: false, error: { code: "invalid_request", message: "The overlay request is invalid." } });\n return;\n }\n const fingerprint = JSON.stringify([raw.op, raw.args, raw.sessionId]);\n const previous = replies.get(raw.requestId);\n if (previous) {\n if (previous.fingerprint !== fingerprint) send({ ...reply, ok: false, error: { code: "duplicate_request", message: "The request id was already used." } });\n else void previous.response.then(send);\n return;\n }\n if (Number(raw.requestId) <= highestRequest || activeRequests >= 32) {\n send({ ...reply, ok: false, error: { code: "stale_request", message: "The request is stale or too many requests are pending." } });\n return;\n }\n highestRequest = Number(raw.requestId);\n activeRequests++;\n const response = Promise.resolve().then(() => coordinator.execute(raw)).then(\n () => ({ ...reply, ok: true }),\n (error) => ({ ...reply, ok: false, error: {\n code: typeof record(error)?.code === "string" ? record(error).code : "internal_error",\n message: error instanceof Error ? error.message : "The operation could not be completed."\n } })\n );\n replies.set(raw.requestId, { fingerprint, response });\n void response.then((value) => {\n activeRequests--;\n send(value);\n if (replies.size > 64) for (const id of replies.keys()) {\n if (Number(id) < highestRequest - 64) replies.delete(id);\n }\n });\n };\n port.addEventListener("message", listener);\n port.start();\n return () => {\n disposed = true;\n port.removeEventListener("message", listener);\n stops.forEach((stop) => stop());\n coordinator.dispose();\n replies.clear();\n };\n}\n\n// src/http.ts\nasync function leggiErrore(response) {\n let corpo = {};\n try {\n corpo = await response.json();\n } catch {\n }\n return creaErrore(\n typeof corpo.error?.code === "string" ? corpo.error.code : response.status === 401 ? "invalid_ticket" : "internal_error",\n typeof corpo.error?.message === "string" ? corpo.error.message : `The request failed with status ${response.status}.`,\n { currentVersion: corpo.error?.currentVersion, roomVersion: corpo.error?.roomVersion }\n );\n}\nfunction creaRichiedente(origin, prefix, fetcher, biglietto) {\n async function manda(path, metodo, ticket, corpo) {\n const headers = new Headers({ Authorization: `Bearer ${ticket}` });\n let body;\n if (corpo !== void 0) {\n headers.set("Content-Type", "application/json");\n try {\n body = JSON.stringify(corpo);\n } catch {\n throw creaErrore("invalid_request", "The value must be valid JSON.");\n }\n }\n try {\n return await fetcher(new URL(prefix + path, origin), {\n method: metodo,\n headers,\n body,\n credentials: "omit"\n });\n } catch {\n throw erroreOffline();\n }\n }\n return async function richiesta(path, metodo, corpo, forzaRinnovo = false) {\n let ticket;\n try {\n ticket = forzaRinnovo ? await biglietto.rinnova() : await biglietto.ottieni();\n } catch {\n throw erroreOffline();\n }\n let response = await manda(path, metodo, ticket, corpo);\n if (response.status === 401) {\n try {\n ticket = await biglietto.rinnova();\n } catch {\n throw erroreOffline();\n }\n response = await manda(path, metodo, ticket, corpo);\n }\n if (!response.ok) throw await leggiErrore(response);\n try {\n return await response.json();\n } catch {\n throw creaErrore("internal_error", "The service returned an invalid response.");\n }\n };\n}\n\n// src/api.ts\nfunction creaClienteApi(appOrigin, fetcher, biglietto) {\n const richiesta = creaRichiedente(appOrigin, "/api/kit", fetcher, biglietto);\n return {\n me: () => richiesta("/me", "GET"),\n saveSet: (key, value) => richiesta(`/saves/${encodeURIComponent(key)}`, "PUT", { value }),\n async saveGet(key) {\n try {\n return (await richiesta(`/saves/${encodeURIComponent(key)}`, "GET")).value;\n } catch (errore) {\n if (codiceErrore(errore) === "not_found") return null;\n throw errore;\n }\n },\n async saveRemove(key) {\n await richiesta(`/saves/${encodeURIComponent(key)}`, "DELETE");\n },\n async saveList() {\n return (await richiesta("/saves", "GET")).saves;\n },\n async boardSubmit(board, score, daily) {\n const risultato = await richiesta("/scores", "POST", { board, score, daily });\n return {\n accepted: true,\n best: risultato.best,\n rank: risultato.rank,\n day: risultato.day,\n verified: risultato.verified\n };\n },\n async boardTop(board, opzioni) {\n if (opzioni.day !== void 0 && (!validBoardDay(opzioni.day) || opzioni.daily === false)) throw creaErrore("invalid_request", "day must be a real UTC date and cannot be combined with daily: false.");\n const query = new URLSearchParams();\n if (opzioni.day !== void 0) query.set("day", opzioni.day);\n if (opzioni.daily) query.set("daily", "1");\n if (opzioni.limit !== void 0) query.set("limit", String(opzioni.limit));\n if (opzioni.guests) query.set("guests", "1");\n const suffisso = query.size === 0 ? "" : `?${query.toString()}`;\n const { day, entries, me } = await richiesta(\n `/scores/${encodeURIComponent(board)}${suffisso}`,\n "GET"\n );\n return { day, entries, me };\n }\n };\n}\n\n// src/daily.ts\nvar DIVISORE_UINT32 = 4294967296;\nfunction prossimaMezzanotteUtc(ora) {\n return (Math.floor(ora / 864e5) + 1) * 864e5;\n}\nfunction creaDaily(initial, now, refresh) {\n const listeners = /* @__PURE__ */ new Set();\n let current = { ...initial }, timer, pending = false;\n function schedule() {\n if (!listeners.size || timer !== void 0 || pending) return;\n timer = setTimeout(check, Math.max(0, Math.min(2147483647, current.expiresAt - now())));\n timer.unref?.();\n }\n async function check() {\n timer = void 0;\n pending = true;\n try {\n const next = await refresh(), changed = next.day !== current.day;\n current = { ...next };\n if (changed) for (const listener of [...listeners]) {\n try {\n listener({ ...next });\n } catch {\n }\n }\n } catch {\n } finally {\n pending = false;\n if (current.expiresAt <= now()) current.expiresAt = now() + 3e4;\n schedule();\n }\n }\n return {\n ...initial,\n random: creaMulberry32(initial.seed),\n rng: () => creaMulberry32(initial.seed),\n onChange(listener) {\n listeners.add(listener);\n schedule();\n return () => {\n listeners.delete(listener);\n if (!listeners.size && timer !== void 0) {\n clearTimeout(timer);\n timer = void 0;\n }\n };\n }\n };\n}\nfunction giornoUtc(ora) {\n return new Date(ora).toISOString().slice(0, 10);\n}\nasync function calcolaSeed(gioco, giorno, subtle) {\n const dati = new TextEncoder().encode(`caisual:${gioco}:${giorno}`);\n const digest = new Uint8Array(await subtle.digest("SHA-256", dati));\n return (digest[0] ?? 0) * 16777216 + ((digest[1] ?? 0) << 16) + ((digest[2] ?? 0) << 8) + (digest[3] ?? 0) >>> 0;\n}\nfunction creaMulberry32(seed) {\n let stato = seed >>> 0;\n return () => {\n stato = stato + 1831565813 >>> 0;\n let valore = stato;\n valore = Math.imul(valore ^ valore >>> 15, valore | 1);\n valore ^= valore + Math.imul(valore ^ valore >>> 7, valore | 61);\n return ((valore ^ valore >>> 14) >>> 0) / DIVISORE_UINT32;\n };\n}\n\n// src/handshake.ts\nfunction record2(valore) {\n return typeof valore === "object" && valore !== null && !Array.isArray(valore) ? valore : null;\n}\nfunction eTipo(valore, tipo) {\n return record2(valore)?.type === tipo;\n}\nfunction leggiOrigine(valore) {\n if (typeof valore !== "string") return null;\n try {\n const url = new URL(valore);\n return url.origin === valore && (url.protocol === "https:" || url.protocol === "http:") ? valore : null;\n } catch {\n return null;\n }\n}\nfunction attendiHandshake(finestra, appOrigin, timeoutMs = 3e3) {\n return new Promise((resolve) => {\n let concluso = false;\n const instance = globalThis.crypto.randomUUID();\n const termina = (esito) => {\n if (concluso) return;\n concluso = true;\n finestra.removeEventListener("message", ascolta);\n finestra.clearTimeout(scadenza);\n resolve(esito);\n };\n const segnalaPronto = () => {\n finestra.parent.postMessage({ type: "caisual:ready", instance, overlayVersion: 1 }, appOrigin);\n };\n const ascolta = (evento) => {\n if (evento.origin !== appOrigin || evento.source !== finestra.parent) return;\n if (eTipo(evento.data, "caisual:ready?")) {\n segnalaPronto();\n return;\n }\n if (!eTipo(evento.data, "caisual:hello")) return;\n const dati = record2(evento.data);\n const porta = evento.ports[0];\n if (typeof dati?.ticket !== "string" || !numeroVersione(dati.n) || porta === void 0) return;\n porta.start();\n const overlay = normalizeOverlayHello(dati.overlay);\n const tags = (value) => Array.isArray(value) ? value.map(normalizeLanguage).filter((tag) => tag !== null) : void 0;\n termina({\n ...overlay ? { overlay } : {},\n ...normalizeLanguage(dati.language) ? { language: normalizeLanguage(dati.language) } : {},\n uiLanguage: normalizeLanguage(dati.uiLanguage) ?? void 0,\n languagePreferences: tags(dati.languagePreferences),\n gameLanguages: tags(dati.gameLanguages),\n ticket: dati.ticket,\n n: dati.n,\n live: leggiOrigine(dati.live),\n invite: typeof dati.invite === "string" ? dati.invite : null,\n porta\n });\n };\n finestra.addEventListener("message", ascolta);\n const scadenza = finestra.setTimeout(() => termina(null), timeoutMs);\n segnalaPronto();\n });\n}\nfunction scadenzaJwt(ticket) {\n const parte = ticket.split(".")[1];\n if (parte === void 0) return null;\n const base64 = parte.replace(/-/g, "+").replace(/_/g, "/").padEnd(\n Math.ceil(parte.length / 4) * 4,\n "="\n );\n try {\n const payload = record2(JSON.parse(globalThis.atob(base64)));\n return typeof payload?.exp === "number" && Number.isFinite(payload.exp) ? payload.exp * 1e3 : null;\n } catch {\n return null;\n }\n}\nfunction chiediBiglietto(porta, finestra, timeoutMs, aud) {\n return new Promise((resolve, reject) => {\n let concluso = false;\n const termina = (ticket) => {\n if (concluso) return;\n concluso = true;\n porta.removeEventListener("message", ascolta);\n finestra.clearTimeout(scadenza);\n if (ticket === null) reject(new Error("Ticket refresh timed out."));\n else resolve(ticket);\n };\n const ascolta = (evento) => {\n const dati = record2(evento.data);\n const destinatario = dati?.aud === void 0 ? "portal" : dati.aud;\n if (dati?.type === "caisual:ticket" && destinatario === aud && typeof dati.ticket === "string") {\n termina(dati.ticket);\n }\n };\n porta.addEventListener("message", ascolta);\n const scadenza = finestra.setTimeout(() => termina(null), timeoutMs);\n try {\n porta.postMessage(aud === "live" ? { type: "caisual:ticket", aud: "live" } : { type: "caisual:ticket" });\n } catch {\n termina(null);\n }\n });\n}\nfunction creaGestoreBiglietto(ticketIniziale, porta, finestra, ora, timeoutMs = 3e3, aud = "portal") {\n let ticket = ticketIniziale;\n let rinnovo = null;\n const rinnova = () => {\n if (rinnovo !== null) return rinnovo;\n const richiesta = chiediBiglietto(porta, finestra, timeoutMs, aud).then((nuovo) => {\n ticket = nuovo;\n return nuovo;\n });\n const completa = richiesta.finally(() => {\n if (rinnovo === completa) rinnovo = null;\n });\n rinnovo = completa;\n return completa;\n };\n return {\n async ottieni() {\n if (ticket === null) return rinnova();\n const scadenza = scadenzaJwt(ticket);\n return scadenza !== null && scadenza - ora() < 3e4 ? rinnova() : ticket;\n },\n rinnova\n };\n}\n\n// src/voce/index.ts\nvar SOGLIA_AUDIO = 0.02;\nvar DURATA_PARLANTE = 300;\nvar INTERVALLO_AUDIO = 200;\nvar DURATA_ZERO = 3e3;\nvar TIMEOUT_CONNESSIONE = 1e4;\nvar RITARDI_RICONNESSIONE = [1e3, 2e3, 4e3];\nfunction limita(value) {\n return Number.isNaN(value) ? 1 : Math.min(1, Math.max(0, value));\n}\nfunction dipendenzeReali(input) {\n const globali = globalThis;\n const AudioContextClass = globali.AudioContext ?? globali.webkitAudioContext;\n if (typeof RTCPeerConnection === "undefined" || typeof MediaStream === "undefined" || AudioContextClass === void 0 || typeof navigator === "undefined" || navigator.mediaDevices?.getUserMedia === void 0 || typeof document === "undefined") return null;\n return {\n ...input,\n creaPeerConnection: (configuration) => new RTCPeerConnection(configuration),\n getUserMedia: (constraints) => navigator.mediaDevices.getUserMedia(constraints),\n creaAudioContext: () => new AudioContextClass(),\n creaAudioElement: () => document.createElement("audio"),\n creaMediaStream: (tracks) => new MediaStream(tracks)\n };\n}\nvar VoceClient = class {\n constructor(contesto, timer, dipendenze) {\n this.contesto = contesto;\n this.modeCorrente = "none";\n this.stateCorrente = "off";\n this.mutedCorrente = false;\n this.speakingCorrente = false;\n this.roster = [];\n this.gains = /* @__PURE__ */ new Map();\n this.volumi = /* @__PURE__ */ new Map();\n this.speakingPeers = /* @__PURE__ */ new Map();\n this.ultimoAudio = /* @__PURE__ */ new Map();\n this.zeroDa = /* @__PURE__ */ new Map();\n this.timerZero = /* @__PURE__ */ new Map();\n this.ascoltatoriPeers = /* @__PURE__ */ new Set();\n this.ascoltatoriState = /* @__PURE__ */ new Set();\n this.richieste = /* @__PURE__ */ new Map();\n this.riproduzioni = /* @__PURE__ */ new Map();\n this.sfuAttive = /* @__PURE__ */ new Map();\n this.midGiocatori = /* @__PURE__ */ new Map();\n this.negati = /* @__PURE__ */ new Set();\n this.mesh = /* @__PURE__ */ new Map();\n this.stream = null;\n this.tracciaMic = null;\n this.audioContext = null;\n this.analyser = null;\n this.peerSfu = null;\n this.sessioneSfu = null;\n this.connessioneSfuAttesa = false;\n this.trasporto = null;\n this.intervalloAudio = null;\n this.timerConnessione = null;\n this.cancellaAttesaConnessione = null;\n this.timerRiconnessione = null;\n this.ultimoAudioMic = Number.NEGATIVE_INFINITY;\n this.sequenzaRichieste = 0;\n this.generazione = 0;\n this.tentativoRiconnessione = 0;\n this.desiderata = false;\n this.micDesiderato = true;\n this.promessaIngresso = null;\n this.negoziazione = Promise.resolve();\n this.dipendenze = dipendenze ?? dipendenzeReali(timer);\n }\n get mode() {\n return this.modeCorrente;\n }\n get state() {\n return this.stateCorrente;\n }\n get mic() {\n return this.stateCorrente === "on" && this.tracciaMic !== null;\n }\n get muted() {\n return this.mutedCorrente;\n }\n get speaking() {\n return this.speakingCorrente;\n }\n get peers() {\n return this.copiaPeers();\n }\n async join(options = {}) {\n if (this.stateCorrente === "on") return;\n if (this.stateCorrente === "joining") {\n if (this.promessaIngresso !== null) await this.promessaIngresso;\n return;\n }\n if (this.stateCorrente === "reconnecting" && this.desiderata) return;\n const mic = this.scegliMic(options);\n this.verificaIngresso(mic);\n this.micDesiderato = mic;\n this.desiderata = true;\n this.tentativoRiconnessione = 0;\n this.aggiornaState("joining");\n const generazione = ++this.generazione;\n const promessa = this.completaIngresso(generazione);\n this.promessaIngresso = promessa;\n try {\n await promessa;\n } finally {\n if (this.promessaIngresso === promessa) this.promessaIngresso = null;\n }\n }\n async completaIngresso(generazione) {\n try {\n await this.entra(generazione);\n } catch (cause) {\n if (generazione !== this.generazione) return;\n this.desiderata = false;\n this.chiudiRisorse();\n this.aggiornaState("off");\n throw this.mappaErrore(cause);\n }\n }\n leave() {\n const deveFermare = this.desiderata || this.stateCorrente !== "off";\n this.desiderata = false;\n this.generazione++;\n this.fermaRiconnessione();\n if (deveFermare && this.contesto.connessa()) {\n void this.richiedi({ t: "voice", op: "stop" }).catch(() => void 0);\n }\n this.rifiutaRichieste(creaErrore("offline", "Voice has stopped."));\n this.chiudiRisorse();\n this.aggiornaState("off");\n }\n mute(muted = true) {\n if (this.stateCorrente !== "on" || this.tracciaMic === null) {\n throw creaErrore("not_publishing", "Join voice before changing mute.");\n }\n this.mutedCorrente = muted;\n this.tracciaMic.enabled = !muted;\n if (muted) this.speakingCorrente = false;\n this.notificaPeers();\n void this.richiedi({ t: "voice", op: "mute", muted }).catch(() => void 0);\n }\n setVolume(playerId, volume) {\n const valore = limita(volume);\n this.volumi.set(playerId, valore);\n this.aggiornaGuadagno(playerId);\n this.notificaPeers();\n }\n onPeers(listener) {\n this.ascoltatoriPeers.add(listener);\n return () => {\n this.ascoltatoriPeers.delete(listener);\n };\n }\n onState(listener) {\n this.ascoltatoriState.add(listener);\n return () => {\n this.ascoltatoriState.delete(listener);\n };\n }\n ricevi(message) {\n if ("r" in message) {\n const pending = this.richieste.get(message.r);\n if (pending !== void 0) {\n this.richieste.delete(message.r);\n if ("error" in message) {\n pending.reject(creaErrore(message.error.code, message.error.message));\n } else pending.resolve(message);\n }\n return;\n }\n if (message.op === "roster") {\n this.negati.clear();\n this.modeCorrente = message.mode;\n const publisher = new Set(message.peers.map((peer) => peer.id));\n this.roster = [\n ...message.peers.map((peer) => ({ ...peer, mic: true })),\n ...message.listeners.flatMap((id) => publisher.has(id) ? [] : [{ id, mic: false, muted: true }])\n ];\n for (const peer of this.roster) {\n if (peer.muted) this.speakingPeers.set(peer.id, false);\n }\n this.pulisciPeerAssenti();\n this.contesto.rosterPronto();\n this.notificaPeers();\n this.accodaRiconciliazione();\n return;\n }\n if (message.op === "gain") {\n this.negati.clear();\n for (const [playerId, gain] of Object.entries(message.gains)) {\n this.gains.set(playerId, limita(gain));\n this.aggiornaZero(playerId);\n this.aggiornaGuadagno(playerId);\n }\n this.notificaPeers();\n this.accodaRiconciliazione();\n return;\n }\n if (message.op === "closed") {\n for (const mid of message.mids) {\n const playerId = this.midGiocatori.get(mid);\n if (playerId === void 0) continue;\n const attiva = this.sfuAttive.get(playerId);\n if (attiva?.mid === mid && !this.riproduzioni.has(playerId)) attiva.receiver?.track.stop();\n if (attiva?.mid === mid) this.sfuAttive.delete(playerId);\n this.midGiocatori.delete(mid);\n this.scollegaTraccia(playerId);\n this.negati.add(playerId);\n }\n this.notificaPeers();\n return;\n }\n if (message.op === "signal") void this.riceviSegnale(message.from, message.data);\n }\n giocatoriCambiati() {\n this.negati.clear();\n const presenti = new Set(this.contesto.giocatori().map((player) => player.id));\n for (const playerId of this.gains.keys()) {\n if (presenti.has(playerId)) continue;\n this.gains.delete(playerId);\n this.zeroDa.delete(playerId);\n const timer = this.timerZero.get(playerId);\n if (timer !== void 0) this.dipendenze?.clearTimeout(timer);\n this.timerZero.delete(playerId);\n this.aggiornaGuadagno(playerId);\n }\n this.notificaPeers();\n this.accodaRiconciliazione();\n }\n socketDisconnesso() {\n this.sequenzaRichieste = 0;\n this.rifiutaRichieste(creaErrore("offline", "The room is reconnecting."));\n if (!this.desiderata) return;\n this.generazione++;\n this.chiudiRisorse();\n this.tentativoRiconnessione = 0;\n this.aggiornaState("reconnecting");\n }\n socketRiconnesso() {\n this.sequenzaRichieste = 0;\n if (this.desiderata && this.stateCorrente === "reconnecting") this.programmaRiconnessione();\n }\n termina() {\n this.desiderata = false;\n this.generazione++;\n this.fermaRiconnessione();\n this.rifiutaRichieste(creaErrore("offline", "The room connection ended."));\n this.chiudiRisorse();\n this.aggiornaState("off");\n }\n scegliMic(options) {\n if (options.mic !== void 0) return options.mic;\n const you = this.contesto.giocatori().find((player) => player.id === this.contesto.you());\n return you?.role !== "spectator";\n }\n verificaIngresso(mic = this.micDesiderato) {\n if (!this.contesto.connessa()) throw creaErrore("offline", "The room is not connected.");\n if (this.modeCorrente === "none") {\n throw creaErrore("voice_disabled", "Voice is disabled for this room.");\n }\n const you = this.contesto.giocatori().find((player) => player.id === this.contesto.you());\n if (you?.role === "spectator" && mic) {\n throw creaErrore("spectator", "Spectators cannot publish voice.");\n }\n if (this.dipendenze === null) {\n throw creaErrore("unsupported", "Voice is not supported in this browser.");\n }\n }\n async entra(generazione) {\n this.verificaIngresso();\n const dipendenze = this.richiediDipendenze();\n const audioContext = dipendenze.creaAudioContext();\n this.audioContext = audioContext;\n if (this.micDesiderato) {\n let stream;\n try {\n stream = await dipendenze.getUserMedia({ audio: true });\n } catch (cause) {\n if (this.permessoNegato(cause)) {\n throw creaErrore("permission_denied", "Microphone permission was denied.");\n }\n throw creaErrore("voice_error", "The microphone could not be opened.");\n }\n try {\n this.controllaGenerazione(generazione);\n } catch (cause) {\n for (const track of stream.getTracks()) track.stop();\n throw cause;\n }\n const mic = stream.getAudioTracks()[0];\n if (mic === void 0) throw creaErrore("voice_error", "The microphone has no audio track.");\n this.stream = stream;\n this.tracciaMic = mic;\n mic.enabled = !this.mutedCorrente;\n this.preparaAnalizzatore(stream);\n }\n try {\n await audioContext.resume();\n } catch {\n }\n this.controllaGenerazione(generazione);\n const risposta = await this.richiedi({ t: "voice", op: "ice" });\n this.controllaGenerazione(generazione);\n if (risposta.op !== "ice") throw creaErrore("voice_error", "The voice service returned an invalid response.");\n this.modeCorrente = risposta.mode;\n if (risposta.mode === "none") throw creaErrore("voice_disabled", "Voice is disabled for this room.");\n this.trasporto = risposta.transport;\n if (risposta.transport === "sfu") {\n await this.entraSfu(risposta.iceServers, generazione);\n } else {\n await this.richiedi({ t: "voice", op: "publish", mic: this.micDesiderato });\n }\n if (this.micDesiderato && this.mutedCorrente) {\n await this.richiedi({ t: "voice", op: "mute", muted: true });\n }\n this.controllaGenerazione(generazione);\n this.tentativoRiconnessione = 0;\n this.aggiornaState("on");\n this.avviaMisuraAudio();\n for (const playerId of this.gains.keys()) this.aggiornaZero(playerId);\n this.accodaRiconciliazione();\n }\n async entraSfu(iceServers, generazione) {\n const pc = this.richiediDipendenze().creaPeerConnection({\n iceServers,\n bundlePolicy: "max-bundle"\n });\n this.peerSfu = pc;\n pc.ontrack = (event) => {\n const mid = event.transceiver.mid;\n const playerId = mid === null ? void 0 : this.midGiocatori.get(mid);\n if (playerId !== void 0) this.collegaTraccia(playerId, event.track, event.receiver);\n };\n this.osservaCaduta(pc);\n let risposta;\n if (this.micDesiderato) {\n const transceiver = pc.addTransceiver(this.richiediMic(), { direction: "sendonly" });\n const offer = await pc.createOffer();\n await pc.setLocalDescription(offer);\n this.controllaGenerazione(generazione);\n const mid = transceiver.mid;\n const sdp = pc.localDescription?.sdp;\n if (mid === null || sdp === void 0) {\n throw creaErrore("voice_error", "The voice connection could not create an offer.");\n }\n risposta = await this.richiedi({ t: "voice", op: "session", sdp, mid });\n } else {\n risposta = await this.richiedi({ t: "voice", op: "session" });\n }\n if (risposta.op !== "session") {\n throw creaErrore("voice_error", "The voice service returned an invalid response.");\n }\n this.sessioneSfu = risposta.session;\n if (this.micDesiderato) {\n if (risposta.sdp === null) {\n throw creaErrore("voice_error", "The voice service returned an invalid response.");\n }\n await pc.setRemoteDescription({ type: "answer", sdp: risposta.sdp });\n await this.attendiConnessione(pc, generazione);\n this.connessioneSfuAttesa = true;\n return;\n }\n if (risposta.sdp !== null) {\n throw creaErrore("voice_error", "The voice service returned an invalid response.");\n }\n if (this.publisherDesiderati().length > 0) {\n await this.riconciliaSfu();\n }\n }\n attendiConnessione(pc, generazione) {\n if (pc.connectionState === "connected") return Promise.resolve();\n const dipendenze = this.richiediDipendenze();\n return new Promise((resolve, reject) => {\n const pulisci = () => {\n pc.removeEventListener("connectionstatechange", cambiata);\n if (this.timerConnessione !== null) dipendenze.clearTimeout(this.timerConnessione);\n this.timerConnessione = null;\n this.cancellaAttesaConnessione = null;\n };\n const cambiata = () => {\n if (generazione !== this.generazione) {\n pulisci();\n reject(creaErrore("offline", "Voice was stopped."));\n } else if (pc.connectionState === "connected") {\n pulisci();\n resolve();\n } else if (pc.connectionState === "failed" || pc.connectionState === "closed") {\n pulisci();\n reject(creaErrore("voice_error", "The voice connection failed."));\n }\n };\n pc.addEventListener("connectionstatechange", cambiata);\n this.cancellaAttesaConnessione = () => {\n pulisci();\n reject(creaErrore("offline", "Voice was stopped."));\n };\n this.timerConnessione = dipendenze.setTimeout(() => {\n pulisci();\n reject(creaErrore("voice_error", "The voice connection timed out."));\n }, TIMEOUT_CONNESSIONE);\n });\n }\n accodaRiconciliazione() {\n if (this.stateCorrente !== "on") return;\n this.negoziazione = this.negoziazione.then(async () => {\n if (this.stateCorrente !== "on") return;\n if (this.trasporto === "sfu") await this.riconciliaSfu();\n else if (this.trasporto === "mesh") this.riconciliaMesh();\n }).catch(() => this.avviaRiconnessione());\n }\n async riconciliaSfu() {\n const sessione = this.sessioneSfu;\n const pc = this.peerSfu;\n if (sessione === null || pc === null) return;\n const desiderati = new Map(this.publisherDesiderati().map((peer) => [peer.id, peer]));\n const daChiudere = [];\n for (const [playerId, attiva] of this.sfuAttive) {\n const peer = desiderati.get(playerId);\n if (peer !== void 0 && peer.session === attiva.session && peer.track === attiva.track) continue;\n daChiudere.push(attiva);\n if (!this.riproduzioni.has(playerId)) attiva.receiver?.track.stop();\n this.sfuAttive.delete(playerId);\n this.midGiocatori.delete(attiva.mid);\n this.scollegaTraccia(playerId);\n }\n if (daChiudere.length > 0) {\n await this.richiedi({\n t: "voice",\n op: "close",\n session: sessione,\n mids: daChiudere.map((item) => item.mid)\n });\n }\n const nuove = [...desiderati.values()].filter((peer) => !this.sfuAttive.has(peer.id));\n if (nuove.length === 0) return;\n let risposta;\n try {\n risposta = await this.richiedi({\n t: "voice",\n op: "subscribe",\n session: sessione,\n tracks: nuove.map((peer) => ({ session: peer.session, track: peer.track }))\n });\n } catch (cause) {\n if (codiceErrore(cause) !== "not_allowed") throw cause;\n for (const peer of nuove) this.negati.add(peer.id);\n return;\n }\n if (risposta.op !== "subscribe") {\n throw creaErrore("voice_error", "The voice service returned an invalid response.");\n }\n for (const risultato of risposta.tracks) {\n const peer = nuove.find(\n (item) => item.session === risultato.session && item.track === risultato.track\n );\n if (risultato.error === "not_allowed" && peer !== void 0) this.negati.add(peer.id);\n if (risultato?.mid === null || risultato?.mid === void 0 || risultato.error !== null || peer === void 0) continue;\n this.midGiocatori.set(risultato.mid, peer.id);\n this.sfuAttive.set(peer.id, {\n session: peer.session,\n track: peer.track,\n mid: risultato.mid,\n receiver: null\n });\n }\n await pc.setRemoteDescription({ type: "offer", sdp: risposta.sdp });\n const answer = await pc.createAnswer();\n await pc.setLocalDescription(answer);\n const sdp = pc.localDescription?.sdp;\n if (sdp === void 0) throw creaErrore("voice_error", "The voice answer is missing.");\n await this.richiedi({ t: "voice", op: "answer", session: sessione, sdp });\n if (!this.connessioneSfuAttesa) {\n await this.attendiConnessione(pc, this.generazione);\n this.connessioneSfuAttesa = true;\n }\n }\n riconciliaMesh() {\n const desiderati = new Map(this.peerDesiderati().map((peer) => [peer.id, peer]));\n for (const [playerId, item] of this.mesh) {\n if (desiderati.has(playerId)) continue;\n item.pc.close();\n this.mesh.delete(playerId);\n this.scollegaTraccia(playerId);\n }\n for (const peer of desiderati.values()) {\n if (!this.mesh.has(peer.id)) this.creaMesh(peer);\n }\n }\n creaMesh(peer) {\n const playerId = peer.id;\n const pc = this.richiediDipendenze().creaPeerConnection();\n const item = {\n pc,\n makingOffer: false,\n ignoreOffer: false,\n settingRemoteAnswer: false,\n polite: this.contesto.you() > playerId,\n receiver: null\n };\n this.mesh.set(playerId, item);\n pc.onicecandidate = (event) => {\n if (event.candidate === null) return;\n void this.inviaSegnale(playerId, { kind: "candidate", candidate: event.candidate.toJSON() });\n };\n if (!item.polite) pc.onnegotiationneeded = () => {\n void this.offriMesh(playerId, item);\n };\n pc.ontrack = (event) => {\n item.receiver = event.receiver;\n this.collegaTraccia(playerId, event.track, event.receiver);\n };\n this.osservaCaduta(pc);\n if (this.micDesiderato) {\n pc.addTransceiver(this.richiediMic(), {\n direction: peer.mic ? "sendrecv" : "sendonly"\n });\n } else {\n pc.addTransceiver("audio", { direction: "recvonly" });\n }\n }\n async offriMesh(playerId, item) {\n try {\n item.makingOffer = true;\n const offer = await item.pc.createOffer();\n await item.pc.setLocalDescription(offer);\n const sdp = item.pc.localDescription?.sdp;\n if (sdp !== void 0) await this.inviaSegnale(playerId, { kind: "offer", sdp });\n } finally {\n item.makingOffer = false;\n }\n }\n async riceviSegnale(from, data) {\n if (this.trasporto !== "mesh" || this.stateCorrente !== "on") return;\n const peer = this.peerDesiderati().find((item2) => item2.id === from);\n if (peer === void 0) return;\n if (!this.mesh.has(from)) this.creaMesh(peer);\n const item = this.mesh.get(from);\n if (item === void 0 || typeof data !== "object" || data === null || Array.isArray(data)) return;\n const segnale = data;\n try {\n if (segnale.kind === "candidate") {\n if (!item.ignoreOffer) await item.pc.addIceCandidate(segnale.candidate);\n return;\n }\n if (segnale.kind !== "offer" && segnale.kind !== "answer" || typeof segnale.sdp !== "string") return;\n const pronta = !item.makingOffer && (item.pc.signalingState === "stable" || item.settingRemoteAnswer);\n const collisione = segnale.kind === "offer" && !pronta;\n item.ignoreOffer = !item.polite && collisione;\n if (item.ignoreOffer) return;\n item.settingRemoteAnswer = segnale.kind === "answer";\n await item.pc.setRemoteDescription({ type: segnale.kind, sdp: segnale.sdp });\n item.settingRemoteAnswer = false;\n if (segnale.kind === "offer") {\n const answer = await item.pc.createAnswer();\n await item.pc.setLocalDescription(answer);\n const sdp = item.pc.localDescription?.sdp;\n if (sdp !== void 0) await this.inviaSegnale(from, { kind: "answer", sdp });\n }\n } catch {\n this.avviaRiconnessione();\n }\n }\n async inviaSegnale(to, data) {\n try {\n await this.richiedi({ t: "voice", op: "signal", to, data });\n } catch (cause) {\n if (codiceErrore(cause) !== "not_allowed") throw cause;\n const item = this.mesh.get(to);\n item?.pc.close();\n this.mesh.delete(to);\n this.scollegaTraccia(to);\n this.negati.add(to);\n }\n }\n peerDesiderati() {\n const you = this.contesto.you();\n const giocatori = this.contesto.giocatori();\n const player = giocatori.find((item) => item.id === you);\n return this.roster.filter((peer) => {\n if (peer.id === you) return false;\n if (this.negati.has(peer.id)) return false;\n if (!this.micDesiderato && !peer.mic) return false;\n if (this.modeCorrente === "team") {\n const altro = giocatori.find((item) => item.id === peer.id);\n if (player?.role !== "spectator" && altro?.team !== player?.team) return false;\n }\n return true;\n });\n }\n publisherDesiderati() {\n return this.peerDesiderati().filter(\n (peer) => {\n if (!peer.mic) return false;\n const zeroAt = this.zeroDa.get(peer.id);\n return zeroAt === void 0 || this.richiediDipendenze().ora() - zeroAt < DURATA_ZERO;\n }\n );\n }\n aggiornaZero(playerId) {\n const dipendenze = this.dipendenze;\n if (dipendenze === null) return;\n const precedente = this.timerZero.get(playerId);\n if (precedente !== void 0) dipendenze.clearTimeout(precedente);\n this.timerZero.delete(playerId);\n if ((this.gains.get(playerId) ?? 1) > 0) {\n this.zeroDa.delete(playerId);\n return;\n }\n if (!this.zeroDa.has(playerId)) this.zeroDa.set(playerId, dipendenze.ora());\n const trascorso = dipendenze.ora() - (this.zeroDa.get(playerId) ?? dipendenze.ora());\n const timer = dipendenze.setTimeout(() => {\n this.timerZero.delete(playerId);\n this.accodaRiconciliazione();\n }, Math.max(0, DURATA_ZERO - trascorso));\n this.timerZero.set(playerId, timer);\n }\n collegaTraccia(playerId, track, receiver) {\n this.scollegaTraccia(playerId);\n const dipendenze = this.richiediDipendenze();\n const media = dipendenze.creaMediaStream([track]);\n const source = this.richiediAudioContext().createMediaStreamSource(media);\n const gain = this.richiediAudioContext().createGain();\n source.connect(gain);\n gain.connect(this.richiediAudioContext().destination);\n let analyser = null;\n try {\n analyser = this.richiediAudioContext().createAnalyser();\n analyser.fftSize = 256;\n source.connect(analyser);\n } catch {\n analyser = null;\n }\n const audio = dipendenze.creaAudioElement();\n audio.srcObject = media;\n audio.muted = true;\n audio.playsInline = true;\n void audio.play().catch(() => void 0);\n this.riproduzioni.set(playerId, { source, gain, analyser, audio, track, receiver });\n const attiva = this.sfuAttive.get(playerId);\n if (attiva !== void 0) attiva.receiver = receiver;\n this.aggiornaGuadagno(playerId);\n }\n scollegaTraccia(playerId) {\n const riproduzione = this.riproduzioni.get(playerId);\n if (riproduzione === void 0) return;\n riproduzione.source.disconnect();\n riproduzione.gain.disconnect();\n riproduzione.analyser?.disconnect();\n riproduzione.track.stop();\n riproduzione.audio.pause();\n riproduzione.audio.srcObject = null;\n this.riproduzioni.delete(playerId);\n this.speakingPeers.delete(playerId);\n this.ultimoAudio.delete(playerId);\n }\n aggiornaGuadagno(playerId) {\n const riproduzione = this.riproduzioni.get(playerId);\n if (riproduzione !== void 0) {\n riproduzione.gain.gain.value = (this.volumi.get(playerId) ?? 1) * (this.gains.get(playerId) ?? 1);\n }\n }\n preparaAnalizzatore(stream) {\n const context = this.richiediAudioContext();\n const analyser = context.createAnalyser();\n analyser.fftSize = 256;\n context.createMediaStreamSource(stream).connect(analyser);\n this.analyser = analyser;\n }\n avviaMisuraAudio() {\n const dipendenze = this.richiediDipendenze();\n if (this.intervalloAudio !== null) dipendenze.clearInterval(this.intervalloAudio);\n this.intervalloAudio = dipendenze.setInterval(() => this.misuraAudio(), INTERVALLO_AUDIO);\n }\n misuraAudio() {\n const dipendenze = this.dipendenze;\n if (dipendenze === null) return;\n let sopraSoglia = false;\n if (this.analyser !== null) sopraSoglia = this.livelloAnalizzatore(this.analyser) > SOGLIA_AUDIO;\n if (sopraSoglia) this.ultimoAudioMic = dipendenze.ora();\n const parlando = !this.mutedCorrente && dipendenze.ora() - this.ultimoAudioMic <= DURATA_PARLANTE;\n if (parlando !== this.speakingCorrente) {\n this.speakingCorrente = parlando;\n this.notificaPeers();\n }\n let cambiato = false;\n for (const peer of this.copiaPeers()) {\n const riproduzione = this.riproduzioni.get(peer.id);\n if (this.livelloAnalizzatore(riproduzione?.analyser ?? null) > SOGLIA_AUDIO) {\n this.ultimoAudio.set(peer.id, dipendenze.ora());\n } else if (riproduzione?.analyser === null || riproduzione?.analyser === void 0) {\n const sources = riproduzione?.receiver?.getSynchronizationSources?.() ?? [];\n if (sources.some((source) => (source.audioLevel ?? 0) > SOGLIA_AUDIO)) {\n this.ultimoAudio.set(peer.id, dipendenze.ora());\n }\n }\n const speaking = !peer.muted && dipendenze.ora() - (this.ultimoAudio.get(peer.id) ?? 0) <= DURATA_PARLANTE;\n if ((this.speakingPeers.get(peer.id) ?? false) !== speaking) {\n this.speakingPeers.set(peer.id, speaking);\n cambiato = true;\n }\n }\n if (cambiato) this.notificaPeers();\n }\n livelloAnalizzatore(analyser) {\n const nodo = analyser;\n if (nodo?.getFloatTimeDomainData === void 0) return 0;\n const campioni = new Float32Array(nodo.fftSize);\n nodo.getFloatTimeDomainData(campioni);\n return Math.sqrt(campioni.reduce((somma, valore) => somma + valore * valore, 0) / Math.max(1, campioni.length));\n }\n copiaPeers() {\n const you = this.contesto.you();\n const giocatori = this.contesto.giocatori();\n const player = giocatori.find((item) => item.id === you);\n return this.roster.flatMap((peer) => {\n if (peer.id === you) return [];\n if (this.modeCorrente === "team") {\n const altro = giocatori.find((item) => item.id === peer.id);\n if (player?.role !== "spectator" && altro?.team !== player?.team) return [];\n }\n return [{\n id: peer.id,\n mic: peer.mic,\n muted: peer.muted,\n speaking: peer.mic && !peer.muted && (this.speakingPeers.get(peer.id) ?? false),\n volume: this.volumi.get(peer.id) ?? 1,\n gain: this.gains.get(peer.id) ?? 1\n }];\n });\n }\n pulisciPeerAssenti() {\n const presenti = new Set(this.roster.map((peer) => peer.id));\n for (const playerId of this.speakingPeers.keys()) {\n if (!presenti.has(playerId)) this.speakingPeers.delete(playerId);\n }\n for (const playerId of this.zeroDa.keys()) {\n if (presenti.has(playerId)) continue;\n this.zeroDa.delete(playerId);\n const timer = this.timerZero.get(playerId);\n if (timer !== void 0) this.dipendenze?.clearTimeout(timer);\n this.timerZero.delete(playerId);\n }\n }\n osservaCaduta(pc) {\n pc.addEventListener("connectionstatechange", () => {\n if (this.stateCorrente === "on" && (pc.connectionState === "failed" || pc.connectionState === "disconnected")) this.avviaRiconnessione();\n });\n }\n avviaRiconnessione() {\n if (!this.desiderata || this.stateCorrente === "reconnecting") return;\n this.generazione++;\n this.rifiutaRichieste(creaErrore("voice_error", "The voice connection was restarted."));\n this.chiudiRisorse();\n this.tentativoRiconnessione = 0;\n this.aggiornaState("reconnecting");\n this.programmaRiconnessione();\n }\n programmaRiconnessione() {\n if (!this.desiderata || !this.contesto.connessa() || this.timerRiconnessione !== null || this.stateCorrente !== "reconnecting") return;\n const ritardo = RITARDI_RICONNESSIONE[this.tentativoRiconnessione];\n if (ritardo === void 0) {\n this.desiderata = false;\n this.aggiornaState("off");\n return;\n }\n this.tentativoRiconnessione++;\n this.timerRiconnessione = this.richiediDipendenze().setTimeout(() => {\n this.timerRiconnessione = null;\n const generazione = ++this.generazione;\n void this.entra(generazione).catch(() => {\n if (generazione !== this.generazione || !this.desiderata) return;\n this.chiudiRisorse();\n this.aggiornaState("reconnecting");\n this.programmaRiconnessione();\n });\n }, ritardo);\n }\n fermaRiconnessione() {\n if (this.timerRiconnessione === null || this.dipendenze === null) return;\n this.dipendenze.clearTimeout(this.timerRiconnessione);\n this.timerRiconnessione = null;\n }\n chiudiRisorse() {\n const dipendenze = this.dipendenze;\n this.cancellaAttesaConnessione?.();\n this.cancellaAttesaConnessione = null;\n if (dipendenze !== null) {\n if (this.intervalloAudio !== null) dipendenze.clearInterval(this.intervalloAudio);\n if (this.timerConnessione !== null) dipendenze.clearTimeout(this.timerConnessione);\n for (const timer of this.timerZero.values()) dipendenze.clearTimeout(timer);\n }\n this.intervalloAudio = null;\n this.timerConnessione = null;\n this.timerZero.clear();\n for (const playerId of [...this.riproduzioni.keys()]) this.scollegaTraccia(playerId);\n this.peerSfu?.close();\n this.peerSfu = null;\n for (const item of this.mesh.values()) item.pc.close();\n this.mesh.clear();\n this.sfuAttive.clear();\n this.midGiocatori.clear();\n this.negati.clear();\n for (const track of this.stream?.getTracks() ?? []) track.stop();\n this.stream = null;\n this.tracciaMic = null;\n this.analyser = null;\n void this.audioContext?.close().catch(() => void 0);\n this.audioContext = null;\n this.sessioneSfu = null;\n this.connessioneSfuAttesa = false;\n this.trasporto = null;\n this.speakingCorrente = false;\n this.ultimoAudioMic = Number.NEGATIVE_INFINITY;\n this.speakingPeers.clear();\n this.ultimoAudio.clear();\n this.negoziazione = Promise.resolve();\n }\n richiedi(message) {\n if (!this.contesto.connessa()) return Promise.reject(creaErrore("offline", "The room is reconnecting."));\n const r = ++this.sequenzaRichieste;\n return new Promise((resolve, reject) => {\n this.richieste.set(r, { resolve, reject });\n try {\n this.contesto.invia({ ...message, r });\n } catch (cause) {\n this.richieste.delete(r);\n reject(cause);\n }\n });\n }\n rifiutaRichieste(reason) {\n for (const richiesta of this.richieste.values()) richiesta.reject(reason);\n this.richieste.clear();\n }\n aggiornaState(state) {\n if (state === this.stateCorrente) return;\n this.stateCorrente = state;\n for (const listener of this.ascoltatoriState) {\n try {\n listener(state);\n } catch {\n }\n }\n }\n notificaPeers() {\n const peers = this.copiaPeers();\n for (const listener of this.ascoltatoriPeers) {\n try {\n listener(peers);\n } catch {\n }\n }\n }\n controllaGenerazione(generazione) {\n if (generazione !== this.generazione || !this.desiderata) {\n throw creaErrore("offline", "Voice was stopped.");\n }\n }\n richiediDipendenze() {\n if (this.dipendenze === null) throw creaErrore("unsupported", "Voice is not supported.");\n return this.dipendenze;\n }\n richiediMic() {\n if (this.tracciaMic === null) throw creaErrore("voice_error", "The microphone is not ready.");\n return this.tracciaMic;\n }\n richiediAudioContext() {\n if (this.audioContext === null) throw creaErrore("voice_error", "Audio is not ready.");\n return this.audioContext;\n }\n permessoNegato(cause) {\n return typeof cause === "object" && cause !== null && "name" in cause && (cause.name === "NotAllowedError" || cause.name === "SecurityError");\n }\n mappaErrore(cause) {\n if (typeof cause === "object" && cause !== null && "code" in cause) {\n const code = cause.code;\n if (code === "voice_disabled" || code === "permission_denied" || code === "unsupported" || code === "spectator" || code === "offline" || code === "voice_error") return cause;\n return creaErrore("voice_error", "Voice could not be started.");\n }\n return creaErrore("voice_error", "Voice could not be started.");\n }\n};\n\n// src/stanza-client/index.ts\nvar APERTO = 1;\nvar RITARDI_RICONNESSIONE2 = [1e3, 2e3, 4e3, 8e3];\nvar GRAZIA_RICONNESSIONE = 6e4;\nvar INTERVALLO_PING = 5e3;\nvar RITARDO_FLUSH = 500;\nvar ATTESA_ROSTER = 2e3;\nvar CHIUSURE_DEFINITIVE = /* @__PURE__ */ new Set([4003, 4004, 4005, 4006, 4008, 4009]);\nfunction record3(value) {\n return typeof value === "object" && value !== null && !Array.isArray(value) ? value : null;\n}\nfunction ingressoValido(value) {\n const dati = record3(value);\n return dati !== null && typeof dati.roomId === "string" && typeof dati.code === "string" && typeof dati.join === "string" && typeof dati.url === "string";\n}\nfunction visioneValida(value) {\n const dati = record3(value);\n return dati !== null && typeof dati.roomId === "string" && typeof dati.code === "string" && typeof dati.watch === "string" && typeof dati.url === "string";\n}\nfunction rispostaMatchValida(value) {\n const dati = record3(value);\n const players = record3(dati?.players);\n return dati !== null && typeof dati.url === "string" && Number.isInteger(dati.timeoutMs) && dati.timeoutMs >= 1e3 && dati.timeoutMs <= 3e5 && players !== null && Number.isInteger(players.min) && Number.isInteger(players.max) && players.min >= 1 && players.max >= players.min;\n}\nfunction copiaJson(value) {\n return JSON.parse(JSON.stringify(value));\n}\nfunction applicaPatch(state, value) {\n let risultato = copiaJson(state);\n for (const operazione of value) {\n if (operazione.path.length === 0) {\n if (operazione.op !== "set") return { ok: false };\n risultato = copiaJson(operazione.value);\n continue;\n }\n let contenitore = risultato;\n const percorso = operazione.path;\n for (let indice = 0; indice < percorso.length - 1; indice++) {\n const parte = percorso[indice];\n if (Array.isArray(contenitore)) {\n if (typeof parte !== "number" || parte >= contenitore.length) return { ok: false };\n contenitore = contenitore[parte];\n } else {\n const oggetto2 = record3(contenitore);\n if (oggetto2 === null || typeof parte !== "string" || !Object.hasOwn(oggetto2, parte)) {\n return { ok: false };\n }\n contenitore = oggetto2[parte];\n }\n }\n const ultima = percorso.at(-1);\n if (Array.isArray(contenitore)) {\n if (operazione.op !== "set" || typeof ultima !== "number" || ultima >= contenitore.length) return { ok: false };\n contenitore[ultima] = copiaJson(operazione.value);\n } else {\n const oggetto2 = record3(contenitore);\n if (oggetto2 === null || typeof ultima !== "string") return { ok: false };\n if (operazione.op === "del") {\n if (!Object.hasOwn(oggetto2, ultima)) return { ok: false };\n delete oggetto2[ultima];\n } else {\n Object.defineProperty(oggetto2, ultima, {\n configurable: true,\n enumerable: true,\n value: copiaJson(operazione.value),\n writable: true\n });\n }\n }\n }\n return { ok: true, state: risultato };\n}\nfunction creaApiLive(input) {\n const richiedi = creaRichiedente(input.liveOrigin, "", input.fetcher, input.biglietto);\n const richiesta = async (path, method, body, renew) => {\n try {\n return await richiedi(path, method, { ...record3(body), n: input.n }, renew);\n } catch (error) {\n if (error instanceof Error && "code" in error && ["version_outdated", "version_mismatch"].includes(String(error.code))) {\n const value = record3(body);\n const code = typeof value?.code === "string" ? value.code.toUpperCase().replace(/[\\s-]/g, "") : void 0;\n const roomId = typeof value?.roomId === "string" ? value.roomId : void 0;\n input.onVersionError?.(error, error.code === "version_mismatch" ? { code, roomId, watch: path === "/rooms/watch" } : void 0);\n }\n throw error;\n }\n };\n async function ingresso(path, body, rinnova = false) {\n const value = await richiesta(path, "POST", body, rinnova);\n if (!ingressoValido(value)) {\n throw creaErrore("internal_error", "The room service returned an invalid response.");\n }\n return value;\n }\n async function match(options) {\n const value = await richiesta("/match", "POST", {\n mode: options.mode,\n key: options.key\n });\n if (!rispostaMatchValida(value)) {\n throw creaErrore("internal_error", "The matchmaking service returned an invalid response.");\n }\n return value;\n }\n async function visione(body, rinnova = false) {\n const value = await richiesta("/rooms/watch", "POST", body, rinnova);\n if (!visioneValida(value)) {\n throw creaErrore("internal_error", "The room service returned an invalid response.");\n }\n return value;\n }\n return {\n create: (mode) => ingresso("/rooms", { mode }),\n joinCode: (code) => ingresso("/rooms/join", { code }),\n joinRoom: (roomId) => ingresso("/rooms/join", { roomId }, true),\n watchCode: (code) => visione({ code }),\n watchRoom: (roomId) => visione({ roomId }, true),\n match,\n flush: (roomId) => richiesta(\n `/rooms/${encodeURIComponent(roomId)}/flush`,\n "POST"\n )\n };\n}\nvar StanzaClient = class {\n constructor(roomId, codice, url, dipendenze, api, segnalaStanza, spettatore = false) {\n this.roomId = roomId;\n this.codice = codice;\n this.dipendenze = dipendenze;\n this.api = api;\n this.segnalaStanza = segnalaStanza;\n this.spettatore = spettatore;\n this.meta = { host: null, mode: null, countdownAt: null, configuration: null, connection: "connecting", closedCode: null };\n this.metaListeners = /* @__PURE__ */ new Set();\n this.connectionListeners = /* @__PURE__ */ new Set();\n this.scoreListeners = /* @__PURE__ */ new Set();\n this.scores = [];\n this.errorListeners = /* @__PURE__ */ new Set();\n this.roleId = 0;\n this.roleRequests = /* @__PURE__ */ new Map();\n this.statoPubblico = null;\n this.statoSincronizzato = null;\n this.tickCorrente = 0;\n this.tickRateCorrente = 0;\n this.latenzaCorrente = null;\n this.ultimoInput = null;\n this.inputInviato = null;\n this.timerInput = null;\n this.ultimoInvioGioco = -Infinity;\n this.inviiGioco = [];\n this.seedCorrente = 0;\n this.statusCorrente = "lobby";\n this.giocatoriCorrenti = [];\n this.youCorrente = "";\n this.hostCorrente = null;\n this.resultCorrente = null;\n this.delaySpettatore = 0;\n this.socket = null;\n this.seq = 0;\n this.scartoOrario = 0;\n this.timerPing = null;\n this.timerRiconnessione = null;\n this.timerFlush = null;\n this.flushInCorso = false;\n this.flushRichiesto = false;\n this.ritardoIndice = 0;\n this.tempoRiconnessione = 0;\n this.resyncRichiesto = false;\n this.terminata = false;\n this.lasciata = false;\n this.prontaRisolta = false;\n this.welcomeRicevuto = false;\n this.rosterRicevuto = false;\n this.timerRoster = null;\n this.risolviPronta = () => void 0;\n this.rifiutaPronta = () => void 0;\n this.ascoltatoriStato = /* @__PURE__ */ new Set();\n this.ascoltatoriGiocatori = /* @__PURE__ */ new Set();\n this.ascoltatoriStatus = /* @__PURE__ */ new Set();\n this.ascoltatoriMessaggi = /* @__PURE__ */ new Set();\n this.promessaPronta = new Promise((resolve, reject) => {\n this.risolviPronta = resolve;\n this.rifiutaPronta = reject;\n });\n this.voice = new VoceClient({\n invia: (message) => this.invia(message),\n connessa: () => this.socket?.readyState === APERTO && this.welcomeRicevuto && !this.terminata && !this.lasciata,\n you: () => this.youCorrente,\n giocatori: () => this.copiaGiocatori(),\n rosterPronto: () => {\n this.rosterRicevuto = true;\n this.risolviProntaSePossibile();\n }\n }, dipendenze, dipendenze.voce);\n if (spettatore) this.rosterRicevuto = true;\n this.apri(url);\n }\n get mode() {\n return this.meta.mode;\n }\n get countdownAt() {\n return this.meta.countdownAt;\n }\n get connection() {\n return this.meta.connection;\n }\n get metadata() {\n return structuredClone(this.meta);\n }\n get queuedScores() {\n return structuredClone(this.scores);\n }\n onMetadata(listener) {\n this.metaListeners.add(listener);\n return () => this.metaListeners.delete(listener);\n }\n onConnection(listener) {\n this.connectionListeners.add(listener);\n return () => this.connectionListeners.delete(listener);\n }\n onError(listener) {\n this.errorListeners.add(listener);\n return () => this.errorListeners.delete(listener);\n }\n onScoreQueued(listener) {\n this.scoreListeners.add(listener);\n return () => this.scoreListeners.delete(listener);\n }\n metadataChanged(change) {\n const old = this.meta.connection;\n this.meta = { ...this.meta, ...change };\n this.notifica(this.metaListeners, this.metadata);\n if (old !== this.meta.connection) this.notifica(this.connectionListeners, this.meta.connection);\n }\n initialMetadata(room) {\n this.metadataChanged({\n host: room.host,\n mode: room.mode,\n countdownAt: room.countdownAt ?? null,\n rematch: room.rematch ?? null,\n configuration: room.configuration ?? null,\n connection: "connected",\n closedCode: null\n });\n }\n requestRole(role) {\n if (typeof role !== "string" || role.length < 1 || role.length > 32) return Promise.reject(creaErrore("invalid_role", "The role is not valid."));\n if (this.connection !== "connected" || this.status !== "playing" || !this.meta.configuration?.requestRole) {\n return Promise.reject(creaErrore("role_change_unavailable", "Roles cannot be requested right now."));\n }\n if (this.roleRequests.size >= 8) return Promise.reject(creaErrore("rate_limited", "Too many role requests."));\n const r = ++this.roleId;\n return new Promise((resolve, reject) => {\n const timer = this.dipendenze.setTimeout(() => {\n this.roleRequests.delete(r);\n reject(creaErrore("timeout", "The role request timed out."));\n }, 5e3);\n this.roleRequests.set(r, { resolve, reject, timer });\n try {\n this.invia({ t: "request-role", r, role });\n } catch (error) {\n this.dipendenze.clearTimeout(timer);\n this.roleRequests.delete(r);\n reject(error);\n }\n });\n }\n clearRoleRequests() {\n for (const request of this.roleRequests.values()) {\n this.dipendenze.clearTimeout(request.timer);\n request.reject(creaErrore("offline", "The room connection ended."));\n }\n this.roleRequests.clear();\n }\n disconnect() {\n if (this.lasciata) return;\n this.lasciata = true;\n const socket = this.socket;\n this.socket = null;\n this.voice.termina();\n this.fermaInput();\n this.fermaPing();\n this.fermaRiconnessione();\n this.clearRoleRequests();\n if (this.timerRoster !== null) this.dipendenze.clearTimeout(this.timerRoster);\n socket?.close(1e3);\n this.segnalaStanza(null);\n this.metadataChanged({ connection: "disconnected", closedCode: null });\n if (!this.prontaRisolta) {\n this.prontaRisolta = true;\n this.rifiutaPronta(creaErrore("cancelled", "The room was disconnected."));\n }\n }\n get state() {\n return this.statoPubblico;\n }\n get tick() {\n return this.tickCorrente;\n }\n get tickRate() {\n return this.tickRateCorrente;\n }\n get latency() {\n return this.latenzaCorrente;\n }\n get seed() {\n return this.seedCorrente;\n }\n get status() {\n return this.statusCorrente;\n }\n get players() {\n return this.copiaGiocatori();\n }\n get you() {\n return this.youCorrente;\n }\n get host() {\n return this.hostCorrente;\n }\n get code() {\n return this.codice;\n }\n get result() {\n return this.resultCorrente;\n }\n get delayMs() {\n return this.delaySpettatore;\n }\n pronta() {\n return this.promessaPronta;\n }\n invite() {\n return { code: this.codice, url: new URL(`/r/${this.codice}`, this.dipendenze.appOrigin).href };\n }\n onState(listener) {\n this.ascoltatoriStato.add(listener);\n return () => {\n this.ascoltatoriStato.delete(listener);\n };\n }\n onPlayers(listener) {\n this.ascoltatoriGiocatori.add(listener);\n return () => {\n this.ascoltatoriGiocatori.delete(listener);\n };\n }\n onStatus(listener) {\n this.ascoltatoriStatus.add(listener);\n return () => {\n this.ascoltatoriStatus.delete(listener);\n };\n }\n onMessage(listener) {\n this.ascoltatoriMessaggi.add(listener);\n return () => {\n this.ascoltatoriMessaggi.delete(listener);\n };\n }\n send(message) {\n if (this.statusCorrente === "finished") return;\n const prossimo = this.seq + 1;\n this.invia({ t: "msg", seq: prossimo, m: message });\n this.seq = prossimo;\n this.ultimoInvioGioco = this.dipendenze.ora();\n this.inviiGioco = [...this.inviiGioco.slice(-(MESSAGGI_GIOCO_AL_SECONDO - 1)), this.ultimoInvioGioco];\n }\n input(value) {\n if (this.terminata || this.lasciata || this.statusCorrente === "finished") return;\n try {\n const serializzato = JSON.stringify(value);\n if (serializzato === void 0) throw new TypeError();\n this.ultimoInput = serializzato;\n } catch {\n throw creaErrore("invalid_request", "Room input must be valid JSON.");\n }\n this.programmaInput();\n }\n pulisciInput() {\n this.fermaInput();\n this.ultimoInput = this.inputInviato = null;\n this.ultimoInvioGioco = -Infinity;\n this.inviiGioco = [];\n }\n fermaInput() {\n if (this.timerInput !== null) this.dipendenze.clearTimeout(this.timerInput);\n this.timerInput = null;\n }\n programmaInput() {\n if (this.timerInput !== null || this.ultimoInput === null || this.ultimoInput === this.inputInviato || !this.welcomeRicevuto || this.socket?.readyState !== APERTO || this.terminata || this.lasciata) return;\n const ora = this.dipendenze.ora();\n const frequenza = this.tickRateCorrente > 0 ? Math.min(MESSAGGI_GIOCO_AL_SECONDO, this.tickRateCorrente) : MESSAGGI_GIOCO_AL_SECONDO;\n const periodo = 1e3 / frequenza;\n this.inviiGioco = this.inviiGioco.filter((at) => ora - at < 1e3);\n const spazio = this.inviiGioco.length >= MESSAGGI_GIOCO_AL_SECONDO ? this.inviiGioco[0] + 1e3 : ora;\n const prossimo = Number.isFinite(this.ultimoInvioGioco) ? this.ultimoInvioGioco + periodo : ora + periodo;\n this.timerInput = this.dipendenze.setTimeout(() => {\n this.timerInput = null;\n if (this.ultimoInput === null || this.ultimoInput === this.inputInviato || !this.welcomeRicevuto || this.socket?.readyState !== APERTO || this.terminata || this.lasciata) return;\n const adesso = this.dipendenze.ora();\n if (adesso < this.ultimoInvioGioco + periodo || this.inviiGioco.filter((at) => adesso - at < 1e3).length >= MESSAGGI_GIOCO_AL_SECONDO) {\n this.programmaInput();\n return;\n }\n const valore = this.ultimoInput;\n try {\n this.send(JSON.parse(valore));\n this.inputInviato = valore;\n } catch {\n }\n }, Math.max(0, Math.ceil(Math.max(prossimo, spazio) - ora)));\n }\n aggiornaTickRate(value) {\n if (value === void 0 || !Number.isInteger(value) || value < 0 || value > 60 || value === this.tickRateCorrente) return;\n this.tickRateCorrente = value;\n this.fermaInput();\n this.programmaInput();\n }\n ready(ready) {\n this.invia({ t: "ready", ready });\n }\n setRole(role) {\n this.invia({ t: "role", role });\n }\n setTeam(team) {\n this.invia({ t: "team", team });\n }\n start() {\n this.invia({ t: "start" });\n }\n restart() {\n if (this.statusCorrente !== "finished") throw creaErrore("rematch_unavailable", "This room is not waiting for a rematch.");\n this.invia({ t: "restart" });\n }\n leave() {\n if (this.lasciata) return;\n if (!this.spettatore) this.voice.leave();\n this.lasciata = true;\n this.segnalaStanza(null);\n if (this.socket?.readyState === APERTO) {\n const socket = this.socket;\n this.invia({ t: "leave" });\n if (this.spettatore) socket.close(1e3);\n }\n this.termina(1e3);\n }\n serverTime() {\n return this.dipendenze.ora() + this.scartoOrario;\n }\n copiaGiocatori() {\n return this.giocatoriCorrenti.map((player) => ({ ...player }));\n }\n notifica(listeners, ...args) {\n for (const listener of listeners) {\n try {\n listener(...args);\n } catch {\n }\n }\n }\n invia(message) {\n if (this.socket?.readyState !== APERTO) {\n throw creaErrore("offline", "The room is reconnecting.");\n }\n let frame;\n try {\n frame = JSON.stringify(message);\n } catch {\n throw creaErrore("invalid_request", "Room messages must be valid JSON.");\n }\n this.socket.send(frame);\n }\n apri(url) {\n let socket;\n try {\n socket = this.dipendenze.apriSocket(url);\n } catch {\n this.programmaRiconnessione();\n return;\n }\n this.socket = socket;\n socket.addEventListener("open", () => {\n if (this.socket === socket) this.avviaPing();\n });\n socket.addEventListener("message", (evento) => {\n if (this.socket === socket && typeof evento.data === "string") this.ricevi(evento.data);\n });\n socket.addEventListener("close", (evento) => {\n if (this.socket === socket) this.chiuso(evento.code, evento.reason);\n });\n }\n avviaPing() {\n if (this.timerPing !== null) this.dipendenze.clearInterval(this.timerPing);\n this.timerPing = this.dipendenze.setInterval(() => {\n if (this.socket?.readyState !== APERTO) return;\n try {\n this.invia({ t: "ping", c: this.dipendenze.ora() });\n } catch {\n }\n }, INTERVALLO_PING);\n }\n fermaPing() {\n if (this.timerPing === null) return;\n this.dipendenze.clearInterval(this.timerPing);\n this.timerPing = null;\n }\n ricevi(frame) {\n let dati;\n try {\n const value = JSON.parse(frame);\n const oggetto2 = record3(value);\n if (oggetto2 === null || typeof oggetto2.t !== "string") return;\n dati = oggetto2;\n } catch {\n return;\n }\n try {\n if (dati.t === "watching") this.riceviWatching(dati);\n else if (dati.t === "welcome") this.riceviWelcome(dati);\n else if (dati.t === "players") this.riceviGiocatori(dati.players, dati.host);\n else if (dati.t === "status") this.riceviStatus(dati);\n else if (dati.t === "state") this.riceviDiff(dati);\n else if (dati.t === "snapshot") this.riceviSnapshot(dati);\n else if (dati.t === "msg") this.notifica(this.ascoltatoriMessaggi, copiaJson(dati.m));\n else if (dati.t === "pong") this.riceviPong(dati);\n else if (dati.t === "error") this.notifica(this.errorListeners, { code: dati.code, message: dati.message });\n else if (dati.t === "flush") this.richiediFlush();\n else if (dati.t === "score-queued" && !this.spettatore) {\n this.scores.push(structuredClone(dati.score));\n this.scores = this.scores.slice(-32);\n this.notifica(this.scoreListeners, structuredClone(dati.score));\n } else if (dati.t === "role-result") {\n const request = this.roleRequests.get(dati.r);\n if (request) {\n this.dipendenze.clearTimeout(request.timer);\n this.roleRequests.delete(dati.r);\n if (dati.ok) request.resolve();\n else request.reject(creaErrore(dati.code ?? "role_change_refused", "The role change was not accepted."));\n }\n } else if (dati.t === "voice") this.voice.ricevi(dati);\n } catch {\n if (dati.t === "state" || dati.t === "snapshot") this.chiediResync();\n }\n }\n riceviWatching(dati) {\n const room = dati.room;\n if (!this.spettatore || room.id !== this.roomId) return;\n this.aggiornaTickRate(room.tickRate);\n this.seedCorrente = room.seed;\n this.hostCorrente = room.host;\n this.statusCorrente = room.status;\n this.resultCorrente = copiaJson(room.result ?? null);\n if (room.status === "finished") this.pulisciInput();\n this.giocatoriCorrenti = dati.players.map((player) => ({ ...player }));\n this.delaySpettatore = dati.delayMs;\n this.aggiornaStato(dati.state, room.tick, room.serverTime);\n this.scartoOrario = room.serverTime - this.dipendenze.ora();\n this.resyncRichiesto = false;\n this.welcomeRicevuto = true;\n this.ritardoIndice = 0;\n this.tempoRiconnessione = 0;\n this.notifica(this.ascoltatoriGiocatori, this.copiaGiocatori());\n this.notifica(\n this.ascoltatoriStatus,\n this.statusCorrente,\n this.resultCorrente,\n room.serverTime\n );\n this.initialMetadata(room);\n this.programmaInput();\n this.risolviProntaSePossibile();\n }\n riceviWelcome(dati) {\n const room = dati.room;\n if (room.id !== this.roomId) return;\n this.youCorrente = dati.you;\n this.aggiornaTickRate(room.tickRate);\n this.seedCorrente = room.seed;\n this.hostCorrente = room.host;\n this.statusCorrente = room.status;\n this.resultCorrente = copiaJson(room.result ?? null);\n if (room.status === "finished") this.pulisciInput();\n this.giocatoriCorrenti = dati.players.map((player) => ({ ...player }));\n this.aggiornaStato(dati.state, room.tick, room.serverTime);\n this.scartoOrario = room.serverTime - this.dipendenze.ora();\n this.resyncRichiesto = false;\n this.welcomeRicevuto = true;\n if (!this.rosterRicevuto && this.timerRoster === null) {\n this.timerRoster = this.dipendenze.setTimeout(() => {\n this.timerRoster = null;\n this.rosterRicevuto = true;\n this.risolviProntaSePossibile();\n }, ATTESA_ROSTER);\n }\n this.ritardoIndice = 0;\n this.tempoRiconnessione = 0;\n this.notifica(this.ascoltatoriGiocatori, this.copiaGiocatori());\n this.voice.giocatoriCambiati();\n this.voice.socketRiconnesso();\n this.notifica(\n this.ascoltatoriStatus,\n this.statusCorrente,\n this.resultCorrente,\n room.serverTime\n );\n this.initialMetadata(room);\n this.programmaInput();\n this.risolviProntaSePossibile();\n }\n riceviGiocatori(value, host) {\n this.giocatoriCorrenti = value.map((player) => ({ ...player }));\n if (host !== void 0) this.hostCorrente = host;\n else if (!this.giocatoriCorrenti.some(\n (player) => player.id === this.hostCorrente && player.connected\n )) {\n this.hostCorrente = this.giocatoriCorrenti.find((player) => player.connected)?.id ?? null;\n }\n this.metadataChanged({ host: this.hostCorrente });\n this.notifica(this.ascoltatoriGiocatori, this.copiaGiocatori());\n this.voice.giocatoriCambiati();\n }\n riceviStatus(dati) {\n this.statusCorrente = dati.status;\n if (dati.host !== void 0) this.hostCorrente = dati.host;\n this.resultCorrente = copiaJson(dati.result);\n if (dati.status === "finished") {\n this.pulisciInput();\n this.clearRoleRequests();\n }\n if (dati.status === "ended") {\n this.terminata = true;\n this.clearRoleRequests();\n this.segnalaStanza(null);\n if (!this.spettatore) this.voice.termina();\n this.fermaPing();\n this.fermaRiconnessione();\n this.fermaInput();\n this.ultimoInput = null;\n }\n this.metadataChanged({\n rematch: dati.rematch ?? null,\n host: this.hostCorrente,\n countdownAt: dati.countdownAt ?? (dati.status === "countdown" ? dati.at : null),\n ...dati.status === "ended" ? { connection: "ended", closedCode: 4004 } : {}\n });\n this.notifica(this.ascoltatoriStatus, this.statusCorrente, this.resultCorrente, dati.at);\n }\n riceviDiff(dati) {\n this.aggiornaTickRate(dati.tickRate);\n if (dati.base !== this.tickCorrente) {\n this.chiediResync();\n return;\n }\n const risultato = applicaPatch(this.statoSincronizzato, dati.patch);\n if (!risultato.ok) {\n this.chiediResync();\n return;\n }\n this.resyncRichiesto = false;\n this.aggiornaStato(risultato.state, dati.tick, dati.serverTime);\n }\n riceviSnapshot(dati) {\n if (dati.tick < this.tickCorrente) return;\n this.aggiornaTickRate(dati.tickRate);\n this.resyncRichiesto = false;\n this.aggiornaStato(dati.state, dati.tick, dati.serverTime);\n }\n aggiornaStato(state, tick, serverTime) {\n this.statoSincronizzato = copiaJson(state);\n this.statoPubblico = copiaJson(state);\n this.tickCorrente = tick;\n this.notifica(this.ascoltatoriStato, this.statoPubblico, tick, serverTime);\n }\n chiediResync() {\n if (this.resyncRichiesto || this.socket?.readyState !== APERTO) return;\n this.resyncRichiesto = true;\n try {\n this.invia({ t: "resync" });\n } catch {\n this.resyncRichiesto = false;\n }\n }\n riceviPong(dati) {\n const ora = this.dipendenze.ora();\n if (!Number.isFinite(dati.c) || !Number.isFinite(dati.s) || dati.c > ora) return;\n const rtt = ora - dati.c;\n this.latenzaCorrente = this.latenzaCorrente === null ? rtt : this.latenzaCorrente * 0.8 + rtt * 0.2;\n this.scartoOrario = dati.s - (dati.c + ora) / 2;\n }\n chiuso(code, reason) {\n this.socket = null;\n this.welcomeRicevuto = false;\n this.latenzaCorrente = null;\n this.fermaInput();\n this.inputInviato = null;\n this.ultimoInvioGioco = -Infinity;\n this.inviiGioco = [];\n this.fermaPing();\n if (this.lasciata || this.terminata) return;\n if (CHIUSURE_DEFINITIVE.has(code)) {\n const errore = code === 4009 && reason === "message_too_large" ? "message_too_large" : void 0;\n if (errore) this.notifica(this.errorListeners, { code: errore, message: "The room message is too large." });\n this.termina(code, errore);\n return;\n }\n this.clearRoleRequests();\n if (!this.spettatore) this.voice.socketDisconnesso();\n this.programmaRiconnessione();\n }\n programmaRiconnessione() {\n if (this.terminata || this.lasciata || this.timerRiconnessione !== null) return;\n this.metadataChanged({ connection: "reconnecting" });\n const indice = Math.min(this.ritardoIndice, RITARDI_RICONNESSIONE2.length - 1);\n const ritardo = RITARDI_RICONNESSIONE2[indice];\n if (this.tempoRiconnessione + ritardo > GRAZIA_RICONNESSIONE) {\n this.termina("timeout");\n return;\n }\n this.ritardoIndice++;\n this.tempoRiconnessione += ritardo;\n this.timerRiconnessione = this.dipendenze.setTimeout(() => {\n this.timerRiconnessione = null;\n void this.riconnetti();\n }, ritardo);\n }\n async riconnetti() {\n if (this.terminata || this.lasciata) return;\n try {\n const ingresso = this.spettatore ? await this.api.watchRoom(this.roomId) : await this.api.joinRoom(this.roomId);\n if (this.terminata || this.lasciata) return;\n const codiceCambiato = this.codice !== ingresso.code;\n this.codice = ingresso.code;\n if (codiceCambiato && this.prontaRisolta && !this.terminata && !this.lasciata) {\n this.segnalaStanza({ code: this.codice });\n }\n this.apri(ingresso.url);\n } catch (error) {\n if (error instanceof Error && "code" in error && ["version_mismatch", "version_outdated", "room_not_found"].includes(String(error.code))) {\n this.notifica(this.errorListeners, { code: String(error.code), message: error.message });\n this.termina(4004, String(error.code));\n } else this.programmaRiconnessione();\n }\n }\n fermaRiconnessione() {\n if (this.timerRiconnessione === null) return;\n this.dipendenze.clearTimeout(this.timerRiconnessione);\n this.timerRiconnessione = null;\n }\n termina(code, errore) {\n this.clearRoleRequests();\n this.metadataChanged({ connection: code === 1e3 ? "disconnected" : code === 4006 ? "replaced" : "closed", closedCode: typeof code === "number" ? code : null });\n const risultato = { closed: code };\n const cambiato = this.statusCorrente !== "ended" || JSON.stringify(this.resultCorrente) !== JSON.stringify(risultato);\n this.terminata = true;\n this.fermaInput();\n this.ultimoInput = null;\n this.segnalaStanza(null);\n this.statusCorrente = "ended";\n this.resultCorrente = risultato;\n if (!this.spettatore) this.voice.termina();\n this.fermaPing();\n this.fermaRiconnessione();\n if (cambiato) this.notifica(this.ascoltatoriStatus, "ended", risultato, this.serverTime());\n if (!this.prontaRisolta) {\n this.prontaRisolta = true;\n const codici = {\n 4003: "kicked",\n 4004: "room_ended",\n 4005: "version_closed",\n 4006: "replaced",\n 4008: "rate_limited",\n 4009: "invalid_request"\n };\n const erroreCode = errore ?? (typeof code === "number" ? codici[code] ?? "offline" : "offline");\n this.rifiutaPronta(creaErrore(erroreCode, "The room connection ended."));\n }\n }\n risolviProntaSePossibile() {\n if (this.prontaRisolta || !this.welcomeRicevuto || !this.rosterRicevuto) return;\n if (this.timerRoster !== null) {\n this.dipendenze.clearTimeout(this.timerRoster);\n this.timerRoster = null;\n }\n this.prontaRisolta = true;\n if (!this.spettatore && !this.terminata && !this.lasciata) {\n this.segnalaStanza({ code: this.codice });\n }\n this.risolviPronta();\n }\n richiediFlush() {\n this.flushRichiesto = true;\n if (this.flushInCorso || this.timerFlush !== null) return;\n this.timerFlush = this.dipendenze.setTimeout(() => {\n this.timerFlush = null;\n void this.eseguiFlush();\n }, RITARDO_FLUSH);\n }\n async eseguiFlush() {\n if (this.flushInCorso || !this.flushRichiesto) return;\n this.flushInCorso = true;\n this.flushRichiesto = false;\n try {\n await this.api.flush(this.roomId);\n } catch {\n } finally {\n this.flushInCorso = false;\n if (this.flushRichiesto) this.richiediFlush();\n }\n }\n};\nfunction creaStanzeOffline(invited = null) {\n return {\n invited,\n reload() {\n if (typeof window !== "undefined") window.location.reload();\n },\n onError() {\n return () => {\n };\n },\n async create() {\n throw erroreOffline();\n },\n async join() {\n throw erroreOffline();\n },\n async watch() {\n throw erroreOffline();\n },\n async match() {\n throw erroreOffline();\n }\n };\n}\nfunction creaGestoreStanze(input, invited) {\n let reloadTarget;\n const errors = /* @__PURE__ */ new Set();\n const api = creaApiLive({ ...input, onVersionError(error, target) {\n reloadTarget = target;\n for (const listener of errors) {\n try {\n listener(error);\n } catch {\n }\n }\n } });\n let haSegnalato = false;\n let ultimoCodice = null;\n const segnalaStanza = (room) => {\n const codice = room?.code ?? null;\n if (haSegnalato && codice === ultimoCodice) return;\n haSegnalato = true;\n ultimoCodice = codice;\n input.segnalaStanza?.(room);\n };\n const collega = async (ingresso) => {\n const stanza = new StanzaClient(\n ingresso.roomId,\n ingresso.code,\n ingresso.url,\n input,\n api,\n segnalaStanza\n );\n await stanza.pronta();\n return stanza;\n };\n const guarda = async (ingresso) => {\n const stanza = new StanzaClient(\n ingresso.roomId,\n ingresso.code,\n ingresso.url,\n input,\n api,\n () => void 0,\n true\n );\n await stanza.pronta();\n return {\n get mode() {\n return stanza.mode;\n },\n get countdownAt() {\n return stanza.countdownAt;\n },\n get connection() {\n return stanza.connection;\n },\n get metadata() {\n return stanza.metadata;\n },\n onMetadata: (listener) => stanza.onMetadata(listener),\n onConnection: (listener) => stanza.onConnection(listener),\n disconnect: () => stanza.disconnect(),\n get state() {\n return stanza.state;\n },\n get tick() {\n return stanza.tick;\n },\n get tickRate() {\n return stanza.tickRate;\n },\n get latency() {\n return stanza.latency;\n },\n get seed() {\n return stanza.seed;\n },\n get status() {\n return stanza.status;\n },\n get players() {\n return stanza.players;\n },\n get host() {\n return stanza.host;\n },\n get code() {\n return stanza.code;\n },\n get result() {\n return stanza.result;\n },\n get delayMs() {\n return stanza.delayMs;\n },\n onState: (listener) => stanza.onState(listener),\n onPlayers: (listener) => stanza.onPlayers(listener),\n onStatus: (listener) => stanza.onStatus(listener),\n onMessage: (listener) => stanza.onMessage(listener),\n leave: () => {\n stanza.leave();\n },\n serverTime: () => stanza.serverTime()\n };\n };\n const attendiMatch = (url, options) => new Promise((resolve, reject) => {\n let socket;\n let conclusa = false;\n const pulisci = () => {\n socket.removeEventListener("message", ricevi);\n socket.removeEventListener("close", chiuso);\n socket.removeEventListener("error", caduto);\n options.signal?.removeEventListener("abort", annulla);\n };\n const chiudi = () => {\n try {\n socket.close(1e3);\n } catch {\n }\n };\n const fallisci = (errore, chiudiSocket) => {\n if (conclusa) return;\n conclusa = true;\n pulisci();\n if (chiudiSocket) chiudi();\n reject(errore);\n };\n function annulla() {\n fallisci(\n creaErrore("cancelled", "The matchmaking search was cancelled."),\n true\n );\n }\n function chiuso() {\n fallisci(erroreOffline(), false);\n }\n function caduto() {\n fallisci(erroreOffline(), true);\n }\n function ricevi(evento) {\n let dati = null;\n try {\n dati = typeof evento.data === "string" ? record3(JSON.parse(evento.data)) : null;\n } catch {\n }\n if (dati === null || typeof dati.t !== "string") {\n fallisci(creaErrore("internal_error", "The matchmaking service sent an invalid message."), true);\n return;\n }\n if (dati.t === "waiting") {\n if (!Number.isInteger(dati.players) || !Number.isInteger(dati.min) || !Number.isInteger(dati.max)) {\n fallisci(creaErrore("internal_error", "The matchmaking service sent an invalid message."), true);\n return;\n }\n try {\n options.onWaiting?.({\n players: dati.players,\n min: dati.min,\n max: dati.max\n });\n } catch {\n }\n return;\n }\n if (dati.t === "matched") {\n if (!ingressoValido(dati)) {\n fallisci(creaErrore("internal_error", "The matchmaking service sent an invalid message."), true);\n return;\n }\n conclusa = true;\n pulisci();\n chiudi();\n resolve(dati);\n return;\n }\n if (dati.t === "no_match") {\n fallisci(creaErrore("no_match", "No match was found before the timeout."), true);\n return;\n }\n if (dati.t === "error") {\n fallisci(creaErrore(\n typeof dati.code === "string" ? dati.code : "internal_error",\n typeof dati.message === "string" ? dati.message : "The matchmaking service could not complete the search."\n ), true);\n return;\n }\n if (dati.t !== "pong") {\n fallisci(creaErrore("internal_error", "The matchmaking service sent an invalid message."), true);\n }\n }\n try {\n socket = input.apriSocket(url);\n } catch {\n reject(erroreOffline());\n return;\n }\n socket.addEventListener("message", ricevi);\n socket.addEventListener("close", chiuso);\n socket.addEventListener("error", caduto);\n options.signal?.addEventListener("abort", annulla, { once: true });\n if (options.signal?.aborted === true) annulla();\n });\n return {\n invited,\n reload() {\n input.reload?.(reloadTarget);\n },\n onError(listener) {\n errors.add(listener);\n return () => {\n errors.delete(listener);\n };\n },\n async create(options) {\n return collega(await api.create(options.mode));\n },\n async join(code) {\n const scelto = code ?? invited;\n if (scelto === null || scelto === void 0 || scelto.length === 0) {\n throw creaErrore("invalid_request", "A room invitation code is required.");\n }\n return collega(await api.joinCode(scelto));\n },\n async watch(code) {\n if (typeof code !== "string" || code.length === 0) {\n throw creaErrore("invalid_request", "A room invitation code is required.");\n }\n return guarda(await api.watchCode(code));\n },\n async match(options) {\n const annullata = () => options.signal?.aborted === true;\n if (annullata()) {\n throw creaErrore("cancelled", "The matchmaking search was cancelled.");\n }\n const risposta = await api.match(options);\n if (annullata()) {\n throw creaErrore("cancelled", "The matchmaking search was cancelled.");\n }\n return collega(await attendiMatch(risposta.url, options));\n }\n };\n}\n\n// src/standalone.ts\nvar PREFISSO = "caisual:save:";\nvar CHIAVE_VALIDA = /^[a-z0-9][a-z0-9_-]{0,31}$/;\nfunction verificaChiave(key) {\n if (!CHIAVE_VALIDA.test(key)) {\n throw creaErrore("invalid_request", "Save keys must use lowercase letters, numbers, underscores, or hyphens.");\n }\n}\nfunction leggiSalvataggio(testo) {\n if (testo === null) return null;\n try {\n return JSON.parse(testo);\n } catch {\n return null;\n }\n}\nfunction chiavi(archivio) {\n const risultato = [];\n for (let indice = 0; indice < archivio.length; indice++) {\n const key = archivio.key(indice);\n if (key?.startsWith(PREFISSO)) risultato.push(key.slice(PREFISSO.length));\n }\n return risultato;\n}\nfunction creaSave(archivio, ora) {\n const disponibile = () => {\n if (archivio === null) throw erroreOffline();\n return archivio;\n };\n return {\n async set(key, value) {\n verificaChiave(key);\n const locale = disponibile();\n const corpo = JSON.stringify({ value });\n const bytes = new TextEncoder().encode(corpo).byteLength;\n if (bytes > 262144) {\n throw creaErrore("payload_too_large", "The save is larger than 262144 bytes.");\n }\n if (locale.getItem(PREFISSO + key) === null && chiavi(locale).length >= 32) {\n throw creaErrore("save_limit", "A game can store at most 32 save keys.");\n }\n const voce = { value, bytes, updatedAt: ora() };\n locale.setItem(PREFISSO + key, JSON.stringify(voce));\n return { key, bytes, updatedAt: voce.updatedAt };\n },\n async get(key) {\n verificaChiave(key);\n return leggiSalvataggio(disponibile().getItem(PREFISSO + key))?.value ?? null;\n },\n async remove(key) {\n verificaChiave(key);\n disponibile().removeItem(PREFISSO + key);\n },\n async list() {\n const locale = disponibile();\n return chiavi(locale).flatMap((key) => {\n const voce = leggiSalvataggio(locale.getItem(PREFISSO + key));\n return voce === null ? [] : [{ key, bytes: voce.bytes, updatedAt: voce.updatedAt }];\n }).sort((a, b) => a.key.localeCompare(b.key));\n }\n };\n}\nasync function creaStandalone(input, invited = null) {\n const startedAt = input.ora(), day = giornoUtc(startedAt);\n const seed = await calcolaSeed(input.hostname, day, input.subtle);\n return {\n connected: false,\n player: { id: "local", name: "Guest", guest: true },\n daily: creaDaily({ day, seed, expiresAt: prossimaMezzanotteUtc(startedAt) }, input.ora, async () => {\n const now = input.ora(), day2 = giornoUtc(now);\n return { day: day2, seed: await calcolaSeed(input.hostname, day2, input.subtle), expiresAt: prossimaMezzanotteUtc(now) };\n }),\n time: { now: input.ora },\n save: creaSave(input.archivio, input.ora),\n board: {\n async submit() {\n return { accepted: false, reason: "offline", verified: false };\n },\n async top(_board, opzioni = {}) {\n if (opzioni.day !== void 0 && (!validBoardDay(opzioni.day) || opzioni.daily === false)) throw creaErrore("invalid_request", "day must be a real UTC date and cannot be combined with daily: false.");\n return { day: opzioni.day ?? (opzioni.daily ? day : null), entries: [], me: null };\n }\n },\n room: creaStanzeOffline(invited)\n };\n}\n\n// src/kit.ts\nfunction leggiAppOrigin(documento) {\n const valore = documento?.querySelector(\'meta[name="caisual-app"]\')?.getAttribute("content");\n if (valore === null || valore === void 0) return null;\n try {\n const url = new URL(valore);\n return url.origin === valore && (url.protocol === "https:" || url.protocol === "http:") ? valore : null;\n } catch {\n return null;\n }\n}\nfunction archivioReale() {\n try {\n return typeof localStorage === "undefined" ? null : localStorage;\n } catch {\n return null;\n }\n}\nfunction dipendenzeReali2() {\n return {\n finestra: typeof window === "undefined" ? null : window,\n documento: typeof document === "undefined" ? null : document,\n fetcher: (input, init) => globalThis.fetch(input, init),\n archivio: archivioReale(),\n language: typeof navigator === "undefined" ? "en" : navigator.language,\n pathname: typeof location === "undefined" ? "/" : location.pathname,\n hostname: typeof location === "undefined" ? "" : location.hostname,\n subtle: globalThis.crypto.subtle,\n ora: Date.now,\n sonda: () => probeDevice()\n };\n}\nasync function connetti(input) {\n const appOrigin = leggiAppOrigin(input.documento);\n const senzaPadre = input.finestra === null || input.finestra.parent === input.finestra;\n if (appOrigin === null || senzaPadre) {\n return localConnection(input);\n }\n const handshake = await attendiHandshake(\n input.finestra,\n appOrigin,\n input.timeoutHandshake\n );\n if (handshake === null) return localConnection(input);\n const biglietto = creaGestoreBiglietto(\n handshake.ticket,\n handshake.porta,\n input.finestra,\n input.ora,\n input.timeoutRinnovo,\n "portal"\n );\n const api = creaClienteApi(appOrigin, input.fetcher, biglietto);\n const prima = input.ora();\n let me;\n try {\n me = await api.me();\n } catch {\n const base2 = await creaStandalone(input, handshake.invite);\n return installSession(base2, handshake, input);\n }\n const dopo = input.ora();\n const scartoOrario = me.serverTime - (prima + dopo) / 2;\n const room = handshake.live === null ? creaStanzeOffline(handshake.invite) : creaGestoreStanze({\n appOrigin,\n n: handshake.n,\n reload: (target) => handshake.porta.postMessage({ type: "caisual:reload", target }),\n liveOrigin: handshake.live,\n fetcher: input.fetcher,\n biglietto: creaGestoreBiglietto(\n null,\n handshake.porta,\n input.finestra,\n input.ora,\n input.timeoutRinnovo,\n "live"\n ),\n apriSocket(url) {\n if (input.apriSocket !== void 0) return input.apriSocket(url);\n if (typeof WebSocket === "undefined") throw erroreOffline();\n return new WebSocket(url);\n },\n ora: input.ora,\n setTimeout: (handler, timeout) => globalThis.setTimeout(handler, timeout),\n clearTimeout: (id) => globalThis.clearTimeout(id),\n setInterval: (handler, timeout) => globalThis.setInterval(handler, timeout),\n clearInterval: (id) => globalThis.clearInterval(id),\n voce: input.voce,\n segnalaStanza(room2) {\n try {\n handshake.porta.postMessage({ type: "caisual:room", room: room2 });\n } catch {\n }\n }\n }, handshake.invite);\n const base = {\n connected: true,\n player: me.player,\n daily: creaDaily(\n { day: me.day, seed: me.seed, expiresAt: me.expiresAt ?? prossimaMezzanotteUtc(me.serverTime) },\n () => input.ora() + scartoOrario,\n async () => {\n const next = await api.me();\n return { day: next.day, seed: next.seed, expiresAt: next.expiresAt ?? prossimaMezzanotteUtc(next.serverTime) };\n }\n ),\n time: { now: () => input.ora() + scartoOrario },\n save: {\n set: (key, value) => api.saveSet(key, value),\n get: (key) => api.saveGet(key),\n remove: (key) => api.saveRemove(key),\n list: () => api.saveList()\n },\n board: {\n async submit(board, score, opzioni = {}) {\n try {\n return await api.boardSubmit(board, score, opzioni.daily === true);\n } catch (errore) {\n if (typeof errore === "object" && errore !== null && "code" in errore && errore.code === "offline") return { accepted: false, reason: "offline", verified: false };\n throw errore;\n }\n },\n top: (board, opzioni = {}) => api.boardTop(board, opzioni)\n },\n room\n };\n return installSession(base, handshake, input);\n}\nfunction installSession(base, handshake, input) {\n const coordinator = createSession(base, handshake?.overlay?.configuration ?? null, base.connected && handshake?.live != null);\n if (handshake?.overlay) {\n const dispose = attachKitBridge(handshake.porta, handshake.overlay, coordinator);\n if (coordinator.session.capabilities.overlay && typeof window !== "undefined" && input?.finestra === window) window.addEventListener("pagehide", dispose, { once: true });\n }\n const preferences = handshake?.languagePreferences?.length ? handshake.languagePreferences : [handshake?.language ?? input?.language ?? "en"];\n const language = resolveGameLanguage(preferences, handshake?.gameLanguages ?? (handshake?.overlay ? manifestLanguages(handshake.overlay.configuration.manifest) : void 0));\n const uiLanguage = overlayLocale(handshake?.uiLanguage ?? handshake?.language ?? input?.language);\n return {\n ...base,\n player: { ...base.player, language, uiLanguage },\n text: createTextLoader(input?.fetcher ?? globalThis.fetch, language, input?.pathname),\n room: coordinator.rooms,\n session: coordinator.session,\n overlay: coordinator.overlay\n };\n}\nasync function localConnection(input) {\n return installSession(await creaStandalone(input), void 0, input);\n}\nfunction dispositivoSconosciuto() {\n return {\n webgl2: false,\n webgpu: false,\n wasm: false,\n threads: false,\n isolated: false,\n gpu: "none",\n memoryMb: null,\n cores: null,\n mobile: false,\n tier: "low"\n };\n}\nasync function attendiSonda(sonda) {\n let timer;\n try {\n return await Promise.race([\n Promise.resolve().then(sonda).catch(() => dispositivoSconosciuto()),\n new Promise((resolve) => {\n timer = globalThis.setTimeout(() => resolve(dispositivoSconosciuto()), 1500);\n })\n ]);\n } finally {\n if (timer !== void 0) globalThis.clearTimeout(timer);\n }\n}\nfunction creaKit(input = dipendenzeReali2()) {\n let promessa = null;\n return {\n connect() {\n promessa ?? (promessa = Promise.all([connetti(input), attendiSonda(input.sonda)]).then(([connessione, device]) => ({ ...connessione, device })));\n return promessa;\n }\n };\n}\n\n// src/index.ts\nvar caisual = creaKit();\nglobalThis.caisual = caisual;\nvar index_default = caisual;\nexport {\n caisual,\n index_default as default\n};\n');
5024
5028
  return;
5025
5029
  }
5026
5030
  const textMatch = url.pathname.match(/^\/__caisual\/text\/([^/]+)\.json$/);
@@ -5075,7 +5079,7 @@ var DevService = class {
5075
5079
  }
5076
5080
  if (url.pathname === "/__caisual/overlay/v1.js" && (request.method === "GET" || request.method === "HEAD")) {
5077
5081
  response.writeHead(200, { "Content-Type": "text/javascript; charset=utf-8", "Cache-Control": "no-store", "X-Content-Type-Options": "nosniff" });
5078
- response.end(request.method === "HEAD" ? void 0 : '// ../contracts/src/slug.ts\nvar NOMI_RISERVATI = [\n "www",\n "api",\n "app",\n "play",\n "live",\n "multi",\n "cdn",\n "assets",\n "static",\n "mail",\n "mx",\n "ns1",\n "ns2",\n "autodiscover",\n "_dmarc",\n "admin",\n "login",\n "account",\n "auth",\n "pay",\n "secure",\n "support",\n "help",\n "blog",\n "status",\n "dev",\n "staging",\n "test",\n "caisual",\n "shipz"\n];\nvar RISERVATI = new Set(NOMI_RISERVATI);\nvar SLUG_NUOVO = /^[a-z0-9]+(?:-[a-z0-9]+)*$/;\nvar SLUG_STORICO = /^[0-9a-f]{8}-[0-9a-f]{4}-4[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/;\nfunction isValidSlug(value) {\n return value.length >= 3 && value.length <= 32 && SLUG_NUOVO.test(value) || SLUG_STORICO.test(value);\n}\nfunction isReservedSlug(value) {\n return RISERVATI.has(value);\n}\n\n// ../contracts/src/i18n.ts\nfunction normalizeLanguage(value) {\n if (typeof value !== "string" || value.length > 128) return null;\n try {\n return Intl.getCanonicalLocales(value)[0] ?? null;\n } catch {\n return null;\n }\n}\nfunction manifestLanguages(manifest) {\n return manifest.languages?.length ? [...manifest.languages] : [manifest.language ?? "en"];\n}\nfunction languageFallbacks(language, defaultLanguage = "en") {\n const result = [];\n let tag = normalizeLanguage(language);\n while (tag) {\n result.push(tag);\n const parts = tag.split("-");\n parts.pop();\n if (parts.at(-1)?.length === 1) parts.pop();\n tag = parts.join("-");\n }\n result.push(normalizeLanguage(defaultLanguage) ?? defaultLanguage);\n return [...new Set(result)];\n}\nfunction resolveText(value, language, defaultLanguage = "en", key = "") {\n if (typeof value === "string") return value;\n if (value) {\n for (const tag of languageFallbacks(language, defaultLanguage)) {\n const name = Object.keys(value).find((name2) => name2.toLowerCase() === tag.toLowerCase());\n if (name !== void 0 && typeof value[name] === "string") return value[name];\n }\n }\n return key;\n}\n\n// ../contracts/src/manifest.ts\nfunction risolviModalita(manifest, mode) {\n const scelta = mode === null ? void 0 : manifest.modes.find((voce) => voce.id === mode);\n if (mode !== null && scelta === void 0) throw new Error("The selected game mode does not exist.");\n return { players: { ...scelta?.players ?? manifest.players }, lobby: scelta?.lobby ?? manifest.lobby };\n}\nfunction risolviPresentazione(manifest, mode, language = manifestLanguages(manifest)[0]) {\n risolviModalita(manifest, mode);\n const scelta = manifest.modes.find((voce) => voce.id === mode);\n return {\n execution: scelta?.execution ?? null,\n label: resolveText(scelta?.label, language, manifestLanguages(manifest)[0], scelta?.id ?? manifest.name ?? "Play"),\n instructions: resolveText(scelta?.instructions, language, manifestLanguages(manifest)[0]) || null\n };\n}\nvar TETTO_GIOCATORI = 24;\nvar RITARDO_SPETTATORI_MS = 3e3;\nvar MASSIMO_CLASSIFICHE = 32;\nvar CAMPI = /* @__PURE__ */ new Set([\n "overlay",\n "manifest",\n "id",\n "name",\n "description",\n "cover",\n "card",\n "icon",\n "screenshots",\n "tags",\n "languages",\n "language",\n "platform",\n "orientation",\n "input",\n "visibility",\n "network",\n "isolated",\n "requires",\n "players",\n "lobby",\n "persistent",\n "spectators",\n "boards",\n "roles",\n "teams",\n "voice",\n "modes"\n]);\nvar INPUT = /* @__PURE__ */ new Set(["keyboard", "mouse", "touch", "gamepad"]);\nvar PLATFORM = /* @__PURE__ */ new Set(["desktop", "mobile", "both"]);\nvar ORIENTATION = /* @__PURE__ */ new Set(["landscape", "portrait"]);\nvar VISIBILITY = /* @__PURE__ */ new Set(["public", "unlisted"]);\nvar VOICE = /* @__PURE__ */ new Set(["none", "room", "team", "proximity"]);\nvar PERFORMANCE = /* @__PURE__ */ new Set(["light", "medium", "heavy"]);\nvar TAG = /^[a-z0-9-]+$/;\nvar ID_INTERNO = /^[a-z0-9]+(?:-[a-z0-9]+)*$/;\nvar CAMPO_MATCHMAKING = /^[a-z0-9][a-z0-9-]{0,31}$/;\nvar ID_CLASSIFICA = /^[a-z0-9][a-z0-9_-]{0,31}$/;\nfunction oggetto(value) {\n if (typeof value !== "object" || value === null || Array.isArray(value)) return null;\n return value;\n}\nfunction percorsoRelativo(value) {\n if (value === "" || value.startsWith("/") || value.includes("\\\\") || value.includes("\\0")) return false;\n if (value.includes("?") || value.includes("#")) return false;\n const parti = value.split("/");\n if (parti.some((parte) => parte === "" || parte === "." || parte === "..")) return false;\n try {\n const decoded = parti.map((parte) => decodeURIComponent(parte));\n return !decoded.some((parte) => parte === "" || parte === "." || parte === ".." || parte.includes("/"));\n } catch {\n return false;\n }\n}\nfunction hostValido(value) {\n if (value.length === 0 || value.length > 253) return false;\n if (value.includes("://") || /[/:?#@]/.test(value)) return false;\n const parti = value.split(".");\n return parti.every(\n (parte) => parte.length >= 1 && parte.length <= 63 && /^[a-z0-9](?:[a-z0-9-]*[a-z0-9])?$/i.test(parte)\n );\n}\nfunction interoTra(value, min, max) {\n return typeof value === "number" && Number.isInteger(value) && value >= min && value <= max;\n}\nfunction stringaDefault(dati, campo, valoreDefault, errori) {\n const value = dati[campo];\n if (value === void 0) return valoreDefault;\n if (typeof value !== "string") {\n errori.push(`${campo}: must be a string.`);\n return valoreDefault;\n }\n return value;\n}\nfunction testoFacoltativo(value, key, max, path, errors) {\n if (value[key] === void 0) return void 0;\n const check = (text2, field2) => {\n if (typeof text2 !== "string" || text2.trim().length === 0 || text2.trim().length > max || /[\\r\\n\\u0000-\\u001f]/.test(text2)) {\n errors.push(`${field2}: must contain 1-${max} characters on one line.`);\n return void 0;\n }\n return text2.trim();\n };\n const text = value[key], field = `${path}.${key}`;\n if (typeof text === "string") return check(text, field);\n const translations = oggetto(text);\n if (!translations || Object.keys(translations).length === 0) {\n errors.push(`${field}: must be a string or a non-empty language-to-text object.`);\n return void 0;\n }\n const result = {};\n for (const [raw, text2] of Object.entries(translations)) {\n const tag = normalizeLanguage(raw);\n if (!tag) {\n errors.push(`${field}.${raw}: must be a BCP 47 language tag.`);\n continue;\n }\n if (Object.hasOwn(result, tag)) errors.push(`${field}.${raw}: duplicate language.`);\n const checked = check(text2, `${field}.${raw}`);\n if (checked !== void 0) result[tag] = checked;\n }\n return result;\n}\nfunction validaManifest(valore) {\n const errori = [];\n const dati = oggetto(valore);\n if (dati === null) return { ok: false, errori: ["manifest: must be a JSON object."] };\n for (const campo of Object.keys(dati)) {\n if (!CAMPI.has(campo)) errori.push(`${campo}: unknown field.`);\n }\n if (dati.manifest === void 0) errori.push("manifest: is required and must be 1.");\n else if (dati.manifest !== 1) errori.push("manifest: must be exactly 1.");\n const id = stringaDefault(dati, "id", "", errori);\n if (dati.id === void 0) errori.push("id: is required.");\n else if (typeof dati.id === "string") {\n if (!isValidSlug(id)) {\n errori.push("id: must be 3-32 lowercase ASCII letters or digits with internal hyphens; historical UUID v4 slugs may be 36 characters.");\n } else if (isReservedSlug(id)) errori.push("id: this slug is reserved.");\n }\n const name = stringaDefault(dati, "name", "", errori);\n if (dati.name === void 0) errori.push("name: is required.");\n else if (typeof dati.name === "string" && (name.trim() === "" || name.length > 60)) {\n errori.push("name: must contain 1-60 characters.");\n }\n const description = stringaDefault(dati, "description", "", errori);\n if (description.length > 500) errori.push("description: must be at most 500 characters.");\n const immagini = { cover: "", card: "", icon: "" };\n const usati = /* @__PURE__ */ new Set();\n for (const campo of ["cover", "card", "icon"]) {\n const path = dati[campo];\n if (path === void 0 || path === null) errori.push(`${campo}: is required.`);\n else if (typeof path !== "string" || !percorsoRelativo(path)) errori.push(`${campo}: must be a relative file path inside client/ without query, fragment, or parent segments.`);\n else {\n if (!/\\.(png|jpe?g|webp)$/i.test(path)) errori.push(`${campo}: must be a PNG, JPEG or WebP file.`);\n const canonical = decodeURIComponent(path);\n if (usati.has(canonical)) errori.push(`${campo}: each image must use a different file; cover, card and icon cannot share a path.`);\n usati.add(canonical);\n immagini[campo] = path;\n }\n }\n const { cover, card, icon } = immagini;\n const screenshots = [];\n if (dati.screenshots !== void 0) {\n if (!Array.isArray(dati.screenshots)) errori.push("screenshots: must be an array of relative file paths.");\n else {\n if (dati.screenshots.length > 8) errori.push("screenshots: must contain at most 8 paths.");\n for (const [indice, value] of dati.screenshots.entries()) {\n if (typeof value !== "string" || !percorsoRelativo(value)) {\n errori.push(`screenshots[${indice}]: must be a relative file path without query, fragment, or parent segments.`);\n } else screenshots.push(value);\n }\n }\n }\n const tags = [];\n if (dati.tags !== void 0) {\n if (!Array.isArray(dati.tags)) errori.push("tags: must be an array.");\n else {\n if (dati.tags.length > 10) errori.push("tags: must contain at most 10 tags.");\n for (const [indice, value] of dati.tags.entries()) {\n if (typeof value !== "string" || value.length > 24 || !TAG.test(value)) {\n errori.push(`tags[${indice}]: must be 1-24 lowercase letters, digits, or hyphens.`);\n } else tags.push(value);\n }\n }\n }\n const legacyLanguage = stringaDefault(dati, "language", "en", errori);\n if (!/^[A-Za-z]{2,8}(?:-[A-Za-z0-9]{1,8})*$/.test(legacyLanguage)) {\n errori.push("language: must be a BCP 47 language tag such as en, it, or pt-BR.");\n }\n const languages2 = [];\n if (!Array.isArray(dati.languages) || dati.languages.length === 0) {\n errori.push("languages: must be a non-empty array of BCP 47 language tags.");\n } else for (const [index, raw] of dati.languages.entries()) {\n const tag = normalizeLanguage(raw);\n if (!tag) errori.push(`languages[${index}]: must be a BCP 47 language tag.`);\n else if (languages2.includes(tag)) errori.push(`languages[${index}]: duplicate language ${tag}.`);\n else languages2.push(tag);\n }\n if (!languages2.includes("en")) errori.push("languages: English is always required alongside the game\'s own languages.");\n const language = languages2[0] ?? legacyLanguage;\n if (dati.language !== void 0 && dati.languages !== void 0 && legacyLanguage.toLowerCase() !== language.toLowerCase()) {\n errori.push("language: must match the first entry in languages when both are present.");\n }\n let platform = "both";\n if (dati.platform === void 0) errori.push("platform: is required.");\n else if (typeof dati.platform !== "string" || !PLATFORM.has(dati.platform)) {\n errori.push("platform: must be desktop, mobile, or both.");\n } else platform = dati.platform;\n let orientation = "landscape";\n if (dati.orientation !== void 0) {\n if (typeof dati.orientation !== "string" || !ORIENTATION.has(dati.orientation)) {\n errori.push("orientation: must be landscape or portrait.");\n } else orientation = dati.orientation;\n }\n const input = [];\n if (dati.input !== void 0) {\n if (!Array.isArray(dati.input)) errori.push("input: must be an array.");\n else for (const [indice, value] of dati.input.entries()) {\n if (typeof value !== "string" || !INPUT.has(value)) {\n errori.push(`input[${indice}]: must be keyboard, mouse, touch, or gamepad.`);\n } else if (input.includes(value)) errori.push(`input[${indice}]: duplicate value ${value}.`);\n else input.push(value);\n }\n }\n let visibility = "public";\n if (dati.visibility !== void 0) {\n if (typeof dati.visibility !== "string" || !VISIBILITY.has(dati.visibility)) {\n errori.push("visibility: must be public or unlisted.");\n } else visibility = dati.visibility;\n }\n const network = [];\n if (dati.network !== void 0) {\n if (!Array.isArray(dati.network)) errori.push("network: must be an array of host names.");\n else for (const [indice, value] of dati.network.entries()) {\n if (typeof value !== "string" || !hostValido(value)) {\n errori.push(`network[${indice}]: must be a host name without scheme, port, path, query, or fragment.`);\n } else if (network.includes(value)) errori.push(`network[${indice}]: duplicate host ${value}.`);\n else network.push(value);\n }\n }\n let isolated = false;\n if (dati.isolated !== void 0) {\n if (typeof dati.isolated !== "boolean") errori.push("isolated: must be a boolean.");\n else isolated = dati.isolated;\n }\n const requires = {\n webgl2: false,\n webgpu: false,\n wasm: false,\n threads: false,\n memoryMb: null,\n performance: "light"\n };\n if (dati.requires !== void 0) {\n const value = oggetto(dati.requires);\n if (value === null) errori.push("requires: must be an object.");\n else {\n for (const campo of Object.keys(value)) {\n if (!["webgl2", "webgpu", "wasm", "threads", "memoryMb", "performance"].includes(campo)) {\n errori.push(`requires.${campo}: unknown field.`);\n }\n }\n for (const campo of ["webgl2", "webgpu", "wasm", "threads"]) {\n if (value[campo] === void 0) continue;\n if (typeof value[campo] !== "boolean") errori.push(`requires.${campo}: must be a boolean.`);\n else requires[campo] = value[campo];\n }\n if (value.memoryMb !== void 0) {\n if (value.memoryMb !== null && (!interoTra(value.memoryMb, 512, 32768) || value.memoryMb % 256 !== 0)) {\n errori.push("requires.memoryMb: must be an integer from 512 to 32768 in steps of 256, or null.");\n } else requires.memoryMb = value.memoryMb;\n }\n if (value.performance !== void 0) {\n if (typeof value.performance !== "string" || !PERFORMANCE.has(value.performance)) {\n errori.push("requires.performance: must be light, medium, or heavy.");\n } else requires.performance = value.performance;\n }\n if (requires.threads && !isolated) errori.push("requires.threads: needs isolated: true.");\n }\n }\n let players = { min: 1, max: 1 };\n if (dati.players !== void 0) {\n const value = oggetto(dati.players);\n if (value === null) errori.push("players: must be an object with min and max.");\n else {\n for (const campo of Object.keys(value)) {\n if (campo !== "min" && campo !== "max") errori.push(`players.${campo}: unknown field.`);\n }\n if (!interoTra(value.min, 1, TETTO_GIOCATORI)) errori.push(`players.min: must be an integer from 1 to ${TETTO_GIOCATORI}.`);\n if (!interoTra(value.max, 1, TETTO_GIOCATORI)) errori.push(`players.max: must be an integer from 1 to ${TETTO_GIOCATORI} in manifest version 1.`);\n if (interoTra(value.min, 1, TETTO_GIOCATORI) && interoTra(value.max, 1, TETTO_GIOCATORI)) {\n if (value.min > value.max) errori.push("players.max: must be greater than or equal to players.min.");\n else players = { min: value.min, max: value.max };\n }\n }\n }\n let lobby = false;\n if (dati.lobby !== void 0) {\n if (typeof dati.lobby !== "boolean") errori.push("lobby: must be a boolean.");\n else lobby = dati.lobby;\n }\n let persistent = false;\n if (dati.persistent !== void 0) {\n if (typeof dati.persistent !== "boolean") errori.push("persistent: must be a boolean.");\n else persistent = dati.persistent;\n }\n let spectators = { delayMs: RITARDO_SPETTATORI_MS };\n if (dati.spectators === false || dati.spectators === null) spectators = null;\n else if (dati.spectators !== void 0 && dati.spectators !== true) {\n const value = oggetto(dati.spectators);\n if (value === null) {\n errori.push("spectators: must be a boolean or an object with delayMs.");\n } else {\n for (const campo of Object.keys(value)) {\n if (campo !== "delayMs") errori.push(`spectators.${campo}: unknown field.`);\n }\n if (!interoTra(value.delayMs, 0, 3e4)) {\n errori.push("spectators.delayMs: must be an integer from 0 to 30000.");\n } else spectators = { delayMs: value.delayMs };\n }\n }\n let overlay = null;\n if (dati.overlay !== void 0 && dati.overlay !== null) {\n const value = oggetto(dati.overlay);\n if (value === null) errori.push("overlay: must be an object or null.");\n else {\n for (const key of Object.keys(value)) if (!["version", "accent"].includes(key)) errori.push(`overlay.${key}: unknown field.`);\n if (value.version !== 1) errori.push("overlay.version: must be exactly 1.");\n if (value.accent !== void 0 && (typeof value.accent !== "string" || !/^#[0-9a-fA-F]{6}$/.test(value.accent))) {\n errori.push("overlay.accent: must be a six-digit hexadecimal color, such as #336699.");\n }\n overlay = { version: 1, ...typeof value.accent === "string" ? { accent: value.accent } : {} };\n }\n }\n const boards = {};\n if (dati.boards !== void 0) {\n const value = oggetto(dati.boards);\n if (value === null) errori.push("boards: must be an object of board ids.");\n else {\n if (Object.keys(value).length > MASSIMO_CLASSIFICHE) {\n errori.push(`boards: at most ${MASSIMO_CLASSIFICHE} boards.`);\n }\n for (const [id2, raw] of Object.entries(value)) {\n let valido = true;\n if (!ID_CLASSIFICA.test(id2)) {\n errori.push(`boards.${id2}: invalid board id.`);\n valido = false;\n }\n const board = oggetto(raw);\n if (board === null) {\n errori.push(`boards.${id2}.source: must be "client" or "server".`);\n continue;\n }\n for (const campo of Object.keys(board)) {\n if (!["source", "label", "periods", "day"].includes(campo)) errori.push(`boards.${id2}.${campo}: unknown field.`);\n }\n if (board.source !== "client" && board.source !== "server") {\n errori.push(`boards.${id2}.source: must be "client" or "server".`);\n valido = false;\n }\n if (board.day !== void 0 && board.day !== "submit" && board.day !== "start") errori.push(`boards.${id2}.day: must be "submit" or "start".`);\n if (board.day === "start" && board.source !== "server") errori.push(`boards.${id2}.day: start requires source "server".`);\n const label = testoFacoltativo(board, "label", 48, `boards.${id2}`, errori);\n let periods = ["all-time"];\n if (board.periods !== void 0) {\n if (!Array.isArray(board.periods) || board.periods.length < 1 || board.periods.length > 2 || board.periods.some((period) => period !== "daily" && period !== "all-time") || new Set(board.periods).size !== board.periods.length) {\n errori.push(`boards.${id2}.periods: must contain daily, all-time, or both without duplicates.`);\n } else periods = [...board.periods];\n }\n if (valido) Object.defineProperty(boards, id2, { value: {\n source: board.source,\n periods,\n ...board.day === void 0 ? {} : { day: board.day },\n ...label === void 0 ? {} : { label }\n }, enumerable: true, configurable: true, writable: true });\n }\n }\n }\n const roles = [];\n if (dati.roles !== void 0) {\n if (!Array.isArray(dati.roles)) errori.push("roles: must be an array.");\n else {\n const ids = /* @__PURE__ */ new Set();\n for (const [indice, raw] of dati.roles.entries()) {\n const value = oggetto(raw);\n if (value === null) {\n errori.push(`roles[${indice}]: must be an object.`);\n continue;\n }\n for (const campo of Object.keys(value)) {\n if (!["id", "min", "max", "label"].includes(campo)) errori.push(`roles[${indice}].${campo}: unknown field.`);\n }\n const idRuolo = value.id;\n const min = value.min;\n const max = value.max;\n let valido = true;\n if (typeof idRuolo !== "string" || idRuolo.length > 32 || !ID_INTERNO.test(idRuolo)) {\n errori.push(`roles[${indice}].id: must be 1-32 lowercase letters, digits, or internal hyphens.`);\n valido = false;\n } else if (ids.has(idRuolo)) {\n errori.push(`roles[${indice}].id: duplicate role ${idRuolo}.`);\n valido = false;\n } else ids.add(idRuolo);\n if (!interoTra(min, 0, TETTO_GIOCATORI)) {\n errori.push(`roles[${indice}].min: must be an integer from 0 to ${TETTO_GIOCATORI}.`);\n valido = false;\n }\n if (max !== void 0 && !interoTra(max, 0, TETTO_GIOCATORI)) {\n errori.push(`roles[${indice}].max: must be an integer from 0 to ${TETTO_GIOCATORI} when present.`);\n valido = false;\n }\n if (typeof min === "number" && typeof max === "number" && min > max) {\n errori.push(`roles[${indice}].max: must be greater than or equal to min.`);\n valido = false;\n }\n const label = testoFacoltativo(value, "label", 32, `roles[${indice}]`, errori);\n if (valido) roles.push({\n id: idRuolo,\n min,\n ...max === void 0 ? {} : { max },\n ...label === void 0 ? {} : { label }\n });\n }\n }\n }\n let teams = null;\n if (dati.teams !== void 0 && dati.teams !== null) {\n const value = oggetto(dati.teams);\n if (value === null) errori.push("teams: must be null or an object with min and max.");\n else {\n for (const campo of Object.keys(value)) {\n if (campo !== "min" && campo !== "max") errori.push(`teams.${campo}: unknown field.`);\n }\n if (!interoTra(value.min, 2, TETTO_GIOCATORI)) errori.push(`teams.min: must be an integer from 2 to ${TETTO_GIOCATORI}.`);\n if (!interoTra(value.max, 2, TETTO_GIOCATORI)) errori.push(`teams.max: must be an integer from 2 to ${TETTO_GIOCATORI}.`);\n if (interoTra(value.min, 2, TETTO_GIOCATORI) && interoTra(value.max, 2, TETTO_GIOCATORI)) {\n if (value.min > value.max) errori.push("teams.max: must be greater than or equal to teams.min.");\n else teams = { min: value.min, max: value.max };\n }\n }\n }\n let voice = "none";\n if (dati.voice !== void 0) {\n if (typeof dati.voice !== "string" || !VOICE.has(dati.voice)) {\n errori.push("voice: must be none, room, team, or proximity.");\n } else voice = dati.voice;\n }\n const modes = [];\n if (dati.modes !== void 0) {\n if (!Array.isArray(dati.modes)) errori.push("modes: must be an array.");\n else {\n const ids = /* @__PURE__ */ new Set();\n for (const [indice, raw] of dati.modes.entries()) {\n const value = oggetto(raw);\n if (value === null) {\n errori.push(`modes[${indice}]: must be an object.`);\n continue;\n }\n for (const campo of Object.keys(value)) {\n if (!["id", "players", "lobby", "matchmaking", "execution", "label", "instructions"].includes(campo)) errori.push(`modes[${indice}].${campo}: unknown field.`);\n }\n if (typeof value.id !== "string" || value.id.length > 32 || !ID_INTERNO.test(value.id)) {\n errori.push(`modes[${indice}].id: must be 1-32 lowercase letters, digits, or internal hyphens.`);\n continue;\n }\n if (ids.has(value.id)) {\n errori.push(`modes[${indice}].id: duplicate mode ${value.id}.`);\n continue;\n }\n ids.add(value.id);\n const modo = { id: value.id };\n for (const [key2, max] of [["label", 48], ["instructions", 160]]) {\n const text = testoFacoltativo(value, key2, max, `modes[${indice}]`, errori);\n if (text !== void 0) modo[key2] = text;\n }\n if (value.execution !== void 0) {\n if (value.execution !== "local" && value.execution !== "room") errori.push(`modes[${indice}].execution: must be local or room.`);\n else modo.execution = value.execution;\n }\n if (overlay !== null && modo.execution === void 0) errori.push(`modes[${indice}].execution: is required with the standard overlay.`);\n if (value.players !== void 0) {\n const campo = `modes[${indice}].players`;\n const range = oggetto(value.players);\n if (range === null) errori.push(`${campo}: must be an object with min and max.`);\n else {\n for (const key2 of Object.keys(range)) {\n if (key2 !== "min" && key2 !== "max") errori.push(`${campo}.${key2}: unknown field.`);\n }\n if (!interoTra(range.min, 1, TETTO_GIOCATORI)) errori.push(`${campo}.min: must be an integer from 1 to ${TETTO_GIOCATORI}.`);\n if (!interoTra(range.max, 1, TETTO_GIOCATORI)) errori.push(`${campo}.max: must be an integer from 1 to ${TETTO_GIOCATORI}.`);\n if (interoTra(range.min, 1, TETTO_GIOCATORI) && interoTra(range.max, 1, TETTO_GIOCATORI)) {\n if (range.min > range.max) errori.push(`${campo}.max: must be greater than or equal to min.`);\n else modo.players = { min: range.min, max: range.max };\n }\n }\n }\n if (value.lobby !== void 0) {\n if (typeof value.lobby !== "boolean") errori.push(`modes[${indice}].lobby: must be a boolean.`);\n else modo.lobby = value.lobby;\n }\n if (modo.execution === "local") {\n const range = modo.players ?? players;\n if (range.min !== 1 || range.max !== 1) errori.push(`modes[${indice}].players: local execution requires min and max to be 1.`);\n if (modo.lobby ?? lobby) errori.push(`modes[${indice}].lobby: local execution requires false.`);\n if (value.matchmaking !== void 0) errori.push(`modes[${indice}].matchmaking: local execution cannot use matchmaking.`);\n }\n if (value.matchmaking === void 0) {\n modes.push(modo);\n continue;\n }\n const matchmaking = oggetto(value.matchmaking);\n if (matchmaking === null) {\n errori.push(`modes[${indice}].matchmaking: must be an object.`);\n continue;\n }\n for (const campo of Object.keys(matchmaking)) {\n if (!["key", "timeoutMs", "defaults"].includes(campo)) {\n errori.push(`modes[${indice}].matchmaking.${campo}: unknown field.`);\n }\n }\n let valido = true;\n const key = [];\n if (!Array.isArray(matchmaking.key) || matchmaking.key.length < 1 || matchmaking.key.length > 8) {\n errori.push(`modes[${indice}].matchmaking.key: must contain from 1 to 8 fields.`);\n valido = false;\n } else for (const [keyIndice, item] of matchmaking.key.entries()) {\n if (typeof item !== "string" || !CAMPO_MATCHMAKING.test(item)) {\n errori.push(`modes[${indice}].matchmaking.key[${keyIndice}]: must be 1-32 lowercase letters, digits, or hyphens and start with a letter or digit.`);\n valido = false;\n } else if (key.includes(item)) {\n errori.push(`modes[${indice}].matchmaking.key[${keyIndice}]: duplicate field ${item}.`);\n valido = false;\n } else key.push(item);\n }\n if (!interoTra(matchmaking.timeoutMs, 1e3, 3e5)) {\n errori.push(`modes[${indice}].matchmaking.timeoutMs: must be an integer from 1000 to 300000.`);\n valido = false;\n }\n let defaults;\n if (matchmaking.defaults !== void 0) {\n const values = oggetto(matchmaking.defaults);\n if (values === null || Object.keys(values).length !== key.length || key.some((field) => !Object.hasOwn(values, field))) {\n errori.push(`modes[${indice}].matchmaking.defaults: must contain exactly the declared key fields.`);\n } else {\n defaults = {};\n for (const [field, value2] of Object.entries(values)) {\n if (!(typeof value2 === "string" && value2.length >= 1 && value2.length <= 64 && /^[A-Za-z0-9_.:-]+$/.test(value2)) && !Number.isSafeInteger(value2)) {\n errori.push(`modes[${indice}].matchmaking.defaults.${field}: must be a string of 1-64 characters or a safe integer.`);\n } else Object.defineProperty(defaults, field, { value: value2, enumerable: true });\n }\n }\n }\n if (valido) modes.push({ ...modo, matchmaking: {\n ...defaults === void 0 ? {} : { defaults },\n key,\n timeoutMs: matchmaking.timeoutMs\n } });\n }\n }\n }\n if (overlay !== null && modes.length === 0) errori.push("modes: at least one explicit mode is required with the standard overlay.");\n if (errori.length > 0) return { ok: false, errori };\n return { ok: true, manifest: {\n manifest: 1,\n overlay,\n id,\n name,\n description,\n cover,\n card,\n icon,\n screenshots,\n tags,\n languages: languages2,\n language,\n platform,\n orientation,\n input,\n visibility,\n network,\n isolated,\n requires,\n players,\n lobby,\n persistent,\n spectators,\n boards,\n roles,\n teams,\n voice,\n modes\n } };\n}\n\n// ../contracts/src/overlay.ts\nvar OVERLAY_PANELS = ["home", "room", "invite", "friends", "voice", "boards"];\nfunction overlayConfiguration(manifest, coverUrl = null, invite = null, iconUrl = null) {\n const validated = validaManifest(manifest);\n if (!validated.ok) throw new Error("The overlay manifest is invalid.");\n return { manifest: validated.manifest, coverUrl, iconUrl, invite };\n}\nfunction record(value) {\n return typeof value === "object" && value !== null && !Array.isArray(value) ? value : null;\n}\nfunction validSafeArea(value) {\n const area = record(value);\n return area !== null && Object.keys(area).length === 4 && ["top", "right", "bottom", "left"].every((key) => typeof area[key] === "number" && Number.isFinite(area[key]) && Number(area[key]) >= 0 && Number(area[key]) <= 1e5);\n}\nfunction validOverlayView(value) {\n const data = record(value);\n return data !== null && Object.keys(data).every((key) => ["inputBlocked", "reservedRects", "safeArea", "shortcutEnabled"].includes(key)) && (data.safeArea === void 0 || validSafeArea(data.safeArea)) && (data.shortcutEnabled === void 0 || typeof data.shortcutEnabled === "boolean") && typeof data.inputBlocked === "boolean" && Array.isArray(data.reservedRects) && data.reservedRects.length <= 8 && data.reservedRects.every((value2) => {\n const rect = record(value2);\n return rect !== null && Object.keys(rect).length === 4 && ["x", "y", "width", "height"].every((key) => typeof rect[key] === "number" && Number.isFinite(rect[key]) && rect[key] >= 0 && rect[key] <= 1e5);\n });\n}\nfunction validOverlayRequest(value) {\n const message = record(value), args = record(message?.args);\n if (message?.type !== "caisual:overlay" || message.v !== 1 || typeof message.epoch !== "string" || message.epoch.length < 1 || message.epoch.length > 128 || typeof message.requestId !== "string" || !(/^[1-9][0-9]{0,15}$/.test(message.requestId) && Number.isSafeInteger(Number(message.requestId))) || args === null) return false;\n if (Object.keys(message).some((key) => !["type", "v", "epoch", "requestId", "sessionId", "op", "args"].includes(key)) || !(message.sessionId === void 0 || message.sessionId === null || typeof message.sessionId === "string" && /^[1-9][0-9]{0,15}$/.test(message.sessionId))) return false;\n const keys = (...allowed) => Object.keys(args).every((key) => allowed.includes(key));\n const text = (key) => typeof args[key] === "string" && args[key].length >= 1 && args[key].length <= 64;\n switch (message.op) {\n case "local.start":\n return keys("mode") && text("mode");\n case "room.create":\n return keys("mode") && (args.mode === null || text("mode"));\n case "room.join":\n return keys("code") && (args.code === void 0 || text("code"));\n case "room.watch":\n return keys("code") && text("code");\n case "room.match": {\n const key = record(args.key);\n return keys("mode", "key") && text("mode") && (args.key === void 0 || key !== null && Object.keys(key).length <= 8 && Object.values(key).every((v) => typeof v === "string" && v.length >= 1 && v.length <= 64 || typeof v === "number" && Number.isSafeInteger(v)));\n }\n case "room.ready":\n return keys("ready") && typeof args.ready === "boolean";\n case "room.role":\n case "room.requestRole":\n return keys("role") && text("role");\n case "room.team":\n return keys("team") && Number.isInteger(args.team) && args.team >= 1 && args.team <= 24;\n case "room.restart":\n case "room.start":\n case "session.cancel":\n case "session.leave":\n case "session.disconnect":\n case "session.resume":\n return keys();\n case "voice.join":\n case "voice.leave":\n return keys() && typeof message.sessionId === "string";\n case "voice.mute":\n return keys("muted") && typeof args.muted === "boolean" && typeof message.sessionId === "string";\n case "voice.setVolume":\n return keys("playerId", "volume") && typeof message.sessionId === "string" && typeof args.playerId === "string" && args.playerId.length > 0 && args.playerId.length <= 128 && typeof args.volume === "number" && Number.isFinite(args.volume) && args.volume >= 0 && args.volume <= 1;\n case "overlay.view":\n return validOverlayView(args);\n default:\n return false;\n }\n}\nfunction validBoardDay(value) {\n if (typeof value !== "string" || !/^\\d{4}-\\d{2}-\\d{2}$/.test(value)) return false;\n const at = Date.parse(`${value}T00:00:00Z`);\n return Number.isFinite(at) && new Date(at).toISOString().slice(0, 10) === value;\n}\nfunction validOverlaySessionState(value) {\n const data = record(value);\n const exact = (v, keys) => v !== null && Object.keys(v).length === keys.length && Object.keys(v).every((key) => keys.includes(key));\n const text = (v) => typeof v === "string" && v.length <= 128;\n const nullable = (v) => v === null || text(v);\n const finite = (v) => typeof v === "number" && Number.isFinite(v);\n if (!data || !exact(data, ["kind", "id", "mode", "localStatus", "ready", "capabilities", "room", "waiting", "resume", "resumeError", ..."voice" in data ? ["voice"] : []])) return false;\n if (data.voice !== void 0 && data.voice !== null && (data.kind !== "room" || !record(data.room) || !validOverlayVoice(data.voice))) return false;\n const capabilities = record(data.capabilities), room = record(data.room), waiting = record(data.waiting), resume = record(data.resume);\n if (!["boot", "home", "attaching", "matching", "local", "room", "watch"].includes(String(data.kind)) || !nullable(data.id) || !nullable(data.mode) || ![null, "playing", "ended"].includes(data.localStatus) || typeof data.ready !== "boolean" || typeof data.resumeError !== "boolean" || !exact(capabilities, ["local", "rooms", "overlay", "requestRole"]) || !Object.values(capabilities).every((v) => typeof v === "boolean")) return false;\n if (data.waiting !== null && (!exact(waiting, ["players", "min", "max"]) || !Object.values(waiting).every((v) => Number.isInteger(v) && Number(v) >= 0 && Number(v) <= 24))) return false;\n if (data.resume !== null && (!exact(resume, ["version", "code", "mode", "updatedAt"]) || resume.version !== 1 || !text(resume.code) || !nullable(resume.mode) || !finite(resume.updatedAt))) return false;\n if (data.room === null) return true;\n if (!exact(room, ["code", "mode", "status", "host", "you", "players", "countdownAt", "connection", "closedCode", "limits", "lobby", "persistent", "delayMs", "requestRole", ..."result" in (room ?? {}) ? ["result"] : [], ..."rematch" in (room ?? {}) ? ["rematch"] : []]) || !room) return false;\n const limits = record(room.limits), rematch = record(room.rematch);\n if (room.rematch !== void 0 && room.rematch !== null && (!exact(rematch, ["keepSetup", "autoStart"]) || typeof rematch.keepSetup !== "boolean" || typeof rematch.autoStart !== "boolean")) return false;\n return text(room.code) && nullable(room.mode) && nullable(room.host) && nullable(room.you) && ["lobby", "countdown", "playing", "finished", "ended"].includes(String(room.status)) && ["connecting", "connected", "reconnecting", "disconnected", "ended", "closed", "replaced"].includes(String(room.connection)) && ["countdownAt", "closedCode", "delayMs"].every((key) => room[key] === null || finite(room[key])) && ["lobby", "persistent", "requestRole"].every((key) => typeof room[key] === "boolean") && exact(limits, ["min", "max"]) && Object.values(limits).every((v) => Number.isInteger(v) && Number(v) >= 1 && Number(v) <= 24) && Array.isArray(room.players) && room.players.length <= 24 && room.players.every((value2) => {\n const player = record(value2);\n return exact(player, ["id", "name", "guest", "role", "team", "ready", "connected"]) && player !== null && text(player.id) && text(player.name) && nullable(player.role) && (player.team === null || Number.isInteger(player.team) && Number(player.team) >= 1 && Number(player.team) <= 24) && ["guest", "ready", "connected"].every((key) => typeof player[key] === "boolean");\n });\n}\nfunction validOverlayVoice(value) {\n const voice = record(value);\n if (!voice || Object.keys(voice).length !== 6 || !["mode", "state", "mic", "muted", "speaking", "peers"].every((key) => key in voice) || !["room", "team", "proximity"].includes(String(voice.mode)) || !["off", "joining", "on", "reconnecting"].includes(String(voice.state)) || !["mic", "muted", "speaking"].every((key) => typeof voice[key] === "boolean") || !Array.isArray(voice.peers) || voice.peers.length > 24) return false;\n const ids = /* @__PURE__ */ new Set();\n return voice.peers.every((value2) => {\n const peer = record(value2);\n if (!peer || Object.keys(peer).length !== 5 || !["id", "mic", "muted", "speaking", "volume"].every((key) => key in peer) || typeof peer.id !== "string" || !peer.id.length || peer.id.length > 128 || ids.has(peer.id) || !["mic", "muted", "speaking"].every((key) => typeof peer[key] === "boolean") || typeof peer.volume !== "number" || !Number.isFinite(peer.volume) || peer.volume < 0 || peer.volume > 1) return false;\n ids.add(peer.id);\n return true;\n });\n}\n\n// ../contracts/src/match-result.ts\nfunction readMatchResult(value, playerIds) {\n const object = (v) => v !== null && typeof v === "object" && !Array.isArray(v) ? v : null;\n const result = object(value);\n if (!result || !Array.isArray(result.standings)) return null;\n const known = new Set(playerIds), seen = /* @__PURE__ */ new Set();\n const standings = [];\n for (const item of result.standings) {\n const row = object(item);\n if (!row || typeof row.playerId !== "string" || !known.has(row.playerId) || seen.has(row.playerId)) continue;\n seen.add(row.playerId);\n standings.push({\n playerId: row.playerId,\n ...typeof row.score === "number" && Number.isFinite(row.score) ? { score: row.score } : {},\n ...typeof row.rank === "number" && Number.isSafeInteger(row.rank) && row.rank > 0 ? { rank: row.rank } : {}\n });\n }\n if (!standings.length) return null;\n return {\n standings,\n ...Array.isArray(result.winners) ? { winners: [...new Set(result.winners.filter((id) => typeof id === "string" && seen.has(id)))] } : {},\n ...typeof result.draw === "boolean" ? { draw: result.draw } : {},\n ...typeof result.unit === "string" ? { unit: result.unit } : {}\n };\n}\n\n// src/errors.ts\nfunction creaErrore(code, message) {\n return Object.assign(new Error(message), { name: "CaisualError", code });\n}\n\n// src/overlay/host-bridge.ts\nfunction eMessaggioReady(value) {\n return record(value)?.type === "caisual:ready";\n}\nfunction eRichiestaBiglietto(value) {\n const data = record(value);\n return data?.type === "caisual:ticket" && (data.aud === void 0 || data.aud === "portal" || data.aud === "live");\n}\nfunction stanzaDaMessaggio(value) {\n const data = record(value);\n if (data?.type !== "caisual:room") return void 0;\n if (data.room === null) return null;\n const room = record(data.room);\n return typeof room?.code === "string" && /^[ABCDEFGHJKLMNPQRSTUVWXYZ23456789]{6}$/.test(room.code) ? { code: room.code } : void 0;\n}\nfunction creaPonteOspite(input) {\n let port = null, epoch = null, instance = null;\n let disposed = false, legacyReady = true, sequence = 0, requestId = 0;\n let state = null, clockOffset = null;\n let polling = null, pollingEnd = null;\n const pending = /* @__PURE__ */ new Map();\n const states = /* @__PURE__ */ new Set();\n const shortcuts = /* @__PURE__ */ new Set();\n const opens = /* @__PURE__ */ new Set();\n const errors = /* @__PURE__ */ new Set();\n const scores = /* @__PURE__ */ new Set();\n const notify = (listeners, value) => {\n for (const listener of listeners) try {\n listener(value);\n } catch {\n }\n };\n const rejectPending = () => {\n for (const value of pending.values()) {\n input.finestra.clearTimeout(value.timer);\n value.reject(creaErrore("session_replaced", "The game document changed."));\n }\n pending.clear();\n };\n const stopPolling = () => {\n if (polling !== null) input.finestra.clearInterval(polling);\n if (pollingEnd !== null) input.finestra.clearTimeout(pollingEnd);\n polling = pollingEnd = null;\n };\n const askReady = () => {\n if (!disposed && input.frame.src !== "") input.frame.contentWindow?.postMessage({ type: "caisual:ready?" }, input.origineGioco);\n };\n const poll = () => {\n stopPolling();\n polling = input.finestra.setInterval(askReady, 500);\n pollingEnd = input.finestra.setTimeout(stopPolling, 1e4);\n askReady();\n };\n const loaded = () => {\n legacyReady = true;\n poll();\n };\n const listen = (event) => {\n if (disposed || event.origin !== input.origineGioco || event.source !== input.frame.contentWindow || !eMessaggioReady(event.data)) return;\n const data = record(event.data);\n const nextInstance = typeof data.instance === "string" && data.instance.length <= 128 ? data.instance : null;\n if (port && (nextInstance !== null ? nextInstance === instance : !legacyReady)) return;\n stopPolling();\n legacyReady = false;\n instance = nextInstance;\n rejectPending();\n port?.close();\n input.onRoom(null);\n epoch = input.epoch?.() ?? crypto.randomUUID();\n sequence = requestId = 0;\n state = null;\n clockOffset = null;\n notify(states, null);\n const channel = input.creaCanale?.() ?? new MessageChannel();\n const currentPort = channel.port1, currentEpoch = epoch;\n port = currentPort;\n const current = () => !disposed && port === currentPort && epoch === currentEpoch;\n currentPort.onmessage = (event2) => {\n if (!current()) return;\n const data2 = record(event2.data);\n if (eRichiestaBiglietto(data2)) {\n const aud = data2?.aud === "live" ? "live" : "portal";\n void input.rinnova(aud).then((ticket) => {\n if (current()) currentPort.postMessage({ type: "caisual:ticket", aud, ticket });\n }).catch(() => {\n if (current()) currentPort.postMessage({ type: "caisual:ticket", aud, error: "offline" });\n });\n return;\n }\n const room = stanzaDaMessaggio(data2);\n if (room !== void 0) {\n input.onRoom(room);\n return;\n }\n if (data2?.v !== 1 || data2.epoch !== currentEpoch) return;\n if (data2.type === "caisual:overlay-response" && typeof data2.requestId === "string") {\n const item = pending.get(data2.requestId);\n if (!item) return;\n if (data2.ok !== true && (data2.ok !== false || typeof record(data2.error)?.code !== "string" || typeof record(data2.error)?.message !== "string")) return;\n pending.delete(data2.requestId);\n input.finestra.clearTimeout(item.timer);\n const response = data2;\n if (response.ok) item.resolve();\n else item.reject(creaErrore(response.error.code, response.error.message));\n } else if (data2.type === "caisual:overlay-state" && Number.isSafeInteger(data2.seq) && data2.seq > sequence) {\n if (!validOverlaySessionState(data2.state) || typeof data2.serverTime !== "number" || !Number.isFinite(data2.serverTime)) return;\n clockOffset = data2.serverTime - Date.now();\n sequence = data2.seq;\n state = structuredClone(data2.state);\n notify(states, state);\n } else if (data2.type === "caisual:overlay-error" && data2.sessionId === state?.id) {\n const error = record(data2.error);\n if (typeof error?.code === "string" && typeof error.message === "string") notify(errors, { sessionId: data2.sessionId, error: { code: error.code, message: error.message } });\n } else if (data2.type === "caisual:overlay-shortcut") {\n notify(shortcuts, void 0);\n } else if (data2.type === "caisual:overlay-open" && OVERLAY_PANELS.includes(data2.panel)) {\n notify(opens, data2.panel);\n } else if (data2.type === "caisual:overlay-score") {\n const score = record(data2.score);\n if (score && typeof score.board === "string" && typeof score.player === "string" && Number.isSafeInteger(score.score) && Number.isFinite(score.submittedAt) && (score.day === null || typeof score.day === "string")) {\n notify(scores, { board: score.board, player: score.player, score: score.score, day: score.day, submittedAt: score.submittedAt });\n }\n }\n };\n currentPort.start();\n input.frame.contentWindow?.postMessage({\n type: "caisual:hello",\n ticket: input.ticket,\n live: input.origineLive,\n invite: input.invite,\n // language resta disponibile ai kit pubblicati prima della separazione delle lingue.\n ...input.language ? { language: input.language, uiLanguage: input.language } : {},\n ...input.languagePreferences ? { languagePreferences: input.languagePreferences } : {},\n ...input.configuration ? { gameLanguages: manifestLanguages(input.configuration.manifest) } : {},\n ...input.configuration && data.overlayVersion === 1 ? { overlay: { v: 1, epoch, configuration: input.configuration } } : {}\n }, input.origineGioco, [channel.port2]);\n };\n input.finestra.addEventListener("message", listen);\n input.frame.addEventListener?.("load", loaded);\n poll();\n return {\n get epoch() {\n return epoch;\n },\n serverTime() {\n return clockOffset === null ? null : Date.now() + clockOffset;\n },\n get state() {\n return state === null ? null : structuredClone(state);\n },\n subscribe(listener) {\n states.add(listener);\n listener(state);\n return () => {\n states.delete(listener);\n };\n },\n onShortcut(listener) {\n shortcuts.add(listener);\n return () => {\n shortcuts.delete(listener);\n };\n },\n onOpen(listener) {\n opens.add(listener);\n return () => {\n opens.delete(listener);\n };\n },\n onError(listener) {\n errors.add(listener);\n return () => {\n errors.delete(listener);\n };\n },\n onScore(listener) {\n scores.add(listener);\n return () => {\n scores.delete(listener);\n };\n },\n request(op, args) {\n if (!port || !epoch || disposed) return Promise.reject(creaErrore("offline", "The game bridge is not connected."));\n if (pending.size >= 32) return Promise.reject(creaErrore("rate_limited", "Too many overlay requests."));\n const id = String(++requestId), request = {\n type: "caisual:overlay",\n v: 1,\n epoch,\n requestId: id,\n op,\n args,\n ...["room.ready", "room.role", "room.requestRole", "room.team", "room.start", "room.restart", "session.leave", "session.disconnect", "voice.join", "voice.mute", "voice.leave", "voice.setVolume"].includes(op) ? { sessionId: state?.id ?? null } : {}\n };\n if (!validOverlayRequest(request)) return Promise.reject(creaErrore("invalid_request", "The overlay request is invalid."));\n return new Promise((resolve, reject) => {\n const timeout = op === "room.match" ? 31e4 : input.requestTimeoutMs ?? 15e3;\n const timer = input.finestra.setTimeout(() => {\n pending.delete(id);\n reject(creaErrore("timeout", "The overlay request timed out."));\n }, timeout);\n pending.set(id, { resolve, reject, timer });\n try {\n port.postMessage(request);\n } catch (error) {\n input.finestra.clearTimeout(timer);\n pending.delete(id);\n reject(error);\n }\n });\n },\n dispose() {\n disposed = true;\n stopPolling();\n rejectPending();\n port?.close();\n port = null;\n input.finestra.removeEventListener("message", listen);\n input.frame.removeEventListener?.("load", loaded);\n states.clear();\n opens.clear();\n shortcuts.clear();\n scores.clear();\n errors.clear();\n }\n };\n}\nfunction avviaHandshake(input) {\n const bridge = creaPonteOspite(input);\n return () => bridge.dispose();\n}\nfunction gameViewport(frame) {\n const rect = frame.getBoundingClientRect();\n const zoomX = frame.offsetWidth ? rect.width / frame.offsetWidth : 1;\n const zoomY = frame.offsetHeight ? rect.height / frame.offsetHeight : 1;\n const left = rect.left + frame.clientLeft * zoomX, top = rect.top + frame.clientTop * zoomY;\n return {\n left,\n top,\n right: left + frame.clientWidth * zoomX,\n bottom: top + frame.clientHeight * zoomY,\n scaleX: zoomX ? 1 / zoomX : 1,\n scaleY: zoomY ? 1 / zoomY : 1\n };\n}\nfunction measureSafeArea(frame, probe) {\n const win = frame.ownerDocument.defaultView, css = win.getComputedStyle(probe), viewport = gameViewport(frame);\n const clamp = (value, max) => Math.max(0, Math.min(max, value));\n return {\n top: clamp(((parseFloat(css.paddingTop) || 0) - viewport.top) * viewport.scaleY, frame.clientHeight),\n right: clamp((viewport.right - (win.innerWidth - (parseFloat(css.paddingRight) || 0))) * viewport.scaleX, frame.clientWidth),\n bottom: clamp((viewport.bottom - (win.innerHeight - (parseFloat(css.paddingBottom) || 0))) * viewport.scaleY, frame.clientHeight),\n left: clamp(((parseFloat(css.paddingLeft) || 0) - viewport.left) * viewport.scaleX, frame.clientWidth)\n };\n}\n\n// src/overlay/boards.ts\nfunction createBoardController(input) {\n let disposed = false, generation = 0, timer;\n const seen = /* @__PURE__ */ new Set();\n let query = null, data = null, error = false, loading = false;\n let queued = null, saving = null, reads = 0;\n const later = input.later ?? setTimeout, clear = input.clear ?? clearTimeout;\n const cancel = () => {\n if (timer !== void 0) clear(timer);\n timer = void 0;\n };\n const notify = () => {\n if (!disposed) input.changed();\n };\n const matches = () => queued && query?.board === queued.board && query.period === (queued.day ? "daily" : "all-time") && (query.day ?? queued.day) === queued.day;\n const refresh = async () => {\n if (!query || disposed) return;\n cancel();\n const current = ++generation, selected = { ...query };\n loading = true;\n error = false;\n notify();\n try {\n const result = await input.read(selected);\n if (disposed || current !== generation) return;\n data = result;\n if (matches()) {\n const own = result.me;\n if (own?.verified && own.score >= queued.score) saving = own.score === queued.score ? "saved" : "bestAlready";\n }\n } catch {\n if (!disposed && current === generation) error = true;\n }\n if (disposed || current !== generation) return;\n loading = false;\n if (matches() && saving !== "saved" && saving !== "bestAlready") {\n reads++;\n if (reads < 4) {\n saving = "saving";\n timer = later(() => {\n void refresh();\n }, [800, 1600, 3200][reads - 1]);\n } else saving = "refreshHint";\n }\n notify();\n };\n return {\n get state() {\n return { query, data, loading, error, saving: matches() ? saving : null };\n },\n select(next) {\n if (JSON.stringify(next) === JSON.stringify(query)) return;\n cancel();\n generation++;\n query = { ...next };\n data = null;\n reads = 0;\n if (matches()) saving = "saving";\n void refresh();\n },\n queued(score) {\n const board = input.manifest.boards[score.board];\n if (score.player !== input.player || !board || !Number.isSafeInteger(score.score) || score.score < 0 || score.day !== null && !validBoardDay(score.day) || !(board.periods ?? ["all-time"]).includes(score.day ? "daily" : "all-time")) return;\n const signature = JSON.stringify(score);\n if (seen.has(signature)) return;\n seen.add(signature);\n if (seen.size > 64) seen.delete(seen.values().next().value);\n queued = score;\n saving = "saving";\n reads = 0;\n this.select({ board: score.board, period: score.day ? "daily" : "all-time", guests: query?.guests ?? input.guests ?? false, ...score.day ? { day: score.day } : {} });\n if (!loading) void refresh();\n notify();\n },\n refresh,\n reset() {\n seen.clear();\n cancel();\n generation++;\n query = null;\n data = null;\n queued = null;\n saving = null;\n loading = false;\n error = false;\n },\n dispose() {\n disposed = true;\n cancel();\n generation++;\n }\n };\n}\n\n// src/overlay/i18n.ts\nvar languages = ["en", "it", "es", "fr", "de", "pt"];\nvar words = {\n gameLanguages: ["Game languages", "Lingue del gioco", "Idiomas del juego", "Langues du jeu", "Spielsprachen", "Idiomas do jogo"],\n loading: ["Loading game...", "Caricamento...", "Cargando...", "Chargement...", "Spiel wird geladen...", "Carregando..."],\n loadingSlow: ["This game is taking longer than expected. You can wait a little longer or try again.", "Il gioco ci sta mettendo pi\\xF9 del previsto. Puoi aspettare ancora un po\\u2019 o riprovare.", "El juego est\\xE1 tardando m\\xE1s de lo esperado. Puedes esperar un poco m\\xE1s o volver a intentarlo.", "Le jeu met plus de temps que pr\\xE9vu. Vous pouvez patienter encore un peu ou r\\xE9essayer.", "Das Spiel braucht l\\xE4nger als erwartet. Du kannst noch etwas warten oder es erneut versuchen.", "O jogo est\\xE1 demorando mais do que o esperado. Voc\\xEA pode esperar mais um pouco ou tentar novamente."],\n home: ["Play", "Gioca", "Jugar", "Jouer", "Spielen", "Jogar"],\n homeMenu: ["Menu", "Menu", "Men\\xFA", "Menu", "Men\\xFC", "Menu"],\n mode: ["Mode", "Modalit\\xE0", "Modo", "Mode", "Modus", "Modo"],\n play: ["Play", "Gioca", "Jugar", "Jouer", "Spielen", "Jogar"],\n friendsPlay: ["Play with friends", "Gioca con amici", "Jugar con amigos", "Jouer entre amis", "Mit Freunden spielen", "Jogar com amigos"],\n find: ["Find players", "Trova giocatori", "Buscar jugadores", "Trouver des joueurs", "Spieler finden", "Buscar jogadores"],\n join: ["Join with code", "Entra con codice", "Entrar con c\\xF3digo", "Rejoindre avec un code", "Mit Code beitreten", "Entrar com c\\xF3digo"],\n joinInvite: ["Join this room", "Entra in questa stanza", "Entrar en est\\xE1 sala", "Rejoindre cette salle", "Diesem Raum beitreten", "Entrar nest\\xE1 sala"],\n watch: ["Watch a room", "Guarda una stanza", "Observar una sala", "Regarder une salle", "Raum ansehen", "Assistir a uma sala"],\n resume: ["Resume", "Riprendi", "Continuar", "Reprendre", "Fortsetzen", "Continuar"],\n room: ["Room", "Stanza", "Sala", "Salle", "Raum", "Sala"],\n code: ["Room code", "Codice stanza", "C\\xF3digo de sala", "Code de salle", "Raumcode", "C\\xF3digo da sala"],\n copy: ["Copy invite", "Copia invito", "Copiar invitaci\\xF3n", "Copier le lien", "Einladung kopieren", "Copiar convite"],\n copied: ["Invite copied", "Invito copiato", "Invitacion copiada", "Lien copi\\xE9", "Einladung kopiert", "Convite copiado"],\n copyFailed: ["Copy this link:", "Copia questo link:", "Copia este enlace:", "Copiez ce lien :", "Diesen Link kopieren:", "Copie este link:"],\n joining: ["Joining room...", "Ingresso nella stanza...", "Entrando en la sala...", "Connexion \\xE0 la salle...", "Raum wird betreten...", "Entrando na sala..."],\n matching: ["Finding your people...", "Ricerca giocatori...", "Buscando jugadores...", "Recherche de joueurs...", "Spieler werden gesucht...", "Buscando jogadores..."],\n queue: ["{n} / {max} players", "{n} / {max} giocatori", "{n} / {max} jugadores", "{n} / {max} joueurs", "{n} / {max} Spieler", "{n} / {max} jogadores"],\n cancel: ["Cancel", "Annulla", "Cancelar", "Annuler", "Abbrechen", "Cancelar"],\n close: ["Close", "Chiudi", "Cerrar", "Fermer", "Schlie\\xDFen", "Fechar"],\n back: ["Back", "Indietro", "Volver", "Retour", "Zur\\xFCck", "Voltar"],\n ready: ["Ready", "Pronto", "Listo", "Pr\\xEAt", "Bereit", "Pronto"],\n unready: ["Not ready", "Non pronto", "No listo", "Pas pr\\xEAt", "Nicht bereit", "N\\xE3o pronto"],\n start: ["Start", "Inizia", "Empezar", "Commencer", "Starten", "Come\\xE7ar"],\n role: ["Role", "Ruolo", "Rol", "R\\xF4le", "Rolle", "Fun\\xE7\\xE3o"],\n team: ["Team", "Squadra", "Equipo", "\\xC9quipe", "Team", "Equipe"],\n host: ["Host", "Host", "Anfitrion", "H\\xF4te", "Host", "Anfitri\\xE3o"],\n you: ["You", "Tu", "T\\xFA", "Vous", "Du", "Voc\\xEA"],\n away: ["Away", "Assente", "Ausente", "Absent", "Abwesend", "Ausente"],\n needPlayers: ["Waiting for more players", "In attesa di giocatori", "Esperando m\\xE1s jugadores", "En attente de joueurs", "Weitere Spieler fehlen", "Esperando mais jogadores"],\n needReady: ["Everyone needs to be ready", "Tutti devono essere pronti", "Todos deben estar listos", "Tout le monde doit \\xEAtre pr\\xEAt", "Alle m\\xFCssen bereit sein", "Todos precisam estar prontos"],\n needRoles: ["Fill the required roles", "Completa i ruoli richiesti", "Completa los roles", "Compl\\xE9tez les r\\xF4les", "Ben\\xF6tigte Rollen besetzen", "Complete as fun\\xE7\\xF5es"],\n needTeams: ["Choose the required teams", "Scegli le squadre richieste", "Elige los equipos", "Choisissez les \\xE9quipes", "Teams auswahlen", "Escolha as equipes"],\n waitHost: ["Waiting for the host", "In attesa dell\'host", "Esperando al anfitrion", "En attente de l\\u2019h\\xF4te", "Warten auf den Host", "Esperando o anfitri\\xE3o"],\n starting: ["Starting in", "Si inizia tra", "Empieza en", "D\\xE9but dans", "Start in", "Come\\xE7a em"],\n playing: ["Playing", "In partita", "Jugando", "En jeu", "Im Spiel", "Jogando"],\n ended: ["Game finished", "Partita conclusa", "Partida terminada", "Partie termin\\xE9e", "Spiel beendet", "Partida encerrada"],\n rematchReady: ["{n}/{max} ready", "{n}/{max} pronti", "{n}/{max} listos", "{n}/{max} pr\\xEAts", "{n}/{max} bereit", "{n}/{max} prontos"],\n rematchStart: ["Start rematch", "Avvia rivincita", "Iniciar revancha", "Lancer la revanche", "Revanche starten", "Iniciar revanche"],\n won: ["You won", "Hai vinto", "Has ganado", "Vous avez gagn\\xE9", "Du hast gewonnen", "Voc\\xEA venceu"],\n lost: ["You lost", "Hai perso", "Has perdido", "Vous avez perdu", "Du hast verloren", "Voc\\xEA perdeu"],\n draw: ["Draw", "Pareggio", "Empate", "\\xC9galit\\xE9", "Unentschieden", "Empate"],\n standings: ["Standings", "Piazzamenti", "Posiciones", "R\\xE9sultats", "Platzierungen", "Coloca\\xE7\\xF5es"],\n points: ["points", "punti", "puntos", "points", "Punkte", "pontos"],\n time: ["time", "tempo", "tiempo", "temps", "Zeit", "tempo"],\n distance: ["distance", "distanza", "distancia", "distance", "Distanz", "dist\\xE2ncia"],\n again: ["Play again", "Gioca ancora", "Jugar de nuevo", "Rejouer", "Erneut spielen", "Jogar novamente"],\n newRoom: ["New room. Share the new invite.", "Nuova stanza. Condividi il nuovo invito.", "Nueva sala. Comparte la invitaci\\xF3n.", "Nouvelle salle. Partagez le lien.", "Neuer Raum. Neue Einladung teilen.", "Nova sala. Compartilhe o convite."],\n watching: ["Watching", "In osservazione", "Observando", "Spectateur", "Zuschauen", "Assistindo"],\n delay: ["{n}s delay", "Ritardo {n}s", "Retraso de {n}s", "Retard de {n}s", "{n}s Verz\\xF6gerung", "Atraso de {n}s"],\n exit: ["Exit", "Esci", "Salir", "Quitter", "Verlassen", "Sair"],\n leaveNow: ["Leave for now", "Esci per ora", "Salir por ahora", "Quitter pour le moment", "Vor\\xFCbergehend verlassen", "Sair por enquanto"],\n leaveRoom: ["Leave room", "Lascia la stanza", "Abandonar sala", "Abandonner la salle", "Raum verlassen", "Deixar a sala"],\n leaveHint: ["Your room stays available for Resume.", "La stanza resta disponibile con Riprendi.", "Podr\\xE1s volver a est\\xE1 sala.", "Vous pourrez reprendre cette salle.", "Du kannst den Raum fortsetzen.", "Voc\\xEA pode voltar a est\\xE1 sala."],\n temporaryHint: ["The game continues. Rejoining may only be possible briefly.", "La partita continua. Il rientro pu\\xF2 essere disponibile solo per poco.", "La partida continua. Volver puede ser posible solo por poco tiempo.", "La partie continue. Le retour peut \\xEAtre limit\\xE9.", "Das Spiel l\\xE4uft weiter. R\\xFCckkehr nur kurz m\\xF6glich.", "A partida continua. O retorno pode ser limitado."],\n abandonHint: ["Leave room gives up your place.", "Lascia la stanza libera il tuo posto.", "Abandonar libera tu plaza.", "Abandonner lib\\xE8re votre place.", "Raum verlassen gibt deinen Platz frei.", "Deixar a sala libera sua vaga."],\n reconnecting: ["Reconnecting...", "Riconnessione...", "Reconectando...", "Reconnexion...", "Verbindung wird erneuert...", "Reconectando..."],\n replaced: ["Opened in another tab", "Aperta in un\\u2019altra scheda", "Abierta en otra pest\\xE1na", "Ouverte dans un autre onglet", "In anderem Tab ge\\xF6ffnet", "Aberta em outra aba"],\n error: ["Something went wrong. Try again.", "Qualcosa non va. Riprova.", "Algo sali\\xF3 mal. Reintenta.", "Une erreur est survenue. R\\xE9essayez.", "Etwas ist schiefgelaufen. Erneut versuchen.", "Algo deu errado. Tente novamente."],\n noRoom: ["This room is no longer available.", "Questa stanza non \\xE8 pi\\xF9 disponibile.", "Esta sala ya no est\\xE1 disponible.", "Cette salle n\'est plus disponible.", "Dieser Raum ist nicht mehr verf\\xFCgbar.", "Esta sala n\\xE3o est\\xE1 mais disponivel."],\n full: ["This room is full.", "La stanza \\xE8 piena.", "La sala est\\xE1 llena.", "Cette salle est pleine.", "Dieser Raum ist voll.", "Esta sala est\\xE1 cheia."],\n noMatch: ["No match this time. Try again.", "Nessun gruppo trovato. Riprova.", "No hay grupo. Reintenta.", "Aucun groupe trouv\\xE9. R\\xE9essayez.", "Keine Gruppe gefunden. Erneut versuchen.", "Nenhum grupo encontrado. Tente novamente."],\n invalidCode: ["Enter a six-character room code.", "Inserisci un codice di sei caratteri.", "Escribe un c\\xF3digo de seis caracteres.", "Entrez un code de six caracteres.", "Sechsstelligen Raumcode eingeben.", "Digite um c\\xF3digo de seis caracteres."],\n refused: ["The room did not accept that change.", "La stanza ha rifiutato la modifica.", "La sala rechaz\\xF3 el cambio.", "La salle a refus\\xE9 ce changement.", "Der Raum hat die \\xC4nderung abgelehnt.", "A sala recusou a altera\\xE7\\xE3o."],\n unavailable: ["Unavailable right now", "Non disponibile ora", "No disponible ahora", "Indisponible pour le moment", "Derzeit nicht verf\\xFCgbar", "Indisponivel agora"],\n offline: ["Connection unavailable. Try again.", "Connessione non disponibile. Riprova.", "Sin conexi\\xF3n. Reintenta.", "Connexion indisponible. R\\xE9essayez.", "Keine Verbindung. Erneut versuchen.", "Sem conex\\xE3o. Tente novamente."],\n saveFailed: ["Keep the room code. Resume could not be saved.", "Conserva il codice. Riprendi non \\xE8 stato salvato.", "Guarda el c\\xF3digo. No se pudo guardar el regreso.", "Gardez le code. La reprise ne peut pas \\xEAtre enregistr\\xE9e.", "Raumcode aufbewahren. Fortsetzen nicht gespeichert.", "Guarde o c\\xF3digo. O retorno n\\xE3o foi salvo."],\n boards: ["Leaderboard", "Classifica", "Clasificaci\\xF3n", "Classement", "Bestenliste", "Classifica\\xE7\\xE3o"],\n board: ["Board", "Classifica", "Tabla", "Classement", "Bestenliste", "Tabela"],\n daily: ["Daily", "Giornaliera", "Diaria", "Du jour", "T\\xE4glich", "Di\\xE1ria"],\n allTime: ["All time", "Di sempre", "Hist\\xF3rica", "Tous les temps", "Gesamt", "Geral"],\n accounts: ["Accounts", "Account", "Cuentas", "Comptes", "Konten", "Contas"],\n guests: ["Guests", "Ospiti", "Invitados", "Invit\\xE9s", "G\\xE4ste", "Visitantes"],\n category: ["Category", "Categoria", "Categoria", "Cat\\xE9gorie", "Kategorie", "Categoria"],\n period: ["Period", "Periodo", "Per\\xEDodo", "P\\xE9riode", "Zeitraum", "Per\\xEDodo"],\n rank: ["Rank", "Posizione", "Puesto", "Rang", "Platz", "Posicao"],\n score: ["Score", "Punteggio", "Puntos", "Score", "Punkte", "Pontos"],\n verified: ["Verified", "Verificato", "Verificado", "V\\xE9rifi\\xE9", "Verifiziert", "Verificado"],\n own: ["Your best", "Il tuo record", "Tu record", "Votre record", "Dein Rekord", "Seu recorde"],\n empty: ["No scores yet", "Nessun punteggio", "A\\xFAn no hay puntos", "Aucun score", "Noch keine Punkte", "Ainda sem pontos"],\n saving: ["Saving score...", "Salvataggio punteggio...", "Guardando puntos...", "Enregistrement du score...", "Punkte werden gespeichert...", "Salvando pontos..."],\n saved: ["Your best is on the board", "Il tuo record \\xE8 in classifica", "Tu record est\\xE1 en la tabla", "Votre record est au classement", "Dein Rekord ist eingetragen", "Seu recorde est\\xE1 na tabela"],\n bestAlready: ["Your best is already on the board", "Il tuo record era gi\\xE0 in classifica", "Tu record ya estaba en la tabla", "Votre record est d\\xE9j\\xE0 au classement", "Dein Rekord ist bereits eingetragen", "Seu recorde j\\xE1 est\\xE1 na tabela"],\n refresh: ["Refresh", "Aggiorna", "Actualizar", "Actualiser", "Aktualisieren", "Atualizar"],\n refreshHint: ["Score not visible yet. Refresh to check.", "Punteggio non ancora visibile. Aggiorna per controllare.", "Puntos a\\xFAn no visibles. Actualiza.", "Score pas encore visible. Actualisez.", "Punkte noch nicht sichtbar. Aktualisieren.", "Pontos ainda n\\xE3o visiveis. Atualize."],\n friends: ["Friends & party", "Amici e gruppo", "Amigos y grupo", "Amis et groupe", "Freunde & Gruppe", "Amigos e grupo"],\n localCrew: ["Friends and party are unavailable in local preview.", "Amici e gruppo non disponibili in anteprima locale.", "Amigos y grupo no disponibles en la vista local.", "Amis et groupe indisponibles en aper\\xE7u local.", "Freunde und Gruppe in lokaler Vorschau nicht verf\\xFCgbar.", "Amigos e grupo indispon\\xEDveis na pr\\xE9via local."],\n loginCrew: ["Sign in on Caisual to use friends and party.", "Accedi a Caisual per amici e gruppo.", "Inicia sesion para amigos y grupo.", "Connectez-vous pour utiliser amis et groupe.", "F\\xFCr Freunde und Gruppe bei Caisual anmelden.", "Entre no Caisual para amigos e grupo."],\n online: ["Online", "Online", "En linea", "En ligne", "Online", "Online"],\n noFriends: ["No friends online", "Nessun amico online", "Sin amigos en linea", "Aucun ami en ligne", "Keine Freunde online", "Nenhum amigo online"],\n createParty: ["Create party", "Crea gruppo", "Crear grupo", "Cr\\xE9er un groupe", "Gruppe erstellen", "Criar grupo"],\n inviteParty: ["Invite to party", "Invita nel gruppo", "Invitar al grupo", "Inviter au groupe", "In Gruppe einladen", "Convidar para o grupo"],\n leaveParty: ["Leave party", "Lascia gruppo", "Salir del grupo", "Quitter le groupe", "Gruppe verlassen", "Sair do grupo"],\n accept: ["Accept", "Accetta", "Aceptar", "Accepter", "Annehmen", "Aceitar"],\n decline: ["Decline", "Rifiuta", "Rechazar", "Refuser", "Ablehnen", "Recusar"],\n follow: ["Join them", "Raggiungi", "Unirse", "Rejoindre", "Beitreten", "Juntar-se"],\n voice: ["Voice", "Voce", "Voz", "Voix", "Sprache", "Voz"],\n voiceJoin: ["Join voice", "Entra in voce", "Unirse a voz", "Activer la voix", "Sprachchat beitreten", "Entrar na voz"],\n voiceLeave: ["Leave voice", "Esci dalla voce", "Salir de voz", "Quitter la voix", "Sprachchat verlassen", "Sair da voz"],\n voiceMute: ["Mute", "Disattiva microfono", "Silenciar", "Couper le micro", "Stummschalten", "Silenciar"],\n voiceUnmute: ["Unmute", "Attiva microfono", "Activar micr\\xF3fono", "Activer le micro", "Mikrofon aktivieren", "Ativar microfone"],\n voiceOff: ["Voice off", "Voce disattivata", "Voz desactivada", "Voix d\\xE9sactiv\\xE9e", "Sprachchat aus", "Voz desativada"],\n voiceJoining: ["Joining voice...", "Connessione voce...", "Conectando voz...", "Connexion vocale...", "Sprachchat verbindet...", "Conectando voz..."],\n voiceOn: ["Voice connected", "Voce connessa", "Voz conectada", "Voix connect\\xE9e", "Sprachchat verbunden", "Voz conectada"],\n voiceMuted: ["Muted", "Microfono disattivato", "Silenciado", "Micro coup\\xE9", "Stumm", "Silenciado"],\n voiceMic: ["Mic on", "Microfono attivo", "Micr\\xF3fono activo", "Micro actif", "Mikrofon an", "Microfone ativo"],\n voiceListening: ["Listening only", "Solo ascolto", "Solo escucha", "\\xC9coute seule", "Nur zuh\\xF6ren", "Somente ouvindo"],\n voiceSpeaking: ["Speaking", "Sta parlando", "Hablando", "Parle", "Spricht", "Falando"],\n voicePeers: ["Voice participants", "Partecipanti in voce", "Participantes de voz", "Participants vocaux", "Sprachteilnehmer", "Participantes de voz"],\n voiceEmpty: ["No one else in voice yet.", "Nessun altro in voce per ora.", "A\\xFAn no hay nadie m\\xE1s en voz.", "Personne d\\u2019autre en voix pour le moment.", "Noch niemand im Sprachchat.", "Ningu\\xE9m mais na voz ainda."],\n voiceVolume: ["Volume for {name}", "Volume di {name}", "Volumen de {name}", "Volume de {name}", "Lautst\\xE4rke f\\xFCr {name}", "Volume de {name}"],\n voiceUnavailable: ["Join a room with voice to use these controls.", "Entra in una stanza con voce per usare questi controlli.", "Entra en una sala con voz para usar estos controles.", "Rejoignez une salle vocale pour utiliser ces commandes.", "Diese Steuerung braucht einen Raum mit Sprachchat.", "Entre em uma sala com voz para usar estes controles."],\n voiceWatch: ["Voice is unavailable while watching.", "La voce non e\' disponibile in osservazione.", "La voz no est\\xE1 disponible al observar.", "La voix est indisponible en observation.", "Beim Zuschauen ist kein Sprachchat verf\\xFCgbar.", "A voz n\\xE3o est\\xE1 dispon\\xEDvel ao assistir."],\n voiceDenied: ["Microphone permission denied. Allow it in your browser, then try again.", "Permesso microfono negato. Consenti l\'accesso nel browser e riprova.", "Permiso de micr\\xF3fono denegado. Act\\xEDvalo en el navegador e int\\xE9ntalo de nuevo.", "Acc\\xE8s au micro refus\\xE9. Autorisez-le dans le navigateur, puis r\\xE9essayez.", "Mikrofonzugriff verweigert. Im Browser erlauben und erneut versuchen.", "Permiss\\xE3o do microfone negada. Permita no navegador e tente novamente."],\n voiceUnsupported: ["Voice is not supported in this browser.", "Questo browser non supporta la voce.", "Este navegador no admite voz.", "Ce navigateur ne prend pas en charge la voix.", "Dieser Browser unterst\\xFCtzt keinen Sprachchat.", "Este navegador n\\xE3o oferece suporte a voz."],\n voiceFailed: ["Voice could not connect. Try again.", "Connessione voce non riuscita. Riprova.", "No se pudo conectar la voz. Int\\xE9ntalo de nuevo.", "Connexion vocale impossible. R\\xE9essayez.", "Sprachverbindung fehlgeschlagen. Erneut versuchen.", "N\\xE3o foi poss\\xEDvel conectar a voz. Tente novamente."],\n voicePeerGone: ["This participant has left voice.", "Questo partecipante e\' uscito dalla voce.", "Este participante sali\\xF3 de voz.", "Ce participant a quitt\\xE9 la voix.", "Diese Person hat den Sprachchat verlassen.", "Este participante saiu da voz."],\n shortcut: ["Shift+Tab shortcut", "Scorciatoia Shift+Tab", "Atajo Shift+Tab", "Raccourci Maj+Tab", "Umschalt+Tab-Kurzbefehl", "Atalho Shift+Tab"],\n menu: ["Caisual menu", "Menu Caisual", "Menu Caisual", "Menu Caisual", "Caisual-Menu", "Menu Caisual"],\n retry: ["Retry", "Riprova", "Reintentar", "R\\xE9essayer", "Erneut versuchen", "Tentar novamente"]\n};\nvar column = (index) => Object.fromEntries(Object.entries(words).map(([key, row]) => [key, row[index]]));\nvar dictionaries = { en: column(0), it: column(1), es: column(2), fr: column(3), de: column(4), pt: column(5) };\nfunction overlayLanguage(raw) {\n const value = raw?.toLowerCase().split("-")[0];\n return languages.includes(value) ? value : "en";\n}\nfunction overlayLocale(raw) {\n const tag = normalizeLanguage(raw);\n return tag && languages.includes(tag.split("-")[0]) ? tag : "en";\n}\nfunction translator(language) {\n const dictionary = dictionaries[overlayLanguage(language)];\n return (key, values = {}) => dictionary[key].replace(/\\{(\\w+)\\}/g, (_all, name) => String(values[name] ?? ""));\n}\nfunction errorText(code) {\n if (code === "permission_denied") return "voiceDenied";\n if (code === "unsupported") return "voiceUnsupported";\n if (code === "voice_disabled") return "voiceUnavailable";\n if (code === "voice_error") return "voiceFailed";\n if (code === "voice_peer_missing") return "voicePeerGone";\n if (code === "not_publishing") return "voiceListening";\n if (["room_not_found", "room_ended", "version_closed", "no_resume"].includes(code)) return "noRoom";\n if (["room_full", "role_full"].includes(code)) return "full";\n if (code === "replaced") return "replaced";\n if (code === "no_match") return "noMatch";\n if (code === "invalid_code") return "invalidCode";\n if (["offline", "timeout"].includes(code)) return "offline";\n if (code.startsWith("role_") || ["not_in_lobby", "not_host", "session_replaced"].includes(code)) return "refused";\n if (code === "save_failed") return "saveFailed";\n return "error";\n}\n\n// src/overlay/ui-model.ts\nfunction phase(session) {\n if (!session || session.kind === "boot") return "boot";\n if (session.kind === "attaching" || session.kind === "matching") return session.kind;\n if (session.room && ["closed", "replaced"].includes(session.room.connection)) return "error";\n if (session.kind === "local") return session.localStatus === "ended" ? "ended" : "playing";\n if (session.room?.status === "ended" || session.room?.status === "finished") return "ended";\n if (session.kind === "watch") return "watching";\n if (session.kind === "room" && session.room) return session.room.status;\n return "home";\n}\nfunction initialUi(manifest) {\n return { session: null, panel: "auto", mode: manifest.modes[0]?.id ?? "", busy: false, error: null, notice: null, shortcutEnabled: true };\n}\nfunction reduceUi(model, action) {\n switch (action.type) {\n case "session": {\n const next = action.session, changed = next?.id !== model.session?.id || next === null;\n const pending = next?.kind === "attaching" || next?.kind === "matching";\n const nextPhase = phase(next), transition = phase(model.session) !== nextPhase;\n const automatic = transition && ["lobby", "countdown", "playing", "ended"].includes(nextPhase) && [null, "auto", "room", "invite", "home"].includes(model.panel);\n return {\n ...model,\n session: next,\n mode: next?.mode ?? model.mode,\n panel: changed || pending || automatic ? "auto" : model.panel,\n error: changed ? null : model.error,\n notice: changed ? null : model.notice\n };\n }\n case "panel":\n return { ...model, panel: action.panel, error: null, notice: null };\n case "mode":\n return { ...model, mode: action.mode, error: null };\n case "busy":\n return { ...model, busy: action.busy };\n case "error":\n return { ...model, error: action.code, busy: false };\n case "notice":\n return { ...model, notice: action.notice };\n case "shortcut":\n return { ...model, shortcutEnabled: action.enabled };\n }\n}\nfunction visiblePanel(model) {\n const current = phase(model.session);\n if (current === "boot" || current === "attaching" || current === "matching" || current === "error") return current;\n if (model.session?.room?.limits.max === 1 && ["room", "invite"].includes(model.panel ?? "")) return "home";\n if (model.panel !== "auto") return model.panel;\n if (model.session?.room?.limits.max === 1 && current === "lobby") return null;\n return current === "home" ? "home" : current === "lobby" ? "room" : current === "countdown" ? "countdown" : null;\n}\nfunction primaryAction(manifest, mode) {\n const selected = manifest.modes.find((item) => item.id === mode);\n if (!selected) return null;\n return {\n op: selected.execution === "local" ? "local.start" : "room.create",\n friends: selected.execution === "room" && risolviModalita(manifest, mode).players.max > 1\n };\n}\nfunction startReason(manifest, session) {\n const room = session?.room;\n if (!room || session.kind !== "room" || room.status !== "lobby" || room.connection !== "connected") return "unavailable";\n const connected = room.players.filter((p) => p.connected), active = connected.filter((p) => p.role !== "spectator");\n if (active.length < room.limits.min) return "needPlayers";\n if (connected.some((p) => !p.ready)) return "needReady";\n if (manifest.roles.some((role) => active.filter((p) => p.role === role.id).length < role.min)) return "needRoles";\n if (manifest.teams && (active.some((p) => p.team === null) || new Set(active.map((p) => p.team)).size < manifest.teams.min)) return "needTeams";\n return room.host !== room.you ? "waitHost" : null;\n}\nfunction canPlayAgain(session) {\n if (phase(session) !== "ended") return false;\n if (session?.kind === "local") return true;\n if (session?.room?.status === "finished") return session.kind === "room" && session.room.connection === "connected" && (session.room.limits.max === 1 || session.room.players.some((p) => p.id === session.room.you && p.connected && p.role !== "spectator" && !p.ready));\n return session?.kind === "room" && (session.room?.limits.max === 1 || !session.room?.lobby || session.room.host === session.room.you);\n}\nfunction normalizeInvite(code) {\n const value = code.toUpperCase().replace(/[\\s-]/g, "");\n return /^[ABCDEFGHJKLMNPQRSTUVWXYZ23456789]{6}$/.test(value) ? value : null;\n}\nfunction matchPresentation(session) {\n const room = session?.room;\n if (!room || !["finished", "ended"].includes(room.status)) return null;\n const result = readMatchResult(room.result, room.players.map((p) => p.id));\n if (!result) return null;\n const own = session?.kind === "room" && room.players.some((p) => p.id === room.you && p.role !== "spectator") && result.standings.some((p) => p.playerId === room.you);\n const first = result.standings[0];\n const winners = result.winners ?? result.standings.filter((p, i) => i === 0 || first.rank !== void 0 && p.rank === first.rank).map((p) => p.playerId);\n return { result, outcome: !own ? "ended" : result.draw ? "draw" : winners.includes(room.you) ? "won" : "lost" };\n}\n\n// src/overlay/styles.ts\nvar styles = `\n.game-icon{width:28px;height:28px;aspect-ratio:1;object-fit:contain;border-radius:7px;flex:none;vertical-align:middle}.game-icon-title{width:56px;height:56px;margin-bottom:12px}\n.safe-area-probe{position:fixed;visibility:hidden;pointer-events:none;padding:env(safe-area-inset-top,0px) env(safe-area-inset-right,0px) env(safe-area-inset-bottom,0px) env(safe-area-inset-left,0px)}\n:host{all:initial;position:fixed;inset:0;z-index:10000;pointer-events:none;font:15px/1.45 system-ui,sans-serif;color:#f4f4f1;color-scheme:dark;--accent:#a8efc5}\n[data-layout],[data-surface],.sr{pointer-events:none}*{box-sizing:border-box}button,input,select{font:inherit}button,a,input,select{touch-action:manipulation}button,select,input{border:1px solid #ffffff30;background:#25292b;color:inherit;border-radius:12px;min-height:44px;padding:10px 14px}button{cursor:pointer}button:disabled{opacity:.45;cursor:default}button:hover:not(:disabled){background:#343b3a}button:focus-visible,a:focus-visible,input:focus-visible,select:focus-visible{outline:3px solid var(--accent);outline-offset:3px}a{color:var(--accent)}.primary{background:var(--accent);color:var(--accent-ink,#11221b);border-color:transparent;font-weight:700}.primary:hover:not(:disabled){filter:brightness(1.1);background:var(--accent)}.quiet{background:transparent}label{display:grid;gap:6px;text-align:left}select,input{width:100%;min-width:0}h1,h2,p{margin:0}h1{font-size:clamp(26px,5vw,42px);line-height:1.1;letter-spacing:-.035em}h2{font-size:20px}small,.muted{color:#bdc5c1}.stack{display:grid;gap:16px}.row{display:flex;gap:8px;align-items:center;flex-wrap:wrap}.row>*{flex:0 1 auto}.row .grow,.grow{flex:1}.split{display:grid;grid-template-columns:1fr 1fr;gap:10px}.pill{position:absolute;top:max(10px,env(safe-area-inset-top));right:max(10px,env(safe-area-inset-right));display:flex;height:44px;border:1px solid #ffffff35;border-radius:24px;background:#171e20eF;box-shadow:0 4px 20px #0004;pointer-events:auto;overflow:hidden}.pill button{border:0;border-radius:0;padding:8px 13px;background:transparent}.pill button:focus-visible{outline-offset:-4px}.pill small{margin-left:8px}.backdrop{position:absolute;inset:0;background:#0b151ce8;backdrop-filter:blur(10px);pointer-events:auto;display:grid;place-items:center;padding:max(16px,env(safe-area-inset-top)) max(16px,env(safe-area-inset-right)) max(16px,env(safe-area-inset-bottom)) max(16px,env(safe-area-inset-left));overflow:auto}.backdrop.home{background-color:#142127;background-size:contain;background-repeat:no-repeat;background-position:center}.dialog{position:relative;width:min(100%,540px);max-height:100%;overflow:auto;overscroll-behavior:contain;background:#141b1df5;border:1px solid #ffffff25;border-radius:22px;padding:24px;box-shadow:0 20px 80px #0005}.dialog.wide{width:min(100%,700px)}.top{display:flex;align-items:center;gap:12px;margin-bottom:18px}.top h2{flex:1}.tabs{display:flex;gap:6px;flex-wrap:wrap;border-bottom:1px solid #ffffff25;padding-bottom:12px}.tabs button{min-height:36px;padding:6px 10px}.tabs [aria-current=true]{border-color:var(--accent)}.roster{list-style:none;padding:0;margin:0;display:grid;gap:8px;max-height:32dvh;overflow:auto}.roster li{display:flex;align-items:center;gap:8px;padding:10px;background:#ffffff08;border-radius:10px}.roster .name{flex:1;overflow-wrap:anywhere}.badge{border:1px solid #ffffff30;border-radius:6px;padding:2px 6px;font-size:12px}.code{font-size:24px;letter-spacing:.13em;font-variant-numeric:tabular-nums}.notice,.error{border-radius:10px;padding:10px;background:#a8efc514;overflow-wrap:anywhere}.error{background:#ff8b7720;color:#ffd2c9}.countdown{font-size:88px;line-height:1;text-align:center;font-variant-numeric:tabular-nums}.ended{position:absolute;bottom:max(12px,env(safe-area-inset-bottom));left:50%;transform:translateX(-50%);max-width:calc(100% - 24px);width:max-content;background:#171e20f5;pointer-events:auto;border:1px solid #ffffff30;border-radius:16px;padding:10px;display:flex;align-items:center;justify-content:center;gap:8px;flex-wrap:wrap}.ended [data-rematch-players]{max-width:100%;max-height:3.2em;overflow:auto;overflow-wrap:anywhere}.standings{list-style:none;margin:0;padding:0;flex-basis:100%;max-height:20dvh;overflow:auto;font-size:13px}.standings li{display:flex;justify-content:space-between;gap:16px;overflow-wrap:anywhere}.ended{max-height:calc(100dvh - 80px);overflow:auto}.ended strong{padding:0 8px}.sr{position:absolute;width:1px;height:1px;overflow:hidden;clip-path:inset(50%);white-space:nowrap}.table-wrap{overflow:auto;max-height:38dvh}table{width:100%;border-collapse:collapse;text-align:left}th,td{padding:9px 6px;border-bottom:1px solid #ffffff20}td:nth-child(3){text-align:right}td small{display:block}.self{background:#ffffff0a}.checkbox{display:flex;align-items:center;gap:8px;font-size:13px}.checkbox input{width:18px;min-height:18px}.full{width:100%}\n[hidden]{display:none!important}.voice-peers{list-style:none;margin:0;padding:0;display:grid;gap:10px}.voice-peers li{border:1px solid #ffffff25;border-radius:12px;padding:12px;display:grid;gap:8px}.voice-peers [data-speaking=true]{border-color:var(--accent)}.voice-peers input{width:100%;accent-color:var(--accent);padding:0}.voice-peers label{font-size:13px}.pill .voice-toggle{width:44px;padding:8px}.voice-toggle[data-voice-state=on][data-muted=false]{color:var(--accent)}\n.boot{position:absolute;inset:0;z-index:2;isolation:isolate;display:grid;place-items:center;overflow:auto;overscroll-behavior:contain;padding:max(100px,env(safe-area-inset-top)) max(24px,env(safe-area-inset-right)) max(48px,env(safe-area-inset-bottom)) max(24px,env(safe-area-inset-left));background:#0b151c;opacity:1;transition:opacity .4s ease;pointer-events:auto;outline:none}\n.boot::before,.boot::after{content:"";position:fixed;inset:0;pointer-events:none;z-index:-1}.boot::before{background:radial-gradient(ellipse at 50% 38%,color-mix(in srgb,var(--accent),transparent 80%),transparent 65%)}.boot::after{background:radial-gradient(ellipse at 50% 38%,#0b151c20,#0b151cd9 85%),linear-gradient(#0b151c66,#0b151cbf)}\n.boot-cover{position:fixed;inset:0;z-index:-2;width:100%;height:100%;aspect-ratio:3 / 2;object-fit:contain;filter:blur(20px);opacity:.65;pointer-events:none}\n.boot-brand{position:absolute;top:max(28px,env(safe-area-inset-top));left:max(32px,env(safe-area-inset-left));display:flex;align-items:center;gap:10px;font-size:14px;font-weight:650;letter-spacing:.02em;color:#f4f4f1b3}.boot-brand span{display:grid;place-items:center;width:36px;height:36px;border:1px solid #ffffff25;border-radius:12px;background:#171e20af;box-shadow:0 4px 20px #0004;color:var(--boot-accent);font-size:20px;font-weight:800}\n.boot-content{width:min(100%,900px);text-align:center;display:grid;justify-items:center;gap:24px}.boot h1{max-width:16ch;font-size:clamp(44px,8vw,108px);font-weight:800;line-height:1.04;letter-spacing:-.05em;overflow-wrap:anywhere;text-wrap:balance;color:var(--boot-accent);text-shadow:0 20px 80px #0005}\n.boot-progress{width:112px;height:3px;border-radius:12px;background:#ffffff20;overflow:hidden;margin-top:12px}.boot-progress span{display:block;width:44%;height:100%;border-radius:inherit;background:var(--boot-accent);animation:boot-progress 1.8s ease-in-out infinite}.boot-status{max-width:42ch;min-height:3em;font-size:14px;line-height:1.5;color:#d3dad6;text-wrap:balance}.boot-recovery{min-height:44px}.boot-recovery .row{justify-content:center}.boot-leaving{opacity:0;pointer-events:none}\n@keyframes boot-progress{0%{transform:translateX(-110%)}100%{transform:translateX(340%)}}\n@media(max-width:480px){.dialog{padding:18px;border-radius:18px}.split{grid-template-columns:1fr 1fr;gap:8px}.tabs{gap:4px}.tabs button{font-size:13px;padding:6px 8px}.pill button:focus-visible{outline-offset:-4px}.pill small{display:none}.ended{gap:6px}.standings{list-style:none;margin:0;padding:0;flex-basis:100%;max-height:20dvh;overflow:auto;font-size:13px}.standings li{display:flex;justify-content:space-between;gap:16px;overflow-wrap:anywhere}.ended{max-height:calc(100dvh - 80px);overflow:auto}.ended strong{font-size:13px}.ended button{padding:8px 10px;font-size:13px}.roster{max-height:28dvh}}\n@media(prefers-reduced-motion:reduce){*{scroll-behavior:auto}.boot{transition:none}.boot-progress span{animation:none;transform:translateX(65%)}}\n`;\n\n// src/overlay/voice-panel.ts\nfunction voiceEligible(manifest, session) {\n return manifest.voice !== "none" && session?.kind !== "watch" && session?.room?.players.find((player) => player.id === session.room?.you)?.role !== "spectator";\n}\nfunction voiceStatus(voice, t) {\n const key = voice.state === "joining" ? "voiceJoining" : voice.state === "reconnecting" ? "reconnecting" : voice.state === "off" ? "voiceOff" : "voiceOn";\n return t(key);\n}\nfunction updateVoicePanel(container, input) {\n const { session, t } = input, voice = session?.kind === "room" ? session.voice : null;\n if (!voiceEligible(input.manifest, session) || !voice) {\n container.replaceChildren();\n const note = container.ownerDocument.createElement("p");\n note.textContent = t(session?.kind === "watch" || session?.room?.players.find((p) => p.id === session.room?.you)?.role === "spectator" ? "voiceWatch" : "voiceUnavailable");\n container.append(note);\n return;\n }\n if (!container.querySelector("[data-voice-status]")) container.innerHTML = `<p role="status" aria-live="polite" data-voice-status></p><p data-voice-self></p>\n <div class="row"><button type="button" data-action="voice-join"></button><button type="button" data-action="voice-mute"></button><button type="button" data-action="voice-leave"></button></div>\n <p class="error" role="alert" data-voice-error hidden></p><h3 data-voice-heading></h3><ul class="voice-peers" data-voice-peers></ul><p class="muted" data-voice-empty></p>`;\n const get = (selector) => container.querySelector(selector);\n const status = get("[data-voice-status]");\n status.textContent = voiceStatus(voice, t);\n status.dataset.voiceState = voice.state;\n const mic = (value) => t(!value.mic ? "voiceListening" : value.muted ? "voiceMuted" : value.speaking ? "voiceSpeaking" : "voiceMic");\n get("[data-voice-self]").textContent = voice.state === "off" ? "" : `${t("you")}: ${mic(voice)}`;\n const join = get(\'[data-action="voice-join"]\'), mute = get(\'[data-action="voice-mute"]\'), leave = get(\'[data-action="voice-leave"]\');\n join.textContent = t("voiceJoin");\n join.hidden = voice.state !== "off";\n join.disabled = session?.room?.connection !== "connected" || input.pending === "voice.join";\n mute.textContent = t(voice.muted ? "voiceUnmute" : "voiceMute");\n mute.hidden = voice.state !== "on" || !voice.mic;\n mute.disabled = input.pending === "voice.mute";\n mute.setAttribute("aria-pressed", String(voice.muted));\n leave.textContent = t("voiceLeave");\n leave.hidden = voice.state === "off" && input.pending !== "voice.join";\n leave.disabled = input.pending === "voice.leave";\n const error = get("[data-voice-error]");\n error.hidden = !input.error;\n error.textContent = input.error ? t(errorText(input.error)) : "";\n get("[data-voice-heading]").textContent = t("voicePeers");\n get("[data-voice-empty]").textContent = t("voiceEmpty");\n get("[data-voice-empty]").hidden = voice.peers.length > 0;\n const list = get("[data-voice-peers]"), ids = new Set(voice.peers.map((peer) => peer.id));\n for (const row of list.querySelectorAll("[data-voice-peer]")) if (!ids.has(row.dataset.voicePeer)) row.remove();\n for (const peer of voice.peers) {\n let row = [...list.children].find((node) => node.dataset.voicePeer === peer.id);\n if (!row) {\n row = container.ownerDocument.createElement("li");\n row.dataset.voicePeer = peer.id;\n row.innerHTML = \'<div class="row"><strong data-peer-name></strong><small data-peer-status></small></div><label><span data-volume-label></span><input type="range" min="0" max="1" step="0.05" data-control="voice-volume"></label>\';\n row.querySelector("input").dataset.peer = peer.id;\n list.append(row);\n }\n const name = session?.room?.players.find((player) => player.id === peer.id)?.name ?? peer.id;\n row.dataset.mic = String(peer.mic);\n row.dataset.muted = String(peer.muted);\n row.dataset.speaking = String(peer.speaking);\n row.querySelector("[data-peer-name]").textContent = name;\n row.querySelector("[data-peer-status]").textContent = mic(peer);\n row.querySelector("[data-volume-label]").textContent = t("voiceVolume", { name });\n const range = row.querySelector("input");\n if (range.dataset.editing !== "true") range.value = String(peer.volume);\n range.setAttribute("aria-valuetext", `${Math.round(Number(range.value) * 100)}%`);\n range.disabled = voice.state !== "on";\n }\n}\n\n// src/overlay/ui.ts\nvar escape = (value) => String(value ?? "").replace(/[&<>"\']/g, (c) => ({ "&": "&amp;", "<": "&lt;", ">": "&gt;", \'"\': "&quot;", "\'": "&#39;" })[c]);\nfunction mountOverlay(input) {\n const manifest = input.configuration.manifest;\n if (manifest.overlay?.version !== 1) return null;\n const document = input.container.ownerDocument, win = document.defaultView, t = translator(input.language);\n const host = document.createElement("div");\n host.dataset.caisualOverlay = "";\n host.lang = overlayLanguage(input.language);\n host.style.setProperty("pointer-events", "none", "important");\n const root = host.attachShadow({ mode: "open" });\n if (typeof win.CSSStyleSheet?.prototype.replaceSync === "function" && "adoptedStyleSheets" in root) {\n const sheet = new win.CSSStyleSheet();\n sheet.replaceSync(styles);\n root.adoptedStyleSheets = [sheet];\n } else {\n const sheet = document.createElement("link");\n sheet.rel = "stylesheet";\n sheet.href = "/__caisual/overlay/v1.css";\n root.append(sheet);\n }\n const elements = document.createElement("div");\n elements.dataset.layout = "";\n elements.style.pointerEvents = "none";\n elements.innerHTML = `<div data-surface></div><div class="sr" role="status" aria-live="polite" data-live></div>`;\n const safeProbe = document.createElement("div");\n safeProbe.className = "safe-area-probe";\n safeProbe.setAttribute("aria-hidden", "true");\n root.append(elements, safeProbe);\n const surface = root.querySelector("[data-surface]"), live = root.querySelector("[data-live]");\n surface.style.pointerEvents = "none";\n live.style.pointerEvents = "none";\n const accent = manifest.overlay.accent ?? "#a8efc5";\n host.style.setProperty("--accent", accent);\n const rgb = [1, 3, 5].map((i) => parseInt(accent.slice(i, i + 2), 16) / 255).map((v) => v <= 0.04045 ? v / 12.92 : ((v + 0.055) / 1.055) ** 2.4);\n const luminance = rgb[0] * 0.2126 + rgb[1] * 0.7152 + rgb[2] * 0.0722;\n host.style.setProperty("--accent-ink", luminance > 0.179 ? "#000000" : "#ffffff");\n host.style.setProperty("--boot-accent", luminance > 0.179 ? accent : `color-mix(in srgb, ${accent}, #ffffff 70%)`);\n input.container.append(host);\n let model = initialUi(manifest), disposed = false, operation = 0, lastView = "", geometryFrame = 0;\n let lastPhase = "", wasModal = false, copyFallback = null;\n let boot = null, bootTimer = 0, bootFadeTimer = 0;\n let voiceError = null, voicePending = null, voiceOperation = 0;\n let codeDraft = input.configuration.invite ?? "";\n const oldInert = Boolean(input.frame.inert), oldTabIndex = input.frame.getAttribute("tabindex");\n try {\n model.shortcutEnabled = win.localStorage.getItem("caisual-overlay-shortcut-v1") !== "off";\n } catch {\n }\n const boards = input.boards ? createBoardController({ manifest, player: input.player.id, guests: input.player.guest, read: input.boards, changed: () => render() }) : null;\n const stops = [];\n const selectedMode = () => manifest.modes.find((mode) => mode.id === model.mode);\n const disabled = () => model.busy ? " disabled" : "";\n const button = (action, key, extra = "", off = false) => `<button type="button" data-action="${action}"${extra}${off || model.busy ? " disabled" : ""}>${t(key)}</button>`;\n function resetBootWait() {\n win.clearTimeout(bootTimer);\n if (!boot || phase(model.session) !== "boot") return;\n boot.querySelector("[data-boot-message]").textContent = t("loading");\n boot.querySelector("[data-boot-recovery]").hidden = true;\n bootTimer = win.setTimeout(() => {\n bootTimer = 0;\n if (disposed || !boot) return;\n boot.querySelector("[data-boot-message]").textContent = t("loadingSlow");\n boot.querySelector("[data-boot-recovery]").hidden = false;\n }, 9e3);\n }\n function updateBoot(loading) {\n if (loading) {\n if (boot) {\n win.clearTimeout(bootFadeTimer);\n bootFadeTimer = 0;\n boot.inert = false;\n boot.removeAttribute("aria-hidden");\n boot.style.pointerEvents = "auto";\n boot.classList.remove("boot-leaving");\n return;\n }\n boot = document.createElement("section");\n boot.className = "boot";\n boot.tabIndex = -1;\n boot.setAttribute("aria-labelledby", "boot-title");\n boot.setAttribute("aria-describedby", "boot-status");\n boot.style.pointerEvents = "auto";\n boot.innerHTML = `<div class="boot-brand" aria-hidden="true"><span>C</span>Caisual</div>\n <div class="boot-content"><h1 id="boot-title">${escape(manifest.name)}</h1>\n <div class="boot-progress" aria-hidden="true"><span></span></div>\n <p class="boot-status" id="boot-status" role="status" aria-live="polite" aria-atomic="true"><span class="sr">${escape(manifest.name)}. </span><span data-boot-message></span></p>\n <div class="boot-recovery"><div class="row" data-boot-recovery hidden>${button("reload", "retry", \' class="primary"\')}${button("exit-now", "exit", \' class="quiet"\')}</div></div>\n </div>`;\n if (input.configuration.coverUrl) {\n const cover = document.createElement("img");\n cover.className = "boot-cover";\n cover.alt = "";\n cover.setAttribute("aria-hidden", "true");\n cover.addEventListener("error", () => {\n cover.hidden = true;\n }, { once: true });\n cover.src = input.configuration.coverUrl;\n boot.prepend(cover);\n }\n elements.append(boot);\n resetBootWait();\n } else if (boot && !boot.inert) {\n boot.inert = true;\n boot.setAttribute("aria-hidden", "true");\n boot.style.pointerEvents = "none";\n boot.classList.add("boot-leaving");\n const remove = () => {\n win.clearTimeout(bootTimer);\n bootTimer = 0;\n boot?.remove();\n boot = null;\n bootFadeTimer = 0;\n };\n if (win.matchMedia?.("(prefers-reduced-motion: reduce)").matches) remove();\n else bootFadeTimer = win.setTimeout(remove, 420);\n }\n }\n const dispatch = (action) => {\n if (disposed) return;\n model = reduceUi(model, action);\n render();\n };\n const announce = (text) => {\n if (live.textContent !== text) live.textContent = text;\n };\n const controls = () => [...root.querySelectorAll(\'button:not(:disabled),a[href],input:not(:disabled),select:not(:disabled),[tabindex="0"]\')].filter((el) => !el.closest("[hidden]"));\n const soloMode = (mode) => (mode === null || manifest.modes.some((item) => item.id === mode)) && risolviModalita(manifest, mode).players.max === 1;\n const solo = () => model.session?.room?.limits.max === 1;\n const roomCode = () => solo() ? null : model.session?.room?.code ?? null;\n const setPanel = (panel) => {\n if (panel === "boards" && boards && !boards.state.query) {\n const id = Object.keys(manifest.boards)[0];\n if (id) boards.select({ board: id, period: (manifest.boards[id].periods ?? ["all-time"])[0], guests: input.player.guest });\n }\n copyFallback = null;\n dispatch({ type: "panel", panel });\n };\n const close = () => {\n const current = phase(model.session), panel = visiblePanel(model);\n if (current === "boot") return;\n if (current === "home") setPanel("home");\n else if (current === "lobby" && panel !== "room") setPanel("room");\n else setPanel(null);\n };\n const toggle = () => {\n if (visiblePanel(model)) close();\n else setPanel(model.session?.room ? "room" : "home");\n };\n async function perform(op, args, after) {\n const token = ++operation;\n dispatch({ type: "error", code: null });\n dispatch({ type: "busy", busy: true });\n try {\n await input.bridge.request(op, args);\n if (token === operation && !disposed) await after?.();\n } catch (error) {\n if (token === operation && !disposed && error.code !== "cancelled") dispatch({ type: "error", code: error.code ?? "offline" });\n } finally {\n if (token === operation && !disposed) dispatch({ type: "busy", busy: false });\n }\n }\n function updateVoice() {\n const container = root.querySelector("[data-voice-panel]");\n if (container) updateVoicePanel(container, { manifest, session: model.session, t, error: voiceError, pending: voicePending });\n const toggle2 = root.querySelector("[data-voice-toggle]"), voice = model.session?.voice;\n if (toggle2) {\n toggle2.dataset.voiceState = voice?.state ?? "off";\n toggle2.dataset.muted = String(voice?.muted ?? false);\n toggle2.setAttribute("aria-label", `${t("voice")}: ${voice ? voiceStatus(voice, t) : t("voiceOff")}`);\n toggle2.textContent = voice?.state === "on" && !voice.muted ? "\\u25CF" : "\\u25CB";\n }\n }\n async function performVoice(op, args) {\n const sessionId = model.session?.id, epoch = input.bridge.epoch, volume = op === "voice.setVolume";\n const token = volume ? voiceOperation : ++voiceOperation;\n const current = () => !disposed && model.session?.id === sessionId && input.bridge.epoch === epoch && token === voiceOperation;\n voiceError = null;\n if (!volume) voicePending = op;\n updateVoice();\n try {\n await input.bridge.request(op, args);\n } catch (error) {\n if (current()) voiceError = error.code ?? "voice_error";\n } finally {\n if (current()) {\n if (!volume) voicePending = null;\n updateVoice();\n }\n }\n }\n async function copyInvite() {\n const code = roomCode();\n if (!code) return;\n const url = input.inviteUrl(code);\n try {\n await win.navigator.clipboard.writeText(url);\n dispatch({ type: "notice", notice: t("copied") });\n } catch {\n copyFallback = url;\n render();\n root.querySelector(\'input[data-control="invite-link"]\')?.select();\n }\n }\n function invitation() {\n if (solo()) return "";\n const code = roomCode();\n if (!code) return `<p>${t("noRoom")}</p>`;\n return `<div class="row"><div class="grow"><small>${t("code")}</small><div class="code" data-room-code>${escape(code)}</div></div>${button("copy", "copy")}</div>${copyFallback ? `<label>${t("copyFailed")}<input data-control="invite-link" readonly value="${escape(copyFallback)}"></label>` : ""}`;\n }\n function navigation(panel) {\n const items = [];\n if (model.session?.room && !solo()) items.push(["room", "room"], ["invite", "copy"]);\n items.push(["friends", "friends"]);\n if (Object.keys(manifest.boards).length) items.push(["boards", "boards"]);\n if (voiceEligible(manifest, model.session)) items.push(["voice", "voice"]);\n return `<nav class="tabs" aria-label="Caisual">${items.map(([id, key]) => button(`panel:${id}`, key, ` aria-current="${id === panel}"`)).join("")}</nav>`;\n }\n function home() {\n const selected = selectedMode(), action = primaryAction(manifest, model.mode), session = model.session;\n const hasRooms = manifest.modes.some((mode) => mode.execution === "room" && risolviModalita(manifest, mode.id).players.max > 1);\n return `${input.configuration.iconUrl ? `<img class="game-icon game-icon-title" src="${escape(input.configuration.iconUrl)}" alt="" />` : ""}<h1>${escape(manifest.name)}</h1><p class="muted" data-game-languages>${t("gameLanguages")}: ${escape(manifestLanguages(manifest).join(" \\xB7 "))}</p><label>${t("mode")}<select data-control="mode"${disabled()}>${manifest.modes.map((mode) => `<option value="${escape(mode.id)}"${mode.id === model.mode ? " selected" : ""}>${escape(risolviPresentazione(manifest, mode.id, input.language).label)}</option>`).join("")}</select></label>\n ${selected?.instructions ? `<p class="muted">${escape(resolveText(selected.instructions, input.language, manifestLanguages(manifest)[0]))}</p>` : ""}\n ${input.configuration.invite && phase(session) === "home" ? button("join-invite", "joinInvite", \' class="primary"\', !session?.ready) : ""}\n ${action ? button("play", action.friends ? "friendsPlay" : "play", \' class="primary"\', !session?.ready) : ""}\n ${selected?.matchmaking ? button("match", "find", "", !selected.matchmaking.defaults || !session?.ready) : ""}\n ${session?.resume ? button("resume", "resume", "", !session.ready) + `<small>${escape(solo() || soloMode(session.resume.mode) ? "" : session.resume.code)}</small>` : ""}\n ${hasRooms && !solo() ? `<div class="split">${button("panel:join", "join", "", !session?.ready)}${manifest.spectators ? button("panel:watch", "watch", "", !session?.ready) : ""}</div>` : ""}\n ${solo() ? button("panel:exit", "exit", \' class="quiet"\') : ""}${navigation("home")}<label class="checkbox"><input type="checkbox" data-control="shortcut"${model.shortcutEnabled ? " checked" : ""}>${t("shortcut")}</label>`;\n }\n function room() {\n const session = model.session, room2 = session?.room;\n if (!room2) return `<p>${t("noRoom")}</p>`;\n const own = room2.players.find((player) => player.id === room2.you), lobby = room2.status === "lobby" && session?.kind === "room";\n const canRole = session?.kind === "room" && (lobby || room2.status === "playing" && room2.requestRole);\n const reason = startReason(manifest, session);\n return `${invitation()}<ul class="roster" aria-label="${t("room")}">${room2.players.map((p) => `<li data-player-id="${escape(p.id)}"><span class="name">${escape(p.name)} ${p.id === room2.you ? `<small>(${t("you")})</small>` : ""}</span>${p.id === room2.host ? `<span class="badge">${t("host")}</span>` : ""}${p.role ? `<small>${escape(resolveText(manifest.roles.find((r) => r.id === p.role)?.label, input.language, manifestLanguages(manifest)[0], p.role))}</small>` : ""}${p.team ? `<small>${t("team")} ${p.team}</small>` : ""}<small>${!p.connected ? t("away") : lobby ? t(p.ready ? "ready" : "unready") : ""}</small></li>`).join("")}</ul>\n ${canRole && manifest.roles.length ? `<label>${t("role")}<select data-control="role"${disabled()}><option value="" disabled${!own?.role ? " selected" : ""}>${t("role")}</option>${manifest.roles.map((role) => `<option value="${escape(role.id)}"${role.id === own?.role ? " selected" : ""}>${escape(resolveText(role.label, input.language, manifestLanguages(manifest)[0], role.id))}</option>`).join("")}</select></label>` : ""}\n ${lobby && manifest.teams ? `<label>${t("team")}<select data-control="team"${disabled()}><option value="" disabled${!own?.team ? " selected" : ""}>${t("team")}</option>${Array.from({ length: manifest.teams.max }, (_, i) => `<option value="${i + 1}"${own?.team === i + 1 ? " selected" : ""}>${t("team")} ${i + 1}</option>`).join("")}</select></label>` : ""}\n ${lobby ? `<div class="row">${button("ready", own?.ready ? "unready" : "ready", \' class="primary"\', room2.connection !== "connected")}${room2.host === room2.you ? button("start", "start", "", reason !== null) : ""}</div>${reason ? `<p class="muted" data-start-reason>${t(reason)}</p>` : ""}` : ""}\n ${session?.kind === "watch" ? `<p>${t("watching")} \\xB7 ${t("delay", { n: (room2.delayMs ?? 0) / 1e3 })}</p>` : ""}\n ${navigation("room")}<label class="checkbox"><input type="checkbox" data-control="shortcut"${model.shortcutEnabled ? " checked" : ""}>${t("shortcut")}</label>${button("panel:exit", "exit", \' class="quiet"\')}`;\n }\n function crew() {\n const provider = input.crew, state = provider?.getSnapshot();\n if (!provider || provider.unavailable || !state?.you) return `<p>${t(provider?.unavailable === "local" ? "localCrew" : "loginCrew")}</p>`;\n const online = state.friends.filter((friend) => friend.online), party = state.party;\n const person = (p) => `<li>${p.game ? `<img class="game-icon" src="${escape(p.game.iconUrl)}" alt="" />` : ""}<span class="name">${escape(p.name)}<small>${p.game ? ` \\xB7 ${escape(p.game.name)}` : ""}</small></span>${p.room && p.game ? button("follow", "follow", ` data-code="${escape(p.room.code)}" data-game="${escape(p.game.slug)}"`) : ""}${party?.leader === state.you.id && !party.members.some((member) => member.id === p.id) ? button("party-invite", "inviteParty", ` data-player="${escape(p.id)}"`) : ""}</li>`;\n return `${!state.connected ? `<p>${t("reconnecting")}</p>` : ""}${party ? `<ul class="roster">${party.members.map(person).join("")}</ul>${button("party-leave", "leaveParty")}` : button("party-create", "createParty")}\n ${state.invites.map((invite) => `<div class="row"><span class="grow">${escape(invite.from.name)}</span>${button("party-accept", "accept", ` data-party="${escape(invite.party)}"`)}${button("party-decline", "decline", ` data-party="${escape(invite.party)}"`)}</div>`).join("")}\n ${state.follow ? `<div class="row"><span class="grow"><img class="game-icon" src="${escape(state.follow.game.iconUrl)}" alt="" /> ${escape(state.follow.from.name)} \\xB7 ${escape(state.follow.game.name)}</span>${button("follow", "follow", ` data-code="${escape(state.follow.code)}" data-game="${escape(state.follow.game.slug)}"`)}</div>` : ""}\n <h2>${t("online")}</h2>${online.length ? `<ul class="roster">${online.map(person).join("")}</ul>` : `<p class="muted">${t("noFriends")}</p>`}`;\n }\n function leaderboard() {\n if (!boards || !boards.state.query) return `<p>${t("unavailable")}</p>`;\n const { query, data, loading, error, saving } = boards.state;\n const board = manifest.boards[query.board];\n return `<label>${t("board")}<select data-control="board">${Object.entries(manifest.boards).map(([id, value]) => `<option value="${escape(id)}"${query.board === id ? " selected" : ""}>${escape(resolveText(value.label, input.language, manifestLanguages(manifest)[0], id))}</option>`).join("")}</select></label>\n <div class="split"><label>${t("period")}<select data-control="period">${(board.periods ?? ["all-time"]).map((period) => `<option value="${period}"${query.period === period ? " selected" : ""}>${t(period === "daily" ? "daily" : "allTime")}</option>`).join("")}</select></label><label>${t("category")}<select data-control="category"><option value="accounts"${!query.guests ? " selected" : ""}>${t("accounts")}</option><option value="guests"${query.guests ? " selected" : ""}>${t("guests")}</option></select></label></div>\n ${query.period === "daily" ? `<small data-board-day>${escape(data?.day ?? query.day ?? new Date(input.bridge.serverTime() ?? Date.now()).toISOString().slice(0, 10))}</small>` : ""}\n ${saving ? `<p role="status" data-saving>${t(saving)}</p>` : ""}${error ? `<p role="alert">${t("offline")}</p>` : ""}\n ${data ? `<div class="table-wrap"><table><thead><tr><th>${t("rank")}</th><th>${t(query.guests ? "guests" : "accounts")}</th><th>${t("score")}</th></tr></thead><tbody>${data.entries.map((entry) => `<tr${entry.me ? \' class="self"\' : ""}><td>${entry.rank}</td><td>${escape(entry.name)}${entry.verified ? `<small>${t("verified")}</small>` : ""}</td><td>${entry.score}</td></tr>`).join("")}</tbody></table>${data.entries.length ? "" : `<p>${t("empty")}</p>`}</div><p data-own-score>${t("own")} (${t(data.ownGuest ? "guests" : "accounts")}): ${data.me ? `#${data.me.rank} \\xB7 ${data.me.score}${data.me.verified ? ` \\xB7 ${t("verified")}` : ""}` : t("empty")}</p>` : `<p>${t(loading ? "loading" : "empty")}</p>`}\n ${button("refresh", "refresh", "", loading)}`;\n }\n function content(panel) {\n switch (panel) {\n case "home":\n return home();\n case "room":\n return room();\n case "invite":\n return invitation();\n case "friends":\n return crew();\n case "voice":\n return \'<div class="stack" data-voice-panel></div>\';\n case "boards":\n return leaderboard();\n case "join":\n case "watch":\n return `<form class="stack" data-form="${panel}"><label>${t("code")}<input data-control="code" name="code" autocomplete="off" autocapitalize="characters" spellcheck="false" maxlength="16" value="${escape(codeDraft)}" required></label><button class="primary" type="submit"${disabled()}>${t(panel === "join" ? "join" : "watch")}</button></form>`;\n case "attaching":\n case "matching":\n return `<p role="status">${t(panel === "matching" ? "matching" : "joining")}</p>${model.session?.waiting ? `<p>${t("queue", { n: model.session.waiting.players, max: model.session.waiting.max })}</p>` : ""}<button type="button" data-action="cancel">${t("cancel")}</button>`;\n case "countdown":\n return `<p>${t("starting")}</p><div class="countdown" data-countdown></div>`;\n case "boot":\n return "";\n case "error":\n return `<p role="alert">${t(model.session?.room?.connection === "replaced" ? "replaced" : "noRoom")}</p>${button("leave", "home")}${button("exit-now", "exit")}`;\n case "exit":\n return model.session?.kind === "room" && phase(model.session) !== "ended" ? `<p>${t(model.session.room?.persistent ? "leaveHint" : "temporaryHint")}</p>${invitation()}${button("disconnect-exit", "leaveNow", \' class="primary"\')}<p class="muted">${t("abandonHint")}</p>${button("leave-exit", "leaveRoom")}` : button("leave-exit", "exit", \' class="primary"\');\n }\n }\n function title(panel) {\n const keys = { boot: "loading", home: "home", room: "room", invite: "copy", friends: "friends", voice: "voice", boards: "boards", join: "join", watch: "watch", attaching: "joining", matching: "matching", countdown: "starting", error: "error", exit: "exit" };\n return t(keys[panel]);\n }\n function updateCountdown() {\n const at = model.session?.room?.countdownAt, now = input.bridge.serverTime();\n const value = at === null || at === void 0 || now === null ? "..." : String(Math.max(0, Math.round((at - now) / 1e3)));\n const node = root.querySelector("[data-countdown]");\n if (node && node.textContent !== value) {\n node.textContent = value;\n announce(`${t("starting")} ${value}`);\n }\n }\n function geometry() {\n geometryFrame = 0;\n if (disposed || !input.bridge.epoch || !model.session) return;\n const frame = gameViewport(input.frame), { scaleX, scaleY } = frame;\n const reservedRects = [...root.querySelectorAll("[data-reserve]")].map((el) => {\n const rect = el.getBoundingClientRect(), left = Math.max(frame.left, rect.left), top = Math.max(frame.top, rect.top), right = Math.min(frame.right, rect.right), bottom = Math.min(frame.bottom, rect.bottom);\n return { x: Math.max(0, Math.round((left - frame.left) * scaleX)), y: Math.max(0, Math.round((top - frame.top) * scaleY)), width: Math.max(0, Math.round((right - left) * scaleX)), height: Math.max(0, Math.round((bottom - top) * scaleY)) };\n }).filter((rect) => rect.width && rect.height).slice(0, 8);\n const view = { inputBlocked: phase(model.session) === "boot" || !!visiblePanel(model), reservedRects, safeArea: measureSafeArea(input.frame, safeProbe), shortcutEnabled: model.shortcutEnabled };\n const serialized = `${input.bridge.epoch}:${JSON.stringify(view)}`;\n if (lastView === serialized) return;\n lastView = serialized;\n void input.bridge.request("overlay.view", view).catch(async (error) => {\n if (error?.code === "invalid_request" && lastView === serialized) {\n const { safeArea, ...legacy } = view;\n try {\n await input.bridge.request("overlay.view", legacy);\n return;\n } catch {\n }\n }\n if (lastView === serialized) lastView = "";\n });\n }\n function resize() {\n if (!geometryFrame) geometryFrame = win.requestAnimationFrame(geometry);\n }\n function resultBar() {\n const presentation = matchPresentation(model.session);\n if (!presentation) return `<strong>${t("ended")}</strong>`;\n const room2 = model.session.room, { result } = presentation;\n const unit = result.unit && ["points", "time", "distance"].includes(result.unit) ? t(result.unit) : result.unit;\n const rows = result.standings.map((p, i) => {\n const name = room2.players.find((player) => player.id === p.playerId).name;\n const score = p.score === void 0 ? "" : `<span>${escape(String(p.score))}${unit ? ` ${escape(unit)}` : ""}</span>`;\n return `<li><span>${p.rank ?? i + 1}. ${escape(name)}</span>${score}</li>`;\n }).join("");\n return `<strong data-outcome>${t(presentation.outcome)}</strong><ol class="standings" aria-label="${t("standings")}">${rows}</ol>`;\n }\n function rematchBar() {\n const session = model.session, room2 = session?.room;\n if (!room2 || room2.status !== "finished" || solo()) return "";\n const active = room2.players.filter((p) => p.connected && p.role !== "spectator");\n const ready = active.filter((p) => p.ready), host2 = session.kind === "room" && room2.host === room2.you;\n const canStart = room2.connection === "connected" && active.length >= room2.limits.min && ready.length === active.length;\n return `<small role="status" data-rematch-ready>${t("rematchReady", { n: ready.length, max: active.length })}</small>\n ${ready.length ? `<small data-rematch-players>${ready.map((p) => escape(p.name)).join(", ")}</small>` : ""}\n ${room2.rematch?.autoStart ? "" : host2 ? button("restart", "rematchStart", \' class="primary"\', !canStart) : `<small>${t("waitHost")}</small>`}`;\n }\n function render() {\n if (disposed) return;\n const panel = visiblePanel(model), current = phase(model.session), room2 = model.session?.room;\n const focused = root.activeElement;\n const focusPeer = focused?.dataset.peer;\n const focusKey = focused?.dataset.control ? ["control", focused.dataset.control] : focused?.dataset.action ? ["action", focused.dataset.action] : null;\n const previousScroll = root.querySelector(".dialog")?.scrollTop ?? 0;\n const selection = focused?.tagName === "INPUT" ? { start: focused.selectionStart, end: focused.selectionEnd } : null;\n const crewState = input.crew?.getSnapshot(), invitations = (crewState?.invites.length ?? 0) + (crewState?.follow ? 1 : 0);\n const label = current === "watching" ? t("watching") : room2?.connection === "reconnecting" ? t("reconnecting") : solo() ? "Caisual" : room2?.code ?? "Caisual";\n surface.innerHTML = current === "boot" ? "" : `<div class="pill" data-reserve><button type="button" data-action="menu" aria-label="${t("menu")}" aria-expanded="${!!panel}">C<span aria-hidden="true"><small>${escape(label)}</small></span></button>${voiceEligible(manifest, model.session) && model.session?.kind === "room" ? `<button type="button" class="voice-toggle" data-voice-toggle data-action="panel:voice"></button>` : ""}${invitations ? `<button type="button" data-action="panel:friends" aria-label="${t("friends")} (${invitations})">${invitations}</button>` : ""}</div>\n ${current === "ended" && !panel ? `<div class="ended" data-reserve role="region" aria-label="${t("ended")}">${resultBar()}${boards?.state.saving ? `<small role="status" data-saving>${t(boards.state.saving)}</small>` : ""}${canPlayAgain(model.session) ? button("again", "again", \' class="primary"\') : model.session?.kind === "room" && room2?.status !== "finished" && !solo() ? `<small>${t("waitHost")}</small>` : ""}${rematchBar()}${Object.keys(manifest.boards).length ? button("panel:boards", "boards") : ""}${button("panel:home", "homeMenu")}</div>` : ""}\n ${panel ? `<div class="backdrop${panel === "home" ? " home" : ""}"><section class="dialog${panel === "boards" || panel === "friends" ? " wide" : ""}" role="dialog" aria-modal="true" aria-labelledby="panel-title" tabindex="-1"><div class="top"><h2 id="panel-title">${title(panel)}</h2><button type="button" data-action="close" aria-label="${t("close")}">\\xD7</button></div><div class="stack">${content(panel)}${model.error ? `<p class="error" role="alert" data-error>${t(errorText(model.error))}</p>` : ""}${model.session?.resumeError ? `<p class="error" role="alert">${t("saveFailed")}</p>` : ""}${model.notice ? `<p class="notice" role="status">${escape(model.notice)}</p>` : ""}</div></section></div>` : ""}`;\n for (const element of surface.querySelectorAll(".pill,.backdrop,.ended")) element.style.pointerEvents = "auto";\n const backdrop = root.querySelector(".backdrop.home");\n if (backdrop && input.configuration.coverUrl) backdrop.style.backgroundImage = `linear-gradient(#0b151c99,#0b151cee),url(${JSON.stringify(input.configuration.coverUrl)})`;\n host.dataset.phase = current;\n host.dataset.panel = panel ?? "";\n input.frame.inert = !!panel || oldInert;\n if (panel) input.frame.tabIndex = -1;\n else if (oldTabIndex === null) input.frame.removeAttribute("tabindex");\n else input.frame.setAttribute("tabindex", oldTabIndex);\n updateVoice();\n updateBoot(current === "boot");\n const dialog = root.querySelector(".dialog");\n if (dialog) dialog.scrollTop = previousScroll;\n const focusPanel = current === "boot" ? boot : dialog;\n const matched = focusKey ? [...root.querySelectorAll(`[data-${focusKey[0]}]`)].find((el) => el.getAttribute(`data-${focusKey[0]}`) === focusKey[1] && el.dataset.peer === focusPeer) : null;\n if (!panel && wasModal && !input.frame.inert && input.frame.isConnected) {\n input.frame.focus({ preventScroll: true });\n input.frame.contentWindow?.focus();\n } else if (matched && (!panel || focusPanel?.contains(matched)) && !matched.hasAttribute("disabled")) {\n matched.focus({ preventScroll: true });\n if (matched.tagName === "INPUT" && selection?.start !== null && selection?.end !== null && selection) matched.setSelectionRange(selection.start, selection.end);\n } else if (panel && (!wasModal || focused)) (current === "boot" ? boot : dialog?.querySelector(\'select,input,button:not([data-action="close"]):not(:disabled)\') ?? dialog)?.focus({ preventScroll: true });\n wasModal = !!panel;\n if (lastPhase !== current) {\n lastPhase = current;\n announce(current === "boot" ? "" : t({ home: "home", attaching: "joining", matching: "matching", lobby: "room", countdown: "starting", playing: "playing", ended: "ended", watching: "watching", error: "error" }[current]));\n }\n updateCountdown();\n resize();\n }\n const click = (event) => {\n const target = event.target.closest("button[data-action]");\n if (!target || target.disabled) return;\n const action = target.dataset.action;\n event.stopPropagation();\n if (action.startsWith("panel:")) {\n setPanel(action.slice(6));\n return;\n }\n switch (action) {\n case "menu":\n toggle();\n break;\n case "close":\n close();\n break;\n case "play": {\n const selected = primaryAction(manifest, model.mode);\n if (selected) void perform(selected.op, { mode: model.mode });\n break;\n }\n case "match":\n void perform("room.match", { mode: model.mode });\n break;\n case "join-invite":\n if (input.configuration.invite) void perform("room.join", { code: input.configuration.invite });\n break;\n case "resume":\n void perform("session.resume", {});\n break;\n case "cancel":\n void perform("session.cancel", {});\n break;\n case "ready":\n void perform("room.ready", { ready: !model.session?.room?.players.find((p) => p.id === model.session?.room?.you)?.ready });\n break;\n case "start":\n void perform("room.start", {});\n break;\n case "restart":\n void perform("room.restart", {});\n break;\n case "copy":\n void copyInvite();\n break;\n case "voice-join":\n void performVoice("voice.join", {});\n break;\n case "voice-mute":\n void performVoice("voice.mute", { muted: !model.session?.voice?.muted });\n break;\n case "voice-leave":\n void performVoice("voice.leave", {});\n break;\n case "again": {\n if (!canPlayAgain(model.session)) break;\n if (model.session?.kind === "local") void perform("local.start", { mode: model.session.mode ?? model.mode });\n else if (model.session?.room?.status === "finished") void perform("room.restart", {});\n else if (solo()) void perform("room.create", { mode: model.session?.room?.mode ?? null });\n else void perform("room.create", { mode: model.session?.room?.mode ?? null }, async () => {\n setPanel("invite");\n dispatch({ type: "notice", notice: t("newRoom") });\n await copyInvite();\n });\n break;\n }\n case "disconnect-exit":\n void perform("session.disconnect", {}, input.exit);\n break;\n case "leave-exit":\n void perform("session.leave", {}, input.exit);\n break;\n case "leave":\n void perform("session.leave", {}, () => setPanel("home"));\n break;\n case "exit-now":\n input.exit();\n break;\n case "reload":\n boot?.focus({ preventScroll: true });\n resetBootWait();\n input.frame.src = input.frame.src;\n break;\n case "refresh":\n void boards?.refresh();\n break;\n case "party-create":\n input.crew?.party.create();\n break;\n case "party-leave":\n input.crew?.party.leave();\n break;\n case "party-invite":\n input.crew?.party.invite(target.dataset.player);\n break;\n case "party-accept":\n input.crew?.party.accept(target.dataset.party);\n break;\n case "party-decline":\n input.crew?.party.decline(target.dataset.party);\n break;\n case "follow":\n input.crew?.follow(target.dataset.game, target.dataset.code);\n break;\n }\n };\n const change = (event) => {\n const target = event.target, field = target.dataset.control;\n if (field === "voice-volume") {\n target.dataset.editing = "true";\n void performVoice("voice.setVolume", { playerId: target.dataset.peer, volume: Number(target.value) }).finally(() => {\n delete target.dataset.editing;\n updateVoice();\n });\n return;\n }\n if (field === "mode") dispatch({ type: "mode", mode: target.value });\n if (field === "role") void perform(model.session?.room?.status === "lobby" ? "room.role" : "room.requestRole", { role: target.value });\n if (field === "team") void perform("room.team", { team: Number(target.value) });\n if (field === "shortcut") {\n const enabled = target.checked;\n try {\n win.localStorage.setItem("caisual-overlay-shortcut-v1", enabled ? "on" : "off");\n } catch {\n }\n dispatch({ type: "shortcut", enabled });\n }\n const query = boards?.state.query;\n if (query && ["board", "period", "category"].includes(field ?? "")) {\n const next = { ...query };\n if (field === "board") {\n next.board = target.value;\n next.period = (manifest.boards[next.board].periods ?? ["all-time"])[0];\n delete next.day;\n }\n if (field === "period") {\n next.period = target.value;\n delete next.day;\n }\n if (field === "category") next.guests = target.value === "guests";\n boards.select(next);\n }\n };\n const submit = (event) => {\n const form = event.target;\n if (!form.dataset.form) return;\n event.preventDefault();\n const code = normalizeInvite(form.querySelector(\'input[data-control="code"]\').value);\n if (!code) {\n dispatch({ type: "error", code: "invalid_code" });\n return;\n }\n void perform(form.dataset.form === "watch" ? "room.watch" : "room.join", { code });\n };\n const keydown = (event) => {\n const panel = visiblePanel(model);\n if (panel && event.key === "Escape") {\n event.preventDefault();\n event.stopImmediatePropagation();\n close();\n return;\n }\n if (panel && event.key === "Tab") {\n const items = controls().filter((el) => el.closest(panel === "boot" ? ".boot" : ".dialog")), first = items[0], last = items.at(-1);\n if (!first) {\n event.preventDefault();\n return;\n }\n if (event.shiftKey && (root.activeElement === first || !items.includes(root.activeElement))) {\n event.preventDefault();\n last?.focus();\n } else if (!event.shiftKey && (root.activeElement === last || !items.includes(root.activeElement))) {\n event.preventDefault();\n first.focus();\n }\n } else if (!panel && model.shortcutEnabled && event.key === "Tab" && event.shiftKey && !event.ctrlKey && !event.altKey && !event.metaKey) {\n event.preventDefault();\n toggle();\n }\n };\n root.addEventListener("input", (event) => {\n const node = event.target;\n if (node.dataset.control === "code") codeDraft = node.value;\n if (node.dataset.control === "voice-volume") node.dataset.editing = "true";\n });\n root.addEventListener("click", click);\n root.addEventListener("change", change);\n root.addEventListener("submit", submit);\n win.addEventListener("keydown", keydown, true);\n win.addEventListener("resize", resize);\n win.addEventListener("scroll", resize, true);\n win.visualViewport?.addEventListener("resize", resize);\n win.visualViewport?.addEventListener("scroll", resize);\n const observer = typeof ResizeObserver !== "undefined" ? new ResizeObserver(resize) : null;\n observer?.observe(input.frame);\n const countdownTimer = win.setInterval(updateCountdown, 250);\n stops.push(input.bridge.subscribe((session) => {\n const previous = model.session;\n if (!session || session.id !== previous?.id) {\n voiceOperation++;\n voicePending = null;\n voiceError = null;\n }\n if (!session) {\n lastView = "";\n boards?.reset();\n operation++;\n model.busy = false;\n }\n if (previous && session && JSON.stringify({ ...previous, voice: null }) === JSON.stringify({ ...session, voice: null })) {\n model = reduceUi(model, { type: "session", session });\n updateVoice();\n } else dispatch({ type: "session", session });\n }));\n stops.push(input.bridge.onOpen((panel) => setPanel(panel)), input.bridge.onShortcut(toggle), input.bridge.onError(({ error }) => {\n if (!visiblePanel(model)) setPanel("room");\n dispatch({ type: "error", code: error.code });\n }));\n stops.push(input.bridge.onScore((score) => boards?.queued(score)));\n if (input.crew) stops.push(input.crew.subscribe(() => {\n const state = input.crew.getSnapshot();\n if (state.follow || state.invites.length) announce(t("friends"));\n render();\n }));\n render();\n return { element: host, root, dispose() {\n disposed = true;\n operation++;\n stops.forEach((stop) => stop());\n boards?.dispose();\n observer?.disconnect();\n win.clearInterval(countdownTimer);\n win.cancelAnimationFrame(geometryFrame);\n win.clearTimeout(bootTimer);\n win.clearTimeout(bootFadeTimer);\n win.removeEventListener("keydown", keydown, true);\n win.removeEventListener("resize", resize);\n win.removeEventListener("scroll", resize, true);\n win.visualViewport?.removeEventListener("resize", resize);\n win.visualViewport?.removeEventListener("scroll", resize);\n input.frame.inert = oldInert;\n if (oldTabIndex === null) input.frame.removeAttribute("tabindex");\n else input.frame.setAttribute("tabindex", oldTabIndex);\n void input.bridge.request("overlay.view", { inputBlocked: false, reservedRects: [], shortcutEnabled: false }).catch(() => {\n });\n host.remove();\n } };\n}\nexport {\n avviaHandshake,\n creaPonteOspite,\n eMessaggioReady,\n eRichiestaBiglietto,\n mountOverlay,\n overlayConfiguration,\n overlayLanguage,\n overlayLocale,\n styles as overlayStyles,\n stanzaDaMessaggio\n};\n');
5082
+ response.end(request.method === "HEAD" ? void 0 : '// ../contracts/src/slug.ts\nvar NOMI_RISERVATI = [\n "www",\n "api",\n "app",\n "play",\n "live",\n "multi",\n "cdn",\n "assets",\n "static",\n "mail",\n "mx",\n "ns1",\n "ns2",\n "autodiscover",\n "_dmarc",\n "admin",\n "login",\n "account",\n "auth",\n "pay",\n "secure",\n "support",\n "help",\n "blog",\n "status",\n "dev",\n "staging",\n "test",\n "caisual",\n "shipz"\n];\nvar RISERVATI = new Set(NOMI_RISERVATI);\nvar SLUG_NUOVO = /^[a-z0-9]+(?:-[a-z0-9]+)*$/;\nvar SLUG_STORICO = /^[0-9a-f]{8}-[0-9a-f]{4}-4[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/;\nfunction isValidSlug(value) {\n return value.length >= 3 && value.length <= 32 && SLUG_NUOVO.test(value) || SLUG_STORICO.test(value);\n}\nfunction isReservedSlug(value) {\n return RISERVATI.has(value);\n}\n\n// ../contracts/src/i18n.ts\nfunction normalizeLanguage(value) {\n if (typeof value !== "string" || value.length > 128) return null;\n try {\n return Intl.getCanonicalLocales(value)[0] ?? null;\n } catch {\n return null;\n }\n}\nfunction manifestLanguages(manifest) {\n return manifest.languages?.length ? [...manifest.languages] : [manifest.language ?? "en"];\n}\nfunction languageFallbacks(language, defaultLanguage = "en") {\n const result = [];\n let tag = normalizeLanguage(language);\n while (tag) {\n result.push(tag);\n const parts = tag.split("-");\n parts.pop();\n if (parts.at(-1)?.length === 1) parts.pop();\n tag = parts.join("-");\n }\n result.push(normalizeLanguage(defaultLanguage) ?? defaultLanguage);\n return [...new Set(result)];\n}\nfunction resolveText(value, language, defaultLanguage = "en", key = "") {\n if (typeof value === "string") return value;\n if (value) {\n for (const tag of languageFallbacks(language, defaultLanguage)) {\n const name = Object.keys(value).find((name2) => name2.toLowerCase() === tag.toLowerCase());\n if (name !== void 0 && typeof value[name] === "string") return value[name];\n }\n }\n return key;\n}\n\n// ../contracts/src/manifest.ts\nfunction risolviModalita(manifest, mode) {\n const scelta = mode === null ? void 0 : manifest.modes.find((voce) => voce.id === mode);\n if (mode !== null && scelta === void 0) throw new Error("The selected game mode does not exist.");\n return { players: { ...scelta?.players ?? manifest.players }, lobby: scelta?.lobby ?? manifest.lobby };\n}\nfunction risolviPresentazione(manifest, mode, language = manifestLanguages(manifest)[0]) {\n risolviModalita(manifest, mode);\n const scelta = manifest.modes.find((voce) => voce.id === mode);\n return {\n execution: scelta?.execution ?? null,\n label: resolveText(scelta?.label, language, manifestLanguages(manifest)[0], scelta?.id ?? manifest.name ?? "Play"),\n instructions: resolveText(scelta?.instructions, language, manifestLanguages(manifest)[0]) || null\n };\n}\nvar TETTO_GIOCATORI = 24;\nvar RITARDO_SPETTATORI_MS = 3e3;\nvar MASSIMO_CLASSIFICHE = 32;\nvar CAMPI = /* @__PURE__ */ new Set([\n "overlay",\n "manifest",\n "id",\n "name",\n "description",\n "cover",\n "card",\n "icon",\n "screenshots",\n "tags",\n "languages",\n "language",\n "platform",\n "orientation",\n "input",\n "visibility",\n "network",\n "isolated",\n "requires",\n "players",\n "lobby",\n "persistent",\n "spectators",\n "boards",\n "roles",\n "teams",\n "voice",\n "modes"\n]);\nvar INPUT = /* @__PURE__ */ new Set(["keyboard", "mouse", "touch", "gamepad"]);\nvar PLATFORM = /* @__PURE__ */ new Set(["desktop", "mobile", "both"]);\nvar ORIENTATION = /* @__PURE__ */ new Set(["landscape", "portrait"]);\nvar VISIBILITY = /* @__PURE__ */ new Set(["public", "unlisted"]);\nvar VOICE = /* @__PURE__ */ new Set(["none", "room", "team", "proximity"]);\nvar PERFORMANCE = /* @__PURE__ */ new Set(["light", "medium", "heavy"]);\nvar TAG = /^[a-z0-9-]+$/;\nvar ID_INTERNO = /^[a-z0-9]+(?:-[a-z0-9]+)*$/;\nvar CAMPO_MATCHMAKING = /^[a-z0-9][a-z0-9-]{0,31}$/;\nvar ID_CLASSIFICA = /^[a-z0-9][a-z0-9_-]{0,31}$/;\nfunction oggetto(value) {\n if (typeof value !== "object" || value === null || Array.isArray(value)) return null;\n return value;\n}\nfunction percorsoRelativo(value) {\n if (value === "" || value.startsWith("/") || value.includes("\\\\") || value.includes("\\0")) return false;\n if (value.includes("?") || value.includes("#")) return false;\n const parti = value.split("/");\n if (parti.some((parte) => parte === "" || parte === "." || parte === "..")) return false;\n try {\n const decoded = parti.map((parte) => decodeURIComponent(parte));\n return !decoded.some((parte) => parte === "" || parte === "." || parte === ".." || parte.includes("/"));\n } catch {\n return false;\n }\n}\nfunction hostValido(value) {\n if (value.length === 0 || value.length > 253) return false;\n if (value.includes("://") || /[/:?#@]/.test(value)) return false;\n const parti = value.split(".");\n return parti.every(\n (parte) => parte.length >= 1 && parte.length <= 63 && /^[a-z0-9](?:[a-z0-9-]*[a-z0-9])?$/i.test(parte)\n );\n}\nfunction interoTra(value, min, max) {\n return typeof value === "number" && Number.isInteger(value) && value >= min && value <= max;\n}\nfunction stringaDefault(dati, campo, valoreDefault, errori) {\n const value = dati[campo];\n if (value === void 0) return valoreDefault;\n if (typeof value !== "string") {\n errori.push(`${campo}: must be a string.`);\n return valoreDefault;\n }\n return value;\n}\nfunction testoFacoltativo(value, key, max, path, errors) {\n if (value[key] === void 0) return void 0;\n const check = (text2, field2) => {\n if (typeof text2 !== "string" || text2.trim().length === 0 || text2.trim().length > max || /[\\r\\n\\u0000-\\u001f]/.test(text2)) {\n errors.push(`${field2}: must contain 1-${max} characters on one line.`);\n return void 0;\n }\n return text2.trim();\n };\n const text = value[key], field = `${path}.${key}`;\n if (typeof text === "string") return check(text, field);\n const translations = oggetto(text);\n if (!translations || Object.keys(translations).length === 0) {\n errors.push(`${field}: must be a string or a non-empty language-to-text object.`);\n return void 0;\n }\n const result = {};\n for (const [raw, text2] of Object.entries(translations)) {\n const tag = normalizeLanguage(raw);\n if (!tag) {\n errors.push(`${field}.${raw}: must be a BCP 47 language tag.`);\n continue;\n }\n if (Object.hasOwn(result, tag)) errors.push(`${field}.${raw}: duplicate language.`);\n const checked = check(text2, `${field}.${raw}`);\n if (checked !== void 0) result[tag] = checked;\n }\n return result;\n}\nfunction validaManifest(valore) {\n const errori = [];\n const dati = oggetto(valore);\n if (dati === null) return { ok: false, errori: ["manifest: must be a JSON object."] };\n for (const campo of Object.keys(dati)) {\n if (!CAMPI.has(campo)) errori.push(`${campo}: unknown field.`);\n }\n if (dati.manifest === void 0) errori.push("manifest: is required and must be 1.");\n else if (dati.manifest !== 1) errori.push("manifest: must be exactly 1.");\n const id = stringaDefault(dati, "id", "", errori);\n if (dati.id === void 0) errori.push("id: is required.");\n else if (typeof dati.id === "string") {\n if (!isValidSlug(id)) {\n errori.push("id: must be 3-32 lowercase ASCII letters or digits with internal hyphens; historical UUID v4 slugs may be 36 characters.");\n } else if (isReservedSlug(id)) errori.push("id: this slug is reserved.");\n }\n const name = stringaDefault(dati, "name", "", errori);\n if (dati.name === void 0) errori.push("name: is required.");\n else if (typeof dati.name === "string" && (name.trim() === "" || name.length > 60)) {\n errori.push("name: must contain 1-60 characters.");\n }\n const description = stringaDefault(dati, "description", "", errori);\n if (description.length > 500) errori.push("description: must be at most 500 characters.");\n const immagini = { cover: "", card: "", icon: "" };\n const usati = /* @__PURE__ */ new Set();\n for (const campo of ["cover", "card", "icon"]) {\n const path = dati[campo];\n if (path === void 0 || path === null) errori.push(`${campo}: is required.`);\n else if (typeof path !== "string" || !percorsoRelativo(path)) errori.push(`${campo}: must be a relative file path inside client/ without query, fragment, or parent segments.`);\n else {\n if (!/\\.(png|jpe?g|webp)$/i.test(path)) errori.push(`${campo}: must be a PNG, JPEG or WebP file.`);\n const canonical = decodeURIComponent(path);\n if (usati.has(canonical)) errori.push(`${campo}: each image must use a different file; cover, card and icon cannot share a path.`);\n usati.add(canonical);\n immagini[campo] = path;\n }\n }\n const { cover, card, icon } = immagini;\n const screenshots = [];\n if (dati.screenshots !== void 0) {\n if (!Array.isArray(dati.screenshots)) errori.push("screenshots: must be an array of relative file paths.");\n else {\n if (dati.screenshots.length > 8) errori.push("screenshots: must contain at most 8 paths.");\n for (const [indice, value] of dati.screenshots.entries()) {\n if (typeof value !== "string" || !percorsoRelativo(value)) {\n errori.push(`screenshots[${indice}]: must be a relative file path without query, fragment, or parent segments.`);\n } else screenshots.push(value);\n }\n }\n }\n const tags = [];\n if (dati.tags !== void 0) {\n if (!Array.isArray(dati.tags)) errori.push("tags: must be an array.");\n else {\n if (dati.tags.length > 10) errori.push("tags: must contain at most 10 tags.");\n for (const [indice, value] of dati.tags.entries()) {\n if (typeof value !== "string" || value.length > 24 || !TAG.test(value)) {\n errori.push(`tags[${indice}]: must be 1-24 lowercase letters, digits, or hyphens.`);\n } else tags.push(value);\n }\n }\n }\n const legacyLanguage = stringaDefault(dati, "language", "en", errori);\n if (!/^[A-Za-z]{2,8}(?:-[A-Za-z0-9]{1,8})*$/.test(legacyLanguage)) {\n errori.push("language: must be a BCP 47 language tag such as en, it, or pt-BR.");\n }\n const languages2 = [];\n if (!Array.isArray(dati.languages) || dati.languages.length === 0) {\n errori.push("languages: must be a non-empty array of BCP 47 language tags.");\n } else for (const [index, raw] of dati.languages.entries()) {\n const tag = normalizeLanguage(raw);\n if (!tag) errori.push(`languages[${index}]: must be a BCP 47 language tag.`);\n else if (languages2.includes(tag)) errori.push(`languages[${index}]: duplicate language ${tag}.`);\n else languages2.push(tag);\n }\n if (!languages2.includes("en")) errori.push("languages: English is always required alongside the game\'s own languages.");\n const language = languages2[0] ?? legacyLanguage;\n if (dati.language !== void 0 && dati.languages !== void 0 && legacyLanguage.toLowerCase() !== language.toLowerCase()) {\n errori.push("language: must match the first entry in languages when both are present.");\n }\n let platform = "both";\n if (dati.platform === void 0) errori.push("platform: is required.");\n else if (typeof dati.platform !== "string" || !PLATFORM.has(dati.platform)) {\n errori.push("platform: must be desktop, mobile, or both.");\n } else platform = dati.platform;\n let orientation = "landscape";\n if (dati.orientation !== void 0) {\n if (typeof dati.orientation !== "string" || !ORIENTATION.has(dati.orientation)) {\n errori.push("orientation: must be landscape or portrait.");\n } else orientation = dati.orientation;\n }\n const input = [];\n if (dati.input !== void 0) {\n if (!Array.isArray(dati.input)) errori.push("input: must be an array.");\n else for (const [indice, value] of dati.input.entries()) {\n if (typeof value !== "string" || !INPUT.has(value)) {\n errori.push(`input[${indice}]: must be keyboard, mouse, touch, or gamepad.`);\n } else if (input.includes(value)) errori.push(`input[${indice}]: duplicate value ${value}.`);\n else input.push(value);\n }\n }\n let visibility = "public";\n if (dati.visibility !== void 0) {\n if (typeof dati.visibility !== "string" || !VISIBILITY.has(dati.visibility)) {\n errori.push("visibility: must be public or unlisted.");\n } else visibility = dati.visibility;\n }\n const network = [];\n if (dati.network !== void 0) {\n if (!Array.isArray(dati.network)) errori.push("network: must be an array of host names.");\n else for (const [indice, value] of dati.network.entries()) {\n if (typeof value !== "string" || !hostValido(value)) {\n errori.push(`network[${indice}]: must be a host name without scheme, port, path, query, or fragment.`);\n } else if (network.includes(value)) errori.push(`network[${indice}]: duplicate host ${value}.`);\n else network.push(value);\n }\n }\n let isolated = false;\n if (dati.isolated !== void 0) {\n if (typeof dati.isolated !== "boolean") errori.push("isolated: must be a boolean.");\n else isolated = dati.isolated;\n }\n const requires = {\n webgl2: false,\n webgpu: false,\n wasm: false,\n threads: false,\n memoryMb: null,\n performance: "light"\n };\n if (dati.requires !== void 0) {\n const value = oggetto(dati.requires);\n if (value === null) errori.push("requires: must be an object.");\n else {\n for (const campo of Object.keys(value)) {\n if (!["webgl2", "webgpu", "wasm", "threads", "memoryMb", "performance"].includes(campo)) {\n errori.push(`requires.${campo}: unknown field.`);\n }\n }\n for (const campo of ["webgl2", "webgpu", "wasm", "threads"]) {\n if (value[campo] === void 0) continue;\n if (typeof value[campo] !== "boolean") errori.push(`requires.${campo}: must be a boolean.`);\n else requires[campo] = value[campo];\n }\n if (value.memoryMb !== void 0) {\n if (value.memoryMb !== null && (!interoTra(value.memoryMb, 512, 32768) || value.memoryMb % 256 !== 0)) {\n errori.push("requires.memoryMb: must be an integer from 512 to 32768 in steps of 256, or null.");\n } else requires.memoryMb = value.memoryMb;\n }\n if (value.performance !== void 0) {\n if (typeof value.performance !== "string" || !PERFORMANCE.has(value.performance)) {\n errori.push("requires.performance: must be light, medium, or heavy.");\n } else requires.performance = value.performance;\n }\n if (requires.threads && !isolated) errori.push("requires.threads: needs isolated: true.");\n }\n }\n let players = { min: 1, max: 1 };\n if (dati.players !== void 0) {\n const value = oggetto(dati.players);\n if (value === null) errori.push("players: must be an object with min and max.");\n else {\n for (const campo of Object.keys(value)) {\n if (campo !== "min" && campo !== "max") errori.push(`players.${campo}: unknown field.`);\n }\n if (!interoTra(value.min, 1, TETTO_GIOCATORI)) errori.push(`players.min: must be an integer from 1 to ${TETTO_GIOCATORI}.`);\n if (!interoTra(value.max, 1, TETTO_GIOCATORI)) errori.push(`players.max: must be an integer from 1 to ${TETTO_GIOCATORI} in manifest version 1.`);\n if (interoTra(value.min, 1, TETTO_GIOCATORI) && interoTra(value.max, 1, TETTO_GIOCATORI)) {\n if (value.min > value.max) errori.push("players.max: must be greater than or equal to players.min.");\n else players = { min: value.min, max: value.max };\n }\n }\n }\n let lobby = false;\n if (dati.lobby !== void 0) {\n if (typeof dati.lobby !== "boolean") errori.push("lobby: must be a boolean.");\n else lobby = dati.lobby;\n }\n let persistent = false;\n if (dati.persistent !== void 0) {\n if (typeof dati.persistent !== "boolean") errori.push("persistent: must be a boolean.");\n else persistent = dati.persistent;\n }\n let spectators = { delayMs: RITARDO_SPETTATORI_MS };\n if (dati.spectators === false || dati.spectators === null) spectators = null;\n else if (dati.spectators !== void 0 && dati.spectators !== true) {\n const value = oggetto(dati.spectators);\n if (value === null) {\n errori.push("spectators: must be a boolean or an object with delayMs.");\n } else {\n for (const campo of Object.keys(value)) {\n if (campo !== "delayMs") errori.push(`spectators.${campo}: unknown field.`);\n }\n if (!interoTra(value.delayMs, 0, 3e4)) {\n errori.push("spectators.delayMs: must be an integer from 0 to 30000.");\n } else spectators = { delayMs: value.delayMs };\n }\n }\n let overlay = null;\n if (dati.overlay !== void 0 && dati.overlay !== null) {\n const value = oggetto(dati.overlay);\n if (value === null) errori.push("overlay: must be an object or null.");\n else {\n for (const key of Object.keys(value)) if (!["version", "accent"].includes(key)) errori.push(`overlay.${key}: unknown field.`);\n if (value.version !== 1) errori.push("overlay.version: must be exactly 1.");\n if (value.accent !== void 0 && (typeof value.accent !== "string" || !/^#[0-9a-fA-F]{6}$/.test(value.accent))) {\n errori.push("overlay.accent: must be a six-digit hexadecimal color, such as #336699.");\n }\n overlay = { version: 1, ...typeof value.accent === "string" ? { accent: value.accent } : {} };\n }\n }\n const boards = {};\n if (dati.boards !== void 0) {\n const value = oggetto(dati.boards);\n if (value === null) errori.push("boards: must be an object of board ids.");\n else {\n if (Object.keys(value).length > MASSIMO_CLASSIFICHE) {\n errori.push(`boards: at most ${MASSIMO_CLASSIFICHE} boards.`);\n }\n for (const [id2, raw] of Object.entries(value)) {\n let valido = true;\n if (!ID_CLASSIFICA.test(id2)) {\n errori.push(`boards.${id2}: invalid board id.`);\n valido = false;\n }\n const board = oggetto(raw);\n if (board === null) {\n errori.push(`boards.${id2}.source: must be "client" or "server".`);\n continue;\n }\n for (const campo of Object.keys(board)) {\n if (!["source", "label", "periods", "day"].includes(campo)) errori.push(`boards.${id2}.${campo}: unknown field.`);\n }\n if (board.source !== "client" && board.source !== "server") {\n errori.push(`boards.${id2}.source: must be "client" or "server".`);\n valido = false;\n }\n if (board.day !== void 0 && board.day !== "submit" && board.day !== "start") errori.push(`boards.${id2}.day: must be "submit" or "start".`);\n if (board.day === "start" && board.source !== "server") errori.push(`boards.${id2}.day: start requires source "server".`);\n const label = testoFacoltativo(board, "label", 48, `boards.${id2}`, errori);\n let periods = ["all-time"];\n if (board.periods !== void 0) {\n if (!Array.isArray(board.periods) || board.periods.length < 1 || board.periods.length > 2 || board.periods.some((period) => period !== "daily" && period !== "all-time") || new Set(board.periods).size !== board.periods.length) {\n errori.push(`boards.${id2}.periods: must contain daily, all-time, or both without duplicates.`);\n } else periods = [...board.periods];\n }\n if (valido) Object.defineProperty(boards, id2, { value: {\n source: board.source,\n periods,\n ...board.day === void 0 ? {} : { day: board.day },\n ...label === void 0 ? {} : { label }\n }, enumerable: true, configurable: true, writable: true });\n }\n }\n }\n const roles = [];\n if (dati.roles !== void 0) {\n if (!Array.isArray(dati.roles)) errori.push("roles: must be an array.");\n else {\n const ids = /* @__PURE__ */ new Set();\n for (const [indice, raw] of dati.roles.entries()) {\n const value = oggetto(raw);\n if (value === null) {\n errori.push(`roles[${indice}]: must be an object.`);\n continue;\n }\n for (const campo of Object.keys(value)) {\n if (!["id", "min", "max", "label"].includes(campo)) errori.push(`roles[${indice}].${campo}: unknown field.`);\n }\n const idRuolo = value.id;\n const min = value.min;\n const max = value.max;\n let valido = true;\n if (typeof idRuolo !== "string" || idRuolo.length > 32 || !ID_INTERNO.test(idRuolo)) {\n errori.push(`roles[${indice}].id: must be 1-32 lowercase letters, digits, or internal hyphens.`);\n valido = false;\n } else if (ids.has(idRuolo)) {\n errori.push(`roles[${indice}].id: duplicate role ${idRuolo}.`);\n valido = false;\n } else ids.add(idRuolo);\n if (!interoTra(min, 0, TETTO_GIOCATORI)) {\n errori.push(`roles[${indice}].min: must be an integer from 0 to ${TETTO_GIOCATORI}.`);\n valido = false;\n }\n if (max !== void 0 && !interoTra(max, 0, TETTO_GIOCATORI)) {\n errori.push(`roles[${indice}].max: must be an integer from 0 to ${TETTO_GIOCATORI} when present.`);\n valido = false;\n }\n if (typeof min === "number" && typeof max === "number" && min > max) {\n errori.push(`roles[${indice}].max: must be greater than or equal to min.`);\n valido = false;\n }\n const label = testoFacoltativo(value, "label", 32, `roles[${indice}]`, errori);\n if (valido) roles.push({\n id: idRuolo,\n min,\n ...max === void 0 ? {} : { max },\n ...label === void 0 ? {} : { label }\n });\n }\n }\n }\n let teams = null;\n if (dati.teams !== void 0 && dati.teams !== null) {\n const value = oggetto(dati.teams);\n if (value === null) errori.push("teams: must be null or an object with min and max.");\n else {\n for (const campo of Object.keys(value)) {\n if (campo !== "min" && campo !== "max") errori.push(`teams.${campo}: unknown field.`);\n }\n if (!interoTra(value.min, 2, TETTO_GIOCATORI)) errori.push(`teams.min: must be an integer from 2 to ${TETTO_GIOCATORI}.`);\n if (!interoTra(value.max, 2, TETTO_GIOCATORI)) errori.push(`teams.max: must be an integer from 2 to ${TETTO_GIOCATORI}.`);\n if (interoTra(value.min, 2, TETTO_GIOCATORI) && interoTra(value.max, 2, TETTO_GIOCATORI)) {\n if (value.min > value.max) errori.push("teams.max: must be greater than or equal to teams.min.");\n else teams = { min: value.min, max: value.max };\n }\n }\n }\n let voice = "none";\n if (dati.voice !== void 0) {\n if (typeof dati.voice !== "string" || !VOICE.has(dati.voice)) {\n errori.push("voice: must be none, room, team, or proximity.");\n } else voice = dati.voice;\n }\n const modes = [];\n if (dati.modes !== void 0) {\n if (!Array.isArray(dati.modes)) errori.push("modes: must be an array.");\n else {\n const ids = /* @__PURE__ */ new Set();\n for (const [indice, raw] of dati.modes.entries()) {\n const value = oggetto(raw);\n if (value === null) {\n errori.push(`modes[${indice}]: must be an object.`);\n continue;\n }\n for (const campo of Object.keys(value)) {\n if (!["id", "players", "lobby", "matchmaking", "execution", "label", "instructions"].includes(campo)) errori.push(`modes[${indice}].${campo}: unknown field.`);\n }\n if (typeof value.id !== "string" || value.id.length > 32 || !ID_INTERNO.test(value.id)) {\n errori.push(`modes[${indice}].id: must be 1-32 lowercase letters, digits, or internal hyphens.`);\n continue;\n }\n if (ids.has(value.id)) {\n errori.push(`modes[${indice}].id: duplicate mode ${value.id}.`);\n continue;\n }\n ids.add(value.id);\n const modo = { id: value.id };\n for (const [key2, max] of [["label", 48], ["instructions", 160]]) {\n const text = testoFacoltativo(value, key2, max, `modes[${indice}]`, errori);\n if (text !== void 0) modo[key2] = text;\n }\n if (value.execution !== void 0) {\n if (value.execution !== "local" && value.execution !== "room") errori.push(`modes[${indice}].execution: must be local or room.`);\n else modo.execution = value.execution;\n }\n if (overlay !== null && modo.execution === void 0) errori.push(`modes[${indice}].execution: is required with the standard overlay.`);\n if (value.players !== void 0) {\n const campo = `modes[${indice}].players`;\n const range = oggetto(value.players);\n if (range === null) errori.push(`${campo}: must be an object with min and max.`);\n else {\n for (const key2 of Object.keys(range)) {\n if (key2 !== "min" && key2 !== "max") errori.push(`${campo}.${key2}: unknown field.`);\n }\n if (!interoTra(range.min, 1, TETTO_GIOCATORI)) errori.push(`${campo}.min: must be an integer from 1 to ${TETTO_GIOCATORI}.`);\n if (!interoTra(range.max, 1, TETTO_GIOCATORI)) errori.push(`${campo}.max: must be an integer from 1 to ${TETTO_GIOCATORI}.`);\n if (interoTra(range.min, 1, TETTO_GIOCATORI) && interoTra(range.max, 1, TETTO_GIOCATORI)) {\n if (range.min > range.max) errori.push(`${campo}.max: must be greater than or equal to min.`);\n else modo.players = { min: range.min, max: range.max };\n }\n }\n }\n if (value.lobby !== void 0) {\n if (typeof value.lobby !== "boolean") errori.push(`modes[${indice}].lobby: must be a boolean.`);\n else modo.lobby = value.lobby;\n }\n if (modo.execution === "local") {\n const range = modo.players ?? players;\n if (range.min !== 1 || range.max !== 1) errori.push(`modes[${indice}].players: local execution requires min and max to be 1.`);\n if (modo.lobby ?? lobby) errori.push(`modes[${indice}].lobby: local execution requires false.`);\n if (value.matchmaking !== void 0) errori.push(`modes[${indice}].matchmaking: local execution cannot use matchmaking.`);\n }\n if (value.matchmaking === void 0) {\n modes.push(modo);\n continue;\n }\n const matchmaking = oggetto(value.matchmaking);\n if (matchmaking === null) {\n errori.push(`modes[${indice}].matchmaking: must be an object.`);\n continue;\n }\n for (const campo of Object.keys(matchmaking)) {\n if (!["key", "timeoutMs", "defaults"].includes(campo)) {\n errori.push(`modes[${indice}].matchmaking.${campo}: unknown field.`);\n }\n }\n let valido = true;\n const key = [];\n if (!Array.isArray(matchmaking.key) || matchmaking.key.length < 1 || matchmaking.key.length > 8) {\n errori.push(`modes[${indice}].matchmaking.key: must contain from 1 to 8 fields.`);\n valido = false;\n } else for (const [keyIndice, item] of matchmaking.key.entries()) {\n if (typeof item !== "string" || !CAMPO_MATCHMAKING.test(item)) {\n errori.push(`modes[${indice}].matchmaking.key[${keyIndice}]: must be 1-32 lowercase letters, digits, or hyphens and start with a letter or digit.`);\n valido = false;\n } else if (key.includes(item)) {\n errori.push(`modes[${indice}].matchmaking.key[${keyIndice}]: duplicate field ${item}.`);\n valido = false;\n } else key.push(item);\n }\n if (!interoTra(matchmaking.timeoutMs, 1e3, 3e5)) {\n errori.push(`modes[${indice}].matchmaking.timeoutMs: must be an integer from 1000 to 300000.`);\n valido = false;\n }\n let defaults;\n if (matchmaking.defaults !== void 0) {\n const values = oggetto(matchmaking.defaults);\n if (values === null || Object.keys(values).length !== key.length || key.some((field) => !Object.hasOwn(values, field))) {\n errori.push(`modes[${indice}].matchmaking.defaults: must contain exactly the declared key fields.`);\n } else {\n defaults = {};\n for (const [field, value2] of Object.entries(values)) {\n if (!(typeof value2 === "string" && value2.length >= 1 && value2.length <= 64 && /^[A-Za-z0-9_.:-]+$/.test(value2)) && !Number.isSafeInteger(value2)) {\n errori.push(`modes[${indice}].matchmaking.defaults.${field}: must be a string of 1-64 characters or a safe integer.`);\n } else Object.defineProperty(defaults, field, { value: value2, enumerable: true });\n }\n }\n }\n if (valido) modes.push({ ...modo, matchmaking: {\n ...defaults === void 0 ? {} : { defaults },\n key,\n timeoutMs: matchmaking.timeoutMs\n } });\n }\n }\n }\n if (overlay !== null && modes.length === 0) errori.push("modes: at least one explicit mode is required with the standard overlay.");\n if (errori.length > 0) return { ok: false, errori };\n return { ok: true, manifest: {\n manifest: 1,\n overlay,\n id,\n name,\n description,\n cover,\n card,\n icon,\n screenshots,\n tags,\n languages: languages2,\n language,\n platform,\n orientation,\n input,\n visibility,\n network,\n isolated,\n requires,\n players,\n lobby,\n persistent,\n spectators,\n boards,\n roles,\n teams,\n voice,\n modes\n } };\n}\n\n// ../contracts/src/overlay.ts\nvar OVERLAY_PANELS = ["home", "room", "invite", "friends", "voice", "boards"];\nfunction overlayConfiguration(manifest, coverUrl = null, invite = null, iconUrl = null) {\n const validated = validaManifest(manifest);\n if (!validated.ok) throw new Error("The overlay manifest is invalid.");\n return { manifest: validated.manifest, coverUrl, iconUrl, invite };\n}\nfunction record(value) {\n return typeof value === "object" && value !== null && !Array.isArray(value) ? value : null;\n}\nfunction validSafeArea(value) {\n const area = record(value);\n return area !== null && Object.keys(area).length === 4 && ["top", "right", "bottom", "left"].every((key) => typeof area[key] === "number" && Number.isFinite(area[key]) && Number(area[key]) >= 0 && Number(area[key]) <= 1e5);\n}\nfunction validOverlayView(value) {\n const data = record(value);\n return data !== null && Object.keys(data).every((key) => ["inputBlocked", "reservedRects", "safeArea", "shortcutEnabled"].includes(key)) && (data.safeArea === void 0 || validSafeArea(data.safeArea)) && (data.shortcutEnabled === void 0 || typeof data.shortcutEnabled === "boolean") && typeof data.inputBlocked === "boolean" && Array.isArray(data.reservedRects) && data.reservedRects.length <= 8 && data.reservedRects.every((value2) => {\n const rect = record(value2);\n return rect !== null && Object.keys(rect).length === 4 && ["x", "y", "width", "height"].every((key) => typeof rect[key] === "number" && Number.isFinite(rect[key]) && rect[key] >= 0 && rect[key] <= 1e5);\n });\n}\nfunction validOverlayRequest(value) {\n const message = record(value), args = record(message?.args);\n if (message?.type !== "caisual:overlay" || message.v !== 1 || typeof message.epoch !== "string" || message.epoch.length < 1 || message.epoch.length > 128 || typeof message.requestId !== "string" || !(/^[1-9][0-9]{0,15}$/.test(message.requestId) && Number.isSafeInteger(Number(message.requestId))) || args === null) return false;\n if (Object.keys(message).some((key) => !["type", "v", "epoch", "requestId", "sessionId", "op", "args"].includes(key)) || !(message.sessionId === void 0 || message.sessionId === null || typeof message.sessionId === "string" && /^[1-9][0-9]{0,15}$/.test(message.sessionId))) return false;\n const keys = (...allowed) => Object.keys(args).every((key) => allowed.includes(key));\n const text = (key) => typeof args[key] === "string" && args[key].length >= 1 && args[key].length <= 64;\n switch (message.op) {\n case "local.start":\n return keys("mode") && text("mode");\n case "room.create":\n return keys("mode") && (args.mode === null || text("mode"));\n case "room.join":\n return keys("code") && (args.code === void 0 || text("code"));\n case "room.watch":\n return keys("code") && text("code");\n case "room.match": {\n const key = record(args.key);\n return keys("mode", "key") && text("mode") && (args.key === void 0 || key !== null && Object.keys(key).length <= 8 && Object.values(key).every((v) => typeof v === "string" && v.length >= 1 && v.length <= 64 || typeof v === "number" && Number.isSafeInteger(v)));\n }\n case "room.ready":\n return keys("ready") && typeof args.ready === "boolean";\n case "room.role":\n case "room.requestRole":\n return keys("role") && text("role");\n case "room.team":\n return keys("team") && Number.isInteger(args.team) && args.team >= 1 && args.team <= 24;\n case "room.restart":\n case "room.start":\n case "session.cancel":\n case "session.leave":\n case "session.disconnect":\n case "session.resume":\n return keys();\n case "voice.join":\n case "voice.leave":\n return keys() && typeof message.sessionId === "string";\n case "voice.mute":\n return keys("muted") && typeof args.muted === "boolean" && typeof message.sessionId === "string";\n case "voice.setVolume":\n return keys("playerId", "volume") && typeof message.sessionId === "string" && typeof args.playerId === "string" && args.playerId.length > 0 && args.playerId.length <= 128 && typeof args.volume === "number" && Number.isFinite(args.volume) && args.volume >= 0 && args.volume <= 1;\n case "overlay.view":\n return validOverlayView(args);\n default:\n return false;\n }\n}\nfunction validBoardDay(value) {\n if (typeof value !== "string" || !/^\\d{4}-\\d{2}-\\d{2}$/.test(value)) return false;\n const at = Date.parse(`${value}T00:00:00Z`);\n return Number.isFinite(at) && new Date(at).toISOString().slice(0, 10) === value;\n}\nfunction validOverlaySessionState(value) {\n const data = record(value);\n const exact = (v, keys) => v !== null && Object.keys(v).length === keys.length && Object.keys(v).every((key) => keys.includes(key));\n const text = (v) => typeof v === "string" && v.length <= 128;\n const nullable = (v) => v === null || text(v);\n const finite = (v) => typeof v === "number" && Number.isFinite(v);\n if (!data || !exact(data, ["kind", "id", "mode", "localStatus", "ready", "capabilities", "room", "waiting", "resume", "resumeError", ..."voice" in data ? ["voice"] : []])) return false;\n if (data.voice !== void 0 && data.voice !== null && (data.kind !== "room" || !record(data.room) || !validOverlayVoice(data.voice))) return false;\n const capabilities = record(data.capabilities), room = record(data.room), waiting = record(data.waiting), resume = record(data.resume);\n if (!["boot", "home", "attaching", "matching", "local", "room", "watch"].includes(String(data.kind)) || !nullable(data.id) || !nullable(data.mode) || ![null, "playing", "ended"].includes(data.localStatus) || typeof data.ready !== "boolean" || typeof data.resumeError !== "boolean" || !exact(capabilities, ["local", "rooms", "overlay", "requestRole"]) || !Object.values(capabilities).every((v) => typeof v === "boolean")) return false;\n if (data.waiting !== null && (!exact(waiting, ["players", "min", "max"]) || !Object.values(waiting).every((v) => Number.isInteger(v) && Number(v) >= 0 && Number(v) <= 24))) return false;\n if (data.resume !== null && (!exact(resume, ["version", "code", "mode", "updatedAt"]) || resume.version !== 1 || !text(resume.code) || !nullable(resume.mode) || !finite(resume.updatedAt))) return false;\n if (data.room === null) return true;\n if (!exact(room, ["code", "mode", "status", "host", "you", "players", "countdownAt", "connection", "closedCode", "limits", "lobby", "persistent", "delayMs", "requestRole", ..."result" in (room ?? {}) ? ["result"] : [], ..."rematch" in (room ?? {}) ? ["rematch"] : []]) || !room) return false;\n const limits = record(room.limits), rematch = record(room.rematch);\n if (room.rematch !== void 0 && room.rematch !== null && (!exact(rematch, ["keepSetup", "autoStart"]) || typeof rematch.keepSetup !== "boolean" || typeof rematch.autoStart !== "boolean")) return false;\n return text(room.code) && nullable(room.mode) && nullable(room.host) && nullable(room.you) && ["lobby", "countdown", "playing", "finished", "ended"].includes(String(room.status)) && ["connecting", "connected", "reconnecting", "disconnected", "ended", "closed", "replaced"].includes(String(room.connection)) && ["countdownAt", "closedCode", "delayMs"].every((key) => room[key] === null || finite(room[key])) && ["lobby", "persistent", "requestRole"].every((key) => typeof room[key] === "boolean") && exact(limits, ["min", "max"]) && Object.values(limits).every((v) => Number.isInteger(v) && Number(v) >= 1 && Number(v) <= 24) && Array.isArray(room.players) && room.players.length <= 24 && room.players.every((value2) => {\n const player = record(value2);\n return exact(player, ["id", "name", "guest", "role", "team", "ready", "connected"]) && player !== null && text(player.id) && text(player.name) && nullable(player.role) && (player.team === null || Number.isInteger(player.team) && Number(player.team) >= 1 && Number(player.team) <= 24) && ["guest", "ready", "connected"].every((key) => typeof player[key] === "boolean");\n });\n}\nfunction validOverlayVoice(value) {\n const voice = record(value);\n if (!voice || Object.keys(voice).length !== 6 || !["mode", "state", "mic", "muted", "speaking", "peers"].every((key) => key in voice) || !["room", "team", "proximity"].includes(String(voice.mode)) || !["off", "joining", "on", "reconnecting"].includes(String(voice.state)) || !["mic", "muted", "speaking"].every((key) => typeof voice[key] === "boolean") || !Array.isArray(voice.peers) || voice.peers.length > 24) return false;\n const ids = /* @__PURE__ */ new Set();\n return voice.peers.every((value2) => {\n const peer = record(value2);\n if (!peer || Object.keys(peer).length !== 5 || !["id", "mic", "muted", "speaking", "volume"].every((key) => key in peer) || typeof peer.id !== "string" || !peer.id.length || peer.id.length > 128 || ids.has(peer.id) || !["mic", "muted", "speaking"].every((key) => typeof peer[key] === "boolean") || typeof peer.volume !== "number" || !Number.isFinite(peer.volume) || peer.volume < 0 || peer.volume > 1) return false;\n ids.add(peer.id);\n return true;\n });\n}\n\n// ../contracts/src/match-result.ts\nfunction readMatchResult(value, playerIds) {\n const object = (v) => v !== null && typeof v === "object" && !Array.isArray(v) ? v : null;\n const result = object(value);\n if (!result || !Array.isArray(result.standings)) return null;\n const known = new Set(playerIds), seen = /* @__PURE__ */ new Set();\n const standings = [];\n for (const item of result.standings) {\n const row = object(item);\n if (!row || typeof row.playerId !== "string" || !known.has(row.playerId) || seen.has(row.playerId)) continue;\n seen.add(row.playerId);\n standings.push({\n playerId: row.playerId,\n ...typeof row.score === "number" && Number.isFinite(row.score) ? { score: row.score } : {},\n ...typeof row.rank === "number" && Number.isSafeInteger(row.rank) && row.rank > 0 ? { rank: row.rank } : {}\n });\n }\n if (!standings.length) return null;\n return {\n standings,\n ...Array.isArray(result.winners) ? { winners: [...new Set(result.winners.filter((id) => typeof id === "string" && seen.has(id)))] } : {},\n ...typeof result.draw === "boolean" ? { draw: result.draw } : {},\n ...typeof result.unit === "string" ? { unit: result.unit } : {}\n };\n}\n\n// src/errors.ts\nfunction creaErrore(code, message, version = {}) {\n return Object.assign(new Error(message), { name: "CaisualError", code, ...version });\n}\n\n// src/overlay/host-bridge.ts\nfunction eMessaggioReady(value) {\n return record(value)?.type === "caisual:ready";\n}\nfunction eRichiestaBiglietto(value) {\n const data = record(value);\n return data?.type === "caisual:ticket" && (data.aud === void 0 || data.aud === "portal" || data.aud === "live");\n}\nfunction stanzaDaMessaggio(value) {\n const data = record(value);\n if (data?.type !== "caisual:room") return void 0;\n if (data.room === null) return null;\n const room = record(data.room);\n return typeof room?.code === "string" && /^[ABCDEFGHJKLMNPQRSTUVWXYZ23456789]{6}$/.test(room.code) ? { code: room.code } : void 0;\n}\nfunction creaPonteOspite(input) {\n let port = null, epoch = null, instance = null;\n let disposed = false, legacyReady = true, sequence = 0, requestId = 0;\n let state = null, clockOffset = null;\n let polling = null, pollingEnd = null;\n const pending = /* @__PURE__ */ new Map();\n const states = /* @__PURE__ */ new Set();\n const shortcuts = /* @__PURE__ */ new Set();\n const opens = /* @__PURE__ */ new Set();\n const errors = /* @__PURE__ */ new Set();\n const scores = /* @__PURE__ */ new Set();\n const notify = (listeners, value) => {\n for (const listener of listeners) try {\n listener(value);\n } catch {\n }\n };\n const rejectPending = () => {\n for (const value of pending.values()) {\n input.finestra.clearTimeout(value.timer);\n value.reject(creaErrore("session_replaced", "The game document changed."));\n }\n pending.clear();\n };\n const stopPolling = () => {\n if (polling !== null) input.finestra.clearInterval(polling);\n if (pollingEnd !== null) input.finestra.clearTimeout(pollingEnd);\n polling = pollingEnd = null;\n };\n const askReady = () => {\n if (!disposed && input.frame.src !== "") input.frame.contentWindow?.postMessage({ type: "caisual:ready?" }, input.origineGioco);\n };\n const poll = () => {\n stopPolling();\n polling = input.finestra.setInterval(askReady, 500);\n pollingEnd = input.finestra.setTimeout(stopPolling, 1e4);\n askReady();\n };\n const loaded = () => {\n legacyReady = true;\n poll();\n };\n const listen = (event) => {\n if (disposed || event.origin !== input.origineGioco || event.source !== input.frame.contentWindow || !eMessaggioReady(event.data)) return;\n const data = record(event.data);\n const nextInstance = typeof data.instance === "string" && data.instance.length <= 128 ? data.instance : null;\n if (port && (nextInstance !== null ? nextInstance === instance : !legacyReady)) return;\n stopPolling();\n legacyReady = false;\n instance = nextInstance;\n rejectPending();\n port?.close();\n input.onRoom(null);\n epoch = input.epoch?.() ?? crypto.randomUUID();\n sequence = requestId = 0;\n state = null;\n clockOffset = null;\n notify(states, null);\n const channel = input.creaCanale?.() ?? new MessageChannel();\n const currentPort = channel.port1, currentEpoch = epoch;\n port = currentPort;\n const current = () => !disposed && port === currentPort && epoch === currentEpoch;\n currentPort.onmessage = (event2) => {\n if (!current()) return;\n const data2 = record(event2.data);\n if (eRichiestaBiglietto(data2)) {\n const aud = data2?.aud === "live" ? "live" : "portal";\n void input.rinnova(aud).then((ticket) => {\n if (current()) currentPort.postMessage({ type: "caisual:ticket", aud, ticket });\n }).catch(() => {\n if (current()) currentPort.postMessage({ type: "caisual:ticket", aud, error: "offline" });\n });\n return;\n }\n if (data2?.type === "caisual:reload") {\n const target = record(data2.target);\n const code = typeof target?.code === "string" && /^[ABCDEFGHJKLMNPQRSTUVWXYZ23456789]{6}$/.test(target.code) ? target.code : void 0;\n const roomId = typeof target?.roomId === "string" && /^g[1-9][0-9]*-[1-9][0-9]*\\.[a-z0-9]{16}$/.test(target.roomId) ? target.roomId : void 0;\n input.reload?.(code || roomId ? { code, roomId, watch: target?.watch === true } : void 0);\n return;\n }\n const room = stanzaDaMessaggio(data2);\n if (room !== void 0) {\n input.onRoom(room);\n return;\n }\n if (data2?.v !== 1 || data2.epoch !== currentEpoch) return;\n if (data2.type === "caisual:overlay-response" && typeof data2.requestId === "string") {\n const item = pending.get(data2.requestId);\n if (!item) return;\n if (data2.ok !== true && (data2.ok !== false || typeof record(data2.error)?.code !== "string" || typeof record(data2.error)?.message !== "string")) return;\n pending.delete(data2.requestId);\n input.finestra.clearTimeout(item.timer);\n const response = data2;\n if (response.ok) item.resolve();\n else item.reject(creaErrore(response.error.code, response.error.message));\n } else if (data2.type === "caisual:overlay-state" && Number.isSafeInteger(data2.seq) && data2.seq > sequence) {\n if (!validOverlaySessionState(data2.state) || typeof data2.serverTime !== "number" || !Number.isFinite(data2.serverTime)) return;\n clockOffset = data2.serverTime - Date.now();\n sequence = data2.seq;\n state = structuredClone(data2.state);\n notify(states, state);\n } else if (data2.type === "caisual:overlay-error" && data2.sessionId === state?.id) {\n const error = record(data2.error);\n if (typeof error?.code === "string" && typeof error.message === "string") notify(errors, { sessionId: data2.sessionId, error: { code: error.code, message: error.message } });\n } else if (data2.type === "caisual:overlay-shortcut") {\n notify(shortcuts, void 0);\n } else if (data2.type === "caisual:overlay-open" && OVERLAY_PANELS.includes(data2.panel)) {\n notify(opens, data2.panel);\n } else if (data2.type === "caisual:overlay-score") {\n const score = record(data2.score);\n if (score && typeof score.board === "string" && typeof score.player === "string" && Number.isSafeInteger(score.score) && Number.isFinite(score.submittedAt) && (score.day === null || typeof score.day === "string")) {\n notify(scores, { board: score.board, player: score.player, score: score.score, day: score.day, submittedAt: score.submittedAt });\n }\n }\n };\n currentPort.start();\n input.frame.contentWindow?.postMessage({\n type: "caisual:hello",\n n: input.n,\n ticket: input.ticket,\n live: input.origineLive,\n invite: input.invite,\n // language resta disponibile ai kit pubblicati prima della separazione delle lingue.\n ...input.language ? { language: input.language, uiLanguage: input.language } : {},\n ...input.languagePreferences ? { languagePreferences: input.languagePreferences } : {},\n ...input.configuration ? { gameLanguages: manifestLanguages(input.configuration.manifest) } : {},\n ...input.configuration && data.overlayVersion === 1 ? { overlay: { v: 1, epoch, configuration: input.configuration } } : {}\n }, input.origineGioco, [channel.port2]);\n };\n input.finestra.addEventListener("message", listen);\n input.frame.addEventListener?.("load", loaded);\n poll();\n return {\n get watch() {\n return input.watch === true;\n },\n reload() {\n input.reload?.();\n },\n get epoch() {\n return epoch;\n },\n serverTime() {\n return clockOffset === null ? null : Date.now() + clockOffset;\n },\n get state() {\n return state === null ? null : structuredClone(state);\n },\n subscribe(listener) {\n states.add(listener);\n listener(state);\n return () => {\n states.delete(listener);\n };\n },\n onShortcut(listener) {\n shortcuts.add(listener);\n return () => {\n shortcuts.delete(listener);\n };\n },\n onOpen(listener) {\n opens.add(listener);\n return () => {\n opens.delete(listener);\n };\n },\n onError(listener) {\n errors.add(listener);\n return () => {\n errors.delete(listener);\n };\n },\n onScore(listener) {\n scores.add(listener);\n return () => {\n scores.delete(listener);\n };\n },\n request(op, args) {\n if (!port || !epoch || disposed) return Promise.reject(creaErrore("offline", "The game bridge is not connected."));\n if (pending.size >= 32) return Promise.reject(creaErrore("rate_limited", "Too many overlay requests."));\n const id = String(++requestId), request = {\n type: "caisual:overlay",\n v: 1,\n epoch,\n requestId: id,\n op,\n args,\n ...["room.ready", "room.role", "room.requestRole", "room.team", "room.start", "room.restart", "session.leave", "session.disconnect", "voice.join", "voice.mute", "voice.leave", "voice.setVolume"].includes(op) ? { sessionId: state?.id ?? null } : {}\n };\n if (!validOverlayRequest(request)) return Promise.reject(creaErrore("invalid_request", "The overlay request is invalid."));\n return new Promise((resolve, reject) => {\n const timeout = op === "room.match" ? 31e4 : input.requestTimeoutMs ?? 15e3;\n const timer = input.finestra.setTimeout(() => {\n pending.delete(id);\n reject(creaErrore("timeout", "The overlay request timed out."));\n }, timeout);\n pending.set(id, { resolve, reject, timer });\n try {\n port.postMessage(request);\n } catch (error) {\n input.finestra.clearTimeout(timer);\n pending.delete(id);\n reject(error);\n }\n });\n },\n dispose() {\n disposed = true;\n stopPolling();\n rejectPending();\n port?.close();\n port = null;\n input.finestra.removeEventListener("message", listen);\n input.frame.removeEventListener?.("load", loaded);\n states.clear();\n opens.clear();\n shortcuts.clear();\n scores.clear();\n errors.clear();\n }\n };\n}\nfunction avviaHandshake(input) {\n const bridge = creaPonteOspite(input);\n return () => bridge.dispose();\n}\nfunction gameViewport(frame) {\n const rect = frame.getBoundingClientRect();\n const zoomX = frame.offsetWidth ? rect.width / frame.offsetWidth : 1;\n const zoomY = frame.offsetHeight ? rect.height / frame.offsetHeight : 1;\n const left = rect.left + frame.clientLeft * zoomX, top = rect.top + frame.clientTop * zoomY;\n return {\n left,\n top,\n right: left + frame.clientWidth * zoomX,\n bottom: top + frame.clientHeight * zoomY,\n scaleX: zoomX ? 1 / zoomX : 1,\n scaleY: zoomY ? 1 / zoomY : 1\n };\n}\nfunction measureSafeArea(frame, probe) {\n const win = frame.ownerDocument.defaultView, css = win.getComputedStyle(probe), viewport = gameViewport(frame);\n const clamp = (value, max) => Math.max(0, Math.min(max, value));\n return {\n top: clamp(((parseFloat(css.paddingTop) || 0) - viewport.top) * viewport.scaleY, frame.clientHeight),\n right: clamp((viewport.right - (win.innerWidth - (parseFloat(css.paddingRight) || 0))) * viewport.scaleX, frame.clientWidth),\n bottom: clamp((viewport.bottom - (win.innerHeight - (parseFloat(css.paddingBottom) || 0))) * viewport.scaleY, frame.clientHeight),\n left: clamp(((parseFloat(css.paddingLeft) || 0) - viewport.left) * viewport.scaleX, frame.clientWidth)\n };\n}\n\n// src/overlay/boards.ts\nfunction createBoardController(input) {\n let disposed = false, generation = 0, timer;\n const seen = /* @__PURE__ */ new Set();\n let query = null, data = null, error = false, loading = false;\n let queued = null, saving = null, reads = 0;\n const later = input.later ?? setTimeout, clear = input.clear ?? clearTimeout;\n const cancel = () => {\n if (timer !== void 0) clear(timer);\n timer = void 0;\n };\n const notify = () => {\n if (!disposed) input.changed();\n };\n const matches = () => queued && query?.board === queued.board && query.period === (queued.day ? "daily" : "all-time") && (query.day ?? queued.day) === queued.day;\n const refresh = async () => {\n if (!query || disposed) return;\n cancel();\n const current = ++generation, selected = { ...query };\n loading = true;\n error = false;\n notify();\n try {\n const result = await input.read(selected);\n if (disposed || current !== generation) return;\n data = result;\n if (matches()) {\n const own = result.me;\n if (own?.verified && own.score >= queued.score) saving = own.score === queued.score ? "saved" : "bestAlready";\n }\n } catch {\n if (!disposed && current === generation) error = true;\n }\n if (disposed || current !== generation) return;\n loading = false;\n if (matches() && saving !== "saved" && saving !== "bestAlready") {\n reads++;\n if (reads < 4) {\n saving = "saving";\n timer = later(() => {\n void refresh();\n }, [800, 1600, 3200][reads - 1]);\n } else saving = "refreshHint";\n }\n notify();\n };\n return {\n get state() {\n return { query, data, loading, error, saving: matches() ? saving : null };\n },\n select(next) {\n if (JSON.stringify(next) === JSON.stringify(query)) return;\n cancel();\n generation++;\n query = { ...next };\n data = null;\n reads = 0;\n if (matches()) saving = "saving";\n void refresh();\n },\n queued(score) {\n const board = input.manifest.boards[score.board];\n if (score.player !== input.player || !board || !Number.isSafeInteger(score.score) || score.score < 0 || score.day !== null && !validBoardDay(score.day) || !(board.periods ?? ["all-time"]).includes(score.day ? "daily" : "all-time")) return;\n const signature = JSON.stringify(score);\n if (seen.has(signature)) return;\n seen.add(signature);\n if (seen.size > 64) seen.delete(seen.values().next().value);\n queued = score;\n saving = "saving";\n reads = 0;\n this.select({ board: score.board, period: score.day ? "daily" : "all-time", guests: query?.guests ?? input.guests ?? false, ...score.day ? { day: score.day } : {} });\n if (!loading) void refresh();\n notify();\n },\n refresh,\n reset() {\n seen.clear();\n cancel();\n generation++;\n query = null;\n data = null;\n queued = null;\n saving = null;\n loading = false;\n error = false;\n },\n dispose() {\n disposed = true;\n cancel();\n generation++;\n }\n };\n}\n\n// src/overlay/i18n.ts\nvar languages = ["en", "it", "es", "fr", "de", "pt"];\nvar words = {\n gameUpdated: ["This game was updated", "Questo gioco \\xE8 stato aggiornato", "Este juego se ha actualizado", "Ce jeu a \\xE9t\\xE9 mis \\xE0 jour", "Dieses Spiel wurde aktualisiert", "Este jogo foi atualizado"],\n reloadGame: ["Reload game", "Ricarica il gioco", "Recargar el juego", "Recharger le jeu", "Spiel neu laden", "Recarregar o jogo"],\n gameLanguages: ["Game languages", "Lingue del gioco", "Idiomas del juego", "Langues du jeu", "Spielsprachen", "Idiomas do jogo"],\n loading: ["Loading game...", "Caricamento...", "Cargando...", "Chargement...", "Spiel wird geladen...", "Carregando..."],\n loadingSlow: ["This game is taking longer than expected. You can wait a little longer or try again.", "Il gioco ci sta mettendo pi\\xF9 del previsto. Puoi aspettare ancora un po\\u2019 o riprovare.", "El juego est\\xE1 tardando m\\xE1s de lo esperado. Puedes esperar un poco m\\xE1s o volver a intentarlo.", "Le jeu met plus de temps que pr\\xE9vu. Vous pouvez patienter encore un peu ou r\\xE9essayer.", "Das Spiel braucht l\\xE4nger als erwartet. Du kannst noch etwas warten oder es erneut versuchen.", "O jogo est\\xE1 demorando mais do que o esperado. Voc\\xEA pode esperar mais um pouco ou tentar novamente."],\n home: ["Play", "Gioca", "Jugar", "Jouer", "Spielen", "Jogar"],\n homeMenu: ["Menu", "Menu", "Men\\xFA", "Menu", "Men\\xFC", "Menu"],\n mode: ["Mode", "Modalit\\xE0", "Modo", "Mode", "Modus", "Modo"],\n play: ["Play", "Gioca", "Jugar", "Jouer", "Spielen", "Jogar"],\n friendsPlay: ["Play with friends", "Gioca con amici", "Jugar con amigos", "Jouer entre amis", "Mit Freunden spielen", "Jogar com amigos"],\n find: ["Find players", "Trova giocatori", "Buscar jugadores", "Trouver des joueurs", "Spieler finden", "Buscar jogadores"],\n join: ["Join with code", "Entra con codice", "Entrar con c\\xF3digo", "Rejoindre avec un code", "Mit Code beitreten", "Entrar com c\\xF3digo"],\n joinInvite: ["Join this room", "Entra in questa stanza", "Entrar en est\\xE1 sala", "Rejoindre cette salle", "Diesem Raum beitreten", "Entrar nest\\xE1 sala"],\n watch: ["Watch a room", "Guarda una stanza", "Observar una sala", "Regarder une salle", "Raum ansehen", "Assistir a uma sala"],\n resume: ["Resume", "Riprendi", "Continuar", "Reprendre", "Fortsetzen", "Continuar"],\n room: ["Room", "Stanza", "Sala", "Salle", "Raum", "Sala"],\n code: ["Room code", "Codice stanza", "C\\xF3digo de sala", "Code de salle", "Raumcode", "C\\xF3digo da sala"],\n copy: ["Copy invite", "Copia invito", "Copiar invitaci\\xF3n", "Copier le lien", "Einladung kopieren", "Copiar convite"],\n copied: ["Invite copied", "Invito copiato", "Invitacion copiada", "Lien copi\\xE9", "Einladung kopiert", "Convite copiado"],\n copyFailed: ["Copy this link:", "Copia questo link:", "Copia este enlace:", "Copiez ce lien :", "Diesen Link kopieren:", "Copie este link:"],\n joining: ["Joining room...", "Ingresso nella stanza...", "Entrando en la sala...", "Connexion \\xE0 la salle...", "Raum wird betreten...", "Entrando na sala..."],\n matching: ["Finding your people...", "Ricerca giocatori...", "Buscando jugadores...", "Recherche de joueurs...", "Spieler werden gesucht...", "Buscando jogadores..."],\n queue: ["{n} / {max} players", "{n} / {max} giocatori", "{n} / {max} jugadores", "{n} / {max} joueurs", "{n} / {max} Spieler", "{n} / {max} jogadores"],\n cancel: ["Cancel", "Annulla", "Cancelar", "Annuler", "Abbrechen", "Cancelar"],\n close: ["Close", "Chiudi", "Cerrar", "Fermer", "Schlie\\xDFen", "Fechar"],\n back: ["Back", "Indietro", "Volver", "Retour", "Zur\\xFCck", "Voltar"],\n ready: ["Ready", "Pronto", "Listo", "Pr\\xEAt", "Bereit", "Pronto"],\n unready: ["Not ready", "Non pronto", "No listo", "Pas pr\\xEAt", "Nicht bereit", "N\\xE3o pronto"],\n start: ["Start", "Inizia", "Empezar", "Commencer", "Starten", "Come\\xE7ar"],\n role: ["Role", "Ruolo", "Rol", "R\\xF4le", "Rolle", "Fun\\xE7\\xE3o"],\n team: ["Team", "Squadra", "Equipo", "\\xC9quipe", "Team", "Equipe"],\n host: ["Host", "Host", "Anfitrion", "H\\xF4te", "Host", "Anfitri\\xE3o"],\n you: ["You", "Tu", "T\\xFA", "Vous", "Du", "Voc\\xEA"],\n away: ["Away", "Assente", "Ausente", "Absent", "Abwesend", "Ausente"],\n needPlayers: ["Waiting for more players", "In attesa di giocatori", "Esperando m\\xE1s jugadores", "En attente de joueurs", "Weitere Spieler fehlen", "Esperando mais jogadores"],\n needReady: ["Everyone needs to be ready", "Tutti devono essere pronti", "Todos deben estar listos", "Tout le monde doit \\xEAtre pr\\xEAt", "Alle m\\xFCssen bereit sein", "Todos precisam estar prontos"],\n needRoles: ["Fill the required roles", "Completa i ruoli richiesti", "Completa los roles", "Compl\\xE9tez les r\\xF4les", "Ben\\xF6tigte Rollen besetzen", "Complete as fun\\xE7\\xF5es"],\n needTeams: ["Choose the required teams", "Scegli le squadre richieste", "Elige los equipos", "Choisissez les \\xE9quipes", "Teams auswahlen", "Escolha as equipes"],\n waitHost: ["Waiting for the host", "In attesa dell\'host", "Esperando al anfitrion", "En attente de l\\u2019h\\xF4te", "Warten auf den Host", "Esperando o anfitri\\xE3o"],\n starting: ["Starting in", "Si inizia tra", "Empieza en", "D\\xE9but dans", "Start in", "Come\\xE7a em"],\n playing: ["Playing", "In partita", "Jugando", "En jeu", "Im Spiel", "Jogando"],\n ended: ["Game finished", "Partita conclusa", "Partida terminada", "Partie termin\\xE9e", "Spiel beendet", "Partida encerrada"],\n rematchReady: ["{n}/{max} ready", "{n}/{max} pronti", "{n}/{max} listos", "{n}/{max} pr\\xEAts", "{n}/{max} bereit", "{n}/{max} prontos"],\n rematchStart: ["Start rematch", "Avvia rivincita", "Iniciar revancha", "Lancer la revanche", "Revanche starten", "Iniciar revanche"],\n won: ["You won", "Hai vinto", "Has ganado", "Vous avez gagn\\xE9", "Du hast gewonnen", "Voc\\xEA venceu"],\n lost: ["You lost", "Hai perso", "Has perdido", "Vous avez perdu", "Du hast verloren", "Voc\\xEA perdeu"],\n draw: ["Draw", "Pareggio", "Empate", "\\xC9galit\\xE9", "Unentschieden", "Empate"],\n standings: ["Standings", "Piazzamenti", "Posiciones", "R\\xE9sultats", "Platzierungen", "Coloca\\xE7\\xF5es"],\n points: ["points", "punti", "puntos", "points", "Punkte", "pontos"],\n time: ["time", "tempo", "tiempo", "temps", "Zeit", "tempo"],\n distance: ["distance", "distanza", "distancia", "distance", "Distanz", "dist\\xE2ncia"],\n again: ["Play again", "Gioca ancora", "Jugar de nuevo", "Rejouer", "Erneut spielen", "Jogar novamente"],\n newRoom: ["New room. Share the new invite.", "Nuova stanza. Condividi il nuovo invito.", "Nueva sala. Comparte la invitaci\\xF3n.", "Nouvelle salle. Partagez le lien.", "Neuer Raum. Neue Einladung teilen.", "Nova sala. Compartilhe o convite."],\n watching: ["Watching", "In osservazione", "Observando", "Spectateur", "Zuschauen", "Assistindo"],\n delay: ["{n}s delay", "Ritardo {n}s", "Retraso de {n}s", "Retard de {n}s", "{n}s Verz\\xF6gerung", "Atraso de {n}s"],\n exit: ["Exit", "Esci", "Salir", "Quitter", "Verlassen", "Sair"],\n leaveNow: ["Leave for now", "Esci per ora", "Salir por ahora", "Quitter pour le moment", "Vor\\xFCbergehend verlassen", "Sair por enquanto"],\n leaveRoom: ["Leave room", "Lascia la stanza", "Abandonar sala", "Abandonner la salle", "Raum verlassen", "Deixar a sala"],\n leaveHint: ["Your room stays available for Resume.", "La stanza resta disponibile con Riprendi.", "Podr\\xE1s volver a est\\xE1 sala.", "Vous pourrez reprendre cette salle.", "Du kannst den Raum fortsetzen.", "Voc\\xEA pode voltar a est\\xE1 sala."],\n temporaryHint: ["The game continues. Rejoining may only be possible briefly.", "La partita continua. Il rientro pu\\xF2 essere disponibile solo per poco.", "La partida continua. Volver puede ser posible solo por poco tiempo.", "La partie continue. Le retour peut \\xEAtre limit\\xE9.", "Das Spiel l\\xE4uft weiter. R\\xFCckkehr nur kurz m\\xF6glich.", "A partida continua. O retorno pode ser limitado."],\n abandonHint: ["Leave room gives up your place.", "Lascia la stanza libera il tuo posto.", "Abandonar libera tu plaza.", "Abandonner lib\\xE8re votre place.", "Raum verlassen gibt deinen Platz frei.", "Deixar a sala libera sua vaga."],\n reconnecting: ["Reconnecting...", "Riconnessione...", "Reconectando...", "Reconnexion...", "Verbindung wird erneuert...", "Reconectando..."],\n replaced: ["Opened in another tab", "Aperta in un\\u2019altra scheda", "Abierta en otra pest\\xE1na", "Ouverte dans un autre onglet", "In anderem Tab ge\\xF6ffnet", "Aberta em outra aba"],\n error: ["Something went wrong. Try again.", "Qualcosa non va. Riprova.", "Algo sali\\xF3 mal. Reintenta.", "Une erreur est survenue. R\\xE9essayez.", "Etwas ist schiefgelaufen. Erneut versuchen.", "Algo deu errado. Tente novamente."],\n noRoom: ["This room is no longer available.", "Questa stanza non \\xE8 pi\\xF9 disponibile.", "Esta sala ya no est\\xE1 disponible.", "Cette salle n\'est plus disponible.", "Dieser Raum ist nicht mehr verf\\xFCgbar.", "Esta sala n\\xE3o est\\xE1 mais disponivel."],\n full: ["This room is full.", "La stanza \\xE8 piena.", "La sala est\\xE1 llena.", "Cette salle est pleine.", "Dieser Raum ist voll.", "Esta sala est\\xE1 cheia."],\n noMatch: ["No match this time. Try again.", "Nessun gruppo trovato. Riprova.", "No hay grupo. Reintenta.", "Aucun groupe trouv\\xE9. R\\xE9essayez.", "Keine Gruppe gefunden. Erneut versuchen.", "Nenhum grupo encontrado. Tente novamente."],\n invalidCode: ["Enter a six-character room code.", "Inserisci un codice di sei caratteri.", "Escribe un c\\xF3digo de seis caracteres.", "Entrez un code de six caracteres.", "Sechsstelligen Raumcode eingeben.", "Digite um c\\xF3digo de seis caracteres."],\n refused: ["The room did not accept that change.", "La stanza ha rifiutato la modifica.", "La sala rechaz\\xF3 el cambio.", "La salle a refus\\xE9 ce changement.", "Der Raum hat die \\xC4nderung abgelehnt.", "A sala recusou a altera\\xE7\\xE3o."],\n unavailable: ["Unavailable right now", "Non disponibile ora", "No disponible ahora", "Indisponible pour le moment", "Derzeit nicht verf\\xFCgbar", "Indisponivel agora"],\n offline: ["Connection unavailable. Try again.", "Connessione non disponibile. Riprova.", "Sin conexi\\xF3n. Reintenta.", "Connexion indisponible. R\\xE9essayez.", "Keine Verbindung. Erneut versuchen.", "Sem conex\\xE3o. Tente novamente."],\n saveFailed: ["Keep the room code. Resume could not be saved.", "Conserva il codice. Riprendi non \\xE8 stato salvato.", "Guarda el c\\xF3digo. No se pudo guardar el regreso.", "Gardez le code. La reprise ne peut pas \\xEAtre enregistr\\xE9e.", "Raumcode aufbewahren. Fortsetzen nicht gespeichert.", "Guarde o c\\xF3digo. O retorno n\\xE3o foi salvo."],\n boards: ["Leaderboard", "Classifica", "Clasificaci\\xF3n", "Classement", "Bestenliste", "Classifica\\xE7\\xE3o"],\n board: ["Board", "Classifica", "Tabla", "Classement", "Bestenliste", "Tabela"],\n daily: ["Daily", "Giornaliera", "Diaria", "Du jour", "T\\xE4glich", "Di\\xE1ria"],\n allTime: ["All time", "Di sempre", "Hist\\xF3rica", "Tous les temps", "Gesamt", "Geral"],\n accounts: ["Accounts", "Account", "Cuentas", "Comptes", "Konten", "Contas"],\n guests: ["Guests", "Ospiti", "Invitados", "Invit\\xE9s", "G\\xE4ste", "Visitantes"],\n category: ["Category", "Categoria", "Categoria", "Cat\\xE9gorie", "Kategorie", "Categoria"],\n period: ["Period", "Periodo", "Per\\xEDodo", "P\\xE9riode", "Zeitraum", "Per\\xEDodo"],\n rank: ["Rank", "Posizione", "Puesto", "Rang", "Platz", "Posicao"],\n score: ["Score", "Punteggio", "Puntos", "Score", "Punkte", "Pontos"],\n verified: ["Verified", "Verificato", "Verificado", "V\\xE9rifi\\xE9", "Verifiziert", "Verificado"],\n own: ["Your best", "Il tuo record", "Tu record", "Votre record", "Dein Rekord", "Seu recorde"],\n empty: ["No scores yet", "Nessun punteggio", "A\\xFAn no hay puntos", "Aucun score", "Noch keine Punkte", "Ainda sem pontos"],\n saving: ["Saving score...", "Salvataggio punteggio...", "Guardando puntos...", "Enregistrement du score...", "Punkte werden gespeichert...", "Salvando pontos..."],\n saved: ["Your best is on the board", "Il tuo record \\xE8 in classifica", "Tu record est\\xE1 en la tabla", "Votre record est au classement", "Dein Rekord ist eingetragen", "Seu recorde est\\xE1 na tabela"],\n bestAlready: ["Your best is already on the board", "Il tuo record era gi\\xE0 in classifica", "Tu record ya estaba en la tabla", "Votre record est d\\xE9j\\xE0 au classement", "Dein Rekord ist bereits eingetragen", "Seu recorde j\\xE1 est\\xE1 na tabela"],\n refresh: ["Refresh", "Aggiorna", "Actualizar", "Actualiser", "Aktualisieren", "Atualizar"],\n refreshHint: ["Score not visible yet. Refresh to check.", "Punteggio non ancora visibile. Aggiorna per controllare.", "Puntos a\\xFAn no visibles. Actualiza.", "Score pas encore visible. Actualisez.", "Punkte noch nicht sichtbar. Aktualisieren.", "Pontos ainda n\\xE3o visiveis. Atualize."],\n friends: ["Friends & party", "Amici e gruppo", "Amigos y grupo", "Amis et groupe", "Freunde & Gruppe", "Amigos e grupo"],\n localCrew: ["Friends and party are unavailable in local preview.", "Amici e gruppo non disponibili in anteprima locale.", "Amigos y grupo no disponibles en la vista local.", "Amis et groupe indisponibles en aper\\xE7u local.", "Freunde und Gruppe in lokaler Vorschau nicht verf\\xFCgbar.", "Amigos e grupo indispon\\xEDveis na pr\\xE9via local."],\n loginCrew: ["Sign in on Caisual to use friends and party.", "Accedi a Caisual per amici e gruppo.", "Inicia sesion para amigos y grupo.", "Connectez-vous pour utiliser amis et groupe.", "F\\xFCr Freunde und Gruppe bei Caisual anmelden.", "Entre no Caisual para amigos e grupo."],\n online: ["Online", "Online", "En linea", "En ligne", "Online", "Online"],\n noFriends: ["No friends online", "Nessun amico online", "Sin amigos en linea", "Aucun ami en ligne", "Keine Freunde online", "Nenhum amigo online"],\n createParty: ["Create party", "Crea gruppo", "Crear grupo", "Cr\\xE9er un groupe", "Gruppe erstellen", "Criar grupo"],\n inviteParty: ["Invite to party", "Invita nel gruppo", "Invitar al grupo", "Inviter au groupe", "In Gruppe einladen", "Convidar para o grupo"],\n leaveParty: ["Leave party", "Lascia gruppo", "Salir del grupo", "Quitter le groupe", "Gruppe verlassen", "Sair do grupo"],\n accept: ["Accept", "Accetta", "Aceptar", "Accepter", "Annehmen", "Aceitar"],\n decline: ["Decline", "Rifiuta", "Rechazar", "Refuser", "Ablehnen", "Recusar"],\n follow: ["Join them", "Raggiungi", "Unirse", "Rejoindre", "Beitreten", "Juntar-se"],\n voice: ["Voice", "Voce", "Voz", "Voix", "Sprache", "Voz"],\n voiceJoin: ["Join voice", "Entra in voce", "Unirse a voz", "Activer la voix", "Sprachchat beitreten", "Entrar na voz"],\n voiceLeave: ["Leave voice", "Esci dalla voce", "Salir de voz", "Quitter la voix", "Sprachchat verlassen", "Sair da voz"],\n voiceMute: ["Mute", "Disattiva microfono", "Silenciar", "Couper le micro", "Stummschalten", "Silenciar"],\n voiceUnmute: ["Unmute", "Attiva microfono", "Activar micr\\xF3fono", "Activer le micro", "Mikrofon aktivieren", "Ativar microfone"],\n voiceOff: ["Voice off", "Voce disattivata", "Voz desactivada", "Voix d\\xE9sactiv\\xE9e", "Sprachchat aus", "Voz desativada"],\n voiceJoining: ["Joining voice...", "Connessione voce...", "Conectando voz...", "Connexion vocale...", "Sprachchat verbindet...", "Conectando voz..."],\n voiceOn: ["Voice connected", "Voce connessa", "Voz conectada", "Voix connect\\xE9e", "Sprachchat verbunden", "Voz conectada"],\n voiceMuted: ["Muted", "Microfono disattivato", "Silenciado", "Micro coup\\xE9", "Stumm", "Silenciado"],\n voiceMic: ["Mic on", "Microfono attivo", "Micr\\xF3fono activo", "Micro actif", "Mikrofon an", "Microfone ativo"],\n voiceListening: ["Listening only", "Solo ascolto", "Solo escucha", "\\xC9coute seule", "Nur zuh\\xF6ren", "Somente ouvindo"],\n voiceSpeaking: ["Speaking", "Sta parlando", "Hablando", "Parle", "Spricht", "Falando"],\n voicePeers: ["Voice participants", "Partecipanti in voce", "Participantes de voz", "Participants vocaux", "Sprachteilnehmer", "Participantes de voz"],\n voiceEmpty: ["No one else in voice yet.", "Nessun altro in voce per ora.", "A\\xFAn no hay nadie m\\xE1s en voz.", "Personne d\\u2019autre en voix pour le moment.", "Noch niemand im Sprachchat.", "Ningu\\xE9m mais na voz ainda."],\n voiceVolume: ["Volume for {name}", "Volume di {name}", "Volumen de {name}", "Volume de {name}", "Lautst\\xE4rke f\\xFCr {name}", "Volume de {name}"],\n voiceUnavailable: ["Join a room with voice to use these controls.", "Entra in una stanza con voce per usare questi controlli.", "Entra en una sala con voz para usar estos controles.", "Rejoignez une salle vocale pour utiliser ces commandes.", "Diese Steuerung braucht einen Raum mit Sprachchat.", "Entre em uma sala com voz para usar estes controles."],\n voiceWatch: ["Voice is unavailable while watching.", "La voce non e\' disponibile in osservazione.", "La voz no est\\xE1 disponible al observar.", "La voix est indisponible en observation.", "Beim Zuschauen ist kein Sprachchat verf\\xFCgbar.", "A voz n\\xE3o est\\xE1 dispon\\xEDvel ao assistir."],\n voiceDenied: ["Microphone permission denied. Allow it in your browser, then try again.", "Permesso microfono negato. Consenti l\'accesso nel browser e riprova.", "Permiso de micr\\xF3fono denegado. Act\\xEDvalo en el navegador e int\\xE9ntalo de nuevo.", "Acc\\xE8s au micro refus\\xE9. Autorisez-le dans le navigateur, puis r\\xE9essayez.", "Mikrofonzugriff verweigert. Im Browser erlauben und erneut versuchen.", "Permiss\\xE3o do microfone negada. Permita no navegador e tente novamente."],\n voiceUnsupported: ["Voice is not supported in this browser.", "Questo browser non supporta la voce.", "Este navegador no admite voz.", "Ce navigateur ne prend pas en charge la voix.", "Dieser Browser unterst\\xFCtzt keinen Sprachchat.", "Este navegador n\\xE3o oferece suporte a voz."],\n voiceFailed: ["Voice could not connect. Try again.", "Connessione voce non riuscita. Riprova.", "No se pudo conectar la voz. Int\\xE9ntalo de nuevo.", "Connexion vocale impossible. R\\xE9essayez.", "Sprachverbindung fehlgeschlagen. Erneut versuchen.", "N\\xE3o foi poss\\xEDvel conectar a voz. Tente novamente."],\n voicePeerGone: ["This participant has left voice.", "Questo partecipante e\' uscito dalla voce.", "Este participante sali\\xF3 de voz.", "Ce participant a quitt\\xE9 la voix.", "Diese Person hat den Sprachchat verlassen.", "Este participante saiu da voz."],\n shortcut: ["Shift+Tab shortcut", "Scorciatoia Shift+Tab", "Atajo Shift+Tab", "Raccourci Maj+Tab", "Umschalt+Tab-Kurzbefehl", "Atalho Shift+Tab"],\n menu: ["Caisual menu", "Menu Caisual", "Menu Caisual", "Menu Caisual", "Caisual-Menu", "Menu Caisual"],\n retry: ["Retry", "Riprova", "Reintentar", "R\\xE9essayer", "Erneut versuchen", "Tentar novamente"]\n};\nvar column = (index) => Object.fromEntries(Object.entries(words).map(([key, row]) => [key, row[index]]));\nvar dictionaries = { en: column(0), it: column(1), es: column(2), fr: column(3), de: column(4), pt: column(5) };\nfunction overlayLanguage(raw) {\n const value = raw?.toLowerCase().split("-")[0];\n return languages.includes(value) ? value : "en";\n}\nfunction overlayLocale(raw) {\n const tag = normalizeLanguage(raw);\n return tag && languages.includes(tag.split("-")[0]) ? tag : "en";\n}\nfunction translator(language) {\n const dictionary = dictionaries[overlayLanguage(language)];\n return (key, values = {}) => dictionary[key].replace(/\\{(\\w+)\\}/g, (_all, name) => String(values[name] ?? ""));\n}\nfunction errorText(code) {\n if (code === "version_outdated") return "gameUpdated";\n if (code === "permission_denied") return "voiceDenied";\n if (code === "unsupported") return "voiceUnsupported";\n if (code === "voice_disabled") return "voiceUnavailable";\n if (code === "voice_error") return "voiceFailed";\n if (code === "voice_peer_missing") return "voicePeerGone";\n if (code === "not_publishing") return "voiceListening";\n if (["room_not_found", "room_ended", "version_closed", "no_resume"].includes(code)) return "noRoom";\n if (["room_full", "role_full"].includes(code)) return "full";\n if (code === "replaced") return "replaced";\n if (code === "no_match") return "noMatch";\n if (code === "invalid_code") return "invalidCode";\n if (["offline", "timeout"].includes(code)) return "offline";\n if (code.startsWith("role_") || ["not_in_lobby", "not_host", "session_replaced"].includes(code)) return "refused";\n if (code === "save_failed") return "saveFailed";\n return "error";\n}\n\n// src/overlay/ui-model.ts\nfunction phase(session) {\n if (!session || session.kind === "boot") return "boot";\n if (session.kind === "attaching" || session.kind === "matching") return session.kind;\n if (session.room && ["closed", "replaced"].includes(session.room.connection)) return "error";\n if (session.kind === "local") return session.localStatus === "ended" ? "ended" : "playing";\n if (session.room?.status === "ended" || session.room?.status === "finished") return "ended";\n if (session.kind === "watch") return "watching";\n if (session.kind === "room" && session.room) return session.room.status;\n return "home";\n}\nfunction initialUi(manifest) {\n return { session: null, panel: "auto", mode: manifest.modes[0]?.id ?? "", busy: false, error: null, notice: null, shortcutEnabled: true };\n}\nfunction reduceUi(model, action) {\n switch (action.type) {\n case "session": {\n const next = action.session, changed = next?.id !== model.session?.id || next === null;\n const pending = next?.kind === "attaching" || next?.kind === "matching";\n const nextPhase = phase(next), transition = phase(model.session) !== nextPhase;\n const automatic = transition && ["lobby", "countdown", "playing", "ended"].includes(nextPhase) && [null, "auto", "room", "invite", "home"].includes(model.panel);\n return {\n ...model,\n session: next,\n mode: next?.mode ?? model.mode,\n panel: changed || pending || automatic ? "auto" : model.panel,\n error: changed ? null : model.error,\n notice: changed ? null : model.notice\n };\n }\n case "panel":\n return { ...model, panel: action.panel, error: null, notice: null };\n case "mode":\n return { ...model, mode: action.mode, error: null };\n case "busy":\n return { ...model, busy: action.busy };\n case "error":\n return { ...model, error: action.code, busy: false };\n case "notice":\n return { ...model, notice: action.notice };\n case "shortcut":\n return { ...model, shortcutEnabled: action.enabled };\n }\n}\nfunction visiblePanel(model) {\n const current = phase(model.session);\n if (current === "boot" || current === "attaching" || current === "matching" || current === "error") return current;\n if (model.session?.room?.limits.max === 1 && ["room", "invite"].includes(model.panel ?? "")) return "home";\n if (model.panel !== "auto") return model.panel;\n if (model.session?.room?.limits.max === 1 && current === "lobby") return null;\n return current === "home" ? "home" : current === "lobby" ? "room" : current === "countdown" ? "countdown" : null;\n}\nfunction primaryAction(manifest, mode) {\n const selected = manifest.modes.find((item) => item.id === mode);\n if (!selected) return null;\n return {\n op: selected.execution === "local" ? "local.start" : "room.create",\n friends: selected.execution === "room" && risolviModalita(manifest, mode).players.max > 1\n };\n}\nfunction startReason(manifest, session) {\n const room = session?.room;\n if (!room || session.kind !== "room" || room.status !== "lobby" || room.connection !== "connected") return "unavailable";\n const connected = room.players.filter((p) => p.connected), active = connected.filter((p) => p.role !== "spectator");\n if (active.length < room.limits.min) return "needPlayers";\n if (connected.some((p) => !p.ready)) return "needReady";\n if (manifest.roles.some((role) => active.filter((p) => p.role === role.id).length < role.min)) return "needRoles";\n if (manifest.teams && (active.some((p) => p.team === null) || new Set(active.map((p) => p.team)).size < manifest.teams.min)) return "needTeams";\n return room.host !== room.you ? "waitHost" : null;\n}\nfunction canPlayAgain(session) {\n if (phase(session) !== "ended") return false;\n if (session?.kind === "local") return true;\n if (session?.room?.status === "finished") return session.kind === "room" && session.room.connection === "connected" && (session.room.limits.max === 1 || session.room.players.some((p) => p.id === session.room.you && p.connected && p.role !== "spectator" && !p.ready));\n return session?.kind === "room" && (session.room?.limits.max === 1 || !session.room?.lobby || session.room.host === session.room.you);\n}\nfunction normalizeInvite(code) {\n const value = code.toUpperCase().replace(/[\\s-]/g, "");\n return /^[ABCDEFGHJKLMNPQRSTUVWXYZ23456789]{6}$/.test(value) ? value : null;\n}\nfunction matchPresentation(session) {\n const room = session?.room;\n if (!room || !["finished", "ended"].includes(room.status)) return null;\n const result = readMatchResult(room.result, room.players.map((p) => p.id));\n if (!result) return null;\n const own = session?.kind === "room" && room.players.some((p) => p.id === room.you && p.role !== "spectator") && result.standings.some((p) => p.playerId === room.you);\n const first = result.standings[0];\n const winners = result.winners ?? result.standings.filter((p, i) => i === 0 || first.rank !== void 0 && p.rank === first.rank).map((p) => p.playerId);\n return { result, outcome: !own ? "ended" : result.draw ? "draw" : winners.includes(room.you) ? "won" : "lost" };\n}\n\n// src/overlay/styles.ts\nvar styles = `\n.game-icon{width:28px;height:28px;aspect-ratio:1;object-fit:contain;border-radius:7px;flex:none;vertical-align:middle}.game-icon-title{width:56px;height:56px;margin-bottom:12px}\n.safe-area-probe{position:fixed;visibility:hidden;pointer-events:none;padding:env(safe-area-inset-top,0px) env(safe-area-inset-right,0px) env(safe-area-inset-bottom,0px) env(safe-area-inset-left,0px)}\n:host{all:initial;position:fixed;inset:0;z-index:10000;pointer-events:none;font:15px/1.45 system-ui,sans-serif;color:#f4f4f1;color-scheme:dark;--accent:#a8efc5}\n[data-layout],[data-surface],.sr{pointer-events:none}*{box-sizing:border-box}button,input,select{font:inherit}button,a,input,select{touch-action:manipulation}button,select,input{border:1px solid #ffffff30;background:#25292b;color:inherit;border-radius:12px;min-height:44px;padding:10px 14px}button{cursor:pointer}button:disabled{opacity:.45;cursor:default}button:hover:not(:disabled){background:#343b3a}button:focus-visible,a:focus-visible,input:focus-visible,select:focus-visible{outline:3px solid var(--accent);outline-offset:3px}a{color:var(--accent)}.primary{background:var(--accent);color:var(--accent-ink,#11221b);border-color:transparent;font-weight:700}.primary:hover:not(:disabled){filter:brightness(1.1);background:var(--accent)}.quiet{background:transparent}label{display:grid;gap:6px;text-align:left}select,input{width:100%;min-width:0}h1,h2,p{margin:0}h1{font-size:clamp(26px,5vw,42px);line-height:1.1;letter-spacing:-.035em}h2{font-size:20px}small,.muted{color:#bdc5c1}.stack{display:grid;gap:16px}.row{display:flex;gap:8px;align-items:center;flex-wrap:wrap}.row>*{flex:0 1 auto}.row .grow,.grow{flex:1}.split{display:grid;grid-template-columns:1fr 1fr;gap:10px}.pill{position:absolute;top:max(10px,env(safe-area-inset-top));right:max(10px,env(safe-area-inset-right));display:flex;height:44px;border:1px solid #ffffff35;border-radius:24px;background:#171e20eF;box-shadow:0 4px 20px #0004;pointer-events:auto;overflow:hidden}.pill button{border:0;border-radius:0;padding:8px 13px;background:transparent}.pill button:focus-visible{outline-offset:-4px}.pill small{margin-left:8px}.backdrop{position:absolute;inset:0;background:#0b151ce8;backdrop-filter:blur(10px);pointer-events:auto;display:grid;place-items:center;padding:max(16px,env(safe-area-inset-top)) max(16px,env(safe-area-inset-right)) max(16px,env(safe-area-inset-bottom)) max(16px,env(safe-area-inset-left));overflow:auto}.backdrop.home{background-color:#142127;background-size:contain;background-repeat:no-repeat;background-position:center}.dialog{position:relative;width:min(100%,540px);max-height:100%;overflow:auto;overscroll-behavior:contain;background:#141b1df5;border:1px solid #ffffff25;border-radius:22px;padding:24px;box-shadow:0 20px 80px #0005}.dialog.wide{width:min(100%,700px)}.top{display:flex;align-items:center;gap:12px;margin-bottom:18px}.top h2{flex:1}.tabs{display:flex;gap:6px;flex-wrap:wrap;border-bottom:1px solid #ffffff25;padding-bottom:12px}.tabs button{min-height:36px;padding:6px 10px}.tabs [aria-current=true]{border-color:var(--accent)}.roster{list-style:none;padding:0;margin:0;display:grid;gap:8px;max-height:32dvh;overflow:auto}.roster li{display:flex;align-items:center;gap:8px;padding:10px;background:#ffffff08;border-radius:10px}.roster .name{flex:1;overflow-wrap:anywhere}.badge{border:1px solid #ffffff30;border-radius:6px;padding:2px 6px;font-size:12px}.code{font-size:24px;letter-spacing:.13em;font-variant-numeric:tabular-nums}.notice,.error{border-radius:10px;padding:10px;background:#a8efc514;overflow-wrap:anywhere}.error{background:#ff8b7720;color:#ffd2c9}.countdown{font-size:88px;line-height:1;text-align:center;font-variant-numeric:tabular-nums}.ended{position:absolute;bottom:max(12px,env(safe-area-inset-bottom));left:50%;transform:translateX(-50%);max-width:calc(100% - 24px);width:max-content;background:#171e20f5;pointer-events:auto;border:1px solid #ffffff30;border-radius:16px;padding:10px;display:flex;align-items:center;justify-content:center;gap:8px;flex-wrap:wrap}.ended [data-rematch-players]{max-width:100%;max-height:3.2em;overflow:auto;overflow-wrap:anywhere}.standings{list-style:none;margin:0;padding:0;flex-basis:100%;max-height:20dvh;overflow:auto;font-size:13px}.standings li{display:flex;justify-content:space-between;gap:16px;overflow-wrap:anywhere}.ended{max-height:calc(100dvh - 80px);overflow:auto}.ended strong{padding:0 8px}.sr{position:absolute;width:1px;height:1px;overflow:hidden;clip-path:inset(50%);white-space:nowrap}.table-wrap{overflow:auto;max-height:38dvh}table{width:100%;border-collapse:collapse;text-align:left}th,td{padding:9px 6px;border-bottom:1px solid #ffffff20}td:nth-child(3){text-align:right}td small{display:block}.self{background:#ffffff0a}.checkbox{display:flex;align-items:center;gap:8px;font-size:13px}.checkbox input{width:18px;min-height:18px}.full{width:100%}\n[hidden]{display:none!important}.voice-peers{list-style:none;margin:0;padding:0;display:grid;gap:10px}.voice-peers li{border:1px solid #ffffff25;border-radius:12px;padding:12px;display:grid;gap:8px}.voice-peers [data-speaking=true]{border-color:var(--accent)}.voice-peers input{width:100%;accent-color:var(--accent);padding:0}.voice-peers label{font-size:13px}.pill .voice-toggle{width:44px;padding:8px}.voice-toggle[data-voice-state=on][data-muted=false]{color:var(--accent)}\n.boot{position:absolute;inset:0;z-index:2;isolation:isolate;display:grid;place-items:center;overflow:auto;overscroll-behavior:contain;padding:max(100px,env(safe-area-inset-top)) max(24px,env(safe-area-inset-right)) max(48px,env(safe-area-inset-bottom)) max(24px,env(safe-area-inset-left));background:#0b151c;opacity:1;transition:opacity .4s ease;pointer-events:auto;outline:none}\n.boot::before,.boot::after{content:"";position:fixed;inset:0;pointer-events:none;z-index:-1}.boot::before{background:radial-gradient(ellipse at 50% 38%,color-mix(in srgb,var(--accent),transparent 80%),transparent 65%)}.boot::after{background:radial-gradient(ellipse at 50% 38%,#0b151c20,#0b151cd9 85%),linear-gradient(#0b151c66,#0b151cbf)}\n.boot-cover{position:fixed;inset:0;z-index:-2;width:100%;height:100%;aspect-ratio:3 / 2;object-fit:contain;filter:blur(20px);opacity:.65;pointer-events:none}\n.boot-brand{position:absolute;top:max(28px,env(safe-area-inset-top));left:max(32px,env(safe-area-inset-left));display:flex;align-items:center;gap:10px;font-size:14px;font-weight:650;letter-spacing:.02em;color:#f4f4f1b3}.boot-brand span{display:grid;place-items:center;width:36px;height:36px;border:1px solid #ffffff25;border-radius:12px;background:#171e20af;box-shadow:0 4px 20px #0004;color:var(--boot-accent);font-size:20px;font-weight:800}\n.boot-content{width:min(100%,900px);text-align:center;display:grid;justify-items:center;gap:24px}.boot h1{max-width:16ch;font-size:clamp(44px,8vw,108px);font-weight:800;line-height:1.04;letter-spacing:-.05em;overflow-wrap:anywhere;text-wrap:balance;color:var(--boot-accent);text-shadow:0 20px 80px #0005}\n.boot-progress{width:112px;height:3px;border-radius:12px;background:#ffffff20;overflow:hidden;margin-top:12px}.boot-progress span{display:block;width:44%;height:100%;border-radius:inherit;background:var(--boot-accent);animation:boot-progress 1.8s ease-in-out infinite}.boot-status{max-width:42ch;min-height:3em;font-size:14px;line-height:1.5;color:#d3dad6;text-wrap:balance}.boot-recovery{min-height:44px}.boot-recovery .row{justify-content:center}.boot-leaving{opacity:0;pointer-events:none}\n@keyframes boot-progress{0%{transform:translateX(-110%)}100%{transform:translateX(340%)}}\n@media(max-width:480px){.dialog{padding:18px;border-radius:18px}.split{grid-template-columns:1fr 1fr;gap:8px}.tabs{gap:4px}.tabs button{font-size:13px;padding:6px 8px}.pill button:focus-visible{outline-offset:-4px}.pill small{display:none}.ended{gap:6px}.standings{list-style:none;margin:0;padding:0;flex-basis:100%;max-height:20dvh;overflow:auto;font-size:13px}.standings li{display:flex;justify-content:space-between;gap:16px;overflow-wrap:anywhere}.ended{max-height:calc(100dvh - 80px);overflow:auto}.ended strong{font-size:13px}.ended button{padding:8px 10px;font-size:13px}.roster{max-height:28dvh}}\n@media(prefers-reduced-motion:reduce){*{scroll-behavior:auto}.boot{transition:none}.boot-progress span{animation:none;transform:translateX(65%)}}\n`;\n\n// src/overlay/voice-panel.ts\nfunction voiceEligible(manifest, session) {\n return manifest.voice !== "none" && session?.kind !== "watch" && session?.room?.players.find((player) => player.id === session.room?.you)?.role !== "spectator";\n}\nfunction voiceStatus(voice, t) {\n const key = voice.state === "joining" ? "voiceJoining" : voice.state === "reconnecting" ? "reconnecting" : voice.state === "off" ? "voiceOff" : "voiceOn";\n return t(key);\n}\nfunction updateVoicePanel(container, input) {\n const { session, t } = input, voice = session?.kind === "room" ? session.voice : null;\n if (!voiceEligible(input.manifest, session) || !voice) {\n container.replaceChildren();\n const note = container.ownerDocument.createElement("p");\n note.textContent = t(session?.kind === "watch" || session?.room?.players.find((p) => p.id === session.room?.you)?.role === "spectator" ? "voiceWatch" : "voiceUnavailable");\n container.append(note);\n return;\n }\n if (!container.querySelector("[data-voice-status]")) container.innerHTML = `<p role="status" aria-live="polite" data-voice-status></p><p data-voice-self></p>\n <div class="row"><button type="button" data-action="voice-join"></button><button type="button" data-action="voice-mute"></button><button type="button" data-action="voice-leave"></button></div>\n <p class="error" role="alert" data-voice-error hidden></p><h3 data-voice-heading></h3><ul class="voice-peers" data-voice-peers></ul><p class="muted" data-voice-empty></p>`;\n const get = (selector) => container.querySelector(selector);\n const status = get("[data-voice-status]");\n status.textContent = voiceStatus(voice, t);\n status.dataset.voiceState = voice.state;\n const mic = (value) => t(!value.mic ? "voiceListening" : value.muted ? "voiceMuted" : value.speaking ? "voiceSpeaking" : "voiceMic");\n get("[data-voice-self]").textContent = voice.state === "off" ? "" : `${t("you")}: ${mic(voice)}`;\n const join = get(\'[data-action="voice-join"]\'), mute = get(\'[data-action="voice-mute"]\'), leave = get(\'[data-action="voice-leave"]\');\n join.textContent = t("voiceJoin");\n join.hidden = voice.state !== "off";\n join.disabled = session?.room?.connection !== "connected" || input.pending === "voice.join";\n mute.textContent = t(voice.muted ? "voiceUnmute" : "voiceMute");\n mute.hidden = voice.state !== "on" || !voice.mic;\n mute.disabled = input.pending === "voice.mute";\n mute.setAttribute("aria-pressed", String(voice.muted));\n leave.textContent = t("voiceLeave");\n leave.hidden = voice.state === "off" && input.pending !== "voice.join";\n leave.disabled = input.pending === "voice.leave";\n const error = get("[data-voice-error]");\n error.hidden = !input.error;\n error.textContent = input.error ? t(errorText(input.error)) : "";\n get("[data-voice-heading]").textContent = t("voicePeers");\n get("[data-voice-empty]").textContent = t("voiceEmpty");\n get("[data-voice-empty]").hidden = voice.peers.length > 0;\n const list = get("[data-voice-peers]"), ids = new Set(voice.peers.map((peer) => peer.id));\n for (const row of list.querySelectorAll("[data-voice-peer]")) if (!ids.has(row.dataset.voicePeer)) row.remove();\n for (const peer of voice.peers) {\n let row = [...list.children].find((node) => node.dataset.voicePeer === peer.id);\n if (!row) {\n row = container.ownerDocument.createElement("li");\n row.dataset.voicePeer = peer.id;\n row.innerHTML = \'<div class="row"><strong data-peer-name></strong><small data-peer-status></small></div><label><span data-volume-label></span><input type="range" min="0" max="1" step="0.05" data-control="voice-volume"></label>\';\n row.querySelector("input").dataset.peer = peer.id;\n list.append(row);\n }\n const name = session?.room?.players.find((player) => player.id === peer.id)?.name ?? peer.id;\n row.dataset.mic = String(peer.mic);\n row.dataset.muted = String(peer.muted);\n row.dataset.speaking = String(peer.speaking);\n row.querySelector("[data-peer-name]").textContent = name;\n row.querySelector("[data-peer-status]").textContent = mic(peer);\n row.querySelector("[data-volume-label]").textContent = t("voiceVolume", { name });\n const range = row.querySelector("input");\n if (range.dataset.editing !== "true") range.value = String(peer.volume);\n range.setAttribute("aria-valuetext", `${Math.round(Number(range.value) * 100)}%`);\n range.disabled = voice.state !== "on";\n }\n}\n\n// src/overlay/ui.ts\nvar escape = (value) => String(value ?? "").replace(/[&<>"\']/g, (c) => ({ "&": "&amp;", "<": "&lt;", ">": "&gt;", \'"\': "&quot;", "\'": "&#39;" })[c]);\nfunction mountOverlay(input) {\n const manifest = input.configuration.manifest;\n if (manifest.overlay?.version !== 1) return null;\n const document = input.container.ownerDocument, win = document.defaultView, t = translator(input.language);\n const host = document.createElement("div");\n host.dataset.caisualOverlay = "";\n host.lang = overlayLanguage(input.language);\n host.style.setProperty("pointer-events", "none", "important");\n const root = host.attachShadow({ mode: "open" });\n if (typeof win.CSSStyleSheet?.prototype.replaceSync === "function" && "adoptedStyleSheets" in root) {\n const sheet = new win.CSSStyleSheet();\n sheet.replaceSync(styles);\n root.adoptedStyleSheets = [sheet];\n } else {\n const sheet = document.createElement("link");\n sheet.rel = "stylesheet";\n sheet.href = "/__caisual/overlay/v1.css";\n root.append(sheet);\n }\n const elements = document.createElement("div");\n elements.dataset.layout = "";\n elements.style.pointerEvents = "none";\n elements.innerHTML = `<div data-surface></div><div class="sr" role="status" aria-live="polite" data-live></div>`;\n const safeProbe = document.createElement("div");\n safeProbe.className = "safe-area-probe";\n safeProbe.setAttribute("aria-hidden", "true");\n root.append(elements, safeProbe);\n const surface = root.querySelector("[data-surface]"), live = root.querySelector("[data-live]");\n surface.style.pointerEvents = "none";\n live.style.pointerEvents = "none";\n const accent = manifest.overlay.accent ?? "#a8efc5";\n host.style.setProperty("--accent", accent);\n const rgb = [1, 3, 5].map((i) => parseInt(accent.slice(i, i + 2), 16) / 255).map((v) => v <= 0.04045 ? v / 12.92 : ((v + 0.055) / 1.055) ** 2.4);\n const luminance = rgb[0] * 0.2126 + rgb[1] * 0.7152 + rgb[2] * 0.0722;\n host.style.setProperty("--accent-ink", luminance > 0.179 ? "#000000" : "#ffffff");\n host.style.setProperty("--boot-accent", luminance > 0.179 ? accent : `color-mix(in srgb, ${accent}, #ffffff 70%)`);\n input.container.append(host);\n let model = initialUi(manifest), disposed = false, operation = 0, lastView = "", geometryFrame = 0;\n let lastPhase = "", wasModal = false, copyFallback = null;\n let boot = null, bootTimer = 0, bootFadeTimer = 0;\n let voiceError = null, voicePending = null, voiceOperation = 0;\n let codeDraft = input.configuration.invite ?? "";\n const oldInert = Boolean(input.frame.inert), oldTabIndex = input.frame.getAttribute("tabindex");\n try {\n model.shortcutEnabled = win.localStorage.getItem("caisual-overlay-shortcut-v1") !== "off";\n } catch {\n }\n const boards = input.boards ? createBoardController({ manifest, player: input.player.id, guests: input.player.guest, read: input.boards, changed: () => render() }) : null;\n const stops = [];\n const selectedMode = () => manifest.modes.find((mode) => mode.id === model.mode);\n const disabled = () => model.busy ? " disabled" : "";\n const button = (action, key, extra = "", off = false) => `<button type="button" data-action="${action}"${extra}${off || model.busy ? " disabled" : ""}>${t(key)}</button>`;\n function resetBootWait() {\n win.clearTimeout(bootTimer);\n if (!boot || phase(model.session) !== "boot") return;\n boot.querySelector("[data-boot-message]").textContent = t("loading");\n boot.querySelector("[data-boot-recovery]").hidden = true;\n bootTimer = win.setTimeout(() => {\n bootTimer = 0;\n if (disposed || !boot) return;\n boot.querySelector("[data-boot-message]").textContent = t("loadingSlow");\n boot.querySelector("[data-boot-recovery]").hidden = false;\n }, 9e3);\n }\n function updateBoot(loading) {\n if (loading) {\n if (boot) {\n win.clearTimeout(bootFadeTimer);\n bootFadeTimer = 0;\n boot.inert = false;\n boot.removeAttribute("aria-hidden");\n boot.style.pointerEvents = "auto";\n boot.classList.remove("boot-leaving");\n return;\n }\n boot = document.createElement("section");\n boot.className = "boot";\n boot.tabIndex = -1;\n boot.setAttribute("aria-labelledby", "boot-title");\n boot.setAttribute("aria-describedby", "boot-status");\n boot.style.pointerEvents = "auto";\n boot.innerHTML = `<div class="boot-brand" aria-hidden="true"><span>C</span>Caisual</div>\n <div class="boot-content"><h1 id="boot-title">${escape(manifest.name)}</h1>\n <div class="boot-progress" aria-hidden="true"><span></span></div>\n <p class="boot-status" id="boot-status" role="status" aria-live="polite" aria-atomic="true"><span class="sr">${escape(manifest.name)}. </span><span data-boot-message></span></p>\n <div class="boot-recovery"><div class="row" data-boot-recovery hidden>${button("reload", "retry", \' class="primary"\')}${button("exit-now", "exit", \' class="quiet"\')}</div></div>\n </div>`;\n if (input.configuration.coverUrl) {\n const cover = document.createElement("img");\n cover.className = "boot-cover";\n cover.alt = "";\n cover.setAttribute("aria-hidden", "true");\n cover.addEventListener("error", () => {\n cover.hidden = true;\n }, { once: true });\n cover.src = input.configuration.coverUrl;\n boot.prepend(cover);\n }\n elements.append(boot);\n resetBootWait();\n } else if (boot && !boot.inert) {\n boot.inert = true;\n boot.setAttribute("aria-hidden", "true");\n boot.style.pointerEvents = "none";\n boot.classList.add("boot-leaving");\n const remove = () => {\n win.clearTimeout(bootTimer);\n bootTimer = 0;\n boot?.remove();\n boot = null;\n bootFadeTimer = 0;\n };\n if (win.matchMedia?.("(prefers-reduced-motion: reduce)").matches) remove();\n else bootFadeTimer = win.setTimeout(remove, 420);\n }\n }\n const dispatch = (action) => {\n if (disposed) return;\n model = reduceUi(model, action);\n render();\n };\n const announce = (text) => {\n if (live.textContent !== text) live.textContent = text;\n };\n const controls = () => [...root.querySelectorAll(\'button:not(:disabled),a[href],input:not(:disabled),select:not(:disabled),[tabindex="0"]\')].filter((el) => !el.closest("[hidden]"));\n const soloMode = (mode) => (mode === null || manifest.modes.some((item) => item.id === mode)) && risolviModalita(manifest, mode).players.max === 1;\n const solo = () => model.session?.room?.limits.max === 1;\n const roomCode = () => solo() ? null : model.session?.room?.code ?? null;\n const setPanel = (panel) => {\n if (panel === "boards" && boards && !boards.state.query) {\n const id = Object.keys(manifest.boards)[0];\n if (id) boards.select({ board: id, period: (manifest.boards[id].periods ?? ["all-time"])[0], guests: input.player.guest });\n }\n copyFallback = null;\n dispatch({ type: "panel", panel });\n };\n const close = () => {\n const current = phase(model.session), panel = visiblePanel(model);\n if (current === "boot") return;\n if (current === "home") setPanel("home");\n else if (current === "lobby" && panel !== "room") setPanel("room");\n else setPanel(null);\n };\n const toggle = () => {\n if (visiblePanel(model)) close();\n else setPanel(model.session?.room ? "room" : "home");\n };\n async function perform(op, args, after) {\n const token = ++operation;\n dispatch({ type: "error", code: null });\n dispatch({ type: "busy", busy: true });\n try {\n await input.bridge.request(op, args);\n if (token === operation && !disposed) await after?.();\n } catch (error) {\n if (token === operation && !disposed && error.code !== "cancelled") dispatch({ type: "error", code: error.code ?? "offline" });\n } finally {\n if (token === operation && !disposed) dispatch({ type: "busy", busy: false });\n }\n }\n function updateVoice() {\n const container = root.querySelector("[data-voice-panel]");\n if (container) updateVoicePanel(container, { manifest, session: model.session, t, error: voiceError, pending: voicePending });\n const toggle2 = root.querySelector("[data-voice-toggle]"), voice = model.session?.voice;\n if (toggle2) {\n toggle2.dataset.voiceState = voice?.state ?? "off";\n toggle2.dataset.muted = String(voice?.muted ?? false);\n toggle2.setAttribute("aria-label", `${t("voice")}: ${voice ? voiceStatus(voice, t) : t("voiceOff")}`);\n toggle2.textContent = voice?.state === "on" && !voice.muted ? "\\u25CF" : "\\u25CB";\n }\n }\n async function performVoice(op, args) {\n const sessionId = model.session?.id, epoch = input.bridge.epoch, volume = op === "voice.setVolume";\n const token = volume ? voiceOperation : ++voiceOperation;\n const current = () => !disposed && model.session?.id === sessionId && input.bridge.epoch === epoch && token === voiceOperation;\n voiceError = null;\n if (!volume) voicePending = op;\n updateVoice();\n try {\n await input.bridge.request(op, args);\n } catch (error) {\n if (current()) voiceError = error.code ?? "voice_error";\n } finally {\n if (current()) {\n if (!volume) voicePending = null;\n updateVoice();\n }\n }\n }\n async function copyInvite() {\n const code = roomCode();\n if (!code) return;\n const url = input.inviteUrl(code);\n try {\n await win.navigator.clipboard.writeText(url);\n dispatch({ type: "notice", notice: t("copied") });\n } catch {\n copyFallback = url;\n render();\n root.querySelector(\'input[data-control="invite-link"]\')?.select();\n }\n }\n function invitation() {\n if (solo()) return "";\n const code = roomCode();\n if (!code) return `<p>${t("noRoom")}</p>`;\n return `<div class="row"><div class="grow"><small>${t("code")}</small><div class="code" data-room-code>${escape(code)}</div></div>${button("copy", "copy")}</div>${copyFallback ? `<label>${t("copyFailed")}<input data-control="invite-link" readonly value="${escape(copyFallback)}"></label>` : ""}`;\n }\n function navigation(panel) {\n const items = [];\n if (model.session?.room && !solo()) items.push(["room", "room"], ["invite", "copy"]);\n items.push(["friends", "friends"]);\n if (Object.keys(manifest.boards).length) items.push(["boards", "boards"]);\n if (voiceEligible(manifest, model.session)) items.push(["voice", "voice"]);\n return `<nav class="tabs" aria-label="Caisual">${items.map(([id, key]) => button(`panel:${id}`, key, ` aria-current="${id === panel}"`)).join("")}</nav>`;\n }\n function home() {\n const selected = selectedMode(), action = primaryAction(manifest, model.mode), session = model.session;\n const hasRooms = manifest.modes.some((mode) => mode.execution === "room" && risolviModalita(manifest, mode.id).players.max > 1);\n return `${input.configuration.iconUrl ? `<img class="game-icon game-icon-title" src="${escape(input.configuration.iconUrl)}" alt="" />` : ""}<h1>${escape(manifest.name)}</h1><p class="muted" data-game-languages>${t("gameLanguages")}: ${escape(manifestLanguages(manifest).join(" \\xB7 "))}</p><label>${t("mode")}<select data-control="mode"${disabled()}>${manifest.modes.map((mode) => `<option value="${escape(mode.id)}"${mode.id === model.mode ? " selected" : ""}>${escape(risolviPresentazione(manifest, mode.id, input.language).label)}</option>`).join("")}</select></label>\n ${selected?.instructions ? `<p class="muted">${escape(resolveText(selected.instructions, input.language, manifestLanguages(manifest)[0]))}</p>` : ""}\n ${input.configuration.invite && phase(session) === "home" ? button("join-invite", input.bridge.watch ? "watch" : "joinInvite", \' class="primary"\', !session?.ready) : ""}\n ${action ? button("play", action.friends ? "friendsPlay" : "play", \' class="primary"\', !session?.ready) : ""}\n ${selected?.matchmaking ? button("match", "find", "", !selected.matchmaking.defaults || !session?.ready) : ""}\n ${session?.resume ? button("resume", "resume", "", !session.ready) + `<small>${escape(solo() || soloMode(session.resume.mode) ? "" : session.resume.code)}</small>` : ""}\n ${hasRooms && !solo() ? `<div class="split">${button("panel:join", "join", "", !session?.ready)}${manifest.spectators ? button("panel:watch", "watch", "", !session?.ready) : ""}</div>` : ""}\n ${solo() ? button("panel:exit", "exit", \' class="quiet"\') : ""}${navigation("home")}<label class="checkbox"><input type="checkbox" data-control="shortcut"${model.shortcutEnabled ? " checked" : ""}>${t("shortcut")}</label>`;\n }\n function room() {\n const session = model.session, room2 = session?.room;\n if (!room2) return `<p>${t("noRoom")}</p>`;\n const own = room2.players.find((player) => player.id === room2.you), lobby = room2.status === "lobby" && session?.kind === "room";\n const canRole = session?.kind === "room" && (lobby || room2.status === "playing" && room2.requestRole);\n const reason = startReason(manifest, session);\n return `${invitation()}<ul class="roster" aria-label="${t("room")}">${room2.players.map((p) => `<li data-player-id="${escape(p.id)}"><span class="name">${escape(p.name)} ${p.id === room2.you ? `<small>(${t("you")})</small>` : ""}</span>${p.id === room2.host ? `<span class="badge">${t("host")}</span>` : ""}${p.role ? `<small>${escape(resolveText(manifest.roles.find((r) => r.id === p.role)?.label, input.language, manifestLanguages(manifest)[0], p.role))}</small>` : ""}${p.team ? `<small>${t("team")} ${p.team}</small>` : ""}<small>${!p.connected ? t("away") : lobby ? t(p.ready ? "ready" : "unready") : ""}</small></li>`).join("")}</ul>\n ${canRole && manifest.roles.length ? `<label>${t("role")}<select data-control="role"${disabled()}><option value="" disabled${!own?.role ? " selected" : ""}>${t("role")}</option>${manifest.roles.map((role) => `<option value="${escape(role.id)}"${role.id === own?.role ? " selected" : ""}>${escape(resolveText(role.label, input.language, manifestLanguages(manifest)[0], role.id))}</option>`).join("")}</select></label>` : ""}\n ${lobby && manifest.teams ? `<label>${t("team")}<select data-control="team"${disabled()}><option value="" disabled${!own?.team ? " selected" : ""}>${t("team")}</option>${Array.from({ length: manifest.teams.max }, (_, i) => `<option value="${i + 1}"${own?.team === i + 1 ? " selected" : ""}>${t("team")} ${i + 1}</option>`).join("")}</select></label>` : ""}\n ${lobby ? `<div class="row">${button("ready", own?.ready ? "unready" : "ready", \' class="primary"\', room2.connection !== "connected")}${room2.host === room2.you ? button("start", "start", "", reason !== null) : ""}</div>${reason ? `<p class="muted" data-start-reason>${t(reason)}</p>` : ""}` : ""}\n ${session?.kind === "watch" ? `<p>${t("watching")} \\xB7 ${t("delay", { n: (room2.delayMs ?? 0) / 1e3 })}</p>` : ""}\n ${navigation("room")}<label class="checkbox"><input type="checkbox" data-control="shortcut"${model.shortcutEnabled ? " checked" : ""}>${t("shortcut")}</label>${button("panel:exit", "exit", \' class="quiet"\')}`;\n }\n function crew() {\n const provider = input.crew, state = provider?.getSnapshot();\n if (!provider || provider.unavailable || !state?.you) return `<p>${t(provider?.unavailable === "local" ? "localCrew" : "loginCrew")}</p>`;\n const online = state.friends.filter((friend) => friend.online), party = state.party;\n const person = (p) => `<li>${p.game ? `<img class="game-icon" src="${escape(p.game.iconUrl)}" alt="" />` : ""}<span class="name">${escape(p.name)}<small>${p.game ? ` \\xB7 ${escape(p.game.name)}` : ""}</small></span>${p.room && p.game ? button("follow", "follow", ` data-code="${escape(p.room.code)}" data-game="${escape(p.game.slug)}"`) : ""}${party?.leader === state.you.id && !party.members.some((member) => member.id === p.id) ? button("party-invite", "inviteParty", ` data-player="${escape(p.id)}"`) : ""}</li>`;\n return `${!state.connected ? `<p>${t("reconnecting")}</p>` : ""}${party ? `<ul class="roster">${party.members.map(person).join("")}</ul>${button("party-leave", "leaveParty")}` : button("party-create", "createParty")}\n ${state.invites.map((invite) => `<div class="row"><span class="grow">${escape(invite.from.name)}</span>${button("party-accept", "accept", ` data-party="${escape(invite.party)}"`)}${button("party-decline", "decline", ` data-party="${escape(invite.party)}"`)}</div>`).join("")}\n ${state.follow ? `<div class="row"><span class="grow"><img class="game-icon" src="${escape(state.follow.game.iconUrl)}" alt="" /> ${escape(state.follow.from.name)} \\xB7 ${escape(state.follow.game.name)}</span>${button("follow", "follow", ` data-code="${escape(state.follow.code)}" data-game="${escape(state.follow.game.slug)}"`)}</div>` : ""}\n <h2>${t("online")}</h2>${online.length ? `<ul class="roster">${online.map(person).join("")}</ul>` : `<p class="muted">${t("noFriends")}</p>`}`;\n }\n function leaderboard() {\n if (!boards || !boards.state.query) return `<p>${t("unavailable")}</p>`;\n const { query, data, loading, error, saving } = boards.state;\n const board = manifest.boards[query.board];\n return `<label>${t("board")}<select data-control="board">${Object.entries(manifest.boards).map(([id, value]) => `<option value="${escape(id)}"${query.board === id ? " selected" : ""}>${escape(resolveText(value.label, input.language, manifestLanguages(manifest)[0], id))}</option>`).join("")}</select></label>\n <div class="split"><label>${t("period")}<select data-control="period">${(board.periods ?? ["all-time"]).map((period) => `<option value="${period}"${query.period === period ? " selected" : ""}>${t(period === "daily" ? "daily" : "allTime")}</option>`).join("")}</select></label><label>${t("category")}<select data-control="category"><option value="accounts"${!query.guests ? " selected" : ""}>${t("accounts")}</option><option value="guests"${query.guests ? " selected" : ""}>${t("guests")}</option></select></label></div>\n ${query.period === "daily" ? `<small data-board-day>${escape(data?.day ?? query.day ?? new Date(input.bridge.serverTime() ?? Date.now()).toISOString().slice(0, 10))}</small>` : ""}\n ${saving ? `<p role="status" data-saving>${t(saving)}</p>` : ""}${error ? `<p role="alert">${t("offline")}</p>` : ""}\n ${data ? `<div class="table-wrap"><table><thead><tr><th>${t("rank")}</th><th>${t(query.guests ? "guests" : "accounts")}</th><th>${t("score")}</th></tr></thead><tbody>${data.entries.map((entry) => `<tr${entry.me ? \' class="self"\' : ""}><td>${entry.rank}</td><td>${escape(entry.name)}${entry.verified ? `<small>${t("verified")}</small>` : ""}</td><td>${entry.score}</td></tr>`).join("")}</tbody></table>${data.entries.length ? "" : `<p>${t("empty")}</p>`}</div><p data-own-score>${t("own")} (${t(data.ownGuest ? "guests" : "accounts")}): ${data.me ? `#${data.me.rank} \\xB7 ${data.me.score}${data.me.verified ? ` \\xB7 ${t("verified")}` : ""}` : t("empty")}</p>` : `<p>${t(loading ? "loading" : "empty")}</p>`}\n ${button("refresh", "refresh", "", loading)}`;\n }\n function content(panel) {\n switch (panel) {\n case "home":\n return home();\n case "room":\n return room();\n case "invite":\n return invitation();\n case "friends":\n return crew();\n case "voice":\n return \'<div class="stack" data-voice-panel></div>\';\n case "boards":\n return leaderboard();\n case "join":\n case "watch":\n return `<form class="stack" data-form="${panel}"><label>${t("code")}<input data-control="code" name="code" autocomplete="off" autocapitalize="characters" spellcheck="false" maxlength="16" value="${escape(codeDraft)}" required></label><button class="primary" type="submit"${disabled()}>${t(panel === "join" ? "join" : "watch")}</button></form>`;\n case "attaching":\n case "matching":\n return `<p role="status">${t(panel === "matching" ? "matching" : "joining")}</p>${model.session?.waiting ? `<p>${t("queue", { n: model.session.waiting.players, max: model.session.waiting.max })}</p>` : ""}<button type="button" data-action="cancel">${t("cancel")}</button>`;\n case "countdown":\n return `<p>${t("starting")}</p><div class="countdown" data-countdown></div>`;\n case "boot":\n return "";\n case "error":\n return `<p role="alert">${t(model.session?.room?.connection === "replaced" ? "replaced" : "noRoom")}</p>${button("leave", "home")}${button("exit-now", "exit")}`;\n case "exit":\n return model.session?.kind === "room" && phase(model.session) !== "ended" ? `<p>${t(model.session.room?.persistent ? "leaveHint" : "temporaryHint")}</p>${invitation()}${button("disconnect-exit", "leaveNow", \' class="primary"\')}<p class="muted">${t("abandonHint")}</p>${button("leave-exit", "leaveRoom")}` : button("leave-exit", "exit", \' class="primary"\');\n }\n }\n function title(panel) {\n const keys = { boot: "loading", home: "home", room: "room", invite: "copy", friends: "friends", voice: "voice", boards: "boards", join: "join", watch: "watch", attaching: "joining", matching: "matching", countdown: "starting", error: "error", exit: "exit" };\n return t(keys[panel]);\n }\n function updateCountdown() {\n const at = model.session?.room?.countdownAt, now = input.bridge.serverTime();\n const value = at === null || at === void 0 || now === null ? "..." : String(Math.max(0, Math.round((at - now) / 1e3)));\n const node = root.querySelector("[data-countdown]");\n if (node && node.textContent !== value) {\n node.textContent = value;\n announce(`${t("starting")} ${value}`);\n }\n }\n function geometry() {\n geometryFrame = 0;\n if (disposed || !input.bridge.epoch || !model.session) return;\n const frame = gameViewport(input.frame), { scaleX, scaleY } = frame;\n const reservedRects = [...root.querySelectorAll("[data-reserve]")].map((el) => {\n const rect = el.getBoundingClientRect(), left = Math.max(frame.left, rect.left), top = Math.max(frame.top, rect.top), right = Math.min(frame.right, rect.right), bottom = Math.min(frame.bottom, rect.bottom);\n return { x: Math.max(0, Math.round((left - frame.left) * scaleX)), y: Math.max(0, Math.round((top - frame.top) * scaleY)), width: Math.max(0, Math.round((right - left) * scaleX)), height: Math.max(0, Math.round((bottom - top) * scaleY)) };\n }).filter((rect) => rect.width && rect.height).slice(0, 8);\n const view = { inputBlocked: phase(model.session) === "boot" || !!visiblePanel(model), reservedRects, safeArea: measureSafeArea(input.frame, safeProbe), shortcutEnabled: model.shortcutEnabled };\n const serialized = `${input.bridge.epoch}:${JSON.stringify(view)}`;\n if (lastView === serialized) return;\n lastView = serialized;\n void input.bridge.request("overlay.view", view).catch(async (error) => {\n if (error?.code === "invalid_request" && lastView === serialized) {\n const { safeArea, ...legacy } = view;\n try {\n await input.bridge.request("overlay.view", legacy);\n return;\n } catch {\n }\n }\n if (lastView === serialized) lastView = "";\n });\n }\n function resize() {\n if (!geometryFrame) geometryFrame = win.requestAnimationFrame(geometry);\n }\n function resultBar() {\n const presentation = matchPresentation(model.session);\n if (!presentation) return `<strong>${t("ended")}</strong>`;\n const room2 = model.session.room, { result } = presentation;\n const unit = result.unit && ["points", "time", "distance"].includes(result.unit) ? t(result.unit) : result.unit;\n const rows = result.standings.map((p, i) => {\n const name = room2.players.find((player) => player.id === p.playerId).name;\n const score = p.score === void 0 ? "" : `<span>${escape(String(p.score))}${unit ? ` ${escape(unit)}` : ""}</span>`;\n return `<li><span>${p.rank ?? i + 1}. ${escape(name)}</span>${score}</li>`;\n }).join("");\n return `<strong data-outcome>${t(presentation.outcome)}</strong><ol class="standings" aria-label="${t("standings")}">${rows}</ol>`;\n }\n function rematchBar() {\n const session = model.session, room2 = session?.room;\n if (!room2 || room2.status !== "finished" || solo()) return "";\n const active = room2.players.filter((p) => p.connected && p.role !== "spectator");\n const ready = active.filter((p) => p.ready), host2 = session.kind === "room" && room2.host === room2.you;\n const canStart = room2.connection === "connected" && active.length >= room2.limits.min && ready.length === active.length;\n return `<small role="status" data-rematch-ready>${t("rematchReady", { n: ready.length, max: active.length })}</small>\n ${ready.length ? `<small data-rematch-players>${ready.map((p) => escape(p.name)).join(", ")}</small>` : ""}\n ${room2.rematch?.autoStart ? "" : host2 ? button("restart", "rematchStart", \' class="primary"\', !canStart) : `<small>${t("waitHost")}</small>`}`;\n }\n function render() {\n if (disposed) return;\n const panel = visiblePanel(model), current = phase(model.session), room2 = model.session?.room;\n const focused = root.activeElement;\n const focusPeer = focused?.dataset.peer;\n const focusKey = focused?.dataset.control ? ["control", focused.dataset.control] : focused?.dataset.action ? ["action", focused.dataset.action] : null;\n const previousScroll = root.querySelector(".dialog")?.scrollTop ?? 0;\n const selection = focused?.tagName === "INPUT" ? { start: focused.selectionStart, end: focused.selectionEnd } : null;\n const crewState = input.crew?.getSnapshot(), invitations = (crewState?.invites.length ?? 0) + (crewState?.follow ? 1 : 0);\n const label = current === "watching" ? t("watching") : room2?.connection === "reconnecting" ? t("reconnecting") : solo() ? "Caisual" : room2?.code ?? "Caisual";\n surface.innerHTML = current === "boot" ? "" : `<div class="pill" data-reserve><button type="button" data-action="menu" aria-label="${t("menu")}" aria-expanded="${!!panel}">C<span aria-hidden="true"><small>${escape(label)}</small></span></button>${voiceEligible(manifest, model.session) && model.session?.kind === "room" ? `<button type="button" class="voice-toggle" data-voice-toggle data-action="panel:voice"></button>` : ""}${invitations ? `<button type="button" data-action="panel:friends" aria-label="${t("friends")} (${invitations})">${invitations}</button>` : ""}</div>\n ${current === "ended" && !panel ? `<div class="ended" data-reserve role="region" aria-label="${t("ended")}">${resultBar()}${boards?.state.saving ? `<small role="status" data-saving>${t(boards.state.saving)}</small>` : ""}${canPlayAgain(model.session) ? button("again", "again", \' class="primary"\') : model.session?.kind === "room" && room2?.status !== "finished" && !solo() ? `<small>${t("waitHost")}</small>` : ""}${rematchBar()}${Object.keys(manifest.boards).length ? button("panel:boards", "boards") : ""}${button("panel:home", "homeMenu")}</div>` : ""}\n ${panel ? `<div class="backdrop${panel === "home" ? " home" : ""}"><section class="dialog${panel === "boards" || panel === "friends" ? " wide" : ""}" role="dialog" aria-modal="true" aria-labelledby="panel-title" tabindex="-1"><div class="top"><h2 id="panel-title">${title(panel)}</h2><button type="button" data-action="close" aria-label="${t("close")}">\\xD7</button></div><div class="stack">${content(panel)}${model.error ? `<p class="error" role="alert" data-error>${t(errorText(model.error))}</p>${model.error === "version_outdated" ? button("reload-game", "reloadGame", \' class="primary"\') : ""}` : ""}${model.session?.resumeError ? `<p class="error" role="alert">${t("saveFailed")}</p>` : ""}${model.notice ? `<p class="notice" role="status">${escape(model.notice)}</p>` : ""}</div></section></div>` : ""}`;\n for (const element of surface.querySelectorAll(".pill,.backdrop,.ended")) element.style.pointerEvents = "auto";\n const backdrop = root.querySelector(".backdrop.home");\n if (backdrop && input.configuration.coverUrl) backdrop.style.backgroundImage = `linear-gradient(#0b151c99,#0b151cee),url(${JSON.stringify(input.configuration.coverUrl)})`;\n host.dataset.phase = current;\n host.dataset.panel = panel ?? "";\n input.frame.inert = !!panel || oldInert;\n if (panel) input.frame.tabIndex = -1;\n else if (oldTabIndex === null) input.frame.removeAttribute("tabindex");\n else input.frame.setAttribute("tabindex", oldTabIndex);\n updateVoice();\n updateBoot(current === "boot");\n const dialog = root.querySelector(".dialog");\n if (dialog) dialog.scrollTop = previousScroll;\n const focusPanel = current === "boot" ? boot : dialog;\n const matched = focusKey ? [...root.querySelectorAll(`[data-${focusKey[0]}]`)].find((el) => el.getAttribute(`data-${focusKey[0]}`) === focusKey[1] && el.dataset.peer === focusPeer) : null;\n if (!panel && wasModal && !input.frame.inert && input.frame.isConnected) {\n input.frame.focus({ preventScroll: true });\n input.frame.contentWindow?.focus();\n } else if (matched && (!panel || focusPanel?.contains(matched)) && !matched.hasAttribute("disabled")) {\n matched.focus({ preventScroll: true });\n if (matched.tagName === "INPUT" && selection?.start !== null && selection?.end !== null && selection) matched.setSelectionRange(selection.start, selection.end);\n } else if (panel && (!wasModal || focused)) (current === "boot" ? boot : dialog?.querySelector(\'select,input,button:not([data-action="close"]):not(:disabled)\') ?? dialog)?.focus({ preventScroll: true });\n wasModal = !!panel;\n if (lastPhase !== current) {\n lastPhase = current;\n announce(current === "boot" ? "" : t({ home: "home", attaching: "joining", matching: "matching", lobby: "room", countdown: "starting", playing: "playing", ended: "ended", watching: "watching", error: "error" }[current]));\n }\n updateCountdown();\n resize();\n }\n const click = (event) => {\n const target = event.target.closest("button[data-action]");\n if (!target || target.disabled) return;\n const action = target.dataset.action;\n event.stopPropagation();\n if (action.startsWith("panel:")) {\n setPanel(action.slice(6));\n return;\n }\n switch (action) {\n case "menu":\n toggle();\n break;\n case "close":\n close();\n break;\n case "play": {\n const selected = primaryAction(manifest, model.mode);\n if (selected) void perform(selected.op, { mode: model.mode });\n break;\n }\n case "match":\n void perform("room.match", { mode: model.mode });\n break;\n case "join-invite":\n if (input.configuration.invite) void perform(input.bridge.watch ? "room.watch" : "room.join", { code: input.configuration.invite });\n break;\n case "resume":\n void perform("session.resume", {});\n break;\n case "cancel":\n void perform("session.cancel", {});\n break;\n case "ready":\n void perform("room.ready", { ready: !model.session?.room?.players.find((p) => p.id === model.session?.room?.you)?.ready });\n break;\n case "start":\n void perform("room.start", {});\n break;\n case "restart":\n void perform("room.restart", {});\n break;\n case "copy":\n void copyInvite();\n break;\n case "voice-join":\n void performVoice("voice.join", {});\n break;\n case "voice-mute":\n void performVoice("voice.mute", { muted: !model.session?.voice?.muted });\n break;\n case "voice-leave":\n void performVoice("voice.leave", {});\n break;\n case "again": {\n if (!canPlayAgain(model.session)) break;\n if (model.session?.kind === "local") void perform("local.start", { mode: model.session.mode ?? model.mode });\n else if (model.session?.room?.status === "finished") void perform("room.restart", {});\n else if (solo()) void perform("room.create", { mode: model.session?.room?.mode ?? null });\n else void perform("room.create", { mode: model.session?.room?.mode ?? null }, async () => {\n setPanel("invite");\n dispatch({ type: "notice", notice: t("newRoom") });\n await copyInvite();\n });\n break;\n }\n case "disconnect-exit":\n void perform("session.disconnect", {}, input.exit);\n break;\n case "leave-exit":\n void perform("session.leave", {}, input.exit);\n break;\n case "leave":\n void perform("session.leave", {}, () => setPanel("home"));\n break;\n case "exit-now":\n input.exit();\n break;\n case "reload-game":\n input.bridge.reload();\n break;\n case "reload":\n boot?.focus({ preventScroll: true });\n resetBootWait();\n input.frame.src = input.frame.src;\n break;\n case "refresh":\n void boards?.refresh();\n break;\n case "party-create":\n input.crew?.party.create();\n break;\n case "party-leave":\n input.crew?.party.leave();\n break;\n case "party-invite":\n input.crew?.party.invite(target.dataset.player);\n break;\n case "party-accept":\n input.crew?.party.accept(target.dataset.party);\n break;\n case "party-decline":\n input.crew?.party.decline(target.dataset.party);\n break;\n case "follow":\n input.crew?.follow(target.dataset.game, target.dataset.code);\n break;\n }\n };\n const change = (event) => {\n const target = event.target, field = target.dataset.control;\n if (field === "voice-volume") {\n target.dataset.editing = "true";\n void performVoice("voice.setVolume", { playerId: target.dataset.peer, volume: Number(target.value) }).finally(() => {\n delete target.dataset.editing;\n updateVoice();\n });\n return;\n }\n if (field === "mode") dispatch({ type: "mode", mode: target.value });\n if (field === "role") void perform(model.session?.room?.status === "lobby" ? "room.role" : "room.requestRole", { role: target.value });\n if (field === "team") void perform("room.team", { team: Number(target.value) });\n if (field === "shortcut") {\n const enabled = target.checked;\n try {\n win.localStorage.setItem("caisual-overlay-shortcut-v1", enabled ? "on" : "off");\n } catch {\n }\n dispatch({ type: "shortcut", enabled });\n }\n const query = boards?.state.query;\n if (query && ["board", "period", "category"].includes(field ?? "")) {\n const next = { ...query };\n if (field === "board") {\n next.board = target.value;\n next.period = (manifest.boards[next.board].periods ?? ["all-time"])[0];\n delete next.day;\n }\n if (field === "period") {\n next.period = target.value;\n delete next.day;\n }\n if (field === "category") next.guests = target.value === "guests";\n boards.select(next);\n }\n };\n const submit = (event) => {\n const form = event.target;\n if (!form.dataset.form) return;\n event.preventDefault();\n const code = normalizeInvite(form.querySelector(\'input[data-control="code"]\').value);\n if (!code) {\n dispatch({ type: "error", code: "invalid_code" });\n return;\n }\n void perform(form.dataset.form === "watch" ? "room.watch" : "room.join", { code });\n };\n const keydown = (event) => {\n const panel = visiblePanel(model);\n if (panel && event.key === "Escape") {\n event.preventDefault();\n event.stopImmediatePropagation();\n close();\n return;\n }\n if (panel && event.key === "Tab") {\n const items = controls().filter((el) => el.closest(panel === "boot" ? ".boot" : ".dialog")), first = items[0], last = items.at(-1);\n if (!first) {\n event.preventDefault();\n return;\n }\n if (event.shiftKey && (root.activeElement === first || !items.includes(root.activeElement))) {\n event.preventDefault();\n last?.focus();\n } else if (!event.shiftKey && (root.activeElement === last || !items.includes(root.activeElement))) {\n event.preventDefault();\n first.focus();\n }\n } else if (!panel && model.shortcutEnabled && event.key === "Tab" && event.shiftKey && !event.ctrlKey && !event.altKey && !event.metaKey) {\n event.preventDefault();\n toggle();\n }\n };\n root.addEventListener("input", (event) => {\n const node = event.target;\n if (node.dataset.control === "code") codeDraft = node.value;\n if (node.dataset.control === "voice-volume") node.dataset.editing = "true";\n });\n root.addEventListener("click", click);\n root.addEventListener("change", change);\n root.addEventListener("submit", submit);\n win.addEventListener("keydown", keydown, true);\n win.addEventListener("resize", resize);\n win.addEventListener("scroll", resize, true);\n win.visualViewport?.addEventListener("resize", resize);\n win.visualViewport?.addEventListener("scroll", resize);\n const observer = typeof ResizeObserver !== "undefined" ? new ResizeObserver(resize) : null;\n observer?.observe(input.frame);\n const countdownTimer = win.setInterval(updateCountdown, 250);\n stops.push(input.bridge.subscribe((session) => {\n const previous = model.session;\n if (!session || session.id !== previous?.id) {\n voiceOperation++;\n voicePending = null;\n voiceError = null;\n }\n if (!session) {\n lastView = "";\n boards?.reset();\n operation++;\n model.busy = false;\n }\n if (previous && session && JSON.stringify({ ...previous, voice: null }) === JSON.stringify({ ...session, voice: null })) {\n model = reduceUi(model, { type: "session", session });\n updateVoice();\n } else dispatch({ type: "session", session });\n }));\n stops.push(input.bridge.onOpen((panel) => setPanel(panel)), input.bridge.onShortcut(toggle), input.bridge.onError(({ error }) => {\n if (!visiblePanel(model)) setPanel("room");\n dispatch({ type: "error", code: error.code });\n }));\n stops.push(input.bridge.onScore((score) => boards?.queued(score)));\n if (input.crew) stops.push(input.crew.subscribe(() => {\n const state = input.crew.getSnapshot();\n if (state.follow || state.invites.length) announce(t("friends"));\n render();\n }));\n render();\n return { element: host, root, dispose() {\n disposed = true;\n operation++;\n stops.forEach((stop) => stop());\n boards?.dispose();\n observer?.disconnect();\n win.clearInterval(countdownTimer);\n win.cancelAnimationFrame(geometryFrame);\n win.clearTimeout(bootTimer);\n win.clearTimeout(bootFadeTimer);\n win.removeEventListener("keydown", keydown, true);\n win.removeEventListener("resize", resize);\n win.removeEventListener("scroll", resize, true);\n win.visualViewport?.removeEventListener("resize", resize);\n win.visualViewport?.removeEventListener("scroll", resize);\n input.frame.inert = oldInert;\n if (oldTabIndex === null) input.frame.removeAttribute("tabindex");\n else input.frame.setAttribute("tabindex", oldTabIndex);\n void input.bridge.request("overlay.view", { inputBlocked: false, reservedRects: [], shortcutEnabled: false }).catch(() => {\n });\n host.remove();\n } };\n}\nexport {\n avviaHandshake,\n creaPonteOspite,\n eMessaggioReady,\n eRichiestaBiglietto,\n mountOverlay,\n overlayConfiguration,\n overlayLanguage,\n overlayLocale,\n styles as overlayStyles,\n stanzaDaMessaggio\n};\n');
5079
5083
  return;
5080
5084
  }
5081
5085
  if (url.pathname === "/__caisual/players" && (request.method === "GET" || request.method === "HEAD")) {
@@ -6363,12 +6367,14 @@ var ApiError = class extends Error {
6363
6367
  hints;
6364
6368
  };
6365
6369
  function help() {
6366
- return `Caisual ${"0.13.0"}
6370
+ return `Caisual ${"0.14.0"}
6367
6371
 
6368
6372
  Usage:
6369
6373
  caisual init [--multiplayer | --arcade] [folder]
6370
6374
  caisual dev [folder] [--port 8790] [--day YYYY-MM-DD] [--latency ms [--jitter ms] [--loss percent]]
6371
6375
  caisual check [folder] [--json]
6376
+ caisual versions [folder|id]
6377
+ caisual rollback [folder|id] --to <n>
6372
6378
  caisual publish [folder]
6373
6379
  caisual unlist [folder|id]
6374
6380
  caisual relist [folder|id]
@@ -6980,7 +6986,10 @@ async function publish(folderArgument) {
6980
6986
  if (typeof completed.url !== "string") {
6981
6987
  throw new CliError(1, "The portal completed the version without returning the game URL.");
6982
6988
  }
6983
- if (version.n !== null) process.stdout.write(`Published version ${version.n}.
6989
+ if (completed.current === false) {
6990
+ process.stdout.write(`Version ${version.n} is ready but not current: version ${completed.currentVersion} was activated in the meantime. Run caisual rollback --to ${version.n} to activate it.
6991
+ `);
6992
+ } else if (version.n !== null) process.stdout.write(`Published version ${version.n}.
6984
6993
  `);
6985
6994
  process.stdout.write(`${completed.url}
6986
6995
  `);
@@ -7024,6 +7033,37 @@ async function manageGame(operation, target) {
7024
7033
  process.stdout.write(`${verb} ${id}.
7025
7034
  `);
7026
7035
  }
7036
+ async function gameVersions(target, to) {
7037
+ const id = await gameIdFromTarget(target);
7038
+ const key = publishingKey();
7039
+ const payload = await requestJson(
7040
+ `${portalOrigin()}/api/games/${encodeURIComponent(id)}/${to === void 0 ? "versions" : "rollback"}`,
7041
+ {
7042
+ method: to === void 0 ? "GET" : "POST",
7043
+ headers: { Authorization: `Bearer ${key}`, "Content-Type": "application/json; charset=utf-8" },
7044
+ ...to === void 0 ? {} : { body: JSON.stringify({ to }) }
7045
+ }
7046
+ );
7047
+ if (to !== void 0) {
7048
+ if (payload.id !== id || payload.n !== to || payload.current !== true) {
7049
+ throw new CliError(1, "The portal returned an invalid rollback response.");
7050
+ }
7051
+ process.stdout.write(`Restored ${id} to version ${to}. This restores code, not saved data or scores.
7052
+ `);
7053
+ return;
7054
+ }
7055
+ if (payload.id !== id || !Array.isArray(payload.versions)) {
7056
+ throw new CliError(1, "The portal returned an invalid versions response.");
7057
+ }
7058
+ for (const raw of payload.versions) {
7059
+ const version = object2(raw);
7060
+ if (!version || !Number.isSafeInteger(version.n) || typeof version.createdAt !== "string" || !["open", "ready", "failed"].includes(String(version.status)) || typeof version.artifactBytes !== "number" || typeof version.current !== "boolean") {
7061
+ throw new CliError(1, "The portal returned an invalid version.");
7062
+ }
7063
+ process.stdout.write(`${version.current ? "* " : " "}Version ${version.n} | ${version.createdAt} | ${version.status} | ${formatBytes(version.artifactBytes)}${version.current ? " | current" : ""}
7064
+ `);
7065
+ }
7066
+ }
7027
7067
  async function installSkill() {
7028
7068
  const root = process.cwd();
7029
7069
  const skillPath = join4(root, ".claude", "skills", "caisual", "SKILL.md");
@@ -7066,7 +7106,7 @@ async function run(argumentsList) {
7066
7106
  return;
7067
7107
  }
7068
7108
  if (command === "--version" || command === "-V") {
7069
- process.stdout.write(`${"0.13.0"}
7109
+ process.stdout.write(`${"0.14.0"}
7070
7110
  `);
7071
7111
  return;
7072
7112
  }
@@ -7102,6 +7142,26 @@ async function run(argumentsList) {
7102
7142
  await check(folder, json);
7103
7143
  return;
7104
7144
  }
7145
+ if (command === "versions" || command === "rollback") {
7146
+ let target = ".";
7147
+ let targetSeen = false;
7148
+ let to;
7149
+ const usage = `Usage: caisual ${command} [folder|id]${command === "rollback" ? " --to <n>" : ""}`;
7150
+ for (let i = 0; i < argumentsAfterCommand.length; i++) {
7151
+ const arg = argumentsAfterCommand[i];
7152
+ if (command === "rollback" && to === void 0 && (arg === "--to" || arg.startsWith("--to="))) {
7153
+ const value = arg === "--to" ? argumentsAfterCommand[++i] : arg.slice(5);
7154
+ if (!value || !/^[1-9][0-9]*$/.test(value) || !Number.isSafeInteger(Number(value))) throw new CliError(1, usage);
7155
+ to = Number(value);
7156
+ } else if (!arg.startsWith("-") && !targetSeen) {
7157
+ target = arg;
7158
+ targetSeen = true;
7159
+ } else throw new CliError(1, usage);
7160
+ }
7161
+ if (command === "rollback" && to === void 0) throw new CliError(1, usage);
7162
+ await gameVersions(target, to);
7163
+ return;
7164
+ }
7105
7165
  if (command === "unlist" || command === "relist") {
7106
7166
  if (argumentsAfterCommand.length > 1 || argumentsAfterCommand.some((value) => value.startsWith("-"))) {
7107
7167
  throw new CliError(1, `Usage: caisual ${command} [folder|id]`);