Files
WT-Offline/packages/mods/src/server-api.ts
T

206 lines
7.0 KiB
TypeScript

/**
* 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 `<dataFolder>/saves/<userId>/mods/<modId>.json`. */
export interface ModPlayerStorage {
read<T = unknown>(): 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 `<dataFolder>/mods-data/<modId>.json`. */
shared(): ModPlayerStorage;
}
/** Mutable snapshot of extracted static datasets exposed before domain catalogues are built. */
export interface ServerModDatasetsView {
readonly vehicles: unknown[];
readonly battleEconomy: Record<string, unknown>;
readonly battleSpawnRules: unknown;
readonly multiplayerMissions: Record<string, unknown>;
readonly uiLabels: Record<string, unknown>;
readonly unitNames: Record<string, unknown>;
/** Escape hatch: read or replace any dataset object by its dataset id. */
get<T = unknown>(datasetId: string): T | undefined;
set(datasetId: string, value: unknown): void;
}
/** Exposed domain catalogues and services after server initialization. */
export interface ServerModCataloguesView {
readonly tuning: Readonly<Record<string, unknown>>;
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<string, unknown>) => void;
}
export interface BattleMatchmakeContext {
readonly userId: string;
readonly eventName: string;
/** Mutable room / matchmake request parameters or generated launch plan. */
readonly request: Record<string, unknown>;
}
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<string, unknown>;
}
export interface BattleSettlementContext {
readonly userId: string;
readonly roomId: string;
/** Mutable settlement payload before final rewards are committed to the profile. */
readonly summary: Record<string, unknown>;
}
export interface ModCharRequest {
readonly action: string;
readonly userId: string;
readonly headers: Readonly<Record<string, string | undefined>>;
readonly body: Buffer;
readonly query: Readonly<Record<string, string>>;
}
export interface ModCharResponse {
readonly status?: number;
readonly contentType?: string;
readonly headers?: Readonly<Record<string, string>>;
readonly body: Buffer | Uint8Array | string;
}
export type ModCharHandler = (request: ModCharRequest) => ModCharResponse | Promise<ModCharResponse>;
export type ModCharWrapper = (
request: ModCharRequest,
next: (request: ModCharRequest) => Promise<ModCharResponse>,
) => ModCharResponse | Promise<ModCharResponse>;
export interface ModMatchingRequest {
readonly method: string;
readonly params: Readonly<Record<string, unknown>>;
readonly userId: string | undefined;
}
export type ModMatchingHandler = (request: ModMatchingRequest) => Record<string, unknown>;
export type ModMatchingWrapper = (
request: ModMatchingRequest,
next: (request: ModMatchingRequest) => Record<string, unknown>,
) => Record<string, unknown>;
export interface ModHttpRequest {
readonly method: string;
readonly path: string;
readonly query: Readonly<Record<string, string>>;
readonly headers: Readonly<Record<string, string | undefined>>;
readonly body: Buffer;
}
export interface ModHttpResponse {
readonly status?: number;
readonly contentType?: string;
readonly headers?: Readonly<Record<string, string>>;
readonly body: Buffer | Uint8Array | string;
}
export type ModHttpHandler = (request: ModHttpRequest) => ModHttpResponse | Promise<ModHttpResponse>;
export interface ServerModHooksApi {
onDatasetsLoaded(handler: (datasets: ServerModDatasetsView) => void | Promise<void>): void;
onCataloguesReady(handler: (catalogues: ServerModCataloguesView) => void | Promise<void>): void;
onServerListening(handler: (info: { readonly port: number }) => void | Promise<void>): void;
onServerClosing(handler: () => void | Promise<void>): 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/<modId>/...` (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<void>;
}