diff --git a/server/game/duel-binding.ts b/server/game/duel-binding.ts index 07c4348..13993d4 100644 --- a/server/game/duel-binding.ts +++ b/server/game/duel-binding.ts @@ -37,6 +37,8 @@ export function duelBinding(port: AnyPort, sources: DuelCardSources = {}): AnyPo ...port, fromChain: (_rules, card) => toDuelCard(card, sources), copyLimit: rules => (rules as { maxCopiesPerPage?: number }).maxCopiesPerPage ?? 4, + // BRIEF "Clock": 60 s per turn plus a 5-minute bank, both ruleset data. + bankSeconds: rules => (rules as { bankSeconds?: number }).bankSeconds ?? 0, }; } diff --git a/server/game/engine-port.ts b/server/game/engine-port.ts index 8d0cd53..53c7a06 100644 --- a/server/game/engine-port.ts +++ b/server/game/engine-port.ts @@ -32,6 +32,8 @@ export interface EnginePort= 0) void this.takePick(l, l.draft.order[l.draft.turn]!, idx).catch(() => { l.phase = 'expired'; }); } else if (l.phase === 'playing' && l.state !== undefined && now - (l.turnStartedAt ?? l.lastMove) > this.turnMs(l)) { const waiting = ([0, 1] as Seat[]).filter(s => engine.legalActions(l.state, s).length > 0); - if (engine.timeoutAction) { + // Past the turn timer, a lone waiting seat spends its bank before it times out. + const bank = waiting.length === 1 && l.bank ? l.bank[waiting[0]!] : 0; + if (now - (l.turnStartedAt ?? l.lastMove) <= this.turnMs(l) + bank) { /* still on the bank */ } + else if (engine.timeoutAction) { // The engine decides what a timeout means (auto-commit, AFK forfeit); it is logged and replayable. - for (const s of waiting) if (l.phase === 'playing') this.apply(l, s, engine.timeoutAction(s)); + for (const s of waiting) if (l.phase === 'playing') { if (l.bank) l.bank[s] = 0; this.apply(l, s, engine.timeoutAction(s)); } l.turnStartedAt = now; } else if (waiting.length === 1) this.finish(l, waiting[0] === 0 ? 1 : 0, 'timeout'); else this.finish(l, null, 'abandoned'); @@ -437,6 +443,8 @@ export class ModeHost { l.state = engine.createMatch(l.rules, mapped, l.seed); l.phase = 'playing'; l.startedAt = l.lastMove = l.turnStartedAt = this.now(); + const bankSeconds = engine.bankSeconds?.(l.rules); + if (bankSeconds !== undefined && bankSeconds > 0) l.bank = [bankSeconds * 1000, bankSeconds * 1000]; l.turn = engine.turnOf?.(l.state); delete l.draft; this.bump(l); @@ -471,7 +479,14 @@ export class ModeHost { l.lastMove = this.now(); this.bump(l); const turn = engine.turnOf?.(l.state); - if (turn !== l.turn) { l.turn = turn; l.turnStartedAt = l.lastMove; } + if (turn !== l.turn) { + // The seat that ended the turn pays any time over the turn timer from its bank. + if (l.bank && l.turnStartedAt !== undefined) { + const over = l.lastMove - l.turnStartedAt - this.turnMs(l); + if (over > 0) l.bank[seat] = Math.max(0, l.bank[seat] - over); + } + l.turn = turn; l.turnStartedAt = l.lastMove; + } if (engine.isOver(l.state)) { const r = engine.result(l.state); this.finish(l, r.winner, reason ?? r.reason ?? 'complete'); } else if (l.actions.length >= 2000) this.finish(l, null, 'action-limit'); } diff --git a/tests/host-pvp-host.test.ts b/tests/host-pvp-host.test.ts index 795648a..1d5b957 100644 --- a/tests/host-pvp-host.test.ts +++ b/tests/host-pvp-host.test.ts @@ -342,3 +342,28 @@ test('host-pvp: seeded shuffle is deterministic and a permutation', () => { assert.notDeepEqual(seededShuffle(xs, 's'), seededShuffle(xs, 't')); assert.deepEqual([...seededShuffle(xs, 's')].sort((a, b) => a - b), xs); }); + +test('host-pvp: a slow seat spends its time bank before it times out', async () => { + let now = 1_000_000; + // Stub turns are simultaneous; a 10 s turn plus a 30 s bank, timeouts from the engine. + const timed: EnginePort = { ...duelStub, turnSeconds: () => 10, bankSeconds: () => 30, turnOf: (s: any) => s.turn, + timeoutAction: () => ({ type: 'play', card: 0 }) }; + const own = fakeOwnership(); + const host = new ModeHost({ identity, modes: [duelMode()], engines: { duel: timed }, ownership: own.port, + store: new MatchStore(null), now: () => now }); + const a = await host.createLobby('duel-classic', { idToken: 'id-alice', deck: deckOf('alice') }); + const b = await host.join(a.lobbyId, { idToken: 'id-bob', deck: deckOf('bob') }); + await host.act(a.lobbyId, a.token, (host.view(a.lobbyId, a.token) as any).legalActions[0]); // alice moves, bob stalls + const clock = (host.view(a.lobbyId, b.token) as any).clock; + assert.deepEqual(clock.bankMs, [30_000, 30_000]); + assert.equal(clock.turnMs, 15_000, 'turn timer includes 5 s network grace'); + now += 15_000 + 20_000; // past the turn timer, inside bob's bank + host.sweep(); + assert.equal((host.view(a.lobbyId, b.token) as any).state.turn, 0, 'bob is still on his bank'); + now += 11_000; // bank exhausted + host.sweep(); + const v = host.view(a.lobbyId, b.token) as any; + assert.equal(v.state.turn, 1, 'timeout applied'); + assert.equal(v.clock.bankMs[1], 0); + assert.equal(v.clock.bankMs[0], 30_000); +});