-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmessages.ts
More file actions
564 lines (478 loc) · 16.3 KB
/
messages.ts
File metadata and controls
564 lines (478 loc) · 16.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
import { plainJson } from "@clusterio/lib";
import { Type, Static } from "@sinclair/typebox";
// A stored Constructron job on the controller.
const ConstructronJobValue = Type.Object({
id: Type.String(),
updatedAtMs: Type.Number(),
isDeleted: Type.Boolean(),
lastInstanceId: Type.Number(),
jobType: Type.String(),
job: Type.Unknown(),
});
export type ConstructronJobValue = Static<typeof ConstructronJobValue>;
/**
* Event from instance -> controller to add a Constructron job.
* The `job` originates from Factorio as a LuaTable and is treated as opaque JSON.
*/
export class ConstructronJobAdd {
declare ["constructor"]: typeof ConstructronJobAdd;
static type = "event" as const;
static src = "instance" as const;
static dst = "controller" as const;
static plugin = "ctron_plugin" as const;
constructor(public instanceId: number, public jobType: string, public job: unknown, public jobKey?: string) { }
static jsonSchema = Type.Object({
instanceId: Type.Number(),
jobType: Type.String(),
job: Type.Unknown(),
jobKey: Type.Optional(Type.String()),
});
static fromJSON(json: Static<typeof this.jsonSchema>) {
return new this(json.instanceId, json.jobType, json.job, json.jobKey);
}
}
/**
* Event from instance -> controller to remove a Constructron job by controller-generated id.
*/
export class ConstructronJobRemove {
declare ["constructor"]: typeof ConstructronJobRemove;
static type = "event" as const;
static src = "instance" as const;
static dst = "controller" as const;
static plugin = "ctron_plugin" as const;
constructor(public instanceId: number, public jobId: string) { }
static jsonSchema = Type.Object({
instanceId: Type.Number(),
jobId: Type.String(),
});
static fromJSON(json: Static<typeof this.jsonSchema>) {
return new this(json.instanceId, json.jobId);
}
}
/**
* Event from instance -> controller indicating a previously added job has been
* consumed/applied on the destination instance and should be removed from the controller list.
*/
export class ConstructronJobConsume {
declare ["constructor"]: typeof ConstructronJobConsume;
static type = "event" as const;
static src = "instance" as const;
static dst = "controller" as const;
static plugin = "ctron_plugin" as const;
constructor(public instanceId: number, public jobKey: string) { }
static jsonSchema = Type.Object({
instanceId: Type.Number(),
jobKey: Type.String(),
});
static fromJSON(json: Static<typeof this.jsonSchema>) {
return new this(json.instanceId, json.jobKey);
}
}
/**
* Subscribable event from controller -> control to update job list.
*/
export class ConstructronJobUpdate {
declare ["constructor"]: typeof ConstructronJobUpdate;
static type = "event" as const;
static src = "controller" as const;
static dst = "control" as const;
static plugin = "ctron_plugin" as const;
static permission = "ctron_plugin.jobs.read";
constructor(public updates: ConstructronJobValue[]) { }
static jsonSchema = Type.Object({
updates: Type.Array(ConstructronJobValue),
});
static fromJSON(json: Static<typeof this.jsonSchema>) {
return new this(json.updates);
}
}
/**
* Request from instance -> controller to atomically claim one pending job.
* Returns the claimed job or null if none are available.
*/
export class ConstructronJobClaim {
declare ["constructor"]: typeof ConstructronJobClaim;
static type = "request" as const;
static src = "instance" as const;
static dst = "controller" as const;
static plugin = "ctron_plugin" as const;
constructor(public instanceId: number) { }
static jsonSchema = Type.Object({
instanceId: Type.Number(),
});
static fromJSON(json: Static<typeof this.jsonSchema>) {
return new this(json.instanceId);
}
static Response = plainJson(Type.Union([
Type.Object({
jobKey: Type.String(),
jobType: Type.String(),
job: Type.Unknown(),
}),
Type.Null(),
]));
}
/** Bounding box serialised for cross-instance path requests. */
const PathBoundingBox = Type.Object({
x1: Type.Number(), y1: Type.Number(),
x2: Type.Number(), y2: Type.Number(),
});
const PathWaypoint = Type.Object({
position: Type.Object({ x: Type.Number(), y: Type.Number() }),
needsDestroyToReach: Type.Boolean(),
});
/** Event instance -> controller: local pathfinding exhausted, route to pathworld. */
export class CtronPathRequest {
declare ["constructor"]: typeof CtronPathRequest;
static type = "event" as const;
static src = "instance" as const;
static dst = "controller" as const;
static plugin = "ctron_plugin" as const;
constructor(
public sourceInstanceId: number,
public requesterId: number,
public surface: string,
public boundingBox: Static<typeof PathBoundingBox>,
public start: { x: number; y: number },
public goal: { x: number; y: number },
public force: string,
public radius: number,
public pathResolutionModifier: number,
) {}
static jsonSchema = Type.Object({
sourceInstanceId: Type.Number(),
requesterId: Type.Number(),
surface: Type.String(),
boundingBox: PathBoundingBox,
start: Type.Object({ x: Type.Number(), y: Type.Number() }),
goal: Type.Object({ x: Type.Number(), y: Type.Number() }),
force: Type.String(),
radius: Type.Number(),
pathResolutionModifier: Type.Number(),
});
static fromJSON(json: Static<typeof this.jsonSchema>) {
return new this(
json.sourceInstanceId, json.requesterId, json.surface,
json.boundingBox, json.start, json.goal, json.force, json.radius,
json.pathResolutionModifier,
);
}
}
/** Event controller -> pathworld instance: forwarded path request. */
export class CtronForwardPathRequest {
declare ["constructor"]: typeof CtronForwardPathRequest;
static type = "event" as const;
static src = "controller" as const;
static dst = "instance" as const;
static plugin = "ctron_plugin" as const;
constructor(
public sourceInstanceId: number,
public requesterId: number,
public surface: string,
public boundingBox: Static<typeof PathBoundingBox>,
public start: { x: number; y: number },
public goal: { x: number; y: number },
public force: string,
public radius: number,
public pathResolutionModifier: number,
) {}
static jsonSchema = Type.Object({
sourceInstanceId: Type.Number(),
requesterId: Type.Number(),
surface: Type.String(),
boundingBox: PathBoundingBox,
start: Type.Object({ x: Type.Number(), y: Type.Number() }),
goal: Type.Object({ x: Type.Number(), y: Type.Number() }),
force: Type.String(),
radius: Type.Number(),
pathResolutionModifier: Type.Number(),
});
static fromJSON(json: Static<typeof this.jsonSchema>) {
return new this(
json.sourceInstanceId, json.requesterId, json.surface,
json.boundingBox, json.start, json.goal, json.force, json.radius,
json.pathResolutionModifier,
);
}
}
/** Event pathworld instance -> controller: path result ready. */
export class CtronPathResponse {
declare ["constructor"]: typeof CtronPathResponse;
static type = "event" as const;
static src = "instance" as const;
static dst = "controller" as const;
static plugin = "ctron_plugin" as const;
constructor(
public requesterId: number,
public sourceInstanceId: number,
public path: Array<Static<typeof PathWaypoint>> | null,
public tryAgainLater: boolean,
public partial: boolean,
public fullyCached: boolean,
) {}
static jsonSchema = Type.Object({
requesterId: Type.Number(),
sourceInstanceId: Type.Number(),
path: Type.Union([Type.Array(PathWaypoint), Type.Null()]),
tryAgainLater: Type.Boolean(),
partial: Type.Boolean(),
fullyCached: Type.Boolean(),
});
static fromJSON(json: Static<typeof this.jsonSchema>) {
return new this(
json.requesterId, json.sourceInstanceId, json.path,
json.tryAgainLater, json.partial, json.fullyCached,
);
}
}
/** Event controller -> game instance: deliver path result back. */
export class CtronReturnPathResponse {
declare ["constructor"]: typeof CtronReturnPathResponse;
static type = "event" as const;
static src = "controller" as const;
static dst = "instance" as const;
static plugin = "ctron_plugin" as const;
constructor(
public requesterId: number,
public path: Array<Static<typeof PathWaypoint>> | null,
public tryAgainLater: boolean,
public partial: boolean,
public fullyCached: boolean,
) {}
static jsonSchema = Type.Object({
requesterId: Type.Number(),
path: Type.Union([Type.Array(PathWaypoint), Type.Null()]),
tryAgainLater: Type.Boolean(),
partial: Type.Boolean(),
fullyCached: Type.Boolean(),
});
static fromJSON(json: Static<typeof this.jsonSchema>) {
return new this(
json.requesterId, json.path,
json.tryAgainLater, json.partial, json.fullyCached,
);
}
}
/**
* Per-instance service station status tracked on the controller.
* Uses the same "subscribable value" shape as other Clusterio web subscriptions.
*/
const InstanceServiceStationStatus = Type.Object({
id: Type.String(),
updatedAtMs: Type.Number(),
isDeleted: Type.Boolean(),
instanceId: Type.Number(),
serviceStationCount: Type.Number(),
isSubscriber: Type.Boolean(),
});
export type InstanceServiceStationStatus = Static<typeof InstanceServiceStationStatus>;
/**
* Event from instance -> controller whenever service station count changes.
*/
export class InstanceServiceStationStatusUpdate {
declare ["constructor"]: typeof InstanceServiceStationStatusUpdate;
static type = "event" as const;
static src = "instance" as const;
static dst = "controller" as const;
static plugin = "ctron_plugin" as const;
constructor(public instanceId: number, public serviceStationCount: number) { }
static jsonSchema = Type.Object({
instanceId: Type.Number(),
serviceStationCount: Type.Number(),
});
static fromJSON(json: Static<typeof this.jsonSchema>) {
return new this(json.instanceId, json.serviceStationCount);
}
}
/**
* Subscribable event from controller -> control to update service station status.
*/
export class InstanceServiceStationStatusStream {
declare ["constructor"]: typeof InstanceServiceStationStatusStream;
static type = "event" as const;
static src = "controller" as const;
static dst = "control" as const;
static plugin = "ctron_plugin" as const;
static permission = "ctron_plugin.jobs.read";
constructor(public updates: InstanceServiceStationStatus[]) { }
static jsonSchema = Type.Object({
updates: Type.Array(InstanceServiceStationStatus),
});
static fromJSON(json: Static<typeof this.jsonSchema>) {
return new this(json.updates);
}
}
/**
* Event from controller -> instance broadcasting subscriber availability.
*/
export class CtronSubscriberAvailabilityBroadcast {
declare ["constructor"]: typeof CtronSubscriberAvailabilityBroadcast;
static type = "event" as const;
static src = "controller" as const;
static dst = "instance" as const;
static plugin = "ctron_plugin" as const;
constructor(public hasSubscribers: boolean) { }
static jsonSchema = Type.Object({
hasSubscribers: Type.Boolean(),
});
static fromJSON(json: Static<typeof this.jsonSchema>) {
return new this(json.hasSubscribers);
}
}
/**
* Hardcoded default values for global constructron settings.
* Update here (and in Lua) when adding/removing settings.
*/
export const DEFAULT_GLOBAL_SETTINGS = {
job_start_delay: 300, // ticks (5 seconds)
debug_toggle: false,
};
/**
* Hardcoded default values for per-surface constructron settings.
* Update here (and in Lua) when adding/removing settings.
*/
export const DEFAULT_SURFACE_SETTINGS = {
horde_mode: false,
construction_job_toggle: true,
rebuild_job_toggle: true,
deconstruction_job_toggle: true,
upgrade_job_toggle: true,
repair_job_toggle: true,
destroy_job_toggle: false,
zone_restriction_job_toggle: false,
desired_robot_count: 50,
desired_robot_name: { name: "construction-robot", quality: "normal" },
repair_tool_name: { name: "repair-pack", quality: "normal" },
ammo_name: { name: "rocket", quality: "normal" },
ammo_count: 0,
atomic_ammo_name: { name: "atomic-bomb", quality: "normal" },
atomic_ammo_count: 0,
destroy_min_cluster_size: 8,
minion_count: 1,
};
/**
* Event from instance -> controller on startup to register which surfaces exist.
* The controller creates entries for new surfaces using hardcoded defaults.
*/
export class CtronSurfaceRegister {
declare ["constructor"]: typeof CtronSurfaceRegister;
static type = "event" as const;
static src = "instance" as const;
static dst = "controller" as const;
static plugin = "ctron_plugin" as const;
constructor(public surfaces: string[]) { }
static jsonSchema = Type.Object({
surfaces: Type.Array(Type.String()),
});
static fromJSON(json: Static<typeof this.jsonSchema>) {
return new this(json.surfaces);
}
}
/**
* Event from instance -> controller when a player changes constructron settings in-game.
*/
export class CtronSettingsUpdate {
declare ["constructor"]: typeof CtronSettingsUpdate;
static type = "event" as const;
static src = "instance" as const;
static dst = "controller" as const;
static plugin = "ctron_plugin" as const;
constructor(
public instanceId: number,
public surfaceName: string | null,
public settings: Record<string, unknown>,
) { }
static jsonSchema = Type.Object({
instanceId: Type.Number(),
surfaceName: Type.Union([Type.String(), Type.Null()]),
settings: Type.Record(Type.String(), Type.Unknown()),
});
static fromJSON(json: Static<typeof this.jsonSchema>) {
return new this(json.instanceId, json.surfaceName, json.settings as Record<string, unknown>);
}
}
/**
* Event from controller -> instance broadcasting settings to all instances.
*/
export class CtronSettingsBroadcast {
declare ["constructor"]: typeof CtronSettingsBroadcast;
static type = "event" as const;
static src = "controller" as const;
static dst = "instance" as const;
static plugin = "ctron_plugin" as const;
constructor(
public surfaceSettings: Record<string, Record<string, unknown>>,
public globalSettings: Record<string, unknown>,
public mode: string,
) { }
static jsonSchema = Type.Object({
surfaceSettings: Type.Record(Type.String(), Type.Record(Type.String(), Type.Unknown())),
globalSettings: Type.Record(Type.String(), Type.Unknown()),
mode: Type.String(),
});
static fromJSON(json: Static<typeof this.jsonSchema>) {
return new this(
json.surfaceSettings as Record<string, Record<string, unknown>>,
json.globalSettings as Record<string, unknown>,
json.mode,
);
}
}
/**
* Request from instance -> controller to pull current settings on startup.
*/
export class CtronSettingsPull {
declare ["constructor"]: typeof CtronSettingsPull;
static type = "request" as const;
static src = "instance" as const;
static dst = "controller" as const;
static plugin = "ctron_plugin" as const;
constructor() { }
static jsonSchema = Type.Object({});
static fromJSON(_json: Static<typeof this.jsonSchema>) { return new this(); }
static Response = plainJson(Type.Object({
surfaceSettings: Type.Record(Type.String(), Type.Record(Type.String(), Type.Unknown())),
globalSettings: Type.Record(Type.String(), Type.Unknown()),
mode: Type.String(),
hasSubscribers: Type.Boolean(),
}));
}
/**
* Request from control -> controller to set settings via web UI (controller authority mode).
*/
export class CtronSettingsSet {
declare ["constructor"]: typeof CtronSettingsSet;
static type = "request" as const;
static src = "control" as const;
static dst = "controller" as const;
static plugin = "ctron_plugin" as const;
static permission = "ctron_plugin.settings.write";
constructor(public surfaceName: string | null, public settings: Record<string, unknown>) { }
static jsonSchema = Type.Object({
surfaceName: Type.Union([Type.String(), Type.Null()]),
settings: Type.Record(Type.String(), Type.Unknown()),
});
static fromJSON(json: Static<typeof this.jsonSchema>) {
return new this(json.surfaceName, json.settings as Record<string, unknown>);
}
static Response = plainJson(Type.Object({}));
}
/**
* Request from control -> controller to get current settings.
*/
export class CtronSettingsGet {
declare ["constructor"]: typeof CtronSettingsGet;
static type = "request" as const;
static src = "control" as const;
static dst = "controller" as const;
static permission = "ctron_plugin.settings.write";
static plugin = "ctron_plugin" as const;
constructor() { }
static jsonSchema = Type.Object({});
static fromJSON(_json: Static<typeof this.jsonSchema>) { return new this(); }
static Response = plainJson(Type.Object({
surfaceSettings: Type.Record(Type.String(), Type.Record(Type.String(), Type.Unknown())),
globalSettings: Type.Record(Type.String(), Type.Unknown()),
mode: Type.String(),
}));
}