@unitn-asa/deliveroo-js-sdk 1.3.3 → 1.3.5

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@unitn-asa/deliveroo-js-sdk",
3
- "version": "1.3.3",
3
+ "version": "1.3.5",
4
4
  "description": "Software development kit for Deliveroo.js",
5
5
  "author": "Marco Robol <marco.robol@unitn.it>",
6
6
  "type": "module",
@@ -31,6 +31,7 @@
31
31
  "publishConfig": {
32
32
  "access": "public"
33
33
  },
34
- "devDependencies": {},
35
- "dependencies": {}
34
+ "devDependencies": {
35
+ "@types/node": "^25.6.0"
36
+ }
36
37
  }
@@ -5,7 +5,7 @@ import { Socket } from 'socket.io-client';
5
5
  * @typedef {import("../types/IOParcel.js").IOParcel} IOParcel
6
6
  * @typedef {import("../types/IOTile.js").IOTile} IOTile
7
7
  * @typedef {import("../types/IOConfig.js").IOConfig} IOConfig
8
- * @typedef {import("../types/IOInfo.js").IOInfo} IOInfo
8
+ * @typedef {import("../types/IOMetrics.js").IOMetrics} IOMetrics
9
9
  *
10
10
  * @typedef {import("../types/IOSocketEvents.js").IOSensing} IOSensing
11
11
  * @typedef {import("../types/IOSocketEvents.js").IOClientEvents} IOClientEvents
