Something went wrong. Try again.
Messages and small helpers so web tile games and their hosts can talk.
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165<!doctype html><html lang="en"><head><meta charset="utf-8"><meta name="viewport" content="width=device-width, initial-scale=1"><title>Game Host Kit: example host</title><style> :root { color-scheme: light dark; font: 15px/1.45 system-ui, sans-serif; } body { margin: 0; padding: 16px; max-width: 1100px; margin-inline: auto; } h1 { font-size: 20px; margin: 0 0 4px; } .lede { margin: 0 0 14px; opacity: .8; } .controls { display: flex; flex-wrap: wrap; gap: 8px 16px; align-items: center; margin-bottom: 12px; } .controls label { display: inline-flex; gap: 6px; align-items: center; } .stage { display: grid; grid-template-columns: minmax(0, 1fr) 300px; gap: 14px; } iframe { width: 100%; aspect-ratio: 16 / 10; border: 1px solid #8884; border-radius: 10px; background: #fff; } .log { font: 12px/1.5 ui-monospace, monospace; border: 1px solid #8884; border-radius: 10px; padding: 8px; height: 420px; overflow: auto; white-space: pre-wrap; overflow-wrap: anywhere; margin: 0; } .log .in { color: #2a7; } .log .out { color: #47c; } dialog { border-radius: 12px; border: 1px solid #8886; max-width: 440px; } dialog pre { font-size: 12px; max-height: 160px; overflow: auto; background: #8881; padding: 8px; border-radius: 8px; } .row { display: flex; gap: 8px; justify-content: flex-end; margin-top: 12px; } .saved { margin-top: 12px; font-size: 13px; } .btnlink { display: inline-block; padding: 2px 10px; border: 1px solid #8886; border-radius: 6px; text-decoration: none; color: inherit; } @media (max-width: 760px) { .stage { grid-template-columns: 1fr; } .log { height: 200px; } }</style></head><body><h1>Example host</h1><p class="lede">A page that runs a game and answers it with the Game Host Kit's host helper. Everything here is made up(no real accounts, nothing leaves the page), so you can try a game's side without signing in anywhere.</p>
<div class="controls"> <label>Signed in as <select id="who"> <option value="">no one</option> <option value="alice" selected>@alice.example (has a character)</option> <option value="bob">@bob.example (no character)</option> </select> </label> <label><input type="checkbox" id="hide"> Don't show who I am to games</label> <button id="reload" type="button">Reload the game</button></div>
<div class="stage"> <iframe id="game" title="The game"></iframe> <pre class="log" id="log" aria-label="Messages"></pre></div><div class="saved" id="saved"></div>
<dialog id="person"> <form method="dialog"> <p>The game asked to show this person:</p> <p><code id="personDid"></code></p> <div class="row"><button value="cancel">Cancel</button><a id="personLink" class="btnlink" href="#" target="_blank" rel="noopener">Open their profile</a></div> </form></dialog>
<dialog id="ask"> <form method="dialog"> <p id="askText"></p> <pre id="askRecord"></pre> <div class="row"><button value="no">No</button><button value="yes" autofocus>Save it</button></div> </form></dialog>
<script type="module">import { attachToFrame } from "../host/game-host.js";
// Which game to run: ?game=path/to/index.html (default: the example game next to this page).const params = new URLSearchParams(location.search);const gameSrc = params.get("game") || "game.html";// The tile this page "loaded". A real host takes these from the tile record it actually read (rule 3).const TILE = { uri: "at://did:plc:examplegameexamplegameex/ing.dasl.masl/3examplegame", cid: "bafyreiexampleversionexampleversion" };// The games this host saves for, and into which collections: only a game's own.const SAVES = { [TILE.uri]: ["com.example.game.score", "cafe.thunderbird.cosmicjunkyard.run"] };
const ACCOUNTS = { alice: { did: "did:plc:aliceexamplealiceexample", handle: "alice.example" }, bob: { did: "did:plc:bobexamplebobexamplebobe", handle: "bob.example" },};const frame = document.getElementById("game");const logBox = document.getElementById("log");const who = document.getElementById("who");const hide = document.getElementById("hide");const savedList = [];
function log(dir, payload) { const line = document.createElement("div"); line.className = dir; const shown = JSON.stringify(payload, (k, v) => (v instanceof Blob ? `[${v.type} ${v.size} bytes]` : typeof v === "string" && v.startsWith("data:") ? "[picture]" : v)); line.textContent = `${dir === "in" ? "game → host" : "host → game"} ${shown}`; logBox.append(line); logBox.scrollTop = logBox.scrollHeight;}
// A made-up character: a 3 × 4 sheet of coloured squares, drawn here.async function makeSheet(colour) { const c = document.createElement("canvas"); c.width = 96; c.height = 128; const g = c.getContext("2d"); for (let r = 0; r < 4; r++) for (let col = 0; col < 3; col++) { g.fillStyle = colour; g.fillRect(col * 32 + 6, r * 32 + 4, 20, 24); g.fillStyle = "#fff"; g.fillRect(col * 32 + 10 + col * 2, r * 32 + 10, 4, 4); } return new Promise((ok) => c.toBlob(ok, "image/png"));}
let host = null;function start() { if (host) host.detach(); logBox.replaceChildren(); host = attachToFrame(frame, { tile: () => TILE, player: () => (hide.checked ? null : ACCOUNTS[who.value] || null), character: async (p) => { if (p.did !== ACCOUNTS.alice.did) return null; return { image: await makeSheet("#c0392b"), frameWidth: 32, frameHeight: 32, sheetWidth: 96, sheetHeight: 128, from: "compendium", entry: { did: p.did, name: "Alice", stats: { speed: 3, luck: 5 } } }; }, save: { collections: (tile) => SAVES[tile.uri] || [], write: ({ collection, record, game, status }) => new Promise((resolve) => { const player = hide.checked ? null : ACCOUNTS[who.value]; if (!player) { resolve({ ok: false, error: "declined" }); return; } // a real host would sign the player in first // The host adds what only it knows: $type, createdAt, and which game and version (rule 3). const full = { $type: collection, ...record, game, createdAt: new Date().toISOString() }; status("asking"); const ask = document.getElementById("ask"); document.getElementById("askText").textContent = `Save this to @${player.handle}'s account?`; document.getElementById("askRecord").textContent = JSON.stringify(full, null, 2); ask.addEventListener("close", function done() { ask.removeEventListener("close", done); if (ask.returnValue !== "yes") { resolve({ ok: false, error: "declined" }); return; } status("saving"); setTimeout(() => { const uri = `at://${player.did}/${collection}/3save${savedList.length + 1}`; savedList.push(uri); document.getElementById("saved").textContent = `Saved here (pretend): ${savedList.join(", ")}`; resolve({ ok: true, uri, handle: player.handle }); }, 300); }); ask.showModal(); }), }, openRecord: (uri) => alert(`The game asked to show a record this page saved:\n${uri}`), openProfile: (did) => { // The host decides where a person goes, and checks before leaving the page. The request arrives as a message, // not a click on this page, so window.open() here would be stopped as a pop-up: show a link the player clicks. const dialog = document.getElementById("person"); document.getElementById("personDid").textContent = did; const link = document.getElementById("personLink"); link.href = `https://bsky.app/profile/${did}`; link.onclick = () => dialog.close(); dialog.showModal(); }, }, { watch: log }); frame.src = `${gameSrc}${gameSrc.includes("?") ? "&" : "?"}t=${Date.now()}`;}who.addEventListener("change", () => host && host.playerChanged());hide.addEventListener("change", () => host && host.playerChanged());document.getElementById("reload").addEventListener("click", start);start();</script></body></html>