Files
noname-shuying/mengsan/cards/temporary-strength.js
T
Diuse 9e1732d2f8 fixed # 补全部分卡牌
fixed # 修复部分已知BUG
2026-10-04 19:32:38 +08:00

76 lines
3.6 KiB
JavaScript

// Temporary combat-only strength. Never alter permanent strength or the run/deck.
const records = new WeakMap();
const SKILL = "mengsan_setup_strength_shuying";
export function forgetTemporaryStrength(player) {
const existed = records.delete(player);
delete player.storage.mengsanSetupStrength_shuying;
return existed;
}
export function clearTemporaryStrength(player) {
if (!records.has(player)) return false;
forgetTemporaryStrength(player);
player.removeSkill(SKILL);
return true;
}
export function temporaryStrengthAmount(player, battle) {
const entry = records.get(player);
return entry?.battle === battle && battle?.session.active ? entry.amount : 0;
}
export function temporaryStrengthExpires(player, phase) {
const entry = records.get(player);
return Boolean(entry && phase.name === "phase" && entry.segments.some(part=>
part.ownTurn ? phase.player===player : !part.turn || part.turn===phase));
}
export function expireTemporaryStrength(player,phase) {
const entry=records.get(player);if(!entry || phase.name!=="phase")return false;
const remaining=entry.segments.filter(part=>part.ownTurn ? phase.player!==player : part.turn && part.turn!==phase);
if(remaining.length===entry.segments.length)return false;
if(!remaining.length)return clearTemporaryStrength(player);
entry.segments=remaining;entry.amount=remaining.reduce((n,part)=>n+part.amount,0);
player.storage.mengsanSetupStrength_shuying=entry.amount;player.markSkill(SKILL);return true;
}
export function createTemporaryStrength(game, getActiveBattle) {
return {
amount(player) { return temporaryStrengthAmount(player, getActiveBattle()); },
capture() {
const battle = getActiveBattle();
return battle?.session.active ? battle : null;
},
grant(player, amount, event, battle, {ownTurn=false}={}) {
if (!battle?.session.active || getActiveBattle() !== battle || !player.isAlive()) return false;
if (!Number.isSafeInteger(amount) || amount <= 0) throw new RangeError("临时力量必须是正安全整数");
const ancestor = event.getParent?.("phase");
const turn = ancestor?.name === "phase" ? ancestor : null;
let entry = records.get(player);
if (entry && entry.battle !== battle) {
clearTemporaryStrength(player);
entry = null;
}
if (amount > Number.MAX_SAFE_INTEGER - (entry?.amount || 0)) throw new RangeError("临时力量层数越界");
if (!entry) {
entry = { battle, segments:[], amount: 0 };
// Register cleanup before publishing state. A stale release cannot clear a later battle.
battle.session.ownResource(entry, () => {
if (records.get(player) === entry) forgetTemporaryStrength(player);
});
records.set(player, entry);
}
// Normal card grants keep their original phase expiry; ally gifts last through the recipient's turn.
const part=entry.segments.find(part=>part.ownTurn===ownTurn&&(ownTurn||part.turn===turn));
if(part)part.amount+=amount;else entry.segments.push({turn:ownTurn?null:turn,ownTurn,amount});
entry.amount += amount;
player.storage.mengsanSetupStrength_shuying = entry.amount;
player.addSkill(SKILL);
player.markSkill(SKILL);
game.log(player, ownTurn?`获得${amount}点力量,持续到自己的行动回合结束`:`在本回合内获得${amount}点力量`);
return true;
},
};
}