perf(server): index profile equipment and research refreshes
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.
This commit is contained in:
1 parent
ebb2937a38
commit
62da2e3038
10 files changed
+401
-155
No files matched your search
@@ -1,25 +1,14 @@
|
||||
/**
|
||||
* The status of each modification of an owned vehicle. The client
|
||||
* works out "owned" and "mounted" from the modifications records and "in research" from
|
||||
* `investMode` itself; the server supplies the rest through `modsResearch`:
|
||||
*
|
||||
* researchable modification (tier 1+, research points)
|
||||
* tier not open LOCKED
|
||||
* research points reached CAN_BUY
|
||||
* otherwise CAN_RESEARCH
|
||||
* other modification (ammunition, expendables, premium)
|
||||
* every required modification bought CAN_BUY
|
||||
* otherwise LOCKED
|
||||
*
|
||||
* Tiers: tier 1 is open; tier N+1 opens once tier N is open and at least
|
||||
* needBuyToOpenNextInTier<N> of its counted modifications are researched (scripts
|
||||
* weaponry/bulletsInfo.nut isWeaponTierAvailable, weaponry/modsTree.nut getNextTierModsCount).
|
||||
* `investMode` itself; the server supplies the rest through `modsResearch`.
|
||||
*/
|
||||
import type { BlkBlock } from '@wto/blk';
|
||||
import type { ModificationFacts, VehicleEquipment } from '../game-data/datasets/vehicle-equipment.ts';
|
||||
import { ownsModification } from '../profile/profile-modifications.ts';
|
||||
import { moduleResearchPoints } from '../profile/profile-module-research.ts';
|
||||
import { countsForTier, isResearchable } from './equipment-catalogue.ts';
|
||||
import type { ProfileEquipmentIndex } from './profile-equipment-index.ts';
|
||||
|
||||
/** shopItemConsts ES_ITEM_STATUS_*; the same values as the vehicle statuses. */
|
||||
export const ModuleStatus = {
|
||||
@@ -37,42 +26,70 @@ export interface VehicleModuleState {
|
||||
readonly statuses: ReadonlyMap<string, ModuleStatus>;
|
||||
}
|
||||
|
||||
export function vehicleModuleState(profile: BlkBlock, equipment: VehicleEquipment): VehicleModuleState {
|
||||
const openTier = openTierOf(profile, equipment);
|
||||
export function vehicleModuleState(
|
||||
profile: BlkBlock,
|
||||
equipment: VehicleEquipment,
|
||||
index?: ProfileEquipmentIndex,
|
||||
): VehicleModuleState {
|
||||
const openTier = openTierOf(profile, equipment, index);
|
||||
const statuses = new Map<string, ModuleStatus>();
|
||||
for (const modification of equipment.modifications) {
|
||||
if (ownsModification(profile, equipment.vehicle, modification.name)) continue;
|
||||
statuses.set(modification.name, statusOf(profile, equipment.vehicle, modification, openTier));
|
||||
if (isOwned(profile, equipment.vehicle, modification.name, index)) continue;
|
||||
statuses.set(modification.name, statusOf(profile, equipment.vehicle, modification, openTier, index));
|
||||
}
|
||||
return { openTier, statuses };
|
||||
}
|
||||
|
||||
export function isModuleResearched(profile: BlkBlock, vehicle: string, modification: ModificationFacts): boolean {
|
||||
return ownsModification(profile, vehicle, modification.name)
|
||||
|| moduleResearchPoints(profile, vehicle, modification.name) >= modification.researchPoints;
|
||||
export function isModuleResearched(
|
||||
profile: BlkBlock,
|
||||
vehicle: string,
|
||||
modification: ModificationFacts,
|
||||
index?: ProfileEquipmentIndex,
|
||||
): boolean {
|
||||
return isOwned(profile, vehicle, modification.name, index)
|
||||
|| researchPointsOf(profile, vehicle, modification.name, index) >= modification.researchPoints;
|
||||
}
|
||||
|
||||
export function arePrerequisitesOwned(profile: BlkBlock, vehicle: string, requires: readonly string[]): boolean {
|
||||
return requires.every((required) => ownsModification(profile, vehicle, required));
|
||||
export function arePrerequisitesOwned(
|
||||
profile: BlkBlock,
|
||||
vehicle: string,
|
||||
requires: readonly string[],
|
||||
index?: ProfileEquipmentIndex,
|
||||
): boolean {
|
||||
return requires.every((required) => isOwned(profile, vehicle, required, index));
|
||||
}
|
||||
|
||||
function openTierOf(profile: BlkBlock, equipment: VehicleEquipment): number {
|
||||
function isOwned(profile: BlkBlock, vehicle: string, modification: string, index?: ProfileEquipmentIndex): boolean {
|
||||
return index ? index.ownsModification(vehicle, modification) : ownsModification(profile, vehicle, modification);
|
||||
}
|
||||
|
||||
function researchPointsOf(profile: BlkBlock, vehicle: string, modification: string, index?: ProfileEquipmentIndex): number {
|
||||
return index ? index.moduleResearchPoints(vehicle, modification) : moduleResearchPoints(profile, vehicle, modification);
|
||||
}
|
||||
|
||||
function openTierOf(profile: BlkBlock, equipment: VehicleEquipment, index?: ProfileEquipmentIndex): number {
|
||||
let tier = 1;
|
||||
while (tier <= equipment.tierRequirements.length) {
|
||||
const needed = equipment.tierRequirements[tier - 1] ?? 0;
|
||||
const tierModifications = equipment.modifications.filter((modification) => modification.tier === tier && countsForTier(modification));
|
||||
if (tierModifications.length === 0) break;
|
||||
const researched = tierModifications.filter((modification) => isModuleResearched(profile, equipment.vehicle, modification)).length;
|
||||
const researched = tierModifications.filter((modification) => isModuleResearched(profile, equipment.vehicle, modification, index)).length;
|
||||
if (researched < needed) break;
|
||||
tier++;
|
||||
}
|
||||
return tier;
|
||||
}
|
||||
|
||||
function statusOf(profile: BlkBlock, vehicle: string, modification: ModificationFacts, openTier: number): ModuleStatus {
|
||||
function statusOf(
|
||||
profile: BlkBlock,
|
||||
vehicle: string,
|
||||
modification: ModificationFacts,
|
||||
openTier: number,
|
||||
index?: ProfileEquipmentIndex,
|
||||
): ModuleStatus {
|
||||
if (isResearchable(modification)) {
|
||||
if (modification.tier > openTier) return ModuleStatus.LOCKED;
|
||||
return isModuleResearched(profile, vehicle, modification) ? ModuleStatus.CAN_BUY : ModuleStatus.CAN_RESEARCH;
|
||||
return isModuleResearched(profile, vehicle, modification, index) ? ModuleStatus.CAN_BUY : ModuleStatus.CAN_RESEARCH;
|
||||
}
|
||||
return arePrerequisitesOwned(profile, vehicle, modification.requires) ? ModuleStatus.CAN_BUY : ModuleStatus.LOCKED;
|
||||
return arePrerequisitesOwned(profile, vehicle, modification.requires, index) ? ModuleStatus.CAN_BUY : ModuleStatus.LOCKED;
|
||||
}
|
||||
Reference in new issue
Block a user