Files
WT-Offline/packages/local-service/src/player-card/showcase-rules.ts
T
N0VAN0M4LY ae6958f291 Add classic items and fix initial showcase responses
Store and activate boosters, personal discounts, wagers, and universal
spares. Apply active personal discounts when pricing vehicle purchases.
Return the server time and default fields for every showcase type.

Add developer commands for inspecting game logs, strings, references,
callers, and disassembly.
2026-11-25 04:17:34 +08:00

148 lines
7.1 KiB
TypeScript

/**
* The profile showcase (implementation notes; scripts user/profileShowcase.nut and
* user/showcase/*.nut).
*
* cln_save_profile_showcase carries showcaseType:t and that type's choices under the names its
* getSaveData uses; the client reads them back (cln_get_showcases, the terse info's showcase
* block) under other names. Per type, request -> stored field:
* favorite_mode favoriteMode -> mode
* battle_hardened battleHardenedMode -> h_mode
* favorite_unit favoriteUnit, favoriteUnitDifficulty -> unit, difficulty
* unit_collector unitCollectorFavorites (repeated) -> units
* medalist medals (repeated) -> medals
* achievement_hunter achievements (repeated) -> achievements
* ace_of_spades acedUnitsFilter{ country_<c>:t=<unit type> ... }, kept as it is; the
* answer turns it into aced_units{ <type>{ <c>:i=<count> } } for the chosen
* pairs and aced_units_hidden for the rest, counting the vehicles a crew
* is an ace on (spec 2)
* atomic_ace, peacemaker, tank_footballer nothing
*/
import type { BlkBlock, BlkParam } from '@wto/blk';
import { allCrews } from '../profile/profile-crews.ts';
import { trainedPairs } from '../profile/profile-crew-skills.ts';
import { currentShowcase, savedShowcases, writeShowcase } from '../profile/profile-showcase.ts';
import { ACCEPTED, refused, type RuleOutcome } from '../research/rule-outcome.ts';
import type { TechTree } from '../research/tech-tree.ts';
import type { VehicleType } from '../research/vehicle-types.ts';
const ACE_OF_SPADES = 'ace_of_spades';
const ACED_FILTER = 'acedUnitsFilter';
const ACE = 2;
/** Request parameter -> stored field, per showcase type. */
const STORED_FIELDS: Readonly<Record<string, Readonly<Record<string, string>>>> = {
favorite_mode: { favoriteMode: 'mode' },
battle_hardened: { battleHardenedMode: 'h_mode' },
favorite_unit: { favoriteUnit: 'unit', favoriteUnitDifficulty: 'difficulty' },
unit_collector: { unitCollectorFavorites: 'units' },
medalist: { medals: 'medals' },
achievement_hunter: { achievements: 'achievements' },
[ACE_OF_SPADES]: {},
atomic_ace: {},
peacemaker: {},
tank_footballer: {},
};
/**
* Fields every answer carries for a type even before the player saved it: the client keeps each
* type's block as a table and assigns some fields with `=`, which fails on a missing slot
* (favorite_unit's saveUnit does `showcase.unit = <unit>`; the unit picker broke on it).
*/
const DEFAULT_FIELDS: Readonly<Record<string, readonly BlkParam[]>> = {
favorite_unit: [
{ name: 'unit', value: { type: 'string', value: '' } },
{ name: 'difficulty', value: { type: 'string', value: 'arcade' } },
],
};
/** The client's unit type names (scripts unit/unitTypesList.nut). */
const SHOWCASE_UNIT_TYPE: Partial<Record<VehicleType, string>> = {
Aircraft: 'aircraft', Tank: 'tank', Ship: 'ship', Helicopter: 'helicopter', Boat: 'boat',
};
export function saveShowcase(profile: BlkBlock, request: BlkBlock): RuleOutcome {
const typeParam = request.params.find((param) => param.name === 'showcaseType')?.value;
const type = typeParam?.type === 'string' ? typeParam.value : undefined;
const fields = type === undefined ? undefined : STORED_FIELDS[type];
if (type === undefined || fields === undefined) return refused('BAD_PARAMS');
const stored = request.params.flatMap((param): BlkParam[] => {
const field = fields[param.name];
return field ? [{ name: field, value: param.value }] : [];
});
writeShowcase(profile, type, stored);
if (type === ACE_OF_SPADES) {
const filter = request.blocks.find((block) => block.name === ACED_FILTER);
const saved = savedShowcases(profile).find((block) => block.name === ACE_OF_SPADES);
// An empty filter is still a saved choice (show nothing), unlike no filter at all.
if (saved) saved.blocks = [filter ? { ...filter, blocks: [] } : { name: ACED_FILTER, params: [], blocks: [] }];
}
return ACCEPTED;
}
/** The cln_get_showcases answer: current:t and one block per showcase type, saved or not. */
export function showcasesAnswer(profile: BlkBlock, techTree: TechTree): BlkBlock {
return {
name: '',
params: [{ name: 'current', value: { type: 'string', value: currentShowcase(profile) } }],
blocks: Object.keys(STORED_FIELDS).map((type) => clientView(profile, techTree, type)),
};
}
/** The current showcase for cln_get_users_terse_info: its type and its block named "showcase". */
export function currentShowcaseView(profile: BlkBlock, techTree: TechTree): { type: string; showcase: BlkBlock } {
const type = currentShowcase(profile);
return { type, showcase: { ...clientView(profile, techTree, type), name: 'showcase' } };
}
function clientView(profile: BlkBlock, techTree: TechTree, type: string): BlkBlock {
const saved = savedShowcases(profile).find((block) => block.name === type);
if (type !== ACE_OF_SPADES) {
const params = saved?.params ?? [];
const defaults = (DEFAULT_FIELDS[type] ?? []).filter((field) => !params.some((param) => param.name === field.name));
return { name: type, params: [...defaults, ...params], blocks: [] };
}
// Before the player saves a filter, every category with an ace is shown.
const filter = saved?.blocks.find((block) => block.name === ACED_FILTER);
const chosen = filter && new Set(filter.params
.flatMap((param) => param.value.type === 'string' ? [`${param.value.value}/${param.name.replace(/^country_/, '')}`] : []));
const shown = new Map<string, Map<string, number>>();
const hidden = new Map<string, Map<string, number>>();
for (const [key, count] of acedVehicleCounts(profile, techTree)) {
const [unitType, country] = key.split('/') as [string, string];
const target = !chosen || chosen.has(key) ? shown : hidden;
let byCountry = target.get(unitType);
if (!byCountry) target.set(unitType, byCountry = new Map());
byCountry.set(country, count);
}
return { name: type, params: [], blocks: [countBlock('aced_units', shown), countBlock('aced_units_hidden', hidden)] };
}
/** "<unit type>/<country without the country_ prefix>" -> vehicles a crew is an ace on. */
function acedVehicleCounts(profile: BlkBlock, techTree: TechTree): Map<string, number> {
const aced = new Set<string>();
for (const crew of allCrews(profile)) {
for (const pair of trainedPairs(crew)) if (pair.spec >= ACE) aced.add(pair.vehicle);
}
const counts = new Map<string, number>();
for (const name of aced) {
const vehicle = techTree.vehicle(name);
const type = vehicle && SHOWCASE_UNIT_TYPE[vehicle.type];
if (!vehicle || !type) continue;
const key = `${type}/${vehicle.country.replace(/^country_/, '')}`;
counts.set(key, (counts.get(key) ?? 0) + 1);
}
return counts;
}
function countBlock(name: string, counts: Map<string, Map<string, number>>): BlkBlock {
return {
name,
params: [],
blocks: [...counts].map(([type, byCountry]) => ({
name: type,
params: [...byCountry].map(([country, count]): BlkParam => ({ name: country, value: { type: 'int', value: count } })),
blocks: [],
})),
};
}