140 lines
5.6 KiB
TypeScript
140 lines
5.6 KiB
TypeScript
/**
|
|
* 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.
|
|
*/
|
|
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 { isCounted, isFree, isFreeWeapon, isPaidWeapon } from './equipment-catalogue.ts';
|
|
import { ProfileEquipmentIndex } from './profile-equipment-index.ts';
|
|
|
|
export interface EquipmentRefillResult {
|
|
readonly silverLionsSpent: number;
|
|
}
|
|
|
|
export function stockAndRefillEquipment(
|
|
profile: BlkBlock,
|
|
equipment: VehicleEquipment,
|
|
autoRefill: boolean,
|
|
index: ProfileEquipmentIndex = new ProfileEquipmentIndex(profile),
|
|
): EquipmentRefillResult {
|
|
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, index, equipment.vehicle, modification, autoRefill);
|
|
}
|
|
for (const weapon of weapons) {
|
|
silverLionsSpent += stockOrRefillPaidWeapon(profile, index, equipment.vehicle, weapon, autoRefill);
|
|
}
|
|
return { silverLionsSpent };
|
|
}
|
|
|
|
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) || 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,
|
|
count: Math.max(1, modification.maxCount),
|
|
});
|
|
progressed = true;
|
|
}
|
|
} while (progressed);
|
|
}
|
|
|
|
function hasEnabledExclusivePeer(
|
|
index: ProfileEquipmentIndex,
|
|
vehicle: string,
|
|
modifications: readonly ModificationFacts[],
|
|
exclusiveGroup: string | undefined,
|
|
): boolean {
|
|
if (!exclusiveGroup) return false;
|
|
return modifications.some((peer) => peer.exclusiveGroup === exclusiveGroup && index.modificationRecord(vehicle, peer.name)?.enabled === true);
|
|
}
|
|
|
|
function grantUnlockedFreeWeapons(index: ProfileEquipmentIndex, vehicle: string, weapons: readonly WeaponFacts[]): void {
|
|
for (const weapon of weapons) {
|
|
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: Math.max(1, weapon.maxCount) });
|
|
}
|
|
}
|
|
|
|
function stockOrRefillCountedModification(
|
|
profile: BlkBlock,
|
|
index: ProfileEquipmentIndex,
|
|
vehicle: string,
|
|
modification: ModificationFacts,
|
|
autoRefill: boolean,
|
|
): number {
|
|
if (!isCounted(modification) || modification.goldenEagles > 0) return 0;
|
|
if (!prerequisitesOwned(index, vehicle, modification.requires)) return 0;
|
|
const record = index.modificationRecord(vehicle, modification.name);
|
|
if (record === undefined) {
|
|
index.writeModificationRecord(vehicle, modification.name, {
|
|
silverLionsSpent: 0,
|
|
goldenEaglesSpent: 0,
|
|
enabled: modification.defaultEnabled,
|
|
count: modification.maxCount,
|
|
});
|
|
return 0;
|
|
}
|
|
if (!autoRefill || record.count >= modification.maxCount) return 0;
|
|
const toBuy = affordableUnits(profile, modification.maxCount - record.count, modification.silverLions);
|
|
if (toBuy <= 0) return 0;
|
|
const cost = toBuy * modification.silverLions;
|
|
if (cost > 0) pay(profile, { silverLions: cost, goldenEagles: 0 });
|
|
index.writeModificationRecord(vehicle, modification.name, {
|
|
...record,
|
|
silverLionsSpent: record.silverLionsSpent + cost,
|
|
count: record.count + toBuy,
|
|
});
|
|
return cost;
|
|
}
|
|
|
|
function stockOrRefillPaidWeapon(
|
|
profile: BlkBlock,
|
|
index: ProfileEquipmentIndex,
|
|
vehicle: string,
|
|
weapon: WeaponFacts,
|
|
autoRefill: boolean,
|
|
): number {
|
|
if (!isPaidWeapon(weapon) || weapon.goldenEagles > 0) return 0;
|
|
if (!prerequisitesOwned(index, vehicle, weapon.requires)) return 0;
|
|
const record = index.weaponRecord(vehicle, weapon.name);
|
|
if (record === undefined) {
|
|
index.writeWeaponRecord(vehicle, weapon.name, { silverLionsSpent: 0, goldenEaglesSpent: 0, count: weapon.maxCount });
|
|
return 0;
|
|
}
|
|
if (!autoRefill || record.count >= weapon.maxCount) return 0;
|
|
const toBuy = affordableUnits(profile, weapon.maxCount - record.count, weapon.silverLions);
|
|
if (toBuy <= 0) return 0;
|
|
const cost = toBuy * weapon.silverLions;
|
|
if (cost > 0) pay(profile, { silverLions: cost, goldenEagles: 0 });
|
|
index.writeWeaponRecord(vehicle, weapon.name, {
|
|
silverLionsSpent: record.silverLionsSpent + cost,
|
|
goldenEaglesSpent: record.goldenEaglesSpent,
|
|
count: record.count + toBuy,
|
|
});
|
|
return cost;
|
|
}
|
|
|
|
function affordableUnits(profile: BlkBlock, missing: number, unitSilverCost: number): number {
|
|
if (missing <= 0) return 0;
|
|
if (unitSilverCost <= 0) return missing;
|
|
return Math.min(missing, Math.floor(balanceOf(profile).silverLions / unitSilverCost));
|
|
}
|