Files
ark-stronghold-protocol/server/match/player/economy.js
T
sganggs 464a823bc5 feedback5 CB5: a 调度中心 upgrade opens the new level's extra slot at once with a new card, the cards shown stay in place (community report of 2026-10-06, item 19; the official tutorial's 休整期 page: 「升级后将出现更多的商品栏位」)
Until 0.2.0 the shop kept its old slot count until the next roll (a refresh or the round start), so a
level-up 1→2 (险境: 3→4 operator slots) or 3→4 (4→5; 标准: 5→6) showed no new card.
PlayerState._openLevelSlots (player/economy.js): the extra slots of the new level are appended to the
chess slots (the item slot stays after them), each drawn at the new level; [ASSUMED] the new card follows
the freeze toggle as a manual refresh's cards do. audit.js checks the slot count and that every card
shown before stays in place. Docs: META.md §3 / §7, PLAYING.md.

Golden: matches — all 18 scenarios moved (solo-FUNNY-1/2, solo-NORMAL-1/2, solo-HARD-1/2, solo-ABYSS-1/2,
coop2-NORMAL-3, coop2-HARD-4, coop3-ABYSS-5, coop4-FUNNY-6, coop4-ABYSS-7, coop2-NORMAL-8-serverrun,
solo-HARD-9-boosted, coop2-ABYSS-10-boosted, coop2-NORMAL-14-standins, coop2-NORMAL-70-diy): every bot
levels its shop and now sees (and draws the shop RNG for) the new card at once, so purchases and boards
part from there. roster / bonds / fields / standins / diy unchanged; no engine error stored.
Tests: test/match/feedback5-shop-levelslots.test.js (new).
2026-10-07 01:44:35 +08:00

232 lines
11 KiB
JavaScript

