@caisual/kit 0.3.0 → 0.5.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 +38 -1
- package/dist/kit.js +602 -79
- package/dist/node.d.ts +14 -1
- package/dist/node.js +458 -55
- package/dist/runtime.d.ts +7 -2
- package/dist/runtime.js +253 -51
- package/dist/server.d.ts +14 -0
- package/package.json +2 -1
package/dist/node.js
CHANGED
|
@@ -4,6 +4,45 @@ import { mkdir, readFile, rename, rm, writeFile } from "node:fs/promises";
|
|
|
4
4
|
import { dirname } from "node:path";
|
|
5
5
|
import { performance } from "node:perf_hooks";
|
|
6
6
|
|
|
7
|
+
// ../contracts/src/slug.ts
|
|
8
|
+
var NOMI_RISERVATI = [
|
|
9
|
+
"www",
|
|
10
|
+
"api",
|
|
11
|
+
"app",
|
|
12
|
+
"play",
|
|
13
|
+
"live",
|
|
14
|
+
"multi",
|
|
15
|
+
"cdn",
|
|
16
|
+
"assets",
|
|
17
|
+
"static",
|
|
18
|
+
"mail",
|
|
19
|
+
"mx",
|
|
20
|
+
"ns1",
|
|
21
|
+
"ns2",
|
|
22
|
+
"autodiscover",
|
|
23
|
+
"_dmarc",
|
|
24
|
+
"admin",
|
|
25
|
+
"login",
|
|
26
|
+
"account",
|
|
27
|
+
"auth",
|
|
28
|
+
"pay",
|
|
29
|
+
"secure",
|
|
30
|
+
"support",
|
|
31
|
+
"help",
|
|
32
|
+
"blog",
|
|
33
|
+
"status",
|
|
34
|
+
"dev",
|
|
35
|
+
"staging",
|
|
36
|
+
"test",
|
|
37
|
+
"caisual",
|
|
38
|
+
"shipz"
|
|
39
|
+
];
|
|
40
|
+
var RISERVATI = new Set(NOMI_RISERVATI);
|
|
41
|
+
|
|
42
|
+
// ../contracts/src/manifest.ts
|
|
43
|
+
var MASSIMO_SPETTATORI = 100;
|
|
44
|
+
var RITARDO_SPETTATORI_MS = 3e3;
|
|
45
|
+
|
|
7
46
|
// src/stanza/json.ts
|
|
8
47
|
function isPlainObject(value) {
|
|
9
48
|
const prototype = Object.getPrototypeOf(value);
|
|
@@ -158,9 +197,8 @@ var COSTANTI_SHA256 = [
|
|
|
158
197
|
function ruotaDestra(value, bits) {
|
|
159
198
|
return value >>> bits | value << 32 - bits;
|
|
160
199
|
}
|
|
161
|
-
function
|
|
162
|
-
const
|
|
163
|
-
const bytes = Array.from(testo, (carattere) => carattere.charCodeAt(0));
|
|
200
|
+
function hashSeed(testo) {
|
|
201
|
+
const bytes = Array.from(new TextEncoder().encode(testo));
|
|
164
202
|
const bitLength = bytes.length * 8;
|
|
165
203
|
bytes.push(128);
|
|
166
204
|
while (bytes.length % 64 !== 56) bytes.push(0);
|
|
@@ -223,6 +261,12 @@ function seedGiornata(slug, day) {
|
|
|
223
261
|
}
|
|
224
262
|
return h0 >>> 0;
|
|
225
263
|
}
|
|
264
|
+
function seedGiornata(slug, day) {
|
|
265
|
+
return hashSeed(`caisual:${slug}:${day}`);
|
|
266
|
+
}
|
|
267
|
+
function seedStanza(roomId) {
|
|
268
|
+
return hashSeed(`caisual:room:${roomId}`);
|
|
269
|
+
}
|
|
226
270
|
|
|
227
271
|
// src/stanza/nucleo.ts
|
|
228
272
|
var CHIAVE_NUCLEO = "nucleo";
|
|
@@ -230,13 +274,25 @@ var PREFISSO_SAVE = "save:";
|
|
|
230
274
|
var LIMITE_FRAME = 16 * 1024;
|
|
231
275
|
var LIMITE_STATO = 256 * 1024;
|
|
232
276
|
var LIMITE_SAVE = 128 * 1024;
|
|
277
|
+
var LIMITE_DEPOSITO = 64 * 1024;
|
|
278
|
+
var LIMITE_OPERAZIONI_DEPOSITO = 120;
|
|
233
279
|
var GRAZIA_MS = 6e4;
|
|
234
280
|
var STANZA_VUOTA_MS = 5 * 6e4;
|
|
235
281
|
var COUNTDOWN_MS = 3e3;
|
|
236
282
|
var RIPOSO_TICK_MS = 3e4;
|
|
237
283
|
var INATTIVITA_MS = 10 * 6e4;
|
|
284
|
+
var SCADENZA_PERSISTENTE_MS = 30 * 24 * 60 * 6e4;
|
|
238
285
|
var CHIAVE = /^[a-z0-9][a-z0-9_-]{0,31}$/;
|
|
286
|
+
var PREFISSO_CHIAVE = /^[a-z0-9_-]{0,32}$/;
|
|
239
287
|
var GAME_DEFINITION = /* @__PURE__ */ Symbol.for("@caisual/kit/game-definition");
|
|
288
|
+
var CODICI_DEPOSITO = /* @__PURE__ */ new Set([
|
|
289
|
+
"store_invalid_key",
|
|
290
|
+
"store_too_large",
|
|
291
|
+
"store_full",
|
|
292
|
+
"store_not_integer",
|
|
293
|
+
"store_unavailable",
|
|
294
|
+
"store_rate_limited"
|
|
295
|
+
]);
|
|
240
296
|
function record(value) {
|
|
241
297
|
return typeof value === "object" && value !== null && !Array.isArray(value) ? value : null;
|
|
242
298
|
}
|
|
@@ -259,6 +315,9 @@ function copiaGiocatore(player) {
|
|
|
259
315
|
function copiaJson(value) {
|
|
260
316
|
return JSON.parse(JSON.stringify(value));
|
|
261
317
|
}
|
|
318
|
+
function erroreConCodice(code, message) {
|
|
319
|
+
return Object.assign(new Error(message), { code });
|
|
320
|
+
}
|
|
262
321
|
function codiceIngresso(code) {
|
|
263
322
|
if (code === "room_not_found") return 4001;
|
|
264
323
|
if (code === "room_full") return 4e3;
|
|
@@ -271,6 +330,7 @@ var NucleoStanza = class _NucleoStanza {
|
|
|
271
330
|
this.adattatore = adattatore;
|
|
272
331
|
this.dati = null;
|
|
273
332
|
this.frequenza = /* @__PURE__ */ new Map();
|
|
333
|
+
this.frequenzaDeposito = [];
|
|
274
334
|
this.kickRichiesti = /* @__PURE__ */ new Set();
|
|
275
335
|
this.voceGuadagniCambiati = /* @__PURE__ */ new Map();
|
|
276
336
|
this.fineRichiesta = null;
|
|
@@ -290,6 +350,9 @@ var NucleoStanza = class _NucleoStanza {
|
|
|
290
350
|
get id() {
|
|
291
351
|
return nucleo.richiediDati().id;
|
|
292
352
|
},
|
|
353
|
+
get seed() {
|
|
354
|
+
return seedStanza(nucleo.richiediDati().id);
|
|
355
|
+
},
|
|
293
356
|
get mode() {
|
|
294
357
|
return nucleo.richiediDati().mode;
|
|
295
358
|
},
|
|
@@ -299,6 +362,12 @@ var NucleoStanza = class _NucleoStanza {
|
|
|
299
362
|
get tick() {
|
|
300
363
|
return nucleo.richiediDati().tick;
|
|
301
364
|
},
|
|
365
|
+
get tickRate() {
|
|
366
|
+
return nucleo.richiediDati().tickRate;
|
|
367
|
+
},
|
|
368
|
+
get result() {
|
|
369
|
+
return nucleo.richiediDati().result;
|
|
370
|
+
},
|
|
302
371
|
get state() {
|
|
303
372
|
return nucleo.richiediDati().state;
|
|
304
373
|
},
|
|
@@ -322,6 +391,12 @@ var NucleoStanza = class _NucleoStanza {
|
|
|
322
391
|
kick(player) {
|
|
323
392
|
nucleo.kickRichiesti.add(idGiocatore(player));
|
|
324
393
|
},
|
|
394
|
+
setRole(player, role) {
|
|
395
|
+
nucleo.impostaRuoloDalServer(idGiocatore(player), role);
|
|
396
|
+
},
|
|
397
|
+
setTeam(player, team) {
|
|
398
|
+
nucleo.impostaSquadraDalServer(idGiocatore(player), team);
|
|
399
|
+
},
|
|
325
400
|
end(result) {
|
|
326
401
|
nucleo.richiediFine(result);
|
|
327
402
|
},
|
|
@@ -331,6 +406,23 @@ var NucleoStanza = class _NucleoStanza {
|
|
|
331
406
|
load(key) {
|
|
332
407
|
return nucleo.caricaSave(key);
|
|
333
408
|
},
|
|
409
|
+
shared: {
|
|
410
|
+
get(key) {
|
|
411
|
+
return nucleo.leggiDeposito(key);
|
|
412
|
+
},
|
|
413
|
+
set(key, value) {
|
|
414
|
+
return nucleo.scriviDeposito(key, value);
|
|
415
|
+
},
|
|
416
|
+
delete(key) {
|
|
417
|
+
return nucleo.eliminaDeposito(key);
|
|
418
|
+
},
|
|
419
|
+
list(prefix) {
|
|
420
|
+
return nucleo.elencaDeposito(prefix);
|
|
421
|
+
},
|
|
422
|
+
increment(key, amount = 1) {
|
|
423
|
+
return nucleo.incrementaDeposito(key, amount);
|
|
424
|
+
}
|
|
425
|
+
},
|
|
334
426
|
schedule(milliseconds, handler, payload) {
|
|
335
427
|
nucleo.pianifica(milliseconds, handler, payload);
|
|
336
428
|
},
|
|
@@ -345,8 +437,14 @@ var NucleoStanza = class _NucleoStanza {
|
|
|
345
437
|
get mode() {
|
|
346
438
|
return nucleo.manifest.voice ?? "none";
|
|
347
439
|
},
|
|
440
|
+
setGain(listener, speaker, gain) {
|
|
441
|
+
nucleo.impostaGuadagno(idGiocatore(listener), idGiocatore(speaker), gain);
|
|
442
|
+
},
|
|
348
443
|
setProximity(a, b, gain) {
|
|
349
|
-
|
|
444
|
+
const primo = idGiocatore(a);
|
|
445
|
+
const secondo = idGiocatore(b);
|
|
446
|
+
nucleo.impostaGuadagno(primo, secondo, gain);
|
|
447
|
+
nucleo.impostaGuadagno(secondo, primo, gain);
|
|
350
448
|
}
|
|
351
449
|
}
|
|
352
450
|
};
|
|
@@ -371,7 +469,7 @@ var NucleoStanza = class _NucleoStanza {
|
|
|
371
469
|
return nucleo;
|
|
372
470
|
}
|
|
373
471
|
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)) {
|
|
472
|
+
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
473
|
throw new TypeError("The room manifest is invalid.");
|
|
376
474
|
}
|
|
377
475
|
}
|
|
@@ -434,16 +532,25 @@ var NucleoStanza = class _NucleoStanza {
|
|
|
434
532
|
await this.persistiEProgramma();
|
|
435
533
|
return true;
|
|
436
534
|
}
|
|
437
|
-
info() {
|
|
535
|
+
info(playerId) {
|
|
438
536
|
if (this.dati === null) return null;
|
|
439
537
|
return {
|
|
440
538
|
roomId: this.dati.id,
|
|
441
539
|
status: this.dati.status,
|
|
442
|
-
players: this.dati.giocatori.filter(
|
|
540
|
+
players: this.manifest.persistent === true ? this.dati.giocatori.length : this.dati.giocatori.filter(
|
|
443
541
|
(player) => player.connected || player.graziaFinoA !== null
|
|
444
542
|
).length,
|
|
445
543
|
max: this.manifest.players.max,
|
|
446
|
-
mode: this.dati.mode
|
|
544
|
+
mode: this.dati.mode,
|
|
545
|
+
member: playerId === void 0 ? false : this.dati.giocatori.some((player) => player.id === playerId)
|
|
546
|
+
};
|
|
547
|
+
}
|
|
548
|
+
fotografia() {
|
|
549
|
+
if (this.dati === null) return null;
|
|
550
|
+
return {
|
|
551
|
+
room: this.stanzaProtocollo(),
|
|
552
|
+
players: this.giocatoriProtocollo(),
|
|
553
|
+
state: this.dati.state
|
|
447
554
|
};
|
|
448
555
|
}
|
|
449
556
|
giocatoreConnesso(connessione) {
|
|
@@ -489,7 +596,6 @@ var NucleoStanza = class _NucleoStanza {
|
|
|
489
596
|
seq: 0
|
|
490
597
|
};
|
|
491
598
|
dati.giocatori.push(player);
|
|
492
|
-
dati.hostId ??= player.id;
|
|
493
599
|
} else {
|
|
494
600
|
if (player.connected && player.connessione !== null && player.connessione !== connessione) {
|
|
495
601
|
this.adattatore.chiudi(player.connessione, 4006, "replaced");
|
|
@@ -501,6 +607,7 @@ var NucleoStanza = class _NucleoStanza {
|
|
|
501
607
|
player.connessione = connessione;
|
|
502
608
|
player.seq = 0;
|
|
503
609
|
}
|
|
610
|
+
dati.hostId ??= player.id;
|
|
504
611
|
dati.vuotaDa = null;
|
|
505
612
|
dati.ultimoInputAt = ora;
|
|
506
613
|
const primaConnessione = !this.manifest.lobby && dati.status === "lobby";
|
|
@@ -534,6 +641,7 @@ var NucleoStanza = class _NucleoStanza {
|
|
|
534
641
|
player.connected = false;
|
|
535
642
|
player.connessione = null;
|
|
536
643
|
player.graziaFinoA = this.adattatore.ora() + GRAZIA_MS;
|
|
644
|
+
if (this.manifest.persistent === true && this.dati.status === "lobby") player.ready = false;
|
|
537
645
|
this.frequenza.delete(connessione);
|
|
538
646
|
if (this.dati.hostId === player.id) this.assegnaHost();
|
|
539
647
|
this.verificaCountdown();
|
|
@@ -603,7 +711,8 @@ var NucleoStanza = class _NucleoStanza {
|
|
|
603
711
|
if (typeof message.role !== "string") return this.messaggioErrato(player);
|
|
604
712
|
if (!this.inLobby(player)) return;
|
|
605
713
|
this.dati.ultimoInputAt = ora;
|
|
606
|
-
this.
|
|
714
|
+
const errore = this.cambiaRuolo(player, message.role);
|
|
715
|
+
if (errore !== null) this.inviaErrore(player, errore.code, errore.message);
|
|
607
716
|
await this.persistiEProgramma();
|
|
608
717
|
return;
|
|
609
718
|
}
|
|
@@ -611,7 +720,8 @@ var NucleoStanza = class _NucleoStanza {
|
|
|
611
720
|
if (!Number.isInteger(message.team)) return this.messaggioErrato(player);
|
|
612
721
|
if (!this.inLobby(player)) return;
|
|
613
722
|
this.dati.ultimoInputAt = ora;
|
|
614
|
-
this.
|
|
723
|
+
const errore = this.cambiaSquadra(player, message.team);
|
|
724
|
+
if (errore !== null) this.inviaErrore(player, errore.code, errore.message);
|
|
615
725
|
await this.persistiEProgramma();
|
|
616
726
|
return;
|
|
617
727
|
}
|
|
@@ -701,7 +811,10 @@ var NucleoStanza = class _NucleoStanza {
|
|
|
701
811
|
const scaduti = this.dati.giocatori.filter(
|
|
702
812
|
(player) => !player.connected && player.graziaFinoA !== null && player.graziaFinoA <= ora
|
|
703
813
|
);
|
|
704
|
-
for (const player of scaduti)
|
|
814
|
+
for (const player of scaduti) {
|
|
815
|
+
if (this.manifest.persistent === true) player.graziaFinoA = null;
|
|
816
|
+
else await this.rimuoviGiocatore(player, "timeout");
|
|
817
|
+
}
|
|
705
818
|
if (this.dati.status === "countdown" && this.dati.countdownAt !== null && this.dati.countdownAt <= ora) {
|
|
706
819
|
const errore = this.erroreMinimi();
|
|
707
820
|
if (errore !== null) {
|
|
@@ -761,6 +874,7 @@ var NucleoStanza = class _NucleoStanza {
|
|
|
761
874
|
player.connected = false;
|
|
762
875
|
player.connessione = null;
|
|
763
876
|
player.graziaFinoA = ora + GRAZIA_MS;
|
|
877
|
+
if (this.manifest.persistent === true && dati.status === "lobby") player.ready = false;
|
|
764
878
|
cambiato = true;
|
|
765
879
|
}
|
|
766
880
|
}
|
|
@@ -804,37 +918,52 @@ var NucleoStanza = class _NucleoStanza {
|
|
|
804
918
|
this.inviaErrore(player, "not_in_lobby", "This action is only available in the lobby.");
|
|
805
919
|
return false;
|
|
806
920
|
}
|
|
807
|
-
|
|
921
|
+
cambiaRuolo(player, roleId) {
|
|
808
922
|
const ruolo = this.manifest.roles.find((item) => item.id === roleId);
|
|
809
923
|
if (ruolo === void 0 && roleId !== "spectator") {
|
|
810
|
-
|
|
811
|
-
return;
|
|
924
|
+
return { code: "invalid_role", message: "This role does not exist." };
|
|
812
925
|
}
|
|
813
926
|
const occupati = this.richiediDati().giocatori.filter(
|
|
814
927
|
(item) => item.id !== player.id && item.role === roleId
|
|
815
928
|
).length;
|
|
816
929
|
if (ruolo?.max !== void 0 && occupati >= ruolo.max) {
|
|
817
|
-
|
|
818
|
-
return;
|
|
930
|
+
return { code: "role_full", message: "This role is full." };
|
|
819
931
|
}
|
|
820
932
|
player.role = roleId;
|
|
821
933
|
if (roleId === "spectator") player.team = null;
|
|
822
934
|
else if (player.team === null) player.team = this.squadraAutomatica();
|
|
823
|
-
player.ready = false;
|
|
935
|
+
if (this.richiediDati().status === "lobby") player.ready = false;
|
|
824
936
|
this.inviaGiocatori();
|
|
937
|
+
return null;
|
|
825
938
|
}
|
|
826
|
-
|
|
827
|
-
if (this.manifest.teams === null || team < 1 || team > this.manifest.teams.max) {
|
|
828
|
-
|
|
829
|
-
return;
|
|
939
|
+
cambiaSquadra(player, team) {
|
|
940
|
+
if (this.manifest.teams === null || !Number.isInteger(team) || team < 1 || team > this.manifest.teams.max) {
|
|
941
|
+
return { code: "invalid_team", message: "This team does not exist." };
|
|
830
942
|
}
|
|
831
943
|
if (player.role === "spectator") {
|
|
832
|
-
|
|
833
|
-
return;
|
|
944
|
+
return { code: "spectator", message: "Spectators cannot join a team." };
|
|
834
945
|
}
|
|
835
946
|
player.team = team;
|
|
836
|
-
player.ready = false;
|
|
947
|
+
if (this.richiediDati().status === "lobby") player.ready = false;
|
|
837
948
|
this.inviaGiocatori();
|
|
949
|
+
return null;
|
|
950
|
+
}
|
|
951
|
+
impostaRuoloDalServer(playerId, roleId) {
|
|
952
|
+
const player = this.giocatorePerModificaDalServer(playerId);
|
|
953
|
+
const errore = this.cambiaRuolo(player, roleId);
|
|
954
|
+
if (errore !== null) throw erroreConCodice(errore.code, errore.message);
|
|
955
|
+
}
|
|
956
|
+
impostaSquadraDalServer(playerId, team) {
|
|
957
|
+
const player = this.giocatorePerModificaDalServer(playerId);
|
|
958
|
+
const errore = this.cambiaSquadra(player, team);
|
|
959
|
+
if (errore !== null) throw erroreConCodice(errore.code, errore.message);
|
|
960
|
+
}
|
|
961
|
+
giocatorePerModificaDalServer(playerId) {
|
|
962
|
+
const dati = this.richiediDati();
|
|
963
|
+
if (dati.status === "ended") throw new Error("The room has ended.");
|
|
964
|
+
const player = dati.giocatori.find((item) => item.id === playerId);
|
|
965
|
+
if (player === void 0) throw erroreConCodice("player_not_found", "Player not found.");
|
|
966
|
+
return player;
|
|
838
967
|
}
|
|
839
968
|
erroreMinimi() {
|
|
840
969
|
const connessi = this.richiediDati().giocatori.filter((player) => player.connected);
|
|
@@ -978,6 +1107,91 @@ var NucleoStanza = class _NucleoStanza {
|
|
|
978
1107
|
if (!json.ok) throw new Error("The saved value is invalid.");
|
|
979
1108
|
return json.valore;
|
|
980
1109
|
}
|
|
1110
|
+
verificaChiaveDeposito(key) {
|
|
1111
|
+
if (typeof key !== "string" || !CHIAVE.test(key)) {
|
|
1112
|
+
throw erroreConCodice(
|
|
1113
|
+
"store_invalid_key",
|
|
1114
|
+
"Shared store keys must use lowercase letters, numbers, underscores, or hyphens."
|
|
1115
|
+
);
|
|
1116
|
+
}
|
|
1117
|
+
}
|
|
1118
|
+
verificaPrefissoDeposito(prefix) {
|
|
1119
|
+
if (prefix !== void 0 && (typeof prefix !== "string" || !PREFISSO_CHIAVE.test(prefix))) {
|
|
1120
|
+
throw erroreConCodice(
|
|
1121
|
+
"store_invalid_key",
|
|
1122
|
+
"Shared store prefixes may contain lowercase letters, numbers, underscores, or hyphens."
|
|
1123
|
+
);
|
|
1124
|
+
}
|
|
1125
|
+
}
|
|
1126
|
+
contaOperazioneDeposito() {
|
|
1127
|
+
const ora = this.adattatore.ora();
|
|
1128
|
+
this.frequenzaDeposito = this.frequenzaDeposito.filter((at) => ora - at < 6e4);
|
|
1129
|
+
if (this.frequenzaDeposito.length >= LIMITE_OPERAZIONI_DEPOSITO) {
|
|
1130
|
+
throw erroreConCodice(
|
|
1131
|
+
"store_rate_limited",
|
|
1132
|
+
"The shared store allows at most 120 operations per minute for each room."
|
|
1133
|
+
);
|
|
1134
|
+
}
|
|
1135
|
+
this.frequenzaDeposito.push(ora);
|
|
1136
|
+
}
|
|
1137
|
+
async usaDeposito(operazione) {
|
|
1138
|
+
this.contaOperazioneDeposito();
|
|
1139
|
+
const deposito = this.adattatore.deposito;
|
|
1140
|
+
if (deposito === null) {
|
|
1141
|
+
throw erroreConCodice("store_unavailable", "The shared store is unavailable.");
|
|
1142
|
+
}
|
|
1143
|
+
try {
|
|
1144
|
+
return await operazione(deposito);
|
|
1145
|
+
} catch (cause) {
|
|
1146
|
+
const code = cause instanceof Error ? cause.code : void 0;
|
|
1147
|
+
if (typeof code === "string" && CODICI_DEPOSITO.has(code)) throw cause;
|
|
1148
|
+
throw erroreConCodice("store_unavailable", "The shared store is unavailable.");
|
|
1149
|
+
}
|
|
1150
|
+
}
|
|
1151
|
+
async leggiDeposito(key) {
|
|
1152
|
+
this.verificaChiaveDeposito(key);
|
|
1153
|
+
const value = await this.usaDeposito((deposito) => deposito.get(key));
|
|
1154
|
+
if (value === null) return null;
|
|
1155
|
+
try {
|
|
1156
|
+
return JSON.parse(JSON.stringify(value));
|
|
1157
|
+
} catch {
|
|
1158
|
+
throw erroreConCodice("store_unavailable", "The shared store returned invalid data.");
|
|
1159
|
+
}
|
|
1160
|
+
}
|
|
1161
|
+
async scriviDeposito(key, value) {
|
|
1162
|
+
this.verificaChiaveDeposito(key);
|
|
1163
|
+
const json = analizzaJson(value);
|
|
1164
|
+
if (!json.ok || json.bytes > LIMITE_DEPOSITO) {
|
|
1165
|
+
throw erroreConCodice(
|
|
1166
|
+
"store_too_large",
|
|
1167
|
+
"Shared store values must be valid JSON of at most 65536 bytes."
|
|
1168
|
+
);
|
|
1169
|
+
}
|
|
1170
|
+
await this.usaDeposito((deposito) => deposito.set(key, json.valore));
|
|
1171
|
+
}
|
|
1172
|
+
async eliminaDeposito(key) {
|
|
1173
|
+
this.verificaChiaveDeposito(key);
|
|
1174
|
+
await this.usaDeposito((deposito) => deposito.delete(key));
|
|
1175
|
+
}
|
|
1176
|
+
async elencaDeposito(prefix) {
|
|
1177
|
+
this.verificaPrefissoDeposito(prefix);
|
|
1178
|
+
const keys = await this.usaDeposito((deposito) => deposito.list(prefix));
|
|
1179
|
+
if (!Array.isArray(keys) || keys.some((key) => typeof key !== "string" || !CHIAVE.test(key))) {
|
|
1180
|
+
throw erroreConCodice("store_unavailable", "The shared store returned invalid keys.");
|
|
1181
|
+
}
|
|
1182
|
+
return [...keys].sort().slice(0, 1024);
|
|
1183
|
+
}
|
|
1184
|
+
async incrementaDeposito(key, amount) {
|
|
1185
|
+
this.verificaChiaveDeposito(key);
|
|
1186
|
+
if (!Number.isSafeInteger(amount)) {
|
|
1187
|
+
throw erroreConCodice("store_not_integer", "Shared store increments must be safe integers.");
|
|
1188
|
+
}
|
|
1189
|
+
const value = await this.usaDeposito((deposito) => deposito.increment(key, amount));
|
|
1190
|
+
if (!Number.isSafeInteger(value)) {
|
|
1191
|
+
throw erroreConCodice("store_unavailable", "The shared store returned an invalid integer.");
|
|
1192
|
+
}
|
|
1193
|
+
return value;
|
|
1194
|
+
}
|
|
981
1195
|
broadcastCreatore(message) {
|
|
982
1196
|
const json = analizzaJson(message);
|
|
983
1197
|
if (!json.ok) throw new TypeError("Messages must be valid JSON.");
|
|
@@ -998,6 +1212,7 @@ var NucleoStanza = class _NucleoStanza {
|
|
|
998
1212
|
this.adattatore.invia(player.connessione, message);
|
|
999
1213
|
}
|
|
1000
1214
|
}
|
|
1215
|
+
if (message.t !== "flush") this.adattatore.pubblica(message);
|
|
1001
1216
|
}
|
|
1002
1217
|
inviaErrore(player, code, message) {
|
|
1003
1218
|
if (player.connected && player.connessione !== null) {
|
|
@@ -1021,15 +1236,7 @@ var NucleoStanza = class _NucleoStanza {
|
|
|
1021
1236
|
this.adattatore.invia(player.connessione, {
|
|
1022
1237
|
t: "welcome",
|
|
1023
1238
|
you: player.id,
|
|
1024
|
-
room:
|
|
1025
|
-
id: dati.id,
|
|
1026
|
-
status: dati.status,
|
|
1027
|
-
mode: dati.mode,
|
|
1028
|
-
tick: dati.tick,
|
|
1029
|
-
tickRate: dati.tickRate,
|
|
1030
|
-
serverTime: this.adattatore.ora(),
|
|
1031
|
-
host: dati.hostId
|
|
1032
|
-
},
|
|
1239
|
+
room: this.stanzaProtocollo(),
|
|
1033
1240
|
players: this.giocatoriProtocollo(),
|
|
1034
1241
|
state: dati.state
|
|
1035
1242
|
});
|
|
@@ -1038,6 +1245,19 @@ var NucleoStanza = class _NucleoStanza {
|
|
|
1038
1245
|
this.adattatore.invia(player.connessione, { t: "voice", op: "gain", gains: { ...gains } });
|
|
1039
1246
|
}
|
|
1040
1247
|
}
|
|
1248
|
+
stanzaProtocollo() {
|
|
1249
|
+
const dati = this.richiediDati();
|
|
1250
|
+
return {
|
|
1251
|
+
id: dati.id,
|
|
1252
|
+
seed: seedStanza(dati.id),
|
|
1253
|
+
status: dati.status,
|
|
1254
|
+
mode: dati.mode,
|
|
1255
|
+
tick: dati.tick,
|
|
1256
|
+
tickRate: dati.tickRate,
|
|
1257
|
+
serverTime: this.adattatore.ora(),
|
|
1258
|
+
host: dati.hostId
|
|
1259
|
+
};
|
|
1260
|
+
}
|
|
1041
1261
|
inviaGiocatori() {
|
|
1042
1262
|
this.broadcast({ t: "players", players: this.giocatoriProtocollo() });
|
|
1043
1263
|
}
|
|
@@ -1123,16 +1343,15 @@ var NucleoStanza = class _NucleoStanza {
|
|
|
1123
1343
|
}
|
|
1124
1344
|
return stato;
|
|
1125
1345
|
}
|
|
1126
|
-
|
|
1127
|
-
if ((this.manifest.voice ?? "none")
|
|
1346
|
+
impostaGuadagno(listener, speaker, gain) {
|
|
1347
|
+
if ((this.manifest.voice ?? "none") === "none") return;
|
|
1128
1348
|
const dati = this.richiediDati();
|
|
1129
|
-
if (!dati.giocatori.some((player) => player.id ===
|
|
1349
|
+
if (!dati.giocatori.some((player) => player.id === listener) || !dati.giocatori.some((player) => player.id === speaker)) throw new Error("Player not found.");
|
|
1130
1350
|
if (Number.isNaN(gain)) throw new TypeError("Voice gain must be a number.");
|
|
1131
|
-
if (
|
|
1351
|
+
if (listener === speaker) return;
|
|
1132
1352
|
const valore = Math.round(Math.min(1, Math.max(0, gain)) * 100) / 100;
|
|
1133
1353
|
dati.voceGuadagni ??= {};
|
|
1134
|
-
this.salvaGuadagno(
|
|
1135
|
-
this.salvaGuadagno(b, a, valore);
|
|
1354
|
+
this.salvaGuadagno(listener, speaker, valore);
|
|
1136
1355
|
}
|
|
1137
1356
|
salvaGuadagno(playerId, altroId, valore) {
|
|
1138
1357
|
const dati = this.richiediDati();
|
|
@@ -1256,6 +1475,11 @@ var NucleoStanza = class _NucleoStanza {
|
|
|
1256
1475
|
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
1476
|
}
|
|
1258
1477
|
async terminaSeInattiva() {
|
|
1478
|
+
if (this.manifest.persistent === true) {
|
|
1479
|
+
if (this.dati?.status === "ended" || this.dati === null || this.adattatore.ora() < Math.max(this.dati.ultimoInputAt, this.dati.ultimoCambioStatoAt) + SCADENZA_PERSISTENTE_MS) return false;
|
|
1480
|
+
await this.terminaInterna({ error: "expired" });
|
|
1481
|
+
return true;
|
|
1482
|
+
}
|
|
1259
1483
|
if (this.dati?.status !== "playing" || this.adattatore.ora() < this.dati.ultimoInputAt + INATTIVITA_MS) return false;
|
|
1260
1484
|
await this.terminaInterna({ error: "idle" });
|
|
1261
1485
|
return true;
|
|
@@ -1270,7 +1494,13 @@ var NucleoStanza = class _NucleoStanza {
|
|
|
1270
1494
|
this.serveTick() ? 1e3 / this.dati.tickRate : null
|
|
1271
1495
|
);
|
|
1272
1496
|
const prossime = [];
|
|
1273
|
-
if (this.
|
|
1497
|
+
if (this.manifest.persistent === true) {
|
|
1498
|
+
prossime.push(
|
|
1499
|
+
Math.max(this.dati.ultimoInputAt, this.dati.ultimoCambioStatoAt) + SCADENZA_PERSISTENTE_MS
|
|
1500
|
+
);
|
|
1501
|
+
} else if (this.dati.status === "playing") {
|
|
1502
|
+
prossime.push(this.dati.ultimoInputAt + INATTIVITA_MS);
|
|
1503
|
+
}
|
|
1274
1504
|
if (this.dati.countdownAt !== null) prossime.push(this.dati.countdownAt);
|
|
1275
1505
|
for (const player of this.dati.giocatori) {
|
|
1276
1506
|
if (!player.connected && player.graziaFinoA !== null) prossime.push(player.graziaFinoA);
|
|
@@ -1551,9 +1781,13 @@ var ArchivioNode = class _ArchivioNode {
|
|
|
1551
1781
|
}
|
|
1552
1782
|
};
|
|
1553
1783
|
var AdattatoreNode = class {
|
|
1554
|
-
constructor(storage) {
|
|
1784
|
+
constructor(storage, deposito, ritardoSpettatori) {
|
|
1555
1785
|
this.storage = storage;
|
|
1786
|
+
this.deposito = deposito;
|
|
1787
|
+
this.ritardoSpettatori = ritardoSpettatori;
|
|
1556
1788
|
this.connessioni = /* @__PURE__ */ new Map();
|
|
1789
|
+
this.spettatori = /* @__PURE__ */ new Map();
|
|
1790
|
+
this.timerSpettatori = /* @__PURE__ */ new Set();
|
|
1557
1791
|
this.tickTimer = null;
|
|
1558
1792
|
this.tickIntervallo = null;
|
|
1559
1793
|
this.tickGenerazione = 0;
|
|
@@ -1572,12 +1806,66 @@ var AdattatoreNode = class {
|
|
|
1572
1806
|
rimuovi(id) {
|
|
1573
1807
|
this.connessioni.delete(id);
|
|
1574
1808
|
}
|
|
1809
|
+
aggiungiSpettatore(id, socket) {
|
|
1810
|
+
this.spettatori.set(id, socket);
|
|
1811
|
+
}
|
|
1812
|
+
rimuoviSpettatore(id) {
|
|
1813
|
+
this.spettatori.delete(id);
|
|
1814
|
+
}
|
|
1815
|
+
numeroSpettatori() {
|
|
1816
|
+
return this.spettatori.size;
|
|
1817
|
+
}
|
|
1818
|
+
elencoSpettatori() {
|
|
1819
|
+
return [...this.spettatori];
|
|
1820
|
+
}
|
|
1575
1821
|
elencoConnessioni() {
|
|
1576
1822
|
return [...this.connessioni];
|
|
1577
1823
|
}
|
|
1578
1824
|
invia(connessione, messaggio) {
|
|
1579
1825
|
this.connessioni.get(connessione)?.send(JSON.stringify(messaggio));
|
|
1580
1826
|
}
|
|
1827
|
+
pubblica(messaggio) {
|
|
1828
|
+
const testo = JSON.stringify(messaggio);
|
|
1829
|
+
for (const [connessione] of this.spettatori) {
|
|
1830
|
+
this.inviaSpettatore(connessione, testo);
|
|
1831
|
+
if (messaggio.t === "status" && messaggio.status === "ended") {
|
|
1832
|
+
this.chiudiSpettatoreRitardato(connessione);
|
|
1833
|
+
}
|
|
1834
|
+
}
|
|
1835
|
+
}
|
|
1836
|
+
inviaSpettatore(connessione, testo) {
|
|
1837
|
+
this.accodaSpettatore(connessione, (socket) => {
|
|
1838
|
+
socket.send(testo);
|
|
1839
|
+
});
|
|
1840
|
+
}
|
|
1841
|
+
inviaSpettatoreSubito(connessione, testo) {
|
|
1842
|
+
this.spettatori.get(connessione)?.send(testo);
|
|
1843
|
+
}
|
|
1844
|
+
chiudiSpettatore(connessione, codice, motivo) {
|
|
1845
|
+
const socket = this.spettatori.get(connessione);
|
|
1846
|
+
this.spettatori.delete(connessione);
|
|
1847
|
+
socket?.close(codice, motivo);
|
|
1848
|
+
}
|
|
1849
|
+
chiudiSpettatoreRitardato(connessione) {
|
|
1850
|
+
this.accodaSpettatore(connessione, (socket) => {
|
|
1851
|
+
this.spettatori.delete(connessione);
|
|
1852
|
+
socket.close(4004, "room_ended");
|
|
1853
|
+
});
|
|
1854
|
+
}
|
|
1855
|
+
accodaSpettatore(connessione, azione) {
|
|
1856
|
+
if (this.ritardoSpettatori === 0) {
|
|
1857
|
+
const socket = this.spettatori.get(connessione);
|
|
1858
|
+
if (socket !== void 0) azione(socket);
|
|
1859
|
+
return;
|
|
1860
|
+
}
|
|
1861
|
+
const timer = setTimeout(() => {
|
|
1862
|
+
this.timerSpettatori.delete(timer);
|
|
1863
|
+
const socket = this.spettatori.get(connessione);
|
|
1864
|
+
if (socket !== void 0) azione(socket);
|
|
1865
|
+
}, this.ritardoSpettatori ?? RITARDO_SPETTATORI_MS);
|
|
1866
|
+
this.timerSpettatori.add(timer);
|
|
1867
|
+
timer.unref();
|
|
1868
|
+
}
|
|
1581
1869
|
chiudi(connessione, codice, motivo) {
|
|
1582
1870
|
this.connessioni.get(connessione)?.close(codice, motivo);
|
|
1583
1871
|
}
|
|
@@ -1610,6 +1898,8 @@ var AdattatoreNode = class {
|
|
|
1610
1898
|
this.tickGenerazione += 1;
|
|
1611
1899
|
if (this.tickTimer !== null) clearTimeout(this.tickTimer);
|
|
1612
1900
|
if (this.svegliaTimer !== null) clearTimeout(this.svegliaTimer);
|
|
1901
|
+
for (const timer of this.timerSpettatori) clearTimeout(timer);
|
|
1902
|
+
this.timerSpettatori.clear();
|
|
1613
1903
|
this.tickTimer = null;
|
|
1614
1904
|
this.svegliaTimer = null;
|
|
1615
1905
|
}
|
|
@@ -1648,6 +1938,7 @@ var StanzaNode = class {
|
|
|
1648
1938
|
this.manifest = manifest;
|
|
1649
1939
|
this.coda = Promise.resolve();
|
|
1650
1940
|
this.voceRoster = /* @__PURE__ */ new Map();
|
|
1941
|
+
this.voceListeners = /* @__PURE__ */ new Map();
|
|
1651
1942
|
this.voceFrequenza = /* @__PURE__ */ new Map();
|
|
1652
1943
|
this.voceUltimaRichiesta = /* @__PURE__ */ new Map();
|
|
1653
1944
|
this.frameFrequenza = /* @__PURE__ */ new Map();
|
|
@@ -1667,6 +1958,9 @@ var StanzaNode = class {
|
|
|
1667
1958
|
canJoin(identity) {
|
|
1668
1959
|
return this.serializza(() => Promise.resolve(this.nucleo.puoEntrare(identity)));
|
|
1669
1960
|
}
|
|
1961
|
+
canWatch() {
|
|
1962
|
+
return this.serializza(() => Promise.resolve(this.permessoSpettatore()));
|
|
1963
|
+
}
|
|
1670
1964
|
async connect(socket, identity) {
|
|
1671
1965
|
const connessione = randomUUID();
|
|
1672
1966
|
this.adattatore.aggiungi(connessione, socket);
|
|
@@ -1697,6 +1991,28 @@ var StanzaNode = class {
|
|
|
1697
1991
|
throw cause;
|
|
1698
1992
|
}
|
|
1699
1993
|
}
|
|
1994
|
+
watch(socket, _identity) {
|
|
1995
|
+
return this.serializza(async () => {
|
|
1996
|
+
const permesso = this.permessoSpettatore();
|
|
1997
|
+
if (!permesso.ok) return permesso;
|
|
1998
|
+
const connessione = randomUUID();
|
|
1999
|
+
this.adattatore.aggiungiSpettatore(connessione, socket);
|
|
2000
|
+
socket.on("message", (message) => {
|
|
2001
|
+
void this.serializza(() => this.riceviSpettatore(connessione, message));
|
|
2002
|
+
});
|
|
2003
|
+
socket.on("close", () => {
|
|
2004
|
+
this.adattatore.rimuoviSpettatore(connessione);
|
|
2005
|
+
this.frameFrequenza.delete(connessione);
|
|
2006
|
+
});
|
|
2007
|
+
const fotografia = this.nucleo.fotografia();
|
|
2008
|
+
if (fotografia === null) {
|
|
2009
|
+
this.adattatore.rimuoviSpettatore(connessione);
|
|
2010
|
+
return { ok: false, code: "room_not_found" };
|
|
2011
|
+
}
|
|
2012
|
+
this.inviaWatching(connessione, fotografia);
|
|
2013
|
+
return { ok: true };
|
|
2014
|
+
});
|
|
2015
|
+
}
|
|
1700
2016
|
flush() {
|
|
1701
2017
|
return this.serializza(() => this.nucleo.flush());
|
|
1702
2018
|
}
|
|
@@ -1711,6 +2027,11 @@ var StanzaNode = class {
|
|
|
1711
2027
|
socket.close(1001, "server_shutdown");
|
|
1712
2028
|
await this.serializza(() => this.nucleo.disconnetti(connessione));
|
|
1713
2029
|
}
|
|
2030
|
+
for (const [connessione, socket] of this.adattatore.elencoSpettatori()) {
|
|
2031
|
+
this.adattatore.rimuoviSpettatore(connessione);
|
|
2032
|
+
this.frameFrequenza.delete(connessione);
|
|
2033
|
+
socket.close(1001, "server_shutdown");
|
|
2034
|
+
}
|
|
1714
2035
|
}
|
|
1715
2036
|
serializza(operazione) {
|
|
1716
2037
|
const risultato = this.coda.then(operazione);
|
|
@@ -1751,6 +2072,66 @@ var StanzaNode = class {
|
|
|
1751
2072
|
}
|
|
1752
2073
|
await this.riceviVoce(connessione, player.id, player.role, message);
|
|
1753
2074
|
}
|
|
2075
|
+
permessoSpettatore() {
|
|
2076
|
+
const info = this.nucleo.info();
|
|
2077
|
+
if (info === null) return { ok: false, code: "room_not_found" };
|
|
2078
|
+
if (info.status === "ended") return { ok: false, code: "room_ended" };
|
|
2079
|
+
if (this.manifest.spectators === null) {
|
|
2080
|
+
return { ok: false, code: "spectators_disabled" };
|
|
2081
|
+
}
|
|
2082
|
+
if (this.adattatore.numeroSpettatori() >= MASSIMO_SPETTATORI) {
|
|
2083
|
+
return { ok: false, code: "spectators_full" };
|
|
2084
|
+
}
|
|
2085
|
+
return { ok: true };
|
|
2086
|
+
}
|
|
2087
|
+
inviaWatching(connessione, fotografia) {
|
|
2088
|
+
this.adattatore.inviaSpettatore(connessione, JSON.stringify({
|
|
2089
|
+
t: "watching",
|
|
2090
|
+
...fotografia,
|
|
2091
|
+
delayMs: this.manifest.spectators?.delayMs ?? RITARDO_SPETTATORI_MS
|
|
2092
|
+
}));
|
|
2093
|
+
}
|
|
2094
|
+
async riceviSpettatore(connessione, frame) {
|
|
2095
|
+
if (Buffer.byteLength(frame, "utf8") > 16 * 1024) {
|
|
2096
|
+
this.adattatore.chiudiSpettatore(connessione, 4008, "rate_limited");
|
|
2097
|
+
this.frameFrequenza.delete(connessione);
|
|
2098
|
+
return;
|
|
2099
|
+
}
|
|
2100
|
+
const ora = Date.now();
|
|
2101
|
+
const frames = (this.frameFrequenza.get(connessione) ?? []).filter((at) => ora - at < 1e3);
|
|
2102
|
+
if (frames.length >= 20) {
|
|
2103
|
+
this.adattatore.chiudiSpettatore(connessione, 4008, "rate_limited");
|
|
2104
|
+
this.frameFrequenza.delete(connessione);
|
|
2105
|
+
return;
|
|
2106
|
+
}
|
|
2107
|
+
frames.push(ora);
|
|
2108
|
+
this.frameFrequenza.set(connessione, frames);
|
|
2109
|
+
let message = null;
|
|
2110
|
+
try {
|
|
2111
|
+
const value = JSON.parse(frame);
|
|
2112
|
+
message = typeof value === "object" && value !== null && !Array.isArray(value) ? value : null;
|
|
2113
|
+
} catch {
|
|
2114
|
+
}
|
|
2115
|
+
if (message?.t === "ping" && typeof message.c === "number" && Number.isFinite(message.c)) {
|
|
2116
|
+
this.adattatore.inviaSpettatoreSubito(
|
|
2117
|
+
connessione,
|
|
2118
|
+
JSON.stringify({ t: "pong", c: message.c, s: ora })
|
|
2119
|
+
);
|
|
2120
|
+
return;
|
|
2121
|
+
}
|
|
2122
|
+
if (message?.t === "resync") {
|
|
2123
|
+
const fotografia = this.nucleo.fotografia();
|
|
2124
|
+
if (fotografia !== null) this.inviaWatching(connessione, fotografia);
|
|
2125
|
+
return;
|
|
2126
|
+
}
|
|
2127
|
+
if (message?.t === "leave") {
|
|
2128
|
+
this.adattatore.chiudiSpettatore(connessione, 1e3, "left");
|
|
2129
|
+
this.frameFrequenza.delete(connessione);
|
|
2130
|
+
return;
|
|
2131
|
+
}
|
|
2132
|
+
this.adattatore.chiudiSpettatore(connessione, 4009, "bad_message");
|
|
2133
|
+
this.frameFrequenza.delete(connessione);
|
|
2134
|
+
}
|
|
1754
2135
|
async riceviVoce(connessione, playerId, role, value) {
|
|
1755
2136
|
const richiesta = this.richiestaVoce(value);
|
|
1756
2137
|
if (richiesta === null) {
|
|
@@ -1798,17 +2179,24 @@ var StanzaNode = class {
|
|
|
1798
2179
|
return;
|
|
1799
2180
|
}
|
|
1800
2181
|
if (richiesta.op === "publish") {
|
|
1801
|
-
|
|
2182
|
+
const mic = richiesta.mic !== false;
|
|
2183
|
+
if (role === "spectator" && mic) {
|
|
1802
2184
|
this.inviaErroreVoce(connessione, richiesta, "spectator", "Spectators cannot join voice.");
|
|
1803
2185
|
return;
|
|
1804
2186
|
}
|
|
1805
|
-
|
|
1806
|
-
|
|
1807
|
-
|
|
1808
|
-
|
|
1809
|
-
|
|
1810
|
-
|
|
1811
|
-
|
|
2187
|
+
if (mic) {
|
|
2188
|
+
this.voceListeners.delete(playerId);
|
|
2189
|
+
this.voceRoster.set(playerId, {
|
|
2190
|
+
id: playerId,
|
|
2191
|
+
session: "mesh",
|
|
2192
|
+
track: "mic",
|
|
2193
|
+
muted: false,
|
|
2194
|
+
connessione
|
|
2195
|
+
});
|
|
2196
|
+
} else {
|
|
2197
|
+
this.voceRoster.delete(playerId);
|
|
2198
|
+
this.voceListeners.set(playerId, connessione);
|
|
2199
|
+
}
|
|
1812
2200
|
this.adattatore.invia(connessione, { t: "voice", op: "publish", r: richiesta.r });
|
|
1813
2201
|
this.broadcastRoster();
|
|
1814
2202
|
return;
|
|
@@ -1844,9 +2232,11 @@ var StanzaNode = class {
|
|
|
1844
2232
|
}
|
|
1845
2233
|
if (richiesta.op === "stop") {
|
|
1846
2234
|
const peer = this.voceRoster.get(playerId);
|
|
1847
|
-
const
|
|
2235
|
+
const listener = this.voceListeners.get(playerId);
|
|
2236
|
+
const rimossaPeer = peer?.connessione === connessione && this.voceRoster.delete(playerId);
|
|
2237
|
+
const rimossoListener = listener === connessione && this.voceListeners.delete(playerId);
|
|
1848
2238
|
this.adattatore.invia(connessione, { t: "voice", op: "stop", r: richiesta.r });
|
|
1849
|
-
if (
|
|
2239
|
+
if (rimossaPeer || rimossoListener) this.broadcastRoster();
|
|
1850
2240
|
return;
|
|
1851
2241
|
}
|
|
1852
2242
|
this.inviaErroreVoce(connessione, richiesta, "invalid_request", "The voice request is invalid.");
|
|
@@ -1856,9 +2246,12 @@ var StanzaNode = class {
|
|
|
1856
2246
|
return null;
|
|
1857
2247
|
}
|
|
1858
2248
|
const base = { t: "voice", r: value.r };
|
|
1859
|
-
if (value.op === "ice" || value.op === "
|
|
2249
|
+
if (value.op === "ice" || value.op === "stop") {
|
|
1860
2250
|
return { ...base, op: value.op };
|
|
1861
2251
|
}
|
|
2252
|
+
if (value.op === "publish" && (value.mic === void 0 || typeof value.mic === "boolean")) {
|
|
2253
|
+
return { ...base, op: "publish", ...value.mic === void 0 ? {} : { mic: value.mic } };
|
|
2254
|
+
}
|
|
1862
2255
|
if (value.op === "mute" && typeof value.muted === "boolean") {
|
|
1863
2256
|
return { ...base, op: "mute", muted: value.muted };
|
|
1864
2257
|
}
|
|
@@ -1881,7 +2274,8 @@ var StanzaNode = class {
|
|
|
1881
2274
|
t: "voice",
|
|
1882
2275
|
op: "roster",
|
|
1883
2276
|
mode: this.modoVoce(),
|
|
1884
|
-
peers: this.rosterPubblico()
|
|
2277
|
+
peers: this.rosterPubblico(),
|
|
2278
|
+
listeners: [...this.voceListeners.keys()].sort((a, b) => a.localeCompare(b))
|
|
1885
2279
|
});
|
|
1886
2280
|
}
|
|
1887
2281
|
broadcastRoster() {
|
|
@@ -1895,6 +2289,11 @@ var StanzaNode = class {
|
|
|
1895
2289
|
this.voceRoster.delete(playerId);
|
|
1896
2290
|
cambiato = true;
|
|
1897
2291
|
}
|
|
2292
|
+
for (const [playerId, connessione] of this.voceListeners) {
|
|
2293
|
+
if (this.nucleo.giocatoreConnesso(connessione)?.id === playerId) continue;
|
|
2294
|
+
this.voceListeners.delete(playerId);
|
|
2295
|
+
cambiato = true;
|
|
2296
|
+
}
|
|
1898
2297
|
if (cambiato) this.broadcastRoster();
|
|
1899
2298
|
}
|
|
1900
2299
|
rimuoviConnessione(connessione) {
|
|
@@ -1910,6 +2309,9 @@ var StanzaNode = class {
|
|
|
1910
2309
|
if (this.voceRoster.get(playerId)?.connessione === connessione) {
|
|
1911
2310
|
this.voceRoster.delete(playerId);
|
|
1912
2311
|
this.broadcastRoster();
|
|
2312
|
+
} else if (this.voceListeners.get(playerId) === connessione) {
|
|
2313
|
+
this.voceListeners.delete(playerId);
|
|
2314
|
+
this.broadcastRoster();
|
|
1913
2315
|
}
|
|
1914
2316
|
}
|
|
1915
2317
|
inviaErroreVoce(connessione, richiesta, code, message) {
|
|
@@ -1920,7 +2322,8 @@ var StanzaNode = class {
|
|
|
1920
2322
|
};
|
|
1921
2323
|
async function createNodeRoom(definition, manifest, options = {}) {
|
|
1922
2324
|
const storage = await ArchivioNode.apri(options.storageFile ?? null);
|
|
1923
|
-
const
|
|
2325
|
+
const ritardoSpettatori = manifest.spectators === null ? null : manifest.spectators?.delayMs ?? RITARDO_SPETTATORI_MS;
|
|
2326
|
+
const adattatore = new AdattatoreNode(storage, options.deposito ?? null, ritardoSpettatori);
|
|
1924
2327
|
const nucleo = await NucleoStanza.apri(definition, manifest, adattatore);
|
|
1925
2328
|
return new StanzaNode(nucleo, adattatore, manifest);
|
|
1926
2329
|
}
|