import type { AiHeroWeaponRecord } from '../types/AiHeroWeaponTypes'; import { aiLog } from './AiLog'; /** * 本局 AI 合成武将武器存档:heroId -> weaponId(需求 5.6)。 * 不同武将按权重独立掷骰,品质/武器 id 允许重复; * 仅同一 heroId 拆开再合时复用,避免对该武将重掷。 */ export class AiHeroWeaponRegistry { private readonly _records: AiHeroWeaponRecord[] = []; /** 已绑定条数 */ get size(): number { return this._records.length; } /** 只读快照 */ getRecords(): readonly AiHeroWeaponRecord[] { return this._records; } /** * 查询已分配武器 id。 * @param heroId Hero.id */ getWeaponId(heroId: number): number | null { const id = Math.floor(Number(heroId) || 0); if (id <= 0) return null; for (let i = 0; i < this._records.length; i++) { if (this._records[i].heroId === id) return this._records[i].weaponId; } return null; } /** * 写入绑定;同一 heroId 重复写入须 id 一致。 * @param record 绑定记录 */ put(record: AiHeroWeaponRecord): boolean { const heroId = Math.floor(Number(record.heroId) || 0); const weaponId = Math.floor(Number(record.weaponId) || 0); if (heroId <= 0 || weaponId <= 0) { aiLog('[AiHeroWeaponRegistry] put rejected: invalid ids'); return false; } const existing = this.getWeaponId(heroId); if (existing != null) { if (existing !== weaponId) { aiLog('[AiHeroWeaponRegistry] heroId already bound with different weapon', heroId, existing, weaponId); } return existing === weaponId; } this._records.push({ heroId, weaponId, quality: Math.floor(Number(record.quality) || 0), }); return true; } /** 战斗结束清空 */ clear(): void { this._records.length = 0; } }