// server/match/player/economy.js — PlayerState methods: the economy — funds, spending (onSpend), prep-side bond layer
// gains (clamped by layerGainRoom), the shop (onPrice prices, copy-weighted rolls from the shared pool plus the player's
// own 自选 stock — player/diy.js —, freeze) and its intents: g.buy, g.refresh, g.freeze, g.levelUp, g.sell.
// Installed on PlayerState.prototype by server/match/PlayerState.js (a method container: never instantiated; `this` is
// the player state).
import { ERR, layerGainRoom } from '../../../shared/constants.js';
import { freeSlot } from '../board.js';
import { OK, fail } from './common.js';
export class PlayerEconomy {
addFunds(n, { reason = '' } = {}) {
if (!Number.isFinite(n) || n === 0) return 0;
const v = Math.trunc(n);
const before = this.funds;
this.funds = Math.max(0, this.funds + v);
if (v > 0) this.stats.fundsGained += v;
this.dirty();
return this.funds - before;
}
spend(n) {
const v = Number.isFinite(n) ? Math.max(0, Math.trunc(n)) : 0;
if (v > this.funds) return false;
this.funds -= v;
this.stats.gold += v;
this.round.spent += v;
this.dirty();
return true;
}
/** onSpend, dispatched once a payment's action is complete (buy / refresh / levelUp / reward / effect). */
_afterSpend(amount, reason) {
if (!(amount > 0)) return;
this.m.dispatch(this, 'onSpend', { amount, reason, total: this.stats.gold });
}
/**
* Bond layer gain (prep-side). `requireActive` = "使已激活的【X】层数+N". Returns layers added: at most the room left
* under BOND_LAYER_CAP (999, shared/constants.js) — a gain at the cap adds 0 and dispatches nothing.
*/
addLayers(bondId, n, { requireActive = false, reason = '' } = {}) {
if (!this.gd.bond(bondId) || !Number.isFinite(n) || n <= 0) return 0;
if (requireActive && !(this.bonds[bondId] && this.bonds[bondId].active)) return 0;
const before = this.layers[bondId] || 0;
const add = layerGainRoom(before, Math.floor(n));
if (add <= 0) return 0;
this.layers[bondId] = before + add;
this.recompute();
this.m.dispatch(this, 'onLayers', { bondId, from: before, to: before + add, reason });
return add;
}
/** Effective price of a shop slot after onPrice modifiers (never negative). */
priceOf(slot) {
if (!slot) return 0;
const ev = { slot, kind: slot.kind, id: slot.id, price: slot.basePrice };
this.m.dispatch(this, 'onPrice', ev, { quiet: true });
const p = Number(ev.price);
return Number.isFinite(p) ? Math.max(0, Math.round(p)) : slot.basePrice;
}
_rollChessSlot() {
// the slotted 自选 pieces join the draw once the 调度中心 reaches their slot's level (player/diy.js diyRollEntries)
const id = this.m.pool.roll(this.m.rngShop, { maxTier: this.shop.level, extra: this.diyRollEntries() });
return id ? { kind: 'chess', id, basePrice: this.gd.chessPrice(id), frozen: false, sold: false } : null;
}
_rollItemSlot() {
const id = this.m.pool.rollItem(this.m.rngShop, this.shop.level);
return id ? { kind: 'item', id, basePrice: this.gd.itemPrice(id), frozen: false, sold: false } : null;
}
/**
* Reroll the shop. keepFrozen → frozen unsold slots survive (round start); otherwise everything is rerolled
* (manual refresh). Slot counts follow the current level.
*/
rollShop({ keepFrozen = false } = {}) {
const { chess: nChess, item: nItem } = this.gd.shopSlots(this.shop.level);
const old = this.shop.slots;
const layout = this.shop.layout || { chess: old.length, item: 0 };
const oldChess = old.slice(0, layout.chess);
const oldItems = old.slice(layout.chess);
const keep = (s, kind) => (keepFrozen && s && s.kind === kind && !s.sold && s.frozen ? { ...s } : null);
// frozen slots keep their position; sold / empty / unfrozen positions are rerolled
const slots = [];
for (let i = 0; i < nChess; i++) slots.push(keep(oldChess[i], 'chess') ?? this._rollChessSlot());
for (let i = 0; i < nItem; i++) slots.push(keep(oldItems[i], 'item') ?? this._rollItemSlot());
this.shop.slots = slots;
this.shop.layout = { chess: nChess, item: nItem };
for (const s of slots) if (s) s.frozen = this.shop.frozen;
this.dirty();
}
/**
* The 调度中心's upgrade opens the new level's extra slots at once, each with a new card drawn at the new level; the
* cards already shown stay where they are (chess slots keep their index, the item slot stays after them). Official: the
* tutorial's 休整期 page 「升级:…升级后将出现更多的商品栏位、可调度干员以及新装备」; the community report of 2026-10-06
* (item 19) 「升级商店获得新的商店位时用新卡补上,而不是空着」. Until 0.2.0 the extra slots waited for the next roll (a
* refresh or the round start). [ASSUMED] the new card follows the freeze toggle, as a manual refresh's cards do.
*/
_openLevelSlots() {
const { chess: nChess, item: nItem } = this.gd.shopSlots(this.shop.level);
const old = this.shop.slots;
const layout = this.shop.layout || { chess: old.length, item: 0 };
if (nChess <= layout.chess && nItem <= layout.item) return;
const chess = old.slice(0, layout.chess);
const items = old.slice(layout.chess);
const fresh = (s) => { if (s) s.frozen = this.shop.frozen; return s; };
while (chess.length < nChess) chess.push(fresh(this._rollChessSlot()));
while (items.length < nItem) items.push(fresh(this._rollItemSlot()));
this.shop.slots = [...chess, ...items];
this.shop.layout = { chess: chess.length, item: items.length };
}
/** Combat start: unfrozen slots are emptied (research 01 A1). */
clearUnfrozenShop() {
this.shop.slots = this.shop.slots.map((s) => (s && s.frozen && !s.sold ? s : null));
this.dirty();
}
/**
* g.buy {slot}. A full regular hand refuses every purchase, also one whose copy would complete a merge at once (PRTS
* 卫戍协议/帮助 §手牌区 "当常规手牌区全满无空位时,玩家将无法执行使手牌溢出的操作" — "例如招募/购入等通常情况下会增加手牌的
* 操作"; BWIKI 盟约 "当整备区达到上限后将无法进行涉及到整备区的操作(购买、撤回等)"; GitHub #82: the reporter's first-hand
* check in the official game, "哪怕是已经有了两个相同干员情况"). Until 0.1.3 such a purchase was let through.
*/
buy(slotIdx) {
const g = this._gate(); if (g) return g;
if (!Number.isInteger(slotIdx) || slotIdx < 0 || slotIdx >= this.shop.slots.length) return fail(ERR.BAD_TARGET);
const slot = this.shop.slots[slotIdx];
if (!slot) return fail(ERR.BAD_TARGET);
if (slot.sold) return fail(ERR.SOLD_OUT);
const price = this.priceOf(slot);
if (this.funds < price) return fail(ERR.NO_FUNDS);
const handFull = freeSlot(this.hand) < 0;
let piece;
if (slot.kind === 'chess') {
const rec = this.gd.chess(slot.id);
// a DIY slot sells only as this player's own 自选 piece (a slotted slot's record carries `diyFor`)
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);
if (pool.has(base) && pool.left(base) < need) return fail(ERR.SOLD_OUT);
if (handFull) return fail(ERR.HAND_FULL);
this.spend(price);
slot.sold = true;
piece = this.acquireChess(slot.id, { source: 'buy' });
} else {
if (!this.gd.item(slot.id)) return fail(ERR.BAD_TARGET);
if (handFull) return fail(ERR.HAND_FULL);
this.spend(price);
slot.sold = true;
piece = this.acquireItem(slot.id, { source: 'buy' });
}
this.stats.buys++;
this.round.buys++;
this.m.dispatch(this, 'onBuy', { piece, slot, price, kind: slot.kind });
this._afterSpend(price, 'buy');
this.recompute();
return OK;
}
refresh() {
const g = this._gate(); if (g) return g;
const free = this.shop.freeRefreshes > 0;
const price = free ? 0 : this.gd.refreshPrice;
if (!free && this.funds < price) return fail(ERR.NO_FUNDS);
if (free) this.shop.freeRefreshes--;
else this.spend(price);
this.rollShop({ keepFrozen: false });
this.stats.refreshes++;
this.round.refreshes++;
this.m.dispatch(this, 'onRefresh', { slots: this.shop.slots, free, price });
this._afterSpend(price, 'refresh');
this.dirty();
return OK;
}
freeze() {
const g = this._gate(); if (g) return g;
this.shop.frozen = !this.shop.frozen;
for (const s of this.shop.slots) if (s && !s.sold) s.frozen = this.shop.frozen;
this.dirty();
return OK;
}
levelUp() {
const g = this._gate(); if (g) return g;
if (this.shop.level >= this.gd.maxShopLevel) return fail(ERR.MAX_LEVEL);
const price = Math.max(0, this.shop.upgradePrice);
if (this.funds < price) return fail(ERR.NO_FUNDS);
this.spend(price);
this.shop.level++;
this.shop.upgradePrice = this.gd.upgradeBase(this.shop.level) ?? 0;
this._openLevelSlots();
this.m.tickerFor('SHOP_LEVEL', [this.name, String(this.shop.level)], { playerId: this.playerId, param: String(this.shop.level) });
this.m.dispatch(this, 'onLevelUp', { level: this.shop.level, price });
this._afterSpend(price, 'levelUp');
this.dirty();
return OK;
}
sell(uid) {
const g = this._gate(); if (g) return g;
const loc = this.find(uid);
if (!loc) return fail(ERR.BAD_TARGET);
if (loc.piece.kind !== 'chess') return fail(ERR.BAD_TARGET, loc.piece.kind === 'item' ? 'items cannot be sold' : 'tokens cannot be sold');
const piece = loc.piece;
// its equipment returns to the hand (overflow temp): refuse rather than destroy it when there is no room
const room = this.hand.filter((x) => x == null).length + this.temp.filter((x) => x == null).length + (loc.area === 'hand' || loc.area === 'temp' ? 1 : 0);
if ((piece.items || []).length > room) return fail(ERR.HAND_FULL, 'no room for the equipment');
this._detach(loc);
this.removeTokensOf(piece.uid);
for (const it of piece.items || []) {
if (!this.stow(it, { allowTemp: true })) this.m.log.warn?.(`[match ${this.m.roomCode}] ${this.playerId}: item ${it.id} lost on sell (no space)`);
}
piece.items = [];
this.returnCopies(piece);
const ev = { piece, gain: this.gd.sellPrice(piece.id) };
this.m.dispatch(this, 'onSold', ev);
const gain = Number.isFinite(ev.gain) ? Math.max(0, Math.trunc(ev.gain)) : 1;
this.addFunds(gain, { reason: 'sell' });
this.stats.sells++;
this.round.sells++;
this.checkItemMerges();
this.recompute();
return OK;
}
}