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
@@ -2,35 +2,12 @@
|
||||
* Initial full stocking and `autoRefillWeapons` Silver-Lion top-up for an owned vehicle's
|
||||
* modifications, ammunition belts/shells, expendables (`tank_medical_kit_expendable`), and
|
||||
* secondary weapon presets.
|
||||
*
|
||||
* Rules:
|
||||
* 1. Zero-cost modifications (`isFree`) whose `requires` are all owned cannot be bought by the
|
||||
* client (`weaponsPurchase.nut` `canBuyItem` rejects zero cost) and receive `count = 1` in
|
||||
* name + topological order, honoring `defaultEnabled` and non-bullet `exclusiveGroup` mutual
|
||||
* exclusion.
|
||||
* 2. Free secondary weapon presets (`isFreeWeapon`, excluding `isWeaponForCustomSlot` containers)
|
||||
* whose `requires` (direct + `sum_weapons` container inheritance) are all owned receive
|
||||
* `count = 1` so `shop_is_weapon_purchased` allows selecting them.
|
||||
* 3. Silver-Lion counted modifications (`isCounted` with `goldenEagles === 0`, covering all
|
||||
* 12,800 shells, gun belts, and `tank_medical_kit_expendable`) and paid weapon presets
|
||||
* (`isPaidWeapon` with `goldenEagles === 0`) whose `requires` are all owned:
|
||||
* - When no profile record exists yet (`record === undefined`, i.e. newly acquired vehicle or
|
||||
* newly unlocked prerequisite modification), receive their initial full load (`maxCount`)
|
||||
* at no extra Silver-Lion charge (`itemInfo.nut` `getAllModsCost` skips `maxAmount > 1`).
|
||||
* - When a record already exists with `count < maxCount` (consumed in battle) and hangar
|
||||
* `autoRefillWeapons` is enabled, buy as many missing pieces with Silver Lions in
|
||||
* deterministic name order as the player's balance affords.
|
||||
*
|
||||
* Returns the Silver Lions spent on refilling (0 for the initial free stock and when
|
||||
* autoRefill is off); battle settlement lists it as the automatic refill cost.
|
||||
*/
|
||||
import type { BlkBlock } from '@wto/blk';
|
||||
import type { ModificationFacts, VehicleEquipment, WeaponFacts } from '../game-data/datasets/vehicle-equipment.ts';
|
||||
import { balanceOf, pay } from '../profile/profile-currency.ts';
|
||||
import { modificationRecord, ownsModification, writeModificationRecord } from '../profile/profile-modifications.ts';
|
||||
import { weaponRecord, writeWeaponRecord } from '../profile/profile-weapons.ts';
|
||||
import { isCounted, isFree, isFreeWeapon, isPaidWeapon } from './equipment-catalogue.ts';
|
||||
import { arePrerequisitesOwned } from './module-status.ts';
|
||||
import { ProfileEquipmentIndex } from './profile-equipment-index.ts';
|
||||
|
||||
export interface EquipmentRefillResult {
|
||||
readonly silverLionsSpent: number;
|
||||
@@ -40,30 +17,34 @@ export function stockAndRefillEquipment(
|
||||
profile: BlkBlock,
|
||||
equipment: VehicleEquipment,
|
||||
autoRefill: boolean,
|
||||
index: ProfileEquipmentIndex = new ProfileEquipmentIndex(profile),
|
||||
): EquipmentRefillResult {
|
||||
const modifications = [...equipment.modifications].sort((a, b) => a.name.localeCompare(b.name));
|
||||
const weapons = [...equipment.weapons].sort((a, b) => a.name.localeCompare(b.name));
|
||||
grantUnlockedFreeModifications(profile, equipment.vehicle, modifications);
|
||||
grantUnlockedFreeWeapons(profile, equipment.vehicle, weapons);
|
||||
const { modifications, weapons } = equipment;
|
||||
grantUnlockedFreeModifications(index, equipment.vehicle, modifications);
|
||||
grantUnlockedFreeWeapons(index, equipment.vehicle, weapons);
|
||||
let silverLionsSpent = 0;
|
||||
for (const modification of modifications) {
|
||||
silverLionsSpent += stockOrRefillCountedModification(profile, equipment.vehicle, modification, autoRefill);
|
||||
silverLionsSpent += stockOrRefillCountedModification(profile, index, equipment.vehicle, modification, autoRefill);
|
||||
}
|
||||
for (const weapon of weapons) {
|
||||
silverLionsSpent += stockOrRefillPaidWeapon(profile, equipment.vehicle, weapon, autoRefill);
|
||||
silverLionsSpent += stockOrRefillPaidWeapon(profile, index, equipment.vehicle, weapon, autoRefill);
|
||||
}
|
||||
return { silverLionsSpent };
|
||||
}
|
||||
|
||||
function grantUnlockedFreeModifications(profile: BlkBlock, vehicle: string, modifications: readonly ModificationFacts[]): void {
|
||||
function prerequisitesOwned(index: ProfileEquipmentIndex, vehicle: string, requires: readonly string[]): boolean {
|
||||
return requires.every((required) => index.ownsModification(vehicle, required));
|
||||
}
|
||||
|
||||
function grantUnlockedFreeModifications(index: ProfileEquipmentIndex, vehicle: string, modifications: readonly ModificationFacts[]): void {
|
||||
let progressed = false;
|
||||
do {
|
||||
progressed = false;
|
||||
for (const modification of modifications) {
|
||||
if (!isFree(modification) || ownsModification(profile, vehicle, modification.name)) continue;
|
||||
if (!arePrerequisitesOwned(profile, vehicle, modification.requires)) continue;
|
||||
const enabled = modification.defaultEnabled && !hasEnabledExclusivePeer(profile, vehicle, modifications, modification.exclusiveGroup);
|
||||
writeModificationRecord(profile, vehicle, modification.name, {
|
||||
if (!isFree(modification) || index.ownsModification(vehicle, modification.name)) continue;
|
||||
if (!prerequisitesOwned(index, vehicle, modification.requires)) continue;
|
||||
const enabled = modification.defaultEnabled && !hasEnabledExclusivePeer(index, vehicle, modifications, modification.exclusiveGroup);
|
||||
index.writeModificationRecord(vehicle, modification.name, {
|
||||
silverLionsSpent: 0,
|
||||
goldenEaglesSpent: 0,
|
||||
enabled,
|
||||
@@ -75,34 +56,35 @@ function grantUnlockedFreeModifications(profile: BlkBlock, vehicle: string, modi
|
||||
}
|
||||
|
||||
function hasEnabledExclusivePeer(
|
||||
profile: BlkBlock,
|
||||
index: ProfileEquipmentIndex,
|
||||
vehicle: string,
|
||||
modifications: readonly ModificationFacts[],
|
||||
exclusiveGroup: string | undefined,
|
||||
): boolean {
|
||||
if (!exclusiveGroup) return false;
|
||||
return modifications.some((peer) => peer.exclusiveGroup === exclusiveGroup && modificationRecord(profile, vehicle, peer.name)?.enabled === true);
|
||||
return modifications.some((peer) => peer.exclusiveGroup === exclusiveGroup && index.modificationRecord(vehicle, peer.name)?.enabled === true);
|
||||
}
|
||||
|
||||
function grantUnlockedFreeWeapons(profile: BlkBlock, vehicle: string, weapons: readonly WeaponFacts[]): void {
|
||||
function grantUnlockedFreeWeapons(index: ProfileEquipmentIndex, vehicle: string, weapons: readonly WeaponFacts[]): void {
|
||||
for (const weapon of weapons) {
|
||||
if (!isFreeWeapon(weapon) || weaponRecord(profile, vehicle, weapon.name)) continue;
|
||||
if (!arePrerequisitesOwned(profile, vehicle, weapon.requires)) continue;
|
||||
writeWeaponRecord(profile, vehicle, weapon.name, { silverLionsSpent: 0, goldenEaglesSpent: 0, count: 1 });
|
||||
if (!isFreeWeapon(weapon) || index.weaponRecord(vehicle, weapon.name)) continue;
|
||||
if (!prerequisitesOwned(index, vehicle, weapon.requires)) continue;
|
||||
index.writeWeaponRecord(vehicle, weapon.name, { silverLionsSpent: 0, goldenEaglesSpent: 0, count: 1 });
|
||||
}
|
||||
}
|
||||
|
||||
function stockOrRefillCountedModification(
|
||||
profile: BlkBlock,
|
||||
index: ProfileEquipmentIndex,
|
||||
vehicle: string,
|
||||
modification: ModificationFacts,
|
||||
autoRefill: boolean,
|
||||
): number {
|
||||
if (!isCounted(modification) || modification.goldenEagles > 0) return 0;
|
||||
if (!arePrerequisitesOwned(profile, vehicle, modification.requires)) return 0;
|
||||
const record = modificationRecord(profile, vehicle, modification.name);
|
||||
if (!prerequisitesOwned(index, vehicle, modification.requires)) return 0;
|
||||
const record = index.modificationRecord(vehicle, modification.name);
|
||||
if (record === undefined) {
|
||||
writeModificationRecord(profile, vehicle, modification.name, {
|
||||
index.writeModificationRecord(vehicle, modification.name, {
|
||||
silverLionsSpent: 0,
|
||||
goldenEaglesSpent: 0,
|
||||
enabled: modification.defaultEnabled,
|
||||
@@ -115,7 +97,7 @@ function stockOrRefillCountedModification(
|
||||
if (toBuy <= 0) return 0;
|
||||
const cost = toBuy * modification.silverLions;
|
||||
if (cost > 0) pay(profile, { silverLions: cost, goldenEagles: 0 });
|
||||
writeModificationRecord(profile, vehicle, modification.name, {
|
||||
index.writeModificationRecord(vehicle, modification.name, {
|
||||
...record,
|
||||
silverLionsSpent: record.silverLionsSpent + cost,
|
||||
count: record.count + toBuy,
|
||||
@@ -125,15 +107,16 @@ function stockOrRefillCountedModification(
|
||||
|
||||
function stockOrRefillPaidWeapon(
|
||||
profile: BlkBlock,
|
||||
index: ProfileEquipmentIndex,
|
||||
vehicle: string,
|
||||
weapon: WeaponFacts,
|
||||
autoRefill: boolean,
|
||||
): number {
|
||||
if (!isPaidWeapon(weapon) || weapon.goldenEagles > 0) return 0;
|
||||
if (!arePrerequisitesOwned(profile, vehicle, weapon.requires)) return 0;
|
||||
const record = weaponRecord(profile, vehicle, weapon.name);
|
||||
if (!prerequisitesOwned(index, vehicle, weapon.requires)) return 0;
|
||||
const record = index.weaponRecord(vehicle, weapon.name);
|
||||
if (record === undefined) {
|
||||
writeWeaponRecord(profile, vehicle, weapon.name, { silverLionsSpent: 0, goldenEaglesSpent: 0, count: weapon.maxCount });
|
||||
index.writeWeaponRecord(vehicle, weapon.name, { silverLionsSpent: 0, goldenEaglesSpent: 0, count: weapon.maxCount });
|
||||
return 0;
|
||||
}
|
||||
if (!autoRefill || record.count >= weapon.maxCount) return 0;
|
||||
@@ -141,7 +124,7 @@ function stockOrRefillPaidWeapon(
|
||||
if (toBuy <= 0) return 0;
|
||||
const cost = toBuy * weapon.silverLions;
|
||||
if (cost > 0) pay(profile, { silverLions: cost, goldenEagles: 0 });
|
||||
writeWeaponRecord(profile, vehicle, weapon.name, {
|
||||
index.writeWeaponRecord(vehicle, weapon.name, {
|
||||
silverLionsSpent: record.silverLionsSpent + cost,
|
||||
goldenEaglesSpent: record.goldenEaglesSpent,
|
||||
count: record.count + toBuy,
|
||||
|
||||
Reference in new issue
Block a user