Replace O(N^2) linear lookups and repeated array filtering across modifications, weapons, modsResearch and unitResearch with single-pass Map indexes, pre-sort catalogue equipment once on load, avoid redundant pre-change disk writes in PlayerProfiles.updateWhen, and shallow-copy only top-level lists in clientProfile.
204 lines
8.3 KiB
TypeScript
204 lines
8.3 KiB
TypeScript
/**
|
|
* Hash-indexed view over a profile's flat `hangar`, `modifications`, `weapons` and `modsResearch`
|
|
* blocks for the duration of a single refresh pass. Large profiles with 1,000+ spaded vehicles
|
|
* carry 30,000+ flat sub-blocks; indexing once drops refresh cost from O(N^2) to O(N).
|
|
*/
|
|
import { ensureBlock, findBlock, findParam, setParam, type BlkBlock, type BlkParam } from '@wto/blk';
|
|
import { recordName, type ModificationRecord } from '../profile/profile-modifications.ts';
|
|
import type { WeaponRecord } from '../profile/profile-weapons.ts';
|
|
|
|
const HANGAR = 'hangar';
|
|
const MODIFICATIONS = 'modifications';
|
|
const WEAPONS = 'weapons';
|
|
const MODS_RESEARCH = 'modsResearch';
|
|
const STATUS = 'modStatus';
|
|
const RESEARCH_POINTS = 'exp';
|
|
|
|
export class ProfileEquipmentIndex {
|
|
readonly #profile: BlkBlock;
|
|
readonly #hangar = new Map<string, BlkBlock>();
|
|
readonly #modifications = new Map<string, BlkBlock>();
|
|
readonly #weapons = new Map<string, BlkBlock>();
|
|
readonly #modsResearch = new Map<string, BlkBlock>();
|
|
readonly #removedResearch = new Set<string>();
|
|
#modificationsBlock: BlkBlock | undefined;
|
|
#weaponsBlock: BlkBlock | undefined;
|
|
#modsResearchBlock: BlkBlock | undefined;
|
|
|
|
constructor(profile: BlkBlock) {
|
|
this.#profile = profile;
|
|
const hangar = findBlock(profile, HANGAR);
|
|
if (hangar) for (const entry of hangar.blocks) this.#hangar.set(entry.name, entry);
|
|
|
|
this.#modificationsBlock = findBlock(profile, MODIFICATIONS);
|
|
if (this.#modificationsBlock) {
|
|
for (const entry of this.#modificationsBlock.blocks) this.#modifications.set(entry.name, entry);
|
|
}
|
|
|
|
this.#weaponsBlock = findBlock(profile, WEAPONS);
|
|
if (this.#weaponsBlock) {
|
|
for (const entry of this.#weaponsBlock.blocks) this.#weapons.set(entry.name, entry);
|
|
}
|
|
|
|
this.#modsResearchBlock = findBlock(profile, MODS_RESEARCH);
|
|
if (this.#modsResearchBlock) {
|
|
for (const entry of this.#modsResearchBlock.blocks) this.#modsResearch.set(entry.name, entry);
|
|
}
|
|
}
|
|
|
|
hangarEntry(vehicle: string): BlkBlock | undefined {
|
|
return this.#hangar.get(vehicle);
|
|
}
|
|
|
|
setOpenTier(vehicle: string, tier: number): void {
|
|
const entry = this.#hangar.get(vehicle);
|
|
if (entry) setParam(entry, 'tierNumber', { type: 'int', value: tier });
|
|
}
|
|
|
|
moduleResearchTarget(vehicle: string): string {
|
|
const entry = this.#hangar.get(vehicle);
|
|
const value = entry && findParam(entry, 'investMode')?.value;
|
|
return value?.type === 'string' ? value.value : '';
|
|
}
|
|
|
|
setModuleResearchTarget(vehicle: string, modification: string): void {
|
|
const entry = this.#hangar.get(vehicle);
|
|
if (entry) setParam(entry, 'investMode', { type: 'string', value: modification });
|
|
}
|
|
|
|
finishedModuleResearch(vehicle: string): string {
|
|
const entry = this.#hangar.get(vehicle);
|
|
const value = entry && findParam(entry, 'investModePrev')?.value;
|
|
return value?.type === 'string' ? value.value : '';
|
|
}
|
|
|
|
vehicleExcessRp(vehicle: string): number {
|
|
const entry = this.#hangar.get(vehicle);
|
|
const value = entry && findParam(entry, 'excessExp')?.value;
|
|
if (value?.type === 'int64') return Math.max(0, Number(value.value));
|
|
return value?.type === 'int' ? Math.max(0, value.value) : 0;
|
|
}
|
|
|
|
setVehicleExcessRp(vehicle: string, amount: number): void {
|
|
const entry = this.#hangar.get(vehicle);
|
|
if (entry) setParam(entry, 'excessExp', { type: 'int64', value: BigInt(Math.max(0, Math.trunc(amount))) });
|
|
}
|
|
|
|
ownsModification(vehicle: string, modification: string): boolean {
|
|
return this.#modifications.has(recordName(vehicle, modification));
|
|
}
|
|
|
|
modificationRecord(vehicle: string, modification: string): ModificationRecord | undefined {
|
|
const block = this.#modifications.get(recordName(vehicle, modification));
|
|
if (!block) return undefined;
|
|
return {
|
|
silverLionsSpent: intOf(block, 'wpSpent'),
|
|
goldenEaglesSpent: intOf(block, 'goldSpent'),
|
|
enabled: boolOf(block, 'enabled') ?? true,
|
|
count: intOf(block, 'count'),
|
|
};
|
|
}
|
|
|
|
writeModificationRecord(vehicle: string, modification: string, record: ModificationRecord): void {
|
|
const key = recordName(vehicle, modification);
|
|
let block = this.#modifications.get(key);
|
|
if (!block) {
|
|
this.#modificationsBlock ??= ensureBlock(this.#profile, MODIFICATIONS);
|
|
block = { name: key, params: [], blocks: [] };
|
|
this.#modificationsBlock.blocks.push(block);
|
|
this.#modifications.set(key, block);
|
|
}
|
|
block.params = orderedModParams(record);
|
|
}
|
|
|
|
weaponRecord(vehicle: string, weapon: string): WeaponRecord | undefined {
|
|
const block = this.#weapons.get(`${vehicle}_${weapon}`);
|
|
if (!block) return undefined;
|
|
return {
|
|
silverLionsSpent: intOf(block, 'wpSpent'),
|
|
goldenEaglesSpent: intOf(block, 'goldSpent'),
|
|
count: intOf(block, 'count'),
|
|
};
|
|
}
|
|
|
|
writeWeaponRecord(vehicle: string, weapon: string, record: WeaponRecord): void {
|
|
const key = `${vehicle}_${weapon}`;
|
|
let block = this.#weapons.get(key);
|
|
if (!block) {
|
|
this.#weaponsBlock ??= ensureBlock(this.#profile, WEAPONS);
|
|
block = { name: key, params: [], blocks: [] };
|
|
this.#weaponsBlock.blocks.push(block);
|
|
this.#weapons.set(key, block);
|
|
}
|
|
block.params = orderedWeaponParams(record);
|
|
}
|
|
|
|
moduleResearchPoints(vehicle: string, modification: string): number {
|
|
const key = recordName(vehicle, modification);
|
|
if (this.#removedResearch.has(key)) return 0;
|
|
const entry = this.#modsResearch.get(key);
|
|
const value = entry && findParam(entry, RESEARCH_POINTS)?.value;
|
|
if (value?.type === 'int64') return Number(value.value);
|
|
return value?.type === 'int' ? value.value : 0;
|
|
}
|
|
|
|
setModuleStatus(vehicle: string, modification: string, status: number): void {
|
|
const key = recordName(vehicle, modification);
|
|
const points = this.moduleResearchPoints(vehicle, modification);
|
|
this.#removedResearch.delete(key);
|
|
let entry = this.#modsResearch.get(key);
|
|
if (!entry) {
|
|
this.#modsResearchBlock ??= ensureBlock(this.#profile, MODS_RESEARCH);
|
|
entry = { name: key, params: [], blocks: [] };
|
|
this.#modsResearchBlock.blocks.push(entry);
|
|
this.#modsResearch.set(key, entry);
|
|
}
|
|
const params: BlkParam[] = [];
|
|
if (points) params.push({ name: RESEARCH_POINTS, value: { type: 'int64', value: BigInt(points) } });
|
|
params.push({ name: STATUS, value: { type: 'int', value: status } });
|
|
entry.params = params;
|
|
}
|
|
|
|
removeModuleResearch(vehicle: string, modification: string): void {
|
|
const key = recordName(vehicle, modification);
|
|
if (!this.#modsResearch.has(key)) return;
|
|
this.#removedResearch.add(key);
|
|
this.#modsResearch.delete(key);
|
|
}
|
|
|
|
flush(): void {
|
|
if (this.#removedResearch.size > 0 && this.#modsResearchBlock) {
|
|
this.#modsResearchBlock.blocks = this.#modsResearchBlock.blocks.filter((entry) => !this.#removedResearch.has(entry.name));
|
|
this.#removedResearch.clear();
|
|
}
|
|
}
|
|
}
|
|
|
|
function orderedModParams(record: ModificationRecord): BlkParam[] {
|
|
const params: BlkParam[] = [];
|
|
if (record.silverLionsSpent) params.push({ name: 'wpSpent', value: { type: 'int', value: record.silverLionsSpent } });
|
|
if (record.goldenEaglesSpent) params.push({ name: 'goldSpent', value: { type: 'int', value: record.goldenEaglesSpent } });
|
|
if (!record.enabled) params.push({ name: 'enabled', value: { type: 'bool', value: false } });
|
|
if (record.count) params.push({ name: 'count', value: { type: 'int64', value: BigInt(record.count) } });
|
|
return params;
|
|
}
|
|
|
|
function orderedWeaponParams(record: WeaponRecord): BlkParam[] {
|
|
const params: BlkParam[] = [];
|
|
if (record.silverLionsSpent) params.push({ name: 'wpSpent', value: { type: 'int', value: record.silverLionsSpent } });
|
|
if (record.goldenEaglesSpent) params.push({ name: 'goldSpent', value: { type: 'int', value: record.goldenEaglesSpent } });
|
|
if (record.count) params.push({ name: 'count', value: { type: 'int64', value: BigInt(record.count) } });
|
|
return params;
|
|
}
|
|
|
|
function intOf(block: BlkBlock, name: string): number {
|
|
const value = block.params.find((param) => param.name === name)?.value;
|
|
if (value?.type === 'int64') return Number(value.value);
|
|
return value?.type === 'int' ? value.value : 0;
|
|
}
|
|
|
|
function boolOf(block: BlkBlock, name: string): boolean | undefined {
|
|
const value = block.params.find((param) => param.name === name)?.value;
|
|
return value?.type === 'bool' ? value.value : undefined;
|
|
}
|