Files
WT-Offline/apps/server/src/inventory/chest-defs.ts
T

128 lines
4.8 KiB
TypeScript

/**
* External Silver-Lion supply chests and their keyless open generators. Which chests exist, their prices and prizes are tuning (tuning/supply-chests.jsonc);
* prize groups resolve deterministically against the game data.
*/
import type { CatalogueVehicle } from '../game-data/datasets/vehicle-catalogue.ts';
import {
makeLocalizedItemDef,
QUALITY_COLORS,
type ExternalItemPrice,
type GeneratorDefinition,
type GeneratorDrop,
type LocalizedItemDef,
} from './itemdef-types.ts';
import type { ChestPrizeSpec, SupplyChestSpec, SupplyChestTuning } from './supply-chest-tuning.ts';
export interface SupplyChestSources {
readonly wrappersByPart: ReadonlyMap<number, readonly number[]>;
readonly skinCouponIds: readonly number[];
readonly decalCouponIds: readonly number[];
readonly attachableCouponIds: readonly number[];
/** Sorted deterministically by (rank, country, name). */
readonly sortedMarketVehicles: readonly CatalogueVehicle[];
}
export interface SupplyChestBuildResult {
readonly defs: readonly LocalizedItemDef[];
readonly prices: readonly ExternalItemPrice[];
readonly generators: readonly GeneratorDefinition[];
}
export function buildSupplyChests(sources: SupplyChestSources, tuning: SupplyChestTuning): SupplyChestBuildResult {
const pairs = tuning.chests.map((spec) => createChestPair(spec, spec.prizes.flatMap((prize) => resolveDrops(prize, sources)), tuning.maxPerPurchase));
return {
defs: pairs.flatMap((pair) => pair.defs),
prices: pairs.map((pair) => pair.price),
generators: pairs.map((pair) => pair.generator),
};
}
/** Turn one prize group of the tuning file into concrete drops. */
function resolveDrops(prize: ChestPrizeSpec, sources: SupplyChestSources): GeneratorDrop[] {
const drops = (ids: readonly number[]) => ids.map((itemdefId) => ({ itemdefId, weight: prize.weightEach }));
switch (prize.kind) {
case 'classicItems':
return drops((sources.wrappersByPart.get(prize.recyclePart) ?? []).slice(0, prize.pick));
case 'coupons': {
const ids = prize.couponKind === 'skin' ? sources.skinCouponIds : prize.couponKind === 'decal' ? sources.decalCouponIds : sources.attachableCouponIds;
return drops(ids.slice(prize.skip, prize.skip + prize.pick));
}
case 'marketVehicles': {
const inRange = sources.sortedMarketVehicles.filter((v) => v.rank >= prize.minRank && v.rank <= prize.maxRank);
return drops(pickAcrossCountries(inRange, prize.pick).map((v) => v.marketplaceItemdefId!));
}
}
}
/**
* Deterministically select `limit` vehicles from an already `(rank, country, name)`-sorted list,
* rotating across distinct countries first so every major nation is represented before repeating.
*/
export function pickAcrossCountries(sorted: readonly CatalogueVehicle[], limit: number): CatalogueVehicle[] {
const picked: CatalogueVehicle[] = [];
const usedNames = new Set<string>();
const seenCountriesInPass = new Set<string>();
while (picked.length < limit && picked.length < sorted.length) {
seenCountriesInPass.clear();
let addedInPass = false;
for (const vehicle of sorted) {
if (usedNames.has(vehicle.name) || seenCountriesInPass.has(vehicle.country)) continue;
seenCountriesInPass.add(vehicle.country);
usedNames.add(vehicle.name);
picked.push(vehicle);
addedInPass = true;
if (picked.length >= limit) break;
}
if (!addedInPass) break;
}
return picked;
}
function createChestPair(spec: SupplyChestSpec, drops: readonly GeneratorDrop[], maxPerPurchase: number): {
readonly defs: readonly LocalizedItemDef[];
readonly price: ExternalItemPrice;
readonly generator: GeneratorDefinition;
} {
const color = QUALITY_COLORS[spec.quality] ?? '';
const chestDef = makeLocalizedItemDef(spec.id, {
type: 'item',
marketable: false,
item_quality: spec.quality,
name_color: color,
used_to_create: `${spec.openerId}x1`,
tags: [
'type:chest',
'openingWndStyle:silverCommon',
'showChances:yes',
`allowToBuyAmount:${maxPerPurchase}`,
'isAllowSkipOpeningAnim:yes',
'hideMarketablePropDesc:yes',
].join(';'),
icon_url: spec.icon,
}, spec.title, spec.description);
const genDef = makeLocalizedItemDef(spec.openerId, {
type: 'generator',
marketable: false,
item_quality: spec.quality,
name_color: color,
exchange: `${spec.id}x1`,
bundle: drops.map((d) => `${d.itemdefId}x1`).join(';'),
tags: 'devItem:yes',
icon_url: spec.icon,
}, spec.title, { en: '', zh: '' });
return {
defs: [chestDef, genDef],
price: { itemdefId: spec.id, silverLions: spec.silverLions, goldenEagles: 0 },
generator: {
itemdefId: spec.openerId,
isChestGenerator: true,
chestItemdefId: spec.id,
recipes: [{ components: [{ itemdefId: spec.id, quantity: 1 }] }],
drops,
},
};
}