@caisual/kit 0.21.0 → 0.22.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/README.md +6 -9
- package/dist/host.d.ts +1 -0
- package/dist/host.js +169 -0
- package/dist/kit.js +2 -4
- package/dist/kit.js.map +4 -4
- package/dist/node.js +164 -409
- package/dist/runtime.js +212 -251
- package/dist/types/api.d.ts +1 -1
- package/dist/types/contracts/connessione.d.ts +1 -0
- package/dist/types/contracts/giornata.d.ts +1 -0
- package/dist/types/contracts/index.d.ts +3 -2
- package/dist/types/contracts/manifest.d.ts +6 -41
- package/dist/types/contracts/network-budget.d.ts +53 -0
- package/dist/types/contracts/versioni.d.ts +0 -1
- package/dist/types/crew/index.d.ts +4 -0
- package/dist/types/handshake.d.ts +0 -4
- package/dist/types/{overlay/host-bridge.d.ts → host/index.d.ts} +60 -37
- package/dist/types/index.d.ts +64 -50
- package/dist/types/node/index.d.ts +6 -23
- package/dist/types/protocollo.d.ts +1 -20
- package/dist/types/runtime/index.d.ts +4 -11
- package/dist/types/runtime/network-probe.d.ts +4 -0
- package/dist/types/server/index.d.ts +0 -10
- package/dist/types/stanza/nucleo.d.ts +8 -16
- package/dist/types/stanza/tipi.d.ts +4 -19
- package/dist/types/voce/index.d.ts +0 -1
- package/package.json +15 -17
- package/dist/overlay.d.ts +0 -1
- package/dist/overlay.js +0 -2157
- package/dist/overlay.js.map +0 -7
- package/dist/types/contracts/overlay.d.ts +0 -235
- package/dist/types/contracts/replay.d.ts +0 -83
- package/dist/types/overlay/bridge.d.ts +0 -3
- package/dist/types/overlay/i18n.d.ts +0 -128
- package/dist/types/overlay/index.d.ts +0 -9
- package/dist/types/overlay/locale.d.ts +0 -4
- package/dist/types/overlay/shortcut.d.ts +0 -4
- package/dist/types/overlay/styles.d.ts +0 -1
- package/dist/types/overlay/ui-model.d.ts +0 -53
- package/dist/types/overlay/ui.d.ts +0 -81
- package/dist/types/overlay/voice-panel.d.ts +0 -13
- package/dist/types/replay/index.d.ts +0 -65
- package/dist/types/session/index.d.ts +0 -49
- package/dist/types/session/resume.d.ts +0 -13
package/dist/node.js
CHANGED
|
@@ -40,19 +40,12 @@ function risolviModalita(manifest, mode) {
|
|
|
40
40
|
return { players: { ...scelta?.players ?? manifest.players }, lobby: scelta?.lobby ?? manifest.lobby };
|
|
41
41
|
}
|
|
42
42
|
function modalitaLocale(manifest, mode) {
|
|
43
|
-
return
|
|
43
|
+
return risolviModalita(manifest, mode).players.max === 1;
|
|
44
44
|
}
|
|
45
|
-
var
|
|
46
|
-
var RITARDO_SPETTATORI_MS = 3e3;
|
|
45
|
+
var MODE_LOCAL_MESSAGE = "Single-player modes run in the browser and have no room.";
|
|
47
46
|
|
|
48
|
-
// ../contracts/src/
|
|
49
|
-
|
|
50
|
-
var REPLAY_MAX_DURATION_MS = 30 * 60 * 1e3;
|
|
51
|
-
var REPLAY_CHUNK_BYTES = 512 * 1024;
|
|
52
|
-
var REPLAY_RETENTION_MS = 30 * 24 * 60 * 60 * 1e3;
|
|
53
|
-
|
|
54
|
-
// ../contracts/src/overlay.ts
|
|
55
|
-
function validBoardDay(value) {
|
|
47
|
+
// ../contracts/src/giornata.ts
|
|
48
|
+
function validDay(value) {
|
|
56
49
|
if (typeof value !== "string" || !/^\d{4}-\d{2}-\d{2}$/.test(value)) return false;
|
|
57
50
|
const at = Date.parse(`${value}T00:00:00Z`);
|
|
58
51
|
return Number.isFinite(at) && new Date(at).toISOString().slice(0, 10) === value;
|
|
@@ -100,6 +93,83 @@ var LimiteMessaggiStanza = class {
|
|
|
100
93
|
}
|
|
101
94
|
};
|
|
102
95
|
|
|
96
|
+
// ../contracts/src/network-budget.ts
|
|
97
|
+
var NETWORK_BUDGET = Object.freeze({
|
|
98
|
+
recipientBytesPerSecond: 1e5,
|
|
99
|
+
roomBytesPerSecond: 2e6,
|
|
100
|
+
warningRatio: 0.8,
|
|
101
|
+
windowMs: 5e3,
|
|
102
|
+
blockingWindows: 3
|
|
103
|
+
});
|
|
104
|
+
var NETWORK_GUIDANCE = `Multiplayer budget: ${NETWORK_BUDGET.recipientBytesPerSecond / 1e3} kB/s per recipient, ${NETWORK_BUDGET.roomBytesPerSecond / 1e6} MB/s per room, before compression over 5 seconds. At 20 updates/s, budget ${NETWORK_BUDGET.recipientBytesPerSecond / 2e4} kB per update. Keep visual trails and animation on the client. Warnings start at 80%. Publication measures your server automatically; repeated excess in real matches blocks new rooms only. Game input: 30 messages/s per connection.`;
|
|
105
|
+
function networkLevel(measurement) {
|
|
106
|
+
const ratio = Math.max(
|
|
107
|
+
measurement.recipientBytesPerSecond / NETWORK_BUDGET.recipientBytesPerSecond,
|
|
108
|
+
measurement.roomBytesPerSecond / NETWORK_BUDGET.roomBytesPerSecond
|
|
109
|
+
);
|
|
110
|
+
return ratio > 1 ? "exceeded" : ratio >= NETWORK_BUDGET.warningRatio ? "warning" : "ok";
|
|
111
|
+
}
|
|
112
|
+
function networkMessage(measurement) {
|
|
113
|
+
return `Network ${networkLevel(measurement)}: ${measurement.recipientBytesPerSecond} B/s per recipient (limit ${NETWORK_BUDGET.recipientBytesPerSecond}), ${measurement.roomBytesPerSecond} B/s per room (limit ${NETWORK_BUDGET.roomBytesPerSecond}), before compression over 5 seconds. Send compact changes; keep visual trails and animation on the client. At 20 updates/s, budget ${NETWORK_BUDGET.recipientBytesPerSecond / 2e4} kB per update.`;
|
|
114
|
+
}
|
|
115
|
+
var NetworkMeter = class {
|
|
116
|
+
events = [];
|
|
117
|
+
head = 0;
|
|
118
|
+
recipients = /* @__PURE__ */ new Map();
|
|
119
|
+
total = 0;
|
|
120
|
+
peak = { recipientBytesPerSecond: 0, roomBytesPerSecond: 0 };
|
|
121
|
+
checkAt = null;
|
|
122
|
+
violations = 0;
|
|
123
|
+
blocked = false;
|
|
124
|
+
encoder = new TextEncoder();
|
|
125
|
+
record(recipient, text, now = Date.now()) {
|
|
126
|
+
this.advance(now);
|
|
127
|
+
const bytes = this.encoder.encode(text).byteLength;
|
|
128
|
+
this.events.push({ at: now, recipient, bytes });
|
|
129
|
+
const recipientBytes = (this.recipients.get(recipient) ?? 0) + bytes;
|
|
130
|
+
this.recipients.set(recipient, recipientBytes);
|
|
131
|
+
this.total += bytes;
|
|
132
|
+
this.peak.recipientBytesPerSecond = Math.max(this.peak.recipientBytesPerSecond, this.rate(recipientBytes));
|
|
133
|
+
this.peak.roomBytesPerSecond = Math.max(this.peak.roomBytesPerSecond, this.rate(this.total));
|
|
134
|
+
}
|
|
135
|
+
sample(now = Date.now()) {
|
|
136
|
+
this.advance(now);
|
|
137
|
+
return { current: this.current(), peak: { ...this.peak }, consecutiveViolations: this.violations, blocked: this.blocked };
|
|
138
|
+
}
|
|
139
|
+
rate(bytes) {
|
|
140
|
+
return Math.ceil(bytes * 1e3 / NETWORK_BUDGET.windowMs);
|
|
141
|
+
}
|
|
142
|
+
current() {
|
|
143
|
+
let maximum = 0;
|
|
144
|
+
for (const bytes of this.recipients.values()) maximum = Math.max(maximum, bytes);
|
|
145
|
+
return { recipientBytesPerSecond: this.rate(maximum), roomBytesPerSecond: this.rate(this.total) };
|
|
146
|
+
}
|
|
147
|
+
advance(now) {
|
|
148
|
+
this.checkAt ??= now;
|
|
149
|
+
this.expire(now - NETWORK_BUDGET.windowMs, false);
|
|
150
|
+
if (now - this.checkAt >= NETWORK_BUDGET.windowMs) {
|
|
151
|
+
if (now - this.checkAt >= NETWORK_BUDGET.windowMs * 2) this.violations = 0;
|
|
152
|
+
this.violations = networkLevel(this.current()) === "exceeded" ? this.violations + 1 : 0;
|
|
153
|
+
this.blocked ||= this.violations >= NETWORK_BUDGET.blockingWindows;
|
|
154
|
+
this.checkAt = now;
|
|
155
|
+
}
|
|
156
|
+
this.expire(now - NETWORK_BUDGET.windowMs, true);
|
|
157
|
+
if (this.head > 1024) {
|
|
158
|
+
this.events = this.events.slice(this.head);
|
|
159
|
+
this.head = 0;
|
|
160
|
+
}
|
|
161
|
+
}
|
|
162
|
+
expire(cutoff, inclusive) {
|
|
163
|
+
while (this.head < this.events.length && (inclusive ? this.events[this.head].at <= cutoff : this.events[this.head].at < cutoff)) {
|
|
164
|
+
const event = this.events[this.head++];
|
|
165
|
+
this.total -= event.bytes;
|
|
166
|
+
const remaining = this.recipients.get(event.recipient) - event.bytes;
|
|
167
|
+
if (remaining === 0) this.recipients.delete(event.recipient);
|
|
168
|
+
else this.recipients.set(event.recipient, remaining);
|
|
169
|
+
}
|
|
170
|
+
}
|
|
171
|
+
};
|
|
172
|
+
|
|
103
173
|
// src/daily.ts
|
|
104
174
|
function prossimaMezzanotteUtc(ora) {
|
|
105
175
|
return (Math.floor(ora / 864e5) + 1) * 864e5;
|
|
@@ -345,8 +415,6 @@ var PREFISSO_SAVE = "save:";
|
|
|
345
415
|
var LIMITE_FRAME = MASSIMO_BYTE_FRAME_STANZA;
|
|
346
416
|
var LIMITE_STATO = 512 * 1024;
|
|
347
417
|
var LIMITE_SAVE = 256 * 1024;
|
|
348
|
-
var LIMITE_DEPOSITO = 256 * 1024;
|
|
349
|
-
var LIMITE_OPERAZIONI_DEPOSITO = 120;
|
|
350
418
|
var GRAZIA_MS = 6e4;
|
|
351
419
|
var STANZA_VUOTA_MS = 5 * 6e4;
|
|
352
420
|
var ATTESA_RIVINCITA_MS = 2 * 6e4;
|
|
@@ -359,16 +427,7 @@ function scadenzaInattivita(ultimoEvento, durata) {
|
|
|
359
427
|
return Math.ceil((ultimoEvento + durata) / BLOCCO_INATTIVITA_MS) * BLOCCO_INATTIVITA_MS;
|
|
360
428
|
}
|
|
361
429
|
var CHIAVE = /^[a-z0-9][a-z0-9_-]{0,31}$/;
|
|
362
|
-
var PREFISSO_CHIAVE = /^[a-z0-9_-]{0,32}$/;
|
|
363
430
|
var GAME_DEFINITION = /* @__PURE__ */ Symbol.for("@caisual/kit/game-definition");
|
|
364
|
-
var CODICI_DEPOSITO = /* @__PURE__ */ new Set([
|
|
365
|
-
"store_invalid_key",
|
|
366
|
-
"store_too_large",
|
|
367
|
-
"store_full",
|
|
368
|
-
"store_not_integer",
|
|
369
|
-
"store_unavailable",
|
|
370
|
-
"store_rate_limited"
|
|
371
|
-
]);
|
|
372
431
|
function record(value) {
|
|
373
432
|
return typeof value === "object" && value !== null && !Array.isArray(value) ? value : null;
|
|
374
433
|
}
|
|
@@ -406,7 +465,6 @@ var NucleoStanza = class _NucleoStanza {
|
|
|
406
465
|
this.adattatore = adattatore;
|
|
407
466
|
this.dati = null;
|
|
408
467
|
this.frequenza = new LimiteMessaggiStanza();
|
|
409
|
-
this.frequenzaDeposito = [];
|
|
410
468
|
this.kickRichiesti = /* @__PURE__ */ new Set();
|
|
411
469
|
this.voceGuadagniCambiati = /* @__PURE__ */ new Map();
|
|
412
470
|
this.fineRichiesta = null;
|
|
@@ -457,11 +515,6 @@ var NucleoStanza = class _NucleoStanza {
|
|
|
457
515
|
get players() {
|
|
458
516
|
return nucleo.richiediDati().giocatori.map(copiaGiocatore);
|
|
459
517
|
},
|
|
460
|
-
get host() {
|
|
461
|
-
const dati = nucleo.richiediDati();
|
|
462
|
-
const host = dati.giocatori.find((player) => player.id === dati.hostId);
|
|
463
|
-
return host === void 0 ? null : copiaGiocatore(host);
|
|
464
|
-
},
|
|
465
518
|
broadcast(message) {
|
|
466
519
|
nucleo.broadcastCreatore(message);
|
|
467
520
|
},
|
|
@@ -486,23 +539,6 @@ var NucleoStanza = class _NucleoStanza {
|
|
|
486
539
|
load(key) {
|
|
487
540
|
return nucleo.caricaSave(key);
|
|
488
541
|
},
|
|
489
|
-
shared: {
|
|
490
|
-
get(key) {
|
|
491
|
-
return nucleo.leggiDeposito(key);
|
|
492
|
-
},
|
|
493
|
-
set(key, value) {
|
|
494
|
-
return nucleo.scriviDeposito(key, value);
|
|
495
|
-
},
|
|
496
|
-
delete(key) {
|
|
497
|
-
return nucleo.eliminaDeposito(key);
|
|
498
|
-
},
|
|
499
|
-
list(prefix) {
|
|
500
|
-
return nucleo.elencaDeposito(prefix);
|
|
501
|
-
},
|
|
502
|
-
increment(key, amount = 1) {
|
|
503
|
-
return nucleo.incrementaDeposito(key, amount);
|
|
504
|
-
}
|
|
505
|
-
},
|
|
506
542
|
schedule(milliseconds, handler, payload) {
|
|
507
543
|
nucleo.pianifica(milliseconds, handler, payload);
|
|
508
544
|
},
|
|
@@ -532,9 +568,11 @@ var NucleoStanza = class _NucleoStanza {
|
|
|
532
568
|
const nucleo = new _NucleoStanza(definizione, manifest, adattatore);
|
|
533
569
|
const salvato = await adattatore.storage.get(CHIAVE_NUCLEO);
|
|
534
570
|
if (salvato !== void 0) {
|
|
571
|
+
if (modalitaLocale(manifest, salvato.mode)) throw Object.assign(new Error(MODE_LOCAL_MESSAGE), { code: "mode_local" });
|
|
535
572
|
nucleo.dati = salvato;
|
|
536
573
|
nucleo.tickRateSincronizzato = salvato.tickRate;
|
|
537
|
-
const daAggiornare = salvato.ultimoInputAt === void 0 || salvato.ultimoCambioStatoAt === void 0 || salvato.daily === void 0;
|
|
574
|
+
const daAggiornare = salvato.ultimoInputAt === void 0 || salvato.ultimoCambioStatoAt === void 0 || salvato.daily === void 0 || salvato.origin === void 0;
|
|
575
|
+
salvato.origin ??= "player";
|
|
538
576
|
salvato.daily ??= nucleo.contestoGiornaliero();
|
|
539
577
|
salvato.ultimoInputAt ??= adattatore.ora();
|
|
540
578
|
salvato.ultimoCambioStatoAt ??= salvato.ultimoInputAt;
|
|
@@ -560,13 +598,13 @@ var NucleoStanza = class _NucleoStanza {
|
|
|
560
598
|
get esiste() {
|
|
561
599
|
return this.dati !== null;
|
|
562
600
|
}
|
|
563
|
-
async crea(id, mode, _creator) {
|
|
601
|
+
async crea(id, mode, _creator, origin = "player") {
|
|
564
602
|
if (this.dati !== null) return false;
|
|
565
|
-
|
|
566
|
-
if (modalitaLocale(this.manifest, mode)) throw new Error("Local modes cannot create rooms.");
|
|
603
|
+
if (modalitaLocale(this.manifest, mode)) throw Object.assign(new Error(MODE_LOCAL_MESSAGE), { code: "mode_local" });
|
|
567
604
|
const ora = this.adattatore.ora();
|
|
568
605
|
this.dati = {
|
|
569
606
|
versione: 1,
|
|
607
|
+
origin,
|
|
570
608
|
daily: this.contestoGiornaliero(ora),
|
|
571
609
|
id,
|
|
572
610
|
mode,
|
|
@@ -577,13 +615,11 @@ var NucleoStanza = class _NucleoStanza {
|
|
|
577
615
|
ultimoCambioStatoAt: ora,
|
|
578
616
|
state: {},
|
|
579
617
|
giocatori: [],
|
|
580
|
-
hostId: null,
|
|
581
618
|
result: null,
|
|
582
619
|
resultAt: null,
|
|
583
620
|
countdownAt: null,
|
|
584
621
|
timer: [],
|
|
585
622
|
prossimoTimerId: 1,
|
|
586
|
-
punteggi: [],
|
|
587
623
|
fineInCoda: null,
|
|
588
624
|
vuotaDa: ora,
|
|
589
625
|
cancellaDopoFlush: false,
|
|
@@ -611,12 +647,14 @@ var NucleoStanza = class _NucleoStanza {
|
|
|
611
647
|
}
|
|
612
648
|
get configurazione() {
|
|
613
649
|
const mode = risolviModalita(this.manifest, this.dati?.mode ?? null);
|
|
614
|
-
return
|
|
650
|
+
return this.dati?.origin === "matchmaking" ? mode : { ...mode, lobby: true };
|
|
615
651
|
}
|
|
616
652
|
info(playerId) {
|
|
617
653
|
if (this.dati === null) return null;
|
|
618
654
|
return {
|
|
619
655
|
roomId: this.dati.id,
|
|
656
|
+
origin: this.dati.origin,
|
|
657
|
+
lobby: this.configurazione.lobby,
|
|
620
658
|
status: this.dati.status,
|
|
621
659
|
players: this.manifest.persistent === true ? this.dati.giocatori.length : this.dati.giocatori.filter(
|
|
622
660
|
(player) => player.connected || player.graziaFinoA !== null
|
|
@@ -650,7 +688,7 @@ var NucleoStanza = class _NucleoStanza {
|
|
|
650
688
|
if (ascoltatore === void 0 || speaker === void 0 || listener === speakerId) {
|
|
651
689
|
return { ok: false, reason: "unknown" };
|
|
652
690
|
}
|
|
653
|
-
if (mode === "team" && ascoltatore.
|
|
691
|
+
if (mode === "team" && ascoltatore.team !== speaker.team) {
|
|
654
692
|
return { ok: false, reason: "team" };
|
|
655
693
|
}
|
|
656
694
|
if ((dati.voceGuadagni?.[listener]?.[speakerId] ?? 1) <= 0) {
|
|
@@ -664,12 +702,15 @@ var NucleoStanza = class _NucleoStanza {
|
|
|
664
702
|
if (this.dati.status === "ended") return { ok: false, code: "room_ended" };
|
|
665
703
|
const esistente = this.dati.giocatori.find((player) => player.id === identity.id);
|
|
666
704
|
if (esistente !== void 0) return { ok: true };
|
|
667
|
-
if (this.configurazione.lobby && !["lobby", "finished"].includes(this.dati.status)) {
|
|
705
|
+
if (this.configurazione.lobby && !["lobby", "countdown", "finished"].includes(this.dati.status)) {
|
|
668
706
|
return { ok: false, code: "room_playing" };
|
|
669
707
|
}
|
|
670
708
|
if (this.dati.giocatori.length >= this.configurazione.players.max) {
|
|
671
709
|
return { ok: false, code: "room_full" };
|
|
672
710
|
}
|
|
711
|
+
if (this.manifest.roles.length > 0 && this.ruoloAutomatico() === null) {
|
|
712
|
+
return { ok: false, code: "room_full" };
|
|
713
|
+
}
|
|
673
714
|
return { ok: true };
|
|
674
715
|
}
|
|
675
716
|
async entra(identity, connessione) {
|
|
@@ -709,7 +750,6 @@ var NucleoStanza = class _NucleoStanza {
|
|
|
709
750
|
player.connessione = connessione;
|
|
710
751
|
player.seq = 0;
|
|
711
752
|
}
|
|
712
|
-
dati.hostId ??= player.id;
|
|
713
753
|
dati.vuotaDa = null;
|
|
714
754
|
dati.ultimoInputAt = ora;
|
|
715
755
|
const primaConnessione = !this.configurazione.lobby && dati.status === "lobby";
|
|
@@ -726,7 +766,6 @@ var NucleoStanza = class _NucleoStanza {
|
|
|
726
766
|
}
|
|
727
767
|
if (primaConnessione) {
|
|
728
768
|
await this.chiama(this.definizione.onStart, this.room);
|
|
729
|
-
this.iniziaReplay();
|
|
730
769
|
this.inviaStatus(ora);
|
|
731
770
|
}
|
|
732
771
|
await this.concludiEvento();
|
|
@@ -734,6 +773,7 @@ var NucleoStanza = class _NucleoStanza {
|
|
|
734
773
|
this.inviaWelcome(player);
|
|
735
774
|
this.inviaSnapshotTutti();
|
|
736
775
|
this.inviaGiocatori();
|
|
776
|
+
this.rivalutaAvvio();
|
|
737
777
|
}
|
|
738
778
|
await this.persistiEProgramma();
|
|
739
779
|
return { ok: true };
|
|
@@ -747,10 +787,9 @@ var NucleoStanza = class _NucleoStanza {
|
|
|
747
787
|
player.connected = false;
|
|
748
788
|
player.connessione = null;
|
|
749
789
|
player.graziaFinoA = this.adattatore.ora() + GRAZIA_MS;
|
|
750
|
-
if (this.dati.status === "finished" || this.manifest.persistent === true && this.dati.status
|
|
790
|
+
if (this.dati.status === "finished" || this.manifest.persistent === true && ["lobby", "countdown"].includes(this.dati.status)) player.ready = false;
|
|
751
791
|
this.frequenza.delete(connessione);
|
|
752
|
-
|
|
753
|
-
this.verificaCountdown();
|
|
792
|
+
this.rivalutaAvvio();
|
|
754
793
|
await this.chiama(this.definizione.onConnection, this.room, copiaGiocatore(player), false);
|
|
755
794
|
await this.concludiEvento();
|
|
756
795
|
this.inviaGiocatori();
|
|
@@ -817,6 +856,7 @@ var NucleoStanza = class _NucleoStanza {
|
|
|
817
856
|
this.dati.ultimoInputAt = ora;
|
|
818
857
|
player.ready = message.ready;
|
|
819
858
|
this.inviaGiocatori();
|
|
859
|
+
this.rivalutaAvvio();
|
|
820
860
|
await this.persistiEProgramma();
|
|
821
861
|
return;
|
|
822
862
|
}
|
|
@@ -858,12 +898,6 @@ var NucleoStanza = class _NucleoStanza {
|
|
|
858
898
|
await this.persistiEProgramma();
|
|
859
899
|
return;
|
|
860
900
|
}
|
|
861
|
-
if (message.t === "start") {
|
|
862
|
-
if (!this.inLobby(player)) return;
|
|
863
|
-
this.dati.ultimoInputAt = ora;
|
|
864
|
-
await this.avviaCountdown(player);
|
|
865
|
-
return;
|
|
866
|
-
}
|
|
867
901
|
if (message.t === "msg") {
|
|
868
902
|
if (!Number.isSafeInteger(message.seq) || message.seq < 1) {
|
|
869
903
|
await this.messaggioErrato(player);
|
|
@@ -877,11 +911,6 @@ var NucleoStanza = class _NucleoStanza {
|
|
|
877
911
|
return;
|
|
878
912
|
}
|
|
879
913
|
player.seq = seq;
|
|
880
|
-
if (player.role === "spectator") {
|
|
881
|
-
this.inviaErrore(player, "spectator", "Spectators cannot send game input.");
|
|
882
|
-
await this.persistiEProgramma();
|
|
883
|
-
return;
|
|
884
|
-
}
|
|
885
914
|
if (this.dati.status === "finished") return;
|
|
886
915
|
this.dati.ultimoInputAt = ora;
|
|
887
916
|
await this.chiama(
|
|
@@ -946,7 +975,6 @@ var NucleoStanza = class _NucleoStanza {
|
|
|
946
975
|
this.dati.ultimoInputAt = ora;
|
|
947
976
|
this.dati.countdownAt = null;
|
|
948
977
|
await this.chiama(this.definizione.onStart, this.room);
|
|
949
|
-
this.iniziaReplay();
|
|
950
978
|
this.inviaStatus(ora);
|
|
951
979
|
}
|
|
952
980
|
}
|
|
@@ -975,9 +1003,8 @@ var NucleoStanza = class _NucleoStanza {
|
|
|
975
1003
|
await this.persistiEProgramma();
|
|
976
1004
|
return dati.flushInAttesa;
|
|
977
1005
|
}
|
|
978
|
-
if (dati.
|
|
979
|
-
dati.flushInAttesa = { id: crypto.randomUUID(),
|
|
980
|
-
dati.punteggi = [];
|
|
1006
|
+
if (dati.fineInCoda === null) return null;
|
|
1007
|
+
dati.flushInAttesa = { id: crypto.randomUUID(), ended: dati.fineInCoda };
|
|
981
1008
|
dati.fineInCoda = null;
|
|
982
1009
|
await this.persistiEProgramma();
|
|
983
1010
|
return dati.flushInAttesa;
|
|
@@ -991,10 +1018,8 @@ var NucleoStanza = class _NucleoStanza {
|
|
|
991
1018
|
async flush() {
|
|
992
1019
|
const dati = this.richiediDati();
|
|
993
1020
|
const esito = {
|
|
994
|
-
scores: dati.punteggi.map((score) => ({ ...score })),
|
|
995
1021
|
ended: dati.fineInCoda === null ? null : { result: dati.fineInCoda.result, at: dati.fineInCoda.at }
|
|
996
1022
|
};
|
|
997
|
-
dati.punteggi = [];
|
|
998
1023
|
dati.fineInCoda = null;
|
|
999
1024
|
if (dati.cancellaDopoFlush && dati.status === "ended") {
|
|
1000
1025
|
const chiavi = await this.adattatore.storage.list();
|
|
@@ -1019,13 +1044,12 @@ var NucleoStanza = class _NucleoStanza {
|
|
|
1019
1044
|
if (player.connessione !== null) this.frequenza.delete(player.connessione);
|
|
1020
1045
|
player.connessione = null;
|
|
1021
1046
|
player.graziaFinoA = ora + GRAZIA_MS;
|
|
1022
|
-
if (dati.status === "finished" || this.manifest.persistent === true && dati.status
|
|
1047
|
+
if (dati.status === "finished" || this.manifest.persistent === true && ["lobby", "countdown"].includes(dati.status)) player.ready = false;
|
|
1023
1048
|
disconnessi.push(player);
|
|
1024
1049
|
}
|
|
1025
1050
|
}
|
|
1026
1051
|
if (disconnessi.length > 0) {
|
|
1027
|
-
this.
|
|
1028
|
-
this.verificaCountdown();
|
|
1052
|
+
this.rivalutaAvvio();
|
|
1029
1053
|
for (const player of disconnessi) {
|
|
1030
1054
|
await this.chiama(this.definizione.onConnection, this.room, copiaGiocatore(player), false);
|
|
1031
1055
|
}
|
|
@@ -1048,7 +1072,7 @@ var NucleoStanza = class _NucleoStanza {
|
|
|
1048
1072
|
const mancaB = countB < b.min ? 0 : 1;
|
|
1049
1073
|
return mancaA - mancaB || countA - countB;
|
|
1050
1074
|
});
|
|
1051
|
-
return candidati[0]?.id ??
|
|
1075
|
+
return candidati[0]?.id ?? null;
|
|
1052
1076
|
}
|
|
1053
1077
|
squadraAutomatica() {
|
|
1054
1078
|
if (this.manifest.teams === null) return null;
|
|
@@ -1071,39 +1095,37 @@ var NucleoStanza = class _NucleoStanza {
|
|
|
1071
1095
|
}
|
|
1072
1096
|
cambiaRuolo(player, roleId) {
|
|
1073
1097
|
const ruolo = this.manifest.roles.find((item) => item.id === roleId);
|
|
1074
|
-
if (ruolo === void 0
|
|
1098
|
+
if (ruolo === void 0) {
|
|
1075
1099
|
return { code: "invalid_role", message: "This role does not exist." };
|
|
1076
1100
|
}
|
|
1077
1101
|
const occupati = this.richiediDati().giocatori.filter(
|
|
1078
1102
|
(item) => item.id !== player.id && item.role === roleId
|
|
1079
1103
|
).length;
|
|
1080
|
-
if (ruolo
|
|
1104
|
+
if (ruolo.max !== void 0 && occupati >= ruolo.max) {
|
|
1081
1105
|
return { code: "role_full", message: "This role is full." };
|
|
1082
1106
|
}
|
|
1083
1107
|
const ruoloPrecedente = player.role;
|
|
1084
1108
|
const squadraPrecedente = player.team;
|
|
1085
1109
|
player.role = roleId;
|
|
1086
|
-
if (
|
|
1087
|
-
else if (player.team === null) player.team = this.squadraAutomatica();
|
|
1110
|
+
if (player.team === null) player.team = this.squadraAutomatica();
|
|
1088
1111
|
if (this.richiediDati().status === "lobby") player.ready = false;
|
|
1089
1112
|
if (player.role !== ruoloPrecedente || player.team !== squadraPrecedente) {
|
|
1090
1113
|
this.rivediVoce();
|
|
1091
1114
|
}
|
|
1092
1115
|
this.inviaGiocatori();
|
|
1116
|
+
this.rivalutaAvvio();
|
|
1093
1117
|
return null;
|
|
1094
1118
|
}
|
|
1095
1119
|
cambiaSquadra(player, team) {
|
|
1096
1120
|
if (this.manifest.teams === null || !Number.isInteger(team) || team < 1 || team > this.manifest.teams.max) {
|
|
1097
1121
|
return { code: "invalid_team", message: "This team does not exist." };
|
|
1098
1122
|
}
|
|
1099
|
-
if (player.role === "spectator") {
|
|
1100
|
-
return { code: "spectator", message: "Spectators cannot join a team." };
|
|
1101
|
-
}
|
|
1102
1123
|
const squadraPrecedente = player.team;
|
|
1103
1124
|
player.team = team;
|
|
1104
1125
|
if (this.richiediDati().status === "lobby") player.ready = false;
|
|
1105
1126
|
if (player.team !== squadraPrecedente) this.rivediVoce();
|
|
1106
1127
|
this.inviaGiocatori();
|
|
1128
|
+
this.rivalutaAvvio();
|
|
1107
1129
|
return null;
|
|
1108
1130
|
}
|
|
1109
1131
|
impostaRuoloDalServer(playerId, roleId) {
|
|
@@ -1125,21 +1147,20 @@ var NucleoStanza = class _NucleoStanza {
|
|
|
1125
1147
|
}
|
|
1126
1148
|
erroreMinimi(saltaPronti = false) {
|
|
1127
1149
|
const connessi = this.richiediDati().giocatori.filter((player) => player.connected);
|
|
1128
|
-
|
|
1129
|
-
if (attivi.length < this.configurazione.players.min) {
|
|
1150
|
+
if (connessi.length < this.configurazione.players.min) {
|
|
1130
1151
|
return { code: "not_enough_players", message: "The room does not have enough players." };
|
|
1131
1152
|
}
|
|
1132
1153
|
if (!saltaPronti && connessi.some((player) => !player.ready)) {
|
|
1133
1154
|
return { code: "players_not_ready", message: "Every connected player must be ready." };
|
|
1134
1155
|
}
|
|
1135
1156
|
for (const role of this.manifest.roles) {
|
|
1136
|
-
if (
|
|
1157
|
+
if (connessi.filter((player) => player.role === role.id).length < role.min) {
|
|
1137
1158
|
return { code: "role_minimum", message: `Role ${role.id} does not meet its minimum.` };
|
|
1138
1159
|
}
|
|
1139
1160
|
}
|
|
1140
1161
|
if (this.manifest.teams !== null) {
|
|
1141
|
-
const squadre = new Set(
|
|
1142
|
-
if (
|
|
1162
|
+
const squadre = new Set(connessi.map((player) => player.team).filter((team) => team !== null));
|
|
1163
|
+
if (connessi.some((player) => player.team === null) || squadre.size < this.manifest.teams.min) {
|
|
1143
1164
|
return { code: "team_minimum", message: "The room does not have enough teams." };
|
|
1144
1165
|
}
|
|
1145
1166
|
}
|
|
@@ -1151,32 +1172,20 @@ var NucleoStanza = class _NucleoStanza {
|
|
|
1151
1172
|
this.inviaErrore(player, "rematch_unavailable", "This room is not waiting for a rematch.");
|
|
1152
1173
|
return;
|
|
1153
1174
|
}
|
|
1154
|
-
if (
|
|
1155
|
-
|
|
1156
|
-
|
|
1157
|
-
|
|
1158
|
-
|
|
1159
|
-
player.ready = true;
|
|
1160
|
-
this.inviaGiocatori();
|
|
1161
|
-
if (dati.rivincita?.autoStart && this.prontiRivincita()) await this.avviaRivincita();
|
|
1162
|
-
else await this.persistiEProgramma();
|
|
1163
|
-
return;
|
|
1164
|
-
}
|
|
1165
|
-
if (dati.hostId !== player.id) return;
|
|
1166
|
-
if (!this.prontiRivincita(dati.rivincita?.autoStart === true)) {
|
|
1167
|
-
this.inviaErrore(player, "players_not_ready", "Enough connected players must be ready for the rematch.");
|
|
1168
|
-
return;
|
|
1169
|
-
}
|
|
1170
|
-
await this.avviaRivincita();
|
|
1175
|
+
if (player.ready) return;
|
|
1176
|
+
player.ready = true;
|
|
1177
|
+
this.inviaGiocatori();
|
|
1178
|
+
if (this.prontiRivincita()) await this.avviaRivincita();
|
|
1179
|
+
else await this.persistiEProgramma();
|
|
1171
1180
|
}
|
|
1172
|
-
prontiRivincita(
|
|
1173
|
-
const attivi = this.richiediDati().giocatori.filter((item) => item.connected
|
|
1174
|
-
return attivi.filter((item) => item.ready).length >= this.configurazione.players.min &&
|
|
1181
|
+
prontiRivincita() {
|
|
1182
|
+
const attivi = this.richiediDati().giocatori.filter((item) => item.connected);
|
|
1183
|
+
return attivi.filter((item) => item.ready).length >= this.configurazione.players.min && attivi.every((item) => item.ready);
|
|
1175
1184
|
}
|
|
1176
1185
|
async avviaRivincita() {
|
|
1177
|
-
const dati = this.richiediDati()
|
|
1178
|
-
const keepSetup = dati.rivincita?.keepSetup === true
|
|
1179
|
-
dati.status =
|
|
1186
|
+
const dati = this.richiediDati();
|
|
1187
|
+
const keepSetup = dati.rivincita?.keepSetup === true;
|
|
1188
|
+
dati.status = keepSetup ? "countdown" : this.configurazione.lobby ? "lobby" : "playing";
|
|
1180
1189
|
dati.countdownRivincita = keepSetup;
|
|
1181
1190
|
dati.countdownAt = keepSetup ? this.adattatore.ora() + COUNTDOWN_MS : null;
|
|
1182
1191
|
dati.rivincitaFinoA = null;
|
|
@@ -1190,7 +1199,6 @@ var NucleoStanza = class _NucleoStanza {
|
|
|
1190
1199
|
await this.applicaAzioni();
|
|
1191
1200
|
if (dati.status === "playing") {
|
|
1192
1201
|
await this.chiama(this.definizione.onStart, this.room);
|
|
1193
|
-
this.iniziaReplay();
|
|
1194
1202
|
}
|
|
1195
1203
|
await this.concludiEvento();
|
|
1196
1204
|
if (dati.status === "lobby" || dati.status === "playing" || dati.status === "countdown") {
|
|
@@ -1200,29 +1208,25 @@ var NucleoStanza = class _NucleoStanza {
|
|
|
1200
1208
|
}
|
|
1201
1209
|
await this.persistiEProgramma();
|
|
1202
1210
|
}
|
|
1203
|
-
|
|
1211
|
+
/**
|
|
1212
|
+
* Nessuno ospita e nessuno preme "inizia": appena tutti i connessi sono pronti e i
|
|
1213
|
+
* minimi di giocatori, ruoli e squadre sono rispettati parte il countdown; se la
|
|
1214
|
+
* condizione cade durante il countdown si torna in lobby.
|
|
1215
|
+
*/
|
|
1216
|
+
rivalutaAvvio() {
|
|
1204
1217
|
const dati = this.richiediDati();
|
|
1205
|
-
if (dati.
|
|
1206
|
-
this.
|
|
1207
|
-
|
|
1208
|
-
|
|
1209
|
-
|
|
1210
|
-
if (errore !== null) {
|
|
1211
|
-
this.inviaErrore(player, errore.code, errore.message);
|
|
1218
|
+
if (dati.status === "countdown") {
|
|
1219
|
+
if (this.erroreMinimi(dati.countdownRivincita) === null) return;
|
|
1220
|
+
dati.status = "lobby";
|
|
1221
|
+
dati.countdownAt = null;
|
|
1222
|
+
this.inviaStatus(this.adattatore.ora());
|
|
1212
1223
|
return;
|
|
1213
1224
|
}
|
|
1225
|
+
if (dati.status !== "lobby" || this.erroreMinimi() !== null) return;
|
|
1214
1226
|
dati.status = "countdown";
|
|
1215
1227
|
dati.countdownRivincita = false;
|
|
1216
1228
|
dati.countdownAt = this.adattatore.ora() + COUNTDOWN_MS;
|
|
1217
1229
|
this.inviaStatus(dati.countdownAt);
|
|
1218
|
-
await this.persistiEProgramma();
|
|
1219
|
-
}
|
|
1220
|
-
verificaCountdown() {
|
|
1221
|
-
const dati = this.richiediDati();
|
|
1222
|
-
if (dati.status !== "countdown" || this.erroreMinimi(dati.countdownRivincita) === null) return;
|
|
1223
|
-
dati.status = "lobby";
|
|
1224
|
-
dati.countdownAt = null;
|
|
1225
|
-
this.inviaStatus(this.adattatore.ora());
|
|
1226
1230
|
}
|
|
1227
1231
|
async messaggioErrato(player) {
|
|
1228
1232
|
await this.chiudiConnessione(player, 4009, "bad_message");
|
|
@@ -1240,7 +1244,6 @@ var NucleoStanza = class _NucleoStanza {
|
|
|
1240
1244
|
if (player.connessione !== null) this.frequenza.delete(player.connessione);
|
|
1241
1245
|
this.rivediVoce();
|
|
1242
1246
|
this.pulisciGuadagni(player.id);
|
|
1243
|
-
if (dati.hostId === player.id) this.assegnaHost();
|
|
1244
1247
|
if (dati.giocatori.length === 0) dati.vuotaDa = this.adattatore.ora();
|
|
1245
1248
|
await this.chiama(
|
|
1246
1249
|
this.definizione.onLeave,
|
|
@@ -1248,12 +1251,7 @@ var NucleoStanza = class _NucleoStanza {
|
|
|
1248
1251
|
copiaGiocatore({ ...player, connected: false, connessione: null }),
|
|
1249
1252
|
reason
|
|
1250
1253
|
);
|
|
1251
|
-
this.
|
|
1252
|
-
}
|
|
1253
|
-
assegnaHost() {
|
|
1254
|
-
const dati = this.richiediDati();
|
|
1255
|
-
const host = dati.giocatori.filter((player) => player.connected).sort((a, b) => a.entratoAt - b.entratoAt)[0];
|
|
1256
|
-
dati.hostId = host?.id ?? null;
|
|
1254
|
+
this.rivalutaAvvio();
|
|
1257
1255
|
}
|
|
1258
1256
|
async eseguiTimer(timer) {
|
|
1259
1257
|
const value = this.definizione[timer.handler];
|
|
@@ -1319,91 +1317,6 @@ var NucleoStanza = class _NucleoStanza {
|
|
|
1319
1317
|
if (!json.ok) throw new Error("The saved value is invalid.");
|
|
1320
1318
|
return json.valore;
|
|
1321
1319
|
}
|
|
1322
|
-
verificaChiaveDeposito(key) {
|
|
1323
|
-
if (typeof key !== "string" || !CHIAVE.test(key)) {
|
|
1324
|
-
throw erroreConCodice(
|
|
1325
|
-
"store_invalid_key",
|
|
1326
|
-
"Shared store keys must use lowercase letters, numbers, underscores, or hyphens."
|
|
1327
|
-
);
|
|
1328
|
-
}
|
|
1329
|
-
}
|
|
1330
|
-
verificaPrefissoDeposito(prefix) {
|
|
1331
|
-
if (prefix !== void 0 && (typeof prefix !== "string" || !PREFISSO_CHIAVE.test(prefix))) {
|
|
1332
|
-
throw erroreConCodice(
|
|
1333
|
-
"store_invalid_key",
|
|
1334
|
-
"Shared store prefixes may contain lowercase letters, numbers, underscores, or hyphens."
|
|
1335
|
-
);
|
|
1336
|
-
}
|
|
1337
|
-
}
|
|
1338
|
-
contaOperazioneDeposito() {
|
|
1339
|
-
const ora = this.adattatore.ora();
|
|
1340
|
-
this.frequenzaDeposito = this.frequenzaDeposito.filter((at) => ora - at < 6e4);
|
|
1341
|
-
if (this.frequenzaDeposito.length >= LIMITE_OPERAZIONI_DEPOSITO) {
|
|
1342
|
-
throw erroreConCodice(
|
|
1343
|
-
"store_rate_limited",
|
|
1344
|
-
"The shared store allows at most 120 operations per minute for each room."
|
|
1345
|
-
);
|
|
1346
|
-
}
|
|
1347
|
-
this.frequenzaDeposito.push(ora);
|
|
1348
|
-
}
|
|
1349
|
-
async usaDeposito(operazione) {
|
|
1350
|
-
this.contaOperazioneDeposito();
|
|
1351
|
-
const deposito = this.adattatore.deposito;
|
|
1352
|
-
if (deposito === null) {
|
|
1353
|
-
throw erroreConCodice("store_unavailable", "The shared store is unavailable.");
|
|
1354
|
-
}
|
|
1355
|
-
try {
|
|
1356
|
-
return await operazione(deposito);
|
|
1357
|
-
} catch (cause) {
|
|
1358
|
-
const code = cause instanceof Error ? cause.code : void 0;
|
|
1359
|
-
if (typeof code === "string" && CODICI_DEPOSITO.has(code)) throw cause;
|
|
1360
|
-
throw erroreConCodice("store_unavailable", "The shared store is unavailable.");
|
|
1361
|
-
}
|
|
1362
|
-
}
|
|
1363
|
-
async leggiDeposito(key) {
|
|
1364
|
-
this.verificaChiaveDeposito(key);
|
|
1365
|
-
const value = await this.usaDeposito((deposito) => deposito.get(key));
|
|
1366
|
-
if (value === null) return null;
|
|
1367
|
-
try {
|
|
1368
|
-
return JSON.parse(JSON.stringify(value));
|
|
1369
|
-
} catch {
|
|
1370
|
-
throw erroreConCodice("store_unavailable", "The shared store returned invalid data.");
|
|
1371
|
-
}
|
|
1372
|
-
}
|
|
1373
|
-
async scriviDeposito(key, value) {
|
|
1374
|
-
this.verificaChiaveDeposito(key);
|
|
1375
|
-
const json = analizzaJson(value);
|
|
1376
|
-
if (!json.ok || json.bytes > LIMITE_DEPOSITO) {
|
|
1377
|
-
throw erroreConCodice(
|
|
1378
|
-
"store_too_large",
|
|
1379
|
-
"Shared store values must be valid JSON of at most 262144 bytes."
|
|
1380
|
-
);
|
|
1381
|
-
}
|
|
1382
|
-
await this.usaDeposito((deposito) => deposito.set(key, json.valore));
|
|
1383
|
-
}
|
|
1384
|
-
async eliminaDeposito(key) {
|
|
1385
|
-
this.verificaChiaveDeposito(key);
|
|
1386
|
-
await this.usaDeposito((deposito) => deposito.delete(key));
|
|
1387
|
-
}
|
|
1388
|
-
async elencaDeposito(prefix) {
|
|
1389
|
-
this.verificaPrefissoDeposito(prefix);
|
|
1390
|
-
const keys = await this.usaDeposito((deposito) => deposito.list(prefix));
|
|
1391
|
-
if (!Array.isArray(keys) || keys.some((key) => typeof key !== "string" || !CHIAVE.test(key))) {
|
|
1392
|
-
throw erroreConCodice("store_unavailable", "The shared store returned invalid keys.");
|
|
1393
|
-
}
|
|
1394
|
-
return [...keys].sort().slice(0, 4096);
|
|
1395
|
-
}
|
|
1396
|
-
async incrementaDeposito(key, amount) {
|
|
1397
|
-
this.verificaChiaveDeposito(key);
|
|
1398
|
-
if (!Number.isSafeInteger(amount)) {
|
|
1399
|
-
throw erroreConCodice("store_not_integer", "Shared store increments must be safe integers.");
|
|
1400
|
-
}
|
|
1401
|
-
const value = await this.usaDeposito((deposito) => deposito.increment(key, amount));
|
|
1402
|
-
if (!Number.isSafeInteger(value)) {
|
|
1403
|
-
throw erroreConCodice("store_unavailable", "The shared store returned an invalid integer.");
|
|
1404
|
-
}
|
|
1405
|
-
return value;
|
|
1406
|
-
}
|
|
1407
1320
|
messaggioCreatore(message) {
|
|
1408
1321
|
const json = analizzaJson(message);
|
|
1409
1322
|
if (!json.ok) throw new TypeError("Messages must be valid JSON.");
|
|
@@ -1430,7 +1343,6 @@ var NucleoStanza = class _NucleoStanza {
|
|
|
1430
1343
|
this.adattatore.invia(player.connessione, message);
|
|
1431
1344
|
}
|
|
1432
1345
|
}
|
|
1433
|
-
if (message.t !== "flush") this.adattatore.pubblica(message);
|
|
1434
1346
|
}
|
|
1435
1347
|
inviaErrore(player, code, message) {
|
|
1436
1348
|
if (player.connected && player.connessione !== null) {
|
|
@@ -1475,7 +1387,6 @@ var NucleoStanza = class _NucleoStanza {
|
|
|
1475
1387
|
tick: dati.tick,
|
|
1476
1388
|
tickRate: dati.tickRate,
|
|
1477
1389
|
serverTime: this.adattatore.ora(),
|
|
1478
|
-
host: dati.hostId,
|
|
1479
1390
|
countdownAt: dati.countdownAt,
|
|
1480
1391
|
configuration: {
|
|
1481
1392
|
players: { ...this.configurazione.players },
|
|
@@ -1486,7 +1397,7 @@ var NucleoStanza = class _NucleoStanza {
|
|
|
1486
1397
|
};
|
|
1487
1398
|
}
|
|
1488
1399
|
inviaGiocatori() {
|
|
1489
|
-
this.broadcast({ t: "players", players: this.giocatoriProtocollo()
|
|
1400
|
+
this.broadcast({ t: "players", players: this.giocatoriProtocollo() });
|
|
1490
1401
|
}
|
|
1491
1402
|
inviaStatus(at) {
|
|
1492
1403
|
const dati = this.richiediDati();
|
|
@@ -1494,7 +1405,6 @@ var NucleoStanza = class _NucleoStanza {
|
|
|
1494
1405
|
t: "status",
|
|
1495
1406
|
rematch: dati.status === "finished" ? dati.rivincita ?? null : null,
|
|
1496
1407
|
status: dati.status,
|
|
1497
|
-
host: dati.hostId,
|
|
1498
1408
|
countdownAt: dati.countdownAt,
|
|
1499
1409
|
at,
|
|
1500
1410
|
result: dati.status === "ended" || dati.status === "finished" ? dati.result : null
|
|
@@ -1516,13 +1426,6 @@ var NucleoStanza = class _NucleoStanza {
|
|
|
1516
1426
|
dati.statoSincronizzato = copiaJson(dati.state);
|
|
1517
1427
|
dati.tickSincronizzato = dati.tick;
|
|
1518
1428
|
}
|
|
1519
|
-
iniziaReplay() {
|
|
1520
|
-
if (!this.manifest.replays || !this.adattatore.inizioReplay) return;
|
|
1521
|
-
const json = analizzaJson(this.richiediDati().state);
|
|
1522
|
-
if (!json.ok || json.bytes > LIMITE_STATO) return;
|
|
1523
|
-
this.inviaSnapshotTutti();
|
|
1524
|
-
this.adattatore.inizioReplay(this.fotografia());
|
|
1525
|
-
}
|
|
1526
1429
|
inviaSnapshotTutti() {
|
|
1527
1430
|
const dati = this.richiediDati();
|
|
1528
1431
|
const stato = analizzaJson(dati.state);
|
|
@@ -1572,7 +1475,7 @@ var NucleoStanza = class _NucleoStanza {
|
|
|
1572
1475
|
async concludiEvento() {
|
|
1573
1476
|
await this.applicaAzioni();
|
|
1574
1477
|
if (this.dati === null || this.dati.status === "ended") return;
|
|
1575
|
-
if (this.dati.status === "finished" && this.dati.rivincita
|
|
1478
|
+
if (this.dati.status === "finished" && this.dati.rivincita !== null && this.prontiRivincita()) {
|
|
1576
1479
|
await this.avviaRivincita();
|
|
1577
1480
|
return;
|
|
1578
1481
|
}
|
|
@@ -1671,10 +1574,7 @@ var NucleoStanza = class _NucleoStanza {
|
|
|
1671
1574
|
if (dati.status === "ended") return;
|
|
1672
1575
|
const giaFinita = dati.status === "finished";
|
|
1673
1576
|
dati.status = rematch ? "finished" : "ended";
|
|
1674
|
-
dati.rivincita = rematch ? {
|
|
1675
|
-
keepSetup: typeof rematch === "object" && rematch.keepSetup === true,
|
|
1676
|
-
autoStart: typeof rematch === "object" && rematch.autoStart === true
|
|
1677
|
-
} : null;
|
|
1577
|
+
dati.rivincita = rematch ? { keepSetup: typeof rematch === "object" && rematch.keepSetup === true } : null;
|
|
1678
1578
|
dati.prontiPrecedenti = rematch ? dati.giocatori.filter((p) => p.ready).map((p) => p.id) : [];
|
|
1679
1579
|
dati.rivincitaFinoA = rematch ? this.adattatore.ora() + ATTESA_RIVINCITA_MS : null;
|
|
1680
1580
|
dati.timer = [];
|
|
@@ -2067,14 +1967,10 @@ var ArchivioNode = class _ArchivioNode {
|
|
|
2067
1967
|
}
|
|
2068
1968
|
};
|
|
2069
1969
|
var AdattatoreNode = class {
|
|
2070
|
-
constructor(storage,
|
|
1970
|
+
constructor(storage, dailyDay) {
|
|
2071
1971
|
this.storage = storage;
|
|
2072
|
-
this.deposito = deposito;
|
|
2073
|
-
this.ritardoSpettatori = ritardoSpettatori;
|
|
2074
1972
|
this.dailyDay = dailyDay;
|
|
2075
1973
|
this.connessioni = /* @__PURE__ */ new Map();
|
|
2076
|
-
this.spettatori = /* @__PURE__ */ new Map();
|
|
2077
|
-
this.timerSpettatori = /* @__PURE__ */ new Set();
|
|
2078
1974
|
this.tickTimer = null;
|
|
2079
1975
|
this.tickIntervallo = null;
|
|
2080
1976
|
this.tickGenerazione = 0;
|
|
@@ -2082,6 +1978,8 @@ var AdattatoreNode = class {
|
|
|
2082
1978
|
this.svegliaAt = null;
|
|
2083
1979
|
this.eseguiTick = async () => void 0;
|
|
2084
1980
|
this.eseguiSveglia = async () => void 0;
|
|
1981
|
+
this.network = new NetworkMeter();
|
|
1982
|
+
this.networkWarning = "ok";
|
|
2085
1983
|
}
|
|
2086
1984
|
collega(input) {
|
|
2087
1985
|
this.eseguiTick = input.tick;
|
|
@@ -2093,65 +1991,21 @@ var AdattatoreNode = class {
|
|
|
2093
1991
|
rimuovi(id) {
|
|
2094
1992
|
this.connessioni.delete(id);
|
|
2095
1993
|
}
|
|
2096
|
-
aggiungiSpettatore(id, socket) {
|
|
2097
|
-
this.spettatori.set(id, socket);
|
|
2098
|
-
}
|
|
2099
|
-
rimuoviSpettatore(id) {
|
|
2100
|
-
this.spettatori.delete(id);
|
|
2101
|
-
}
|
|
2102
|
-
numeroSpettatori() {
|
|
2103
|
-
return this.spettatori.size;
|
|
2104
|
-
}
|
|
2105
|
-
elencoSpettatori() {
|
|
2106
|
-
return [...this.spettatori];
|
|
2107
|
-
}
|
|
2108
1994
|
elencoConnessioni() {
|
|
2109
1995
|
return [...this.connessioni];
|
|
2110
1996
|
}
|
|
2111
|
-
|
|
2112
|
-
|
|
2113
|
-
|
|
2114
|
-
|
|
2115
|
-
const
|
|
2116
|
-
|
|
2117
|
-
|
|
2118
|
-
|
|
2119
|
-
|
|
2120
|
-
}
|
|
2121
|
-
}
|
|
2122
|
-
}
|
|
2123
|
-
inviaSpettatore(connessione, testo) {
|
|
2124
|
-
this.accodaSpettatore(connessione, (socket) => {
|
|
2125
|
-
socket.send(testo);
|
|
2126
|
-
});
|
|
2127
|
-
}
|
|
2128
|
-
inviaSpettatoreSubito(connessione, testo) {
|
|
2129
|
-
this.spettatori.get(connessione)?.send(testo);
|
|
2130
|
-
}
|
|
2131
|
-
chiudiSpettatore(connessione, codice, motivo) {
|
|
2132
|
-
const socket = this.spettatori.get(connessione);
|
|
2133
|
-
this.spettatori.delete(connessione);
|
|
2134
|
-
socket?.close(codice, motivo);
|
|
1997
|
+
sendMeasured(socket, connection, text) {
|
|
1998
|
+
if (!socket) return;
|
|
1999
|
+
socket.send(text);
|
|
2000
|
+
this.network.record(connection, text);
|
|
2001
|
+
const report = this.network.sample();
|
|
2002
|
+
const warning = networkLevel(report.current);
|
|
2003
|
+
if (warning !== "ok" && warning !== this.networkWarning) process.stderr.write(`${networkMessage(report.current)}
|
|
2004
|
+
`);
|
|
2005
|
+
this.networkWarning = warning;
|
|
2135
2006
|
}
|
|
2136
|
-
|
|
2137
|
-
this.
|
|
2138
|
-
this.spettatori.delete(connessione);
|
|
2139
|
-
socket.close(4004, "room_ended");
|
|
2140
|
-
});
|
|
2141
|
-
}
|
|
2142
|
-
accodaSpettatore(connessione, azione) {
|
|
2143
|
-
if (this.ritardoSpettatori === 0) {
|
|
2144
|
-
const socket = this.spettatori.get(connessione);
|
|
2145
|
-
if (socket !== void 0) azione(socket);
|
|
2146
|
-
return;
|
|
2147
|
-
}
|
|
2148
|
-
const timer = setTimeout(() => {
|
|
2149
|
-
this.timerSpettatori.delete(timer);
|
|
2150
|
-
const socket = this.spettatori.get(connessione);
|
|
2151
|
-
if (socket !== void 0) azione(socket);
|
|
2152
|
-
}, this.ritardoSpettatori ?? RITARDO_SPETTATORI_MS);
|
|
2153
|
-
this.timerSpettatori.add(timer);
|
|
2154
|
-
timer.unref();
|
|
2007
|
+
invia(connessione, messaggio) {
|
|
2008
|
+
this.sendMeasured(this.connessioni.get(connessione), connessione, JSON.stringify(messaggio));
|
|
2155
2009
|
}
|
|
2156
2010
|
chiudi(connessione, codice, motivo) {
|
|
2157
2011
|
this.connessioni.get(connessione)?.close(codice, motivo);
|
|
@@ -2191,8 +2045,6 @@ var AdattatoreNode = class {
|
|
|
2191
2045
|
this.tickGenerazione += 1;
|
|
2192
2046
|
if (this.tickTimer !== null) clearTimeout(this.tickTimer);
|
|
2193
2047
|
if (this.svegliaTimer !== null) clearTimeout(this.svegliaTimer);
|
|
2194
|
-
for (const timer of this.timerSpettatori) clearTimeout(timer);
|
|
2195
|
-
this.timerSpettatori.clear();
|
|
2196
2048
|
this.tickTimer = null;
|
|
2197
2049
|
this.svegliaTimer = null;
|
|
2198
2050
|
}
|
|
@@ -2242,8 +2094,11 @@ var StanzaNode = class {
|
|
|
2242
2094
|
sveglia: () => this.serializza(() => nucleo.sveglia())
|
|
2243
2095
|
});
|
|
2244
2096
|
}
|
|
2245
|
-
|
|
2246
|
-
return this.
|
|
2097
|
+
network() {
|
|
2098
|
+
return this.adattatore.network.sample();
|
|
2099
|
+
}
|
|
2100
|
+
create(roomId, mode, creator, origin = "player") {
|
|
2101
|
+
return this.serializza(() => this.nucleo.crea(roomId, mode, creator, origin));
|
|
2247
2102
|
}
|
|
2248
2103
|
info() {
|
|
2249
2104
|
return this.serializza(() => Promise.resolve(this.nucleo.info()));
|
|
@@ -2251,9 +2106,6 @@ var StanzaNode = class {
|
|
|
2251
2106
|
canJoin(identity) {
|
|
2252
2107
|
return this.serializza(() => Promise.resolve(this.nucleo.puoEntrare(identity)));
|
|
2253
2108
|
}
|
|
2254
|
-
canWatch() {
|
|
2255
|
-
return this.serializza(() => Promise.resolve(this.permessoSpettatore()));
|
|
2256
|
-
}
|
|
2257
2109
|
async connect(socket, identity) {
|
|
2258
2110
|
const connessione = randomUUID();
|
|
2259
2111
|
this.adattatore.aggiungi(connessione, socket);
|
|
@@ -2284,28 +2136,6 @@ var StanzaNode = class {
|
|
|
2284
2136
|
throw cause;
|
|
2285
2137
|
}
|
|
2286
2138
|
}
|
|
2287
|
-
watch(socket, _identity) {
|
|
2288
|
-
return this.serializza(async () => {
|
|
2289
|
-
const permesso = this.permessoSpettatore();
|
|
2290
|
-
if (!permesso.ok) return permesso;
|
|
2291
|
-
const connessione = randomUUID();
|
|
2292
|
-
this.adattatore.aggiungiSpettatore(connessione, socket);
|
|
2293
|
-
socket.on("message", (message) => {
|
|
2294
|
-
void this.serializza(() => this.riceviSpettatore(connessione, message));
|
|
2295
|
-
});
|
|
2296
|
-
socket.on("close", () => {
|
|
2297
|
-
this.adattatore.rimuoviSpettatore(connessione);
|
|
2298
|
-
this.frameFrequenza.delete(connessione);
|
|
2299
|
-
});
|
|
2300
|
-
const fotografia = this.nucleo.fotografia();
|
|
2301
|
-
if (fotografia === null) {
|
|
2302
|
-
this.adattatore.rimuoviSpettatore(connessione);
|
|
2303
|
-
return { ok: false, code: "room_not_found" };
|
|
2304
|
-
}
|
|
2305
|
-
this.inviaWatching(connessione, fotografia);
|
|
2306
|
-
return { ok: true };
|
|
2307
|
-
});
|
|
2308
|
-
}
|
|
2309
2139
|
flush() {
|
|
2310
2140
|
return this.serializza(() => this.nucleo.flush());
|
|
2311
2141
|
}
|
|
@@ -2320,11 +2150,6 @@ var StanzaNode = class {
|
|
|
2320
2150
|
socket.close(1001, "server_shutdown");
|
|
2321
2151
|
await this.serializza(() => this.nucleo.disconnetti(connessione));
|
|
2322
2152
|
}
|
|
2323
|
-
for (const [connessione, socket] of this.adattatore.elencoSpettatori()) {
|
|
2324
|
-
this.adattatore.rimuoviSpettatore(connessione);
|
|
2325
|
-
this.frameFrequenza.delete(connessione);
|
|
2326
|
-
socket.close(1001, "server_shutdown");
|
|
2327
|
-
}
|
|
2328
2153
|
}
|
|
2329
2154
|
serializza(operazione) {
|
|
2330
2155
|
const risultato = this.coda.then(operazione);
|
|
@@ -2368,74 +2193,9 @@ var StanzaNode = class {
|
|
|
2368
2193
|
this.riconciliaRoster();
|
|
2369
2194
|
return;
|
|
2370
2195
|
}
|
|
2371
|
-
await this.riceviVoce(connessione, player.id,
|
|
2372
|
-
}
|
|
2373
|
-
permessoSpettatore() {
|
|
2374
|
-
const info = this.nucleo.info();
|
|
2375
|
-
if (info === null) return { ok: false, code: "room_not_found" };
|
|
2376
|
-
if (info.status === "ended") return { ok: false, code: "room_ended" };
|
|
2377
|
-
if (this.manifest.spectators === null) {
|
|
2378
|
-
return { ok: false, code: "spectators_disabled" };
|
|
2379
|
-
}
|
|
2380
|
-
if (this.adattatore.numeroSpettatori() >= MASSIMO_SPETTATORI) {
|
|
2381
|
-
return { ok: false, code: "spectators_full" };
|
|
2382
|
-
}
|
|
2383
|
-
return { ok: true };
|
|
2384
|
-
}
|
|
2385
|
-
inviaWatching(connessione, fotografia) {
|
|
2386
|
-
this.adattatore.inviaSpettatore(connessione, JSON.stringify({
|
|
2387
|
-
t: "watching",
|
|
2388
|
-
...fotografia,
|
|
2389
|
-
delayMs: this.manifest.spectators?.delayMs ?? RITARDO_SPETTATORI_MS
|
|
2390
|
-
}));
|
|
2391
|
-
}
|
|
2392
|
-
async riceviSpettatore(connessione, frame) {
|
|
2393
|
-
if (Buffer.byteLength(frame, "utf8") > MASSIMO_BYTE_FRAME_STANZA) {
|
|
2394
|
-
this.adattatore.chiudiSpettatore(connessione, 4009, "message_too_large");
|
|
2395
|
-
this.frameFrequenza.delete(connessione);
|
|
2396
|
-
return;
|
|
2397
|
-
}
|
|
2398
|
-
const ora = Date.now();
|
|
2399
|
-
const limite = this.frameFrequenza.controlla(connessione, false, ora);
|
|
2400
|
-
if (!limite.accetta) {
|
|
2401
|
-
if (limite.avvisa) this.adattatore.inviaSpettatoreSubito(connessione, JSON.stringify({
|
|
2402
|
-
t: "error",
|
|
2403
|
-
code: "rate_limited",
|
|
2404
|
-
message: "Too many room messages. Excess messages are dropped."
|
|
2405
|
-
}));
|
|
2406
|
-
if (limite.chiudi) {
|
|
2407
|
-
this.adattatore.chiudiSpettatore(connessione, 4008, "rate_limited");
|
|
2408
|
-
this.frameFrequenza.delete(connessione);
|
|
2409
|
-
}
|
|
2410
|
-
return;
|
|
2411
|
-
}
|
|
2412
|
-
let message = null;
|
|
2413
|
-
try {
|
|
2414
|
-
const value = JSON.parse(frame);
|
|
2415
|
-
message = typeof value === "object" && value !== null && !Array.isArray(value) ? value : null;
|
|
2416
|
-
} catch {
|
|
2417
|
-
}
|
|
2418
|
-
if (message?.t === "ping" && typeof message.c === "number" && Number.isFinite(message.c)) {
|
|
2419
|
-
this.adattatore.inviaSpettatoreSubito(
|
|
2420
|
-
connessione,
|
|
2421
|
-
JSON.stringify({ t: "pong", c: message.c, s: ora })
|
|
2422
|
-
);
|
|
2423
|
-
return;
|
|
2424
|
-
}
|
|
2425
|
-
if (message?.t === "resync") {
|
|
2426
|
-
const fotografia = this.nucleo.fotografia();
|
|
2427
|
-
if (fotografia !== null) this.inviaWatching(connessione, fotografia);
|
|
2428
|
-
return;
|
|
2429
|
-
}
|
|
2430
|
-
if (message?.t === "leave") {
|
|
2431
|
-
this.adattatore.chiudiSpettatore(connessione, 1e3, "left");
|
|
2432
|
-
this.frameFrequenza.delete(connessione);
|
|
2433
|
-
return;
|
|
2434
|
-
}
|
|
2435
|
-
this.adattatore.chiudiSpettatore(connessione, 4009, "bad_message");
|
|
2436
|
-
this.frameFrequenza.delete(connessione);
|
|
2196
|
+
await this.riceviVoce(connessione, player.id, message);
|
|
2437
2197
|
}
|
|
2438
|
-
async riceviVoce(connessione, playerId,
|
|
2198
|
+
async riceviVoce(connessione, playerId, value) {
|
|
2439
2199
|
const richiesta = this.richiestaVoce(value);
|
|
2440
2200
|
if (richiesta === null) {
|
|
2441
2201
|
this.inviaErroreVoce(connessione, value, "invalid_request", "The voice request is invalid.");
|
|
@@ -2483,10 +2243,6 @@ var StanzaNode = class {
|
|
|
2483
2243
|
}
|
|
2484
2244
|
if (richiesta.op === "publish") {
|
|
2485
2245
|
const mic = richiesta.mic !== false;
|
|
2486
|
-
if (role === "spectator" && mic) {
|
|
2487
|
-
this.inviaErroreVoce(connessione, richiesta, "spectator", "Spectators cannot join voice.");
|
|
2488
|
-
return;
|
|
2489
|
-
}
|
|
2490
2246
|
if (mic) {
|
|
2491
2247
|
this.voceListeners.delete(playerId);
|
|
2492
2248
|
this.voceRoster.set(playerId, {
|
|
@@ -2601,7 +2357,7 @@ var StanzaNode = class {
|
|
|
2601
2357
|
let cambiato = false;
|
|
2602
2358
|
for (const [playerId, peer] of this.voceRoster) {
|
|
2603
2359
|
const player = this.nucleo.giocatoreConnesso(peer.connessione);
|
|
2604
|
-
if (player?.id === playerId
|
|
2360
|
+
if (player?.id === playerId) continue;
|
|
2605
2361
|
this.voceRoster.delete(playerId);
|
|
2606
2362
|
cambiato = true;
|
|
2607
2363
|
}
|
|
@@ -2637,10 +2393,9 @@ var StanzaNode = class {
|
|
|
2637
2393
|
}
|
|
2638
2394
|
};
|
|
2639
2395
|
async function createNodeRoom(definition, manifest, options = {}) {
|
|
2640
|
-
if (options.dailyDay !== void 0 && !
|
|
2396
|
+
if (options.dailyDay !== void 0 && !validDay(options.dailyDay)) throw new TypeError("dailyDay must be a real UTC date in YYYY-MM-DD format.");
|
|
2641
2397
|
const storage = await ArchivioNode.apri(options.storageFile ?? null);
|
|
2642
|
-
const
|
|
2643
|
-
const adattatore = new AdattatoreNode(storage, options.deposito ?? null, ritardoSpettatori, options.dailyDay);
|
|
2398
|
+
const adattatore = new AdattatoreNode(storage, options.dailyDay);
|
|
2644
2399
|
const nucleo = await NucleoStanza.apri(definition, manifest, adattatore);
|
|
2645
2400
|
return new StanzaNode(nucleo, adattatore, manifest);
|
|
2646
2401
|
}
|