@unitn-asa/deliveroo-js-sdk 1.3.3 → 1.3.4
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 +4 -3
- package/src/client/DjsClientSocket.js +13 -6
- package/src/client/DjsConnect.js +16 -3
- package/src/global.d.ts +12 -0
- package/src/types/IOAgent.json +62 -0
- package/src/types/IOGameOptions.js +1 -2
- package/src/types/IOSocketEvents.js +20 -0
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@unitn-asa/deliveroo-js-sdk",
|
|
3
|
-
"version": "1.3.
|
|
3
|
+
"version": "1.3.4",
|
|
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
|
-
|
|
34
|
+
"devDependencies": {
|
|
35
|
+
"@types/node": "^25.6.0"
|
|
36
|
+
}
|
|
36
37
|
}
|
|
@@ -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
|
|
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 );
|
|
@@ -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(
|
|
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 =
|
|
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 (
|
|
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-expect-error
|
|
261
268
|
delete descriptors.constructor;
|
|
262
269
|
|
|
263
270
|
Object.defineProperties(target, descriptors);
|
package/src/client/DjsConnect.js
CHANGED
|
@@ -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( { clientTimestamp: performance.now() } );
|
|
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) => {
|
package/src/global.d.ts
ADDED
|
@@ -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 {
|
|
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
|
/**
|
|
@@ -4,6 +4,24 @@
|
|
|
4
4
|
/** @typedef {import('./IOInfo.js').IOInfo} IOInfo */
|
|
5
5
|
/** @typedef {import('./IOSensing.js').IOSensing} IOSensing */
|
|
6
6
|
|
|
7
|
+
/**
|
|
8
|
+
* @typedef {Object} IOLatency
|
|
9
|
+
* @property {number} roundTrip - Total round trip time in milliseconds
|
|
10
|
+
* @property {number} clientProcessing - Time spent processing on the client side in milliseconds
|
|
11
|
+
* @property {number} serverProcessing - Time spent processing on the server side in milliseconds
|
|
12
|
+
* @property {number} networkLag - Estimated network latency in milliseconds
|
|
13
|
+
* @property {number} timestamp - Timestamp when the latency was measured (in milliseconds since epoch)
|
|
14
|
+
*/
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* @typedef {Object} IOMetrics
|
|
18
|
+
* @property { {ms: number, frame: number, fps: number, uptime: number} } timing
|
|
19
|
+
* @property { {heapUsed: number, heapTotal: number, rss: number, external: number} } memory
|
|
20
|
+
* @property { {current: number, samples: number[]} } cpu
|
|
21
|
+
* @property { {currentLag: number, maxLag: number, avgLag: number} } eventLoop
|
|
22
|
+
* @property { {count: number, avg: number, min: number, max: number, samples: IOLatency[], byAgent: Object.<string, {id: string, name: string, teamId: string, teamName: string, sockets: []}>} } latency
|
|
23
|
+
*/
|
|
24
|
+
|
|
7
25
|
/**
|
|
8
26
|
* Client -> Server events. Emitted by the client and listened by the server.
|
|
9
27
|
* @typedef {{
|
|
@@ -35,6 +53,8 @@
|
|
|
35
53
|
* 'you': function ( IOAgent ) : void,
|
|
36
54
|
* 'sensing': function ( IOSensing ) : void,
|
|
37
55
|
* 'info': function ( IOInfo ) : void,
|
|
56
|
+
* 'ping': function ( { timestamp: number }, function ( { clientTimestamp: number } ) : void ) : void,
|
|
57
|
+
* 'metrics': function ( IOMetrics ) : void,
|
|
38
58
|
* 'msg': function ( string, string, Object, function ( Object ) : void = ) : Object,
|
|
39
59
|
* 'log': function ( 'server' | { socket:string, id:string, name:string }, ...any ) : void
|
|
40
60
|
* }} IOServerEvents
|