bonescript-compiler 0.5.8 → 0.6.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/ast.d.ts +2 -0
- package/dist/cli.js +52 -8
- package/dist/cli.js.map +1 -1
- package/dist/emit_admin.d.ts +5 -0
- package/dist/emit_admin.js +340 -35
- package/dist/emit_admin.js.map +1 -1
- package/dist/emit_audit.js +38 -4
- package/dist/emit_audit.js.map +1 -1
- package/dist/emit_capability.js +14 -0
- package/dist/emit_capability.js.map +1 -1
- package/dist/emit_full.js +10 -2
- package/dist/emit_full.js.map +1 -1
- package/dist/emit_maintenance.js +35 -3
- package/dist/emit_maintenance.js.map +1 -1
- package/dist/emit_nakama.js +36 -36
- package/dist/emit_notify.js +30 -2
- package/dist/emit_notify.js.map +1 -1
- package/dist/emit_runtime.d.ts +18 -1
- package/dist/emit_runtime.js +265 -85
- package/dist/emit_runtime.js.map +1 -1
- package/dist/emit_websocket.js +22 -2
- package/dist/emit_websocket.js.map +1 -1
- package/dist/emit_zod.js +12 -1
- package/dist/emit_zod.js.map +1 -1
- package/dist/formatter.d.ts +1 -0
- package/dist/formatter.js +10 -2
- package/dist/formatter.js.map +1 -1
- package/dist/ir.d.ts +2 -0
- package/dist/lexer.d.ts +1 -0
- package/dist/lexer.js +4 -0
- package/dist/lexer.js.map +1 -1
- package/dist/lowering.js +2 -0
- package/dist/lowering.js.map +1 -1
- package/dist/parse_decls.js +36 -1
- package/dist/parse_decls.js.map +1 -1
- package/dist/typechecker.js +9 -0
- package/dist/typechecker.js.map +1 -1
- package/package.json +1 -1
- package/src/ast.ts +2 -0
- package/src/cli.ts +58 -10
- package/src/emit_admin.ts +342 -35
- package/src/emit_audit.ts +40 -4
- package/src/emit_capability.ts +13 -0
- package/src/emit_full.ts +9 -2
- package/src/emit_maintenance.ts +35 -3
- package/src/emit_nakama.ts +576 -576
- package/src/emit_notify.ts +30 -2
- package/src/emit_runtime.ts +955 -763
- package/src/emit_websocket.ts +22 -2
- package/src/emit_zod.ts +11 -1
- package/src/formatter.ts +9 -2
- package/src/ir.ts +2 -0
- package/src/lexer.ts +2 -0
- package/src/lowering.ts +5 -3
- package/src/parse_decls.ts +31 -1
- package/src/typechecker.ts +10 -0
package/src/emit_nakama.ts
CHANGED
|
@@ -1,576 +1,576 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* BoneScript Nakama Emitter
|
|
3
|
-
*
|
|
4
|
-
* Generates a Nakama TypeScript runtime module from an IRSystem.
|
|
5
|
-
* Output is a self-contained src/index.ts that registers:
|
|
6
|
-
* - nk.registerRpc() for every capability (capability -> RPC)
|
|
7
|
-
* - nk.registerMatch() for every realtime_service module (channel -> match handler)
|
|
8
|
-
* - Storage helpers for every entity/model (entity -> storage object)
|
|
9
|
-
* - Event hooks for every event emission (event -> stream send)
|
|
10
|
-
*
|
|
11
|
-
* Deploy with: nakama migrate up && nakama --runtime.path ./build
|
|
12
|
-
* Docs: https://heroiclabs.com/docs/nakama/server-framework/typescript-runtime/
|
|
13
|
-
*/
|
|
14
|
-
|
|
15
|
-
import * as IR from "./ir";
|
|
16
|
-
import { EmittedFile } from "./emitter";
|
|
17
|
-
|
|
18
|
-
// ─── Helpers ──────────────────────────────────────────────────────────────────
|
|
19
|
-
|
|
20
|
-
function toSnakeCase(s: string): string {
|
|
21
|
-
return s.replace(/([a-z])([A-Z])/g, "$1_$2").toLowerCase();
|
|
22
|
-
}
|
|
23
|
-
|
|
24
|
-
function toCamelCase(s: string): string {
|
|
25
|
-
return s.replace(/_([a-z])/g, (_, c: string) => c.toUpperCase());
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
function toPascalCase(s: string): string {
|
|
29
|
-
const c = toCamelCase(s);
|
|
30
|
-
return c.charAt(0).toUpperCase() + c.slice(1);
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
const TS_TYPE_MAP: Record<string, string> = {
|
|
34
|
-
string: "string",
|
|
35
|
-
uint: "number",
|
|
36
|
-
int: "number",
|
|
37
|
-
float: "number",
|
|
38
|
-
bool: "boolean",
|
|
39
|
-
timestamp: "string", // ISO string — Nakama runtime has no Date
|
|
40
|
-
uuid: "string",
|
|
41
|
-
bytes: "string", // base64 in Nakama
|
|
42
|
-
json: "unknown",
|
|
43
|
-
};
|
|
44
|
-
|
|
45
|
-
function toTsType(irType: string): string {
|
|
46
|
-
if (TS_TYPE_MAP[irType]) return TS_TYPE_MAP[irType];
|
|
47
|
-
const listMatch = irType.match(/^list<(.+)>$/);
|
|
48
|
-
if (listMatch) return `${toTsType(listMatch[1])}[]`;
|
|
49
|
-
const setMatch = irType.match(/^set<(.+)>$/);
|
|
50
|
-
if (setMatch) return `${toTsType(setMatch[1])}[]`;
|
|
51
|
-
const optMatch = irType.match(/^optional<(.+)>$/);
|
|
52
|
-
if (optMatch) return `${toTsType(optMatch[1])} | null`;
|
|
53
|
-
return irType;
|
|
54
|
-
}
|
|
55
|
-
|
|
56
|
-
// ─── Package.json ─────────────────────────────────────────────────────────────
|
|
57
|
-
|
|
58
|
-
function emitNakamaPackageJson(system: IR.IRSystem): string {
|
|
59
|
-
return JSON.stringify({
|
|
60
|
-
name: toSnakeCase(system.name),
|
|
61
|
-
version: system.version,
|
|
62
|
-
private: true,
|
|
63
|
-
scripts: {
|
|
64
|
-
build: "npx tsc",
|
|
65
|
-
dev: "npx tsc --watch",
|
|
66
|
-
},
|
|
67
|
-
devDependencies: {
|
|
68
|
-
"@heroiclabs/nakama-runtime": "1.16.0",
|
|
69
|
-
typescript: "5.3.3",
|
|
70
|
-
"ts-node": "10.9.2",
|
|
71
|
-
},
|
|
72
|
-
}, null, 2);
|
|
73
|
-
}
|
|
74
|
-
|
|
75
|
-
// ─── tsconfig.json ────────────────────────────────────────────────────────────
|
|
76
|
-
|
|
77
|
-
function emitNakamaTsConfig(): string {
|
|
78
|
-
return JSON.stringify({
|
|
79
|
-
compilerOptions: {
|
|
80
|
-
target: "ES2020",
|
|
81
|
-
module: "commonjs",
|
|
82
|
-
lib: ["ES2020"],
|
|
83
|
-
outDir: "./build",
|
|
84
|
-
rootDir: "./src",
|
|
85
|
-
strict: true,
|
|
86
|
-
esModuleInterop: true,
|
|
87
|
-
skipLibCheck: true,
|
|
88
|
-
},
|
|
89
|
-
include: ["src/**/*"],
|
|
90
|
-
exclude: ["node_modules", "build"],
|
|
91
|
-
}, null, 2);
|
|
92
|
-
}
|
|
93
|
-
|
|
94
|
-
// ─── Storage helpers ──────────────────────────────────────────────────────────
|
|
95
|
-
// One collection per entity. Nakama storage: collection/key/userId pattern.
|
|
96
|
-
|
|
97
|
-
function emitStorageHelpers(system: IR.IRSystem): string {
|
|
98
|
-
const lines: string[] = [];
|
|
99
|
-
lines.push(`// Generated by BoneScript compiler. DO NOT EDIT.`);
|
|
100
|
-
lines.push(`// Storage helpers — one Nakama storage collection per entity.`);
|
|
101
|
-
lines.push(``);
|
|
102
|
-
lines.push(`const nkRuntime: nkruntime.Nakama = (globalThis as any).__nk;`);
|
|
103
|
-
lines.push(``);
|
|
104
|
-
|
|
105
|
-
for (const mod of system.modules) {
|
|
106
|
-
for (const model of mod.models) {
|
|
107
|
-
const collection = toSnakeCase(model.name) + "s";
|
|
108
|
-
const typeName = toPascalCase(model.name);
|
|
109
|
-
|
|
110
|
-
// TypeScript interface
|
|
111
|
-
lines.push(`export interface ${typeName} {`);
|
|
112
|
-
for (const field of model.fields) {
|
|
113
|
-
const nullable = field.nullable ? " | null" : "";
|
|
114
|
-
lines.push(` ${field.name}: ${toTsType(field.type)}${nullable};`);
|
|
115
|
-
}
|
|
116
|
-
lines.push(`}`);
|
|
117
|
-
lines.push(``);
|
|
118
|
-
|
|
119
|
-
// read
|
|
120
|
-
lines.push(`export function read${typeName}(nk: nkruntime.Nakama, userId: string, id: string): ${typeName} | null {`);
|
|
121
|
-
lines.push(` const objs = nk.storageRead([{ collection: "${collection}", key: id, userId }]);`);
|
|
122
|
-
lines.push(` if (objs.length === 0) return null;`);
|
|
123
|
-
lines.push(` return JSON.parse(objs[0].value) as ${typeName};`);
|
|
124
|
-
lines.push(`}`);
|
|
125
|
-
lines.push(``);
|
|
126
|
-
|
|
127
|
-
// write
|
|
128
|
-
lines.push(`export function write${typeName}(nk: nkruntime.Nakama, userId: string, obj: ${typeName}): void {`);
|
|
129
|
-
lines.push(` nk.storageWrite([{`);
|
|
130
|
-
lines.push(` collection: "${collection}",`);
|
|
131
|
-
lines.push(` key: (obj as any).id ?? userId,`);
|
|
132
|
-
lines.push(` userId,`);
|
|
133
|
-
lines.push(` value: JSON.stringify(obj),`);
|
|
134
|
-
lines.push(` permissionRead: 1,`);
|
|
135
|
-
lines.push(` permissionWrite: 0,`);
|
|
136
|
-
lines.push(` }]);`);
|
|
137
|
-
lines.push(`}`);
|
|
138
|
-
lines.push(``);
|
|
139
|
-
|
|
140
|
-
// delete
|
|
141
|
-
lines.push(`export function delete${typeName}(nk: nkruntime.Nakama, userId: string, id: string): void {`);
|
|
142
|
-
lines.push(` nk.storageDelete([{ collection: "${collection}", key: id, userId }]);`);
|
|
143
|
-
lines.push(`}`);
|
|
144
|
-
lines.push(``);
|
|
145
|
-
}
|
|
146
|
-
}
|
|
147
|
-
|
|
148
|
-
return lines.join("\n");
|
|
149
|
-
}
|
|
150
|
-
|
|
151
|
-
// ─── RPC handlers (capabilities) ─────────────────────────────────────────────
|
|
152
|
-
|
|
153
|
-
function emitRpcHandlers(system: IR.IRSystem): string {
|
|
154
|
-
const lines: string[] = [];
|
|
155
|
-
lines.push(`// Generated by BoneScript compiler. DO NOT EDIT.`);
|
|
156
|
-
lines.push(`// RPC handlers — one nk.registerRpc() per capability.`);
|
|
157
|
-
lines.push(``);
|
|
158
|
-
lines.push(`import * as storage from "./storage";`);
|
|
159
|
-
lines.push(``);
|
|
160
|
-
|
|
161
|
-
for (const mod of system.modules) {
|
|
162
|
-
for (const iface of mod.interfaces) {
|
|
163
|
-
for (const method of iface.methods) {
|
|
164
|
-
const rpcId = `${toSnakeCase(mod.name)}/${toSnakeCase(method.name)}`;
|
|
165
|
-
const fnName = `rpc${toPascalCase(mod.name)}${toPascalCase(method.name)}`;
|
|
166
|
-
|
|
167
|
-
lines.push(`// RPC: ${rpcId}`);
|
|
168
|
-
lines.push(`// Capability: ${method.name} on ${mod.name}`);
|
|
169
|
-
if (method.preconditions.length > 0) {
|
|
170
|
-
lines.push(`// Preconditions: ${method.preconditions.map(p => p.description).join(", ")}`);
|
|
171
|
-
}
|
|
172
|
-
lines.push(`const ${fnName}: nkruntime.RpcFunction = (`);
|
|
173
|
-
lines.push(` ctx: nkruntime.Context,`);
|
|
174
|
-
lines.push(` logger: nkruntime.Logger,`);
|
|
175
|
-
lines.push(` nk: nkruntime.Nakama,`);
|
|
176
|
-
lines.push(` payload: string`);
|
|
177
|
-
lines.push(`): string => {`);
|
|
178
|
-
|
|
179
|
-
// Auth guard
|
|
180
|
-
if (method.authenticated) {
|
|
181
|
-
lines.push(` if (!ctx.userId) {`);
|
|
182
|
-
lines.push(` throw Error("Authentication required");`);
|
|
183
|
-
lines.push(` }`);
|
|
184
|
-
}
|
|
185
|
-
|
|
186
|
-
// Parse payload
|
|
187
|
-
if (method.input.length > 0) {
|
|
188
|
-
const inputType = method.input.map(f => `${f.name}: ${toTsType(f.type)}`).join("; ");
|
|
189
|
-
lines.push(` const input: { ${inputType} } = payload ? JSON.parse(payload) : {};`);
|
|
190
|
-
}
|
|
191
|
-
|
|
192
|
-
// Precondition stubs
|
|
193
|
-
if (method.preconditions.length > 0) {
|
|
194
|
-
lines.push(``);
|
|
195
|
-
lines.push(` // Preconditions`);
|
|
196
|
-
for (const pre of method.preconditions) {
|
|
197
|
-
lines.push(` // CHECK: ${pre.description}`);
|
|
198
|
-
lines.push(` // if (!(${pre.expression})) throw Error("Precondition failed: ${pre.description.replace(/"/g, "'")}"); `);
|
|
199
|
-
}
|
|
200
|
-
}
|
|
201
|
-
|
|
202
|
-
// Effect stubs
|
|
203
|
-
if (method.effects.length > 0) {
|
|
204
|
-
lines.push(``);
|
|
205
|
-
lines.push(` // Effects`);
|
|
206
|
-
for (const eff of method.effects) {
|
|
207
|
-
const op = eff.op === "assign" ? "=" : eff.op === "add" ? "+=" : "-=";
|
|
208
|
-
lines.push(` // EFFECT: ${eff.target} ${op} ${eff.value}`);
|
|
209
|
-
}
|
|
210
|
-
}
|
|
211
|
-
|
|
212
|
-
// Event emissions via Nakama streams
|
|
213
|
-
if (method.emissions.length > 0) {
|
|
214
|
-
lines.push(``);
|
|
215
|
-
lines.push(` // Event emissions — broadcast to stream subscribers`);
|
|
216
|
-
for (const ev of method.emissions) {
|
|
217
|
-
lines.push(` nk.streamSend(`);
|
|
218
|
-
lines.push(` { mode: 1, subject: ctx.userId ?? "", label: "${ev}" },`);
|
|
219
|
-
lines.push(` JSON.stringify({ event: "${ev}", actor: ctx.userId, ts: Date.now() }),`);
|
|
220
|
-
lines.push(` undefined,`);
|
|
221
|
-
lines.push(` true`);
|
|
222
|
-
lines.push(` );`);
|
|
223
|
-
}
|
|
224
|
-
}
|
|
225
|
-
|
|
226
|
-
lines.push(``);
|
|
227
|
-
lines.push(` logger.info("rpc_called", "${rpcId}", ctx.userId ?? "anon");`);
|
|
228
|
-
lines.push(` return JSON.stringify({ ok: true, action: "${method.name}" });`);
|
|
229
|
-
lines.push(`};`);
|
|
230
|
-
lines.push(``);
|
|
231
|
-
|
|
232
|
-
// Export the function name and rpc id for registration
|
|
233
|
-
lines.push(`export const ${fnName}Id = "${rpcId}";`);
|
|
234
|
-
lines.push(`export { ${fnName} };`);
|
|
235
|
-
lines.push(``);
|
|
236
|
-
}
|
|
237
|
-
}
|
|
238
|
-
}
|
|
239
|
-
|
|
240
|
-
return lines.join("\n");
|
|
241
|
-
}
|
|
242
|
-
|
|
243
|
-
// ─── Match handler (realtime channels) ───────────────────────────────────────
|
|
244
|
-
|
|
245
|
-
function emitMatchHandlers(system: IR.IRSystem): string {
|
|
246
|
-
const realtimeMods = system.modules.filter(m => m.kind === "realtime_service");
|
|
247
|
-
if (realtimeMods.length === 0) return "";
|
|
248
|
-
|
|
249
|
-
const lines: string[] = [];
|
|
250
|
-
lines.push(`// Generated by BoneScript compiler. DO NOT EDIT.`);
|
|
251
|
-
lines.push(`// Match handlers — one per realtime_service module.`);
|
|
252
|
-
lines.push(``);
|
|
253
|
-
|
|
254
|
-
for (const mod of realtimeMods) {
|
|
255
|
-
const handlerName = `${toCamelCase(mod.name)}MatchHandler`;
|
|
256
|
-
const matchName = toSnakeCase(mod.name);
|
|
257
|
-
|
|
258
|
-
lines.push(`// Match handler for channel: ${mod.name}`);
|
|
259
|
-
lines.push(`const ${handlerName}: nkruntime.MatchHandler = {`);
|
|
260
|
-
lines.push(``);
|
|
261
|
-
|
|
262
|
-
// matchInit
|
|
263
|
-
lines.push(` matchInit(`);
|
|
264
|
-
lines.push(` ctx: nkruntime.Context,`);
|
|
265
|
-
lines.push(` logger: nkruntime.Logger,`);
|
|
266
|
-
lines.push(` nk: nkruntime.Nakama,`);
|
|
267
|
-
lines.push(` params: { [key: string]: string }`);
|
|
268
|
-
lines.push(` ): { state: unknown; tickRate: number; label: string } {`);
|
|
269
|
-
lines.push(` logger.info("match_init", "${matchName}");`);
|
|
270
|
-
lines.push(` return {`);
|
|
271
|
-
lines.push(` state: { players: {}, tick: 0 },`);
|
|
272
|
-
lines.push(` tickRate: ${mod.config["tick_rate"] ?? 10},`);
|
|
273
|
-
lines.push(` label: "${matchName}",`);
|
|
274
|
-
lines.push(` };`);
|
|
275
|
-
lines.push(` },`);
|
|
276
|
-
lines.push(``);
|
|
277
|
-
|
|
278
|
-
// matchJoinAttempt
|
|
279
|
-
lines.push(` matchJoinAttempt(`);
|
|
280
|
-
lines.push(` ctx: nkruntime.Context,`);
|
|
281
|
-
lines.push(` logger: nkruntime.Logger,`);
|
|
282
|
-
lines.push(` nk: nkruntime.Nakama,`);
|
|
283
|
-
lines.push(` dispatcher: nkruntime.MatchDispatcher,`);
|
|
284
|
-
lines.push(` tick: number,`);
|
|
285
|
-
lines.push(` state: unknown,`);
|
|
286
|
-
lines.push(` presence: nkruntime.Presence,`);
|
|
287
|
-
lines.push(` metadata: { [key: string]: any }`);
|
|
288
|
-
lines.push(` ): { state: unknown; accept: boolean; rejectMessage?: string } {`);
|
|
289
|
-
lines.push(` return { state, accept: true };`);
|
|
290
|
-
lines.push(` },`);
|
|
291
|
-
lines.push(``);
|
|
292
|
-
|
|
293
|
-
// matchJoin
|
|
294
|
-
lines.push(` matchJoin(`);
|
|
295
|
-
lines.push(` ctx: nkruntime.Context,`);
|
|
296
|
-
lines.push(` logger: nkruntime.Logger,`);
|
|
297
|
-
lines.push(` nk: nkruntime.Nakama,`);
|
|
298
|
-
lines.push(` dispatcher: nkruntime.MatchDispatcher,`);
|
|
299
|
-
lines.push(` tick: number,`);
|
|
300
|
-
lines.push(` state: unknown,`);
|
|
301
|
-
lines.push(` presences: nkruntime.Presence[]`);
|
|
302
|
-
lines.push(` ): { state: unknown } | null {`);
|
|
303
|
-
lines.push(` const s = state as any;`);
|
|
304
|
-
lines.push(` for (const p of presences) {`);
|
|
305
|
-
lines.push(` s.players[p.userId] = { userId: p.userId, sessionId: p.sessionId, joinedAt: Date.now() };`);
|
|
306
|
-
lines.push(` }`);
|
|
307
|
-
lines.push(` dispatcher.broadcastMessage(1, JSON.stringify({ type: "player_joined", players: Object.keys(s.players) }), null, null, true);`);
|
|
308
|
-
lines.push(` return { state: s };`);
|
|
309
|
-
lines.push(` },`);
|
|
310
|
-
lines.push(``);
|
|
311
|
-
|
|
312
|
-
// matchLeave
|
|
313
|
-
lines.push(` matchLeave(`);
|
|
314
|
-
lines.push(` ctx: nkruntime.Context,`);
|
|
315
|
-
lines.push(` logger: nkruntime.Logger,`);
|
|
316
|
-
lines.push(` nk: nkruntime.Nakama,`);
|
|
317
|
-
lines.push(` dispatcher: nkruntime.MatchDispatcher,`);
|
|
318
|
-
lines.push(` tick: number,`);
|
|
319
|
-
lines.push(` state: unknown,`);
|
|
320
|
-
lines.push(` presences: nkruntime.Presence[]`);
|
|
321
|
-
lines.push(` ): { state: unknown } | null {`);
|
|
322
|
-
lines.push(` const s = state as any;`);
|
|
323
|
-
lines.push(` for (const p of presences) delete s.players[p.userId];`);
|
|
324
|
-
lines.push(` if (Object.keys(s.players).length === 0) return null; // end match`);
|
|
325
|
-
lines.push(` return { state: s };`);
|
|
326
|
-
lines.push(` },`);
|
|
327
|
-
lines.push(``);
|
|
328
|
-
|
|
329
|
-
// matchLoop
|
|
330
|
-
lines.push(` matchLoop(`);
|
|
331
|
-
lines.push(` ctx: nkruntime.Context,`);
|
|
332
|
-
lines.push(` logger: nkruntime.Logger,`);
|
|
333
|
-
lines.push(` nk: nkruntime.Nakama,`);
|
|
334
|
-
lines.push(` dispatcher: nkruntime.MatchDispatcher,`);
|
|
335
|
-
lines.push(` tick: number,`);
|
|
336
|
-
lines.push(` state: unknown,`);
|
|
337
|
-
lines.push(` messages: nkruntime.MatchMessage[]`);
|
|
338
|
-
lines.push(` ): { state: unknown } | null {`);
|
|
339
|
-
lines.push(` const s = state as any;`);
|
|
340
|
-
lines.push(` s.tick = tick;`);
|
|
341
|
-
lines.push(` for (const msg of messages) {`);
|
|
342
|
-
lines.push(` // Route incoming messages to capability handlers`);
|
|
343
|
-
lines.push(` try {`);
|
|
344
|
-
lines.push(` const data = JSON.parse(nk.binaryToString(msg.data));`);
|
|
345
|
-
lines.push(` dispatcher.broadcastMessage(msg.opCode, msg.data, null, msg.sender, true);`);
|
|
346
|
-
lines.push(` } catch (e) {`);
|
|
347
|
-
lines.push(` logger.error("match_message_error", String(e));`);
|
|
348
|
-
lines.push(` }`);
|
|
349
|
-
lines.push(` }`);
|
|
350
|
-
lines.push(` return { state: s };`);
|
|
351
|
-
lines.push(` },`);
|
|
352
|
-
lines.push(``);
|
|
353
|
-
|
|
354
|
-
// matchTerminate
|
|
355
|
-
lines.push(` matchTerminate(`);
|
|
356
|
-
lines.push(` ctx: nkruntime.Context,`);
|
|
357
|
-
lines.push(` logger: nkruntime.Logger,`);
|
|
358
|
-
lines.push(` nk: nkruntime.Nakama,`);
|
|
359
|
-
lines.push(` dispatcher: nkruntime.MatchDispatcher,`);
|
|
360
|
-
lines.push(` tick: number,`);
|
|
361
|
-
lines.push(` state: unknown,`);
|
|
362
|
-
lines.push(` graceSeconds: number`);
|
|
363
|
-
lines.push(` ): { state: unknown } | null {`);
|
|
364
|
-
lines.push(` dispatcher.broadcastMessage(0, JSON.stringify({ type: "match_terminated" }), null, null, true);`);
|
|
365
|
-
lines.push(` return null;`);
|
|
366
|
-
lines.push(` },`);
|
|
367
|
-
lines.push(``);
|
|
368
|
-
|
|
369
|
-
// matchSignal
|
|
370
|
-
lines.push(` matchSignal(`);
|
|
371
|
-
lines.push(` ctx: nkruntime.Context,`);
|
|
372
|
-
lines.push(` logger: nkruntime.Logger,`);
|
|
373
|
-
lines.push(` nk: nkruntime.Nakama,`);
|
|
374
|
-
lines.push(` dispatcher: nkruntime.MatchDispatcher,`);
|
|
375
|
-
lines.push(` tick: number,`);
|
|
376
|
-
lines.push(` state: unknown,`);
|
|
377
|
-
lines.push(` data: string`);
|
|
378
|
-
lines.push(` ): { state: unknown; data: string } {`);
|
|
379
|
-
lines.push(` return { state, data };`);
|
|
380
|
-
lines.push(` },`);
|
|
381
|
-
lines.push(`};`);
|
|
382
|
-
lines.push(``);
|
|
383
|
-
lines.push(`export const ${handlerName}Name = "${matchName}";`);
|
|
384
|
-
lines.push(`export { ${handlerName} };`);
|
|
385
|
-
lines.push(``);
|
|
386
|
-
}
|
|
387
|
-
|
|
388
|
-
return lines.join("\n");
|
|
389
|
-
}
|
|
390
|
-
|
|
391
|
-
// ─── Main entry point (InitModule) ───────────────────────────────────────────
|
|
392
|
-
|
|
393
|
-
function emitNakamaIndex(system: IR.IRSystem): string {
|
|
394
|
-
const lines: string[] = [];
|
|
395
|
-
lines.push(`// Generated by BoneScript compiler. DO NOT EDIT.`);
|
|
396
|
-
lines.push(`// Nakama TypeScript runtime module for: ${system.name}`);
|
|
397
|
-
lines.push(`// Deploy: copy build/ to Nakama runtime path and restart.`);
|
|
398
|
-
lines.push(``);
|
|
399
|
-
lines.push(`import * as rpc from "./rpc";`);
|
|
400
|
-
|
|
401
|
-
const hasRealtime = system.modules.some(m => m.kind === "realtime_service");
|
|
402
|
-
if (hasRealtime) {
|
|
403
|
-
lines.push(`import * as match from "./match";`);
|
|
404
|
-
}
|
|
405
|
-
|
|
406
|
-
lines.push(``);
|
|
407
|
-
lines.push(`function InitModule(`);
|
|
408
|
-
lines.push(` ctx: nkruntime.Context,`);
|
|
409
|
-
lines.push(` logger: nkruntime.Logger,`);
|
|
410
|
-
lines.push(` nk: nkruntime.Nakama,`);
|
|
411
|
-
lines.push(` initializer: nkruntime.Initializer`);
|
|
412
|
-
lines.push(`): Error | void {`);
|
|
413
|
-
lines.push(` logger.info("init_module", "${system.name} v${system.version}");`);
|
|
414
|
-
lines.push(``);
|
|
415
|
-
|
|
416
|
-
// Register all RPCs
|
|
417
|
-
const allMethods: { mod: IR.IRModule; method: IR.IRMethod }[] = [];
|
|
418
|
-
for (const mod of system.modules) {
|
|
419
|
-
for (const iface of mod.interfaces) {
|
|
420
|
-
for (const method of iface.methods) {
|
|
421
|
-
allMethods.push({ mod, method });
|
|
422
|
-
}
|
|
423
|
-
}
|
|
424
|
-
}
|
|
425
|
-
|
|
426
|
-
if (allMethods.length > 0) {
|
|
427
|
-
lines.push(` // Register RPC handlers`);
|
|
428
|
-
for (const { mod, method } of allMethods) {
|
|
429
|
-
const fnName = `rpc${toPascalCase(mod.name)}${toPascalCase(method.name)}`;
|
|
430
|
-
lines.push(` initializer.registerRpc(rpc.${fnName}Id, rpc.${fnName});`);
|
|
431
|
-
}
|
|
432
|
-
lines.push(``);
|
|
433
|
-
}
|
|
434
|
-
|
|
435
|
-
// Register match handlers
|
|
436
|
-
if (hasRealtime) {
|
|
437
|
-
const realtimeMods = system.modules.filter(m => m.kind === "realtime_service");
|
|
438
|
-
lines.push(` // Register match handlers`);
|
|
439
|
-
for (const mod of realtimeMods) {
|
|
440
|
-
const handlerName = `${toCamelCase(mod.name)}MatchHandler`;
|
|
441
|
-
lines.push(` initializer.registerMatch(match.${handlerName}Name, match.${handlerName});`);
|
|
442
|
-
}
|
|
443
|
-
lines.push(``);
|
|
444
|
-
}
|
|
445
|
-
|
|
446
|
-
lines.push(` logger.info("init_complete", "${allMethods.length} RPCs, ${hasRealtime ? system.modules.filter(m => m.kind === "realtime_service").length : 0} match handlers");`);
|
|
447
|
-
lines.push(`}`);
|
|
448
|
-
lines.push(``);
|
|
449
|
-
lines.push(`// Required Nakama module export`);
|
|
450
|
-
lines.push(`(globalThis as any).InitModule = InitModule;`);
|
|
451
|
-
|
|
452
|
-
return lines.join("\n");
|
|
453
|
-
}
|
|
454
|
-
|
|
455
|
-
// ─── README ───────────────────────────────────────────────────────────────────
|
|
456
|
-
|
|
457
|
-
function emitNakamaReadme(system: IR.IRSystem): string {
|
|
458
|
-
const rpcs: string[] = [];
|
|
459
|
-
for (const mod of system.modules) {
|
|
460
|
-
for (const iface of mod.interfaces) {
|
|
461
|
-
for (const method of iface.methods) {
|
|
462
|
-
rpcs.push(`- \`${toSnakeCase(mod.name)}/${toSnakeCase(method.name)}\``);
|
|
463
|
-
}
|
|
464
|
-
}
|
|
465
|
-
}
|
|
466
|
-
const realtimeMods = system.modules.filter(m => m.kind === "realtime_service");
|
|
467
|
-
|
|
468
|
-
return `# ${system.name} — Nakama Backend
|
|
469
|
-
|
|
470
|
-
Generated by BoneScript compiler (target: nakama). Source hash: ${system.source_hash}
|
|
471
|
-
|
|
472
|
-
## Quick Start
|
|
473
|
-
|
|
474
|
-
\`\`\`bash
|
|
475
|
-
# Install deps
|
|
476
|
-
npm install
|
|
477
|
-
|
|
478
|
-
# Build TypeScript -> JS
|
|
479
|
-
npm run build
|
|
480
|
-
|
|
481
|
-
# Start Nakama with this module
|
|
482
|
-
docker run --rm -v $(pwd)/build:/nakama/data/modules \\
|
|
483
|
-
heroiclabs/nakama:latest
|
|
484
|
-
\`\`\`
|
|
485
|
-
|
|
486
|
-
## RPC Endpoints
|
|
487
|
-
|
|
488
|
-
Call these from any Nakama client SDK:
|
|
489
|
-
|
|
490
|
-
${rpcs.join("\n")}
|
|
491
|
-
|
|
492
|
-
## Match Handlers
|
|
493
|
-
|
|
494
|
-
${realtimeMods.length > 0
|
|
495
|
-
? realtimeMods.map(m => `- \`${toSnakeCase(m.name)}\` (tick rate: ${m.config["tick_rate"] ?? 10}/s)`).join("\n")
|
|
496
|
-
: "_No realtime channels declared._"}
|
|
497
|
-
|
|
498
|
-
## Storage Collections
|
|
499
|
-
|
|
500
|
-
${system.modules.flatMap(m => m.models).map(m => `- \`${toSnakeCase(m.name)}s\``).join("\n") || "_No entities declared._"}
|
|
501
|
-
|
|
502
|
-
## Auth
|
|
503
|
-
|
|
504
|
-
All authenticated RPCs require a valid Nakama session token.
|
|
505
|
-
Use any Nakama client SDK to authenticate before calling RPCs.
|
|
506
|
-
`;
|
|
507
|
-
}
|
|
508
|
-
|
|
509
|
-
// ─── Public API ───────────────────────────────────────────────────────────────
|
|
510
|
-
|
|
511
|
-
export interface NakamaEmittedFile {
|
|
512
|
-
path: string;
|
|
513
|
-
content: string;
|
|
514
|
-
language: "typescript" | "json" | "markdown";
|
|
515
|
-
source_module: string;
|
|
516
|
-
}
|
|
517
|
-
|
|
518
|
-
export class NakamaEmitter {
|
|
519
|
-
emit(system: IR.IRSystem): NakamaEmittedFile[] {
|
|
520
|
-
const files: NakamaEmittedFile[] = [];
|
|
521
|
-
|
|
522
|
-
files.push({
|
|
523
|
-
path: "package.json",
|
|
524
|
-
content: emitNakamaPackageJson(system),
|
|
525
|
-
language: "json",
|
|
526
|
-
source_module: "root",
|
|
527
|
-
});
|
|
528
|
-
|
|
529
|
-
files.push({
|
|
530
|
-
path: "tsconfig.json",
|
|
531
|
-
content: emitNakamaTsConfig(),
|
|
532
|
-
language: "json",
|
|
533
|
-
source_module: "root",
|
|
534
|
-
});
|
|
535
|
-
|
|
536
|
-
files.push({
|
|
537
|
-
path: "src/storage.ts",
|
|
538
|
-
content: emitStorageHelpers(system),
|
|
539
|
-
language: "typescript",
|
|
540
|
-
source_module: "storage",
|
|
541
|
-
});
|
|
542
|
-
|
|
543
|
-
files.push({
|
|
544
|
-
path: "src/rpc.ts",
|
|
545
|
-
content: emitRpcHandlers(system),
|
|
546
|
-
language: "typescript",
|
|
547
|
-
source_module: "rpc",
|
|
548
|
-
});
|
|
549
|
-
|
|
550
|
-
const matchContent = emitMatchHandlers(system);
|
|
551
|
-
if (matchContent) {
|
|
552
|
-
files.push({
|
|
553
|
-
path: "src/match.ts",
|
|
554
|
-
content: matchContent,
|
|
555
|
-
language: "typescript",
|
|
556
|
-
source_module: "match",
|
|
557
|
-
});
|
|
558
|
-
}
|
|
559
|
-
|
|
560
|
-
files.push({
|
|
561
|
-
path: "src/index.ts",
|
|
562
|
-
content: emitNakamaIndex(system),
|
|
563
|
-
language: "typescript",
|
|
564
|
-
source_module: "root",
|
|
565
|
-
});
|
|
566
|
-
|
|
567
|
-
files.push({
|
|
568
|
-
path: "README.md",
|
|
569
|
-
content: emitNakamaReadme(system),
|
|
570
|
-
language: "markdown",
|
|
571
|
-
source_module: "root",
|
|
572
|
-
});
|
|
573
|
-
|
|
574
|
-
return files;
|
|
575
|
-
}
|
|
576
|
-
}
|
|
1
|
+
/**
|
|
2
|
+
* BoneScript Nakama Emitter
|
|
3
|
+
*
|
|
4
|
+
* Generates a Nakama TypeScript runtime module from an IRSystem.
|
|
5
|
+
* Output is a self-contained src/index.ts that registers:
|
|
6
|
+
* - nk.registerRpc() for every capability (capability -> RPC)
|
|
7
|
+
* - nk.registerMatch() for every realtime_service module (channel -> match handler)
|
|
8
|
+
* - Storage helpers for every entity/model (entity -> storage object)
|
|
9
|
+
* - Event hooks for every event emission (event -> stream send)
|
|
10
|
+
*
|
|
11
|
+
* Deploy with: nakama migrate up && nakama --runtime.path ./build
|
|
12
|
+
* Docs: https://heroiclabs.com/docs/nakama/server-framework/typescript-runtime/
|
|
13
|
+
*/
|
|
14
|
+
|
|
15
|
+
import * as IR from "./ir";
|
|
16
|
+
import { EmittedFile } from "./emitter";
|
|
17
|
+
|
|
18
|
+
// ─── Helpers ──────────────────────────────────────────────────────────────────
|
|
19
|
+
|
|
20
|
+
function toSnakeCase(s: string): string {
|
|
21
|
+
return s.replace(/([a-z])([A-Z])/g, "$1_$2").toLowerCase();
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
function toCamelCase(s: string): string {
|
|
25
|
+
return s.replace(/_([a-z])/g, (_, c: string) => c.toUpperCase());
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
function toPascalCase(s: string): string {
|
|
29
|
+
const c = toCamelCase(s);
|
|
30
|
+
return c.charAt(0).toUpperCase() + c.slice(1);
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
const TS_TYPE_MAP: Record<string, string> = {
|
|
34
|
+
string: "string",
|
|
35
|
+
uint: "number",
|
|
36
|
+
int: "number",
|
|
37
|
+
float: "number",
|
|
38
|
+
bool: "boolean",
|
|
39
|
+
timestamp: "string", // ISO string — Nakama runtime has no Date
|
|
40
|
+
uuid: "string",
|
|
41
|
+
bytes: "string", // base64 in Nakama
|
|
42
|
+
json: "unknown",
|
|
43
|
+
};
|
|
44
|
+
|
|
45
|
+
function toTsType(irType: string): string {
|
|
46
|
+
if (TS_TYPE_MAP[irType]) return TS_TYPE_MAP[irType];
|
|
47
|
+
const listMatch = irType.match(/^list<(.+)>$/);
|
|
48
|
+
if (listMatch) return `${toTsType(listMatch[1])}[]`;
|
|
49
|
+
const setMatch = irType.match(/^set<(.+)>$/);
|
|
50
|
+
if (setMatch) return `${toTsType(setMatch[1])}[]`;
|
|
51
|
+
const optMatch = irType.match(/^optional<(.+)>$/);
|
|
52
|
+
if (optMatch) return `${toTsType(optMatch[1])} | null`;
|
|
53
|
+
return irType;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
// ─── Package.json ─────────────────────────────────────────────────────────────
|
|
57
|
+
|
|
58
|
+
function emitNakamaPackageJson(system: IR.IRSystem): string {
|
|
59
|
+
return JSON.stringify({
|
|
60
|
+
name: toSnakeCase(system.name),
|
|
61
|
+
version: system.version,
|
|
62
|
+
private: true,
|
|
63
|
+
scripts: {
|
|
64
|
+
build: "npx tsc",
|
|
65
|
+
dev: "npx tsc --watch",
|
|
66
|
+
},
|
|
67
|
+
devDependencies: {
|
|
68
|
+
"@heroiclabs/nakama-runtime": "1.16.0",
|
|
69
|
+
typescript: "5.3.3",
|
|
70
|
+
"ts-node": "10.9.2",
|
|
71
|
+
},
|
|
72
|
+
}, null, 2);
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
// ─── tsconfig.json ────────────────────────────────────────────────────────────
|
|
76
|
+
|
|
77
|
+
function emitNakamaTsConfig(): string {
|
|
78
|
+
return JSON.stringify({
|
|
79
|
+
compilerOptions: {
|
|
80
|
+
target: "ES2020",
|
|
81
|
+
module: "commonjs",
|
|
82
|
+
lib: ["ES2020"],
|
|
83
|
+
outDir: "./build",
|
|
84
|
+
rootDir: "./src",
|
|
85
|
+
strict: true,
|
|
86
|
+
esModuleInterop: true,
|
|
87
|
+
skipLibCheck: true,
|
|
88
|
+
},
|
|
89
|
+
include: ["src/**/*"],
|
|
90
|
+
exclude: ["node_modules", "build"],
|
|
91
|
+
}, null, 2);
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
// ─── Storage helpers ──────────────────────────────────────────────────────────
|
|
95
|
+
// One collection per entity. Nakama storage: collection/key/userId pattern.
|
|
96
|
+
|
|
97
|
+
function emitStorageHelpers(system: IR.IRSystem): string {
|
|
98
|
+
const lines: string[] = [];
|
|
99
|
+
lines.push(`// Generated by BoneScript compiler. DO NOT EDIT.`);
|
|
100
|
+
lines.push(`// Storage helpers — one Nakama storage collection per entity.`);
|
|
101
|
+
lines.push(``);
|
|
102
|
+
lines.push(`const nkRuntime: nkruntime.Nakama = (globalThis as any).__nk;`);
|
|
103
|
+
lines.push(``);
|
|
104
|
+
|
|
105
|
+
for (const mod of system.modules) {
|
|
106
|
+
for (const model of mod.models) {
|
|
107
|
+
const collection = toSnakeCase(model.name) + "s";
|
|
108
|
+
const typeName = toPascalCase(model.name);
|
|
109
|
+
|
|
110
|
+
// TypeScript interface
|
|
111
|
+
lines.push(`export interface ${typeName} {`);
|
|
112
|
+
for (const field of model.fields) {
|
|
113
|
+
const nullable = field.nullable ? " | null" : "";
|
|
114
|
+
lines.push(` ${field.name}: ${toTsType(field.type)}${nullable};`);
|
|
115
|
+
}
|
|
116
|
+
lines.push(`}`);
|
|
117
|
+
lines.push(``);
|
|
118
|
+
|
|
119
|
+
// read
|
|
120
|
+
lines.push(`export function read${typeName}(nk: nkruntime.Nakama, userId: string, id: string): ${typeName} | null {`);
|
|
121
|
+
lines.push(` const objs = nk.storageRead([{ collection: "${collection}", key: id, userId }]);`);
|
|
122
|
+
lines.push(` if (objs.length === 0) return null;`);
|
|
123
|
+
lines.push(` return JSON.parse(objs[0].value) as ${typeName};`);
|
|
124
|
+
lines.push(`}`);
|
|
125
|
+
lines.push(``);
|
|
126
|
+
|
|
127
|
+
// write
|
|
128
|
+
lines.push(`export function write${typeName}(nk: nkruntime.Nakama, userId: string, obj: ${typeName}): void {`);
|
|
129
|
+
lines.push(` nk.storageWrite([{`);
|
|
130
|
+
lines.push(` collection: "${collection}",`);
|
|
131
|
+
lines.push(` key: (obj as any).id ?? userId,`);
|
|
132
|
+
lines.push(` userId,`);
|
|
133
|
+
lines.push(` value: JSON.stringify(obj),`);
|
|
134
|
+
lines.push(` permissionRead: 1,`);
|
|
135
|
+
lines.push(` permissionWrite: 0,`);
|
|
136
|
+
lines.push(` }]);`);
|
|
137
|
+
lines.push(`}`);
|
|
138
|
+
lines.push(``);
|
|
139
|
+
|
|
140
|
+
// delete
|
|
141
|
+
lines.push(`export function delete${typeName}(nk: nkruntime.Nakama, userId: string, id: string): void {`);
|
|
142
|
+
lines.push(` nk.storageDelete([{ collection: "${collection}", key: id, userId }]);`);
|
|
143
|
+
lines.push(`}`);
|
|
144
|
+
lines.push(``);
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
return lines.join("\n");
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
// ─── RPC handlers (capabilities) ─────────────────────────────────────────────
|
|
152
|
+
|
|
153
|
+
function emitRpcHandlers(system: IR.IRSystem): string {
|
|
154
|
+
const lines: string[] = [];
|
|
155
|
+
lines.push(`// Generated by BoneScript compiler. DO NOT EDIT.`);
|
|
156
|
+
lines.push(`// RPC handlers — one nk.registerRpc() per capability.`);
|
|
157
|
+
lines.push(``);
|
|
158
|
+
lines.push(`import * as storage from "./storage";`);
|
|
159
|
+
lines.push(``);
|
|
160
|
+
|
|
161
|
+
for (const mod of system.modules) {
|
|
162
|
+
for (const iface of mod.interfaces) {
|
|
163
|
+
for (const method of iface.methods) {
|
|
164
|
+
const rpcId = `${toSnakeCase(mod.name)}/${toSnakeCase(method.name)}`;
|
|
165
|
+
const fnName = `rpc${toPascalCase(mod.name)}${toPascalCase(method.name)}`;
|
|
166
|
+
|
|
167
|
+
lines.push(`// RPC: ${rpcId}`);
|
|
168
|
+
lines.push(`// Capability: ${method.name} on ${mod.name}`);
|
|
169
|
+
if (method.preconditions.length > 0) {
|
|
170
|
+
lines.push(`// Preconditions: ${method.preconditions.map(p => p.description).join(", ")}`);
|
|
171
|
+
}
|
|
172
|
+
lines.push(`const ${fnName}: nkruntime.RpcFunction = (`);
|
|
173
|
+
lines.push(` ctx: nkruntime.Context,`);
|
|
174
|
+
lines.push(` logger: nkruntime.Logger,`);
|
|
175
|
+
lines.push(` nk: nkruntime.Nakama,`);
|
|
176
|
+
lines.push(` payload: string`);
|
|
177
|
+
lines.push(`): string => {`);
|
|
178
|
+
|
|
179
|
+
// Auth guard
|
|
180
|
+
if (method.authenticated) {
|
|
181
|
+
lines.push(` if (!ctx.userId) {`);
|
|
182
|
+
lines.push(` throw Error("Authentication required");`);
|
|
183
|
+
lines.push(` }`);
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
// Parse payload
|
|
187
|
+
if (method.input.length > 0) {
|
|
188
|
+
const inputType = method.input.map(f => `${f.name}: ${toTsType(f.type)}`).join("; ");
|
|
189
|
+
lines.push(` const input: { ${inputType} } = payload ? JSON.parse(payload) : {};`);
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
// Precondition stubs
|
|
193
|
+
if (method.preconditions.length > 0) {
|
|
194
|
+
lines.push(``);
|
|
195
|
+
lines.push(` // Preconditions`);
|
|
196
|
+
for (const pre of method.preconditions) {
|
|
197
|
+
lines.push(` // CHECK: ${pre.description}`);
|
|
198
|
+
lines.push(` // if (!(${pre.expression})) throw Error("Precondition failed: ${pre.description.replace(/"/g, "'")}"); `);
|
|
199
|
+
}
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
// Effect stubs
|
|
203
|
+
if (method.effects.length > 0) {
|
|
204
|
+
lines.push(``);
|
|
205
|
+
lines.push(` // Effects`);
|
|
206
|
+
for (const eff of method.effects) {
|
|
207
|
+
const op = eff.op === "assign" ? "=" : eff.op === "add" ? "+=" : "-=";
|
|
208
|
+
lines.push(` // EFFECT: ${eff.target} ${op} ${eff.value}`);
|
|
209
|
+
}
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
// Event emissions via Nakama streams
|
|
213
|
+
if (method.emissions.length > 0) {
|
|
214
|
+
lines.push(``);
|
|
215
|
+
lines.push(` // Event emissions — broadcast to stream subscribers`);
|
|
216
|
+
for (const ev of method.emissions) {
|
|
217
|
+
lines.push(` nk.streamSend(`);
|
|
218
|
+
lines.push(` { mode: 1, subject: ctx.userId ?? "", label: "${ev}" },`);
|
|
219
|
+
lines.push(` JSON.stringify({ event: "${ev}", actor: ctx.userId, ts: Date.now() }),`);
|
|
220
|
+
lines.push(` undefined,`);
|
|
221
|
+
lines.push(` true`);
|
|
222
|
+
lines.push(` );`);
|
|
223
|
+
}
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
lines.push(``);
|
|
227
|
+
lines.push(` logger.info("rpc_called", "${rpcId}", ctx.userId ?? "anon");`);
|
|
228
|
+
lines.push(` return JSON.stringify({ ok: true, action: "${method.name}" });`);
|
|
229
|
+
lines.push(`};`);
|
|
230
|
+
lines.push(``);
|
|
231
|
+
|
|
232
|
+
// Export the function name and rpc id for registration
|
|
233
|
+
lines.push(`export const ${fnName}Id = "${rpcId}";`);
|
|
234
|
+
lines.push(`export { ${fnName} };`);
|
|
235
|
+
lines.push(``);
|
|
236
|
+
}
|
|
237
|
+
}
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
return lines.join("\n");
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
// ─── Match handler (realtime channels) ───────────────────────────────────────
|
|
244
|
+
|
|
245
|
+
function emitMatchHandlers(system: IR.IRSystem): string {
|
|
246
|
+
const realtimeMods = system.modules.filter(m => m.kind === "realtime_service");
|
|
247
|
+
if (realtimeMods.length === 0) return "";
|
|
248
|
+
|
|
249
|
+
const lines: string[] = [];
|
|
250
|
+
lines.push(`// Generated by BoneScript compiler. DO NOT EDIT.`);
|
|
251
|
+
lines.push(`// Match handlers — one per realtime_service module.`);
|
|
252
|
+
lines.push(``);
|
|
253
|
+
|
|
254
|
+
for (const mod of realtimeMods) {
|
|
255
|
+
const handlerName = `${toCamelCase(mod.name)}MatchHandler`;
|
|
256
|
+
const matchName = toSnakeCase(mod.name);
|
|
257
|
+
|
|
258
|
+
lines.push(`// Match handler for channel: ${mod.name}`);
|
|
259
|
+
lines.push(`const ${handlerName}: nkruntime.MatchHandler = {`);
|
|
260
|
+
lines.push(``);
|
|
261
|
+
|
|
262
|
+
// matchInit
|
|
263
|
+
lines.push(` matchInit(`);
|
|
264
|
+
lines.push(` ctx: nkruntime.Context,`);
|
|
265
|
+
lines.push(` logger: nkruntime.Logger,`);
|
|
266
|
+
lines.push(` nk: nkruntime.Nakama,`);
|
|
267
|
+
lines.push(` params: { [key: string]: string }`);
|
|
268
|
+
lines.push(` ): { state: unknown; tickRate: number; label: string } {`);
|
|
269
|
+
lines.push(` logger.info("match_init", "${matchName}");`);
|
|
270
|
+
lines.push(` return {`);
|
|
271
|
+
lines.push(` state: { players: {}, tick: 0 },`);
|
|
272
|
+
lines.push(` tickRate: ${mod.config["tick_rate"] ?? 10},`);
|
|
273
|
+
lines.push(` label: "${matchName}",`);
|
|
274
|
+
lines.push(` };`);
|
|
275
|
+
lines.push(` },`);
|
|
276
|
+
lines.push(``);
|
|
277
|
+
|
|
278
|
+
// matchJoinAttempt
|
|
279
|
+
lines.push(` matchJoinAttempt(`);
|
|
280
|
+
lines.push(` ctx: nkruntime.Context,`);
|
|
281
|
+
lines.push(` logger: nkruntime.Logger,`);
|
|
282
|
+
lines.push(` nk: nkruntime.Nakama,`);
|
|
283
|
+
lines.push(` dispatcher: nkruntime.MatchDispatcher,`);
|
|
284
|
+
lines.push(` tick: number,`);
|
|
285
|
+
lines.push(` state: unknown,`);
|
|
286
|
+
lines.push(` presence: nkruntime.Presence,`);
|
|
287
|
+
lines.push(` metadata: { [key: string]: any }`);
|
|
288
|
+
lines.push(` ): { state: unknown; accept: boolean; rejectMessage?: string } {`);
|
|
289
|
+
lines.push(` return { state, accept: true };`);
|
|
290
|
+
lines.push(` },`);
|
|
291
|
+
lines.push(``);
|
|
292
|
+
|
|
293
|
+
// matchJoin
|
|
294
|
+
lines.push(` matchJoin(`);
|
|
295
|
+
lines.push(` ctx: nkruntime.Context,`);
|
|
296
|
+
lines.push(` logger: nkruntime.Logger,`);
|
|
297
|
+
lines.push(` nk: nkruntime.Nakama,`);
|
|
298
|
+
lines.push(` dispatcher: nkruntime.MatchDispatcher,`);
|
|
299
|
+
lines.push(` tick: number,`);
|
|
300
|
+
lines.push(` state: unknown,`);
|
|
301
|
+
lines.push(` presences: nkruntime.Presence[]`);
|
|
302
|
+
lines.push(` ): { state: unknown } | null {`);
|
|
303
|
+
lines.push(` const s = state as any;`);
|
|
304
|
+
lines.push(` for (const p of presences) {`);
|
|
305
|
+
lines.push(` s.players[p.userId] = { userId: p.userId, sessionId: p.sessionId, joinedAt: Date.now() };`);
|
|
306
|
+
lines.push(` }`);
|
|
307
|
+
lines.push(` dispatcher.broadcastMessage(1, JSON.stringify({ type: "player_joined", players: Object.keys(s.players) }), null, null, true);`);
|
|
308
|
+
lines.push(` return { state: s };`);
|
|
309
|
+
lines.push(` },`);
|
|
310
|
+
lines.push(``);
|
|
311
|
+
|
|
312
|
+
// matchLeave
|
|
313
|
+
lines.push(` matchLeave(`);
|
|
314
|
+
lines.push(` ctx: nkruntime.Context,`);
|
|
315
|
+
lines.push(` logger: nkruntime.Logger,`);
|
|
316
|
+
lines.push(` nk: nkruntime.Nakama,`);
|
|
317
|
+
lines.push(` dispatcher: nkruntime.MatchDispatcher,`);
|
|
318
|
+
lines.push(` tick: number,`);
|
|
319
|
+
lines.push(` state: unknown,`);
|
|
320
|
+
lines.push(` presences: nkruntime.Presence[]`);
|
|
321
|
+
lines.push(` ): { state: unknown } | null {`);
|
|
322
|
+
lines.push(` const s = state as any;`);
|
|
323
|
+
lines.push(` for (const p of presences) delete s.players[p.userId];`);
|
|
324
|
+
lines.push(` if (Object.keys(s.players).length === 0) return null; // end match`);
|
|
325
|
+
lines.push(` return { state: s };`);
|
|
326
|
+
lines.push(` },`);
|
|
327
|
+
lines.push(``);
|
|
328
|
+
|
|
329
|
+
// matchLoop
|
|
330
|
+
lines.push(` matchLoop(`);
|
|
331
|
+
lines.push(` ctx: nkruntime.Context,`);
|
|
332
|
+
lines.push(` logger: nkruntime.Logger,`);
|
|
333
|
+
lines.push(` nk: nkruntime.Nakama,`);
|
|
334
|
+
lines.push(` dispatcher: nkruntime.MatchDispatcher,`);
|
|
335
|
+
lines.push(` tick: number,`);
|
|
336
|
+
lines.push(` state: unknown,`);
|
|
337
|
+
lines.push(` messages: nkruntime.MatchMessage[]`);
|
|
338
|
+
lines.push(` ): { state: unknown } | null {`);
|
|
339
|
+
lines.push(` const s = state as any;`);
|
|
340
|
+
lines.push(` s.tick = tick;`);
|
|
341
|
+
lines.push(` for (const msg of messages) {`);
|
|
342
|
+
lines.push(` // Route incoming messages to capability handlers`);
|
|
343
|
+
lines.push(` try {`);
|
|
344
|
+
lines.push(` const data = JSON.parse(nk.binaryToString(msg.data));`);
|
|
345
|
+
lines.push(` dispatcher.broadcastMessage(msg.opCode, msg.data, null, msg.sender, true);`);
|
|
346
|
+
lines.push(` } catch (e) {`);
|
|
347
|
+
lines.push(` logger.error("match_message_error", String(e));`);
|
|
348
|
+
lines.push(` }`);
|
|
349
|
+
lines.push(` }`);
|
|
350
|
+
lines.push(` return { state: s };`);
|
|
351
|
+
lines.push(` },`);
|
|
352
|
+
lines.push(``);
|
|
353
|
+
|
|
354
|
+
// matchTerminate
|
|
355
|
+
lines.push(` matchTerminate(`);
|
|
356
|
+
lines.push(` ctx: nkruntime.Context,`);
|
|
357
|
+
lines.push(` logger: nkruntime.Logger,`);
|
|
358
|
+
lines.push(` nk: nkruntime.Nakama,`);
|
|
359
|
+
lines.push(` dispatcher: nkruntime.MatchDispatcher,`);
|
|
360
|
+
lines.push(` tick: number,`);
|
|
361
|
+
lines.push(` state: unknown,`);
|
|
362
|
+
lines.push(` graceSeconds: number`);
|
|
363
|
+
lines.push(` ): { state: unknown } | null {`);
|
|
364
|
+
lines.push(` dispatcher.broadcastMessage(0, JSON.stringify({ type: "match_terminated" }), null, null, true);`);
|
|
365
|
+
lines.push(` return null;`);
|
|
366
|
+
lines.push(` },`);
|
|
367
|
+
lines.push(``);
|
|
368
|
+
|
|
369
|
+
// matchSignal
|
|
370
|
+
lines.push(` matchSignal(`);
|
|
371
|
+
lines.push(` ctx: nkruntime.Context,`);
|
|
372
|
+
lines.push(` logger: nkruntime.Logger,`);
|
|
373
|
+
lines.push(` nk: nkruntime.Nakama,`);
|
|
374
|
+
lines.push(` dispatcher: nkruntime.MatchDispatcher,`);
|
|
375
|
+
lines.push(` tick: number,`);
|
|
376
|
+
lines.push(` state: unknown,`);
|
|
377
|
+
lines.push(` data: string`);
|
|
378
|
+
lines.push(` ): { state: unknown; data: string } {`);
|
|
379
|
+
lines.push(` return { state, data };`);
|
|
380
|
+
lines.push(` },`);
|
|
381
|
+
lines.push(`};`);
|
|
382
|
+
lines.push(``);
|
|
383
|
+
lines.push(`export const ${handlerName}Name = "${matchName}";`);
|
|
384
|
+
lines.push(`export { ${handlerName} };`);
|
|
385
|
+
lines.push(``);
|
|
386
|
+
}
|
|
387
|
+
|
|
388
|
+
return lines.join("\n");
|
|
389
|
+
}
|
|
390
|
+
|
|
391
|
+
// ─── Main entry point (InitModule) ───────────────────────────────────────────
|
|
392
|
+
|
|
393
|
+
function emitNakamaIndex(system: IR.IRSystem): string {
|
|
394
|
+
const lines: string[] = [];
|
|
395
|
+
lines.push(`// Generated by BoneScript compiler. DO NOT EDIT.`);
|
|
396
|
+
lines.push(`// Nakama TypeScript runtime module for: ${system.name}`);
|
|
397
|
+
lines.push(`// Deploy: copy build/ to Nakama runtime path and restart.`);
|
|
398
|
+
lines.push(``);
|
|
399
|
+
lines.push(`import * as rpc from "./rpc";`);
|
|
400
|
+
|
|
401
|
+
const hasRealtime = system.modules.some(m => m.kind === "realtime_service");
|
|
402
|
+
if (hasRealtime) {
|
|
403
|
+
lines.push(`import * as match from "./match";`);
|
|
404
|
+
}
|
|
405
|
+
|
|
406
|
+
lines.push(``);
|
|
407
|
+
lines.push(`function InitModule(`);
|
|
408
|
+
lines.push(` ctx: nkruntime.Context,`);
|
|
409
|
+
lines.push(` logger: nkruntime.Logger,`);
|
|
410
|
+
lines.push(` nk: nkruntime.Nakama,`);
|
|
411
|
+
lines.push(` initializer: nkruntime.Initializer`);
|
|
412
|
+
lines.push(`): Error | void {`);
|
|
413
|
+
lines.push(` logger.info("init_module", "${system.name} v${system.version}");`);
|
|
414
|
+
lines.push(``);
|
|
415
|
+
|
|
416
|
+
// Register all RPCs
|
|
417
|
+
const allMethods: { mod: IR.IRModule; method: IR.IRMethod }[] = [];
|
|
418
|
+
for (const mod of system.modules) {
|
|
419
|
+
for (const iface of mod.interfaces) {
|
|
420
|
+
for (const method of iface.methods) {
|
|
421
|
+
allMethods.push({ mod, method });
|
|
422
|
+
}
|
|
423
|
+
}
|
|
424
|
+
}
|
|
425
|
+
|
|
426
|
+
if (allMethods.length > 0) {
|
|
427
|
+
lines.push(` // Register RPC handlers`);
|
|
428
|
+
for (const { mod, method } of allMethods) {
|
|
429
|
+
const fnName = `rpc${toPascalCase(mod.name)}${toPascalCase(method.name)}`;
|
|
430
|
+
lines.push(` initializer.registerRpc(rpc.${fnName}Id, rpc.${fnName});`);
|
|
431
|
+
}
|
|
432
|
+
lines.push(``);
|
|
433
|
+
}
|
|
434
|
+
|
|
435
|
+
// Register match handlers
|
|
436
|
+
if (hasRealtime) {
|
|
437
|
+
const realtimeMods = system.modules.filter(m => m.kind === "realtime_service");
|
|
438
|
+
lines.push(` // Register match handlers`);
|
|
439
|
+
for (const mod of realtimeMods) {
|
|
440
|
+
const handlerName = `${toCamelCase(mod.name)}MatchHandler`;
|
|
441
|
+
lines.push(` initializer.registerMatch(match.${handlerName}Name, match.${handlerName});`);
|
|
442
|
+
}
|
|
443
|
+
lines.push(``);
|
|
444
|
+
}
|
|
445
|
+
|
|
446
|
+
lines.push(` logger.info("init_complete", "${allMethods.length} RPCs, ${hasRealtime ? system.modules.filter(m => m.kind === "realtime_service").length : 0} match handlers");`);
|
|
447
|
+
lines.push(`}`);
|
|
448
|
+
lines.push(``);
|
|
449
|
+
lines.push(`// Required Nakama module export`);
|
|
450
|
+
lines.push(`(globalThis as any).InitModule = InitModule;`);
|
|
451
|
+
|
|
452
|
+
return lines.join("\n");
|
|
453
|
+
}
|
|
454
|
+
|
|
455
|
+
// ─── README ───────────────────────────────────────────────────────────────────
|
|
456
|
+
|
|
457
|
+
function emitNakamaReadme(system: IR.IRSystem): string {
|
|
458
|
+
const rpcs: string[] = [];
|
|
459
|
+
for (const mod of system.modules) {
|
|
460
|
+
for (const iface of mod.interfaces) {
|
|
461
|
+
for (const method of iface.methods) {
|
|
462
|
+
rpcs.push(`- \`${toSnakeCase(mod.name)}/${toSnakeCase(method.name)}\``);
|
|
463
|
+
}
|
|
464
|
+
}
|
|
465
|
+
}
|
|
466
|
+
const realtimeMods = system.modules.filter(m => m.kind === "realtime_service");
|
|
467
|
+
|
|
468
|
+
return `# ${system.name} — Nakama Backend
|
|
469
|
+
|
|
470
|
+
Generated by BoneScript compiler (target: nakama). Source hash: ${system.source_hash}
|
|
471
|
+
|
|
472
|
+
## Quick Start
|
|
473
|
+
|
|
474
|
+
\`\`\`bash
|
|
475
|
+
# Install deps
|
|
476
|
+
npm install
|
|
477
|
+
|
|
478
|
+
# Build TypeScript -> JS
|
|
479
|
+
npm run build
|
|
480
|
+
|
|
481
|
+
# Start Nakama with this module
|
|
482
|
+
docker run --rm -v $(pwd)/build:/nakama/data/modules \\
|
|
483
|
+
heroiclabs/nakama:latest
|
|
484
|
+
\`\`\`
|
|
485
|
+
|
|
486
|
+
## RPC Endpoints
|
|
487
|
+
|
|
488
|
+
Call these from any Nakama client SDK:
|
|
489
|
+
|
|
490
|
+
${rpcs.join("\n")}
|
|
491
|
+
|
|
492
|
+
## Match Handlers
|
|
493
|
+
|
|
494
|
+
${realtimeMods.length > 0
|
|
495
|
+
? realtimeMods.map(m => `- \`${toSnakeCase(m.name)}\` (tick rate: ${m.config["tick_rate"] ?? 10}/s)`).join("\n")
|
|
496
|
+
: "_No realtime channels declared._"}
|
|
497
|
+
|
|
498
|
+
## Storage Collections
|
|
499
|
+
|
|
500
|
+
${system.modules.flatMap(m => m.models).map(m => `- \`${toSnakeCase(m.name)}s\``).join("\n") || "_No entities declared._"}
|
|
501
|
+
|
|
502
|
+
## Auth
|
|
503
|
+
|
|
504
|
+
All authenticated RPCs require a valid Nakama session token.
|
|
505
|
+
Use any Nakama client SDK to authenticate before calling RPCs.
|
|
506
|
+
`;
|
|
507
|
+
}
|
|
508
|
+
|
|
509
|
+
// ─── Public API ───────────────────────────────────────────────────────────────
|
|
510
|
+
|
|
511
|
+
export interface NakamaEmittedFile {
|
|
512
|
+
path: string;
|
|
513
|
+
content: string;
|
|
514
|
+
language: "typescript" | "json" | "markdown";
|
|
515
|
+
source_module: string;
|
|
516
|
+
}
|
|
517
|
+
|
|
518
|
+
export class NakamaEmitter {
|
|
519
|
+
emit(system: IR.IRSystem): NakamaEmittedFile[] {
|
|
520
|
+
const files: NakamaEmittedFile[] = [];
|
|
521
|
+
|
|
522
|
+
files.push({
|
|
523
|
+
path: "package.json",
|
|
524
|
+
content: emitNakamaPackageJson(system),
|
|
525
|
+
language: "json",
|
|
526
|
+
source_module: "root",
|
|
527
|
+
});
|
|
528
|
+
|
|
529
|
+
files.push({
|
|
530
|
+
path: "tsconfig.json",
|
|
531
|
+
content: emitNakamaTsConfig(),
|
|
532
|
+
language: "json",
|
|
533
|
+
source_module: "root",
|
|
534
|
+
});
|
|
535
|
+
|
|
536
|
+
files.push({
|
|
537
|
+
path: "src/storage.ts",
|
|
538
|
+
content: emitStorageHelpers(system),
|
|
539
|
+
language: "typescript",
|
|
540
|
+
source_module: "storage",
|
|
541
|
+
});
|
|
542
|
+
|
|
543
|
+
files.push({
|
|
544
|
+
path: "src/rpc.ts",
|
|
545
|
+
content: emitRpcHandlers(system),
|
|
546
|
+
language: "typescript",
|
|
547
|
+
source_module: "rpc",
|
|
548
|
+
});
|
|
549
|
+
|
|
550
|
+
const matchContent = emitMatchHandlers(system);
|
|
551
|
+
if (matchContent) {
|
|
552
|
+
files.push({
|
|
553
|
+
path: "src/match.ts",
|
|
554
|
+
content: matchContent,
|
|
555
|
+
language: "typescript",
|
|
556
|
+
source_module: "match",
|
|
557
|
+
});
|
|
558
|
+
}
|
|
559
|
+
|
|
560
|
+
files.push({
|
|
561
|
+
path: "src/index.ts",
|
|
562
|
+
content: emitNakamaIndex(system),
|
|
563
|
+
language: "typescript",
|
|
564
|
+
source_module: "root",
|
|
565
|
+
});
|
|
566
|
+
|
|
567
|
+
files.push({
|
|
568
|
+
path: "README.md",
|
|
569
|
+
content: emitNakamaReadme(system),
|
|
570
|
+
language: "markdown",
|
|
571
|
+
source_module: "root",
|
|
572
|
+
});
|
|
573
|
+
|
|
574
|
+
return files;
|
|
575
|
+
}
|
|
576
|
+
}
|