@@ -30,12 +30,12 @@ export class DjsClientSocket extends Socket {
30
30
 
31
31
  /** @type { Promise < IOAgent > } */
32
32
  me = new Promise( (res) => {
33
- this.once( 'you', (agent, info) => {
33
+ this.once( 'you', (agent) => {
34
34
  res( agent );
35
35
  } );
36
36
  } );
37
37
 
38
- /** @type { Promise } */
38
+ /** @type { Promise < IOConfig > } */
39
39
  config = new Promise( (res) => {
40
40
  this.once( 'config', (config) => {
41
41
  res( config );
@@ -118,10 +118,10 @@ export class DjsClientSocket extends Socket {
118
118
 
119
119
  /**
120
120
  * Listen to 'info' events
121
- * @param { function( IOInfo ) : void } callback
121
+ * @param { function( ) : void } callback
122
+ * @deprecated 'info' events are deprecated, use 'metrics' instead (only for admin)
122
123
  */
123
124
  onInfo ( callback ) {
124
- this.on( "info", callback )
125
125
  }
126
126
 
127
127
  /**
@@ -129,7 +129,7 @@ export class DjsClientSocket extends Socket {
129
129
  * @param { string } id
130
130
  * @param { string } name
131
131
  * @param { {} } msg
132
- * @param { function( any ) : void } replyAcknowledgmentCallback ( reply )
132
+ * @param { function( Object ) : void = } replyAcknowledgmentCallback ( reply )
133
133
  */
134
134
  /**
135
135
  * Listen to 'msg' events
@@ -187,6 +187,10 @@ export class DjsClientSocket extends Socket {
187
187
  } );
188
188
  }
189
189
 
190
+ /**ì
191
+ * @param {any} msg
192
+ * @returns { Promise < { any } > } reply
193
+ */
190
194
  async emitShout ( msg ) {
191
195
  return new Promise( (success) => {
192
196
  this.emit( 'shout', msg, async ( status ) => {
@@ -219,10 +223,10 @@ export class DjsClientSocket extends Socket {
219
223
  * - if array of ids is provided: putdown only specified parcels
220
224
  * - if no list is provided: put down all parcels
221
225
  * When completed, resolves to the list of dropped parcels
222
- * @param { string [] } selected array of parcels id to drop
226
+ * @param { string [] = } selected array of parcels id to drop
223
227
  * @returns { Promise < { id:string } [] >}
224
228
  */
225
- async emitPutdown ( selected = null ) {
229
+ async emitPutdown ( selected = [] ) {
226
230
  return this.emitAndResolveOnAck( 'putdown', selected );
227
231
  }
228
232
 
@@ -235,7 +239,7 @@ export class DjsClientSocket extends Socket {
235
239
 
236
240
  /**
237
241
  * Listen to 'log' events from server and those redirected here from others client
238
- * @param { function ( { src:'server'|'client', ms:number, frame: number, socket:string, id:string, name:string }, ...any) : void } callback ( { src, ms, frame, socket, id, name }, ...msgArgs )
242
+ * @param { function ( 'server' | { socket:string, id:string, name:string }, ...any) : void } callback ( { src, ms, frame, socket, id, name }, ...msgArgs )
239
243
  */
240
244
  onLog ( callback ) {
241
245
  this.on( "log", callback )
@@ -252,12 +256,15 @@ export class DjsClientSocket extends Socket {
252
256
 
253
257
  /**
254
258
  * Mixin function to copy methods from a class prototype to an object
259
+ * @param { any } target the object to enhance
260
+ * @param { any } MixinClass the class whose methods will be copied to the target
255
261
  */
256
262
  function applyMixin(target, MixinClass) {
257
263
 
258
264
  let proto = MixinClass.prototype;
259
265
 
260
266
  const descriptors = Object.getOwnPropertyDescriptors(proto);
267
+ // @ts-ignore
261
268
  delete descriptors.constructor;
262
269
 
263
270
  Object.defineProperties(target, descriptors);
@@ -25,15 +25,28 @@ export function DjsConnect ( host = process.env.HOST || 'http://localhost:8080',
25
25
  console.log( `Connecting to ${host} ${ token ? 'with token '+ (token).substring(0,5)+'...'+(token).substring(token.length-5) : name ? 'as '+name : 'with no token and no name' }` );
26
26
 
27
27
  enhancedClientSocket.onConnect( () => {
28
- console.log( `Connected` )
28
+ console.log( `Connected` );
29
29
  });
30
30
 
31
31
  enhancedClientSocket.onceYou( me => {
32
- console.log( `Authenticated as ${me.name}(${me.id}) in team ${me.teamName}(${me.teamId})` )
32
+ console.log( `Authenticated as ${me.name}(${me.id}) in team ${me.teamName}(${me.teamId})` );
33
33
  });
34
+
35
+ // Handle ping events to measure latency. The server will send a 'ping' event with a timestamp, and the client responds with a 'clientTimestamp' to allow the server to calculate round-trip time and network latency.
36
+ try {
37
+ enhancedClientSocket.on( "ping", ( pingData, callback ) => {
38
+ try {
39
+ callback();
40
+ } catch (error) {
41
+ console.warn("Error handling ping event");
42
+ }
43
+ } );
44
+ } catch (error) {
45
+ console.error("Error setting up ping event:", error);
46
+ }
34
47
 
35
48
  enhancedClientSocket.on( 'disconnect', (reason) => {
36
- console.log( `Disconnected from ${host} because ${reason}` )
49
+ console.log( `Disconnected from ${host} because ${reason}` );
37
50
  });
38
51
 
39
52
  enhancedClientSocket.io.on("error", (error) => {
@@ -10,8 +10,8 @@
10
10
  * @typedef {import("../types/IOTile.js").IOTile} IOTile
11
11
  *
12
12
  * @typedef {import("../types/IOIdentity.js").IOIdentity} IOIdentity
13
- * @typedef {import("../types/IOInfo.js").IOInfo} IOInfo
14
13
  * @typedef {import("../types/IOSensing.js").IOSensing} IOSensing
14
+ * @typedef {import("../types/IOMetrics.js").IOMetrics} IOMetrics
15
15
  *
16
16
  * @typedef {import("../types/IOSocketEvents.js").IOClientEvents} IOClientEvents
17
17
  * @typedef {import("../types/IOSocketEvents.js").IOServerEvents} IOServerEvents
@@ -0,0 +1,12 @@
1
+ /**
2
+ * Global type declarations for Node.js environment
3
+ */
4
+
5
+ declare const process: {
6
+ env: {
7
+ HOST?: string;
8
+ TOKEN?: string;
9
+ NAME?: string;
10
+ [key: string]: string | undefined;
11
+ };
12
+ };
@@ -6,7 +6,7 @@ import { DjsServerSocket } from './DjsServerSocket.js';
6
6
  * @typedef {import("../types/IOParcel.js").IOParcel} IOParcel
7
7
  * @typedef {import("../types/IOTile.js").IOTile} IOTile
8
8
  * @typedef {import("../types/IOConfig.js").IOConfig} IOConfig
9
- * @typedef {import("../types/IOInfo.js").IOInfo} IOInfo
9
+ * @typedef {import("../types/IOMetrics.js").IOMetrics} IOMetrics
10
10
  *
11
11
  * @typedef {import("../types/IOSocketEvents.js").IOSensing} IOSensing
12
12
  * @typedef {import("../types/IOSocketEvents.js").IOClientEvents} IOClientEvents on the client side these are to be emitted with .emit
@@ -67,11 +67,10 @@ export class DjsServer extends Server {
67
67
  /**
68
68
  * Broadcast a log message to all connected clients
69
69
  * @param { 'server' | { socket:string, id:string, name:string } } src - 'server' or client
70
- * @param { IOInfo } info
71
70
  * @param { ...any } message
72
71
  */
73
- broadcastLog(src, info, ...message) {
74
- this.emit('log', src, info, ...message);
72
+ broadcastLog(src, ...message) {
73
+ this.emit('log', src, ...message);
75
74
  }
76
75
 
77
76
  // /**
@@ -117,6 +116,7 @@ export class DjsServer extends Server {
117
116
  let proto = MixinClass.prototype;
118
117
 
119
118
  const descriptors = Object.getOwnPropertyDescriptors(proto);
119
+ // @ts-ignore
120
120
  delete descriptors.constructor;
121
121
 
122
122
  Object.defineProperties(target, descriptors);
@@ -5,7 +5,7 @@ import { Socket } from 'socket.io';
5
5
  * @typedef {import("../types/IOParcel.js").IOParcel} IOParcel
6
6
  * @typedef {import("../types/IOTile.js").IOTile} IOTile
7
7
  * @typedef {import("../types/IOConfig.js").IOConfig} IOConfig
8
- * @typedef {import("../types/IOInfo.js").IOInfo} IOInfo
8
+ * @typedef {import("../types/IOMetrics.js").IOMetrics} IOMetrics
9
9
  *
10
10
  * @typedef {import("../types/IOSocketEvents.js").IOSensing} IOSensing
11
11
  * @typedef {import("../types/IOSocketEvents.js").IOClientEvents} IOClientEvents on the client side these are to be emitted with .emit
@@ -28,7 +28,9 @@ export class DjsServerSocket extends Socket {
28
28
  // * @param {IOClientEvents[K]} listener
29
29
  // * @returns {void}
30
30
  // */
31
+ // @ts-ignore
31
32
  on ( event, listener ) {
33
+ // @ts-ignore
32
34
  return super.on( event, (...args) => {
33
35
  try {
34
36
  return listener.apply( this, args );
@@ -95,10 +97,10 @@ export class DjsServerSocket extends Socket {
95
97
  }
96
98
 
97
99
  /**
98
- * @param { IOInfo } info
100
+ * @param { IOMetrics } metrics
99
101
  */
100
- emitInfo ( info ) {
101
- super.emit( 'info', info );
102
+ emitMetrics ( metrics ) {
103
+ super.emit( 'metrics', metrics );
102
104
  }
103
105
 
104
106
 
@@ -106,7 +108,7 @@ export class DjsServerSocket extends Socket {
106
108
  /**
107
109
  * @callback onMoveCallback
108
110
  * @param { 'up' | 'right' | 'left' | 'down' } direction
109
- * @param { function( { x:number, y:number } | false ) : void } replyAcknowledgmentCallback ( reply )
111
+ * @param { function( { x:number, y:number } | false ) : void = } replyAcknowledgmentCallback ( reply )
110
112
  */
111
113
  /**
112
114
  * @param { onMoveCallback } callback ( direction, acknowledgementCallback )
@@ -117,7 +119,7 @@ export class DjsServerSocket extends Socket {
117
119
 
118
120
  /**
119
121
  * @callback onPickupCallback
120
- * @param { function( { id:string } [] ) : void } acknowledgementCallback
122
+ * @param { function( { id:string } [] ) : void = } acknowledgementCallback
121
123
  */
122
124
  /**
123
125
  * @param { onPickupCallback } callback ( acknowledgementCallback )
@@ -128,11 +130,11 @@ export class DjsServerSocket extends Socket {
128
130
 
129
131
  /**
130
132
  * @callback onPutdownCallback
131
- * @param { string [] } selected ids of parcels to drop
132
- * @param { function( { id:string } [] ) : void } acknowledgementCallback
133
+ * @param { string [] = } selected ids of parcels to drop
134
+ * @param { function( { id:string } [] ) : void = } acknowledgementCallback
133
135
  */
134
136
  /**
135
- * @param { onPutdownCallback } callback ( selected, acknowledgementCallback )
137
+ * @param { onPutdownCallback } callback
136
138
  */
137
139
  onPutdown ( callback ) {
138
140
  super.on( 'putdown', callback );
@@ -184,6 +186,7 @@ export class DjsServerSocket extends Socket {
184
186
  const sockets = await super.to( "agent:" + toId ).fetchSockets();
185
187
  const emissionPromises = sockets.map( socket => {
186
188
  return new Promise( (res) => {
189
+ // @ts-ignore
187
190
  socket.timeout(1000).emit( 'msg', me.id, me.name, msg, (err, response) => {
188
191
  if (err)
189
192
  res('timeout');
@@ -202,6 +205,7 @@ export class DjsServerSocket extends Socket {
202
205
  }
203
206
 
204
207
  /**
208
+ * @param { IOAgent } me
205
209
  * @param { any } msg
206
210
  * @returns { void } reply
207
211
  */
@@ -217,21 +221,18 @@ export class DjsServerSocket extends Socket {
217
221
  }
218
222
 
219
223
  /**
220
- * @param { string } myId
221
- * @param { string } myName
222
224
  * @param { 'server' | { socket:string, id:string, name:string } } src - 'server' or client
223
- * @param { IOInfo } info
224
225
  * @param { ...any } message
225
226
  */
226
- broadcastLog ( myId, myName, src, info, ...message ) {
227
- super.broadcast.emit( 'log', src, info, ...message );
227
+ broadcastLog ( src, ...message ) {
228
+ super.broadcast.emit( 'log', src, ...message );
228
229
  }
229
230
 
230
231
 
231
232
 
232
233
  /**
233
234
  * Process request for creating a parcel on x, y or disposing or setting its reward given the id
234
- * @param { function ( 'create' | 'dispose' | 'set', {id:string, x:number, y:number, reward:number} ) : void } callback
235
+ * @param { function ( 'create' | 'dispose' | 'set', { x:number, y:number } | { id:string, reward?:number } ) : void } callback
235
236
  */
236
237
  onParcel ( callback ) {
237
238
  super.on( 'parcel', callback );
@@ -265,11 +266,13 @@ export class DjsServerSocket extends Socket {
265
266
  /**
266
267
  * Mixin function to copy methods from a class prototype to an object
267
268
  */
269
+ // @ts-ignore
268
270
  function applyMixin(target, MixinClass) {
269
271
 
270
272
  let proto = MixinClass.prototype;
271
273
 
272
274
  const descriptors = Object.getOwnPropertyDescriptors(proto);
275
+ // @ts-ignore
273
276
  delete descriptors.constructor;
274
277
 
275
278
  Object.defineProperties(target, descriptors);
@@ -10,8 +10,8 @@
10
10
  * @typedef {import("../types/IOTile.js").IOTile} IOTile
11
11
  *
12
12
  * @typedef {import("../types/IOIdentity.js").IOIdentity} IOIdentity
13
- * @typedef {import("../types/IOInfo.js").IOInfo} IOInfo
14
13
  * @typedef {import("../types/IOSensing.js").IOSensing} IOSensing
14
+ * @typedef {import("../types/IOMetrics.js").IOMetrics} IOMetrics
15
15
  *
16
16
  * @typedef {import("../types/IOSocketEvents.js").IOClientEvents} IOClientEvents
17
17
  * @typedef {import("../types/IOSocketEvents.js").IOServerEvents} IOServerEvents
@@ -0,0 +1,62 @@
1
+ [
2
+ {
3
+ "type": "object",
4
+ "title": "IOAgent",
5
+ "required": [
6
+ "id",
7
+ "name",
8
+ "teamId",
9
+ "teamName",
10
+ "x",
11
+ "y",
12
+ "score",
13
+ "penalty"
14
+ ],
15
+ "properties": {
16
+ "id": {
17
+ "type": "string"
18
+ },
19
+ "name": {
20
+ "type": "string"
21
+ },
22
+ "teamId": {
23
+ "type": "string"
24
+ },
25
+ "teamName": {
26
+ "type": "string"
27
+ },
28
+ "x": {
29
+ "type": "object",
30
+ "anyOf": [
31
+ {
32
+ "classRelation": "is-a",
33
+ "$ref": "#/$defs/number"
34
+ },
35
+ {
36
+ "classRelation": "is-a",
37
+ "$ref": "#/$defs/undefined"
38
+ }
39
+ ]
40
+ },
41
+ "y": {
42
+ "type": "object",
43
+ "anyOf": [
44
+ {
45
+ "classRelation": "is-a",
46
+ "$ref": "#/$defs/number"
47
+ },
48
+ {
49
+ "classRelation": "is-a",
50
+ "$ref": "#/$defs/undefined"
51
+ }
52
+ ]
53
+ },
54
+ "score": {
55
+ "type": "number"
56
+ },
57
+ "penalty": {
58
+ "type": "number"
59
+ }
60
+ }
61
+ }
62
+ ]
@@ -26,9 +26,8 @@
26
26
  * @typedef IONpcsOptions
27
27
  * NPC configuration object
28
28
  * @property {IOClockEvent} moving_event Event whenever the NPC moves
29
- * @property {string} type NPC type (random, collector, etc.)
29
+ * @property {'random'|'intelligent'} type NPC type (random, collector, etc.)
30
30
  * @property {number} count Number of NPCs of this type
31
- * @property {number} capacity Capacity (for collector NPCs)
32
31
  */
33
32
 
34
33
  /**
@@ -0,0 +1,17 @@
1
+
2
+ /**
3
+ * @typedef {Object} IOMetrics
4
+ * @property { {ms: number, frame: number, fps: number, uptime: number} } timing
5
+ * @property { {heapUsed: number, heapTotal: number, rss: number, external: number} } memory
6
+ * @property { {current: number} } cpu
7
+ * @property { {currentLag: number, maxLag: number, avgLag: number} } eventLoop
8
+ * @property { {avg: number, min: number, max: number, byAgent: Object.<string, {
9
+ * id: string,
10
+ * name: string,
11
+ * teamId: string,
12
+ * teamName: string,
13
+ * sockets: ( { socketId: string, frame: number, avg: number, min: number, max: number } ) []
14
+ * }> } } latency
15
+ */
16
+
17
+ export { };
@@ -1,8 +1,10 @@
1
1
 
2
2
  /** @typedef {import('./IOAgent.js').IOAgent} IOAgent */
3
3
  /** @typedef {import('./IOTile.js').IOTile} IOTile */
4
- /** @typedef {import('./IOInfo.js').IOInfo} IOInfo */
5
4
  /** @typedef {import('./IOSensing.js').IOSensing} IOSensing */
5
+ /** @typedef {import('./IOMetrics.js').IOMetrics} IOMetrics */
6
+
7
+
6
8
 
7
9
  /**
8
10
  * Client -> Server events. Emitted by the client and listened by the server.
@@ -34,7 +36,8 @@
34
36
  * 'controller': function ( 'connected' | 'disconnected', {id:string, name:string, teamId:string, teamName:string, score:number} ) : void,
35
37
  * 'you': function ( IOAgent ) : void,
36
38
  * 'sensing': function ( IOSensing ) : void,
37
- * 'info': function ( IOInfo ) : void,
39
+ * 'ping': function ( { frame: number, roundTrip: number }, function () : void ) : void,
40
+ * 'metrics': function ( IOMetrics ) : void,
38
41
  * 'msg': function ( string, string, Object, function ( Object ) : void = ) : Object,
39
42
  * 'log': function ( 'server' | { socket:string, id:string, name:string }, ...any ) : void
40
43
  * }} IOServerEvents
@@ -1,11 +0,0 @@
1
-
2
- /**
3
- * @typedef IOInfo
4
- * @property {number} ms
5
- * @property {number} frame
6
- * @property {number} fps
7
- * @property {number} heapUsed
8
- * @property {number} heapTotal
9
- */
10
-
11
- export { };