-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.ts
708 lines (611 loc) · 19.7 KB
/
index.ts
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
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
const SPACE_ID = "iVeuxmC1wz9bpz3p\\ACKspace";
import { API_KEY } from "./api-key";
import { Game, WireObject, ClientServerActionAction, MapSetObjects, MapDeleteObject, SetName } from "@gathertown/gather-game-client";
import { EventObject } from "./EventObject";
global.WebSocket = require("isomorphic-ws");
const modules: EventObject[] = [];
const addModule = ( module: any, param: string ) => {
if ( !process.argv.includes( param ) )
modules.push( new module() )
};
// Step 1: Import modules here
import { Spacestate } from "./SpaceAPI";
// Step 2: Add the module with its feature disable parameter
addModule( Spacestate, "--nospaceapi" );
// Flags
const DEBUG = process.argv.includes( "--debug" ); // debug setting (object printing and feature testing)
const VERBOSE = process.argv.includes( "--verbose" ); // verbose console output
const READONLY = process.argv.includes( "--readonly" ); // "readonly" gather connection
const CHANGE_NAME = !process.argv.includes( "--nonamechange" ); // don't touch our nickname
const game = new Game( () => Promise.resolve( { apiKey: API_KEY } ) );
const engineQueue: Array<ClientServerActionAction> = [];
let mapQueueTimer: NodeJS.Timer|undefined;
// Program initialization
( () => {
console.log( new Date(), "init" );
if ( DEBUG )
console.log( "DEBUG mode");
if ( VERBOSE )
console.log( "VERBOSE mode");
console.log("initializing.. press ctrl+c to stop this script");
// Register events
modules.forEach( module => {
module.on( "objectRegister", objectRegister );
module.on( "objectChanged", objectChanged );
module.on( "objectRemove", objectRemove );
} );
} )();
game.connect( SPACE_ID );
game.subscribeToConnection( (connected) => {
if ( VERBOSE )
console.log( `connected: ${connected}` );
// TODO: Generate the name from the active modules (or set a generic name)
if ( CHANGE_NAME )
{
if ( READONLY )
{
console.log( "readonly Gather:setName", "NPC:spacestate" );
}
else
{
if ( VERBOSE )
console.log( "setting name" );
sendMapAction( {
$case: "setName",
setName: {
name: "NPC:spacestate",
},
} );
}
}
modules.forEach( module => {
module.init();
} );
} );
interface RoomObjects { [key: number]: WireObject }
const roomObjects: { [room: string]: RoomObjects } = {};
const mutexRooms: { [room: string]: Promise<boolean> } = {};
const mutexResolvers: { [room: string]: Function } = {};
const subscribers: { [id: string]: { subscriber: EventObject, key: number } } = {};
async function getMutex( _room: string, _timeout: number|undefined ): Promise<boolean>
{
if ( _room in mutexRooms )
{
// Mutex already defined, return it
return mutexRooms[ _room ];
}
else
{
const mutexResolver = new Promise<boolean>( resolve =>
{
// Assign resolver function
mutexResolvers[ _room ] = resolve;
} );
// Unresolved, return a promise that can be resolved by the data event handler
if ( _timeout )
{
// Race condition between mutex resolver and timout
mutexRooms[ _room ] = Promise.race( [ mutexResolver, timeout( _timeout, false ) ] );
}
else
{
mutexRooms[ _room ] = mutexResolver;
}
return mutexRooms[ _room ];
}
}
/** This function tries to return room objects within 5 seconds (or earlier)
* it tries to read a mutex which is already "released" or set (together with a resolver)
* for the `mapSetObjects` event to resolve after it has assigned its (new) data
*/
async function getRoomObjects( _room: string ): Promise<RoomObjects>
{
const hasRoom = await getMutex( _room, 5000 );
if ( hasRoom )
return roomObjects[ _room ];
else
return {} as RoomObjects;
}
let newKey = -1;
function getNewKey( _roomObjects: RoomObjects ):number
{
Object.keys( _roomObjects ).forEach( strkey =>
{
const key = parseInt( strkey );
if ( key > newKey )
newKey = key;
} );
return ++newKey;
}
function getObjectKey( _roomObjects: RoomObjects, _objId: string ):number|null
{
// I guess the objects are a sparse array,
// so we have the downsides of an object
// and the downsides of an array
const key = Object.keys( _roomObjects ).find( ( key ) => {
const currentObject = _roomObjects[ parseInt( key ) ];
if ( key === _objId )
console.warn( "found index as id, this is unexpected" );
return currentObject.id === _objId;
} ) || null;
if ( VERBOSE )
{
if ( key )
{
console.log( `found key ${key} for id ${_objId}` );
if ( DEBUG )
console.log( "objects", _roomObjects[ parseInt( key ) ] );
}
else
{
console.log( `id not found: "${_objId}"` );
}
}
return key ? parseInt( key ) : null;
}
function findObject( _roomObjects: RoomObjects, _objId: string ):WireObject|null
{
const key = getObjectKey( _roomObjects, _objId );
return key ? _roomObjects[ key ] : null;
}
// Object interaction
game.subscribeToEvent( "playerInteracts", (data, _context) => {
const id = data.playerInteracts.objId;
if ( VERBOSE )
console.log( `interact: ${id}` );
// Lookup object reference and interact
if ( subscribers[ id ] )
subscribers[ id ].subscriber.objectInteract( id );
else
console.warn( `Interact: no subscription for id ${id}` )
} );
// Object data from room (map)
game.subscribeToEvent( "mapSetObjects", (data, _context) =>
{
// Lookup object references (per MAP_ID) and delegate
const room = data.mapSetObjects.mapId;
// Update room data
const roomlength = roomObjects[ room ] && Object.keys(roomObjects[ room ]).length;
const initialCall = !roomObjects[ room ];
roomObjects[ room ] = Object.assign( {}, roomObjects[ room ], data.mapSetObjects.objects );
if ( VERBOSE )
console.log( `TODO: object count (${room} / ${roomlength}): ${Object.keys(data.mapSetObjects.objects).length}` );
if ( data.mapSetObjects.mapId in mutexResolvers )
{
if ( VERBOSE )
console.log( `resolving mutex for room ${room}` )
mutexResolvers[ room ]( true );
}
else
{
if ( VERBOSE )
console.log( `dummy resolve mutex for room ${room}` )
mutexResolvers[ room ] = () => { console.log( "dummy mutex resolve" ) };
mutexRooms[ room ] = Promise.resolve( true );
}
// Iterate all objects and handle object subscription
const ids: { [id: string]: number } = { };
Object.keys( roomObjects[ room ] ).find( ( key ) =>
{
const nkey = parseInt( key );
const currentObject = roomObjects[ room ][ nkey ];
if ( currentObject.id )
{
if ( currentObject.id in ids )
console.warn( `/!\\ duplicate id's for "${currentObject.id}" in ${room}: ${ids[ currentObject.id ]} and ${nkey}` );
ids[ currentObject.id ] = nkey;
}
if ( currentObject.id && subscribers[ currentObject.id ] )
{
if ( VERBOSE )
console.log( `calling setObject for identifier ${currentObject.id}` );
subscribers[ currentObject.id ].subscriber.setObject( currentObject, initialCall );
}
} );
} );
/**
* timeout promise
* @param delay amount of milliseconds to wait before timeout
* @param data the data of which the timeout will resolve to (for example `false` for comnnected timeout)
* @returns a promise that will resolve to `data` provided as parameter
*/
async function timeout<T>( delay:number, data:T ): Promise<T>
{
return new Promise( (resolve) =>
{
setTimeout( resolve.bind( null, data ), delay );
} );
}
async function objectRegister( data: { source: EventObject, room: string, id: string, create: boolean } )
{
// Register reference to this object from caller
console.log( `Object register for "${data.id}" (${data.create}) at ${data.room}` )
const objects = await getRoomObjects( data.room );
let key = getObjectKey( objects, data.id );
if ( key !== null )
{
data.source.setObject( objects[ key ], true );
}
else if ( data.create )
{
// Not found and source wants it? Let it create one for us
key = getNewKey( objects );
// NOTE: Trigger object change after `source` is a valid subscriber
}
// Store reference (currently only one per object)
// NOTE: if the object appears later, the source will not receive updates
if ( key )
{
subscribers[ data.id ] = { subscriber: data.source, key: key };
// Trigger object change which fetches the object from `source` and updates the room
if ( data.create )
objectChanged( data );
}
}
function objectChanged( data: { source: EventObject, room: string, id: string, create: boolean } )
{
// Register reference to this object from caller
if ( VERBOSE )
console.log( "object changed", data.room, data.id )
// TODO: /!\ getMutex will most likely not synchronize objectRegister reliably
// Verify subscription
if ( !subscribers[ data.id ] )
{
console.warn( `Changed: no subscription for id "${data.id}"` );
return;
}
const subscription = subscribers[ data.id ];
if ( subscription.subscriber !== data.source )
{
console.warn( `Changed: source not subscribed on id "${data.id}"` );
return;
}
const object = data.source.getObject( data.id, data.create );
if ( VERBOSE )
console.log( `set room object for key ${subscription.key}:`, object );
// Update gather
if ( !READONLY )
{
// Sanity check
if ( object )
{
sendMapAction( {
$case: "mapSetObjects",
mapSetObjects: {
mapId: data.room,
objects: {
[ subscription.key ]: object
},
},
} );
}
}
else
{
console.log( "readonly Gather:mapSetObjects", data.room, subscription.key, object );
}
}
function sendMapAction( action: ClientServerActionAction )
{
if ( mapQueueTimer )
{
// Add to queue
console.log( `queued action: ${action.$case} (${action.$case === "mapDeleteObject" ? action.mapDeleteObject.key : ""})` );
engineQueue.push( action )
}
else
{
if ( DEBUG )
console.log( `direct action call: ${action.$case}` );
// TODO: find a cleaner way to exit
if ( action.$case === "exit" )
process.exit( );
else
game.engine.sendAction( action );
// Set a "timeout" for upcoming object changes so we can roll them up in one frame
mapQueueTimer = setInterval( handleMapObjectQueue, 1000 / 15 );
}
}
function handleMapObjectQueue()
{
// Iterate array and combine objects of the same room
if ( !engineQueue.length )
{
if ( DEBUG )
console.log( "action queue empty" );
//!\ NOTE: you might need to
// change `function clearInterval(intervalId: NodeJS.Timeout): void;`
// into: `function clearInterval(intervalId: NodeJS.Timer): void;`
// inside `node_modules/@types/node/timers.d.ts` since that was incorrect at xopr's repository
if ( mapQueueTimer )
clearInterval( mapQueueTimer );
mapQueueTimer = undefined; // TODO: make this prettier
}
else
{
// Sort delete object by key (high to low), because keys get reassigned
// https://github.com/ACKspace/gather-spaceapi/issues/7
engineQueue.sort( sortAction );
// Iterate queue
for ( let n = 1; n < engineQueue.length; )
{
// Compare and splice magic
if ( compareActionDeleteCurrent( engineQueue[ 0 ], engineQueue[ n ] ) )
engineQueue.splice( n, 1 );
else
n++;
}
// Execute our combined object
if ( DEBUG )
console.log( `queued action call: ${engineQueue[ 0 ].$case}` );
// TODO: find a cleaner way to exit
if ( engineQueue[ 0 ].$case === "exit" )
process.exit( );
else
game.engine.sendAction( engineQueue.splice( 0, 1 )[ 0 ] );
}
}
function sortAction( action1: ClientServerActionAction, action2: ClientServerActionAction ): number
{
// Exit last
if ( action1.$case === "exit" )
return 1;
if ( action2.$case === "exit" )
return -1;
// mapDeleteObject second to last
if ( action1.$case === "mapDeleteObject" && action1.$case !== action2.$case )
return 1;
if ( action2.$case === "mapDeleteObject" && action1.$case !== action2.$case )
return -1;
// High key (index) first
if ( action1.$case === "mapDeleteObject" && action1.$case === action2.$case )
{
if ( action1.mapDeleteObject.key > action2.mapDeleteObject.key )
return -1;
if ( action2.mapDeleteObject.key > action1.mapDeleteObject.key )
return 1;
}
return 0;
}
function compareActionDeleteCurrent( action1: ClientServerActionAction, action2: ClientServerActionAction ): boolean
{
// Different actions?
if ( action1.$case !== action2.$case )
return false;
switch ( action1.$case )
{
case "mapSetObjects":
const setObjects1 = action1.mapSetObjects;
// The transcompiler has no way to assert we're working with the same types, so force it this way:
const setObjects2 = (action2 as any).mapSetObjects as MapSetObjects;
// Different room (map)?
if ( setObjects1.mapId !== setObjects2.mapId )
return false;
// Copy over latter objects to former
// TODO: check if assignment works
Object.keys( setObjects2.objects ).forEach( key => {
const nKey = parseInt( key );
setObjects1.objects[ nKey ] = setObjects2.objects[ nKey ];
} );
// Objects migrated: remove the latter from the list
return true;
case "mapDeleteObject":
const deleteObject1 = action1.mapDeleteObject;
// The transcompiler has no way to assert we're working with the same types, so force it this way:
const deleteObject2 = (action2 as any).mapDeleteObject as MapDeleteObject;
// Different room (map)?
if ( deleteObject1.mapId !== deleteObject2.mapId )
return false;
// Different key?
if ( deleteObject1.key !== deleteObject2.key )
return false;
// Identical delete: remove the latter from the list
console.warn( `deleting key twice: ${deleteObject1.key}` );
return true;
case "setName":
const setName1 = action1.setName;
// The transcompiler has no way to assert we're working with the same types, so force it this way:
const setName2 = (action2 as any).setName as SetName;
// Overwrite name and remove the latter from the list
setName1.name = setName2.name;
// TODO: check if assignment works
// TODO: setName1.targetId?
return true;
}
return false;
}
function objectRemove( data: { source: EventObject, room: string, id: string } )
{
// Verify subscription
if ( !subscribers[ data.id ] )
{
console.warn( `Remove: no subscription for id ${data.id}` );
return;
}
const subscription = subscribers[ data.id ];
if ( subscription.subscriber !== data.source )
{
console.warn( `Remove: source not subscribed on id ${data.id}` );
return;
}
//console.log( "TODO: verify remove object (room,id,key)", data.room, data.id, subscription.key );
// Update gather
if ( !READONLY )
{
sendMapAction( {
$case: "mapDeleteObject",
mapDeleteObject: {
mapId: data.room,
key: subscription.key
},
} );
}
else
{
console.log( "readonly Gather:mapDeleteObject", data.room, subscription.key );
}
}
// Press ctrl+c to exit the script (and cleanup)
process.on( "SIGINT", function()
{
console.log("Caught interrupt signal; cleaning up");
modules.forEach( module => {
module.destroy();
} );
if ( CHANGE_NAME )
{
if ( READONLY )
{
console.log( "readonly Gather:setName", "xopr" );
}
else
{
if ( VERBOSE )
console.log( "restoring name" );
// Restore name
sendMapAction( {
$case: "setName",
setName: {
name: "xopr",
},
} );
}
}
// TODO: find a cleaner way to exit
sendMapAction( { $case: "exit", exit: false } );
} );
/*
enum ServerClientEvent
{
Info = "info",
Warn = "warn",
Error = "error",
Ready = "ready",
ServerHeartbeat = "serverHeartbeat",
PlayerMoves = "playerMoves",
PlayerSetsStatus = "playerSetsStatus",
PlayerSpotlights = "playerSpotlights",
PlayerRings = "playerRings",
PlayerChats = "playerChats",
PlayerInteracts = "playerInteracts",
PlayerGhosts = "playerGhosts",
PlayerLeavesWhisper = "playerLeavesWhisper", // deprected?
PlayerActivelySpeaks = "playerActivelySpeaks",
PlayerSetsEmote = "playerSetsEmote",
PlayerSetsWorkCondition = "playerSetsWorkCondition",
PlayerSetsName = "playerSetsName",
PlayerSetsTextStatus = "playerSetsTextStatus",
PlayerSetsEmojiStatus = "playerSetsEmojiStatus",
PlayerSetsAffiliation = "playerSetsAffiliation",
PlayerExits = "playerExits",
PlayerSetsSprite = "playerSetsSprite",
PlayerSetsOutfitString = "playerSetsOutfitString",
PlayerSetsIsSignedIn = "playerSetsIsSignedIn",
SpaceOverwrites = "spaceOverwrites",
SpaceIsClosed = "spaceIsClosed",
PlayerEntersPortal = "playerEntersPortal",
SpaceSetsIdMapping = "spaceSetsIdMapping",
PlayerSetsLastActive = "playerSetsLastActive", // experimental
PlayerShootsConfetti = "playerShootsConfetti", // experimental
PlayerSetsEventStatus = "playerSetsEventStatus", // experimental
PlayerSetsInConversation = "playerSetsInConversation", // experimental
PlayerSetsCurrentDesk = "playerSetsCurrentDesk", // experimental
PlayerSetsCurrentArea = "playerSetsCurrentArea", // experimental
PlayerSetsImagePointer = "playerSetsImagePointer",
CookieFound = "cookieFound", // experimental
PlayerEntersWhisperV2 = "playerEntersWhisperV2",
PlayerSetsGoKartId = "playerSetsGoKartId", // experimental
MapSetDimensions = "mapSetDimensions",
MapSetCollisions = "mapSetCollisions",
MapSetBackgroundImagePath = "mapSetBackgroundImagePath",
MapSetForegroundImagePath = "mapSetForegroundImagePath",
MapSetSprites = "mapSetSprites",
MapSetSpaces = "mapSetSpaces",
MapSetSpawns = "mapSetSpawns",
MapSetPortals = "mapSetPortals",
MapSetAnnouncer = "mapSetAnnouncer",
MapSetAudio = "mapSetAudio",
MapSetAnimations = "mapSetAnimations",
MapSetAssets = "mapSetAssets",
MapSetObjects = "mapSetObjects",
MapSetName = "mapSetName",
MapSetDefaultChat = "mapSetDefaultChat",
MapSetMuteOnEntry = "mapSetMuteOnEntry",
MapSetUseDrawnBG = "mapSetUseDrawnBG",
MapSetWalls = "mapSetWalls",
MapSetFloors = "mapSetFloors",
MapSetAreas = "mapSetAreas",
MapDeleteObject = "mapDeleteObject",
PlayerSetsIsAlone = "playerSetsIsAlone", // experimental
PlayerJoins = "playerJoins",
MapSetEnabledChats = "mapSetEnabledChats",
MapSetDescription = "mapSetDescription",
MapSetDecoration = "mapSetDecoration",
MapSetTutorialTasks = "mapSetTutorialTasks",
MapSetMiniMapImagePath = "mapSetMiniMapImagePath"
}
enum ClientServerAction
{
ClientHeartbeat = "clientHeartbeat",
ClientBackupHeartbeat = "clientBackupHeartbeat",
UpdateSubscriptions = "updateSubscriptions",
Move = "move",
SetSprite = "setSprite",
SetAffiliation = "setAffiliation",
SetStatus = "setStatus",
Spotlight = "spotlight",
Ring = "ring",
Ban = "ban",
Kick = "kick",
Chat = "chat",
Interact = "interact",
EnterWhisper = "enterWhisper",
LeaveWhisper = "leaveWhisper",
SetEmojiStatus = "setEmojiStatus",
ActivelySpeaking = "activelySpeaking",
SetEmote = "setEmote",
SetName = "setName",
SetTextStatus = "setTextStatus",
Teleport = "teleport",
Exit = "exit",
Enter = "enter",
SetWorkCondition = "setWorkCondition",
Respawn = "respawn",
Spawn = "spawn",
Ghost = "ghost",
Init = "init",
SetOutfitString = "setOutfitString",
ShootConfetti = "shootConfetti", // experimental
SetEventStatus = "setEventStatus", // experimental
SetInConversation = "setInConversation", // experimental
SetCurrentDesk = "setCurrentDesk", // experimental
SetCurrentArea = "setCurrentArea", // experimental
SetImagePointer = "setImagePointer",
SetGoKartId = "setGoKartId", // experimental
MapSetDimensions = "mapSetDimensions",
MapSetCollisions = "mapSetCollisions",
MapSetBackgroundImagePath = "mapSetBackgroundImagePath",
MapSetForegroundImagePath = "mapSetForegroundImagePath",
MapSetSprites = "mapSetSprites",
MapSetSpawns = "mapSetSpawns",
MapSetSpaces = "mapSetSpaces",
MapSetPortals = "mapSetPortals",
MapSetAnnouncer = "mapSetAnnouncer",
MapSetObjects = "mapSetObjects",
MapSetName = "mapSetName",
MapSetDefaultChat = "mapSetDefaultChat",
MapSetMuteOnEntry = "mapSetMuteOnEntry",
MapSetUseDrawnBG = "mapSetUseDrawnBG",
MapSetWalls = "mapSetWalls",
MapSetFloors = "mapSetFloors",
MapSetAreas = "mapSetAreas", // experimental
MapAddObject = "mapAddObject",
MapDeleteObject = "mapDeleteObject",
SetIsAlone = "setIsAlone", // experimental
MapSetMiniMapImagePath = "mapSetMiniMapImagePath",
MapSetEnabledChats = "mapSetEnabledChats",
MapSetDescription = "mapSetDescription",
MapSetDecoration = "mapSetDecoration",
MapSetTutorialTasks = "mapSetTutorialTasks"
}
*/