@caisual/kit 0.21.0 → 0.23.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 +12 -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 +193 -427
- package/dist/runtime.js +241 -269
- 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 +7 -50
- 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 +66 -57
- package/dist/types/node/index.d.ts +5 -25
- package/dist/types/protocollo.d.ts +2 -21
- package/dist/types/runtime/index.d.ts +3 -13
- package/dist/types/runtime/network-probe.d.ts +4 -0
- package/dist/types/server/index.d.ts +1 -11
- package/dist/types/stanza/nucleo.d.ts +8 -16
- package/dist/types/stanza/tipi.d.ts +3 -21
- 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/runtime.js
CHANGED
|
@@ -37,17 +37,12 @@ var RISERVATI = new Set(NOMI_RISERVATI);
|
|
|
37
37
|
function risolviModalita(manifest, mode) {
|
|
38
38
|
const scelta = mode === null ? void 0 : manifest.modes.find((voce) => voce.id === mode);
|
|
39
39
|
if (mode !== null && scelta === void 0) throw new Error("The selected game mode does not exist.");
|
|
40
|
-
return { players: { ...scelta?.players ?? manifest.players }
|
|
40
|
+
return { players: { ...scelta?.players ?? manifest.players } };
|
|
41
41
|
}
|
|
42
42
|
function modalitaLocale(manifest, mode) {
|
|
43
|
-
return
|
|
43
|
+
return risolviModalita(manifest, mode).players.max === 1;
|
|
44
44
|
}
|
|
45
|
-
|
|
46
|
-
// ../contracts/src/replay.ts
|
|
47
|
-
var REPLAY_MAX_BYTES = 10 * 1024 * 1024;
|
|
48
|
-
var REPLAY_MAX_DURATION_MS = 30 * 60 * 1e3;
|
|
49
|
-
var REPLAY_CHUNK_BYTES = 512 * 1024;
|
|
50
|
-
var REPLAY_RETENTION_MS = 30 * 24 * 60 * 60 * 1e3;
|
|
45
|
+
var MODE_LOCAL_MESSAGE = "Single-player modes run in the browser and have no room.";
|
|
51
46
|
|
|
52
47
|
// ../contracts/src/room-limits.ts
|
|
53
48
|
var MASSIMO_BYTE_FRAME_STANZA = 64 * 1024;
|
|
@@ -91,6 +86,80 @@ var LimiteMessaggiStanza = class {
|
|
|
91
86
|
}
|
|
92
87
|
};
|
|
93
88
|
|
|
89
|
+
// ../contracts/src/network-budget.ts
|
|
90
|
+
var NETWORK_BUDGET = Object.freeze({
|
|
91
|
+
recipientBytesPerSecond: 1e5,
|
|
92
|
+
roomBytesPerSecond: 2e6,
|
|
93
|
+
warningRatio: 0.8,
|
|
94
|
+
windowMs: 5e3,
|
|
95
|
+
blockingWindows: 3
|
|
96
|
+
});
|
|
97
|
+
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.`;
|
|
98
|
+
function networkLevel(measurement) {
|
|
99
|
+
const ratio = Math.max(
|
|
100
|
+
measurement.recipientBytesPerSecond / NETWORK_BUDGET.recipientBytesPerSecond,
|
|
101
|
+
measurement.roomBytesPerSecond / NETWORK_BUDGET.roomBytesPerSecond
|
|
102
|
+
);
|
|
103
|
+
return ratio > 1 ? "exceeded" : ratio >= NETWORK_BUDGET.warningRatio ? "warning" : "ok";
|
|
104
|
+
}
|
|
105
|
+
var NetworkMeter = class {
|
|
106
|
+
events = [];
|
|
107
|
+
head = 0;
|
|
108
|
+
recipients = /* @__PURE__ */ new Map();
|
|
109
|
+
total = 0;
|
|
110
|
+
peak = { recipientBytesPerSecond: 0, roomBytesPerSecond: 0 };
|
|
111
|
+
checkAt = null;
|
|
112
|
+
violations = 0;
|
|
113
|
+
blocked = false;
|
|
114
|
+
encoder = new TextEncoder();
|
|
115
|
+
record(recipient, text, now = Date.now()) {
|
|
116
|
+
this.advance(now);
|
|
117
|
+
const bytes = this.encoder.encode(text).byteLength;
|
|
118
|
+
this.events.push({ at: now, recipient, bytes });
|
|
119
|
+
const recipientBytes = (this.recipients.get(recipient) ?? 0) + bytes;
|
|
120
|
+
this.recipients.set(recipient, recipientBytes);
|
|
121
|
+
this.total += bytes;
|
|
122
|
+
this.peak.recipientBytesPerSecond = Math.max(this.peak.recipientBytesPerSecond, this.rate(recipientBytes));
|
|
123
|
+
this.peak.roomBytesPerSecond = Math.max(this.peak.roomBytesPerSecond, this.rate(this.total));
|
|
124
|
+
}
|
|
125
|
+
sample(now = Date.now()) {
|
|
126
|
+
this.advance(now);
|
|
127
|
+
return { current: this.current(), peak: { ...this.peak }, consecutiveViolations: this.violations, blocked: this.blocked };
|
|
128
|
+
}
|
|
129
|
+
rate(bytes) {
|
|
130
|
+
return Math.ceil(bytes * 1e3 / NETWORK_BUDGET.windowMs);
|
|
131
|
+
}
|
|
132
|
+
current() {
|
|
133
|
+
let maximum = 0;
|
|
134
|
+
for (const bytes of this.recipients.values()) maximum = Math.max(maximum, bytes);
|
|
135
|
+
return { recipientBytesPerSecond: this.rate(maximum), roomBytesPerSecond: this.rate(this.total) };
|
|
136
|
+
}
|
|
137
|
+
advance(now) {
|
|
138
|
+
this.checkAt ??= now;
|
|
139
|
+
this.expire(now - NETWORK_BUDGET.windowMs, false);
|
|
140
|
+
if (now - this.checkAt >= NETWORK_BUDGET.windowMs) {
|
|
141
|
+
if (now - this.checkAt >= NETWORK_BUDGET.windowMs * 2) this.violations = 0;
|
|
142
|
+
this.violations = networkLevel(this.current()) === "exceeded" ? this.violations + 1 : 0;
|
|
143
|
+
this.blocked ||= this.violations >= NETWORK_BUDGET.blockingWindows;
|
|
144
|
+
this.checkAt = now;
|
|
145
|
+
}
|
|
146
|
+
this.expire(now - NETWORK_BUDGET.windowMs, true);
|
|
147
|
+
if (this.head > 1024) {
|
|
148
|
+
this.events = this.events.slice(this.head);
|
|
149
|
+
this.head = 0;
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
expire(cutoff, inclusive) {
|
|
153
|
+
while (this.head < this.events.length && (inclusive ? this.events[this.head].at <= cutoff : this.events[this.head].at < cutoff)) {
|
|
154
|
+
const event = this.events[this.head++];
|
|
155
|
+
this.total -= event.bytes;
|
|
156
|
+
const remaining = this.recipients.get(event.recipient) - event.bytes;
|
|
157
|
+
if (remaining === 0) this.recipients.delete(event.recipient);
|
|
158
|
+
else this.recipients.set(event.recipient, remaining);
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
};
|
|
162
|
+
|
|
94
163
|
// src/daily.ts
|
|
95
164
|
function prossimaMezzanotteUtc(ora) {
|
|
96
165
|
return (Math.floor(ora / 864e5) + 1) * 864e5;
|
|
@@ -327,8 +396,6 @@ var PREFISSO_SAVE = "save:";
|
|
|
327
396
|
var LIMITE_FRAME = MASSIMO_BYTE_FRAME_STANZA;
|
|
328
397
|
var LIMITE_STATO = 512 * 1024;
|
|
329
398
|
var LIMITE_SAVE = 256 * 1024;
|
|
330
|
-
var LIMITE_DEPOSITO = 256 * 1024;
|
|
331
|
-
var LIMITE_OPERAZIONI_DEPOSITO = 120;
|
|
332
399
|
var GRAZIA_MS = 6e4;
|
|
333
400
|
var STANZA_VUOTA_MS = 5 * 6e4;
|
|
334
401
|
var ATTESA_RIVINCITA_MS = 2 * 6e4;
|
|
@@ -341,16 +408,7 @@ function scadenzaInattivita(ultimoEvento, durata) {
|
|
|
341
408
|
return Math.ceil((ultimoEvento + durata) / BLOCCO_INATTIVITA_MS) * BLOCCO_INATTIVITA_MS;
|
|
342
409
|
}
|
|
343
410
|
var CHIAVE = /^[a-z0-9][a-z0-9_-]{0,31}$/;
|
|
344
|
-
var PREFISSO_CHIAVE = /^[a-z0-9_-]{0,32}$/;
|
|
345
411
|
var GAME_DEFINITION = /* @__PURE__ */ Symbol.for("@caisual/kit/game-definition");
|
|
346
|
-
var CODICI_DEPOSITO = /* @__PURE__ */ new Set([
|
|
347
|
-
"store_invalid_key",
|
|
348
|
-
"store_too_large",
|
|
349
|
-
"store_full",
|
|
350
|
-
"store_not_integer",
|
|
351
|
-
"store_unavailable",
|
|
352
|
-
"store_rate_limited"
|
|
353
|
-
]);
|
|
354
412
|
function record(value) {
|
|
355
413
|
return typeof value === "object" && value !== null && !Array.isArray(value) ? value : null;
|
|
356
414
|
}
|
|
@@ -388,7 +446,6 @@ var NucleoStanza = class _NucleoStanza {
|
|
|
388
446
|
this.adattatore = adattatore;
|
|
389
447
|
this.dati = null;
|
|
390
448
|
this.frequenza = new LimiteMessaggiStanza();
|
|
391
|
-
this.frequenzaDeposito = [];
|
|
392
449
|
this.kickRichiesti = /* @__PURE__ */ new Set();
|
|
393
450
|
this.voceGuadagniCambiati = /* @__PURE__ */ new Map();
|
|
394
451
|
this.fineRichiesta = null;
|
|
@@ -439,11 +496,6 @@ var NucleoStanza = class _NucleoStanza {
|
|
|
439
496
|
get players() {
|
|
440
497
|
return nucleo.richiediDati().giocatori.map(copiaGiocatore);
|
|
441
498
|
},
|
|
442
|
-
get host() {
|
|
443
|
-
const dati = nucleo.richiediDati();
|
|
444
|
-
const host = dati.giocatori.find((player) => player.id === dati.hostId);
|
|
445
|
-
return host === void 0 ? null : copiaGiocatore(host);
|
|
446
|
-
},
|
|
447
499
|
broadcast(message) {
|
|
448
500
|
nucleo.broadcastCreatore(message);
|
|
449
501
|
},
|
|
@@ -468,23 +520,6 @@ var NucleoStanza = class _NucleoStanza {
|
|
|
468
520
|
load(key) {
|
|
469
521
|
return nucleo.caricaSave(key);
|
|
470
522
|
},
|
|
471
|
-
shared: {
|
|
472
|
-
get(key) {
|
|
473
|
-
return nucleo.leggiDeposito(key);
|
|
474
|
-
},
|
|
475
|
-
set(key, value) {
|
|
476
|
-
return nucleo.scriviDeposito(key, value);
|
|
477
|
-
},
|
|
478
|
-
delete(key) {
|
|
479
|
-
return nucleo.eliminaDeposito(key);
|
|
480
|
-
},
|
|
481
|
-
list(prefix) {
|
|
482
|
-
return nucleo.elencaDeposito(prefix);
|
|
483
|
-
},
|
|
484
|
-
increment(key, amount = 1) {
|
|
485
|
-
return nucleo.incrementaDeposito(key, amount);
|
|
486
|
-
}
|
|
487
|
-
},
|
|
488
523
|
schedule(milliseconds, handler, payload) {
|
|
489
524
|
nucleo.pianifica(milliseconds, handler, payload);
|
|
490
525
|
},
|
|
@@ -514,21 +549,28 @@ var NucleoStanza = class _NucleoStanza {
|
|
|
514
549
|
const nucleo = new _NucleoStanza(definizione, manifest, adattatore);
|
|
515
550
|
const salvato = await adattatore.storage.get(CHIAVE_NUCLEO);
|
|
516
551
|
if (salvato !== void 0) {
|
|
552
|
+
if (modalitaLocale(manifest, salvato.mode)) throw Object.assign(new Error(MODE_LOCAL_MESSAGE), { code: "mode_local" });
|
|
517
553
|
nucleo.dati = salvato;
|
|
518
554
|
nucleo.tickRateSincronizzato = salvato.tickRate;
|
|
519
|
-
|
|
555
|
+
let daAggiornare = salvato.ultimoInputAt === void 0 || salvato.ultimoCambioStatoAt === void 0 || salvato.daily === void 0 || salvato.origin === void 0;
|
|
556
|
+
salvato.origin ??= "player";
|
|
557
|
+
if (salvato.origin === "matchmaking" && salvato.status === "lobby") {
|
|
558
|
+
salvato.status = "waiting";
|
|
559
|
+
daAggiornare = true;
|
|
560
|
+
}
|
|
520
561
|
salvato.daily ??= nucleo.contestoGiornaliero();
|
|
521
562
|
salvato.ultimoInputAt ??= adattatore.ora();
|
|
522
563
|
salvato.ultimoCambioStatoAt ??= salvato.ultimoInputAt;
|
|
523
564
|
nucleo.ultimoStatoOsservato = JSON.stringify(salvato.state);
|
|
524
565
|
await nucleo.riconciliaConnessioni();
|
|
566
|
+
if (salvato.origin === "matchmaking") nucleo.rivalutaAvvio();
|
|
525
567
|
if (daAggiornare) await nucleo.persisti();
|
|
526
568
|
}
|
|
527
569
|
await nucleo.aggiornaProgrammazione();
|
|
528
570
|
return nucleo;
|
|
529
571
|
}
|
|
530
572
|
static verificaManifest(manifest) {
|
|
531
|
-
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 ||
|
|
573
|
+
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 || 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)) {
|
|
532
574
|
throw new TypeError("The room manifest is invalid.");
|
|
533
575
|
}
|
|
534
576
|
}
|
|
@@ -542,30 +584,28 @@ var NucleoStanza = class _NucleoStanza {
|
|
|
542
584
|
get esiste() {
|
|
543
585
|
return this.dati !== null;
|
|
544
586
|
}
|
|
545
|
-
async crea(id, mode, _creator) {
|
|
587
|
+
async crea(id, mode, _creator, origin = "player") {
|
|
546
588
|
if (this.dati !== null) return false;
|
|
547
|
-
|
|
548
|
-
if (modalitaLocale(this.manifest, mode)) throw new Error("Local modes cannot create rooms.");
|
|
589
|
+
if (modalitaLocale(this.manifest, mode)) throw Object.assign(new Error(MODE_LOCAL_MESSAGE), { code: "mode_local" });
|
|
549
590
|
const ora = this.adattatore.ora();
|
|
550
591
|
this.dati = {
|
|
551
592
|
versione: 1,
|
|
593
|
+
origin,
|
|
552
594
|
daily: this.contestoGiornaliero(ora),
|
|
553
595
|
id,
|
|
554
596
|
mode,
|
|
555
|
-
status: "lobby",
|
|
597
|
+
status: origin === "matchmaking" ? "waiting" : "lobby",
|
|
556
598
|
tick: 0,
|
|
557
599
|
tickRate: this.definizione.tickRate,
|
|
558
600
|
ultimoInputAt: ora,
|
|
559
601
|
ultimoCambioStatoAt: ora,
|
|
560
602
|
state: {},
|
|
561
603
|
giocatori: [],
|
|
562
|
-
hostId: null,
|
|
563
604
|
result: null,
|
|
564
605
|
resultAt: null,
|
|
565
606
|
countdownAt: null,
|
|
566
607
|
timer: [],
|
|
567
608
|
prossimoTimerId: 1,
|
|
568
|
-
punteggi: [],
|
|
569
609
|
fineInCoda: null,
|
|
570
610
|
vuotaDa: ora,
|
|
571
611
|
cancellaDopoFlush: false,
|
|
@@ -592,13 +632,13 @@ var NucleoStanza = class _NucleoStanza {
|
|
|
592
632
|
return true;
|
|
593
633
|
}
|
|
594
634
|
get configurazione() {
|
|
595
|
-
|
|
596
|
-
return mode.players.max === 1 ? { ...mode, lobby: false } : mode;
|
|
635
|
+
return risolviModalita(this.manifest, this.dati?.mode ?? null);
|
|
597
636
|
}
|
|
598
637
|
info(playerId) {
|
|
599
638
|
if (this.dati === null) return null;
|
|
600
639
|
return {
|
|
601
640
|
roomId: this.dati.id,
|
|
641
|
+
origin: this.dati.origin,
|
|
602
642
|
status: this.dati.status,
|
|
603
643
|
players: this.manifest.persistent === true ? this.dati.giocatori.length : this.dati.giocatori.filter(
|
|
604
644
|
(player) => player.connected || player.graziaFinoA !== null
|
|
@@ -632,7 +672,7 @@ var NucleoStanza = class _NucleoStanza {
|
|
|
632
672
|
if (ascoltatore === void 0 || speaker === void 0 || listener === speakerId) {
|
|
633
673
|
return { ok: false, reason: "unknown" };
|
|
634
674
|
}
|
|
635
|
-
if (mode === "team" && ascoltatore.
|
|
675
|
+
if (mode === "team" && ascoltatore.team !== speaker.team) {
|
|
636
676
|
return { ok: false, reason: "team" };
|
|
637
677
|
}
|
|
638
678
|
if ((dati.voceGuadagni?.[listener]?.[speakerId] ?? 1) <= 0) {
|
|
@@ -643,15 +683,18 @@ var NucleoStanza = class _NucleoStanza {
|
|
|
643
683
|
}
|
|
644
684
|
puoEntrare(identity) {
|
|
645
685
|
if (this.dati === null) return { ok: false, code: "room_not_found" };
|
|
646
|
-
if (this.dati.status === "ended") return { ok: false, code: "room_ended" };
|
|
686
|
+
if (this.dati.status === "ended" || this.dati.origin === "matchmaking" && this.dati.status === "finished") return { ok: false, code: "room_ended" };
|
|
647
687
|
const esistente = this.dati.giocatori.find((player) => player.id === identity.id);
|
|
648
688
|
if (esistente !== void 0) return { ok: true };
|
|
649
|
-
if (this.
|
|
689
|
+
if (this.dati?.origin === "player" && !["lobby", "countdown", "finished"].includes(this.dati.status)) {
|
|
650
690
|
return { ok: false, code: "room_playing" };
|
|
651
691
|
}
|
|
652
692
|
if (this.dati.giocatori.length >= this.configurazione.players.max) {
|
|
653
693
|
return { ok: false, code: "room_full" };
|
|
654
694
|
}
|
|
695
|
+
if (this.manifest.roles.length > 0 && this.ruoloAutomatico() === null) {
|
|
696
|
+
return { ok: false, code: "room_full" };
|
|
697
|
+
}
|
|
655
698
|
return { ok: true };
|
|
656
699
|
}
|
|
657
700
|
async entra(identity, connessione) {
|
|
@@ -691,11 +734,8 @@ var NucleoStanza = class _NucleoStanza {
|
|
|
691
734
|
player.connessione = connessione;
|
|
692
735
|
player.seq = 0;
|
|
693
736
|
}
|
|
694
|
-
dati.hostId ??= player.id;
|
|
695
737
|
dati.vuotaDa = null;
|
|
696
738
|
dati.ultimoInputAt = ora;
|
|
697
|
-
const primaConnessione = !this.configurazione.lobby && dati.status === "lobby";
|
|
698
|
-
if (primaConnessione) dati.status = "playing";
|
|
699
739
|
if (nuovo) {
|
|
700
740
|
await this.chiama(
|
|
701
741
|
this.definizione.onJoin,
|
|
@@ -706,16 +746,12 @@ var NucleoStanza = class _NucleoStanza {
|
|
|
706
746
|
if (riconnesso) {
|
|
707
747
|
await this.chiama(this.definizione.onConnection, this.room, copiaGiocatore(player), true);
|
|
708
748
|
}
|
|
709
|
-
if (primaConnessione) {
|
|
710
|
-
await this.chiama(this.definizione.onStart, this.room);
|
|
711
|
-
this.iniziaReplay();
|
|
712
|
-
this.inviaStatus(ora);
|
|
713
|
-
}
|
|
714
749
|
await this.concludiEvento();
|
|
715
750
|
if (dati.status !== "ended") {
|
|
716
751
|
this.inviaWelcome(player);
|
|
717
752
|
this.inviaSnapshotTutti();
|
|
718
753
|
this.inviaGiocatori();
|
|
754
|
+
this.rivalutaAvvio();
|
|
719
755
|
}
|
|
720
756
|
await this.persistiEProgramma();
|
|
721
757
|
return { ok: true };
|
|
@@ -729,10 +765,9 @@ var NucleoStanza = class _NucleoStanza {
|
|
|
729
765
|
player.connected = false;
|
|
730
766
|
player.connessione = null;
|
|
731
767
|
player.graziaFinoA = this.adattatore.ora() + GRAZIA_MS;
|
|
732
|
-
if (this.dati.status === "finished" || this.manifest.persistent === true && this.dati.status
|
|
768
|
+
if (this.dati.status === "finished" || this.manifest.persistent === true && ["lobby", "countdown"].includes(this.dati.status)) player.ready = false;
|
|
733
769
|
this.frequenza.delete(connessione);
|
|
734
|
-
|
|
735
|
-
this.verificaCountdown();
|
|
770
|
+
this.rivalutaAvvio();
|
|
736
771
|
await this.chiama(this.definizione.onConnection, this.room, copiaGiocatore(player), false);
|
|
737
772
|
await this.concludiEvento();
|
|
738
773
|
this.inviaGiocatori();
|
|
@@ -789,6 +824,11 @@ var NucleoStanza = class _NucleoStanza {
|
|
|
789
824
|
await this.persistiEProgramma();
|
|
790
825
|
return;
|
|
791
826
|
}
|
|
827
|
+
if (this.dati.origin === "matchmaking" && (message.t === "ready" || message.t === "restart")) return;
|
|
828
|
+
if (this.dati.origin === "matchmaking" && (message.t === "role" || message.t === "team")) {
|
|
829
|
+
this.inviaErrore(player, "matchmaking_room", "Roles and teams are assigned by the server in matchmaking rooms.");
|
|
830
|
+
return;
|
|
831
|
+
}
|
|
792
832
|
if (message.t === "restart") {
|
|
793
833
|
await this.rivincita(player);
|
|
794
834
|
return;
|
|
@@ -799,6 +839,7 @@ var NucleoStanza = class _NucleoStanza {
|
|
|
799
839
|
this.dati.ultimoInputAt = ora;
|
|
800
840
|
player.ready = message.ready;
|
|
801
841
|
this.inviaGiocatori();
|
|
842
|
+
this.rivalutaAvvio();
|
|
802
843
|
await this.persistiEProgramma();
|
|
803
844
|
return;
|
|
804
845
|
}
|
|
@@ -840,12 +881,6 @@ var NucleoStanza = class _NucleoStanza {
|
|
|
840
881
|
await this.persistiEProgramma();
|
|
841
882
|
return;
|
|
842
883
|
}
|
|
843
|
-
if (message.t === "start") {
|
|
844
|
-
if (!this.inLobby(player)) return;
|
|
845
|
-
this.dati.ultimoInputAt = ora;
|
|
846
|
-
await this.avviaCountdown(player);
|
|
847
|
-
return;
|
|
848
|
-
}
|
|
849
884
|
if (message.t === "msg") {
|
|
850
885
|
if (!Number.isSafeInteger(message.seq) || message.seq < 1) {
|
|
851
886
|
await this.messaggioErrato(player);
|
|
@@ -859,11 +894,6 @@ var NucleoStanza = class _NucleoStanza {
|
|
|
859
894
|
return;
|
|
860
895
|
}
|
|
861
896
|
player.seq = seq;
|
|
862
|
-
if (player.role === "spectator") {
|
|
863
|
-
this.inviaErrore(player, "spectator", "Spectators cannot send game input.");
|
|
864
|
-
await this.persistiEProgramma();
|
|
865
|
-
return;
|
|
866
|
-
}
|
|
867
897
|
if (this.dati.status === "finished") return;
|
|
868
898
|
this.dati.ultimoInputAt = ora;
|
|
869
899
|
await this.chiama(
|
|
@@ -919,7 +949,7 @@ var NucleoStanza = class _NucleoStanza {
|
|
|
919
949
|
if (this.dati.status === "countdown" && this.dati.countdownAt !== null && this.dati.countdownAt <= ora) {
|
|
920
950
|
const errore = this.erroreMinimi(this.dati.countdownRivincita);
|
|
921
951
|
if (errore !== null) {
|
|
922
|
-
this.dati.status = "lobby";
|
|
952
|
+
this.dati.status = this.dati.origin === "matchmaking" ? "waiting" : "lobby";
|
|
923
953
|
this.dati.countdownAt = null;
|
|
924
954
|
this.broadcast({ t: "error", code: errore.code, message: errore.message });
|
|
925
955
|
this.inviaStatus(ora);
|
|
@@ -928,7 +958,6 @@ var NucleoStanza = class _NucleoStanza {
|
|
|
928
958
|
this.dati.ultimoInputAt = ora;
|
|
929
959
|
this.dati.countdownAt = null;
|
|
930
960
|
await this.chiama(this.definizione.onStart, this.room);
|
|
931
|
-
this.iniziaReplay();
|
|
932
961
|
this.inviaStatus(ora);
|
|
933
962
|
}
|
|
934
963
|
}
|
|
@@ -957,9 +986,8 @@ var NucleoStanza = class _NucleoStanza {
|
|
|
957
986
|
await this.persistiEProgramma();
|
|
958
987
|
return dati.flushInAttesa;
|
|
959
988
|
}
|
|
960
|
-
if (dati.
|
|
961
|
-
dati.flushInAttesa = { id: crypto.randomUUID(),
|
|
962
|
-
dati.punteggi = [];
|
|
989
|
+
if (dati.fineInCoda === null) return null;
|
|
990
|
+
dati.flushInAttesa = { id: crypto.randomUUID(), ended: dati.fineInCoda };
|
|
963
991
|
dati.fineInCoda = null;
|
|
964
992
|
await this.persistiEProgramma();
|
|
965
993
|
return dati.flushInAttesa;
|
|
@@ -973,10 +1001,8 @@ var NucleoStanza = class _NucleoStanza {
|
|
|
973
1001
|
async flush() {
|
|
974
1002
|
const dati = this.richiediDati();
|
|
975
1003
|
const esito = {
|
|
976
|
-
scores: dati.punteggi.map((score) => ({ ...score })),
|
|
977
1004
|
ended: dati.fineInCoda === null ? null : { result: dati.fineInCoda.result, at: dati.fineInCoda.at }
|
|
978
1005
|
};
|
|
979
|
-
dati.punteggi = [];
|
|
980
1006
|
dati.fineInCoda = null;
|
|
981
1007
|
if (dati.cancellaDopoFlush && dati.status === "ended") {
|
|
982
1008
|
const chiavi = await this.adattatore.storage.list();
|
|
@@ -1001,13 +1027,12 @@ var NucleoStanza = class _NucleoStanza {
|
|
|
1001
1027
|
if (player.connessione !== null) this.frequenza.delete(player.connessione);
|
|
1002
1028
|
player.connessione = null;
|
|
1003
1029
|
player.graziaFinoA = ora + GRAZIA_MS;
|
|
1004
|
-
if (dati.status === "finished" || this.manifest.persistent === true && dati.status
|
|
1030
|
+
if (dati.status === "finished" || this.manifest.persistent === true && ["lobby", "countdown"].includes(dati.status)) player.ready = false;
|
|
1005
1031
|
disconnessi.push(player);
|
|
1006
1032
|
}
|
|
1007
1033
|
}
|
|
1008
1034
|
if (disconnessi.length > 0) {
|
|
1009
|
-
this.
|
|
1010
|
-
this.verificaCountdown();
|
|
1035
|
+
this.rivalutaAvvio();
|
|
1011
1036
|
for (const player of disconnessi) {
|
|
1012
1037
|
await this.chiama(this.definizione.onConnection, this.room, copiaGiocatore(player), false);
|
|
1013
1038
|
}
|
|
@@ -1030,7 +1055,7 @@ var NucleoStanza = class _NucleoStanza {
|
|
|
1030
1055
|
const mancaB = countB < b.min ? 0 : 1;
|
|
1031
1056
|
return mancaA - mancaB || countA - countB;
|
|
1032
1057
|
});
|
|
1033
|
-
return candidati[0]?.id ??
|
|
1058
|
+
return candidati[0]?.id ?? null;
|
|
1034
1059
|
}
|
|
1035
1060
|
squadraAutomatica() {
|
|
1036
1061
|
if (this.manifest.teams === null) return null;
|
|
@@ -1053,39 +1078,37 @@ var NucleoStanza = class _NucleoStanza {
|
|
|
1053
1078
|
}
|
|
1054
1079
|
cambiaRuolo(player, roleId) {
|
|
1055
1080
|
const ruolo = this.manifest.roles.find((item) => item.id === roleId);
|
|
1056
|
-
if (ruolo === void 0
|
|
1081
|
+
if (ruolo === void 0) {
|
|
1057
1082
|
return { code: "invalid_role", message: "This role does not exist." };
|
|
1058
1083
|
}
|
|
1059
1084
|
const occupati = this.richiediDati().giocatori.filter(
|
|
1060
1085
|
(item) => item.id !== player.id && item.role === roleId
|
|
1061
1086
|
).length;
|
|
1062
|
-
if (ruolo
|
|
1087
|
+
if (ruolo.max !== void 0 && occupati >= ruolo.max) {
|
|
1063
1088
|
return { code: "role_full", message: "This role is full." };
|
|
1064
1089
|
}
|
|
1065
1090
|
const ruoloPrecedente = player.role;
|
|
1066
1091
|
const squadraPrecedente = player.team;
|
|
1067
1092
|
player.role = roleId;
|
|
1068
|
-
if (
|
|
1069
|
-
else if (player.team === null) player.team = this.squadraAutomatica();
|
|
1093
|
+
if (player.team === null) player.team = this.squadraAutomatica();
|
|
1070
1094
|
if (this.richiediDati().status === "lobby") player.ready = false;
|
|
1071
1095
|
if (player.role !== ruoloPrecedente || player.team !== squadraPrecedente) {
|
|
1072
1096
|
this.rivediVoce();
|
|
1073
1097
|
}
|
|
1074
1098
|
this.inviaGiocatori();
|
|
1099
|
+
this.rivalutaAvvio();
|
|
1075
1100
|
return null;
|
|
1076
1101
|
}
|
|
1077
1102
|
cambiaSquadra(player, team) {
|
|
1078
1103
|
if (this.manifest.teams === null || !Number.isInteger(team) || team < 1 || team > this.manifest.teams.max) {
|
|
1079
1104
|
return { code: "invalid_team", message: "This team does not exist." };
|
|
1080
1105
|
}
|
|
1081
|
-
if (player.role === "spectator") {
|
|
1082
|
-
return { code: "spectator", message: "Spectators cannot join a team." };
|
|
1083
|
-
}
|
|
1084
1106
|
const squadraPrecedente = player.team;
|
|
1085
1107
|
player.team = team;
|
|
1086
1108
|
if (this.richiediDati().status === "lobby") player.ready = false;
|
|
1087
1109
|
if (player.team !== squadraPrecedente) this.rivediVoce();
|
|
1088
1110
|
this.inviaGiocatori();
|
|
1111
|
+
this.rivalutaAvvio();
|
|
1089
1112
|
return null;
|
|
1090
1113
|
}
|
|
1091
1114
|
impostaRuoloDalServer(playerId, roleId) {
|
|
@@ -1107,21 +1130,21 @@ var NucleoStanza = class _NucleoStanza {
|
|
|
1107
1130
|
}
|
|
1108
1131
|
erroreMinimi(saltaPronti = false) {
|
|
1109
1132
|
const connessi = this.richiediDati().giocatori.filter((player) => player.connected);
|
|
1110
|
-
|
|
1111
|
-
if (attivi.length < this.configurazione.players.min) {
|
|
1133
|
+
if (connessi.length < this.configurazione.players.min) {
|
|
1112
1134
|
return { code: "not_enough_players", message: "The room does not have enough players." };
|
|
1113
1135
|
}
|
|
1136
|
+
if (this.dati?.origin === "matchmaking") return null;
|
|
1114
1137
|
if (!saltaPronti && connessi.some((player) => !player.ready)) {
|
|
1115
1138
|
return { code: "players_not_ready", message: "Every connected player must be ready." };
|
|
1116
1139
|
}
|
|
1117
1140
|
for (const role of this.manifest.roles) {
|
|
1118
|
-
if (
|
|
1141
|
+
if (connessi.filter((player) => player.role === role.id).length < role.min) {
|
|
1119
1142
|
return { code: "role_minimum", message: `Role ${role.id} does not meet its minimum.` };
|
|
1120
1143
|
}
|
|
1121
1144
|
}
|
|
1122
1145
|
if (this.manifest.teams !== null) {
|
|
1123
|
-
const squadre = new Set(
|
|
1124
|
-
if (
|
|
1146
|
+
const squadre = new Set(connessi.map((player) => player.team).filter((team) => team !== null));
|
|
1147
|
+
if (connessi.some((player) => player.team === null) || squadre.size < this.manifest.teams.min) {
|
|
1125
1148
|
return { code: "team_minimum", message: "The room does not have enough teams." };
|
|
1126
1149
|
}
|
|
1127
1150
|
}
|
|
@@ -1133,32 +1156,25 @@ var NucleoStanza = class _NucleoStanza {
|
|
|
1133
1156
|
this.inviaErrore(player, "rematch_unavailable", "This room is not waiting for a rematch.");
|
|
1134
1157
|
return;
|
|
1135
1158
|
}
|
|
1136
|
-
if (
|
|
1137
|
-
|
|
1138
|
-
|
|
1139
|
-
|
|
1140
|
-
|
|
1141
|
-
player.ready = true;
|
|
1142
|
-
this.inviaGiocatori();
|
|
1143
|
-
if (dati.rivincita?.autoStart && this.prontiRivincita()) await this.avviaRivincita();
|
|
1144
|
-
else await this.persistiEProgramma();
|
|
1145
|
-
return;
|
|
1146
|
-
}
|
|
1147
|
-
if (dati.hostId !== player.id) return;
|
|
1148
|
-
if (!this.prontiRivincita(dati.rivincita?.autoStart === true)) {
|
|
1149
|
-
this.inviaErrore(player, "players_not_ready", "Enough connected players must be ready for the rematch.");
|
|
1150
|
-
return;
|
|
1151
|
-
}
|
|
1152
|
-
await this.avviaRivincita();
|
|
1159
|
+
if (player.ready) return;
|
|
1160
|
+
player.ready = true;
|
|
1161
|
+
this.inviaGiocatori();
|
|
1162
|
+
if (this.prontiRivincita()) await this.avviaRivincita();
|
|
1163
|
+
else await this.persistiEProgramma();
|
|
1153
1164
|
}
|
|
1154
|
-
prontiRivincita(
|
|
1155
|
-
const attivi = this.richiediDati().giocatori.filter((item) => item.connected
|
|
1156
|
-
return attivi.filter((item) => item.ready).length >= this.configurazione.players.min &&
|
|
1165
|
+
prontiRivincita() {
|
|
1166
|
+
const attivi = this.richiediDati().giocatori.filter((item) => item.connected);
|
|
1167
|
+
return attivi.filter((item) => item.ready).length >= this.configurazione.players.min && attivi.every((item) => item.ready);
|
|
1157
1168
|
}
|
|
1158
1169
|
async avviaRivincita() {
|
|
1159
|
-
const dati = this.richiediDati()
|
|
1160
|
-
|
|
1161
|
-
|
|
1170
|
+
const dati = this.richiediDati();
|
|
1171
|
+
if (dati.origin === "matchmaking") {
|
|
1172
|
+
for (const player of dati.giocatori.filter((item) => !item.connected)) await this.rimuoviGiocatore(player, "timeout");
|
|
1173
|
+
await this.applicaAzioni();
|
|
1174
|
+
if (this.stanzaTerminata()) return;
|
|
1175
|
+
}
|
|
1176
|
+
const keepSetup = dati.origin === "player" && dati.rivincita?.keepSetup === true;
|
|
1177
|
+
dati.status = dati.origin === "matchmaking" ? "waiting" : keepSetup ? "countdown" : "lobby";
|
|
1162
1178
|
dati.countdownRivincita = keepSetup;
|
|
1163
1179
|
dati.countdownAt = keepSetup ? this.adattatore.ora() + COUNTDOWN_MS : null;
|
|
1164
1180
|
dati.rivincitaFinoA = null;
|
|
@@ -1170,41 +1186,34 @@ var NucleoStanza = class _NucleoStanza {
|
|
|
1170
1186
|
dati.prontiPrecedenti = [];
|
|
1171
1187
|
await this.chiama(this.definizione.onRestart, this.room);
|
|
1172
1188
|
await this.applicaAzioni();
|
|
1173
|
-
if (dati.status === "playing") {
|
|
1174
|
-
await this.chiama(this.definizione.onStart, this.room);
|
|
1175
|
-
this.iniziaReplay();
|
|
1176
|
-
}
|
|
1177
1189
|
await this.concludiEvento();
|
|
1178
|
-
|
|
1190
|
+
this.rivalutaAvvio();
|
|
1191
|
+
if (["waiting", "lobby", "playing", "countdown"].includes(dati.status)) {
|
|
1179
1192
|
this.inviaSnapshotTutti();
|
|
1180
1193
|
this.inviaGiocatori();
|
|
1181
1194
|
this.inviaStatus(this.adattatore.ora());
|
|
1182
1195
|
}
|
|
1183
1196
|
await this.persistiEProgramma();
|
|
1184
1197
|
}
|
|
1185
|
-
|
|
1198
|
+
/**
|
|
1199
|
+
* Nessuno ospita e nessuno preme "inizia": appena tutti i connessi sono pronti e i
|
|
1200
|
+
* minimi di giocatori, ruoli e squadre sono rispettati parte il countdown; se la
|
|
1201
|
+
* condizione cade durante il countdown si torna in lobby.
|
|
1202
|
+
*/
|
|
1203
|
+
rivalutaAvvio() {
|
|
1186
1204
|
const dati = this.richiediDati();
|
|
1187
|
-
if (dati.
|
|
1188
|
-
this.
|
|
1189
|
-
|
|
1190
|
-
|
|
1191
|
-
|
|
1192
|
-
if (errore !== null) {
|
|
1193
|
-
this.inviaErrore(player, errore.code, errore.message);
|
|
1205
|
+
if (dati.status === "countdown") {
|
|
1206
|
+
if (this.erroreMinimi(dati.countdownRivincita) === null) return;
|
|
1207
|
+
dati.status = dati.origin === "matchmaking" ? "waiting" : "lobby";
|
|
1208
|
+
dati.countdownAt = null;
|
|
1209
|
+
this.inviaStatus(this.adattatore.ora());
|
|
1194
1210
|
return;
|
|
1195
1211
|
}
|
|
1212
|
+
if (!["lobby", "waiting"].includes(dati.status) || this.erroreMinimi() !== null) return;
|
|
1196
1213
|
dati.status = "countdown";
|
|
1197
1214
|
dati.countdownRivincita = false;
|
|
1198
1215
|
dati.countdownAt = this.adattatore.ora() + COUNTDOWN_MS;
|
|
1199
1216
|
this.inviaStatus(dati.countdownAt);
|
|
1200
|
-
await this.persistiEProgramma();
|
|
1201
|
-
}
|
|
1202
|
-
verificaCountdown() {
|
|
1203
|
-
const dati = this.richiediDati();
|
|
1204
|
-
if (dati.status !== "countdown" || this.erroreMinimi(dati.countdownRivincita) === null) return;
|
|
1205
|
-
dati.status = "lobby";
|
|
1206
|
-
dati.countdownAt = null;
|
|
1207
|
-
this.inviaStatus(this.adattatore.ora());
|
|
1208
1217
|
}
|
|
1209
1218
|
async messaggioErrato(player) {
|
|
1210
1219
|
await this.chiudiConnessione(player, 4009, "bad_message");
|
|
@@ -1222,7 +1231,6 @@ var NucleoStanza = class _NucleoStanza {
|
|
|
1222
1231
|
if (player.connessione !== null) this.frequenza.delete(player.connessione);
|
|
1223
1232
|
this.rivediVoce();
|
|
1224
1233
|
this.pulisciGuadagni(player.id);
|
|
1225
|
-
if (dati.hostId === player.id) this.assegnaHost();
|
|
1226
1234
|
if (dati.giocatori.length === 0) dati.vuotaDa = this.adattatore.ora();
|
|
1227
1235
|
await this.chiama(
|
|
1228
1236
|
this.definizione.onLeave,
|
|
@@ -1230,12 +1238,7 @@ var NucleoStanza = class _NucleoStanza {
|
|
|
1230
1238
|
copiaGiocatore({ ...player, connected: false, connessione: null }),
|
|
1231
1239
|
reason
|
|
1232
1240
|
);
|
|
1233
|
-
this.
|
|
1234
|
-
}
|
|
1235
|
-
assegnaHost() {
|
|
1236
|
-
const dati = this.richiediDati();
|
|
1237
|
-
const host = dati.giocatori.filter((player) => player.connected).sort((a, b) => a.entratoAt - b.entratoAt)[0];
|
|
1238
|
-
dati.hostId = host?.id ?? null;
|
|
1241
|
+
this.rivalutaAvvio();
|
|
1239
1242
|
}
|
|
1240
1243
|
async eseguiTimer(timer) {
|
|
1241
1244
|
const value = this.definizione[timer.handler];
|
|
@@ -1301,91 +1304,6 @@ var NucleoStanza = class _NucleoStanza {
|
|
|
1301
1304
|
if (!json.ok) throw new Error("The saved value is invalid.");
|
|
1302
1305
|
return json.valore;
|
|
1303
1306
|
}
|
|
1304
|
-
verificaChiaveDeposito(key) {
|
|
1305
|
-
if (typeof key !== "string" || !CHIAVE.test(key)) {
|
|
1306
|
-
throw erroreConCodice(
|
|
1307
|
-
"store_invalid_key",
|
|
1308
|
-
"Shared store keys must use lowercase letters, numbers, underscores, or hyphens."
|
|
1309
|
-
);
|
|
1310
|
-
}
|
|
1311
|
-
}
|
|
1312
|
-
verificaPrefissoDeposito(prefix) {
|
|
1313
|
-
if (prefix !== void 0 && (typeof prefix !== "string" || !PREFISSO_CHIAVE.test(prefix))) {
|
|
1314
|
-
throw erroreConCodice(
|
|
1315
|
-
"store_invalid_key",
|
|
1316
|
-
"Shared store prefixes may contain lowercase letters, numbers, underscores, or hyphens."
|
|
1317
|
-
);
|
|
1318
|
-
}
|
|
1319
|
-
}
|
|
1320
|
-
contaOperazioneDeposito() {
|
|
1321
|
-
const ora = this.adattatore.ora();
|
|
1322
|
-
this.frequenzaDeposito = this.frequenzaDeposito.filter((at) => ora - at < 6e4);
|
|
1323
|
-
if (this.frequenzaDeposito.length >= LIMITE_OPERAZIONI_DEPOSITO) {
|
|
1324
|
-
throw erroreConCodice(
|
|
1325
|
-
"store_rate_limited",
|
|
1326
|
-
"The shared store allows at most 120 operations per minute for each room."
|
|
1327
|
-
);
|
|
1328
|
-
}
|
|
1329
|
-
this.frequenzaDeposito.push(ora);
|
|
1330
|
-
}
|
|
1331
|
-
async usaDeposito(operazione) {
|
|
1332
|
-
this.contaOperazioneDeposito();
|
|
1333
|
-
const deposito = this.adattatore.deposito;
|
|
1334
|
-
if (deposito === null) {
|
|
1335
|
-
throw erroreConCodice("store_unavailable", "The shared store is unavailable.");
|
|
1336
|
-
}
|
|
1337
|
-
try {
|
|
1338
|
-
return await operazione(deposito);
|
|
1339
|
-
} catch (cause) {
|
|
1340
|
-
const code = cause instanceof Error ? cause.code : void 0;
|
|
1341
|
-
if (typeof code === "string" && CODICI_DEPOSITO.has(code)) throw cause;
|
|
1342
|
-
throw erroreConCodice("store_unavailable", "The shared store is unavailable.");
|
|
1343
|
-
}
|
|
1344
|
-
}
|
|
1345
|
-
async leggiDeposito(key) {
|
|
1346
|
-
this.verificaChiaveDeposito(key);
|
|
1347
|
-
const value = await this.usaDeposito((deposito) => deposito.get(key));
|
|
1348
|
-
if (value === null) return null;
|
|
1349
|
-
try {
|
|
1350
|
-
return JSON.parse(JSON.stringify(value));
|
|
1351
|
-
} catch {
|
|
1352
|
-
throw erroreConCodice("store_unavailable", "The shared store returned invalid data.");
|
|
1353
|
-
}
|
|
1354
|
-
}
|
|
1355
|
-
async scriviDeposito(key, value) {
|
|
1356
|
-
this.verificaChiaveDeposito(key);
|
|
1357
|
-
const json = analizzaJson(value);
|
|
1358
|
-
if (!json.ok || json.bytes > LIMITE_DEPOSITO) {
|
|
1359
|
-
throw erroreConCodice(
|
|
1360
|
-
"store_too_large",
|
|
1361
|
-
"Shared store values must be valid JSON of at most 262144 bytes."
|
|
1362
|
-
);
|
|
1363
|
-
}
|
|
1364
|
-
await this.usaDeposito((deposito) => deposito.set(key, json.valore));
|
|
1365
|
-
}
|
|
1366
|
-
async eliminaDeposito(key) {
|
|
1367
|
-
this.verificaChiaveDeposito(key);
|
|
1368
|
-
await this.usaDeposito((deposito) => deposito.delete(key));
|
|
1369
|
-
}
|
|
1370
|
-
async elencaDeposito(prefix) {
|
|
1371
|
-
this.verificaPrefissoDeposito(prefix);
|
|
1372
|
-
const keys = await this.usaDeposito((deposito) => deposito.list(prefix));
|
|
1373
|
-
if (!Array.isArray(keys) || keys.some((key) => typeof key !== "string" || !CHIAVE.test(key))) {
|
|
1374
|
-
throw erroreConCodice("store_unavailable", "The shared store returned invalid keys.");
|
|
1375
|
-
}
|
|
1376
|
-
return [...keys].sort().slice(0, 4096);
|
|
1377
|
-
}
|
|
1378
|
-
async incrementaDeposito(key, amount) {
|
|
1379
|
-
this.verificaChiaveDeposito(key);
|
|
1380
|
-
if (!Number.isSafeInteger(amount)) {
|
|
1381
|
-
throw erroreConCodice("store_not_integer", "Shared store increments must be safe integers.");
|
|
1382
|
-
}
|
|
1383
|
-
const value = await this.usaDeposito((deposito) => deposito.increment(key, amount));
|
|
1384
|
-
if (!Number.isSafeInteger(value)) {
|
|
1385
|
-
throw erroreConCodice("store_unavailable", "The shared store returned an invalid integer.");
|
|
1386
|
-
}
|
|
1387
|
-
return value;
|
|
1388
|
-
}
|
|
1389
1307
|
messaggioCreatore(message) {
|
|
1390
1308
|
const json = analizzaJson(message);
|
|
1391
1309
|
if (!json.ok) throw new TypeError("Messages must be valid JSON.");
|
|
@@ -1412,7 +1330,6 @@ var NucleoStanza = class _NucleoStanza {
|
|
|
1412
1330
|
this.adattatore.invia(player.connessione, message);
|
|
1413
1331
|
}
|
|
1414
1332
|
}
|
|
1415
|
-
if (message.t !== "flush") this.adattatore.pubblica(message);
|
|
1416
1333
|
}
|
|
1417
1334
|
inviaErrore(player, code, message) {
|
|
1418
1335
|
if (player.connected && player.connessione !== null) {
|
|
@@ -1449,6 +1366,7 @@ var NucleoStanza = class _NucleoStanza {
|
|
|
1449
1366
|
const dati = this.richiediDati();
|
|
1450
1367
|
return {
|
|
1451
1368
|
id: dati.id,
|
|
1369
|
+
origin: dati.origin,
|
|
1452
1370
|
seed: seedStanza(dati.id),
|
|
1453
1371
|
status: dati.status,
|
|
1454
1372
|
result: dati.result,
|
|
@@ -1457,18 +1375,16 @@ var NucleoStanza = class _NucleoStanza {
|
|
|
1457
1375
|
tick: dati.tick,
|
|
1458
1376
|
tickRate: dati.tickRate,
|
|
1459
1377
|
serverTime: this.adattatore.ora(),
|
|
1460
|
-
host: dati.hostId,
|
|
1461
1378
|
countdownAt: dati.countdownAt,
|
|
1462
1379
|
configuration: {
|
|
1463
1380
|
players: { ...this.configurazione.players },
|
|
1464
|
-
lobby: this.configurazione.lobby,
|
|
1465
1381
|
persistent: this.manifest.persistent === true,
|
|
1466
1382
|
requestRole: this.definizione.onRoleRequest !== void 0
|
|
1467
1383
|
}
|
|
1468
1384
|
};
|
|
1469
1385
|
}
|
|
1470
1386
|
inviaGiocatori() {
|
|
1471
|
-
this.broadcast({ t: "players", players: this.giocatoriProtocollo()
|
|
1387
|
+
this.broadcast({ t: "players", players: this.giocatoriProtocollo() });
|
|
1472
1388
|
}
|
|
1473
1389
|
inviaStatus(at) {
|
|
1474
1390
|
const dati = this.richiediDati();
|
|
@@ -1476,7 +1392,6 @@ var NucleoStanza = class _NucleoStanza {
|
|
|
1476
1392
|
t: "status",
|
|
1477
1393
|
rematch: dati.status === "finished" ? dati.rivincita ?? null : null,
|
|
1478
1394
|
status: dati.status,
|
|
1479
|
-
host: dati.hostId,
|
|
1480
1395
|
countdownAt: dati.countdownAt,
|
|
1481
1396
|
at,
|
|
1482
1397
|
result: dati.status === "ended" || dati.status === "finished" ? dati.result : null
|
|
@@ -1498,13 +1413,6 @@ var NucleoStanza = class _NucleoStanza {
|
|
|
1498
1413
|
dati.statoSincronizzato = copiaJson(dati.state);
|
|
1499
1414
|
dati.tickSincronizzato = dati.tick;
|
|
1500
1415
|
}
|
|
1501
|
-
iniziaReplay() {
|
|
1502
|
-
if (!this.manifest.replays || !this.adattatore.inizioReplay) return;
|
|
1503
|
-
const json = analizzaJson(this.richiediDati().state);
|
|
1504
|
-
if (!json.ok || json.bytes > LIMITE_STATO) return;
|
|
1505
|
-
this.inviaSnapshotTutti();
|
|
1506
|
-
this.adattatore.inizioReplay(this.fotografia());
|
|
1507
|
-
}
|
|
1508
1416
|
inviaSnapshotTutti() {
|
|
1509
1417
|
const dati = this.richiediDati();
|
|
1510
1418
|
const stato = analizzaJson(dati.state);
|
|
@@ -1554,7 +1462,7 @@ var NucleoStanza = class _NucleoStanza {
|
|
|
1554
1462
|
async concludiEvento() {
|
|
1555
1463
|
await this.applicaAzioni();
|
|
1556
1464
|
if (this.dati === null || this.dati.status === "ended") return;
|
|
1557
|
-
if (this.dati.status === "finished" && this.dati.rivincita
|
|
1465
|
+
if (this.dati.origin === "player" && this.dati.status === "finished" && this.dati.rivincita !== null && this.prontiRivincita()) {
|
|
1558
1466
|
await this.avviaRivincita();
|
|
1559
1467
|
return;
|
|
1560
1468
|
}
|
|
@@ -1652,13 +1560,11 @@ var NucleoStanza = class _NucleoStanza {
|
|
|
1652
1560
|
const dati = this.richiediDati();
|
|
1653
1561
|
if (dati.status === "ended") return;
|
|
1654
1562
|
const giaFinita = dati.status === "finished";
|
|
1563
|
+
if (dati.origin === "matchmaking" && record(result)?.error === void 0) rematch = true;
|
|
1655
1564
|
dati.status = rematch ? "finished" : "ended";
|
|
1656
|
-
dati.rivincita = rematch ? {
|
|
1657
|
-
keepSetup: typeof rematch === "object" && rematch.keepSetup === true,
|
|
1658
|
-
autoStart: typeof rematch === "object" && rematch.autoStart === true
|
|
1659
|
-
} : null;
|
|
1565
|
+
dati.rivincita = rematch ? { keepSetup: typeof rematch === "object" && rematch.keepSetup === true } : null;
|
|
1660
1566
|
dati.prontiPrecedenti = rematch ? dati.giocatori.filter((p) => p.ready).map((p) => p.id) : [];
|
|
1661
|
-
dati.rivincitaFinoA = rematch ? this.adattatore.ora() + ATTESA_RIVINCITA_MS : null;
|
|
1567
|
+
dati.rivincitaFinoA = rematch ? this.adattatore.ora() + (dati.origin === "matchmaking" ? 8e3 : ATTESA_RIVINCITA_MS) : null;
|
|
1662
1568
|
dati.timer = [];
|
|
1663
1569
|
if (rematch) for (const player of dati.giocatori) player.ready = false;
|
|
1664
1570
|
dati.countdownAt = null;
|
|
@@ -1736,6 +1642,10 @@ var NucleoStanza = class _NucleoStanza {
|
|
|
1736
1642
|
async terminaSeInattiva() {
|
|
1737
1643
|
if (this.dati?.status === "finished") {
|
|
1738
1644
|
if (this.adattatore.ora() < (this.dati.rivincitaFinoA ?? 0)) return false;
|
|
1645
|
+
if (this.dati.origin === "matchmaking") {
|
|
1646
|
+
await this.avviaRivincita();
|
|
1647
|
+
return false;
|
|
1648
|
+
}
|
|
1739
1649
|
await this.terminaInterna(this.dati.result);
|
|
1740
1650
|
return true;
|
|
1741
1651
|
}
|
|
@@ -1781,6 +1691,78 @@ var NucleoStanza = class _NucleoStanza {
|
|
|
1781
1691
|
}
|
|
1782
1692
|
};
|
|
1783
1693
|
|
|
1694
|
+
// src/runtime/network-probe.ts
|
|
1695
|
+
async function measureNetwork(definition, manifest) {
|
|
1696
|
+
const modes = (manifest.modes.length ? manifest.modes : [{ id: null }]).filter((mode) => !modalitaLocale(manifest, mode.id));
|
|
1697
|
+
const results = [];
|
|
1698
|
+
for (const mode of modes) {
|
|
1699
|
+
const storage = /* @__PURE__ */ new Map();
|
|
1700
|
+
const connections = /* @__PURE__ */ new Set();
|
|
1701
|
+
const meter = new NetworkMeter();
|
|
1702
|
+
let now = Date.now();
|
|
1703
|
+
let interval = definition.tickRate > 0 ? 1e3 / definition.tickRate : 50;
|
|
1704
|
+
let error = null;
|
|
1705
|
+
let playing = false;
|
|
1706
|
+
const adapter = {
|
|
1707
|
+
storage: {
|
|
1708
|
+
async get(key) {
|
|
1709
|
+
return structuredClone(storage.get(key));
|
|
1710
|
+
},
|
|
1711
|
+
async put(key, value) {
|
|
1712
|
+
storage.set(key, structuredClone(value));
|
|
1713
|
+
},
|
|
1714
|
+
async delete(key) {
|
|
1715
|
+
storage.delete(key);
|
|
1716
|
+
},
|
|
1717
|
+
async list(prefix = "") {
|
|
1718
|
+
return new Map([...storage].filter(([key]) => key.startsWith(prefix)));
|
|
1719
|
+
}
|
|
1720
|
+
},
|
|
1721
|
+
invia(connection, message) {
|
|
1722
|
+
if (connections.has(connection)) meter.record(connection, JSON.stringify(message), now);
|
|
1723
|
+
if (message.t === "status" && message.status === "playing") playing = true;
|
|
1724
|
+
if (message.t === "error" || message.t === "status" && message.result !== null && typeof message.result === "object" && !Array.isArray(message.result) && ["callback_error", "state_too_large", "state_invalid", "cpu_budget"].includes(String(message.result.error))) error = "The server reported an error during the publication probe.";
|
|
1725
|
+
},
|
|
1726
|
+
chiudi(connection) {
|
|
1727
|
+
connections.delete(connection);
|
|
1728
|
+
},
|
|
1729
|
+
connessioniAttive: () => [...connections],
|
|
1730
|
+
ora: () => now,
|
|
1731
|
+
misuraCpu: () => performance.now(),
|
|
1732
|
+
rivediVoce() {
|
|
1733
|
+
},
|
|
1734
|
+
programmaTick(ms) {
|
|
1735
|
+
if (ms !== null) interval = ms;
|
|
1736
|
+
},
|
|
1737
|
+
async programmaSveglia() {
|
|
1738
|
+
}
|
|
1739
|
+
};
|
|
1740
|
+
const room = await NucleoStanza.apri(definition, manifest, adapter);
|
|
1741
|
+
const identity = (n) => ({ id: `probe-${n}`, name: `Player ${n + 1}`, guest: true });
|
|
1742
|
+
await room.crea("publication-probe", mode.id, identity(0), "player");
|
|
1743
|
+
const maximum = ("players" in mode ? mode.players?.max : void 0) ?? manifest.players.max;
|
|
1744
|
+
for (let n = 0; n < maximum; n++) {
|
|
1745
|
+
connections.add(`probe-${n}`);
|
|
1746
|
+
const joined = await room.entra(identity(n), `probe-${n}`);
|
|
1747
|
+
if (!joined.ok) throw new Error("The publication probe could not fill the room.");
|
|
1748
|
+
}
|
|
1749
|
+
if (room.info()?.status === "lobby") {
|
|
1750
|
+
for (const connection of connections) await room.ricevi(connection, JSON.stringify({ t: "ready", ready: true }));
|
|
1751
|
+
}
|
|
1752
|
+
const start = now;
|
|
1753
|
+
while (now - start < 2e4) {
|
|
1754
|
+
now += Math.min(interval, 2e4 - (now - start));
|
|
1755
|
+
await room.sveglia();
|
|
1756
|
+
playing ||= room.info()?.status === "playing";
|
|
1757
|
+
await room.eseguiTick();
|
|
1758
|
+
if (error !== null) throw new Error(error);
|
|
1759
|
+
}
|
|
1760
|
+
if (!playing) throw new Error("The publication probe could not start this room mode. Check lobby roles and teams.");
|
|
1761
|
+
results.push({ mode: mode.id, players: maximum, playing, simulatedSeconds: 20, ...meter.sample(now).peak });
|
|
1762
|
+
}
|
|
1763
|
+
return { modes: results };
|
|
1764
|
+
}
|
|
1765
|
+
|
|
1784
1766
|
// src/runtime/index.ts
|
|
1785
1767
|
var CHIAVE_ADATTATORE = "adattatore";
|
|
1786
1768
|
var ArchivioFacet = class {
|
|
@@ -1801,11 +1783,10 @@ var ArchivioFacet = class {
|
|
|
1801
1783
|
}
|
|
1802
1784
|
};
|
|
1803
1785
|
var AdattatoreFacet = class {
|
|
1804
|
-
constructor(archivio, tickRate
|
|
1786
|
+
constructor(archivio, tickRate) {
|
|
1805
1787
|
this.connessioni = /* @__PURE__ */ new Set();
|
|
1806
1788
|
this.cpuMessaggiOltreCento = 0;
|
|
1807
1789
|
this.frames = [];
|
|
1808
|
-
this.pubblici = [];
|
|
1809
1790
|
this.chiusure = [];
|
|
1810
1791
|
this.sveglia = null;
|
|
1811
1792
|
this.tick = false;
|
|
@@ -1813,7 +1794,6 @@ var AdattatoreFacet = class {
|
|
|
1813
1794
|
this.salvato = "";
|
|
1814
1795
|
this.storage = new ArchivioFacet(archivio);
|
|
1815
1796
|
this.tickRate = tickRate;
|
|
1816
|
-
this.deposito = deposito;
|
|
1817
1797
|
}
|
|
1818
1798
|
async ripristina() {
|
|
1819
1799
|
const dati = await this.storage.get(CHIAVE_ADATTATORE);
|
|
@@ -1837,12 +1817,6 @@ var AdattatoreFacet = class {
|
|
|
1837
1817
|
invia(connessione, messaggio) {
|
|
1838
1818
|
this.frames.push({ connessione, testo: JSON.stringify(messaggio) });
|
|
1839
1819
|
}
|
|
1840
|
-
inizioReplay(fotografia) {
|
|
1841
|
-
this.pubblici.push(JSON.stringify({ t: "replay-start", ...fotografia }));
|
|
1842
|
-
}
|
|
1843
|
-
pubblica(messaggio) {
|
|
1844
|
-
this.pubblici.push(JSON.stringify(messaggio));
|
|
1845
|
-
}
|
|
1846
1820
|
chiudi(connessione, codice, motivo) {
|
|
1847
1821
|
this.chiusure.push({ connessione, codice, motivo });
|
|
1848
1822
|
this.connessioni.delete(connessione);
|
|
@@ -1869,7 +1843,6 @@ var AdattatoreFacet = class {
|
|
|
1869
1843
|
esito(status) {
|
|
1870
1844
|
const risultato = {
|
|
1871
1845
|
frames: this.frames,
|
|
1872
|
-
pubblici: this.pubblici,
|
|
1873
1846
|
chiusure: this.chiusure,
|
|
1874
1847
|
sveglia: this.sveglia,
|
|
1875
1848
|
tick: this.tick,
|
|
@@ -1878,7 +1851,6 @@ var AdattatoreFacet = class {
|
|
|
1878
1851
|
voceRivedi: this.voceRivedi
|
|
1879
1852
|
};
|
|
1880
1853
|
this.frames = [];
|
|
1881
|
-
this.pubblici = [];
|
|
1882
1854
|
this.chiusure = [];
|
|
1883
1855
|
this.voceRivedi = false;
|
|
1884
1856
|
return risultato;
|
|
@@ -1893,8 +1865,7 @@ function creaFacetStanza(DurableObject, definizione, manifest) {
|
|
|
1893
1865
|
constructor(stato, ambiente) {
|
|
1894
1866
|
super(stato, ambiente);
|
|
1895
1867
|
this.#roomId = typeof stato.id === "string" ? stato.id : stato.id.name ?? stato.id.toString();
|
|
1896
|
-
const
|
|
1897
|
-
const adattatore = new AdattatoreFacet(stato.storage, definizione.tickRate, deposito);
|
|
1868
|
+
const adattatore = new AdattatoreFacet(stato.storage, definizione.tickRate);
|
|
1898
1869
|
this.#adattatore = adattatore;
|
|
1899
1870
|
const misurata = Object.create(definizione);
|
|
1900
1871
|
if (definizione.onMessage !== void 0) {
|
|
@@ -1929,7 +1900,7 @@ function creaFacetStanza(DurableObject, definizione, manifest) {
|
|
|
1929
1900
|
}
|
|
1930
1901
|
crea(input) {
|
|
1931
1902
|
return this.#serializza(async (nucleo) => {
|
|
1932
|
-
const ok = await nucleo.crea(this.#roomId, input.mode, input.creator);
|
|
1903
|
+
const ok = await nucleo.crea(this.#roomId, input.mode, input.creator, input.origin);
|
|
1933
1904
|
await this.#adattatore.persisti();
|
|
1934
1905
|
return {
|
|
1935
1906
|
...this.#adattatore.esito(nucleo.info()?.status ?? null),
|
|
@@ -1994,5 +1965,6 @@ function creaFacetStanza(DurableObject, definizione, manifest) {
|
|
|
1994
1965
|
};
|
|
1995
1966
|
}
|
|
1996
1967
|
export {
|
|
1997
|
-
creaFacetStanza
|
|
1968
|
+
creaFacetStanza,
|
|
1969
|
+
measureNetwork
|
|
1998
1970
|
};
|