refactor(repo)!: move apps, shared packages, and patch definitions
Everything sat in packages/: two programs next to the small libraries
they use, and the patch data loose at the root. The programs now live in
apps/ and the shared libraries in packages/:
apps/server the local server (was packages/local-service),
package @wto/server
packages/patcher builds the game copy (was packages/build),
package @wto/patcher
patches/exe exe patch definitions (was exe-patches)
patches/gui GUI script patches (was gui-patches)
Inside the server, the network layer folder becomes transport/ so it is
not server/server/, the entry module is server.ts, and startLocalService
and its types become startServer, ServerOptions and RunningServer. The
health answer names the app wto-server, and the TLS files in the data
folder are server.key and server.crt (made again on the next start).
Comments, logs and tool texts say "local server".
The patcher no longer reaches into the server's folder. It read the
default tuning/store.jsonc by a relative path to register bundle GUIDs
for the multi-vehicle packs, which also ignored the player's overrides
in <data>/tuning/. It now takes the pack ids from its caller; the
server exports multiVehiclePackIds(dataFolder), which reads them the way
the running server does. rebuildGuiPack takes the patch folder from its
caller as well.
The development build moves out of the patcher to
scripts/build-game-copy.ts, since it uses both the server and the
patcher. It needs --data (the folder the server will run with) and
defaults the patch folders and the hook library to this repository.
Checked with the root typecheck, check-patches, check-gui,
check-offsets, the native build, a run of build-game-copy.ts into a
scratch folder (12 exe patches, 35 GUI patches, 496 bundle GUIDs as
before) and a server start from apps/server answering wto-server.
BREAKING CHANGE: Use @wto/server and @wto/patcher from the new workspace paths. Run the development build with scripts/build-game-copy.ts and pass --data.
This commit is contained in:
1 parent
9c813ccf7d
commit
930ea07630
476 files changed
+275
-251
No files matched your search
@@ -0,0 +1,203 @@
|
||||
/**
|
||||
* 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 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();
|
||||
Reference in new issue
Block a user