Skip to content

Open-source platform for 3D AI agents. Turn text or a photo into a rigged, animated GLB avatar, give it an LLM brain, memory and a wallet, and embed it anywhere with one web component. Remote MCP server, x402 payments, Three.js, browser-native.

feat(wars): queue a community and pair it with the next one waiting

nirholased60b0de5522
diff --git a/api/_lib/war-store.js b/api/_lib/war-store.js
new file mode 100644
index 000000000..e210b0a2f
--- /dev/null
+++ b/api/_lib/war-store.js
@@ -0,0 +1,339 @@
+// Coin Wars — persistence for the war league.
+//
+// Three small stores behind one module, all keyed off the coin mint that a
+// community fights under:
+//
+// 1. The BATTLE LEDGER — every finished clash, appended by the game server
+// (multiplayer/src/rooms/ClashRoom.js → war-report.js → POST /api/wars).
+// The Elo standings are recomputed from this list on every read with the
+// shared math in multiplayer/src/war-standings.js, so there is exactly one
+// implementation of "who is winning the war" and nothing to keep in sync.
+// 2. The LIVE REGISTRY — a short-TTL snapshot of every clash currently in
+// progress, refreshed by the room itself. This is what lets a player
+// standing in a coin's world watch a war they are not in without opening a
+// second 3D client.
+// 3. The MATCHMAKING QUEUE — communities waiting for an opponent. Pairing two
+// of them mints the `matchKey` that Colyseus's filterBy(['matchKey']) uses
+// to seat both sides in the same ClashRoom instance.
+//
+// Storage is Upstash Redis, with an in-process fallback that has the same
+// semantics so the whole feature is playable on a laptop with no infra (the
+// pattern clash-store.js established, for the same reason: a Redis blip must
+// degrade the league, never 5xx the world).
+
+import { getRedis } from './redis.js';
+
+const redis = getRedis();
+
+const K = {
+ ledger: 'wars:ledger', // capped LIST of battle JSON, newest first
+ live: 'wars:live', // HASH matchKey → live-war JSON (self-expiring by field)
+ queue: 'wars:queue', // HASH mint → queued-community JSON
+ pairing: 'wars:pair', // HASH `${mint}:${wallet}` → pairing JSON
+};
+
+// How many finished battles the ledger keeps. Elo is path-dependent, so a
+// truncated ledger re-bases ratings on the window it can see; 1000 battles is
+// far more history than the standings board ever shows and keeps the recompute
+// on a single Redis read.
+const LEDGER_MAX = 1000;
+
+// A live-war snapshot is stale this long after its last heartbeat. ClashRoom
+// beats every 10s while a match is alive, so three missed beats retire it — a
+// crashed game server can never leave a phantom war on the portal board.
+export const LIVE_TTL_MS = 35_000;
+
+// A community waits this long for an opponent before the queue forgets it. Long
+// enough to walk to the portal and read the board, short enough that a stale
+// entry never pairs someone who has closed the tab.
+export const QUEUE_TTL_MS = 180_000;
+
+// A minted pairing waits this long to be collected by the side that was already
+// queued. Comfortably longer than one poll interval and shorter than the queue
+// window, so a collected pairing is always fresher than a re-queue.
+export const PAIRING_TTL_MS = 120_000;
+
+let _degradedAt = 0;
+function degraded(err) {
+ const now = Date.now();
+ if (now - _degradedAt > 60_000) {
+ _degradedAt = now;
+ console.warn('[war-store] redis degraded — serving from in-memory fallback:', err?.message || err);
+ }
+}
+
+// ─── In-memory fallback (dev / tests / Redis outage) ─────────────────────────
+
+const mem = {
+ ledger: [],
+ live: new Map(),
+ queue: new Map(),
+ pairing: new Map(),
+};
+
+function memHashPrune(map, ttlMs) {
+ const now = Date.now();
+ for (const [k, v] of map) if (!v || now - (v.at || 0) > ttlMs) map.delete(k);
+ return map;
+}
+
+// ─── Battle ledger ───────────────────────────────────────────────────────────
+
+/**
+ * Append one finished battle. Returns true when it landed durably, false when it
+ * only reached the in-process fallback — the caller reports that honestly rather
+ * than claiming a league write it did not make.
+ * @param {object} battle the shape ClashMatch.result() emits, plus matchKey/network
+ */
+export async function appendBattle(battle) {
+ mem.ledger.unshift(battle);
+ if (mem.ledger.length > LEDGER_MAX) mem.ledger.length = LEDGER_MAX;
+ if (!redis) return false;
+ try {
+ await redis.lpush(K.ledger, JSON.stringify(battle));
+ await redis.ltrim(K.ledger, 0, LEDGER_MAX - 1);
+ return true;
+ } catch (err) {
+ degraded(err);
+ return false;
+ }
+}
+
+/**
+ * The battle ledger, newest first. `limit` bounds the read; the standings
+ * recompute wants the whole window, a "recent results" board wants a handful.
+ * @returns {Promise<object[]>}
+ */
+export async function listBattles(limit = LEDGER_MAX) {
+ const n = Math.max(1, Math.min(LEDGER_MAX, limit | 0 || LEDGER_MAX));
+ if (redis) {
+ try {
+ const raw = await redis.lrange(K.ledger, 0, n - 1);
+ return (raw || []).map(parseMaybe).filter(Boolean);
+ } catch (err) {
+ degraded(err);
+ }
+ }
+ return mem.ledger.slice(0, n);
+}
+
+// ─── Live registry ───────────────────────────────────────────────────────────
+
+/**
+ * Publish (or refresh) the live snapshot of one clash. `war` carries the two
+ * factions, the score, the phase and the clock — everything the portal board
+ * renders. Stamped with `at` so readers can retire it without a per-key TTL.
+ */
+export async function putLiveWar(war) {
+ const record = { ...war, at: Date.now() };
+ mem.live.set(record.matchKey, record);
+ memHashPrune(mem.live, LIVE_TTL_MS);
+ if (!redis) return false;
+ try {
+ await redis.hset(K.live, { [record.matchKey]: JSON.stringify(record) });
+ // The hash itself expires if the game server goes away entirely, so a dead
+ // deployment cannot leave a permanent key behind.
+ await redis.expire(K.live, Math.ceil((LIVE_TTL_MS * 6) / 1000));
+ return true;
+ } catch (err) {
+ degraded(err);
+ return false;
+ }
+}
+
+/** Retire one live war (the match ended, or the room disposed). Idempotent. */
+export async function dropLiveWar(matchKey) {
+ mem.live.delete(matchKey);
+ if (!redis) return;
+ try {
+ await redis.hdel(K.live, matchKey);
+ } catch (err) {
+ degraded(err);
+ }
+}
+
+/**
+ * Every clash currently in progress, freshest first. Stale entries (no heartbeat
+ * within LIVE_TTL_MS) are dropped from the result AND swept from the hash, so a
+ * crashed room self-cleans on the next read instead of needing a cron.
+ * @returns {Promise<object[]>}
+ */
+export async function listLiveWars() {
+ const now = Date.now();
+ if (redis) {
+ try {
+ const all = (await redis.hgetall(K.live)) || {};
+ const live = [];
+ const stale = [];
+ for (const [key, value] of Object.entries(all)) {
+ const war = parseMaybe(value);
+ if (!war || now - (war.at || 0) > LIVE_TTL_MS) stale.push(key);
+ else live.push(war);
+ }
+ if (stale.length) await redis.hdel(K.live, ...stale).catch(() => {});
+ return live.sort((a, b) => (b.at || 0) - (a.at || 0));
+ } catch (err) {
+ degraded(err);
+ }
+ }
+ memHashPrune(mem.live, LIVE_TTL_MS);
+ return [...mem.live.values()].sort((a, b) => (b.at || 0) - (a.at || 0));
+}
+
+// ─── Matchmaking queue ───────────────────────────────────────────────────────
+
+/**
+ * The communities currently waiting for an opponent, freshest first. Expired
+ * entries are swept on read for the same reason the live registry sweeps.
+ * @returns {Promise<object[]>}
+ */
+export async function listQueue() {
+ const now = Date.now();
+ if (redis) {
+ try {
+ const all = (await redis.hgetall(K.queue)) || {};
+ const waiting = [];
+ const stale = [];
+ for (const [mint, value] of Object.entries(all)) {
+ const entry = parseMaybe(value);
+ if (!entry || now - (entry.at || 0) > QUEUE_TTL_MS) stale.push(mint);
+ else waiting.push(entry);
+ }
+ if (stale.length) await redis.hdel(K.queue, ...stale).catch(() => {});
+ return waiting.sort((a, b) => (b.at || 0) - (a.at || 0));
+ } catch (err) {
+ degraded(err);
+ }
+ }
+ memHashPrune(mem.queue, QUEUE_TTL_MS);
+ return [...mem.queue.values()].sort((a, b) => (b.at || 0) - (a.at || 0));
+}
+
+/** Seat (or refresh) a community in the queue. */
+export async function enqueue(entry) {
+ const record = { ...entry, at: Date.now() };
+ mem.queue.set(record.mint, record);
+ if (!redis) return record;
+ try {
+ await redis.hset(K.queue, { [record.mint]: JSON.stringify(record) });
+ await redis.expire(K.queue, Math.ceil((QUEUE_TTL_MS * 4) / 1000));
+ } catch (err) {
+ degraded(err);
+ }
+ return record;
+}
+
+/**
+ * Claim a waiting community out of the queue. The Redis HDEL reply IS the claim
+ * token: exactly one concurrent caller sees a 1, so two players queueing at the
+ * same instant can never both pair with the same opponent and end up in two
+ * different rooms. Returns true when this caller won the claim.
+ */
+export async function claimQueued(mint) {
+ if (!redis) return mem.queue.delete(mint);
+ try {
+ const removed = await redis.hdel(K.queue, mint);
+ mem.queue.delete(mint);
+ return Number(removed) > 0;
+ } catch (err) {
+ degraded(err);
+ return mem.queue.delete(mint);
+ }
+}
+
+/** Drop a community from the queue (they walked away, or they just got paired). */
+export async function dequeue(mint) {
+ mem.queue.delete(mint);
+ if (!redis) return;
+ try {
+ await redis.hdel(K.queue, mint);
+ } catch (err) {
+ degraded(err);
+ }
+}
+
+// ─── Pairings ────────────────────────────────────────────────────────────────
+//
+// When a fresh challenger pairs with a queued community, only the challenger's
+// request knows the minted matchKey. The queued side is not holding an open
+// request, so the pairing is parked here under their mint+wallet and collected
+// by their next poll. That keeps matchmaking to two plain reads instead of a
+// socket the portal would have to hold open while nobody is even near it.
+
+function pairKey(mint, wallet) {
+ return `${mint}:${wallet}`;
+}
+
+export async function putPairing(mint, wallet, pairing) {
+ const record = { ...pairing, at: Date.now() };
+ mem.pairing.set(pairKey(mint, wallet), record);
+ if (!redis) return record;
+ try {
+ await redis.hset(K.pairing, { [pairKey(mint, wallet)]: JSON.stringify(record) });
+ await redis.expire(K.pairing, Math.ceil((PAIRING_TTL_MS * 4) / 1000));
+ } catch (err) {
+ degraded(err);
+ }
+ return record;
+}
+
+/**
+ * Read the pairing waiting for this wallet, or null. Reads do NOT consume it: a
+ * player who reloads mid-handoff must still find the war they were matched into.
+ * It lapses on its own after PAIRING_TTL_MS.
+ */
+export async function getPairing(mint, wallet) {
+ const key = pairKey(mint, wallet);
+ const now = Date.now();
+ if (redis) {
+ try {
+ const raw = await redis.hget(K.pairing, key);
+ const rec = parseMaybe(raw);
+ if (!rec) return null;
+ if (now - (rec.at || 0) > PAIRING_TTL_MS) {
+ await redis.hdel(K.pairing, key).catch(() => {});
+ return null;
+ }
+ return rec;
+ } catch (err) {
+ degraded(err);
+ }
+ }
+ const rec = mem.pairing.get(key);
+ if (!rec) return null;
+ if (now - (rec.at || 0) > PAIRING_TTL_MS) { mem.pairing.delete(key); return null; }
+ return rec;
+}
+
+export async function dropPairing(mint, wallet) {
+ mem.pairing.delete(pairKey(mint, wallet));
+ if (!redis) return;
+ try {
+ await redis.hdel(K.pairing, pairKey(mint, wallet));
+ } catch (err) {
+ degraded(err);
+ }
+}
+
+// ─── internals ───────────────────────────────────────────────────────────────
+
+// Upstash decodes JSON string values for us on some paths and hands back the raw
+// string on others; accept both rather than guessing which one a given command
+// took.
+function parseMaybe(value) {
+ if (value == null) return null;
+ if (typeof value === 'object') return value;
+ try {
+ return JSON.parse(String(value));
+ } catch {
+ return null;
+ }
+}
+
+/** Test seam: clear the in-process fallback between cases. */
+export function __resetWarStoreMemory() {
+ mem.ledger.length = 0;
+ mem.live.clear();
+ mem.queue.clear();
+ mem.pairing.clear();
+}
diff --git a/multiplayer/src/war-matchmaking.js b/multiplayer/src/war-matchmaking.js
new file mode 100644
index 000000000..07cd74e80
--- /dev/null
+++ b/multiplayer/src/war-matchmaking.js
@@ -0,0 +1,235 @@
+// War matchmaking — the pure pairing math behind the Coin Wars portal.
+//
+// A player standing in their coin's world presses "Enter the war". That queues
+// their community. When a SECOND community is waiting, the two are paired and
+// both sides are handed the SAME `matchKey`, which is what actually puts them in
+// one arena: ClashRoom is defined with filterBy(['matchKey']), so every fighter
+// carrying a given key lands in the same room instance.
+//
+// This module owns three things and nothing else:
+// • the matchKey format (mint it, parse it, verify it names two given mints)
+// • the queue fold: given the current queue and a joining community, decide
+// whether this is a pairing or a wait, and produce the next queue
+// • the staleness rule that keeps a queue from pairing someone who walked away
+//
+// Pure and dependency-free, like clash.js and war-standings.js, so the pairing
+// rules are unit-testable without Redis, a room, or a browser. The API layer
+// (api/_lib/wars-store.js) supplies the stored queue and persists what comes
+// back; ClashRoom trusts the key only after the signed ticket (war-ticket.js)
+// proves the API minted it.
+
+// How long a queued community stays live. Long enough to survive a slow avatar
+// load and a holder-pass round trip, short enough that a player who closed the
+// tab is not still "waiting" when someone else walks up two minutes later.
+export const QUEUE_TTL_MS = 90_000;
+
+// How long a pairing stays claimable after it is made. A paired entry lives a
+// little longer than an unpaired one so the second side can poll, load the arena
+// page, and join without the pairing evaporating underneath them.
+export const PAIR_TTL_MS = 10 * 60_000;
+
+const KEY_VERSION = 'w1';
+const MINT_RE = /^[A-Za-z0-9]{32,64}$/;
+
+// Canonical faction order for a pair of mints. Both sides of a match derive the
+// SAME key regardless of who queued first, which is the whole point: the key is
+// the rendezvous, so it cannot depend on arrival order.
+export function orderMints(mintA, mintB) {
+ return mintA < mintB ? [mintA, mintB] : [mintB, mintA];
+}
+
+/**
+ * Mint the rendezvous key for one battle between two communities.
+ * `slot` disambiguates repeat matchups: the same two coins fighting again later
+ * get a different key, so the ledger's per-matchKey uniqueness still holds and a
+ * finished room never absorbs the next battle's fighters.
+ * @param {{network?:string, mintA:string, mintB:string, slot:number|string}} args
+ * @returns {string|null} the key, or null when the inputs cannot name a match
+ */
+export function mintMatchKey({ network = 'mainnet', mintA, mintB, slot }) {
+ if (!isMint(mintA) || !isMint(mintB) || mintA === mintB) return null;
+ const net = cleanNetwork(network);
+ const [lo, hi] = orderMints(mintA, mintB);
+ const tag = slotTag(slot);
+ if (!tag) return null;
+ return `${KEY_VERSION}:${net}:${lo}:${hi}:${tag}`;
+}
+
+/**
+ * Parse a matchKey back into the match it names. Returns null for anything this
+ * module did not mint, so a hand-crafted key can never open a room.
+ * @param {unknown} key
+ * @returns {{network:string, mints:[string,string], slot:string}|null}
+ */
+export function parseMatchKey(key) {
+ if (typeof key !== 'string' || key.length > 200) return null;
+ const parts = key.split(':');
+ if (parts.length !== 5) return null;
+ const [version, network, lo, hi, slot] = parts;
+ if (version !== KEY_VERSION) return null;
+ if (!network || network !== cleanNetwork(network)) return null;
+ if (!isMint(lo) || !isMint(hi) || lo >= hi) return null;
+ if (!/^[a-z0-9]{1,16}$/.test(slot)) return null;
+ return { network, mints: [lo, hi], slot };
+}
+
+// Does this key name a battle between exactly these two communities? ClashRoom
+// asks this before trusting the faction identities a joining client declared, so
+// a fighter cannot invent an opponent that the key does not describe.
+export function matchKeyNames(key, mintA, mintB) {
+ const parsed = parseMatchKey(key);
+ if (!parsed) return false;
+ const [lo, hi] = orderMints(String(mintA || ''), String(mintB || ''));
+ return parsed.mints[0] === lo && parsed.mints[1] === hi;
+}
+
+// Which side of the key a mint sits on: 'a' is the lexicographically lower mint.
+// Both the arena client and the portal read scores as A/B, so they need to agree
+// with the room, which seats factions in the order the key implies.
+export function sideOf(key, mint) {
+ const parsed = parseMatchKey(key);
+ if (!parsed) return null;
+ if (parsed.mints[0] === mint) return 'a';
+ if (parsed.mints[1] === mint) return 'b';
+ return null;
+}
+
+// Is a queue entry still live at `now`? Paired entries get the longer window.
+export function entryLive(entry, now) {
+ if (!entry || typeof entry.at !== 'number') return false;
+ const ttl = entry.matchKey ? PAIR_TTL_MS : QUEUE_TTL_MS;
+ return now - entry.at < ttl;
+}
+
+/**
+ * The queue fold. Given every stored entry and the community now asking to
+ * fight, return the next queue and what to tell the caller.
+ *
+ * Four outcomes, in the order they are checked:
+ * 'paired' — this community was already matched by an earlier call (the
+ * other side queued first and paired with us); hand back the key.
+ * 'matched' — we just paired with a waiting community; both entries are
+ * stamped with the new key.
+ * 'waiting' — nobody else is queued; we are now the one waiting.
+ * 'invalid' — the community is not a coin we can seat.
+ *
+ * @param {object} args
+ * @param {Array<object>} args.queue stored entries (any staleness)
+ * @param {object} args.coin { mint, name, symbol, image }
+ * @param {string} args.network
+ * @param {number} args.now epoch ms
+ * @returns {{status:string, queue:Array<object>, entry:object|null, opponent:object|null,
+ * matchKey:string|null, side:string|null, waiting:number}}
+ */
+export function joinQueue({ queue = [], coin, network = 'mainnet', now = 0 }) {
+ const mint = String(coin?.mint || '');
+ const net = cleanNetwork(network);
+ if (!isMint(mint)) {
+ return { status: 'invalid', queue: liveOnly(queue, now), entry: null, opponent: null, matchKey: null, side: null, waiting: 0 };
+ }
+
+ const live = liveOnly(queue, now).filter((e) => e.network === net || !e.network);
+ const mine = live.find((e) => e.mint === mint) || null;
+
+ // Already paired by whoever queued second — nothing to do but report it.
+ if (mine?.matchKey) {
+ const rest = live.filter((e) => e.mint !== mint);
+ return {
+ status: 'paired',
+ queue: live,
+ entry: mine,
+ opponent: mine.opponent || null,
+ matchKey: mine.matchKey,
+ side: sideOf(mine.matchKey, mint),
+ waiting: rest.filter((e) => !e.matchKey).length,
+ };
+ }
+
+ const identity = {
+ mint,
+ name: str(coin?.name, 48) || str(coin?.symbol, 16) || 'Community',
+ symbol: str(coin?.symbol, 16),
+ image: str(coin?.image, 400),
+ };
+
+ // The oldest unpaired community that is not us — first come, first fought.
+ const rival = live
+ .filter((e) => e.mint !== mint && !e.matchKey)
+ .sort((x, y) => (x.at || 0) - (y.at || 0))[0] || null;
+
+ if (rival) {
+ const matchKey = mintMatchKey({ network: net, mintA: mint, mintB: rival.mint, slot: now });
+ if (matchKey) {
+ const rivalIdentity = { mint: rival.mint, name: rival.name, symbol: rival.symbol, image: rival.image };
+ const nextMine = { ...identity, network: net, at: now, matchKey, opponent: rivalIdentity };
+ const nextRival = { ...rival, network: net, at: now, matchKey, opponent: identity };
+ const queueNext = live
+ .filter((e) => e.mint !== mint && e.mint !== rival.mint)
+ .concat([nextMine, nextRival]);
+ return {
+ status: 'matched',
+ queue: queueNext,
+ entry: nextMine,
+ opponent: rivalIdentity,
+ matchKey,
+ side: sideOf(matchKey, mint),
+ waiting: queueNext.filter((e) => !e.matchKey).length,
+ };
+ }
+ }
+
+ // Nobody to fight yet: take (or refresh) our place in line.
+ const nextMine = { ...identity, network: net, at: now, matchKey: null, opponent: null };
+ const queueNext = live.filter((e) => e.mint !== mint).concat([nextMine]);
+ return {
+ status: 'waiting',
+ queue: queueNext,
+ entry: nextMine,
+ opponent: null,
+ matchKey: null,
+ side: null,
+ waiting: queueNext.filter((e) => !e.matchKey).length,
+ };
+}
+
+// Drop one community from the queue (they backed out of the portal). Returns the
+// next queue; stale entries are pruned in the same pass so a cancel also tidies.
+export function leaveQueue({ queue = [], mint, now = 0 }) {
+ return liveOnly(queue, now).filter((e) => e.mint !== String(mint || ''));
+}
+
+// The queue as a player should see it: live, unpaired communities waiting for an
+// opponent, oldest first. This is what the portal board renders as "waiting".
+export function waitingCommunities({ queue = [], network = 'mainnet', now = 0 }) {
+ const net = cleanNetwork(network);
+ return liveOnly(queue, now)
+ .filter((e) => !e.matchKey && (e.network === net || !e.network))
+ .sort((x, y) => (x.at || 0) - (y.at || 0))
+ .map((e) => ({ mint: e.mint, name: e.name, symbol: e.symbol, image: e.image, since: e.at }));
+}
+
+// --- internals --------------------------------------------------------------
+
+function liveOnly(queue, now) {
+ return (Array.isArray(queue) ? queue : []).filter((e) => entryLive(e, now));
+}
+
+function isMint(v) {
+ return typeof v === 'string' && MINT_RE.test(v);
+}
+
+function cleanNetwork(v) {
+ const s = String(v || 'mainnet').toLowerCase();
+ return /^[a-z]{1,12}$/.test(s) ? s : 'mainnet';
+}
+
+function slotTag(slot) {
+ const n = Number(slot);
+ if (Number.isFinite(n) && n > 0) return Math.floor(n).toString(36).slice(-10);
+ const s = String(slot || '').toLowerCase();
+ return /^[a-z0-9]{1,16}$/.test(s) ? s : null;
+}
+
+function str(v, max) {
+ return typeof v === 'string' ? v.replace(/[\u0000-\u001f]/g, '').trim().slice(0, max) : '';
+}