Use the native embedded browser and pass the game language through auto_local. Share web assets and extract vehicle icons, flags, and localized names from the installed client. Load and validate per-domain JSONC tuning and merge local overrides by field. Use official store USD prices for GJN products, keep GJN balances in integer cents, and split the native and web store handlers by protocol. Multi-vehicle packs are still listed as single vehicles at this point. Type checking, patch checks, and configuration loading passed. In-game acceptance is pending.
98 lines
4.5 KiB
TypeScript
98 lines
4.5 KiB
TypeScript
/**
|
|
* Assemble the local service: data layer, save layer, rules, the
|
|
* protocol-layer services, and the listening port.
|
|
*/
|
|
import type { Server } from 'node:net';
|
|
import { AccountStore } from './accounts/account-store.ts';
|
|
import { actionTable } from './char/actions/action-table.ts';
|
|
import { CharService } from './char/char-service.ts';
|
|
import { ClanStore } from './clans/clan-store.ts';
|
|
import { CloudSettingsStore } from './cloud-settings/cloud-settings-store.ts';
|
|
import { DownloadableContentService } from './downloadable-content/downloadable-content-service.ts';
|
|
import { DatasetCache } from './game-data/datasets/dataset-cache.ts';
|
|
import { multiplayerMissions } from './game-data/datasets/multiplayer-missions.ts';
|
|
import { shopCountries } from './game-data/datasets/shop-countries.ts';
|
|
import { GameFiles } from './game-data/game-files.ts';
|
|
import { GameModes } from './game-modes/game-modes.ts';
|
|
import { InventoryService } from './inventory/inventory-service.ts';
|
|
import { MarketplaceService } from './inventory/marketplace-service.ts';
|
|
import { LoginService } from './login/login-service.ts';
|
|
import { matchingRequests } from './matching/matching-requests.ts';
|
|
import type { MatchingService } from './matching/matching-service.ts';
|
|
import { setUpProfiles } from './profiles-setup.ts';
|
|
import { createPortListener } from './server/port-listener.ts';
|
|
import { Router } from './server/router.ts';
|
|
import { loadOrCreateCredentials } from './server/tls/tls-certificate.ts';
|
|
import { StoreService } from './store/store-service.ts';
|
|
import { TrafficLog } from './traffic-log/traffic-log.ts';
|
|
import { loadTuning } from './tuning/tuning.ts';
|
|
import { uiLabels } from './game-data/datasets/ui-labels.ts';
|
|
import { webIcons } from './game-data/datasets/web-icons.ts';
|
|
import { WebUiService } from './web-ui/web-ui-service.ts';
|
|
|
|
export interface LocalServiceOptions {
|
|
/** The player's game folder; its data packs are read, never written. */
|
|
readonly gameFolder: string;
|
|
/** Accounts, saves and logs. */
|
|
readonly dataFolder: string;
|
|
readonly port: number;
|
|
/** Start-up progress lines (static data extraction, game modes). */
|
|
readonly report?: (line: string) => void;
|
|
}
|
|
|
|
export const LOOPBACK = '127.0.0.1';
|
|
|
|
export interface RunningLocalService {
|
|
readonly logFolder: string;
|
|
close(): Promise<void>;
|
|
}
|
|
|
|
export async function startLocalService(options: LocalServiceOptions): Promise<RunningLocalService> {
|
|
const report = options.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 gameModes = new GameModes(datasets.load(multiplayerMissions), datasets.load(shopCountries), report);
|
|
const accounts = new AccountStore(options.dataFolder);
|
|
const tuning = loadTuning(options.dataFolder, report);
|
|
const clans = new ClanStore(options.dataFolder, tuning.clans);
|
|
const cloudSettings = new CloudSettingsStore(options.dataFolder);
|
|
const { profiles, research } = setUpProfiles(options.dataFolder, gameFiles, datasets, tuning, { accounts, clans });
|
|
const webUi = new WebUiService(datasets.load(webIcons), datasets.load(uiLabels));
|
|
gameFiles.releasePacks();
|
|
|
|
let matching: MatchingService | undefined;
|
|
const notifyInventoryChanged = () => matching?.broadcastNotify('mrpc.generic_notify', { from: 'inventory', func: 'changed' });
|
|
|
|
const router = new Router([
|
|
webUi,
|
|
new StoreService(accounts, profiles, research.store, tuning.redeemCodes),
|
|
new MarketplaceService(accounts, profiles, research.inventory, notifyInventoryChanged, tuning.marketplace.maxPerPurchase),
|
|
new InventoryService(accounts, profiles, research.inventory),
|
|
new LoginService(accounts),
|
|
new CharService(actionTable({ profiles, accounts, research, clans, cloudSettings, notifyInventoryChanged })),
|
|
new DownloadableContentService(),
|
|
]);
|
|
const log = new TrafficLog(options.dataFolder);
|
|
const listener = createPortListener(router, matchingRequests({ gameModes }), log, loadOrCreateCredentials(options.dataFolder));
|
|
matching = listener.matching;
|
|
await listen(listener.server, options.port);
|
|
return {
|
|
logFolder: log.folder,
|
|
close: () => new Promise((resolve) => {
|
|
listener.server.close(() => resolve());
|
|
listener.destroyConnections();
|
|
}),
|
|
};
|
|
}
|
|
|
|
function listen(server: Server, port: number): Promise<void> {
|
|
return new Promise((resolve, reject) => {
|
|
server.once('error', reject);
|
|
server.listen(port, LOOPBACK, () => {
|
|
server.off('error', reject);
|
|
resolve();
|
|
});
|
|
});
|
|
}
|