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:
N0VAN0M4LY committed 2026-11-29 03:20:54 +08:00
1 parent 9c813ccf7d
commit 930ea07630
476 files changed
+275 -251

No files matched your search

@@ -0,0 +1,156 @@
/**
* Initial full stocking and `autoRefillWeapons` Silver-Lion top-up for an owned vehicle's
* modifications, ammunition belts/shells, expendables (`tank_medical_kit_expendable`), and
* secondary weapon presets.
*
* Rules:
* 1. Zero-cost modifications (`isFree`) whose `requires` are all owned cannot be bought by the
* client (`weaponsPurchase.nut` `canBuyItem` rejects zero cost) and receive `count = 1` in
* name + topological order, honoring `defaultEnabled` and non-bullet `exclusiveGroup` mutual
* exclusion.
* 2. Free secondary weapon presets (`isFreeWeapon`, excluding `isWeaponForCustomSlot` containers)
* whose `requires` (direct + `sum_weapons` container inheritance) are all owned receive
* `count = 1` so `shop_is_weapon_purchased` allows selecting them.
* 3. Silver-Lion counted modifications (`isCounted` with `goldenEagles === 0`, covering all
* 12,800 shells, gun belts, and `tank_medical_kit_expendable`) and paid weapon presets
* (`isPaidWeapon` with `goldenEagles === 0`) whose `requires` are all owned:
* - When no profile record exists yet (`record === undefined`, i.e. newly acquired vehicle or
* newly unlocked prerequisite modification), receive their initial full load (`maxCount`)
* at no extra Silver-Lion charge (`itemInfo.nut` `getAllModsCost` skips `maxAmount > 1`).
* - When a record already exists with `count < maxCount` (consumed in battle) and hangar
* `autoRefillWeapons` is enabled, buy as many missing pieces with Silver Lions in
* deterministic name order as the player's balance affords.
*
* Returns the Silver Lions spent on refilling (0 for the initial free stock and when
* autoRefill is off); battle settlement lists it as the automatic refill cost.
*/
import type { BlkBlock } from '@wto/blk';
import type { ModificationFacts, VehicleEquipment, WeaponFacts } from '../game-data/datasets/vehicle-equipment.ts';
import { balanceOf, pay } from '../profile/profile-currency.ts';
import { modificationRecord, ownsModification, writeModificationRecord } from '../profile/profile-modifications.ts';
import { weaponRecord, writeWeaponRecord } from '../profile/profile-weapons.ts';
import { isCounted, isFree, isFreeWeapon, isPaidWeapon } from './equipment-catalogue.ts';
import { arePrerequisitesOwned } from './module-status.ts';
export interface EquipmentRefillResult {
readonly silverLionsSpent: number;
}
export function stockAndRefillEquipment(
profile: BlkBlock,
equipment: VehicleEquipment,
autoRefill: boolean,
): EquipmentRefillResult {
const modifications = [...equipment.modifications].sort((a, b) => a.name.localeCompare(b.name));
const weapons = [...equipment.weapons].sort((a, b) => a.name.localeCompare(b.name));
grantUnlockedFreeModifications(profile, equipment.vehicle, modifications);
grantUnlockedFreeWeapons(profile, equipment.vehicle, weapons);
let silverLionsSpent = 0;
for (const modification of modifications) {
silverLionsSpent += stockOrRefillCountedModification(profile, equipment.vehicle, modification, autoRefill);
}
for (const weapon of weapons) {
silverLionsSpent += stockOrRefillPaidWeapon(profile, equipment.vehicle, weapon, autoRefill);
}
return { silverLionsSpent };
}
function grantUnlockedFreeModifications(profile: BlkBlock, vehicle: string, modifications: readonly ModificationFacts[]): void {
let progressed = false;
do {
progressed = false;
for (const modification of modifications) {
if (!isFree(modification) || ownsModification(profile, vehicle, modification.name)) continue;
if (!arePrerequisitesOwned(profile, vehicle, modification.requires)) continue;
const enabled = modification.defaultEnabled && !hasEnabledExclusivePeer(profile, vehicle, modifications, modification.exclusiveGroup);
writeModificationRecord(profile, vehicle, modification.name, {
silverLionsSpent: 0,
goldenEaglesSpent: 0,
enabled,
count: Math.max(1, modification.maxCount),
});
progressed = true;
}
} while (progressed);
}
function hasEnabledExclusivePeer(
profile: BlkBlock,
vehicle: string,
modifications: readonly ModificationFacts[],
exclusiveGroup: string | undefined,
): boolean {
if (!exclusiveGroup) return false;
return modifications.some((peer) => peer.exclusiveGroup === exclusiveGroup && modificationRecord(profile, vehicle, peer.name)?.enabled === true);
}
function grantUnlockedFreeWeapons(profile: BlkBlock, vehicle: string, weapons: readonly WeaponFacts[]): void {
for (const weapon of weapons) {
if (!isFreeWeapon(weapon) || weaponRecord(profile, vehicle, weapon.name)) continue;
if (!arePrerequisitesOwned(profile, vehicle, weapon.requires)) continue;
writeWeaponRecord(profile, vehicle, weapon.name, { silverLionsSpent: 0, goldenEaglesSpent: 0, count: 1 });
}
}
function stockOrRefillCountedModification(
profile: BlkBlock,
vehicle: string,
modification: ModificationFacts,
autoRefill: boolean,
): number {
if (!isCounted(modification) || modification.goldenEagles > 0) return 0;
if (!arePrerequisitesOwned(profile, vehicle, modification.requires)) return 0;
const record = modificationRecord(profile, vehicle, modification.name);
if (record === undefined) {
writeModificationRecord(profile, vehicle, modification.name, {
silverLionsSpent: 0,
goldenEaglesSpent: 0,
enabled: modification.defaultEnabled,
count: modification.maxCount,
});
return 0;
}
if (!autoRefill || record.count >= modification.maxCount) return 0;
const toBuy = affordableUnits(profile, modification.maxCount - record.count, modification.silverLions);
if (toBuy <= 0) return 0;
const cost = toBuy * modification.silverLions;
if (cost > 0) pay(profile, { silverLions: cost, goldenEagles: 0 });
writeModificationRecord(profile, vehicle, modification.name, {
...record,
silverLionsSpent: record.silverLionsSpent + cost,
count: record.count + toBuy,
});
return cost;
}
function stockOrRefillPaidWeapon(
profile: BlkBlock,
vehicle: string,
weapon: WeaponFacts,
autoRefill: boolean,
): number {
if (!isPaidWeapon(weapon) || weapon.goldenEagles > 0) return 0;
if (!arePrerequisitesOwned(profile, vehicle, weapon.requires)) return 0;
const record = weaponRecord(profile, vehicle, weapon.name);
if (record === undefined) {
writeWeaponRecord(profile, vehicle, weapon.name, { silverLionsSpent: 0, goldenEaglesSpent: 0, count: weapon.maxCount });
return 0;
}
if (!autoRefill || record.count >= weapon.maxCount) return 0;
const toBuy = affordableUnits(profile, weapon.maxCount - record.count, weapon.silverLions);
if (toBuy <= 0) return 0;
const cost = toBuy * weapon.silverLions;
if (cost > 0) pay(profile, { silverLions: cost, goldenEagles: 0 });
writeWeaponRecord(profile, vehicle, weapon.name, {
silverLionsSpent: record.silverLionsSpent + cost,
goldenEaglesSpent: record.goldenEaglesSpent,
count: record.count + toBuy,
});
return cost;
}
function affordableUnits(profile: BlkBlock, missing: number, unitSilverCost: number): number {
if (missing <= 0) return 0;
if (unitSilverCost <= 0) return missing;
return Math.min(missing, Math.floor(balanceOf(profile).silverLions / unitSilverCost));
}