forked from WikiDeck/wikideck
Modes: claim lobby before await in join; trend warm-up 20 s, configurable (review on #22)
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -57,6 +57,7 @@ Configure modes with environment variables on your node:
|
||||
| `WIKIDECK_GAME_OPERATOR` | your origin's host | The name shown as **"Hosted by …"**. |
|
||||
| `WIKIDECK_GAME_NOTE` | unset | A short note shown next to your name, e.g. "EU, best effort". |
|
||||
| `WIKIDECK_GAME_PEERS` | `WIKIDECK_PEERS` | Peer origins whose modes to list. Use the peers' **public** origins, because players open the peer's own site to play there. |
|
||||
| `WIKIDECK_GAME_TREND_INTERVAL_MS` | `20000` | Background trend warm-up pace for `trending` modes (minimum 15000). |
|
||||
| `PUBLIC_ORIGIN` | `http://127.0.0.1:$PORT` | Your node's identity as a host. |
|
||||
|
||||
At most 16 modes per node. A bad mode file stops the node at startup with a readable error,
|
||||
@@ -143,7 +144,7 @@ for how they could become rewards without trusting the host's word.
|
||||
| Match length | engine bounds (`turns` ≤ 12, turns × turnSeconds ≤ 240 s) plus a hard 2,000-action cap |
|
||||
| Rate limits per IP | 240/min reads and plays, 60/min lobby create/join/resign, 30/min peer-discovery routes (on top of the node-wide 300/min `/api` limit and the RPC admission budget) |
|
||||
| Request body | the node's 68 KB JSON limit |
|
||||
| Wikipedia lookups (`trending`) | background only: one page per 3 s, at most 256 queued, never per request |
|
||||
| Wikipedia lookups (`trending`) | background only: one page per 20 s by default (`WIKIDECK_GAME_TREND_INTERVAL_MS`, minimum 15 s), at most 256 queued, never per request |
|
||||
|
||||
### Mode validation
|
||||
|
||||
@@ -247,7 +248,7 @@ be pinned in consensus.
|
||||
for the lobby (and then the engine's `cardFromStats`). That's a follow-up once accounts and
|
||||
wallet linking (#7) settle.
|
||||
- `trending` never calls Wikipedia when a match starts. The node warms a cache-only trend
|
||||
store in the background, one page every 3 s, through its existing enrichment cache (6 h TTL),
|
||||
store in the background, one page every 20 s (configurable, never faster than 15 s), through its existing enrichment cache (6 h TTL),
|
||||
starting with the preview-card pages. A card whose page isn't warm yet gets no bonus. This
|
||||
keeps load on the shared, rate-limited Wikipedia egress tiny: about 24 pages per 6 hours per node.
|
||||
keeps load on the shared, rate-limited Wikipedia egress tiny: a few dozen pages per 6 hours per node.
|
||||
- Spectating and rematches.
|
||||
|
||||
+5
-3
@@ -19,7 +19,7 @@ export class GameError extends Error {
|
||||
}
|
||||
|
||||
type Player = { id: string; name: string; tokenHash: Buffer; seen: number };
|
||||
type Phase = 'waiting' | 'drafting' | 'playing' | 'finished' | 'expired';
|
||||
type Phase = 'waiting' | 'starting' | 'drafting' | 'playing' | 'finished' | 'expired';
|
||||
type Lobby = {
|
||||
id: string; mode: ModeDocument; rules: unknown; seed: string; createdAt: number;
|
||||
players: Player[]; phase: Phase;
|
||||
@@ -92,8 +92,8 @@ export class ModeHost {
|
||||
for (const l of this.lobbies.values()) {
|
||||
if (l.mode.id !== modeId) continue;
|
||||
if (l.phase === 'waiting') open++;
|
||||
if (l.phase === 'drafting' || l.phase === 'playing') live++;
|
||||
if (l.phase === 'waiting' || l.phase === 'drafting' || l.phase === 'playing') online += l.players.length;
|
||||
if (l.phase === 'starting' || l.phase === 'drafting' || l.phase === 'playing') live++;
|
||||
if (l.phase !== 'finished' && l.phase !== 'expired') online += l.players.length;
|
||||
}
|
||||
return { playersOnline: online, openLobbies: open, liveMatches: live,
|
||||
matchesPlayed: this.store.modeMatches(modeId), distinctPlayers: this.store.modePlayers(modeId) };
|
||||
@@ -138,6 +138,8 @@ export class ModeHost {
|
||||
if (l.players[0]!.id === p.player.id) throw new GameError(409, 'You cannot play against yourself.');
|
||||
l.players.push(p.player);
|
||||
l.lastMove = this.now();
|
||||
// Claim the lobby before any await, so a concurrent join can't also pass the 'waiting' check.
|
||||
l.phase = 'starting';
|
||||
if (l.mode.decks.kind === 'draft') this.startDraft(l);
|
||||
else await this.startMatch(l, [this.engine.starterDeck(l.rules, `${l.seed}:0`), this.engine.starterDeck(l.rules, `${l.seed}:1`)]);
|
||||
return { lobbyId: l.id, seat: 1, token: p.token };
|
||||
|
||||
@@ -23,7 +23,10 @@ export async function gameRouter(options: { dataDir: string | null; enrichment?:
|
||||
let trends: TrendCache | undefined;
|
||||
if (options.enrichment && modes.some(m => m.cards.preset === 'trending')) {
|
||||
const enrichment = options.enrichment;
|
||||
trends = new TrendCache(pageId => enrichment.get(pageId));
|
||||
// Wikipedia egress is shared and rate-limited: default one page per 20 s (≥ 15 s enforced).
|
||||
const interval = Number(env.WIKIDECK_GAME_TREND_INTERVAL_MS ?? 20_000);
|
||||
if (!Number.isSafeInteger(interval) || interval < 15_000 || interval > 3_600_000) throw new Error('WIKIDECK_GAME_TREND_INTERVAL_MS must be 15000..3600000');
|
||||
trends = new TrendCache(pageId => enrichment.get(pageId), { intervalMs: interval });
|
||||
for (const card of engine.starterDeck(engine.defaultRuleset, 'trend-warmup')) {
|
||||
const id = engine.cardPageId(card); if (id !== null) trends.enqueue(id);
|
||||
}
|
||||
|
||||
@@ -36,7 +36,7 @@ export class TrendCache {
|
||||
|
||||
start(): void {
|
||||
if (this.timer) return;
|
||||
this.timer = setInterval(() => void this.step(), this.options.intervalMs ?? 3_000);
|
||||
this.timer = setInterval(() => void this.step(), this.options.intervalMs ?? 20_000);
|
||||
this.timer.unref();
|
||||
}
|
||||
stop(): void { if (this.timer) clearInterval(this.timer); this.timer = undefined; }
|
||||
|
||||
@@ -184,3 +184,13 @@ test('modes trends: cache-only at match time, warmed one page at a time', async
|
||||
now = 5000; t.get(1); await t.step(); await t.step();
|
||||
assert.deepEqual(calls, [1, 2, 2, 1], 'stale entries are refreshed in the background');
|
||||
});
|
||||
|
||||
test('modes host: two concurrent joins cannot both take the second seat', async () => {
|
||||
const slow = { ...stubEngine };
|
||||
const host = new ModeHost({ identity, modes: [mode({ cards: { preset: 'trending' } })], engine: slow, store: new MatchStore(null),
|
||||
liveData: async () => { await new Promise(r => setTimeout(r, 20)); return { trendPercent: 0 }; } });
|
||||
const a = host.createLobby('stub', alice);
|
||||
const results = await Promise.allSettled([host.join(a.lobbyId, bob), host.join(a.lobbyId, { name: 'Eve', secret: 'eve-secret-0123456789abcdefgh' })]);
|
||||
assert.equal(results.filter(r => r.status === 'fulfilled').length, 1);
|
||||
assert.equal((host.view(a.lobbyId) as any).players.length, 2);
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user