feat(server): add experimental JS/TS mod runtime, hooks, tuning list ops, and isolated mod storage
This commit is contained in:
1 parent
fd9d25d346
commit
aa7a6e7d02
14 files changed
+743
-61
No files matched your search
+149
-34
@@ -1,8 +1,9 @@
|
||||
/**
|
||||
* Assemble the local server: data layer, save layer, rules,
|
||||
* Assemble the local server: data layer, save layer, rules, experimental additive mod runtime,
|
||||
* protocol-layer services, and the single loopback listening port.
|
||||
*/
|
||||
import type { Server } from 'node:net';
|
||||
import type { DiscoveredMod, ServerModDatasetsView } from '@wto/mods';
|
||||
import { AccountStore } from './accounts/account-store.ts';
|
||||
import { RegistrationService } from './accounts/registration-service.ts';
|
||||
import { BattleEconomy } from './battle/battle-economy.ts';
|
||||
@@ -16,7 +17,7 @@ import { CloudSettingsStore } from './cloud-settings/cloud-settings-store.ts';
|
||||
import { CrewSkillCatalogue } from './crew/crew-skill-rules.ts';
|
||||
import { DecorShop } from './decor/decor-purchase.ts';
|
||||
import { DownloadableContentService } from './downloadable-content/downloadable-content-service.ts';
|
||||
import { battleEconomy } from './game-data/datasets/battle-economy.ts';
|
||||
import { battleEconomy, type BattleEconomyData } from './game-data/datasets/battle-economy.ts';
|
||||
import { battleSpawnRules } from './game-data/datasets/battle-spawn-rules.ts';
|
||||
import { researchEfficiency } from './game-data/datasets/research-efficiency.ts';
|
||||
import { ResearchEfficiency } from './research/research-efficiency.ts';
|
||||
@@ -31,11 +32,11 @@ import { decorCatalogue } from './game-data/datasets/decor-catalogue.ts';
|
||||
import { externalItemSources } from './game-data/datasets/external-item-sources.ts';
|
||||
import { flightModelPresets } from './game-data/datasets/flight-model-presets.ts';
|
||||
import { itemCatalogue } from './game-data/datasets/item-catalogue.ts';
|
||||
import { multiplayerMissions } from './game-data/datasets/multiplayer-missions.ts';
|
||||
import { multiplayerMissions, type MultiplayerMissions } from './game-data/datasets/multiplayer-missions.ts';
|
||||
import { shopCountries } from './game-data/datasets/shop-countries.ts';
|
||||
import { storeBundles } from './game-data/datasets/store-bundles.ts';
|
||||
import { uiLabels, type UiLabels } from './game-data/datasets/ui-labels.ts';
|
||||
import { unitNames } from './game-data/datasets/unit-names.ts';
|
||||
import { unitNames, type UnitNamesMap } from './game-data/datasets/unit-names.ts';
|
||||
import { unlockPrices } from './game-data/datasets/unlock-prices.ts';
|
||||
import { vehicleCatalogue, type CatalogueVehicle } from './game-data/datasets/vehicle-catalogue.ts';
|
||||
import { webIcons } from './game-data/datasets/web-icons.ts';
|
||||
@@ -49,6 +50,7 @@ import { ItemCatalogue } from './items/item-catalogue.ts';
|
||||
import { LoginService } from './login/login-service.ts';
|
||||
import { matchingRequests } from './matching/matching-requests.ts';
|
||||
import type { MatchingService } from './matching/matching-service.ts';
|
||||
import { ServerModRuntime } from './mods/server-mod-runtime.ts';
|
||||
import { setUpProfiles } from './profiles-setup.ts';
|
||||
import { createPortListener } from './transport/port-listener.ts';
|
||||
import { Router } from './transport/router.ts';
|
||||
@@ -67,39 +69,100 @@ export interface ServerOptions {
|
||||
/** Accounts, saves and logs. */
|
||||
readonly dataFolder: string;
|
||||
readonly port: number;
|
||||
/** Start-up progress lines (static data extraction, game modes). */
|
||||
/** Start-up progress lines (static data extraction, game modes, mod loading). */
|
||||
readonly report?: (line: string) => void;
|
||||
/** Whether to write traffic logs under `<dataFolder>/logs`; defaults to true. */
|
||||
readonly loggingEnabled?: boolean;
|
||||
/** Ordered list of enabled additive mods to load for this session. */
|
||||
readonly mods?: readonly DiscoveredMod[];
|
||||
}
|
||||
|
||||
export const LOOPBACK = '127.0.0.1';
|
||||
|
||||
export interface RunningServer {
|
||||
readonly logFolder: string;
|
||||
readonly loadedModIds: readonly string[];
|
||||
close(): Promise<void>;
|
||||
}
|
||||
|
||||
export async function startServer(options: ServerOptions): Promise<RunningServer> {
|
||||
const report = options.report ?? (() => {});
|
||||
const enabledMods = options.mods ?? [];
|
||||
const modRuntime = await ServerModRuntime.load({
|
||||
mods: enabledMods,
|
||||
gameFolder: options.gameFolder,
|
||||
dataFolder: options.dataFolder,
|
||||
port: options.port,
|
||||
report,
|
||||
});
|
||||
|
||||
const gameFiles = new GameFiles(options.gameFolder);
|
||||
// Static data and stores first, before the port opens.
|
||||
const datasets = new DatasetCache(options.dataFolder, gameFiles, report);
|
||||
const tuning = loadTuning(options.dataFolder, report);
|
||||
const tuning = loadTuning(options.dataFolder, report, enabledMods);
|
||||
|
||||
const mutableDatasets = new Map<string, unknown>();
|
||||
const vehiclesList: CatalogueVehicle[] = [...datasets.load(vehicleCatalogue)];
|
||||
const rawMissions = datasets.load(multiplayerMissions);
|
||||
const missionsObj: MultiplayerMissions = {
|
||||
categories: [...rawMissions.categories],
|
||||
missions: { ...rawMissions.missions },
|
||||
};
|
||||
const spawnRulesObj = datasets.load(battleSpawnRules);
|
||||
const economyDataObj: BattleEconomyData = { ...datasets.load(battleEconomy), vehicles: { ...datasets.load(battleEconomy).vehicles } };
|
||||
const labelsObj: UiLabels = { ...datasets.load(uiLabels) };
|
||||
const localizedUnitNamesObj: UnitNamesMap = { ...datasets.load(unitNames) };
|
||||
|
||||
mutableDatasets.set('vehicle-catalogue', vehiclesList);
|
||||
mutableDatasets.set('multiplayer-missions', missionsObj);
|
||||
mutableDatasets.set('battle-spawn-rules', spawnRulesObj);
|
||||
mutableDatasets.set('battle-economy', economyDataObj);
|
||||
mutableDatasets.set('ui-labels', labelsObj);
|
||||
mutableDatasets.set('unit-names', localizedUnitNamesObj);
|
||||
|
||||
const datasetsView: ServerModDatasetsView = {
|
||||
vehicles: vehiclesList,
|
||||
battleEconomy: economyDataObj as unknown as Record<string, unknown>,
|
||||
battleSpawnRules: spawnRulesObj,
|
||||
multiplayerMissions: missionsObj as unknown as Record<string, unknown>,
|
||||
uiLabels: labelsObj as unknown as Record<string, unknown>,
|
||||
unitNames: localizedUnitNamesObj as unknown as Record<string, unknown>,
|
||||
get: <T = unknown>(id: string) => mutableDatasets.get(id) as T | undefined,
|
||||
set: (id: string, value: unknown) => { mutableDatasets.set(id, value); },
|
||||
};
|
||||
await modRuntime.runDatasetsLoaded(datasetsView);
|
||||
|
||||
const vehicles = (mutableDatasets.get('vehicle-catalogue') as readonly CatalogueVehicle[]) ?? vehiclesList;
|
||||
const missions = (mutableDatasets.get('multiplayer-missions') as MultiplayerMissions) ?? missionsObj;
|
||||
const spawnRules = (mutableDatasets.get('battle-spawn-rules') as typeof spawnRulesObj) ?? spawnRulesObj;
|
||||
const economyData = (mutableDatasets.get('battle-economy') as BattleEconomyData) ?? economyDataObj;
|
||||
const labels = (mutableDatasets.get('ui-labels') as UiLabels) ?? labelsObj;
|
||||
const localizedUnitNames = (mutableDatasets.get('unit-names') as UnitNamesMap) ?? localizedUnitNamesObj;
|
||||
|
||||
const gameModes = new GameModes(
|
||||
datasets.load(multiplayerMissions), datasets.load(shopCountries), report, tuning.battle.fixedAirRealisticMaxMrank,
|
||||
missions, datasets.load(shopCountries), report, tuning.battle.fixedAirRealisticMaxMrank,
|
||||
);
|
||||
const accounts = new AccountStore(options.dataFolder);
|
||||
const clans = new ClanStore(options.dataFolder, tuning.clans);
|
||||
const cloudSettings = new CloudSettingsStore(options.dataFolder);
|
||||
const vehicles = datasets.load(vehicleCatalogue);
|
||||
const spawnRules = datasets.load(battleSpawnRules);
|
||||
const economyData = datasets.load(battleEconomy);
|
||||
const efficiencyData = datasets.load(researchEfficiency);
|
||||
const streakDefinitions = datasets.load(battleStreaks);
|
||||
const labels = datasets.load(uiLabels);
|
||||
const { profiles, research, equipment, repair } = setUpProfiles(options.dataFolder, gameFiles, datasets, tuning, { accounts, clans }, vehicles);
|
||||
const { clanEconomy: squadronEconomy, ...catalogues } = loadDomainCatalogues(datasets, tuning, vehicles, labels);
|
||||
const { profiles, research, equipment, repair } = setUpProfiles(
|
||||
options.dataFolder,
|
||||
gameFiles,
|
||||
datasets,
|
||||
tuning,
|
||||
{ accounts, clans },
|
||||
vehicles,
|
||||
(profile, userId) => modRuntime.runProfileRefresh(profile, userId),
|
||||
);
|
||||
const { clanEconomy: squadronEconomy, ...catalogues } = loadDomainCatalogues(
|
||||
datasets,
|
||||
tuning,
|
||||
vehicles,
|
||||
labels,
|
||||
localizedUnitNames,
|
||||
);
|
||||
const storeProducts = catalogues.store.allProducts();
|
||||
const artwork = new GameArtwork(options.gameFolder, {
|
||||
vehicles: storeProducts.flatMap((product) => product.vehicles?.map((vehicle) => vehicle.name) ?? []),
|
||||
@@ -112,6 +175,9 @@ export async function startServer(options: ServerOptions): Promise<RunningServer
|
||||
|
||||
let matching: MatchingService | undefined;
|
||||
const notifyInventoryChanged = () => matching?.broadcastNotify('mrpc.generic_notify', { from: 'inventory', func: 'changed' });
|
||||
const notifyUser = (userId: string, eventName: string, payload: Record<string, unknown>) => {
|
||||
matching?.notifyUser(userId, eventName, payload);
|
||||
};
|
||||
const battleSessions = new BattleSessionStore(tuning.limits.battleSessionsKept);
|
||||
const battleMatchmaker = new BattleMatchmaker(gameModes, vehicles, battleSessions, tuning.battle, spawnRules, botAircraftLoadouts);
|
||||
const economy = new BattleEconomy(
|
||||
@@ -124,7 +190,31 @@ export async function startServer(options: ServerOptions): Promise<RunningServer
|
||||
const streaks = new BattleStreaks(streakDefinitions, vehicles, economy);
|
||||
const supportPlanes = supportPlaneUnits(spawnRules);
|
||||
|
||||
await modRuntime.runCataloguesReady({
|
||||
tuning: tuning as unknown as Readonly<Record<string, unknown>>,
|
||||
gameModes,
|
||||
profiles,
|
||||
accounts,
|
||||
research,
|
||||
equipment,
|
||||
repair,
|
||||
store: catalogues.store,
|
||||
inventory: catalogues.inventory,
|
||||
trophies: catalogues.trophies,
|
||||
items: catalogues.items,
|
||||
crew: catalogues.crew,
|
||||
decor: catalogues.decor,
|
||||
unlocks: catalogues.unlocks,
|
||||
clans,
|
||||
battleMatchmaker,
|
||||
battleEconomy: economy,
|
||||
battleStreaks: streaks,
|
||||
notifyInventoryChanged,
|
||||
notifyUser,
|
||||
});
|
||||
|
||||
const router = new Router([
|
||||
modRuntime.httpService(),
|
||||
webUi,
|
||||
new RegistrationService(accounts),
|
||||
new StoreService(accounts, profiles, catalogues.store, tuning.redeemCodes, options.port),
|
||||
@@ -140,44 +230,69 @@ export async function startServer(options: ServerOptions): Promise<RunningServer
|
||||
...catalogues,
|
||||
clans: { store: clans, economy: squadronEconomy },
|
||||
cloudSettings,
|
||||
battle: { sessions: battleSessions, economy, efficiency: new ResearchEfficiency(efficiencyData, research.techTree), streaks, supportPlanes },
|
||||
battle: {
|
||||
sessions: battleSessions,
|
||||
economy,
|
||||
efficiency: new ResearchEfficiency(efficiencyData, research.techTree),
|
||||
streaks,
|
||||
supportPlanes,
|
||||
onAfterSettle: (ctx) => modRuntime.runBattleAfterSettle(ctx),
|
||||
},
|
||||
notifyInventoryChanged,
|
||||
})),
|
||||
}), modRuntime),
|
||||
new DownloadableContentService(),
|
||||
]);
|
||||
const log = new TrafficLog(options.dataFolder, options.loggingEnabled ?? true);
|
||||
const matchingTable = modRuntime.applyMatchingRoutes(matchingRequests({
|
||||
gameModes,
|
||||
accounts,
|
||||
battle: {
|
||||
gameModes,
|
||||
profiles,
|
||||
matchmaker: battleMatchmaker,
|
||||
sessions: battleSessions,
|
||||
economy,
|
||||
notifyLaunch: (userId, payload) => matching?.notifyUser(userId, 'mrpc.generic_notify', payload),
|
||||
onBeforeMatchmake: (ctx) => modRuntime.runBattleBeforeMatchmake(ctx),
|
||||
},
|
||||
battleLive: {
|
||||
sessions: battleSessions,
|
||||
liveLogs: new BattleLiveLogs(tuning.limits.battleSessionsKept),
|
||||
streaks,
|
||||
supportPlanes,
|
||||
onLiveFact: (ctx) => modRuntime.runBattleLiveFact(ctx),
|
||||
},
|
||||
}));
|
||||
const listener = createPortListener(
|
||||
router,
|
||||
matchingRequests({
|
||||
gameModes,
|
||||
accounts,
|
||||
battle: {
|
||||
gameModes,
|
||||
profiles,
|
||||
matchmaker: battleMatchmaker,
|
||||
sessions: battleSessions,
|
||||
economy,
|
||||
notifyLaunch: (userId, payload) => matching?.notifyUser(userId, 'mrpc.generic_notify', payload),
|
||||
},
|
||||
battleLive: { sessions: battleSessions, liveLogs: new BattleLiveLogs(tuning.limits.battleSessionsKept), streaks, supportPlanes },
|
||||
}),
|
||||
matchingTable,
|
||||
log,
|
||||
loadOrCreateCredentials(options.dataFolder),
|
||||
);
|
||||
matching = listener.matching;
|
||||
await listen(listener.server, options.port);
|
||||
await modRuntime.runServerListening(options.port);
|
||||
return {
|
||||
logFolder: log.folder,
|
||||
close: () => new Promise((resolve) => {
|
||||
listener.server.close(() => resolve());
|
||||
listener.destroyConnections();
|
||||
}),
|
||||
loadedModIds: modRuntime.loadedModIds,
|
||||
close: async () => {
|
||||
await modRuntime.runServerClosing();
|
||||
await new Promise<void>((resolve) => {
|
||||
listener.server.close(() => resolve());
|
||||
listener.destroyConnections();
|
||||
});
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
function loadDomainCatalogues(datasets: DatasetCache, tuning: Tuning, vehicles: readonly CatalogueVehicle[], labels: UiLabels) {
|
||||
function loadDomainCatalogues(
|
||||
datasets: DatasetCache,
|
||||
tuning: Tuning,
|
||||
vehicles: readonly CatalogueVehicle[],
|
||||
labels: UiLabels,
|
||||
localizedUnitNames: UnitNamesMap,
|
||||
) {
|
||||
const classicItemDefs = datasets.load(itemCatalogue);
|
||||
const localizedUnitNames = datasets.load(unitNames);
|
||||
return {
|
||||
crew: new CrewSkillCatalogue(datasets.load(crewSkillData)),
|
||||
decor: new DecorShop(datasets.load(decorCatalogue)),
|
||||
|
||||
Reference in new issue
Block a user