Recompute unlocked ranks, prerequisites, and unit states before serving profiles. Add research target selection, research conversion, vehicle and slot purchases, and the price table. Support editing convertible RP and restrict hidden vehicle visibility to eligible entries. The client still displays zero purchase prices pending price table integration.
52 lines
2.9 KiB
TypeScript
52 lines
2.9 KiB
TypeScript
/**
|
|
* Bringing the research state of a profile up to date. The char
|
|
* server keeps it current online; the local service 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.
|
|
*
|
|
* 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. Drop research targets that are no longer researchable (bought, researched, ...).
|
|
*/
|
|
import type { BlkBlock } from '@wto/blk';
|
|
import { ownedVehicles } from '../profile/profile-hangar.ts';
|
|
import { researchTarget, 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 { openEarnedRanks } from './era-progress.ts';
|
|
import type { ResearchRules } from './research-rules.ts';
|
|
import { earnVehicleUnlocks } from './vehicle-unlock-rules.ts';
|
|
import { VehicleStatus, vehicleStatuses } from './vehicle-status.ts';
|
|
|
|
/** More rounds than any chain in the game data needs; stops a faulty data loop. */
|
|
const MAX_ROUNDS = 16;
|
|
|
|
export function refreshResearchState(profile: BlkBlock, rules: ResearchRules): void {
|
|
const owned = ownedVehicles(profile);
|
|
const earned = earnedUnlocks(profile);
|
|
for (let round = 0; round < MAX_ROUNDS; round++) {
|
|
const ranksChanged = openEarnedRanks(profile, owned, rules.techTree, rules.eraRequirements);
|
|
const unlocksChanged = earnVehicleUnlocks(profile, { earned, owned }, rules.vehicleUnlocks, rules.techTree);
|
|
if (!ranksChanged && !unlocksChanged) break;
|
|
}
|
|
|
|
const statuses = vehicleStatuses({ profile, owned, earnedUnlocks: earned, techTree: rules.techTree });
|
|
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);
|
|
else setVehicleStatus(profile, vehicle.name, status);
|
|
}
|
|
|
|
for (const { country, type } of rules.techTree.branches()) {
|
|
const target = researchTarget(profile, country, type);
|
|
if (target && statuses.get(target) !== VehicleStatus.CAN_RESEARCH) setResearchTarget(profile, country, type, '');
|
|
}
|
|
}
|
|
|
|
/** The current status of one vehicle the player does not own; undefined when it is owned or unknown. */
|
|
export function currentStatus(profile: BlkBlock, rules: ResearchRules, vehicle: string): VehicleStatus | undefined {
|
|
return vehicleStatuses({ profile, owned: ownedVehicles(profile), earnedUnlocks: earnedUnlocks(profile), techTree: rules.techTree }).get(vehicle);
|
|
}
|