203 lines
9.6 KiB
TypeScript
203 lines
9.6 KiB
TypeScript
/**
|
|
* Command-line editor for local server saves.
|
|
*
|
|
* node apps/server/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 type { BlkBlock } from '@wto/blk';
|
|
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 { externalItemSources } from './game-data/datasets/external-item-sources.ts';
|
|
import { itemCatalogue, type ItemDefinition } from './game-data/datasets/item-catalogue.ts';
|
|
import { unitNames } from './game-data/datasets/unit-names.ts';
|
|
import { vehicleCatalogue, type CatalogueVehicle } from './game-data/datasets/vehicle-catalogue.ts';
|
|
import { GameFiles } from './game-data/game-files.ts';
|
|
import { InventoryCatalogue } from './inventory/itemdef-catalogue.ts';
|
|
import { ItemCatalogue } from './items/item-catalogue.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, type Tuning } from './tuning/tuning.ts';
|
|
|
|
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 vehicles = datasets.load(vehicleCatalogue);
|
|
const { profiles } = setUpProfiles(values.data, gameFiles, datasets, tuning, { accounts, clans }, vehicles);
|
|
|
|
const edits = parseRequestedEdits(values, datasets, tuning, vehicles);
|
|
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>,
|
|
datasets: DatasetCache,
|
|
tuning: Tuning,
|
|
vehicles: readonly CatalogueVehicle[],
|
|
): 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 itemRequests = (values['give-item'] as string[] | undefined) ?? [];
|
|
const couponRequests = (values['give-coupon'] as string[] | undefined) ?? [];
|
|
const classicItemDefs = (itemRequests.length > 0 || couponRequests.length > 0) ? datasets.load(itemCatalogue) : undefined;
|
|
const gifts = classicItemDefs && itemRequests.length > 0
|
|
? parseItemGifts(itemRequests, new ItemCatalogue(classicItemDefs, tuning.limits.itemPiecesPerPurchase))
|
|
: [];
|
|
if (!gifts) return undefined;
|
|
|
|
const coupons = classicItemDefs && couponRequests.length > 0
|
|
? parseCouponGifts(
|
|
couponRequests,
|
|
new InventoryCatalogue(vehicles, datasets.load(unitNames), datasets.load(externalItemSources), classicItemDefs, tuning),
|
|
)
|
|
: [];
|
|
if (!coupons) return undefined;
|
|
|
|
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 parseItemGifts(
|
|
requests: readonly string[],
|
|
items: ItemCatalogue,
|
|
): Array<{ item: ItemDefinition; pieces: number }> | undefined {
|
|
const gifts: Array<{ item: ItemDefinition; pieces: number }> = [];
|
|
for (const request of requests) {
|
|
const [name = '', piecesText = '1'] = request.split(':');
|
|
const item = 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 });
|
|
}
|
|
return gifts;
|
|
}
|
|
|
|
function parseCouponGifts(
|
|
requests: readonly string[],
|
|
inventory: InventoryCatalogue,
|
|
): Array<{ itemdefId: number; quantity: number }> | undefined {
|
|
const coupons: Array<{ itemdefId: number; quantity: number }> = [];
|
|
for (const request of requests) {
|
|
const [idText = '', qtyText = '1'] = request.split(':');
|
|
const itemdefId = optionalAmount(idText);
|
|
const quantity = optionalAmount(qtyText);
|
|
if (!itemdefId || !quantity || !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 });
|
|
}
|
|
return coupons;
|
|
}
|
|
|
|
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();
|