Files
WT-Offline/apps/server/src/modifications/module-status.ts
T
N0VAN0M4LY f6b00bc787 fix(modifications): emit RESEARCHED status and align buy-all pricing
Add ModuleStatus.RESEARCHED (0x40, ES_ITEM_STATUS_RESEARCHED) for modifications that have reached their RP requirement while a reqModification prerequisite is not yet owned, and keep unresearched modifications locked until their prerequisites are researched.

Update buyAllModifications / buyAllEntry to accept both CAN_BUY and RESEARCHED so the server matches client getAllModsCost (canBuyMod || isModStatusResearched) instead of rejecting cln_buy_all_modification with PRICE_CHANGED (shown as Internal character server error).

Verified: root and launcher typecheck, and runtime check against 2.61.0.58 wpcost.blk chained modifications. Pending in-game acceptance.
2026-12-03 06:38:15 +08:00

120 lines
4.7 KiB
TypeScript

/**
* 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`.
*/
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_* (aces.exe 2.61.0.58 0x17e2894..0x17e294b); same values as VehicleStatus. */
export const ModuleStatus = {
LOCKED: 1,
CAN_RESEARCH: 2,
CAN_BUY: 8,
RESEARCHED: 0x40,
} as const;
export type ModuleStatus = (typeof ModuleStatus)[keyof typeof ModuleStatus];
export interface VehicleModuleState {
/** The highest open tier (hangar tierNumber). */
readonly openTier: number;
/** Status of every modification the player does not own. */
readonly statuses: ReadonlyMap<string, ModuleStatus>;
}
export function vehicleModuleState(
profile: BlkBlock,
equipment: VehicleEquipment,
index?: ProfileEquipmentIndex,
): VehicleModuleState {
const openTier = openTierOf(profile, equipment, index);
const byName = new Map(equipment.modifications.map((mod) => [mod.name, mod]));
const statuses = new Map<string, ModuleStatus>();
for (const modification of equipment.modifications) {
if (isOwned(profile, equipment.vehicle, modification.name, index)) continue;
statuses.set(modification.name, statusOf(profile, equipment.vehicle, modification, openTier, byName, index));
}
return { openTier, statuses };
}
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[],
index?: ProfileEquipmentIndex,
): boolean {
return requires.every((required) => isOwned(profile, vehicle, required, index));
}
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, index)).length;
if (researched < needed) break;
tier++;
}
return tier;
}
function arePrerequisitesResearched(
profile: BlkBlock,
vehicle: string,
requires: readonly string[],
byName: ReadonlyMap<string, ModificationFacts>,
index?: ProfileEquipmentIndex,
): boolean {
return requires.every((required) => {
if (isOwned(profile, vehicle, required, index)) return true;
const mod = byName.get(required);
return mod !== undefined && isResearchable(mod) && isModuleResearched(profile, vehicle, mod, index);
});
}
function statusOf(
profile: BlkBlock,
vehicle: string,
modification: ModificationFacts,
openTier: number,
byName: ReadonlyMap<string, ModificationFacts>,
index?: ProfileEquipmentIndex,
): ModuleStatus {
if (isResearchable(modification)) {
if (modification.tier > openTier) return ModuleStatus.LOCKED;
if (isModuleResearched(profile, vehicle, modification, index)) {
return arePrerequisitesOwned(profile, vehicle, modification.requires, index)
? ModuleStatus.CAN_BUY
: ModuleStatus.RESEARCHED;
}
return arePrerequisitesResearched(profile, vehicle, modification.requires, byName, index)
? ModuleStatus.CAN_RESEARCH
: ModuleStatus.LOCKED;
}
return arePrerequisitesOwned(profile, vehicle, modification.requires, index) ? ModuleStatus.CAN_BUY : ModuleStatus.LOCKED;
}