61 lines
2.0 KiB
TypeScript
61 lines
2.0 KiB
TypeScript
/**
|
|
* Example server-side TypeScript mod (erasable TypeScript syntax, runs directly on Electron's Node 24).
|
|
*
|
|
* EXPERIMENTAL: Internal server contracts and hook shapes may change across WTOffline releases.
|
|
*/
|
|
interface ModState {
|
|
refreshes: number;
|
|
lastUpdated: string;
|
|
}
|
|
|
|
export default {
|
|
setup(ctx: {
|
|
readonly apiVersion: string;
|
|
readonly manifest: { readonly id: string; readonly version: string };
|
|
readonly logger: { info(msg: string): void };
|
|
readonly storage: {
|
|
forPlayer(userId: string): { read<T>(): T | undefined; write(value: unknown): void };
|
|
};
|
|
readonly hooks: {
|
|
onDatasetsLoaded(fn: (datasets: { readonly vehicles: unknown[] }) => void): void;
|
|
onServerListening(fn: (info: { readonly port: number }) => void): void;
|
|
readonly profile: {
|
|
onRefresh(fn: (profile: unknown, userId: string) => void): void;
|
|
};
|
|
};
|
|
readonly routes: {
|
|
readonly http: {
|
|
register(subPath: string, fn: () => { status: number; contentType: string; body: string }): void;
|
|
};
|
|
};
|
|
}) {
|
|
let vehicleCount = 0;
|
|
|
|
ctx.hooks.onDatasetsLoaded((datasets) => {
|
|
vehicleCount = datasets.vehicles.length;
|
|
ctx.logger.info(`observed ${vehicleCount} vehicles in merged dataset`);
|
|
});
|
|
|
|
ctx.hooks.profile.onRefresh((_profile, userId) => {
|
|
const store = ctx.storage.forPlayer(userId);
|
|
const prev = store.read<ModState>() ?? { refreshes: 0, lastUpdated: '' };
|
|
store.write({ refreshes: prev.refreshes + 1, lastUpdated: new Date().toISOString() });
|
|
});
|
|
|
|
ctx.hooks.onServerListening(({ port }) => {
|
|
ctx.logger.info(`custom status endpoint ready at http://127.0.0.1:${port}/mods/${ctx.manifest.id}/status`);
|
|
});
|
|
|
|
ctx.routes.http.register('/status', () => ({
|
|
status: 200,
|
|
contentType: 'application/json; charset=utf-8',
|
|
body: JSON.stringify({
|
|
modId: ctx.manifest.id,
|
|
version: ctx.manifest.version,
|
|
apiVersion: ctx.apiVersion,
|
|
vehicleCount,
|
|
}),
|
|
}));
|
|
},
|
|
};
|