/** * 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 --data * --player [--silver ] [--gold ] [--gjn ] [--free-rp ] * [--squadron-rp ] [--give-item [:]]... [--give-coupon [:]]... */ 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, setGjn, 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 type { ResearchRules } from './research/research-rules.ts'; import type { BlkBlock } from '@wto/blk'; const USAGE = 'usage: save-cli.ts --game --data --player ' + '[--silver ] [--gold ] [--gjn ] [--free-rp ] [--squadron-rp ] ' + '[--give-item [:]]... [--give-coupon [:]]...'; 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 clans = new ClanStore(values.data); const gameFiles = new GameFiles(values.game); const datasets = new DatasetCache(values.data, gameFiles, () => {}); const { profiles, research } = setUpProfiles(values.data, gameFiles, datasets, { 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, research: ResearchRules, ): ParsedEdits | undefined { const silver = optionalAmount(values.silver as string | undefined); const gold = optionalAmount(values.gold as string | undefined); const gjn = optionalAmount(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, --gjn, --free-rp and --squadron-rp take a whole number of 0 or more'); 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) setGjn(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, ${balance.gjn} 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(', ')}`); } 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();