Seen: the Art 教鞭 (杜宾's strategy) drew three 战术特训 cards and added a random one — a documented simplification (docs/META.md §2.5: a personal choice would need its own phase, protocol message, timer and AI takeover). Official: act2autochess choice_event hunter_band_1 is a PERSONAL_CHOOSE; PRTS 下半 记录 §法术 教鞭 "使用后销毁,于3个战术特训的悬赏任务中选择一项". The owner decided on 2026-10-08 to follow it. Taken from PR #353 (code and tests by @Sukvii), ported to the split 0.2.2 modules. Now: ctx.offerBountyChoice -> Match.offerBountyChoice keeps up to three different mode-eligible cards (15 of 20 in 标准 / 险境, 20 in 绝境 / 终极; one meta-rng shuffle) in PlayerState.personalChoice, m.private only (same cards after a reconnect); g.choice { idx, choiceId } confirms it (Match.pickPersonalChoice: owner, id, round, index; a request without a choiceId is still the public draft, a stale id never falls through to it). No new phase or timer: Ready is refused while it is open, the prep's deadline picks at random before the temp pieces and Ready, a bot or a 托管 seat picks with the bounty scorer (botPickCard, the bots' seeded rng; at the prep's start, after the cast, before Ready; the scheduler's fallback picks at random if a step fails). The client reuses ChoiceOverlay (personal: no order, no turns, the prep's countdown) over a locked prep, the Ready reasons, the mock harness; six UI strings in en / ja / ko / zh-TW. An eliminated player's offer goes with it; invariants.js flags an offer outside its owner's own open prep. Not taken from the PR: its §25.24 in the released docs/history/0.2.0.md (the 0.2.2 section is the integrator's), its edits of the Ready toggle beyond the personal-choice case, its tail placement of the i18n keys. Golden: coop2-NORMAL-8-serverrun (ai_0 杜宾 casts 教鞭 from R2 on and now scores the three cards: teamLp 39 -> 34, virtualMs 121400 -> 118400, rng shop 511 -> 483 / bots 18063 -> 18070 / meta 112 -> 103) and coop2-ABYSS-10-boosted (ai_1 杜宾: virtualMs 121650 -> 124650, shop 590 -> 589 / bots 22075 -> 22096 / meta 141 -> 154); both first differ in round 2, every other scenario and family is unchanged; node tools/golden.mjs --twice is identical. Tests: items.test.js (three distinct cards, the tapped one applied once, nothing before the confirm, a refused cast keeps the Art / count / rng / uid, an eliminated player's offer), economy.test.js (Ready refused, the deadline picks before endPrep, solo prep untimed), connection.test.js (two players' offers, reconnect, foreign / duplicate / old ids, an offline player's pick at the deadline), bot.test.js (托管 takeover incl. the failing-step fallback), bot-smarter.test.js (the scorer's pick, deterministic per seed, no rng draw when nothing is pending), followups.test.js (R14 / R15), draft.test.js, unitStats.test.js (protocol), hardening.test.js (invariant), gameLogic / playtest3 / playtest4 (view model, Ready reason, ChoiceView, actions), mock.e2e.test.js and playtest2.real.e2e.test.js (browser, SP_E2E=1).
93 lines
4.1 KiB
JavaScript
93 lines
4.1 KiB
JavaScript
// server/match/player/prep.js — PlayerState methods: the gate of every prep intent (_gate: alive, PREP, not ready),
|
|
// g.reward (a reward offer's pick), Ready (refused while temp holds pieces) and the resolution of the temp pieces due
|
|
// at a prep's deadline.
|
|
// Installed on PlayerState.prototype by server/match/PlayerState.js (a method container: never instantiated; `this` is
|
|
// the player state).
|
|
|
|
import { ERR, PHASE } from '../../../shared/constants.js';
|
|
import { freeSlot } from '../board.js';
|
|
import { OK, fail } from './common.js';
|
|
|
|
export class PlayerPrep {
|
|
_gate({ allowWhenReady = false } = {}) {
|
|
if (!this.alive) return fail(ERR.ELIMINATED);
|
|
if (this.m.phase !== PHASE.PREP) return fail(ERR.WRONG_PHASE);
|
|
if (this.ready && !allowWhenReady) return fail(ERR.WRONG_PHASE, 'ready');
|
|
return null;
|
|
}
|
|
|
|
/**
|
|
* g.reward {idx}: the pick of the first queued offer (promotion reward, a strategy's special refresh, an item offer).
|
|
* A pick is a 招募 like a purchase: a full regular hand refuses it, also when it would complete a merge (PRTS
|
|
* 卫戍协议/帮助 §手牌区, see buy; GitHub #82).
|
|
*/
|
|
pickReward(idx) {
|
|
const g = this._gate(); if (g) return g;
|
|
const offer = this.offers[0];
|
|
if (!offer) return fail(ERR.BAD_TARGET, 'no reward');
|
|
if (!Number.isInteger(idx) || idx < 0 || idx >= offer.slots.length) return fail(ERR.BAD_TARGET);
|
|
const slot = offer.slots[idx];
|
|
if (!slot || slot.sold) return fail(ERR.SOLD_OUT);
|
|
const handFull = freeSlot(this.hand) < 0;
|
|
if (slot.kind === 'item') {
|
|
if (!this.gd.item(slot.id)) return fail(ERR.BAD_TARGET);
|
|
if (handFull) return fail(ERR.HAND_FULL);
|
|
} else {
|
|
const rec = this.gd.chess(slot.id);
|
|
if (!rec || (rec.isDiy && !rec.diyFor)) return fail(ERR.BAD_TARGET);
|
|
const base = this.gd.baseIdOf(slot.id);
|
|
const need = rec.isGolden ? this.gd.goldenCopies : 1;
|
|
const pool = this.poolOf(base); // the shared pool, or this player's 自选 stock
|
|
if (pool.has(base) && pool.left(base) < need) return fail(ERR.SOLD_OUT);
|
|
if (handFull) return fail(ERR.HAND_FULL);
|
|
}
|
|
const price = Number.isFinite(slot.price) && slot.price > 0 ? Math.trunc(slot.price) : 0;
|
|
if (price > this.funds) return fail(ERR.NO_FUNDS);
|
|
slot.sold = true;
|
|
this.offers.shift();
|
|
if (price > 0) this.spend(price);
|
|
if (slot.kind === 'item') this.acquireItem(slot.id, { source: 'reward' });
|
|
else this.acquireChess(slot.id, { source: 'reward' });
|
|
this._afterSpend(price, 'reward');
|
|
this.recompute();
|
|
return OK;
|
|
}
|
|
|
|
setReady(on) {
|
|
if (!this.alive) return fail(ERR.ELIMINATED);
|
|
if (this.m.phase !== PHASE.PREP) return fail(ERR.WRONG_PHASE);
|
|
if (on && this.personalChoice) return fail(ERR.BAD_TARGET, '请先完成教鞭选择'); // i18n-ignore: developer error detail
|
|
if (on && !this.tempEmpty) return fail(ERR.TEMP_NOT_EMPTY);
|
|
if (this.ready === !!on) return OK;
|
|
this.ready = !!on;
|
|
// un-ready: the player can act again, so what overflowed while it was ready is due at this prep's deadline
|
|
if (!on) for (const p of this.temp) if (p && this.tempDue(p) > this.prepsEnded) this._tempDue.set(p.uid, this.prepsEnded);
|
|
this.dirty();
|
|
this.m.onReadyChanged(this);
|
|
return OK;
|
|
}
|
|
|
|
/**
|
|
* Prep deadline: every temp piece due at this prep (tempDue ≤ prepsEnded) is resolved — a chess is sold back (its
|
|
* pool copies return, its summons are removed), items are destroyed, the summon stack of an owner still on the board
|
|
* is removed and comes back at the next round start (startRound → grantTokensFor; PRTS "干员所属召唤物会于下一回合
|
|
* 返还"). Pieces that overflowed after the player could no longer act on them (after Ready, at the prep end) are kept
|
|
* for the next prep.
|
|
*/
|
|
resolveTemp() {
|
|
let changed = false;
|
|
for (let i = 0; i < this.temp.length; i++) {
|
|
const p = this.temp[i];
|
|
if (!p || this.tempDue(p) > this.prepsEnded) continue;
|
|
this.temp[i] = null;
|
|
this._tempDue.delete(p.uid);
|
|
changed = true;
|
|
if (p.kind === 'chess') {
|
|
this.removeTokensOf(p.uid);
|
|
this.returnCopies(p);
|
|
}
|
|
}
|
|
if (changed) this.recompute();
|
|
}
|
|
}
|