Support purchasing cosmetics
Handle cln_buy_resource, persist ownership in resourcesInfo, and expose the prices of skins, decals, and attachments.
This commit is contained in:
1 parent
c6c8f85ddd
commit
391082278f
8 files changed
+178
-6
No files matched your search
@@ -6,6 +6,7 @@ import type { CharActionHandler } from '../char-action.ts';
|
||||
import { acceptEula } from './agreement-actions.ts';
|
||||
import { bulkTrainCrews, trainCrew } from './crew-actions.ts';
|
||||
import { buyAllModificationsAction, buyModificationAction, buySparesAction, enableModificationsAction } from './modification-actions.ts';
|
||||
import { buyResourceAction } from './decor-actions.ts';
|
||||
import { saveDecorLayoutsAction } from './decor-layout-actions.ts';
|
||||
import { entitlementsFull, initialMeta, legacyUserData } from './online-init-actions.ts';
|
||||
import { priceEx } from './price-actions.ts';
|
||||
@@ -38,6 +39,7 @@ export function actionTable(deps: ActionDependencies): ReadonlyMap<string, CharA
|
||||
['cln_buy_all_modification', buyAllModificationsAction(deps.profiles, deps.accounts, deps.research)],
|
||||
['cln_buy_spare_aircrafts', buySparesAction(deps.profiles, deps.accounts, deps.research)],
|
||||
['cln_enable_modifications', enableModificationsAction(deps.profiles, deps.accounts)],
|
||||
['cln_buy_resource', buyResourceAction(deps.profiles, deps.accounts, deps.research)],
|
||||
['cln_save_decals', saveDecorLayoutsAction(deps.profiles, deps.accounts, 'decals')],
|
||||
['cln_save_attachables', saveDecorLayoutsAction(deps.profiles, deps.accounts, 'attachables')],
|
||||
]);
|
||||
|
||||
@@ -0,0 +1,28 @@
|
||||
/**
|
||||
* Buying decorations. cln_buy_resource is sent with char_send_blk (job
|
||||
* type 13), so the answer is the whole profile.
|
||||
*
|
||||
* cln_buy_resource (scripts customization/decoratorAcquire.nut buyResourceImpl):
|
||||
* name:t the skin, decal or attachable
|
||||
* type:t skin | decal | attachable
|
||||
* unitName:t the vehicle (skins)
|
||||
* cost:i costGold:i
|
||||
*/
|
||||
import type { AccountStore } from '../../accounts/account-store.ts';
|
||||
import { buyDecoration, decorKey, isDecorKind } from '../../decor/decor-purchase.ts';
|
||||
import type { PlayerProfiles } from '../../profile/player-profiles.ts';
|
||||
import type { ResearchRules } from '../../research/research-rules.ts';
|
||||
import type { CharActionHandler } from '../char-action.ts';
|
||||
import { intParam, stringParam } from './payload-params.ts';
|
||||
import { ruleAction } from './rule-answer.ts';
|
||||
|
||||
export function buyResourceAction(profiles: PlayerProfiles, accounts: AccountStore, rules: ResearchRules): CharActionHandler {
|
||||
return ruleAction(profiles, accounts, (payload) => {
|
||||
const name = stringParam(payload, 'name');
|
||||
const type = stringParam(payload, 'type');
|
||||
if (name === undefined || type === undefined || !isDecorKind(type)) return undefined;
|
||||
const key = decorKey(type, name, stringParam(payload, 'unitName') ?? '');
|
||||
const offered = { silverLions: intParam(payload, 'cost') ?? 0, goldenEagles: intParam(payload, 'costGold') ?? 0 };
|
||||
return (profile) => buyDecoration(profile, rules.decor, type, key, offered);
|
||||
});
|
||||
}
|
||||
@@ -9,6 +9,6 @@ import { priceTable } from '../../research/price-table.ts';
|
||||
import type { ResearchRules } from '../../research/research-rules.ts';
|
||||
|
||||
export function priceEx(rules: ResearchRules): CharActionHandler {
|
||||
const table = priceTable(rules.techTree, rules.equipment);
|
||||
const table = priceTable(rules.techTree, rules.equipment, rules.decor);
|
||||
return () => ({ kind: 'data', blk: table });
|
||||
}
|
||||
@@ -0,0 +1,48 @@
|
||||
/**
|
||||
* Buying decorations: cln_buy_resource with name:t, type:t (skin, decal,
|
||||
* attachable), unitName:t, cost:i, costGold:i (scripts customization/decoratorAcquire.nut).
|
||||
* A skin's key is "<unitName>/<name>". The decoration must be for sale (a positive price in
|
||||
* config/skins.blk, decals.blk or attachable.blk), not owned yet, the offered price must match;
|
||||
* it then goes into resourcesInfo.
|
||||
*/
|
||||
import type { BlkBlock } from '@wto/blk';
|
||||
import type { DecorKind, DecorPrice } from '../game-data/datasets/decor-catalogue.ts';
|
||||
import { canAfford, pay, type Price } from '../profile/profile-currency.ts';
|
||||
import { addResource, ownsResource } from '../profile/profile-resources.ts';
|
||||
import { ACCEPTED, refused, type RuleOutcome } from '../research/rule-outcome.ts';
|
||||
|
||||
export class DecorShop {
|
||||
readonly #prices: ReadonlyMap<string, DecorPrice>;
|
||||
|
||||
constructor(prices: readonly DecorPrice[]) {
|
||||
this.#prices = new Map(prices.map((price) => [`${price.kind}:${price.key}`, price]));
|
||||
}
|
||||
|
||||
all(): Iterable<DecorPrice> {
|
||||
return this.#prices.values();
|
||||
}
|
||||
|
||||
price(kind: DecorKind, key: string): DecorPrice | undefined {
|
||||
return this.#prices.get(`${kind}:${key}`);
|
||||
}
|
||||
}
|
||||
|
||||
export function decorKey(kind: DecorKind, name: string, vehicle: string): string {
|
||||
return kind === 'skin' ? `${vehicle}/${name}` : name;
|
||||
}
|
||||
|
||||
export function isDecorKind(type: string): type is DecorKind {
|
||||
return type === 'skin' || type === 'decal' || type === 'attachable';
|
||||
}
|
||||
|
||||
export function buyDecoration(profile: BlkBlock, shop: DecorShop, kind: DecorKind, key: string, offered: Price): RuleOutcome {
|
||||
const forSale = shop.price(kind, key);
|
||||
if (!forSale) return refused('NOT_AVAILABLE');
|
||||
if (ownsResource(profile, key)) return refused('NOT_AVAILABLE');
|
||||
const price: Price = { silverLions: forSale.silverLions, goldenEagles: forSale.goldenEagles };
|
||||
if (price.silverLions !== offered.silverLions || price.goldenEagles !== offered.goldenEagles) return refused('PRICE_CHANGED');
|
||||
if (!canAfford(profile, price)) return refused('NOT_ENOUGH_MONEY');
|
||||
pay(profile, price);
|
||||
addResource(profile, key, price);
|
||||
return ACCEPTED;
|
||||
}
|
||||
@@ -0,0 +1,44 @@
|
||||
/**
|
||||
* Dataset: the decorations that can be bought.
|
||||
*
|
||||
* char.vromfs.bin config/skins.blk, config/decals.blk, config/attachable.blk: one block per
|
||||
* decoration, named like the client keys it (skins "<vehicle>/<skin>", decals and attachables by
|
||||
* name), with
|
||||
* cost:i silver lions (missing = -1)
|
||||
* costGold:i golden eagles (missing = -1; -1 means not for sale)
|
||||
* unlock:t an unlock that also gives it
|
||||
* Only decorations with a positive price are recorded: the others are either free (the client
|
||||
* counts them as owned, aces.exe 0x95e1c5) or not for sale.
|
||||
*/
|
||||
import type { BlkBlock } from '@wto/blk';
|
||||
import { numberField } from './blk-fields.ts';
|
||||
import type { Dataset } from './dataset.ts';
|
||||
|
||||
export type DecorKind = 'skin' | 'decal' | 'attachable';
|
||||
|
||||
export interface DecorPrice {
|
||||
readonly kind: DecorKind;
|
||||
/** The client's key: "<vehicle>/<skin>" for skins, the name otherwise. */
|
||||
readonly key: string;
|
||||
readonly silverLions: number;
|
||||
readonly goldenEagles: number;
|
||||
}
|
||||
|
||||
const SOURCES: readonly { kind: DecorKind; path: string }[] = [
|
||||
{ kind: 'skin', path: 'config/skins.blk' },
|
||||
{ kind: 'decal', path: 'config/decals.blk' },
|
||||
{ kind: 'attachable', path: 'config/attachable.blk' },
|
||||
];
|
||||
|
||||
export const decorCatalogue: Dataset<readonly DecorPrice[]> = {
|
||||
id: 'decor-catalogue',
|
||||
version: 1,
|
||||
extract: (source) => SOURCES.flatMap(({ kind, path }) =>
|
||||
source.readBlk('char.vromfs.bin', path).blocks.flatMap((entry) => priceOf(kind, entry))),
|
||||
};
|
||||
|
||||
function priceOf(kind: DecorKind, entry: BlkBlock): DecorPrice[] {
|
||||
const silverLions = Math.max(0, numberField(entry, 'cost') ?? 0);
|
||||
const goldenEagles = Math.max(0, numberField(entry, 'costGold') ?? 0);
|
||||
return silverLions > 0 || goldenEagles > 0 ? [{ kind, key: entry.name, silverLions, goldenEagles }] : [];
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
/**
|
||||
* Bought decorations in the profile:
|
||||
*
|
||||
* resourcesInfo{ core{ "us_m22_locust/us_camo_olive"{ spendGold:i=200 } cow_skull{ spendGold:i=500 } } }
|
||||
*
|
||||
* A generic name-keyed storage (FastStrMapStorageBase, aces.exe 0x93da88): one block per bought
|
||||
* decoration under core, named by the client's key; having a block means owned (player_have_skin,
|
||||
* player_have_decal, player_have_attachable -> 0x95dfc0). Each entry's params are read by name
|
||||
* (0x95eaa0): flags:i, spendWP:i, spendGold:i, exteriorInfoParams.
|
||||
*/
|
||||
import { ensureBlock, findBlock, setParam, type BlkBlock } from '@wto/blk';
|
||||
|
||||
const RESOURCES = 'resourcesInfo';
|
||||
const CORE = 'core';
|
||||
|
||||
export function ownsResource(profile: BlkBlock, key: string): boolean {
|
||||
const resources = findBlock(profile, RESOURCES);
|
||||
const core = resources && findBlock(resources, CORE);
|
||||
return core !== undefined && findBlock(core, key) !== undefined;
|
||||
}
|
||||
|
||||
export function addResource(profile: BlkBlock, key: string, spent: { silverLions: number; goldenEagles: number }): void {
|
||||
const entry = ensureBlock(ensureBlock(ensureBlock(profile, RESOURCES), CORE), key);
|
||||
if (spent.silverLions) setParam(entry, 'spendWP', { type: 'int', value: spent.silverLions });
|
||||
if (spent.goldenEagles) setParam(entry, 'spendGold', { type: 'int', value: spent.goldenEagles });
|
||||
}
|
||||
@@ -18,22 +18,27 @@
|
||||
* spare{ cost:i costGold:i }
|
||||
* }
|
||||
* }
|
||||
* skins{ "<vehicle>/<skin>"{ cost:i costGold:i } } decorations (get_skin_cost_*, 0x17a8910...)
|
||||
* decals{ <decal>{ ... } }
|
||||
* attachable{ <attachable>{ ... } }
|
||||
* }
|
||||
*
|
||||
* Every entry is read as cost:i (silver lions), costGold:i (golden eagles), discount:i (percent
|
||||
* off, not sent: no discounts), each 0 when missing (so zero prices are left out); openCostGold
|
||||
* is the outright price of a modification before research progress (0xec0910). Anything the lookup does not find costs 0 for the client, which
|
||||
* then offers it at 0 (lab round 20). The prices are config/wpcost.blk value and costGold, the
|
||||
* same the purchase rules charge.
|
||||
* is the outright price of a modification before research progress (0xec0910). Anything the
|
||||
* lookup does not find costs 0 for the client, which then offers it at 0 (lab round 20). The
|
||||
* prices are config/wpcost.blk value and costGold (decorations: config/skins.blk, decals.blk,
|
||||
* attachable.blk), the same the purchase rules charge.
|
||||
*/
|
||||
import type { BlkBlock, BlkParam } from '@wto/blk';
|
||||
import type { DecorShop } from '../decor/decor-purchase.ts';
|
||||
import type { EquipmentCatalogue } from '../modifications/equipment-catalogue.ts';
|
||||
import type { TechTree } from './tech-tree.ts';
|
||||
|
||||
/** The top-level block every client lookup falls back to (0x908e28). */
|
||||
const COMMON_SECTION = 'Common';
|
||||
|
||||
export function priceTable(techTree: TechTree, equipment: EquipmentCatalogue): BlkBlock {
|
||||
export function priceTable(techTree: TechTree, equipment: EquipmentCatalogue, decor: DecorShop): BlkBlock {
|
||||
const vehicles: BlkBlock[] = [];
|
||||
for (const vehicle of techTree.all()) {
|
||||
const entry: BlkBlock = { name: vehicle.name, params: priceParams(vehicle.silverLions, vehicle.goldenEagles), blocks: [] };
|
||||
@@ -52,7 +57,7 @@ export function priceTable(techTree: TechTree, equipment: EquipmentCatalogue): B
|
||||
}
|
||||
if (entry.blocks.length || vehicle.silverLions > 0 || vehicle.goldenEagles > 0) vehicles.push(entry);
|
||||
}
|
||||
const common: BlkBlock = { name: COMMON_SECTION, params: [], blocks: [{ name: 'aircrafts', params: [], blocks: vehicles }] };
|
||||
const common: BlkBlock = { name: COMMON_SECTION, params: [], blocks: [{ name: 'aircrafts', params: [], blocks: vehicles }, ...decorSections(decor)] };
|
||||
return { name: '', params: [], blocks: [common] };
|
||||
}
|
||||
|
||||
@@ -67,3 +72,17 @@ function priceParams(silverLions: number, goldenEagles: number): BlkParam[] {
|
||||
function intParam(name: string, value: number): BlkParam {
|
||||
return { name, value: { type: 'int', value } };
|
||||
}
|
||||
|
||||
/** The price-table section each decoration kind is looked up in. */
|
||||
const DECOR_SECTIONS = { skin: 'skins', decal: 'decals', attachable: 'attachable' } as const;
|
||||
|
||||
function decorSections(decor: DecorShop): BlkBlock[] {
|
||||
const sections = new Map<string, BlkBlock>();
|
||||
for (const price of decor.all()) {
|
||||
const name = DECOR_SECTIONS[price.kind];
|
||||
let section = sections.get(name);
|
||||
if (!section) sections.set(name, section = { name, params: [], blocks: [] });
|
||||
section.blocks.push({ name: price.key, params: priceParams(price.silverLions, price.goldenEagles), blocks: [] });
|
||||
}
|
||||
return [...sections.values()];
|
||||
}
|
||||
@@ -6,7 +6,9 @@ import type { DatasetCache } from '../game-data/datasets/dataset-cache.ts';
|
||||
import { crewSlotPrices, eraRequirements, type CrewSlotPrices, type EraRequirements } from '../game-data/datasets/rank-settings.ts';
|
||||
import { researchPointsPerGoldenEagle } from '../game-data/datasets/research-point-rate.ts';
|
||||
import { vehicleCatalogue } from '../game-data/datasets/vehicle-catalogue.ts';
|
||||
import { decorCatalogue } from '../game-data/datasets/decor-catalogue.ts';
|
||||
import { vehicleEquipment } from '../game-data/datasets/vehicle-equipment.ts';
|
||||
import { DecorShop } from '../decor/decor-purchase.ts';
|
||||
import { vehicleUnlocks, type UnlockDefinition } from '../game-data/datasets/vehicle-unlocks.ts';
|
||||
import { EquipmentCatalogue } from '../modifications/equipment-catalogue.ts';
|
||||
import { TechTree } from './tech-tree.ts';
|
||||
@@ -19,6 +21,8 @@ export interface ResearchRules {
|
||||
readonly researchPointsPerGoldenEagle: number;
|
||||
/** Modifications, ammunition, weapon presets and spares of every vehicle. */
|
||||
readonly equipment: EquipmentCatalogue;
|
||||
/** Decorations for sale. */
|
||||
readonly decor: DecorShop;
|
||||
}
|
||||
|
||||
export function loadResearchRules(datasets: DatasetCache): ResearchRules {
|
||||
@@ -29,5 +33,6 @@ export function loadResearchRules(datasets: DatasetCache): ResearchRules {
|
||||
crewSlotPrices: datasets.load(crewSlotPrices),
|
||||
researchPointsPerGoldenEagle: datasets.load(researchPointsPerGoldenEagle),
|
||||
equipment: new EquipmentCatalogue(datasets.load(vehicleEquipment)),
|
||||
decor: new DecorShop(datasets.load(decorCatalogue)),
|
||||
};
|
||||
}
|
||||
Reference in new issue
Block a user