/** * Public TypeScript contract for WTOffline server-side JS/TS mods. * * EXPERIMENTAL / UNSTABLE: * WTOffline is in early development. Internal server types, dataset shapes, hook signatures, * and route contracts may change without backwards compatibility across releases. Mod authors * using server scripts should expect breaking changes and maintain compatibility with updates. */ import type { BlkBlock } from '@wto/blk'; import type { ModManifest } from './types.ts'; export interface ModLogger { info(message: string): void; warn(message: string): void; error(message: string): void; } /** Per-account JSON storage isolated under `/saves//mods/.json`. */ export interface ModPlayerStorage { read(): T | undefined; write(value: unknown): void; remove(): void; } export interface ModStorageApi { /** Storage scoped to one local player account (never mutates stock `profile.blk`). */ forPlayer(userId: string): ModPlayerStorage; /** Shared mod storage under `/mods-data/.json`. */ shared(): ModPlayerStorage; } /** Mutable snapshot of extracted static datasets exposed before domain catalogues are built. */ export interface ServerModDatasetsView { readonly vehicles: unknown[]; readonly battleEconomy: Record; readonly battleSpawnRules: unknown; readonly multiplayerMissions: Record; readonly uiLabels: Record; readonly unitNames: Record; /** Escape hatch: read or replace any dataset object by its dataset id. */ get(datasetId: string): T | undefined; set(datasetId: string, value: unknown): void; } /** Exposed domain catalogues and services after server initialization. */ export interface ServerModCataloguesView { readonly tuning: Readonly>; readonly gameModes: unknown; readonly profiles: unknown; readonly accounts: unknown; readonly research: unknown; readonly equipment: unknown; readonly repair: unknown; readonly store: unknown; readonly inventory: unknown; readonly trophies: unknown; readonly items: unknown; readonly crew: unknown; readonly decor: unknown; readonly unlocks: unknown; readonly clans: unknown; readonly battleMatchmaker: unknown; readonly battleEconomy: unknown; readonly battleStreaks: unknown; readonly notifyInventoryChanged: () => void; readonly notifyUser: (userId: string, eventName: string, payload: Record) => void; } export interface BattleMatchmakeContext { readonly userId: string; readonly eventName: string; /** Mutable room / matchmake request parameters or generated launch plan. */ readonly request: Record; } export interface BattleMissionCandidate { readonly name: string; readonly path: string; readonly level?: string; readonly type?: string; readonly locName?: string; readonly postfix?: string; } export interface BattleFilterMissionsContext { readonly userId: string; readonly modeId: number; readonly modeName: string; readonly economicName: string; readonly minMrank: number; readonly maxMrank: number; readonly missions: readonly BattleMissionCandidate[]; } export interface BattleLiveFactContext { readonly userId: string; readonly roomId: string; readonly fact: Record; } export interface BattleSettlementContext { readonly userId: string; readonly roomId: string; /** Mutable settlement payload before final rewards are committed to the profile. */ readonly summary: Record; } export interface ModCharRequest { readonly action: string; readonly userId: string; readonly headers: Readonly>; readonly body: Buffer; readonly query: Readonly>; } export interface ModCharResponse { readonly status?: number; readonly contentType?: string; readonly headers?: Readonly>; readonly body: Buffer | Uint8Array | string; } export type ModCharHandler = (request: ModCharRequest) => ModCharResponse | Promise; export type ModCharWrapper = ( request: ModCharRequest, next: (request: ModCharRequest) => Promise, ) => ModCharResponse | Promise; export interface ModMatchingRequest { readonly method: string; readonly params: Readonly>; readonly userId: string | undefined; } export type ModMatchingHandler = (request: ModMatchingRequest) => Record; export type ModMatchingWrapper = ( request: ModMatchingRequest, next: (request: ModMatchingRequest) => Record, ) => Record; export interface ModHttpRequest { readonly method: string; readonly path: string; readonly query: Readonly>; readonly headers: Readonly>; readonly body: Buffer; } export interface ModHttpResponse { readonly status?: number; readonly contentType?: string; readonly headers?: Readonly>; readonly body: Buffer | Uint8Array | string; } export type ModHttpHandler = (request: ModHttpRequest) => ModHttpResponse | Promise; export interface ServerModHooksApi { onDatasetsLoaded(handler: (datasets: ServerModDatasetsView) => void | Promise): void; onCataloguesReady(handler: (catalogues: ServerModCataloguesView) => void | Promise): void; onServerListening(handler: (info: { readonly port: number }) => void | Promise): void; onServerClosing(handler: () => void | Promise): void; readonly profile: { onRefresh(handler: (profile: BlkBlock, userId: string) => void): void; }; readonly battle: { onBeforeMatchmake(handler: (ctx: BattleMatchmakeContext) => void): void; /** Filter or reorder candidate missions before matchmaking picks one; return the desired subset. */ onFilterMissions(handler: (ctx: BattleFilterMissionsContext) => readonly BattleMissionCandidate[] | void): void; onLiveFact(handler: (ctx: BattleLiveFactContext) => void): void; onAfterSettle(handler: (ctx: BattleSettlementContext) => void): void; }; } export interface ServerModRoutesApi { readonly char: { register(action: string, handler: ModCharHandler): void; wrap(action: string, wrapper: ModCharWrapper): void; }; readonly matching: { register(method: string, handler: ModMatchingHandler): void; wrap(method: string, wrapper: ModMatchingWrapper): void; }; readonly http: { /** Register a route under `/mods//...` (automatically namespaced to this mod). */ register(subPath: string, handler: ModHttpHandler): void; }; } export interface WtoServerModContext { readonly apiVersion: string; readonly manifest: ModManifest; readonly modDir: string; readonly gameFolder: string; readonly dataFolder: string; readonly port: number; readonly logger: ModLogger; readonly storage: ModStorageApi; readonly hooks: ServerModHooksApi; readonly routes: ServerModRoutesApi; } export interface WtoServerMod { setup(ctx: WtoServerModContext): void | Promise; }