You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
AdLike2048/assets/script/manager/battleBuff/BattleBuffCarryStore.ts

154 lines
5.3 KiB

3 weeks ago
import { TableNames } from '../../game/ConfigTableData';
import { parseBattleBuffRow } from '../../manager/BattleBuffParse';
import type { DailyShopParsed } from '../../manager/DailyShopTypes';
4 days ago
import { DailyShopKind } from '../../manager/DailyShopTypes';
3 weeks ago
/** 单玩家本局携带的 Battlebuff 解析结果 */
interface BattleBuffPlayerCarry {
/** playerId */
playerId: number;
/** 主动 */
actives: DailyShopParsed[];
/** 被动 */
passives: DailyShopParsed[];
}
/**
4 days ago
* 局内按 playerId 的 buff 携带库(单局有效,开战 clear)。
3 weeks ago
*/
export class BattleBuffCarryStore {
private static readonly _players: BattleBuffPlayerCarry[] = [];
4 days ago
/** 清空本局携带(开战/结束时调用) */
3 weeks ago
static clear(): void {
this._players.length = 0;
}
/**
4 days ago
* 写入某玩家本局主动/被动 Battlebuff.id 列表(覆盖)。
3 weeks ago
* @param playerId 槽位
* @param activeIds 主动 id
* @param passiveIds 被动 id
*/
static setFromBuffIds(playerId: number, activeIds: readonly number[], passiveIds: readonly number[]): void {
const pid = Math.max(0, Math.floor(Number(playerId) || 0));
4 days ago
const actives = this._parseIds(activeIds, DailyShopKind.Active);
const passives = this._parseIds(passiveIds, DailyShopKind.Passive);
const existing = this._ensure(pid);
existing.actives = actives;
existing.passives = passives;
}
/**
* 追加一个本局获得的 buff(默认入被动列表)。
* @param playerId 槽位
* @param buffId Battlebuff.id
*/
static appendBuffId(playerId: number, buffId: number): DailyShopParsed | null {
const id = Math.floor(Number(buffId) || 0);
if (id <= 0) {
console.warn('[BattleBuffCarryStore] append 非法 id', buffId);
return null;
}
const row = gg.data.table.getTableData<ITableBattlebuff>(TableNames.Battlebuff, id);
if (!row) {
console.warn('[BattleBuffCarryStore] Battlebuff 缺失', id);
return null;
}
const parsed = parseBattleBuffRow(row);
if (!parsed) return null;
const pid = Math.max(0, Math.floor(Number(playerId) || 0));
const carry = this._ensure(pid);
if (parsed.kind === DailyShopKind.Active) {
carry.actives.push(parsed);
} else {
carry.passives.push(parsed);
}
return parsed;
}
/**
* 本局某 buff 已获得次数(主动+被动合计)。
* @param playerId 槽位
* @param buffId Battlebuff.id
*/
static getObtainCount(playerId: number, buffId: number): number {
const id = Math.floor(Number(buffId) || 0);
if (id <= 0) return 0;
const row = this._find(Math.max(0, Math.floor(Number(playerId) || 0)));
if (!row) return 0;
let n = 0;
for (let i = 0; i < row.passives.length; i++) {
if (row.passives[i].id === id) n++;
}
for (let i = 0; i < row.actives.length; i++) {
if (row.actives[i].id === id) n++;
3 weeks ago
}
4 days ago
return n;
3 weeks ago
}
/**
* 本局被动列表。
4 days ago
* @param playerId 槽位
3 weeks ago
*/
static getPassives(playerId: number): DailyShopParsed[] {
4 days ago
const row = this._find(Math.max(0, Math.floor(Number(playerId) || 0)));
return row ? row.passives.slice() : [];
3 weeks ago
}
/**
* 本局主动列表。
4 days ago
* @param playerId 槽位
3 weeks ago
*/
static getActives(playerId: number): DailyShopParsed[] {
4 days ago
const row = this._find(Math.max(0, Math.floor(Number(playerId) || 0)));
return row ? row.actives.slice() : [];
3 weeks ago
}
/** 是否已为该玩家写入携带(含空列表) */
static hasOverride(playerId: number): boolean {
return !!this._find(Math.max(0, Math.floor(Number(playerId) || 0)));
}
4 days ago
private static _ensure(playerId: number): BattleBuffPlayerCarry {
const existing = this._find(playerId);
if (existing) return existing;
const created: BattleBuffPlayerCarry = { playerId, actives: [], passives: [] };
this._players.push(created);
return created;
}
3 weeks ago
private static _find(playerId: number): BattleBuffPlayerCarry | null {
for (let i = 0; i < this._players.length; i++) {
if (this._players[i].playerId === playerId) return this._players[i];
}
return null;
}
/**
4 days ago
* Battlebuff.id → DailyShopParsed。
3 weeks ago
* @param ids id 列表
4 days ago
* @param expectKind 期望 kind
3 weeks ago
*/
4 days ago
private static _parseIds(ids: readonly number[], expectKind: DailyShopKind): DailyShopParsed[] {
3 weeks ago
const out: DailyShopParsed[] = [];
for (let i = 0; i < ids.length; i++) {
const id = Math.floor(Number(ids[i]) || 0);
if (id <= 0) continue;
const row = gg.data.table.getTableData<ITableBattlebuff>(TableNames.Battlebuff, id);
if (!row) {
4 days ago
console.warn('[BattleBuffCarryStore] Battlebuff 缺失', id);
3 weeks ago
continue;
}
const parsed = parseBattleBuffRow(row);
if (!parsed) continue;
4 days ago
if (parsed.kind !== expectKind) {
console.warn('[BattleBuffCarryStore] kind 不符', id, parsed.kind, 'expect', expectKind);
continue;
}
3 weeks ago
out.push(parsed);
}
return out;
}
}