fix(server): persist squadrons and align inventory wire formats

Read squadron action parameters from the request headers and persist
squadron state on profile refresh. Use floating-point trophy weights
to match the client probability display and assign unique log IDs
when opening multiple chests.

Tighten shop, marketplace, squadron, and cloud settings validation.
Move PBBD and inventory encoding into the protocol modules and share
CSV reading. Update the research and acceptance instructions.

Type checking and all eight executable patch checks passed.
In-game acceptance is pending.
This commit is contained in:
N0VAN0M4LY committed 2026-11-25 09:10:09 +08:00
1 parent b68f321b84
commit 29e5cf2bd8
40 files changed
+1347 -1248

No files matched your search

+70 -42
View File
@@ -4,19 +4,14 @@
*
* 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>]
* [--give-item <item>[:<pieces>]]...
*
* --free-rp sets the free research points. --give-item gives classic
* items from config/items.blk, as a reward would; it can be repeated.
* Without any change it only prints the balance. The edit is written to the save; the game
* shows it after its next profile update (any action, or signing in again).
* [--squadron-rp <n>] [--give-item <item>[:<pieces>]]... [--give-coupon <itemdefId>[:<qty>]]...
*/
import { parseArgs } from 'node:util';
import { AccountStore } from './accounts/account-store.ts';
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 { GameFiles } from './game-data/game-files.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';
@@ -26,11 +21,24 @@ 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 <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: {
@@ -50,63 +58,85 @@ function main(): number {
console.error(USAGE);
return 2;
}
const silver = optionalAmount(values.silver);
const gold = optionalAmount(values.gold);
const gjn = optionalAmount(values.gjn);
const freeRp = optionalAmount(values['free-rp']);
const squadronRp = optionalAmount(values['squadron-rp']);
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 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);
const gifts: { item: ItemDefinition; pieces: number }[] = [];
for (const request of values['give-item'] ?? []) {
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<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 = 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 2;
return undefined;
}
gifts.push({ item, pieces });
}
const coupons: { itemdefId: number; quantity: number }[] = [];
for (const request of values['give-coupon'] ?? []) {
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 2;
return undefined;
}
coupons.push({ itemdefId, quantity });
}
const changes = silver !== undefined || gold !== undefined || gjn !== undefined || freeRp !== undefined || squadronRp !== undefined || gifts.length > 0 || coupons.length > 0;
const now = serverTimeNow();
const clanEarned = clans.earnedSquadronExpOf(account.userId);
const profile = !changes
? profiles.profileOf(account.userId)
: profiles.update(account.userId, (saved) => {
clans.syncProfile(saved, account);
if (silver !== undefined) setSilverLions(saved, silver);
if (gold !== undefined) setGoldenEagles(saved, gold);
if (gjn !== undefined) setGjn(saved, gjn);
if (freeRp !== undefined) setFreeResearchPoints(saved, freeRp);
if (squadronRp !== undefined) setAvailableSquadronRp(saved, squadronRp, clanEarned);
for (const gift of gifts) grantItem(saved, gift.item, gift.pieces, now);
for (const coupon of coupons) grantExternalItem(saved, coupon.itemdefId, coupon.quantity);
});
clans.syncProfile(profile, account);
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` : '';
@@ -118,10 +148,8 @@ function main(): number {
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(', ')}`);
return 0;
}
/** undefined when not given, null when not a valid amount. */
function optionalAmount(text: string | undefined): number | undefined | null {
if (text === undefined) return undefined;
return /^\d+$/.test(text) && Number.isSafeInteger(Number(text)) ? Number(text) : null;