Playing alone, together, saved games
One game file plays alone or together. The lobby has Play alone (a private room of one: it starts by itself, and keeps the saved profile and saved game) when the game allows one player (min 1, no cup; multiplayer.solo: false hides it), and Play with others in a room of one (before it starts or after it ends). While playing alone the bar has End (tap twice) back to that lobby. A room of one is joined with alone: true (room.js join({alone}), relay j.alone): the hub keeps its world save under the player (SavePort who = "alone:<seat key>"), so a game alone and the shared game played together never read or overwrite each other, and nobody else can join it by code. The profile is the same in both. Invite anyone shows in a room for anyone when the host app offers it (relay option inviteAnyone, info anyone: true, room.js inviteAnyone(), port message invite-anyone): the app shows its own way in for people without an account (for example a share dialog with a public link). The public wrapper and the view page do not offer it. With no host app around the page it runs as the one-player game. Write for g.players (1 to max) and it works both ways.
Saving needs a host app that gives the hub a storage port (new RoomHub({ saves }), SavePort in hub.ts: load(scope, slot, who, {claim?}) gives {at, data, v?} or null, store(scope, slot, who, json | null, {session?, v?})) and a stable slot per game in the socket’s identity (not the page hash, so editing a game keeps its saves). Without them everything below is off and g.saves is false.
- Player profile (
g.profile,g.profile.set({...})): one small document per person and game, for stats, level, items, weapons, outfits, hp, mp, lives left. Loaded beforesetup(the game waits up to 3 s); merges like shared writes; written within seconds and when the person leaves. A profile that arrives after that wait is taken only if the game has not written its own yet; otherwise this page does not save and says “Progress is not being saved in this game”. A public link’s visitor keeps theirs in their own browser (relay.jsoptionprofile). - World save (
multiplayer.save: true): the host’sg.shared(map level, monsters and their stats, progress) is written while it changes and at the end of a match. A new room finds it: the host sees Continue and New game (New game asks first: it replaces the save); Continue starts from the saved state (g.resumed, writes tog.sharedinsidesetupare ignored),g.clearSave()ends the saved game. Shell keys (teams, seats, cup) are saved with it. One room at a time plays a given save: a second room of the same game (another group, a second tab alone) plays unsaved (saveBusyin welcome; the lobby says so and offers no Continue), and the next room after the first one closes continues it. A save that could not be read is never written over: until it is read the room writes and clears nothing (a Start waits up to 3 s for it, so Continue is still offered); a failed read is tried again after 2, 5, 15 s, then every 30 s, and meanwhile the lobby says “The saved game could not be opened, so this game is not being saved” (saveErrorin welcome,savestate {error}when it changes). A match that started without the save stays unsaved; the next start offers Continue. - A save per room (
multiplayer.save: "room", made forRoom()boards): as the world save, but each room has its own, stored underroom:<code>, and only rooms with a 12-letter code keep one (made for them; the code is the key, so it must not be guessable). The relay keeps the code in the address (#room=;keepCode: falseleaves the address alone). Joining that code when the room is not live opens it again from its save. - Checkpoint saves (
multiplayer.save: "checkpoint"): nothing is written while the game runs; the host callsg.checkpoint(data)with a plain object it chose (the map, levels, lanterns lit: what is needed to start again, not monsters mid-fight), the hub writes it at once (one a second at most,{t:"world", save}) and the result comes toonCheckpoint(g, ok, why). The run belongs to the signed-in person who opened the room (party:<seat>in the store,alone:<seat>for Play alone), so two groups never share one run and only that person, as host, saves it.g.saveState()says what applies to this player:"ok","sign_in"(a guest: the room of a guest keeps nothing),"host"(someone else’s run),"busy","off". Continue starts from the checkpoint withg.resumed. Signed-in players on a remote server: givecreateRemoteServeranaccountSecretand the deployment anaccountsURL on your own site that answers a signed-in visitor (your cookie) with{ticket: accountTicket(new Tickets(secret), depId, {id, name})}; the boot script asks it before each connect and seats the visitor asu:<id>; everyone else plays as a guest. - Save versions (
multiplayer.saveVersion, a whole number, default 1): kept with every world save and profile (a save without one counts as 1). When a change to the game makes old saves unreadable, raise it and givemigrateSave(old, fromVersion)(andmigrateProfile(old, fromVersion)) returning the new shape: the host runs it once beforesetup()and its result replaces the game’s own keys (shell keys stay). WithoutmigrateSave, or for a save from a newer version, the lobby says the save was made by another version and offers no Continue; an old profile withoutmigrateProfileis used as it is. Pages with differentsaveVersionnever share a room (the newer one is told to reload). - Stores:
memorySaves()andfileSaves(dir)innode.ts(the demo server uses files:TICKCLI_SAVES, default~/.tickcli-saves). A host app can bring its ownSavePort; keeping earlier copies of each world save (the save before a session’s first write, one a day) helps with support.fileSaveswrites async (temp file, fsync, rename), keeps no earlier copies, does not queue writes per file, and reads a damaged file as no save (after moving it to.bad-<time>). The store hears one session id per room and the save format (store(..., {session, v})). A world load passes{claim: session}: a store shared by several processes (blue/green) should remember it before reading and refuse a world write or delete from any other session (error codesuperseded), so a late write from a room that handed the save on never lands over the new owner’s game.fileSavesdoes this with a<hash>.ownerfile beside the save, checked again after the flush. - Example:
examples/games/delve.html(levels, xp, gold, potions, weapons and outfits by level, hp and mp, team lives, depth).