@caisual/kit 0.3.0 → 0.4.0
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/dist/kit.d.ts +20 -1
- package/dist/kit.js +482 -69
- package/dist/node.d.ts +3 -1
- package/dist/node.js +247 -46
- package/dist/runtime.d.ts +4 -2
- package/dist/runtime.js +222 -42
- package/dist/server.d.ts +14 -0
- package/package.json +2 -1
package/dist/node.d.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import type { IncomingMessage } from 'node:http';
|
|
2
2
|
import type { Duplex } from 'node:stream';
|
|
3
|
-
import type { GameDefinition, JsonValue, RoomStatus } from './server.js';
|
|
3
|
+
import type { GameDefinition, JsonValue, RoomStatus, SharedGameStore } from './server.js';
|
|
4
4
|
export interface NodeWebSocket {
|
|
5
5
|
on(event: 'message', listener: (message: string) => void): this;
|
|
6
6
|
on(event: 'close', listener: () => void): this;
|
|
@@ -20,6 +20,7 @@ export interface NodeRoomManifest {
|
|
|
20
20
|
max: number;
|
|
21
21
|
};
|
|
22
22
|
lobby: boolean;
|
|
23
|
+
persistent?: boolean;
|
|
23
24
|
roles: Array<{
|
|
24
25
|
id: string;
|
|
25
26
|
min: number;
|
|
@@ -61,6 +62,7 @@ export interface NodeRoomFlush {
|
|
|
61
62
|
}
|
|
62
63
|
export interface NodeRoomOptions {
|
|
63
64
|
storageFile?: string;
|
|
65
|
+
deposito?: SharedGameStore | null;
|
|
64
66
|
}
|
|
65
67
|
export interface NodeRoom {
|
|
66
68
|
create(roomId: string, mode: string | null, creator: NodePlayerIdentity): Promise<boolean>;
|
package/dist/node.js
CHANGED
|
@@ -158,9 +158,8 @@ var COSTANTI_SHA256 = [
|
|
|
158
158
|
function ruotaDestra(value, bits) {
|
|
159
159
|
return value >>> bits | value << 32 - bits;
|
|
160
160
|
}
|
|
161
|
-
function
|
|
162
|
-
const
|
|
163
|
-
const bytes = Array.from(testo, (carattere) => carattere.charCodeAt(0));
|
|
161
|
+
function hashSeed(testo) {
|
|
162
|
+
const bytes = Array.from(new TextEncoder().encode(testo));
|
|
164
163
|
const bitLength = bytes.length * 8;
|
|
165
164
|
bytes.push(128);
|
|
166
165
|
while (bytes.length % 64 !== 56) bytes.push(0);
|
|
@@ -223,6 +222,12 @@ function seedGiornata(slug, day) {
|
|
|
223
222
|
}
|
|
224
223
|
return h0 >>> 0;
|
|
225
224
|
}
|
|
225
|
+
function seedGiornata(slug, day) {
|
|
226
|
+
return hashSeed(`caisual:${slug}:${day}`);
|
|
227
|
+
}
|
|
228
|
+
function seedStanza(roomId) {
|
|
229
|
+
return hashSeed(`caisual:room:${roomId}`);
|
|
230
|
+
}
|
|
226
231
|
|
|
227
232
|
// src/stanza/nucleo.ts
|
|
228
233
|
var CHIAVE_NUCLEO = "nucleo";
|
|
@@ -230,13 +235,25 @@ var PREFISSO_SAVE = "save:";
|
|
|
230
235
|
var LIMITE_FRAME = 16 * 1024;
|
|
231
236
|
var LIMITE_STATO = 256 * 1024;
|
|
232
237
|
var LIMITE_SAVE = 128 * 1024;
|
|
238
|
+
var LIMITE_DEPOSITO = 64 * 1024;
|
|
239
|
+
var LIMITE_OPERAZIONI_DEPOSITO = 120;
|
|
233
240
|
var GRAZIA_MS = 6e4;
|
|
234
241
|
var STANZA_VUOTA_MS = 5 * 6e4;
|
|
235
242
|
var COUNTDOWN_MS = 3e3;
|
|
236
243
|
var RIPOSO_TICK_MS = 3e4;
|
|
237
244
|
var INATTIVITA_MS = 10 * 6e4;
|
|
245
|
+
var SCADENZA_PERSISTENTE_MS = 30 * 24 * 60 * 6e4;
|
|
238
246
|
var CHIAVE = /^[a-z0-9][a-z0-9_-]{0,31}$/;
|
|
247
|
+
var PREFISSO_CHIAVE = /^[a-z0-9_-]{0,32}$/;
|
|
239
248
|
var GAME_DEFINITION = /* @__PURE__ */ Symbol.for("@caisual/kit/game-definition");
|
|
249
|
+
var CODICI_DEPOSITO = /* @__PURE__ */ new Set([
|
|
250
|
+
"store_invalid_key",
|
|
251
|
+
"store_too_large",
|
|
252
|
+
"store_full",
|
|
253
|
+
"store_not_integer",
|
|
254
|
+
"store_unavailable",
|
|
255
|
+
"store_rate_limited"
|
|
256
|
+
]);
|
|
240
257
|
function record(value) {
|
|
241
258
|
return typeof value === "object" && value !== null && !Array.isArray(value) ? value : null;
|
|
242
259
|
}
|
|
@@ -259,6 +276,9 @@ function copiaGiocatore(player) {
|
|
|
259
276
|
function copiaJson(value) {
|
|
260
277
|
return JSON.parse(JSON.stringify(value));
|
|
261
278
|
}
|
|
279
|
+
function erroreConCodice(code, message) {
|
|
280
|
+
return Object.assign(new Error(message), { code });
|
|
281
|
+
}
|
|
262
282
|
function codiceIngresso(code) {
|
|
263
283
|
if (code === "room_not_found") return 4001;
|
|
264
284
|
if (code === "room_full") return 4e3;
|
|
@@ -271,6 +291,7 @@ var NucleoStanza = class _NucleoStanza {
|
|
|
271
291
|
this.adattatore = adattatore;
|
|
272
292
|
this.dati = null;
|
|
273
293
|
this.frequenza = /* @__PURE__ */ new Map();
|
|
294
|
+
this.frequenzaDeposito = [];
|
|
274
295
|
this.kickRichiesti = /* @__PURE__ */ new Set();
|
|
275
296
|
this.voceGuadagniCambiati = /* @__PURE__ */ new Map();
|
|
276
297
|
this.fineRichiesta = null;
|
|
@@ -290,6 +311,9 @@ var NucleoStanza = class _NucleoStanza {
|
|
|
290
311
|
get id() {
|
|
291
312
|
return nucleo.richiediDati().id;
|
|
292
313
|
},
|
|
314
|
+
get seed() {
|
|
315
|
+
return seedStanza(nucleo.richiediDati().id);
|
|
316
|
+
},
|
|
293
317
|
get mode() {
|
|
294
318
|
return nucleo.richiediDati().mode;
|
|
295
319
|
},
|
|
@@ -299,6 +323,12 @@ var NucleoStanza = class _NucleoStanza {
|
|
|
299
323
|
get tick() {
|
|
300
324
|
return nucleo.richiediDati().tick;
|
|
301
325
|
},
|
|
326
|
+
get tickRate() {
|
|
327
|
+
return nucleo.richiediDati().tickRate;
|
|
328
|
+
},
|
|
329
|
+
get result() {
|
|
330
|
+
return nucleo.richiediDati().result;
|
|
331
|
+
},
|
|
302
332
|
get state() {
|
|
303
333
|
return nucleo.richiediDati().state;
|
|
304
334
|
},
|
|
@@ -322,6 +352,12 @@ var NucleoStanza = class _NucleoStanza {
|
|
|
322
352
|
kick(player) {
|
|
323
353
|
nucleo.kickRichiesti.add(idGiocatore(player));
|
|
324
354
|
},
|
|
355
|
+
setRole(player, role) {
|
|
356
|
+
nucleo.impostaRuoloDalServer(idGiocatore(player), role);
|
|
357
|
+
},
|
|
358
|
+
setTeam(player, team) {
|
|
359
|
+
nucleo.impostaSquadraDalServer(idGiocatore(player), team);
|
|
360
|
+
},
|
|
325
361
|
end(result) {
|
|
326
362
|
nucleo.richiediFine(result);
|
|
327
363
|
},
|
|
@@ -331,6 +367,23 @@ var NucleoStanza = class _NucleoStanza {
|
|
|
331
367
|
load(key) {
|
|
332
368
|
return nucleo.caricaSave(key);
|
|
333
369
|
},
|
|
370
|
+
shared: {
|
|
371
|
+
get(key) {
|
|
372
|
+
return nucleo.leggiDeposito(key);
|
|
373
|
+
},
|
|
374
|
+
set(key, value) {
|
|
375
|
+
return nucleo.scriviDeposito(key, value);
|
|
376
|
+
},
|
|
377
|
+
delete(key) {
|
|
378
|
+
return nucleo.eliminaDeposito(key);
|
|
379
|
+
},
|
|
380
|
+
list(prefix) {
|
|
381
|
+
return nucleo.elencaDeposito(prefix);
|
|
382
|
+
},
|
|
383
|
+
increment(key, amount = 1) {
|
|
384
|
+
return nucleo.incrementaDeposito(key, amount);
|
|
385
|
+
}
|
|
386
|
+
},
|
|
334
387
|
schedule(milliseconds, handler, payload) {
|
|
335
388
|
nucleo.pianifica(milliseconds, handler, payload);
|
|
336
389
|
},
|
|
@@ -345,8 +398,14 @@ var NucleoStanza = class _NucleoStanza {
|
|
|
345
398
|
get mode() {
|
|
346
399
|
return nucleo.manifest.voice ?? "none";
|
|
347
400
|
},
|
|
401
|
+
setGain(listener, speaker, gain) {
|
|
402
|
+
nucleo.impostaGuadagno(idGiocatore(listener), idGiocatore(speaker), gain);
|
|
403
|
+
},
|
|
348
404
|
setProximity(a, b, gain) {
|
|
349
|
-
|
|
405
|
+
const primo = idGiocatore(a);
|
|
406
|
+
const secondo = idGiocatore(b);
|
|
407
|
+
nucleo.impostaGuadagno(primo, secondo, gain);
|
|
408
|
+
nucleo.impostaGuadagno(secondo, primo, gain);
|
|
350
409
|
}
|
|
351
410
|
}
|
|
352
411
|
};
|
|
@@ -371,7 +430,7 @@ var NucleoStanza = class _NucleoStanza {
|
|
|
371
430
|
return nucleo;
|
|
372
431
|
}
|
|
373
432
|
static verificaManifest(manifest) {
|
|
374
|
-
if (typeof manifest?.id !== "string" || !Number.isInteger(manifest.players?.min) || !Number.isInteger(manifest.players?.max) || manifest.players.min < 1 || manifest.players.max < manifest.players.min || typeof manifest.lobby !== "boolean" || !Array.isArray(manifest.roles) || !Array.isArray(manifest.modes) || manifest.voice !== void 0 && !["none", "room", "team", "proximity"].includes(manifest.voice)) {
|
|
433
|
+
if (typeof manifest?.id !== "string" || !Number.isInteger(manifest.players?.min) || !Number.isInteger(manifest.players?.max) || manifest.players.min < 1 || manifest.players.max < manifest.players.min || typeof manifest.lobby !== "boolean" || manifest.persistent !== void 0 && typeof manifest.persistent !== "boolean" || !Array.isArray(manifest.roles) || !Array.isArray(manifest.modes) || manifest.voice !== void 0 && !["none", "room", "team", "proximity"].includes(manifest.voice)) {
|
|
375
434
|
throw new TypeError("The room manifest is invalid.");
|
|
376
435
|
}
|
|
377
436
|
}
|
|
@@ -434,16 +493,17 @@ var NucleoStanza = class _NucleoStanza {
|
|
|
434
493
|
await this.persistiEProgramma();
|
|
435
494
|
return true;
|
|
436
495
|
}
|
|
437
|
-
info() {
|
|
496
|
+
info(playerId) {
|
|
438
497
|
if (this.dati === null) return null;
|
|
439
498
|
return {
|
|
440
499
|
roomId: this.dati.id,
|
|
441
500
|
status: this.dati.status,
|
|
442
|
-
players: this.dati.giocatori.filter(
|
|
501
|
+
players: this.manifest.persistent === true ? this.dati.giocatori.length : this.dati.giocatori.filter(
|
|
443
502
|
(player) => player.connected || player.graziaFinoA !== null
|
|
444
503
|
).length,
|
|
445
504
|
max: this.manifest.players.max,
|
|
446
|
-
mode: this.dati.mode
|
|
505
|
+
mode: this.dati.mode,
|
|
506
|
+
member: playerId === void 0 ? false : this.dati.giocatori.some((player) => player.id === playerId)
|
|
447
507
|
};
|
|
448
508
|
}
|
|
449
509
|
giocatoreConnesso(connessione) {
|
|
@@ -489,7 +549,6 @@ var NucleoStanza = class _NucleoStanza {
|
|
|
489
549
|
seq: 0
|
|
490
550
|
};
|
|
491
551
|
dati.giocatori.push(player);
|
|
492
|
-
dati.hostId ??= player.id;
|
|
493
552
|
} else {
|
|
494
553
|
if (player.connected && player.connessione !== null && player.connessione !== connessione) {
|
|
495
554
|
this.adattatore.chiudi(player.connessione, 4006, "replaced");
|
|
@@ -501,6 +560,7 @@ var NucleoStanza = class _NucleoStanza {
|
|
|
501
560
|
player.connessione = connessione;
|
|
502
561
|
player.seq = 0;
|
|
503
562
|
}
|
|
563
|
+
dati.hostId ??= player.id;
|
|
504
564
|
dati.vuotaDa = null;
|
|
505
565
|
dati.ultimoInputAt = ora;
|
|
506
566
|
const primaConnessione = !this.manifest.lobby && dati.status === "lobby";
|
|
@@ -534,6 +594,7 @@ var NucleoStanza = class _NucleoStanza {
|
|
|
534
594
|
player.connected = false;
|
|
535
595
|
player.connessione = null;
|
|
536
596
|
player.graziaFinoA = this.adattatore.ora() + GRAZIA_MS;
|
|
597
|
+
if (this.manifest.persistent === true && this.dati.status === "lobby") player.ready = false;
|
|
537
598
|
this.frequenza.delete(connessione);
|
|
538
599
|
if (this.dati.hostId === player.id) this.assegnaHost();
|
|
539
600
|
this.verificaCountdown();
|
|
@@ -603,7 +664,8 @@ var NucleoStanza = class _NucleoStanza {
|
|
|
603
664
|
if (typeof message.role !== "string") return this.messaggioErrato(player);
|
|
604
665
|
if (!this.inLobby(player)) return;
|
|
605
666
|
this.dati.ultimoInputAt = ora;
|
|
606
|
-
this.
|
|
667
|
+
const errore = this.cambiaRuolo(player, message.role);
|
|
668
|
+
if (errore !== null) this.inviaErrore(player, errore.code, errore.message);
|
|
607
669
|
await this.persistiEProgramma();
|
|
608
670
|
return;
|
|
609
671
|
}
|
|
@@ -611,7 +673,8 @@ var NucleoStanza = class _NucleoStanza {
|
|
|
611
673
|
if (!Number.isInteger(message.team)) return this.messaggioErrato(player);
|
|
612
674
|
if (!this.inLobby(player)) return;
|
|
613
675
|
this.dati.ultimoInputAt = ora;
|
|
614
|
-
this.
|
|
676
|
+
const errore = this.cambiaSquadra(player, message.team);
|
|
677
|
+
if (errore !== null) this.inviaErrore(player, errore.code, errore.message);
|
|
615
678
|
await this.persistiEProgramma();
|
|
616
679
|
return;
|
|
617
680
|
}
|
|
@@ -701,7 +764,10 @@ var NucleoStanza = class _NucleoStanza {
|
|
|
701
764
|
const scaduti = this.dati.giocatori.filter(
|
|
702
765
|
(player) => !player.connected && player.graziaFinoA !== null && player.graziaFinoA <= ora
|
|
703
766
|
);
|
|
704
|
-
for (const player of scaduti)
|
|
767
|
+
for (const player of scaduti) {
|
|
768
|
+
if (this.manifest.persistent === true) player.graziaFinoA = null;
|
|
769
|
+
else await this.rimuoviGiocatore(player, "timeout");
|
|
770
|
+
}
|
|
705
771
|
if (this.dati.status === "countdown" && this.dati.countdownAt !== null && this.dati.countdownAt <= ora) {
|
|
706
772
|
const errore = this.erroreMinimi();
|
|
707
773
|
if (errore !== null) {
|
|
@@ -761,6 +827,7 @@ var NucleoStanza = class _NucleoStanza {
|
|
|
761
827
|
player.connected = false;
|
|
762
828
|
player.connessione = null;
|
|
763
829
|
player.graziaFinoA = ora + GRAZIA_MS;
|
|
830
|
+
if (this.manifest.persistent === true && dati.status === "lobby") player.ready = false;
|
|
764
831
|
cambiato = true;
|
|
765
832
|
}
|
|
766
833
|
}
|
|
@@ -804,37 +871,52 @@ var NucleoStanza = class _NucleoStanza {
|
|
|
804
871
|
this.inviaErrore(player, "not_in_lobby", "This action is only available in the lobby.");
|
|
805
872
|
return false;
|
|
806
873
|
}
|
|
807
|
-
|
|
874
|
+
cambiaRuolo(player, roleId) {
|
|
808
875
|
const ruolo = this.manifest.roles.find((item) => item.id === roleId);
|
|
809
876
|
if (ruolo === void 0 && roleId !== "spectator") {
|
|
810
|
-
|
|
811
|
-
return;
|
|
877
|
+
return { code: "invalid_role", message: "This role does not exist." };
|
|
812
878
|
}
|
|
813
879
|
const occupati = this.richiediDati().giocatori.filter(
|
|
814
880
|
(item) => item.id !== player.id && item.role === roleId
|
|
815
881
|
).length;
|
|
816
882
|
if (ruolo?.max !== void 0 && occupati >= ruolo.max) {
|
|
817
|
-
|
|
818
|
-
return;
|
|
883
|
+
return { code: "role_full", message: "This role is full." };
|
|
819
884
|
}
|
|
820
885
|
player.role = roleId;
|
|
821
886
|
if (roleId === "spectator") player.team = null;
|
|
822
887
|
else if (player.team === null) player.team = this.squadraAutomatica();
|
|
823
|
-
player.ready = false;
|
|
888
|
+
if (this.richiediDati().status === "lobby") player.ready = false;
|
|
824
889
|
this.inviaGiocatori();
|
|
890
|
+
return null;
|
|
825
891
|
}
|
|
826
|
-
|
|
827
|
-
if (this.manifest.teams === null || team < 1 || team > this.manifest.teams.max) {
|
|
828
|
-
|
|
829
|
-
return;
|
|
892
|
+
cambiaSquadra(player, team) {
|
|
893
|
+
if (this.manifest.teams === null || !Number.isInteger(team) || team < 1 || team > this.manifest.teams.max) {
|
|
894
|
+
return { code: "invalid_team", message: "This team does not exist." };
|
|
830
895
|
}
|
|
831
896
|
if (player.role === "spectator") {
|
|
832
|
-
|
|
833
|
-
return;
|
|
897
|
+
return { code: "spectator", message: "Spectators cannot join a team." };
|
|
834
898
|
}
|
|
835
899
|
player.team = team;
|
|
836
|
-
player.ready = false;
|
|
900
|
+
if (this.richiediDati().status === "lobby") player.ready = false;
|
|
837
901
|
this.inviaGiocatori();
|
|
902
|
+
return null;
|
|
903
|
+
}
|
|
904
|
+
impostaRuoloDalServer(playerId, roleId) {
|
|
905
|
+
const player = this.giocatorePerModificaDalServer(playerId);
|
|
906
|
+
const errore = this.cambiaRuolo(player, roleId);
|
|
907
|
+
if (errore !== null) throw erroreConCodice(errore.code, errore.message);
|
|
908
|
+
}
|
|
909
|
+
impostaSquadraDalServer(playerId, team) {
|
|
910
|
+
const player = this.giocatorePerModificaDalServer(playerId);
|
|
911
|
+
const errore = this.cambiaSquadra(player, team);
|
|
912
|
+
if (errore !== null) throw erroreConCodice(errore.code, errore.message);
|
|
913
|
+
}
|
|
914
|
+
giocatorePerModificaDalServer(playerId) {
|
|
915
|
+
const dati = this.richiediDati();
|
|
916
|
+
if (dati.status === "ended") throw new Error("The room has ended.");
|
|
917
|
+
const player = dati.giocatori.find((item) => item.id === playerId);
|
|
918
|
+
if (player === void 0) throw erroreConCodice("player_not_found", "Player not found.");
|
|
919
|
+
return player;
|
|
838
920
|
}
|
|
839
921
|
erroreMinimi() {
|
|
840
922
|
const connessi = this.richiediDati().giocatori.filter((player) => player.connected);
|
|
@@ -978,6 +1060,91 @@ var NucleoStanza = class _NucleoStanza {
|
|
|
978
1060
|
if (!json.ok) throw new Error("The saved value is invalid.");
|
|
979
1061
|
return json.valore;
|
|
980
1062
|
}
|
|
1063
|
+
verificaChiaveDeposito(key) {
|
|
1064
|
+
if (typeof key !== "string" || !CHIAVE.test(key)) {
|
|
1065
|
+
throw erroreConCodice(
|
|
1066
|
+
"store_invalid_key",
|
|
1067
|
+
"Shared store keys must use lowercase letters, numbers, underscores, or hyphens."
|
|
1068
|
+
);
|
|
1069
|
+
}
|
|
1070
|
+
}
|
|
1071
|
+
verificaPrefissoDeposito(prefix) {
|
|
1072
|
+
if (prefix !== void 0 && (typeof prefix !== "string" || !PREFISSO_CHIAVE.test(prefix))) {
|
|
1073
|
+
throw erroreConCodice(
|
|
1074
|
+
"store_invalid_key",
|
|
1075
|
+
"Shared store prefixes may contain lowercase letters, numbers, underscores, or hyphens."
|
|
1076
|
+
);
|
|
1077
|
+
}
|
|
1078
|
+
}
|
|
1079
|
+
contaOperazioneDeposito() {
|
|
1080
|
+
const ora = this.adattatore.ora();
|
|
1081
|
+
this.frequenzaDeposito = this.frequenzaDeposito.filter((at) => ora - at < 6e4);
|
|
1082
|
+
if (this.frequenzaDeposito.length >= LIMITE_OPERAZIONI_DEPOSITO) {
|
|
1083
|
+
throw erroreConCodice(
|
|
1084
|
+
"store_rate_limited",
|
|
1085
|
+
"The shared store allows at most 120 operations per minute for each room."
|
|
1086
|
+
);
|
|
1087
|
+
}
|
|
1088
|
+
this.frequenzaDeposito.push(ora);
|
|
1089
|
+
}
|
|
1090
|
+
async usaDeposito(operazione) {
|
|
1091
|
+
this.contaOperazioneDeposito();
|
|
1092
|
+
const deposito = this.adattatore.deposito;
|
|
1093
|
+
if (deposito === null) {
|
|
1094
|
+
throw erroreConCodice("store_unavailable", "The shared store is unavailable.");
|
|
1095
|
+
}
|
|
1096
|
+
try {
|
|
1097
|
+
return await operazione(deposito);
|
|
1098
|
+
} catch (cause) {
|
|
1099
|
+
const code = cause instanceof Error ? cause.code : void 0;
|
|
1100
|
+
if (typeof code === "string" && CODICI_DEPOSITO.has(code)) throw cause;
|
|
1101
|
+
throw erroreConCodice("store_unavailable", "The shared store is unavailable.");
|
|
1102
|
+
}
|
|
1103
|
+
}
|
|
1104
|
+
async leggiDeposito(key) {
|
|
1105
|
+
this.verificaChiaveDeposito(key);
|
|
1106
|
+
const value = await this.usaDeposito((deposito) => deposito.get(key));
|
|
1107
|
+
if (value === null) return null;
|
|
1108
|
+
try {
|
|
1109
|
+
return JSON.parse(JSON.stringify(value));
|
|
1110
|
+
} catch {
|
|
1111
|
+
throw erroreConCodice("store_unavailable", "The shared store returned invalid data.");
|
|
1112
|
+
}
|
|
1113
|
+
}
|
|
1114
|
+
async scriviDeposito(key, value) {
|
|
1115
|
+
this.verificaChiaveDeposito(key);
|
|
1116
|
+
const json = analizzaJson(value);
|
|
1117
|
+
if (!json.ok || json.bytes > LIMITE_DEPOSITO) {
|
|
1118
|
+
throw erroreConCodice(
|
|
1119
|
+
"store_too_large",
|
|
1120
|
+
"Shared store values must be valid JSON of at most 65536 bytes."
|
|
1121
|
+
);
|
|
1122
|
+
}
|
|
1123
|
+
await this.usaDeposito((deposito) => deposito.set(key, json.valore));
|
|
1124
|
+
}
|
|
1125
|
+
async eliminaDeposito(key) {
|
|
1126
|
+
this.verificaChiaveDeposito(key);
|
|
1127
|
+
await this.usaDeposito((deposito) => deposito.delete(key));
|
|
1128
|
+
}
|
|
1129
|
+
async elencaDeposito(prefix) {
|
|
1130
|
+
this.verificaPrefissoDeposito(prefix);
|
|
1131
|
+
const keys = await this.usaDeposito((deposito) => deposito.list(prefix));
|
|
1132
|
+
if (!Array.isArray(keys) || keys.some((key) => typeof key !== "string" || !CHIAVE.test(key))) {
|
|
1133
|
+
throw erroreConCodice("store_unavailable", "The shared store returned invalid keys.");
|
|
1134
|
+
}
|
|
1135
|
+
return [...keys].sort().slice(0, 1024);
|
|
1136
|
+
}
|
|
1137
|
+
async incrementaDeposito(key, amount) {
|
|
1138
|
+
this.verificaChiaveDeposito(key);
|
|
1139
|
+
if (!Number.isSafeInteger(amount)) {
|
|
1140
|
+
throw erroreConCodice("store_not_integer", "Shared store increments must be safe integers.");
|
|
1141
|
+
}
|
|
1142
|
+
const value = await this.usaDeposito((deposito) => deposito.increment(key, amount));
|
|
1143
|
+
if (!Number.isSafeInteger(value)) {
|
|
1144
|
+
throw erroreConCodice("store_unavailable", "The shared store returned an invalid integer.");
|
|
1145
|
+
}
|
|
1146
|
+
return value;
|
|
1147
|
+
}
|
|
981
1148
|
broadcastCreatore(message) {
|
|
982
1149
|
const json = analizzaJson(message);
|
|
983
1150
|
if (!json.ok) throw new TypeError("Messages must be valid JSON.");
|
|
@@ -1023,6 +1190,7 @@ var NucleoStanza = class _NucleoStanza {
|
|
|
1023
1190
|
you: player.id,
|
|
1024
1191
|
room: {
|
|
1025
1192
|
id: dati.id,
|
|
1193
|
+
seed: seedStanza(dati.id),
|
|
1026
1194
|
status: dati.status,
|
|
1027
1195
|
mode: dati.mode,
|
|
1028
1196
|
tick: dati.tick,
|
|
@@ -1123,16 +1291,15 @@ var NucleoStanza = class _NucleoStanza {
|
|
|
1123
1291
|
}
|
|
1124
1292
|
return stato;
|
|
1125
1293
|
}
|
|
1126
|
-
|
|
1127
|
-
if ((this.manifest.voice ?? "none")
|
|
1294
|
+
impostaGuadagno(listener, speaker, gain) {
|
|
1295
|
+
if ((this.manifest.voice ?? "none") === "none") return;
|
|
1128
1296
|
const dati = this.richiediDati();
|
|
1129
|
-
if (!dati.giocatori.some((player) => player.id ===
|
|
1297
|
+
if (!dati.giocatori.some((player) => player.id === listener) || !dati.giocatori.some((player) => player.id === speaker)) throw new Error("Player not found.");
|
|
1130
1298
|
if (Number.isNaN(gain)) throw new TypeError("Voice gain must be a number.");
|
|
1131
|
-
if (
|
|
1299
|
+
if (listener === speaker) return;
|
|
1132
1300
|
const valore = Math.round(Math.min(1, Math.max(0, gain)) * 100) / 100;
|
|
1133
1301
|
dati.voceGuadagni ??= {};
|
|
1134
|
-
this.salvaGuadagno(
|
|
1135
|
-
this.salvaGuadagno(b, a, valore);
|
|
1302
|
+
this.salvaGuadagno(listener, speaker, valore);
|
|
1136
1303
|
}
|
|
1137
1304
|
salvaGuadagno(playerId, altroId, valore) {
|
|
1138
1305
|
const dati = this.richiediDati();
|
|
@@ -1256,6 +1423,11 @@ var NucleoStanza = class _NucleoStanza {
|
|
|
1256
1423
|
return dati !== null && dati.status === "playing" && dati.tickRate > 0 && dati.giocatori.some((player) => player.connected) && this.adattatore.ora() - Math.max(dati.ultimoInputAt, dati.ultimoCambioStatoAt) < RIPOSO_TICK_MS;
|
|
1257
1424
|
}
|
|
1258
1425
|
async terminaSeInattiva() {
|
|
1426
|
+
if (this.manifest.persistent === true) {
|
|
1427
|
+
if (this.dati?.status === "ended" || this.dati === null || this.adattatore.ora() < Math.max(this.dati.ultimoInputAt, this.dati.ultimoCambioStatoAt) + SCADENZA_PERSISTENTE_MS) return false;
|
|
1428
|
+
await this.terminaInterna({ error: "expired" });
|
|
1429
|
+
return true;
|
|
1430
|
+
}
|
|
1259
1431
|
if (this.dati?.status !== "playing" || this.adattatore.ora() < this.dati.ultimoInputAt + INATTIVITA_MS) return false;
|
|
1260
1432
|
await this.terminaInterna({ error: "idle" });
|
|
1261
1433
|
return true;
|
|
@@ -1270,7 +1442,13 @@ var NucleoStanza = class _NucleoStanza {
|
|
|
1270
1442
|
this.serveTick() ? 1e3 / this.dati.tickRate : null
|
|
1271
1443
|
);
|
|
1272
1444
|
const prossime = [];
|
|
1273
|
-
if (this.
|
|
1445
|
+
if (this.manifest.persistent === true) {
|
|
1446
|
+
prossime.push(
|
|
1447
|
+
Math.max(this.dati.ultimoInputAt, this.dati.ultimoCambioStatoAt) + SCADENZA_PERSISTENTE_MS
|
|
1448
|
+
);
|
|
1449
|
+
} else if (this.dati.status === "playing") {
|
|
1450
|
+
prossime.push(this.dati.ultimoInputAt + INATTIVITA_MS);
|
|
1451
|
+
}
|
|
1274
1452
|
if (this.dati.countdownAt !== null) prossime.push(this.dati.countdownAt);
|
|
1275
1453
|
for (const player of this.dati.giocatori) {
|
|
1276
1454
|
if (!player.connected && player.graziaFinoA !== null) prossime.push(player.graziaFinoA);
|
|
@@ -1551,8 +1729,9 @@ var ArchivioNode = class _ArchivioNode {
|
|
|
1551
1729
|
}
|
|
1552
1730
|
};
|
|
1553
1731
|
var AdattatoreNode = class {
|
|
1554
|
-
constructor(storage) {
|
|
1732
|
+
constructor(storage, deposito) {
|
|
1555
1733
|
this.storage = storage;
|
|
1734
|
+
this.deposito = deposito;
|
|
1556
1735
|
this.connessioni = /* @__PURE__ */ new Map();
|
|
1557
1736
|
this.tickTimer = null;
|
|
1558
1737
|
this.tickIntervallo = null;
|
|
@@ -1648,6 +1827,7 @@ var StanzaNode = class {
|
|
|
1648
1827
|
this.manifest = manifest;
|
|
1649
1828
|
this.coda = Promise.resolve();
|
|
1650
1829
|
this.voceRoster = /* @__PURE__ */ new Map();
|
|
1830
|
+
this.voceListeners = /* @__PURE__ */ new Map();
|
|
1651
1831
|
this.voceFrequenza = /* @__PURE__ */ new Map();
|
|
1652
1832
|
this.voceUltimaRichiesta = /* @__PURE__ */ new Map();
|
|
1653
1833
|
this.frameFrequenza = /* @__PURE__ */ new Map();
|
|
@@ -1798,17 +1978,24 @@ var StanzaNode = class {
|
|
|
1798
1978
|
return;
|
|
1799
1979
|
}
|
|
1800
1980
|
if (richiesta.op === "publish") {
|
|
1801
|
-
|
|
1981
|
+
const mic = richiesta.mic !== false;
|
|
1982
|
+
if (role === "spectator" && mic) {
|
|
1802
1983
|
this.inviaErroreVoce(connessione, richiesta, "spectator", "Spectators cannot join voice.");
|
|
1803
1984
|
return;
|
|
1804
1985
|
}
|
|
1805
|
-
|
|
1806
|
-
|
|
1807
|
-
|
|
1808
|
-
|
|
1809
|
-
|
|
1810
|
-
|
|
1811
|
-
|
|
1986
|
+
if (mic) {
|
|
1987
|
+
this.voceListeners.delete(playerId);
|
|
1988
|
+
this.voceRoster.set(playerId, {
|
|
1989
|
+
id: playerId,
|
|
1990
|
+
session: "mesh",
|
|
1991
|
+
track: "mic",
|
|
1992
|
+
muted: false,
|
|
1993
|
+
connessione
|
|
1994
|
+
});
|
|
1995
|
+
} else {
|
|
1996
|
+
this.voceRoster.delete(playerId);
|
|
1997
|
+
this.voceListeners.set(playerId, connessione);
|
|
1998
|
+
}
|
|
1812
1999
|
this.adattatore.invia(connessione, { t: "voice", op: "publish", r: richiesta.r });
|
|
1813
2000
|
this.broadcastRoster();
|
|
1814
2001
|
return;
|
|
@@ -1844,9 +2031,11 @@ var StanzaNode = class {
|
|
|
1844
2031
|
}
|
|
1845
2032
|
if (richiesta.op === "stop") {
|
|
1846
2033
|
const peer = this.voceRoster.get(playerId);
|
|
1847
|
-
const
|
|
2034
|
+
const listener = this.voceListeners.get(playerId);
|
|
2035
|
+
const rimossaPeer = peer?.connessione === connessione && this.voceRoster.delete(playerId);
|
|
2036
|
+
const rimossoListener = listener === connessione && this.voceListeners.delete(playerId);
|
|
1848
2037
|
this.adattatore.invia(connessione, { t: "voice", op: "stop", r: richiesta.r });
|
|
1849
|
-
if (
|
|
2038
|
+
if (rimossaPeer || rimossoListener) this.broadcastRoster();
|
|
1850
2039
|
return;
|
|
1851
2040
|
}
|
|
1852
2041
|
this.inviaErroreVoce(connessione, richiesta, "invalid_request", "The voice request is invalid.");
|
|
@@ -1856,9 +2045,12 @@ var StanzaNode = class {
|
|
|
1856
2045
|
return null;
|
|
1857
2046
|
}
|
|
1858
2047
|
const base = { t: "voice", r: value.r };
|
|
1859
|
-
if (value.op === "ice" || value.op === "
|
|
2048
|
+
if (value.op === "ice" || value.op === "stop") {
|
|
1860
2049
|
return { ...base, op: value.op };
|
|
1861
2050
|
}
|
|
2051
|
+
if (value.op === "publish" && (value.mic === void 0 || typeof value.mic === "boolean")) {
|
|
2052
|
+
return { ...base, op: "publish", ...value.mic === void 0 ? {} : { mic: value.mic } };
|
|
2053
|
+
}
|
|
1862
2054
|
if (value.op === "mute" && typeof value.muted === "boolean") {
|
|
1863
2055
|
return { ...base, op: "mute", muted: value.muted };
|
|
1864
2056
|
}
|
|
@@ -1881,7 +2073,8 @@ var StanzaNode = class {
|
|
|
1881
2073
|
t: "voice",
|
|
1882
2074
|
op: "roster",
|
|
1883
2075
|
mode: this.modoVoce(),
|
|
1884
|
-
peers: this.rosterPubblico()
|
|
2076
|
+
peers: this.rosterPubblico(),
|
|
2077
|
+
listeners: [...this.voceListeners.keys()].sort((a, b) => a.localeCompare(b))
|
|
1885
2078
|
});
|
|
1886
2079
|
}
|
|
1887
2080
|
broadcastRoster() {
|
|
@@ -1895,6 +2088,11 @@ var StanzaNode = class {
|
|
|
1895
2088
|
this.voceRoster.delete(playerId);
|
|
1896
2089
|
cambiato = true;
|
|
1897
2090
|
}
|
|
2091
|
+
for (const [playerId, connessione] of this.voceListeners) {
|
|
2092
|
+
if (this.nucleo.giocatoreConnesso(connessione)?.id === playerId) continue;
|
|
2093
|
+
this.voceListeners.delete(playerId);
|
|
2094
|
+
cambiato = true;
|
|
2095
|
+
}
|
|
1898
2096
|
if (cambiato) this.broadcastRoster();
|
|
1899
2097
|
}
|
|
1900
2098
|
rimuoviConnessione(connessione) {
|
|
@@ -1910,6 +2108,9 @@ var StanzaNode = class {
|
|
|
1910
2108
|
if (this.voceRoster.get(playerId)?.connessione === connessione) {
|
|
1911
2109
|
this.voceRoster.delete(playerId);
|
|
1912
2110
|
this.broadcastRoster();
|
|
2111
|
+
} else if (this.voceListeners.get(playerId) === connessione) {
|
|
2112
|
+
this.voceListeners.delete(playerId);
|
|
2113
|
+
this.broadcastRoster();
|
|
1913
2114
|
}
|
|
1914
2115
|
}
|
|
1915
2116
|
inviaErroreVoce(connessione, richiesta, code, message) {
|
|
@@ -1920,7 +2121,7 @@ var StanzaNode = class {
|
|
|
1920
2121
|
};
|
|
1921
2122
|
async function createNodeRoom(definition, manifest, options = {}) {
|
|
1922
2123
|
const storage = await ArchivioNode.apri(options.storageFile ?? null);
|
|
1923
|
-
const adattatore = new AdattatoreNode(storage);
|
|
2124
|
+
const adattatore = new AdattatoreNode(storage, options.deposito ?? null);
|
|
1924
2125
|
const nucleo = await NucleoStanza.apri(definition, manifest, adattatore);
|
|
1925
2126
|
return new StanzaNode(nucleo, adattatore, manifest);
|
|
1926
2127
|
}
|
package/dist/runtime.d.ts
CHANGED
|
@@ -6,6 +6,7 @@ export interface RoomManifest {
|
|
|
6
6
|
max: number;
|
|
7
7
|
};
|
|
8
8
|
lobby: boolean;
|
|
9
|
+
persistent?: boolean;
|
|
9
10
|
roles: Array<{
|
|
10
11
|
id: string;
|
|
11
12
|
min: number;
|
|
@@ -38,6 +39,7 @@ export interface Esito {
|
|
|
38
39
|
sveglia: number | null;
|
|
39
40
|
tick: boolean;
|
|
40
41
|
tickRate: number;
|
|
42
|
+
status: RoomStatus | null;
|
|
41
43
|
}
|
|
42
44
|
export interface InfoStanza {
|
|
43
45
|
roomId: string;
|
|
@@ -45,6 +47,7 @@ export interface InfoStanza {
|
|
|
45
47
|
players: number;
|
|
46
48
|
max: number;
|
|
47
49
|
mode: string | null;
|
|
50
|
+
member: boolean;
|
|
48
51
|
}
|
|
49
52
|
export type EsitoIngresso = {
|
|
50
53
|
ok: true;
|
|
@@ -70,9 +73,8 @@ export interface FacetStanza {
|
|
|
70
73
|
creator: IdentitaGiocatore;
|
|
71
74
|
}): Promise<Esito & {
|
|
72
75
|
ok: boolean;
|
|
73
|
-
status: 'lobby' | 'playing';
|
|
74
76
|
}>;
|
|
75
|
-
info(): Promise<InfoStanza | null>;
|
|
77
|
+
info(playerId?: string): Promise<InfoStanza | null>;
|
|
76
78
|
puoEntrare(identita: IdentitaGiocatore): Promise<EsitoIngresso>;
|
|
77
79
|
entra(identita: IdentitaGiocatore, connessione: string): Promise<Esito>;
|
|
78
80
|
ricevi(connessione: string, testo: string): Promise<Esito>;
|