forked from clusterio/gridworld
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmessages.ts
More file actions
598 lines (503 loc) · 16 KB
/
messages.ts
File metadata and controls
598 lines (503 loc) · 16 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
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
import { plainJson } from "@clusterio/lib";
import { Type, type Static } from "@sinclair/typebox";
const GridworldTile = Type.Object({
x: Type.Number(),
y: Type.Number(),
instanceId: Type.Number(),
saveName: Type.String(),
createdAtMs: Type.Number(),
});
export type GridworldTile = Static<typeof GridworldTile>;
export const GridworldStateResponse = Type.Object({
tiles: Type.Array(GridworldTile),
tileSize: Type.Number(),
initialTile: Type.Object({
x: Type.Number(),
y: Type.Number(),
}),
mapExchangeError: Type.Union([Type.String(), Type.Null()]),
});
export type GridworldStateResponse = Static<typeof GridworldStateResponse>;
export const GridworldStateValue = Type.Intersect([
Type.Object({
id: Type.Literal("state"),
updatedAtMs: Type.Number(),
isDeleted: Type.Boolean(),
}),
GridworldStateResponse,
]);
export type GridworldStateValue = Static<typeof GridworldStateValue>;
export class GridworldStateUpdate {
declare ["constructor"]: typeof GridworldStateUpdate;
static type = "event" as const;
static src = "controller" as const;
static dst = "control" as const;
static plugin = "gridworld" as const;
static permission = "gridworld.view" as const;
constructor(public updates: GridworldStateValue[]) { }
static jsonSchema = Type.Object({
updates: Type.Array(GridworldStateValue),
});
static fromJSON(json: Static<typeof this.jsonSchema>) {
return new this(json.updates);
}
}
export class GridworldStateRequest {
declare ["constructor"]: typeof GridworldStateRequest;
static type = "request" as const;
static src = "control" as const;
static dst = "controller" as const;
static plugin = "gridworld" as const;
static permission = "gridworld.view" as const;
static jsonSchema = Type.Object({});
static Response = plainJson(GridworldStateResponse);
static fromJSON() {
return new this();
}
}
export class GridworldCreateRequest {
declare ["constructor"]: typeof GridworldCreateRequest;
static type = "request" as const;
static src = "control" as const;
static dst = "controller" as const;
static plugin = "gridworld" as const;
static permission = "gridworld.manage" as const;
static jsonSchema = Type.Object({});
static Response = plainJson(GridworldStateResponse);
static fromJSON() {
return new this();
}
}
export class GridworldDeleteRequest {
declare ["constructor"]: typeof GridworldDeleteRequest;
static type = "request" as const;
static src = "control" as const;
static dst = "controller" as const;
static plugin = "gridworld" as const;
static permission = "gridworld.manage" as const;
static jsonSchema = Type.Object({});
static Response = plainJson(GridworldStateResponse);
static fromJSON() {
return new this();
}
}
export class GridworldSyncTileAreas {
declare ["constructor"]: typeof GridworldSyncTileAreas;
static type = "event" as const;
static src = "controller" as const;
static dst = "instance" as const;
static plugin = "gridworld" as const;
constructor(
public tiles: Array<{
minX: number;
maxX: number;
minY: number;
maxY: number;
surfaceName: string;
}>,
) { }
static jsonSchema = Type.Object({
tiles: Type.Array(Type.Object({
minX: Type.Number(),
maxX: Type.Number(),
minY: Type.Number(),
maxY: Type.Number(),
surfaceName: Type.String(),
})),
});
static fromJSON(json: Static<typeof this.jsonSchema>) {
return new this(json.tiles);
}
}
const RailEntity = Type.Object({
name: Type.String(),
type: Type.String(),
surface: Type.String(),
x: Type.Number(),
y: Type.Number(),
direction: Type.Number(),
// train-stop fields
stopName: Type.Optional(Type.String()),
color: Type.Optional(Type.Object({
r: Type.Number(),
g: Type.Number(),
b: Type.Number(),
a: Type.Number(),
})),
priority: Type.Optional(Type.Number()),
trainLimit: Type.Optional(Type.Union([Type.Number(), Type.Null()])),
trainCount: Type.Optional(Type.Number()),
});
export type RailEntity = Static<typeof RailEntity>;
export class GridworldSyncRailEntities {
declare ["constructor"]: typeof GridworldSyncRailEntities;
static type = "event" as const;
static src = "instance" as const;
static dst = "controller" as const;
static plugin = "gridworld" as const;
constructor(
public instanceId: number,
public tileX: number,
public tileY: number,
public tileSize: number,
public entities: RailEntity[],
) { }
static jsonSchema = Type.Object({
instanceId: Type.Number(),
tileX: Type.Number(),
tileY: Type.Number(),
tileSize: Type.Number(),
entities: Type.Array(RailEntity),
});
static fromJSON(json: Static<typeof this.jsonSchema>) {
return new this(json.instanceId, json.tileX, json.tileY, json.tileSize, json.entities);
}
}
export class GridworldApplyRailEntities {
declare ["constructor"]: typeof GridworldApplyRailEntities;
static type = "event" as const;
static src = "controller" as const;
static dst = "instance" as const;
static plugin = "gridworld" as const;
constructor(
public tileX: number,
public tileY: number,
public tileSize: number,
public entities: RailEntity[],
) { }
static jsonSchema = Type.Object({
tileX: Type.Number(),
tileY: Type.Number(),
tileSize: Type.Number(),
entities: Type.Array(RailEntity),
});
static fromJSON(json: Static<typeof this.jsonSchema>) {
return new this(json.tileX, json.tileY, json.tileSize, json.entities);
}
}
// Sent from an instance to the controller when a train needs a cross-instance path.
export class GridworldRequestTrainPath {
declare ["constructor"]: typeof GridworldRequestTrainPath;
static type = "event" as const;
static src = "instance" as const;
static dst = "controller" as const;
static plugin = "gridworld" as const;
constructor(
public id: number,
public surface: string,
public position: { x: number; y: number },
public direction: number,
public destination: string,
) { }
static jsonSchema = Type.Object({
id: Type.Number(),
surface: Type.String(),
position: Type.Object({ x: Type.Number(), y: Type.Number() }),
direction: Type.Number(),
destination: Type.String(),
});
static fromJSON(json: Static<typeof this.jsonSchema>) {
return new this(json.id, json.surface, json.position, json.direction, json.destination);
}
}
// Sent from a source instance to cancel a pending path request.
export class GridworldClearTrainPath {
declare ["constructor"]: typeof GridworldClearTrainPath;
static type = "event" as const;
static src = "instance" as const;
static dst = "controller" as const;
static plugin = "gridworld" as const;
constructor(public id: number) { }
static jsonSchema = Type.Object({ id: Type.Number() });
static fromJSON(json: Static<typeof this.jsonSchema>) {
return new this(json.id);
}
}
// Forwarded from the controller to the pathworld to cancel a queued path request.
export class GridworldForwardClearTrainPath {
declare ["constructor"]: typeof GridworldForwardClearTrainPath;
static type = "event" as const;
static src = "controller" as const;
static dst = "instance" as const;
static plugin = "gridworld" as const;
constructor(public id: number) { }
static jsonSchema = Type.Object({ id: Type.Number() });
static fromJSON(json: Static<typeof this.jsonSchema>) {
return new this(json.id);
}
}
// Forwarded from the controller to the pathworld instance to find the path.
export class GridworldForwardTrainPath {
declare ["constructor"]: typeof GridworldForwardTrainPath;
static type = "event" as const;
static src = "controller" as const;
static dst = "instance" as const;
static plugin = "gridworld" as const;
constructor(
public id: number,
public surface: string,
public position: { x: number; y: number },
public direction: number,
public destination: string,
public sourceInstanceId: number,
) { }
static jsonSchema = Type.Object({
id: Type.Number(),
surface: Type.String(),
position: Type.Object({ x: Type.Number(), y: Type.Number() }),
direction: Type.Number(),
destination: Type.String(),
sourceInstanceId: Type.Number(),
});
static fromJSON(json: Static<typeof this.jsonSchema>) {
return new this(json.id, json.surface, json.position, json.direction, json.destination, json.sourceInstanceId);
}
}
// Sent from the pathworld instance to the controller with the resolved path result.
export class GridworldReturnTrainPathResult {
declare ["constructor"]: typeof GridworldReturnTrainPathResult;
static type = "event" as const;
static src = "instance" as const;
static dst = "controller" as const;
static plugin = "gridworld" as const;
constructor(
public id: number,
public path: string[],
public sourceInstanceId: number,
) { }
static jsonSchema = Type.Object({
id: Type.Number(),
path: Type.Array(Type.String()),
sourceInstanceId: Type.Number(),
});
static fromJSON(json: Static<typeof this.jsonSchema>) {
return new this(json.id, json.path, json.sourceInstanceId);
}
}
// Sent from the controller back to the originating instance with the resolved path.
export class GridworldReturnTrainPath {
declare ["constructor"]: typeof GridworldReturnTrainPath;
static type = "event" as const;
static src = "controller" as const;
static dst = "instance" as const;
static plugin = "gridworld" as const;
constructor(
public id: number,
public path: string[],
) { }
static jsonSchema = Type.Object({
id: Type.Number(),
path: Type.Array(Type.String()),
});
static fromJSON(json: Static<typeof this.jsonSchema>) {
return new this(json.id, json.path);
}
}
// Sent from the controller to the destination instance to create a proxy train for station slot reservation.
export class GridworldCreateTrainProxy {
declare ["constructor"]: typeof GridworldCreateTrainProxy;
static type = "event" as const;
static src = "controller" as const;
static dst = "instance" as const;
static plugin = "gridworld" as const;
constructor(
public destination: string,
public edgeId: string,
public offset: number,
) { }
static jsonSchema = Type.Object({
destination: Type.String(),
edgeId: Type.String(),
offset: Type.Number(),
});
static fromJSON(json: Static<typeof this.jsonSchema>) {
return new this(json.destination, json.edgeId, json.offset);
}
}
const UeStop = Type.Object({
surface: Type.String(),
x: Type.Number(),
y: Type.Number(),
direction: Type.Number(),
stopName: Type.Optional(Type.String()),
});
export type UeStop = Static<typeof UeStop>;
// Sent from a tile instance to the controller with its current ue_source_trainstop entities.
export class GridworldSyncUeStops {
declare ["constructor"]: typeof GridworldSyncUeStops;
static type = "event" as const;
static src = "instance" as const;
static dst = "controller" as const;
static plugin = "gridworld" as const;
constructor(
public tileX: number,
public tileY: number,
public stops: UeStop[],
) { }
static jsonSchema = Type.Object({
tileX: Type.Number(),
tileY: Type.Number(),
stops: Type.Array(UeStop),
});
static fromJSON(json: Static<typeof this.jsonSchema>) {
return new this(json.tileX, json.tileY, json.stops);
}
}
// Sent from the controller to tile instances to synchronize day/night cycle.
export class GridworldSyncDaytime {
declare ["constructor"]: typeof GridworldSyncDaytime;
static type = "event" as const;
static src = "controller" as const;
static dst = "instance" as const;
static plugin = "gridworld" as const;
constructor(
public daytime: number,
public isStartup: boolean,
public ticksPerDay: number,
) { }
static jsonSchema = Type.Object({
daytime: Type.Number(),
isStartup: Type.Boolean(),
ticksPerDay: Type.Number(),
});
static fromJSON(json: Static<typeof this.jsonSchema>) {
return new this(json.daytime, json.isStartup, json.ticksPerDay);
}
}
// Sent from source instance when a train cancels; tells controller to remove proxy at destination.
export class GridworldRemoveTrainProxy {
declare ["constructor"]: typeof GridworldRemoveTrainProxy;
static type = "event" as const;
static src = "instance" as const;
static dst = "controller" as const;
static plugin = "gridworld" as const;
constructor(public lastEdgeStop: string, public destination: string) { }
static jsonSchema = Type.Object({
lastEdgeStop: Type.String(),
destination: Type.String(),
});
static fromJSON(json: Static<typeof this.jsonSchema>) {
return new this(json.lastEdgeStop, json.destination);
}
}
// Forwarded from controller to the destination instance to destroy one proxy train.
export class GridworldForwardRemoveTrainProxy {
declare ["constructor"]: typeof GridworldForwardRemoveTrainProxy;
static type = "event" as const;
static src = "controller" as const;
static dst = "instance" as const;
static plugin = "gridworld" as const;
constructor(public destination: string) { }
static jsonSchema = Type.Object({
destination: Type.String(),
});
static fromJSON(json: Static<typeof this.jsonSchema>) {
return new this(json.destination);
}
}
// Forwarded from the controller to the pathworld instance to apply ue_source_trainstops.
export class GridworldApplyUeStops {
declare ["constructor"]: typeof GridworldApplyUeStops;
static type = "event" as const;
static src = "controller" as const;
static dst = "instance" as const;
static plugin = "gridworld" as const;
constructor(
public tileX: number,
public tileY: number,
public stops: UeStop[],
) { }
static jsonSchema = Type.Object({
tileX: Type.Number(),
tileY: Type.Number(),
stops: Type.Array(UeStop),
});
static fromJSON(json: Static<typeof this.jsonSchema>) {
return new this(json.tileX, json.tileY, json.stops);
}
}
// Controller → Instance: update corner neighbor instance IDs for diagonal transport.
export class GridworldCornerNeighbors {
declare ["constructor"]: typeof GridworldCornerNeighbors;
static type = "event" as const;
static src = "controller" as const;
static dst = "instance" as const;
static plugin = "gridworld" as const;
constructor(public neighbors: {
ne?: number;
se?: number;
sw?: number;
nw?: number;
}) { }
static jsonSchema = Type.Object({
neighbors: Type.Object({
ne: Type.Optional(Type.Number()),
se: Type.Optional(Type.Number()),
sw: Type.Optional(Type.Number()),
nw: Type.Optional(Type.Number()),
}),
});
static fromJSON(json: Static<typeof this.jsonSchema>) {
return new this(json.neighbors);
}
}
// Instance → Controller: resolve server address for diagonal corner teleport.
export class GridworldCornerTeleportPlayer {
declare ["constructor"]: typeof GridworldCornerTeleportPlayer;
static type = "request" as const;
static src = "instance" as const;
static dst = "controller" as const;
static plugin = "gridworld" as const;
constructor(public playerName: string, public instanceId: number) { }
static jsonSchema = Type.Object({
playerName: Type.String(),
instanceId: Type.Number(),
});
static fromJSON(json: Static<typeof this.jsonSchema>) {
return new this(json.playerName, json.instanceId);
}
static Response = plainJson(Type.Object({
address: Type.String(),
name: Type.String(),
}));
}
// Instance → Instance: transfer entities diagonally to a corner neighbor.
export class GridworldDiagonalEntityTransfer {
declare ["constructor"]: typeof GridworldDiagonalEntityTransfer;
static type = "request" as const;
static src = "instance" as const;
static dst = "instance" as const;
static plugin = "gridworld" as const;
constructor(
public entityTransfers: Array<{
type: "player" | "vehicle";
world_position: [number, number];
player_name?: string;
serialized_entity?: Record<string, unknown>;
driver_name?: string;
passenger_name?: string;
}>,
) { }
static jsonSchema = Type.Object({
entityTransfers: Type.Array(Type.Union([
Type.Object({
type: Type.Literal("player"),
player_name: Type.String(),
world_position: Type.Tuple([Type.Number(), Type.Number()]),
}),
Type.Object({
type: Type.Literal("vehicle"),
serialized_entity: Type.Object({}),
world_position: Type.Tuple([Type.Number(), Type.Number()]),
driver_name: Type.Optional(Type.String()),
passenger_name: Type.Optional(Type.String()),
}),
])),
});
static fromJSON(json: Static<typeof this.jsonSchema>) {
return new this(json.entityTransfers);
}
static Response = plainJson(Type.Object({
success: Type.Boolean(),
}));
}