Files
WT-Offline/mod-dev-docs/en/06-server-scripting-api.md

41 lines
2.6 KiB
Markdown

# 06 · Server JS/TS Scripting API Reference
> **Experimental Warning (API `0.1-experimental`)**: All server script contracts, internal catalogue classes, and hook shapes are unstable and may change across releases without backwards compatibility.
## 1. Runtime & TypeScript Rules
Server scripts run inside Electron 43's bundled **Node.js 24** with built-in TypeScript type stripping (`erasableSyntaxOnly` syntax: no `enum`, `namespace`, or parameter properties). Pre-bundled `.mjs` / `.js` files are also supported.
Default entry lookup order: `server/index.ts` → `server/index.mjs` → `server/index.js` (or explicit `server.entry` in `mod.jsonc`).
```ts
export default {
async setup(ctx) {
ctx.logger.info('mod initialized');
},
};
```
Full TypeScript definitions live in `packages/mods/src/server-api.ts`.
## 2. `WtoServerModContext`
- `apiVersion`, `manifest`, `modDir`, `gameFolder`, `dataFolder`, `port`
- `logger`: `{ info(msg), warn(msg), error(msg) }` (prefixed with `[mod:<id>]`)
- `storage`:
- `storage.forPlayer(userId)` → atomic JSON file under `<dataFolder>/saves/<userId>/mods/<modId>.json` (never mutate stock `profile.blk` with custom keys)
- `storage.shared()` → `<dataFolder>/mods-data/<modId>.json`
- `hooks`:
- `onDatasetsLoaded(datasets)` — inspect/mutate `vehicles`, `battleEconomy`, `battleSpawnRules`, `multiplayerMissions`, `uiLabels`, `unitNames`, or `get(id)` / `set(id, value)`
- `onCataloguesReady(catalogues)` — access `tuning`, `gameModes`, `profiles`, `accounts`, `research`, `equipment`, `repair`, `store`, `inventory`, `trophies`, `items`, `crew`, `decor`, `unlocks`, `clans`, `battleMatchmaker`, `battleEconomy`, `battleStreaks`, `notifyInventoryChanged()`, `notifyUser(userId, eventName, payload)`
- `onServerListening({ port })` / `onServerClosing()`
- `profile.onRefresh(profile, userId)`
- `battle.onBeforeMatchmake({ userId, eventName, request })`
- `battle.onFilterMissions({ userId, modeId, modeName, economicName, minMrank, maxMrank, missions })` — return a filtered/reordered subset of candidate missions (empty results safely fall back to the prior pool)
- `battle.onLiveFact({ userId, roomId, fact })`
- `battle.onAfterSettle({ userId, roomId, summary })`
- `routes`:
- `routes.char.register(action, handler)` / `routes.char.wrap(action, wrapper)`
- `routes.matching.register(method, handler)` / `routes.matching.wrap(method, wrapper)` (callable from client Quirrel via `matchingApiFunc`)
- `routes.http.register(subPath, handler)` — automatically namespaced under `/mods/<modId>/...` (supports exact paths and `/*` wildcards)