Use the native embedded browser and pass the game language through auto_local. Share web assets and extract vehicle icons, flags, and localized names from the installed client. Load and validate per-domain JSONC tuning and merge local overrides by field. Use official store USD prices for GJN products, keep GJN balances in integer cents, and split the native and web store handlers by protocol. Multi-vehicle packs are still listed as single vehicles at this point. Type checking, patch checks, and configuration loading passed. In-game acceptance is pending.
169 lines
8.3 KiB
TypeScript
169 lines
8.3 KiB
TypeScript
/**
|
|
* Development entry point for editing a save (the save editor of docs/implementation notes
|
|
* section A; the launcher will call the same profile functions).
|
|
*
|
|
* node packages/local-service/src/save-cli.ts --game <game folder> --data <data folder>
|
|
* --player <login or user id> [--silver <n>] [--gold <n>] [--gjn <n>] [--free-rp <n>]
|
|
* [--squadron-rp <n>] [--give-item <item>[:<pieces>]]... [--give-coupon <itemdefId>[:<qty>]]...
|
|
*/
|
|
import { parseArgs } from 'node:util';
|
|
import { AccountStore, type Account } from './accounts/account-store.ts';
|
|
import { ClanStore } from './clans/clan-store.ts';
|
|
import { DatasetCache } from './game-data/datasets/dataset-cache.ts';
|
|
import type { ItemDefinition } from './game-data/datasets/item-catalogue.ts';
|
|
import { GameFiles } from './game-data/game-files.ts';
|
|
import { grantItem } from './items/item-grants.ts';
|
|
import { availableSquadronRp, clanMembershipOf, setAvailableSquadronRp } from './profile/profile-clan.ts';
|
|
import { balanceOf, setGjnCents, setGoldenEagles, setSilverLions } from './profile/profile-currency.ts';
|
|
import { remainingPremiumDays } from './profile/profile-entitlements.ts';
|
|
import { externalInventoryItems, grantExternalItem } from './profile/profile-external-inventory.ts';
|
|
import { ownedItems } from './profile/profile-items.ts';
|
|
import { freeResearchPoints, setFreeResearchPoints } from './profile/profile-research-points.ts';
|
|
import { serverTimeNow } from './profile/server-time.ts';
|
|
import { setUpProfiles } from './profiles-setup.ts';
|
|
import { gjnText } from './store/redeem-codes.ts';
|
|
import { loadTuning } from './tuning/tuning.ts';
|
|
import type { ResearchRules } from './research/research-rules.ts';
|
|
import type { BlkBlock } from '@wto/blk';
|
|
|
|
const USAGE = 'usage: save-cli.ts --game <game folder> --data <data folder> --player <login or user id> '
|
|
+ '[--silver <n>] [--gold <n>] [--gjn <n>] [--free-rp <n>] [--squadron-rp <n>] '
|
|
+ '[--give-item <item>[:<pieces>]]... [--give-coupon <itemdefId>[:<qty>]]...';
|
|
|
|
interface ParsedEdits {
|
|
readonly silver: number | undefined;
|
|
readonly gold: number | undefined;
|
|
readonly gjn: number | undefined;
|
|
readonly freeRp: number | undefined;
|
|
readonly squadronRp: number | undefined;
|
|
readonly gifts: ReadonlyArray<{ readonly item: ItemDefinition; readonly pieces: number }>;
|
|
readonly coupons: ReadonlyArray<{ readonly itemdefId: number; readonly quantity: number }>;
|
|
readonly hasChanges: boolean;
|
|
}
|
|
|
|
function main(): number {
|
|
const { values } = parseArgs({
|
|
options: {
|
|
game: { type: 'string' },
|
|
data: { type: 'string' },
|
|
player: { type: 'string' },
|
|
silver: { type: 'string' },
|
|
gold: { type: 'string' },
|
|
gjn: { type: 'string' },
|
|
'free-rp': { type: 'string' },
|
|
'squadron-rp': { type: 'string' },
|
|
'give-item': { type: 'string', multiple: true },
|
|
'give-coupon': { type: 'string', multiple: true },
|
|
},
|
|
});
|
|
if (!values.game || !values.data || !values.player) {
|
|
console.error(USAGE);
|
|
return 2;
|
|
}
|
|
|
|
const accounts = new AccountStore(values.data);
|
|
const account = accounts.byLogin(values.player) ?? accounts.byUserId(values.player);
|
|
if (!account) {
|
|
console.error(`no player "${values.player}" in ${values.data}`);
|
|
return 1;
|
|
}
|
|
|
|
const tuning = loadTuning(values.data, console.log);
|
|
const clans = new ClanStore(values.data, tuning.clans);
|
|
const gameFiles = new GameFiles(values.game);
|
|
const datasets = new DatasetCache(values.data, gameFiles, () => {});
|
|
const { profiles, research } = setUpProfiles(values.data, gameFiles, datasets, tuning, { accounts, clans });
|
|
|
|
const edits = parseRequestedEdits(values, research);
|
|
if (!edits) return 2;
|
|
|
|
const now = serverTimeNow();
|
|
const clanEarned = clans.earnedSquadronExpOf(account.userId);
|
|
const profile = !edits.hasChanges
|
|
? profiles.profileOf(account.userId)
|
|
: profiles.update(account.userId, (saved) => applyEdits(saved, edits, clanEarned, now));
|
|
|
|
printSaveSummary(account, profile, clanEarned, now);
|
|
return 0;
|
|
}
|
|
|
|
function parseRequestedEdits(
|
|
values: Record<string, string | string[] | undefined>,
|
|
research: ResearchRules,
|
|
): ParsedEdits | undefined {
|
|
const silver = optionalAmount(values.silver as string | undefined);
|
|
const gold = optionalAmount(values.gold as string | undefined);
|
|
const gjn = optionalGjnCents(values.gjn as string | undefined);
|
|
const freeRp = optionalAmount(values['free-rp'] as string | undefined);
|
|
const squadronRp = optionalAmount(values['squadron-rp'] as string | undefined);
|
|
if (silver === null || gold === null || gjn === null || freeRp === null || squadronRp === null) {
|
|
console.error('--silver, --gold, --free-rp and --squadron-rp take a whole number of 0 or more; --gjn an amount of 0 or more with at most two decimals');
|
|
return undefined;
|
|
}
|
|
|
|
const gifts: Array<{ item: ItemDefinition; pieces: number }> = [];
|
|
for (const request of (values['give-item'] as string[] | undefined) ?? []) {
|
|
const [name = '', piecesText = '1'] = request.split(':');
|
|
const item = research.items.item(name);
|
|
const pieces = optionalAmount(piecesText);
|
|
if (!item || !pieces) {
|
|
console.error(`--give-item: no classic item "${name}", or pieces not a whole number of 1 or more`);
|
|
return undefined;
|
|
}
|
|
gifts.push({ item, pieces });
|
|
}
|
|
|
|
const coupons: Array<{ itemdefId: number; quantity: number }> = [];
|
|
for (const request of (values['give-coupon'] as string[] | undefined) ?? []) {
|
|
const [idText = '', qtyText = '1'] = request.split(':');
|
|
const itemdefId = optionalAmount(idText);
|
|
const quantity = optionalAmount(qtyText);
|
|
if (!itemdefId || !quantity || !research.inventory.hasItemDef(itemdefId)) {
|
|
console.error(`--give-coupon: unknown itemdef id "${idText}", or quantity not a whole number of 1 or more`);
|
|
return undefined;
|
|
}
|
|
coupons.push({ itemdefId, quantity });
|
|
}
|
|
|
|
const hasChanges = silver !== undefined || gold !== undefined || gjn !== undefined || freeRp !== undefined || squadronRp !== undefined || gifts.length > 0 || coupons.length > 0;
|
|
return { silver, gold, gjn, freeRp, squadronRp, gifts, coupons, hasChanges };
|
|
}
|
|
|
|
function applyEdits(saved: BlkBlock, edits: ParsedEdits, clanEarned: number, now: bigint): void {
|
|
if (edits.silver !== undefined) setSilverLions(saved, edits.silver);
|
|
if (edits.gold !== undefined) setGoldenEagles(saved, edits.gold);
|
|
if (edits.gjn !== undefined) setGjnCents(saved, edits.gjn);
|
|
if (edits.freeRp !== undefined) setFreeResearchPoints(saved, edits.freeRp);
|
|
if (edits.squadronRp !== undefined) setAvailableSquadronRp(saved, edits.squadronRp, clanEarned);
|
|
for (const gift of edits.gifts) grantItem(saved, gift.item, gift.pieces, now);
|
|
for (const coupon of edits.coupons) grantExternalItem(saved, coupon.itemdefId, coupon.quantity);
|
|
}
|
|
|
|
function printSaveSummary(account: Account, profile: BlkBlock, clanEarned: number, now: bigint): void {
|
|
const balance = balanceOf(profile);
|
|
const premiumDays = remainingPremiumDays(profile, now);
|
|
const premiumNote = premiumDays > 0 ? `, premium: ${premiumDays}d` : '';
|
|
const sqRp = availableSquadronRp(profile, clanEarned);
|
|
const clanInfo = clanMembershipOf(profile);
|
|
const clanNote = clanInfo.clanId !== '-1' ? `, clan: ${clanInfo.clanTag} ${clanInfo.clanName}` : '';
|
|
console.log(`${account.login} (${account.userId}): ${balance.silverLions} silver lions, ${balance.goldenEagles} golden eagles, ${gjnText(balance.gjnCents)} GJN, ${freeResearchPoints(profile)} free research points, ${sqRp} squadron RP${premiumNote}${clanNote}`);
|
|
const items = ownedItems(profile);
|
|
if (items.length) console.log(`items: ${items.map((item) => `${item.id} x${item.count}`).join(', ')}`);
|
|
const extItems = externalInventoryItems(profile);
|
|
if (extItems.length) console.log(`extInventory: ${extItems.map((item) => `#${item.itemDefId} x${item.quantity}`).join(', ')}`);
|
|
}
|
|
|
|
/** --gjn: GJN with at most two decimals, returned in hundredths. */
|
|
function optionalGjnCents(text: string | undefined): number | undefined | null {
|
|
if (text === undefined) return undefined;
|
|
const match = /^(\d+)(?:\.(\d{1,2}))?$/.exec(text);
|
|
return match ? Number(match[1]) * 100 + Number((match[2] ?? '').padEnd(2, '0')) : null;
|
|
}
|
|
|
|
function optionalAmount(text: string | undefined): number | undefined | null {
|
|
if (text === undefined) return undefined;
|
|
return /^\d+$/.test(text) && Number.isSafeInteger(Number(text)) ? Number(text) : null;
|
|
}
|
|
|
|
process.exitCode = main();
|