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:
N0VAN0M4LY committed 2026-11-30 17:54:36 +08:00
1 parent ebb2937a38
commit 62da2e3038
10 files changed
+401 -155

No files matched your search

+29 -18
View File
@@ -3,21 +3,8 @@
* server keeps it current online; the local server does it whenever a profile is handed out
* or changed (profile/player-profiles.ts), so the state always follows from the facts:
* owned vehicles, research points, earned unlocks, banked excess research points.
*
* 1. Open ranks and earn unlocks until neither changes anything (an unlock can give a vehicle,
* a vehicle can open a rank, a rank can earn an unlock).
* 2. Write every vehicle's status into unitResearch: owned vehicles have no entry, locked
* vehicles only keep one while they hold research points.
* 3. Keep every branch researching while it can: a target that is no
* longer researchable (bought, researched, ...) is cleared, and a new one is picked by
* auto-research-target.ts when the branch has just finished a vehicle (prevUnit set: the
* stock scripts then open the "choose the next research" window, ShopCheckResearch, from
* scripts/researches/researchActions.nut checkNonApprovedResearches) or has no banked
* exp<Branch>. With banked RP and nothing just finished it stays empty, so the player chooses
* where the RP goes (cln_move_exp_to_unit). A branch with nothing left to research has its
* banked RP cleared.
*/
import type { BlkBlock } from '@wto/blk';
import { ensureBlock, findBlock, findParam, setParam, type BlkBlock } from '@wto/blk';
import { ownedVehicles } from '../profile/profile-hangar.ts';
import {
branchExcessRp,
@@ -27,7 +14,6 @@ import {
setResearchTarget,
} from '../profile/profile-research-targets.ts';
import { earnedUnlocks } from '../profile/profile-unlocks.ts';
import { removeVehicleResearch, researchPointsOf, setVehicleStatus } from '../profile/profile-vehicle-research.ts';
import { pickResearchTarget } from './auto-research-target.ts';
import { openEarnedRanks } from './era-progress.ts';
import type { ResearchRules } from './research-rules.ts';
@@ -47,15 +33,40 @@ export function refreshResearchState(profile: BlkBlock, rules: ResearchRules): v
}
const statuses = vehicleStatuses({ profile, owned, earnedUnlocks: earned, techTree: rules.techTree });
let researchBlock = findBlock(profile, 'unitResearch');
const researchEntries = new Map<string, BlkBlock>();
if (researchBlock) {
for (const entry of researchBlock.blocks) researchEntries.set(entry.name, entry);
}
const removed = new Set<string>();
for (const vehicle of rules.techTree.all()) {
const status = statuses.get(vehicle.name);
if (status === undefined || (status === VehicleStatus.LOCKED && researchPointsOf(profile, vehicle.name) === 0)) {
removeVehicleResearch(profile, vehicle.name);
const entry = researchEntries.get(vehicle.name);
const expParam = entry && findParam(entry, 'exp')?.value;
const rp = expParam?.type === 'int64' ? Number(expParam.value) : (expParam?.type === 'int' ? expParam.value : 0);
if (status === undefined || (status === VehicleStatus.LOCKED && rp === 0)) {
if (entry) {
removed.add(vehicle.name);
researchEntries.delete(vehicle.name);
}
} else {
setVehicleStatus(profile, vehicle.name, status);
let target = entry;
if (!target) {
researchBlock ??= ensureBlock(profile, 'unitResearch');
target = { name: vehicle.name, params: [], blocks: [] };
researchBlock.blocks.push(target);
researchEntries.set(vehicle.name, target);
}
setParam(target, 'unitStatus', { type: 'int', value: status });
}
}
if (removed.size > 0 && researchBlock) {
researchBlock.blocks = researchBlock.blocks.filter((entry) => !removed.has(entry.name));
}
const candidates = researchCandidates(rules, statuses);
const unresearchedBranches = branchesWithUnresearchedTreeVehicles(rules, statuses);
for (const { country, type } of rules.techTree.branches()) {
+30 -21
View File
@@ -1,24 +1,9 @@
/**
* The status of each vehicle the player does not own, in
* the order of the stock explanation of why a vehicle cannot be bought or researched
* (gui.vromfs.bin scripts/unit/unitInfoTexts.nut getCantBuyUnitReason):
*
* not in the tech tree, part, hidden, gift, event locked
* reqUnlock not earned locked
* premium can buy
* rank not open (squadron vehicles have no rank gate) locked
* research points reached (or none needed):
* previous vehicle owned, or none can buy
* otherwise researched ("buy the previous first")
* not reached:
* previous vehicle researched or owned, or none can research
* otherwise locked
*
* "In research" is not a status the server gives: the client shows a researchable vehicle
* that way when it is the research target (aces.exe 2.61.0.37 0xebf7a0).
* (gui.vromfs.bin scripts/unit/unitInfoTexts.nut getCantBuyUnitReason).
*/
import type { BlkBlock } from '@wto/blk';
import { openRank } from '../profile/profile-eras.ts';
import { findBlock, findParam, type BlkBlock } from '@wto/blk';
import { researchPointsOf } from '../profile/profile-vehicle-research.ts';
import type { TechTree, TreeVehicle } from './tech-tree.ts';
@@ -42,13 +27,31 @@ export interface StatusInputs {
/** Status of every vehicle the player does not own. */
export function vehicleStatuses(inputs: StatusInputs): Map<string, VehicleStatus> {
const eras = new Map<string, number>();
const erasBlock = findBlock(inputs.profile, 'eras');
if (erasBlock) {
for (const param of erasBlock.params) {
if (param.value.type === 'int') eras.set(param.name, param.value.value);
}
}
const rpByVehicle = new Map<string, number>();
const researchBlock = findBlock(inputs.profile, 'unitResearch');
if (researchBlock) {
for (const entry of researchBlock.blocks) {
const value = findParam(entry, 'exp')?.value;
if (value?.type === 'int64') rpByVehicle.set(entry.name, Number(value.value));
else if (value?.type === 'int') rpByVehicle.set(entry.name, value.value);
}
}
const statuses = new Map<string, VehicleStatus>();
const statusOf = (vehicle: TreeVehicle): VehicleStatus => {
let status = statuses.get(vehicle.name);
if (status === undefined) {
// Locked while being worked out, so a loop in the previous-vehicle data cannot recurse forever.
statuses.set(vehicle.name, VehicleStatus.LOCKED);
status = computeStatus(vehicle, inputs, statusOfName);
status = computeStatus(vehicle, inputs, eras, rpByVehicle, statusOfName);
statuses.set(vehicle.name, status);
}
return status;
@@ -62,14 +65,20 @@ export function vehicleStatuses(inputs: StatusInputs): Map<string, VehicleStatus
return statuses;
}
function computeStatus(vehicle: TreeVehicle, inputs: StatusInputs, statusOfName: (name: string) => VehicleStatus | 'owned' | undefined): VehicleStatus {
function computeStatus(
vehicle: TreeVehicle,
inputs: StatusInputs,
eras: ReadonlyMap<string, number>,
rpByVehicle: ReadonlyMap<string, number>,
statusOfName: (name: string) => VehicleStatus | 'owned' | undefined,
): VehicleStatus {
if (!vehicle.inTechTree || UNAVAILABLE_KINDS.has(vehicle.kind)) return VehicleStatus.LOCKED;
if (vehicle.requiredUnlock && !inputs.earnedUnlocks.has(vehicle.requiredUnlock)) return VehicleStatus.LOCKED;
if (vehicle.kind === 'premium') return VehicleStatus.CAN_BUY;
// getCantBuyUnitReason skips the rank check for squadron vehicles.
if (vehicle.kind !== 'squadron' && openRank(inputs.profile, vehicle.country, vehicle.type) < vehicle.rank) return VehicleStatus.LOCKED;
if (vehicle.kind !== 'squadron' && (eras.get(`${vehicle.type}_${vehicle.country}`) ?? 0) < vehicle.rank) return VehicleStatus.LOCKED;
const previous = vehicle.requires === undefined ? 'owned' : statusOfName(vehicle.requires);
if (isResearched(vehicle, inputs.profile)) return previous === 'owned' ? VehicleStatus.CAN_BUY : VehicleStatus.RESEARCHED;
if ((rpByVehicle.get(vehicle.name) ?? 0) >= vehicle.researchPoints) return previous === 'owned' ? VehicleStatus.CAN_BUY : VehicleStatus.RESEARCHED;
const previousResearched = previous === 'owned' || previous === VehicleStatus.CAN_BUY || previous === VehicleStatus.RESEARCHED;
return previousResearched ? VehicleStatus.CAN_RESEARCH : VehicleStatus.LOCKED;
}