forked from WikiDeck/wikideck
host-pvp: per-seat time bank (bankSeconds), clock in the seat view; tested on engine-wip
This commit is contained in:
@@ -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,
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
@@ -32,6 +32,8 @@ export interface EnginePort<Rules = unknown, Card = unknown, State = unknown, Ac
|
||||
timeoutAction?(seat: Seat): Action;
|
||||
/** Optional per-turn time limit from the ruleset, in seconds. */
|
||||
turnSeconds?(rules: Rules): number;
|
||||
/** Optional per-seat time bank (seconds), spent once a turn runs past `turnSeconds`. */
|
||||
bankSeconds?(rules: Rules): number;
|
||||
/** Optional current turn number, so the host can time turns rather than actions. */
|
||||
turnOf?(state: State): number;
|
||||
/** Optional: freeze live data onto a card at match start (e.g. a trend arrow). */
|
||||
|
||||
+19
-4
@@ -47,6 +47,8 @@ type Lobby = {
|
||||
draft?: { pool: unknown[]; taken: (Seat | null)[]; order: Seat[]; turn: number; deadline: number };
|
||||
decks?: [unknown[], unknown[]];
|
||||
state?: unknown; actions: { seat: Seat; action: unknown }[]; startedAt?: number; lastMove: number; turnStartedAt?: number; turn?: number;
|
||||
/** Per-seat time bank in ms (engines with `bankSeconds`): spent after the turn timer runs out. */
|
||||
bank?: [number, number];
|
||||
outcome?: { winner: Seat | null; reason: string }; matchId?: string;
|
||||
};
|
||||
type HostedMode = { mode: ModeDocument; rules: unknown; engine: AnyEngine };
|
||||
@@ -280,7 +282,8 @@ export class ModeHost {
|
||||
toPick: d.order[d.turn] ?? null, deadline: d.deadline } };
|
||||
}
|
||||
if ((l.phase === 'playing' || l.phase === 'finished') && l.state !== undefined && seat !== null) {
|
||||
return { ...base, state: engine.viewFor(l.state, seat),
|
||||
return { ...base, clock: l.phase === 'playing' ? { turnStartedAt: l.turnStartedAt ?? null, turnMs: this.turnMs(l), bankMs: l.bank ?? null } : null,
|
||||
state: engine.viewFor(l.state, seat),
|
||||
legalActions: l.phase === 'playing' ? engine.legalActions(l.state, seat) : [] };
|
||||
}
|
||||
return base;
|
||||
@@ -388,9 +391,12 @@ export class ModeHost {
|
||||
if (idx >= 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');
|
||||
}
|
||||
|
||||
@@ -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<any, StubCard, any, any> = { ...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);
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user