@caisual/kit 0.20.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 +182 -457
- package/dist/runtime.js +236 -299
- package/dist/types/api.d.ts +2 -9
- 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 -3
- package/dist/types/contracts/manifest.d.ts +6 -40
- 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 -38
- package/dist/types/index.d.ts +64 -90
- package/dist/types/node/index.d.ts +6 -27
- package/dist/types/protocollo.d.ts +1 -20
- package/dist/types/runtime/index.d.ts +8 -15
- package/dist/types/runtime/network-probe.d.ts +4 -0
- package/dist/types/server/index.d.ts +0 -20
- package/dist/types/stanza/nucleo.d.ts +12 -17
- package/dist/types/stanza/tipi.d.ts +7 -23
- package/dist/types/voce/index.d.ts +0 -1
- package/package.json +16 -19
- package/dist/overlay.d.ts +0 -1
- package/dist/overlay.js +0 -2326
- package/dist/overlay.js.map +0 -7
- package/dist/types/contracts/overlay-boards.d.ts +0 -3
- package/dist/types/contracts/overlay.d.ts +0 -240
- package/dist/types/contracts/replay.d.ts +0 -83
- package/dist/types/overlay/boards.d.ts +0 -48
- package/dist/types/overlay/bridge.d.ts +0 -3
- package/dist/types/overlay/i18n.d.ts +0 -146
- package/dist/types/overlay/index.d.ts +0 -10
- 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 -83
- 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 -50
- package/dist/types/session/resume.d.ts +0 -13
package/dist/runtime.js
CHANGED
|
@@ -40,14 +40,9 @@ 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
|
-
|
|
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;
|
|
@@ -163,10 +232,10 @@ function visitaDiff(prima, dopo, path, patch) {
|
|
|
163
232
|
}
|
|
164
233
|
if (isObject(prima) && isObject(dopo)) {
|
|
165
234
|
for (const key of Object.keys(prima)) {
|
|
166
|
-
if (!(key
|
|
235
|
+
if (!Object.hasOwn(dopo, key)) patch.push({ op: "del", path: [...path, key] });
|
|
167
236
|
}
|
|
168
237
|
for (const [key, value] of Object.entries(dopo)) {
|
|
169
|
-
if (!(key
|
|
238
|
+
if (!Object.hasOwn(prima, key)) patch.push({ op: "set", path: [...path, key], value });
|
|
170
239
|
else visitaDiff(prima[key], value, [...path, key], patch);
|
|
171
240
|
}
|
|
172
241
|
return;
|
|
@@ -327,13 +396,10 @@ 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;
|
|
335
402
|
var COUNTDOWN_MS = 3e3;
|
|
336
|
-
var GRAZIA_GIORNALIERA_MS = 10 * 6e4;
|
|
337
403
|
var RIPOSO_TICK_MS = 3e4;
|
|
338
404
|
var INATTIVITA_MS = 10 * 6e4;
|
|
339
405
|
var SCADENZA_PERSISTENTE_MS = 30 * 24 * 60 * 6e4;
|
|
@@ -342,16 +408,7 @@ function scadenzaInattivita(ultimoEvento, durata) {
|
|
|
342
408
|
return Math.ceil((ultimoEvento + durata) / BLOCCO_INATTIVITA_MS) * BLOCCO_INATTIVITA_MS;
|
|
343
409
|
}
|
|
344
410
|
var CHIAVE = /^[a-z0-9][a-z0-9_-]{0,31}$/;
|
|
345
|
-
var PREFISSO_CHIAVE = /^[a-z0-9_-]{0,32}$/;
|
|
346
411
|
var GAME_DEFINITION = /* @__PURE__ */ Symbol.for("@caisual/kit/game-definition");
|
|
347
|
-
var CODICI_DEPOSITO = /* @__PURE__ */ new Set([
|
|
348
|
-
"store_invalid_key",
|
|
349
|
-
"store_too_large",
|
|
350
|
-
"store_full",
|
|
351
|
-
"store_not_integer",
|
|
352
|
-
"store_unavailable",
|
|
353
|
-
"store_rate_limited"
|
|
354
|
-
]);
|
|
355
412
|
function record(value) {
|
|
356
413
|
return typeof value === "object" && value !== null && !Array.isArray(value) ? value : null;
|
|
357
414
|
}
|
|
@@ -389,7 +446,6 @@ var NucleoStanza = class _NucleoStanza {
|
|
|
389
446
|
this.adattatore = adattatore;
|
|
390
447
|
this.dati = null;
|
|
391
448
|
this.frequenza = new LimiteMessaggiStanza();
|
|
392
|
-
this.frequenzaDeposito = [];
|
|
393
449
|
this.kickRichiesti = /* @__PURE__ */ new Set();
|
|
394
450
|
this.voceGuadagniCambiati = /* @__PURE__ */ new Map();
|
|
395
451
|
this.fineRichiesta = null;
|
|
@@ -440,11 +496,6 @@ var NucleoStanza = class _NucleoStanza {
|
|
|
440
496
|
get players() {
|
|
441
497
|
return nucleo.richiediDati().giocatori.map(copiaGiocatore);
|
|
442
498
|
},
|
|
443
|
-
get host() {
|
|
444
|
-
const dati = nucleo.richiediDati();
|
|
445
|
-
const host = dati.giocatori.find((player) => player.id === dati.hostId);
|
|
446
|
-
return host === void 0 ? null : copiaGiocatore(host);
|
|
447
|
-
},
|
|
448
499
|
broadcast(message) {
|
|
449
500
|
nucleo.broadcastCreatore(message);
|
|
450
501
|
},
|
|
@@ -469,35 +520,9 @@ var NucleoStanza = class _NucleoStanza {
|
|
|
469
520
|
load(key) {
|
|
470
521
|
return nucleo.caricaSave(key);
|
|
471
522
|
},
|
|
472
|
-
shared: {
|
|
473
|
-
get(key) {
|
|
474
|
-
return nucleo.leggiDeposito(key);
|
|
475
|
-
},
|
|
476
|
-
set(key, value) {
|
|
477
|
-
return nucleo.scriviDeposito(key, value);
|
|
478
|
-
},
|
|
479
|
-
delete(key) {
|
|
480
|
-
return nucleo.eliminaDeposito(key);
|
|
481
|
-
},
|
|
482
|
-
list(prefix) {
|
|
483
|
-
return nucleo.elencaDeposito(prefix);
|
|
484
|
-
},
|
|
485
|
-
increment(key, amount = 1) {
|
|
486
|
-
return nucleo.incrementaDeposito(key, amount);
|
|
487
|
-
}
|
|
488
|
-
},
|
|
489
523
|
schedule(milliseconds, handler, payload) {
|
|
490
524
|
nucleo.pianifica(milliseconds, handler, payload);
|
|
491
525
|
},
|
|
492
|
-
board: {
|
|
493
|
-
submit(player, board, score, options = {}) {
|
|
494
|
-
if (typeof board === "number") {
|
|
495
|
-
const active = nucleo.richiediDati().giocatori.filter((p) => p.role !== "spectator");
|
|
496
|
-
if (nucleo.configurazione.players.max !== 1 || active.length !== 1) throw erroreConCodice("board_player_required", "Specify the player when submitting a multiplayer score.");
|
|
497
|
-
nucleo.accodaPunteggio(active[0].id, player, board, typeof score === "object" ? score : {});
|
|
498
|
-
} else nucleo.accodaPunteggio(idGiocatore(player), board, score, options);
|
|
499
|
-
}
|
|
500
|
-
},
|
|
501
526
|
daily,
|
|
502
527
|
time: { now: () => nucleo.adattatore.ora() },
|
|
503
528
|
voice: {
|
|
@@ -524,9 +549,11 @@ var NucleoStanza = class _NucleoStanza {
|
|
|
524
549
|
const nucleo = new _NucleoStanza(definizione, manifest, adattatore);
|
|
525
550
|
const salvato = await adattatore.storage.get(CHIAVE_NUCLEO);
|
|
526
551
|
if (salvato !== void 0) {
|
|
552
|
+
if (modalitaLocale(manifest, salvato.mode)) throw Object.assign(new Error(MODE_LOCAL_MESSAGE), { code: "mode_local" });
|
|
527
553
|
nucleo.dati = salvato;
|
|
528
554
|
nucleo.tickRateSincronizzato = salvato.tickRate;
|
|
529
|
-
const daAggiornare = salvato.ultimoInputAt === void 0 || salvato.ultimoCambioStatoAt === void 0 || salvato.daily === void 0;
|
|
555
|
+
const daAggiornare = salvato.ultimoInputAt === void 0 || salvato.ultimoCambioStatoAt === void 0 || salvato.daily === void 0 || salvato.origin === void 0;
|
|
556
|
+
salvato.origin ??= "player";
|
|
530
557
|
salvato.daily ??= nucleo.contestoGiornaliero();
|
|
531
558
|
salvato.ultimoInputAt ??= adattatore.ora();
|
|
532
559
|
salvato.ultimoCambioStatoAt ??= salvato.ultimoInputAt;
|
|
@@ -552,13 +579,13 @@ var NucleoStanza = class _NucleoStanza {
|
|
|
552
579
|
get esiste() {
|
|
553
580
|
return this.dati !== null;
|
|
554
581
|
}
|
|
555
|
-
async crea(id, mode, _creator) {
|
|
582
|
+
async crea(id, mode, _creator, origin = "player") {
|
|
556
583
|
if (this.dati !== null) return false;
|
|
557
|
-
|
|
558
|
-
if (modalitaLocale(this.manifest, mode)) throw new Error("Local modes cannot create rooms.");
|
|
584
|
+
if (modalitaLocale(this.manifest, mode)) throw Object.assign(new Error(MODE_LOCAL_MESSAGE), { code: "mode_local" });
|
|
559
585
|
const ora = this.adattatore.ora();
|
|
560
586
|
this.dati = {
|
|
561
587
|
versione: 1,
|
|
588
|
+
origin,
|
|
562
589
|
daily: this.contestoGiornaliero(ora),
|
|
563
590
|
id,
|
|
564
591
|
mode,
|
|
@@ -569,13 +596,11 @@ var NucleoStanza = class _NucleoStanza {
|
|
|
569
596
|
ultimoCambioStatoAt: ora,
|
|
570
597
|
state: {},
|
|
571
598
|
giocatori: [],
|
|
572
|
-
hostId: null,
|
|
573
599
|
result: null,
|
|
574
600
|
resultAt: null,
|
|
575
601
|
countdownAt: null,
|
|
576
602
|
timer: [],
|
|
577
603
|
prossimoTimerId: 1,
|
|
578
|
-
punteggi: [],
|
|
579
604
|
fineInCoda: null,
|
|
580
605
|
vuotaDa: ora,
|
|
581
606
|
cancellaDopoFlush: false,
|
|
@@ -603,12 +628,14 @@ var NucleoStanza = class _NucleoStanza {
|
|
|
603
628
|
}
|
|
604
629
|
get configurazione() {
|
|
605
630
|
const mode = risolviModalita(this.manifest, this.dati?.mode ?? null);
|
|
606
|
-
return
|
|
631
|
+
return this.dati?.origin === "matchmaking" ? mode : { ...mode, lobby: true };
|
|
607
632
|
}
|
|
608
633
|
info(playerId) {
|
|
609
634
|
if (this.dati === null) return null;
|
|
610
635
|
return {
|
|
611
636
|
roomId: this.dati.id,
|
|
637
|
+
origin: this.dati.origin,
|
|
638
|
+
lobby: this.configurazione.lobby,
|
|
612
639
|
status: this.dati.status,
|
|
613
640
|
players: this.manifest.persistent === true ? this.dati.giocatori.length : this.dati.giocatori.filter(
|
|
614
641
|
(player) => player.connected || player.graziaFinoA !== null
|
|
@@ -642,7 +669,7 @@ var NucleoStanza = class _NucleoStanza {
|
|
|
642
669
|
if (ascoltatore === void 0 || speaker === void 0 || listener === speakerId) {
|
|
643
670
|
return { ok: false, reason: "unknown" };
|
|
644
671
|
}
|
|
645
|
-
if (mode === "team" && ascoltatore.
|
|
672
|
+
if (mode === "team" && ascoltatore.team !== speaker.team) {
|
|
646
673
|
return { ok: false, reason: "team" };
|
|
647
674
|
}
|
|
648
675
|
if ((dati.voceGuadagni?.[listener]?.[speakerId] ?? 1) <= 0) {
|
|
@@ -656,12 +683,15 @@ var NucleoStanza = class _NucleoStanza {
|
|
|
656
683
|
if (this.dati.status === "ended") return { ok: false, code: "room_ended" };
|
|
657
684
|
const esistente = this.dati.giocatori.find((player) => player.id === identity.id);
|
|
658
685
|
if (esistente !== void 0) return { ok: true };
|
|
659
|
-
if (this.configurazione.lobby && !["lobby", "finished"].includes(this.dati.status)) {
|
|
686
|
+
if (this.configurazione.lobby && !["lobby", "countdown", "finished"].includes(this.dati.status)) {
|
|
660
687
|
return { ok: false, code: "room_playing" };
|
|
661
688
|
}
|
|
662
689
|
if (this.dati.giocatori.length >= this.configurazione.players.max) {
|
|
663
690
|
return { ok: false, code: "room_full" };
|
|
664
691
|
}
|
|
692
|
+
if (this.manifest.roles.length > 0 && this.ruoloAutomatico() === null) {
|
|
693
|
+
return { ok: false, code: "room_full" };
|
|
694
|
+
}
|
|
665
695
|
return { ok: true };
|
|
666
696
|
}
|
|
667
697
|
async entra(identity, connessione) {
|
|
@@ -701,7 +731,6 @@ var NucleoStanza = class _NucleoStanza {
|
|
|
701
731
|
player.connessione = connessione;
|
|
702
732
|
player.seq = 0;
|
|
703
733
|
}
|
|
704
|
-
dati.hostId ??= player.id;
|
|
705
734
|
dati.vuotaDa = null;
|
|
706
735
|
dati.ultimoInputAt = ora;
|
|
707
736
|
const primaConnessione = !this.configurazione.lobby && dati.status === "lobby";
|
|
@@ -718,7 +747,6 @@ var NucleoStanza = class _NucleoStanza {
|
|
|
718
747
|
}
|
|
719
748
|
if (primaConnessione) {
|
|
720
749
|
await this.chiama(this.definizione.onStart, this.room);
|
|
721
|
-
this.iniziaReplay();
|
|
722
750
|
this.inviaStatus(ora);
|
|
723
751
|
}
|
|
724
752
|
await this.concludiEvento();
|
|
@@ -726,6 +754,7 @@ var NucleoStanza = class _NucleoStanza {
|
|
|
726
754
|
this.inviaWelcome(player);
|
|
727
755
|
this.inviaSnapshotTutti();
|
|
728
756
|
this.inviaGiocatori();
|
|
757
|
+
this.rivalutaAvvio();
|
|
729
758
|
}
|
|
730
759
|
await this.persistiEProgramma();
|
|
731
760
|
return { ok: true };
|
|
@@ -739,10 +768,9 @@ var NucleoStanza = class _NucleoStanza {
|
|
|
739
768
|
player.connected = false;
|
|
740
769
|
player.connessione = null;
|
|
741
770
|
player.graziaFinoA = this.adattatore.ora() + GRAZIA_MS;
|
|
742
|
-
if (this.dati.status === "finished" || this.manifest.persistent === true && this.dati.status
|
|
771
|
+
if (this.dati.status === "finished" || this.manifest.persistent === true && ["lobby", "countdown"].includes(this.dati.status)) player.ready = false;
|
|
743
772
|
this.frequenza.delete(connessione);
|
|
744
|
-
|
|
745
|
-
this.verificaCountdown();
|
|
773
|
+
this.rivalutaAvvio();
|
|
746
774
|
await this.chiama(this.definizione.onConnection, this.room, copiaGiocatore(player), false);
|
|
747
775
|
await this.concludiEvento();
|
|
748
776
|
this.inviaGiocatori();
|
|
@@ -809,6 +837,7 @@ var NucleoStanza = class _NucleoStanza {
|
|
|
809
837
|
this.dati.ultimoInputAt = ora;
|
|
810
838
|
player.ready = message.ready;
|
|
811
839
|
this.inviaGiocatori();
|
|
840
|
+
this.rivalutaAvvio();
|
|
812
841
|
await this.persistiEProgramma();
|
|
813
842
|
return;
|
|
814
843
|
}
|
|
@@ -850,12 +879,6 @@ var NucleoStanza = class _NucleoStanza {
|
|
|
850
879
|
await this.persistiEProgramma();
|
|
851
880
|
return;
|
|
852
881
|
}
|
|
853
|
-
if (message.t === "start") {
|
|
854
|
-
if (!this.inLobby(player)) return;
|
|
855
|
-
this.dati.ultimoInputAt = ora;
|
|
856
|
-
await this.avviaCountdown(player);
|
|
857
|
-
return;
|
|
858
|
-
}
|
|
859
882
|
if (message.t === "msg") {
|
|
860
883
|
if (!Number.isSafeInteger(message.seq) || message.seq < 1) {
|
|
861
884
|
await this.messaggioErrato(player);
|
|
@@ -869,11 +892,6 @@ var NucleoStanza = class _NucleoStanza {
|
|
|
869
892
|
return;
|
|
870
893
|
}
|
|
871
894
|
player.seq = seq;
|
|
872
|
-
if (player.role === "spectator") {
|
|
873
|
-
this.inviaErrore(player, "spectator", "Spectators cannot send game input.");
|
|
874
|
-
await this.persistiEProgramma();
|
|
875
|
-
return;
|
|
876
|
-
}
|
|
877
895
|
if (this.dati.status === "finished") return;
|
|
878
896
|
this.dati.ultimoInputAt = ora;
|
|
879
897
|
await this.chiama(
|
|
@@ -938,7 +956,6 @@ var NucleoStanza = class _NucleoStanza {
|
|
|
938
956
|
this.dati.ultimoInputAt = ora;
|
|
939
957
|
this.dati.countdownAt = null;
|
|
940
958
|
await this.chiama(this.definizione.onStart, this.room);
|
|
941
|
-
this.iniziaReplay();
|
|
942
959
|
this.inviaStatus(ora);
|
|
943
960
|
}
|
|
944
961
|
}
|
|
@@ -961,13 +978,29 @@ var NucleoStanza = class _NucleoStanza {
|
|
|
961
978
|
await this.concludiEvento();
|
|
962
979
|
await this.persistiEProgramma();
|
|
963
980
|
}
|
|
981
|
+
async preparaFlush() {
|
|
982
|
+
const dati = this.richiediDati();
|
|
983
|
+
if (dati.flushInAttesa !== void 0) {
|
|
984
|
+
await this.persistiEProgramma();
|
|
985
|
+
return dati.flushInAttesa;
|
|
986
|
+
}
|
|
987
|
+
if (dati.fineInCoda === null) return null;
|
|
988
|
+
dati.flushInAttesa = { id: crypto.randomUUID(), ended: dati.fineInCoda };
|
|
989
|
+
dati.fineInCoda = null;
|
|
990
|
+
await this.persistiEProgramma();
|
|
991
|
+
return dati.flushInAttesa;
|
|
992
|
+
}
|
|
993
|
+
async confermaFlush(id) {
|
|
994
|
+
const dati = this.richiediDati();
|
|
995
|
+
if (dati.flushInAttesa?.id !== id) return;
|
|
996
|
+
delete dati.flushInAttesa;
|
|
997
|
+
await this.persistiEProgramma();
|
|
998
|
+
}
|
|
964
999
|
async flush() {
|
|
965
1000
|
const dati = this.richiediDati();
|
|
966
1001
|
const esito = {
|
|
967
|
-
scores: dati.punteggi.map((score) => ({ ...score })),
|
|
968
1002
|
ended: dati.fineInCoda === null ? null : { result: dati.fineInCoda.result, at: dati.fineInCoda.at }
|
|
969
1003
|
};
|
|
970
|
-
dati.punteggi = [];
|
|
971
1004
|
dati.fineInCoda = null;
|
|
972
1005
|
if (dati.cancellaDopoFlush && dati.status === "ended") {
|
|
973
1006
|
const chiavi = await this.adattatore.storage.list();
|
|
@@ -992,13 +1025,12 @@ var NucleoStanza = class _NucleoStanza {
|
|
|
992
1025
|
if (player.connessione !== null) this.frequenza.delete(player.connessione);
|
|
993
1026
|
player.connessione = null;
|
|
994
1027
|
player.graziaFinoA = ora + GRAZIA_MS;
|
|
995
|
-
if (dati.status === "finished" || this.manifest.persistent === true && dati.status
|
|
1028
|
+
if (dati.status === "finished" || this.manifest.persistent === true && ["lobby", "countdown"].includes(dati.status)) player.ready = false;
|
|
996
1029
|
disconnessi.push(player);
|
|
997
1030
|
}
|
|
998
1031
|
}
|
|
999
1032
|
if (disconnessi.length > 0) {
|
|
1000
|
-
this.
|
|
1001
|
-
this.verificaCountdown();
|
|
1033
|
+
this.rivalutaAvvio();
|
|
1002
1034
|
for (const player of disconnessi) {
|
|
1003
1035
|
await this.chiama(this.definizione.onConnection, this.room, copiaGiocatore(player), false);
|
|
1004
1036
|
}
|
|
@@ -1021,7 +1053,7 @@ var NucleoStanza = class _NucleoStanza {
|
|
|
1021
1053
|
const mancaB = countB < b.min ? 0 : 1;
|
|
1022
1054
|
return mancaA - mancaB || countA - countB;
|
|
1023
1055
|
});
|
|
1024
|
-
return candidati[0]?.id ??
|
|
1056
|
+
return candidati[0]?.id ?? null;
|
|
1025
1057
|
}
|
|
1026
1058
|
squadraAutomatica() {
|
|
1027
1059
|
if (this.manifest.teams === null) return null;
|
|
@@ -1044,39 +1076,37 @@ var NucleoStanza = class _NucleoStanza {
|
|
|
1044
1076
|
}
|
|
1045
1077
|
cambiaRuolo(player, roleId) {
|
|
1046
1078
|
const ruolo = this.manifest.roles.find((item) => item.id === roleId);
|
|
1047
|
-
if (ruolo === void 0
|
|
1079
|
+
if (ruolo === void 0) {
|
|
1048
1080
|
return { code: "invalid_role", message: "This role does not exist." };
|
|
1049
1081
|
}
|
|
1050
1082
|
const occupati = this.richiediDati().giocatori.filter(
|
|
1051
1083
|
(item) => item.id !== player.id && item.role === roleId
|
|
1052
1084
|
).length;
|
|
1053
|
-
if (ruolo
|
|
1085
|
+
if (ruolo.max !== void 0 && occupati >= ruolo.max) {
|
|
1054
1086
|
return { code: "role_full", message: "This role is full." };
|
|
1055
1087
|
}
|
|
1056
1088
|
const ruoloPrecedente = player.role;
|
|
1057
1089
|
const squadraPrecedente = player.team;
|
|
1058
1090
|
player.role = roleId;
|
|
1059
|
-
if (
|
|
1060
|
-
else if (player.team === null) player.team = this.squadraAutomatica();
|
|
1091
|
+
if (player.team === null) player.team = this.squadraAutomatica();
|
|
1061
1092
|
if (this.richiediDati().status === "lobby") player.ready = false;
|
|
1062
1093
|
if (player.role !== ruoloPrecedente || player.team !== squadraPrecedente) {
|
|
1063
1094
|
this.rivediVoce();
|
|
1064
1095
|
}
|
|
1065
1096
|
this.inviaGiocatori();
|
|
1097
|
+
this.rivalutaAvvio();
|
|
1066
1098
|
return null;
|
|
1067
1099
|
}
|
|
1068
1100
|
cambiaSquadra(player, team) {
|
|
1069
1101
|
if (this.manifest.teams === null || !Number.isInteger(team) || team < 1 || team > this.manifest.teams.max) {
|
|
1070
1102
|
return { code: "invalid_team", message: "This team does not exist." };
|
|
1071
1103
|
}
|
|
1072
|
-
if (player.role === "spectator") {
|
|
1073
|
-
return { code: "spectator", message: "Spectators cannot join a team." };
|
|
1074
|
-
}
|
|
1075
1104
|
const squadraPrecedente = player.team;
|
|
1076
1105
|
player.team = team;
|
|
1077
1106
|
if (this.richiediDati().status === "lobby") player.ready = false;
|
|
1078
1107
|
if (player.team !== squadraPrecedente) this.rivediVoce();
|
|
1079
1108
|
this.inviaGiocatori();
|
|
1109
|
+
this.rivalutaAvvio();
|
|
1080
1110
|
return null;
|
|
1081
1111
|
}
|
|
1082
1112
|
impostaRuoloDalServer(playerId, roleId) {
|
|
@@ -1098,21 +1128,20 @@ var NucleoStanza = class _NucleoStanza {
|
|
|
1098
1128
|
}
|
|
1099
1129
|
erroreMinimi(saltaPronti = false) {
|
|
1100
1130
|
const connessi = this.richiediDati().giocatori.filter((player) => player.connected);
|
|
1101
|
-
|
|
1102
|
-
if (attivi.length < this.configurazione.players.min) {
|
|
1131
|
+
if (connessi.length < this.configurazione.players.min) {
|
|
1103
1132
|
return { code: "not_enough_players", message: "The room does not have enough players." };
|
|
1104
1133
|
}
|
|
1105
1134
|
if (!saltaPronti && connessi.some((player) => !player.ready)) {
|
|
1106
1135
|
return { code: "players_not_ready", message: "Every connected player must be ready." };
|
|
1107
1136
|
}
|
|
1108
1137
|
for (const role of this.manifest.roles) {
|
|
1109
|
-
if (
|
|
1138
|
+
if (connessi.filter((player) => player.role === role.id).length < role.min) {
|
|
1110
1139
|
return { code: "role_minimum", message: `Role ${role.id} does not meet its minimum.` };
|
|
1111
1140
|
}
|
|
1112
1141
|
}
|
|
1113
1142
|
if (this.manifest.teams !== null) {
|
|
1114
|
-
const squadre = new Set(
|
|
1115
|
-
if (
|
|
1143
|
+
const squadre = new Set(connessi.map((player) => player.team).filter((team) => team !== null));
|
|
1144
|
+
if (connessi.some((player) => player.team === null) || squadre.size < this.manifest.teams.min) {
|
|
1116
1145
|
return { code: "team_minimum", message: "The room does not have enough teams." };
|
|
1117
1146
|
}
|
|
1118
1147
|
}
|
|
@@ -1124,32 +1153,20 @@ var NucleoStanza = class _NucleoStanza {
|
|
|
1124
1153
|
this.inviaErrore(player, "rematch_unavailable", "This room is not waiting for a rematch.");
|
|
1125
1154
|
return;
|
|
1126
1155
|
}
|
|
1127
|
-
if (
|
|
1128
|
-
|
|
1129
|
-
|
|
1130
|
-
|
|
1131
|
-
|
|
1132
|
-
player.ready = true;
|
|
1133
|
-
this.inviaGiocatori();
|
|
1134
|
-
if (dati.rivincita?.autoStart && this.prontiRivincita()) await this.avviaRivincita();
|
|
1135
|
-
else await this.persistiEProgramma();
|
|
1136
|
-
return;
|
|
1137
|
-
}
|
|
1138
|
-
if (dati.hostId !== player.id) return;
|
|
1139
|
-
if (!this.prontiRivincita(dati.rivincita?.autoStart === true)) {
|
|
1140
|
-
this.inviaErrore(player, "players_not_ready", "Enough connected players must be ready for the rematch.");
|
|
1141
|
-
return;
|
|
1142
|
-
}
|
|
1143
|
-
await this.avviaRivincita();
|
|
1156
|
+
if (player.ready) return;
|
|
1157
|
+
player.ready = true;
|
|
1158
|
+
this.inviaGiocatori();
|
|
1159
|
+
if (this.prontiRivincita()) await this.avviaRivincita();
|
|
1160
|
+
else await this.persistiEProgramma();
|
|
1144
1161
|
}
|
|
1145
|
-
prontiRivincita(
|
|
1146
|
-
const attivi = this.richiediDati().giocatori.filter((item) => item.connected
|
|
1147
|
-
return attivi.filter((item) => item.ready).length >= this.configurazione.players.min &&
|
|
1162
|
+
prontiRivincita() {
|
|
1163
|
+
const attivi = this.richiediDati().giocatori.filter((item) => item.connected);
|
|
1164
|
+
return attivi.filter((item) => item.ready).length >= this.configurazione.players.min && attivi.every((item) => item.ready);
|
|
1148
1165
|
}
|
|
1149
1166
|
async avviaRivincita() {
|
|
1150
|
-
const dati = this.richiediDati()
|
|
1151
|
-
const keepSetup = dati.rivincita?.keepSetup === true
|
|
1152
|
-
dati.status =
|
|
1167
|
+
const dati = this.richiediDati();
|
|
1168
|
+
const keepSetup = dati.rivincita?.keepSetup === true;
|
|
1169
|
+
dati.status = keepSetup ? "countdown" : this.configurazione.lobby ? "lobby" : "playing";
|
|
1153
1170
|
dati.countdownRivincita = keepSetup;
|
|
1154
1171
|
dati.countdownAt = keepSetup ? this.adattatore.ora() + COUNTDOWN_MS : null;
|
|
1155
1172
|
dati.rivincitaFinoA = null;
|
|
@@ -1163,7 +1180,6 @@ var NucleoStanza = class _NucleoStanza {
|
|
|
1163
1180
|
await this.applicaAzioni();
|
|
1164
1181
|
if (dati.status === "playing") {
|
|
1165
1182
|
await this.chiama(this.definizione.onStart, this.room);
|
|
1166
|
-
this.iniziaReplay();
|
|
1167
1183
|
}
|
|
1168
1184
|
await this.concludiEvento();
|
|
1169
1185
|
if (dati.status === "lobby" || dati.status === "playing" || dati.status === "countdown") {
|
|
@@ -1173,29 +1189,25 @@ var NucleoStanza = class _NucleoStanza {
|
|
|
1173
1189
|
}
|
|
1174
1190
|
await this.persistiEProgramma();
|
|
1175
1191
|
}
|
|
1176
|
-
|
|
1192
|
+
/**
|
|
1193
|
+
* Nessuno ospita e nessuno preme "inizia": appena tutti i connessi sono pronti e i
|
|
1194
|
+
* minimi di giocatori, ruoli e squadre sono rispettati parte il countdown; se la
|
|
1195
|
+
* condizione cade durante il countdown si torna in lobby.
|
|
1196
|
+
*/
|
|
1197
|
+
rivalutaAvvio() {
|
|
1177
1198
|
const dati = this.richiediDati();
|
|
1178
|
-
if (dati.
|
|
1179
|
-
this.
|
|
1180
|
-
|
|
1181
|
-
|
|
1182
|
-
|
|
1183
|
-
if (errore !== null) {
|
|
1184
|
-
this.inviaErrore(player, errore.code, errore.message);
|
|
1199
|
+
if (dati.status === "countdown") {
|
|
1200
|
+
if (this.erroreMinimi(dati.countdownRivincita) === null) return;
|
|
1201
|
+
dati.status = "lobby";
|
|
1202
|
+
dati.countdownAt = null;
|
|
1203
|
+
this.inviaStatus(this.adattatore.ora());
|
|
1185
1204
|
return;
|
|
1186
1205
|
}
|
|
1206
|
+
if (dati.status !== "lobby" || this.erroreMinimi() !== null) return;
|
|
1187
1207
|
dati.status = "countdown";
|
|
1188
1208
|
dati.countdownRivincita = false;
|
|
1189
1209
|
dati.countdownAt = this.adattatore.ora() + COUNTDOWN_MS;
|
|
1190
1210
|
this.inviaStatus(dati.countdownAt);
|
|
1191
|
-
await this.persistiEProgramma();
|
|
1192
|
-
}
|
|
1193
|
-
verificaCountdown() {
|
|
1194
|
-
const dati = this.richiediDati();
|
|
1195
|
-
if (dati.status !== "countdown" || this.erroreMinimi(dati.countdownRivincita) === null) return;
|
|
1196
|
-
dati.status = "lobby";
|
|
1197
|
-
dati.countdownAt = null;
|
|
1198
|
-
this.inviaStatus(this.adattatore.ora());
|
|
1199
1211
|
}
|
|
1200
1212
|
async messaggioErrato(player) {
|
|
1201
1213
|
await this.chiudiConnessione(player, 4009, "bad_message");
|
|
@@ -1213,7 +1225,6 @@ var NucleoStanza = class _NucleoStanza {
|
|
|
1213
1225
|
if (player.connessione !== null) this.frequenza.delete(player.connessione);
|
|
1214
1226
|
this.rivediVoce();
|
|
1215
1227
|
this.pulisciGuadagni(player.id);
|
|
1216
|
-
if (dati.hostId === player.id) this.assegnaHost();
|
|
1217
1228
|
if (dati.giocatori.length === 0) dati.vuotaDa = this.adattatore.ora();
|
|
1218
1229
|
await this.chiama(
|
|
1219
1230
|
this.definizione.onLeave,
|
|
@@ -1221,12 +1232,7 @@ var NucleoStanza = class _NucleoStanza {
|
|
|
1221
1232
|
copiaGiocatore({ ...player, connected: false, connessione: null }),
|
|
1222
1233
|
reason
|
|
1223
1234
|
);
|
|
1224
|
-
this.
|
|
1225
|
-
}
|
|
1226
|
-
assegnaHost() {
|
|
1227
|
-
const dati = this.richiediDati();
|
|
1228
|
-
const host = dati.giocatori.filter((player) => player.connected).sort((a, b) => a.entratoAt - b.entratoAt)[0];
|
|
1229
|
-
dati.hostId = host?.id ?? null;
|
|
1235
|
+
this.rivalutaAvvio();
|
|
1230
1236
|
}
|
|
1231
1237
|
async eseguiTimer(timer) {
|
|
1232
1238
|
const value = this.definizione[timer.handler];
|
|
@@ -1265,45 +1271,6 @@ var NucleoStanza = class _NucleoStanza {
|
|
|
1265
1271
|
const day = this.giornata(ora);
|
|
1266
1272
|
return { day, seed: seedGiornata(this.manifest.id, day), expiresAt: prossimaMezzanotteUtc(ora) };
|
|
1267
1273
|
}
|
|
1268
|
-
accodaPunteggio(playerId, board, score, options) {
|
|
1269
|
-
if (!this.richiediDati().giocatori.some((player2) => player2.id === playerId)) {
|
|
1270
|
-
throw new Error("Player not found.");
|
|
1271
|
-
}
|
|
1272
|
-
if (!CHIAVE.test(board)) {
|
|
1273
|
-
throw new TypeError("Board names must use lowercase letters, numbers, underscores, or hyphens.");
|
|
1274
|
-
}
|
|
1275
|
-
if (!Number.isSafeInteger(score) || score < 0) {
|
|
1276
|
-
throw new TypeError("Score must be a non-negative safe integer.");
|
|
1277
|
-
}
|
|
1278
|
-
const submittedAt = this.adattatore.ora();
|
|
1279
|
-
const daily = options.daily === true || options.day !== void 0;
|
|
1280
|
-
let day = daily ? this.giornata(submittedAt) : null;
|
|
1281
|
-
const start = this.manifest.boards?.[board]?.day === "start";
|
|
1282
|
-
if (options.day !== void 0 && (!start || options.daily === false)) {
|
|
1283
|
-
throw erroreConCodice("board_day_policy", "An explicit day requires a start-day board and a daily score.");
|
|
1284
|
-
}
|
|
1285
|
-
if (daily && start) {
|
|
1286
|
-
const run = this.richiediDati().daily;
|
|
1287
|
-
if (options.day === void 0) throw erroreConCodice("board_day_required", "Submit the daily score with room.daily.day.");
|
|
1288
|
-
if (options.day !== run.day) throw erroreConCodice("board_day_mismatch", "The score day must match room.daily.day.");
|
|
1289
|
-
if (submittedAt > run.expiresAt + GRAZIA_GIORNALIERA_MS) throw erroreConCodice("board_day_expired", "The starting day grace period has expired.");
|
|
1290
|
-
day = run.day;
|
|
1291
|
-
}
|
|
1292
|
-
this.richiediDati().punteggi.push({
|
|
1293
|
-
playerId,
|
|
1294
|
-
board,
|
|
1295
|
-
score,
|
|
1296
|
-
daily,
|
|
1297
|
-
submittedAt,
|
|
1298
|
-
day
|
|
1299
|
-
});
|
|
1300
|
-
const player = this.richiediDati().giocatori.find((item) => item.id === playerId);
|
|
1301
|
-
if (player.connected && player.connessione !== null) this.adattatore.invia(player.connessione, {
|
|
1302
|
-
t: "score-queued",
|
|
1303
|
-
score: { player: playerId, board, score, day, submittedAt }
|
|
1304
|
-
});
|
|
1305
|
-
this.broadcast({ t: "flush" });
|
|
1306
|
-
}
|
|
1307
1274
|
richiediFine(result, rematch = false) {
|
|
1308
1275
|
const json = analizzaJson(result);
|
|
1309
1276
|
if (!json.ok) throw new TypeError("Game result must be valid JSON.");
|
|
@@ -1331,91 +1298,6 @@ var NucleoStanza = class _NucleoStanza {
|
|
|
1331
1298
|
if (!json.ok) throw new Error("The saved value is invalid.");
|
|
1332
1299
|
return json.valore;
|
|
1333
1300
|
}
|
|
1334
|
-
verificaChiaveDeposito(key) {
|
|
1335
|
-
if (typeof key !== "string" || !CHIAVE.test(key)) {
|
|
1336
|
-
throw erroreConCodice(
|
|
1337
|
-
"store_invalid_key",
|
|
1338
|
-
"Shared store keys must use lowercase letters, numbers, underscores, or hyphens."
|
|
1339
|
-
);
|
|
1340
|
-
}
|
|
1341
|
-
}
|
|
1342
|
-
verificaPrefissoDeposito(prefix) {
|
|
1343
|
-
if (prefix !== void 0 && (typeof prefix !== "string" || !PREFISSO_CHIAVE.test(prefix))) {
|
|
1344
|
-
throw erroreConCodice(
|
|
1345
|
-
"store_invalid_key",
|
|
1346
|
-
"Shared store prefixes may contain lowercase letters, numbers, underscores, or hyphens."
|
|
1347
|
-
);
|
|
1348
|
-
}
|
|
1349
|
-
}
|
|
1350
|
-
contaOperazioneDeposito() {
|
|
1351
|
-
const ora = this.adattatore.ora();
|
|
1352
|
-
this.frequenzaDeposito = this.frequenzaDeposito.filter((at) => ora - at < 6e4);
|
|
1353
|
-
if (this.frequenzaDeposito.length >= LIMITE_OPERAZIONI_DEPOSITO) {
|
|
1354
|
-
throw erroreConCodice(
|
|
1355
|
-
"store_rate_limited",
|
|
1356
|
-
"The shared store allows at most 120 operations per minute for each room."
|
|
1357
|
-
);
|
|
1358
|
-
}
|
|
1359
|
-
this.frequenzaDeposito.push(ora);
|
|
1360
|
-
}
|
|
1361
|
-
async usaDeposito(operazione) {
|
|
1362
|
-
this.contaOperazioneDeposito();
|
|
1363
|
-
const deposito = this.adattatore.deposito;
|
|
1364
|
-
if (deposito === null) {
|
|
1365
|
-
throw erroreConCodice("store_unavailable", "The shared store is unavailable.");
|
|
1366
|
-
}
|
|
1367
|
-
try {
|
|
1368
|
-
return await operazione(deposito);
|
|
1369
|
-
} catch (cause) {
|
|
1370
|
-
const code = cause instanceof Error ? cause.code : void 0;
|
|
1371
|
-
if (typeof code === "string" && CODICI_DEPOSITO.has(code)) throw cause;
|
|
1372
|
-
throw erroreConCodice("store_unavailable", "The shared store is unavailable.");
|
|
1373
|
-
}
|
|
1374
|
-
}
|
|
1375
|
-
async leggiDeposito(key) {
|
|
1376
|
-
this.verificaChiaveDeposito(key);
|
|
1377
|
-
const value = await this.usaDeposito((deposito) => deposito.get(key));
|
|
1378
|
-
if (value === null) return null;
|
|
1379
|
-
try {
|
|
1380
|
-
return JSON.parse(JSON.stringify(value));
|
|
1381
|
-
} catch {
|
|
1382
|
-
throw erroreConCodice("store_unavailable", "The shared store returned invalid data.");
|
|
1383
|
-
}
|
|
1384
|
-
}
|
|
1385
|
-
async scriviDeposito(key, value) {
|
|
1386
|
-
this.verificaChiaveDeposito(key);
|
|
1387
|
-
const json = analizzaJson(value);
|
|
1388
|
-
if (!json.ok || json.bytes > LIMITE_DEPOSITO) {
|
|
1389
|
-
throw erroreConCodice(
|
|
1390
|
-
"store_too_large",
|
|
1391
|
-
"Shared store values must be valid JSON of at most 262144 bytes."
|
|
1392
|
-
);
|
|
1393
|
-
}
|
|
1394
|
-
await this.usaDeposito((deposito) => deposito.set(key, json.valore));
|
|
1395
|
-
}
|
|
1396
|
-
async eliminaDeposito(key) {
|
|
1397
|
-
this.verificaChiaveDeposito(key);
|
|
1398
|
-
await this.usaDeposito((deposito) => deposito.delete(key));
|
|
1399
|
-
}
|
|
1400
|
-
async elencaDeposito(prefix) {
|
|
1401
|
-
this.verificaPrefissoDeposito(prefix);
|
|
1402
|
-
const keys = await this.usaDeposito((deposito) => deposito.list(prefix));
|
|
1403
|
-
if (!Array.isArray(keys) || keys.some((key) => typeof key !== "string" || !CHIAVE.test(key))) {
|
|
1404
|
-
throw erroreConCodice("store_unavailable", "The shared store returned invalid keys.");
|
|
1405
|
-
}
|
|
1406
|
-
return [...keys].sort().slice(0, 4096);
|
|
1407
|
-
}
|
|
1408
|
-
async incrementaDeposito(key, amount) {
|
|
1409
|
-
this.verificaChiaveDeposito(key);
|
|
1410
|
-
if (!Number.isSafeInteger(amount)) {
|
|
1411
|
-
throw erroreConCodice("store_not_integer", "Shared store increments must be safe integers.");
|
|
1412
|
-
}
|
|
1413
|
-
const value = await this.usaDeposito((deposito) => deposito.increment(key, amount));
|
|
1414
|
-
if (!Number.isSafeInteger(value)) {
|
|
1415
|
-
throw erroreConCodice("store_unavailable", "The shared store returned an invalid integer.");
|
|
1416
|
-
}
|
|
1417
|
-
return value;
|
|
1418
|
-
}
|
|
1419
1301
|
messaggioCreatore(message) {
|
|
1420
1302
|
const json = analizzaJson(message);
|
|
1421
1303
|
if (!json.ok) throw new TypeError("Messages must be valid JSON.");
|
|
@@ -1442,7 +1324,6 @@ var NucleoStanza = class _NucleoStanza {
|
|
|
1442
1324
|
this.adattatore.invia(player.connessione, message);
|
|
1443
1325
|
}
|
|
1444
1326
|
}
|
|
1445
|
-
if (message.t !== "flush") this.adattatore.pubblica(message);
|
|
1446
1327
|
}
|
|
1447
1328
|
inviaErrore(player, code, message) {
|
|
1448
1329
|
if (player.connected && player.connessione !== null) {
|
|
@@ -1487,7 +1368,6 @@ var NucleoStanza = class _NucleoStanza {
|
|
|
1487
1368
|
tick: dati.tick,
|
|
1488
1369
|
tickRate: dati.tickRate,
|
|
1489
1370
|
serverTime: this.adattatore.ora(),
|
|
1490
|
-
host: dati.hostId,
|
|
1491
1371
|
countdownAt: dati.countdownAt,
|
|
1492
1372
|
configuration: {
|
|
1493
1373
|
players: { ...this.configurazione.players },
|
|
@@ -1498,7 +1378,7 @@ var NucleoStanza = class _NucleoStanza {
|
|
|
1498
1378
|
};
|
|
1499
1379
|
}
|
|
1500
1380
|
inviaGiocatori() {
|
|
1501
|
-
this.broadcast({ t: "players", players: this.giocatoriProtocollo()
|
|
1381
|
+
this.broadcast({ t: "players", players: this.giocatoriProtocollo() });
|
|
1502
1382
|
}
|
|
1503
1383
|
inviaStatus(at) {
|
|
1504
1384
|
const dati = this.richiediDati();
|
|
@@ -1506,7 +1386,6 @@ var NucleoStanza = class _NucleoStanza {
|
|
|
1506
1386
|
t: "status",
|
|
1507
1387
|
rematch: dati.status === "finished" ? dati.rivincita ?? null : null,
|
|
1508
1388
|
status: dati.status,
|
|
1509
|
-
host: dati.hostId,
|
|
1510
1389
|
countdownAt: dati.countdownAt,
|
|
1511
1390
|
at,
|
|
1512
1391
|
result: dati.status === "ended" || dati.status === "finished" ? dati.result : null
|
|
@@ -1528,13 +1407,6 @@ var NucleoStanza = class _NucleoStanza {
|
|
|
1528
1407
|
dati.statoSincronizzato = copiaJson(dati.state);
|
|
1529
1408
|
dati.tickSincronizzato = dati.tick;
|
|
1530
1409
|
}
|
|
1531
|
-
iniziaReplay() {
|
|
1532
|
-
if (!this.manifest.replays || !this.adattatore.inizioReplay) return;
|
|
1533
|
-
const json = analizzaJson(this.richiediDati().state);
|
|
1534
|
-
if (!json.ok || json.bytes > LIMITE_STATO) return;
|
|
1535
|
-
this.inviaSnapshotTutti();
|
|
1536
|
-
this.adattatore.inizioReplay(this.fotografia());
|
|
1537
|
-
}
|
|
1538
1410
|
inviaSnapshotTutti() {
|
|
1539
1411
|
const dati = this.richiediDati();
|
|
1540
1412
|
const stato = analizzaJson(dati.state);
|
|
@@ -1584,7 +1456,7 @@ var NucleoStanza = class _NucleoStanza {
|
|
|
1584
1456
|
async concludiEvento() {
|
|
1585
1457
|
await this.applicaAzioni();
|
|
1586
1458
|
if (this.dati === null || this.dati.status === "ended") return;
|
|
1587
|
-
if (this.dati.status === "finished" && this.dati.rivincita
|
|
1459
|
+
if (this.dati.status === "finished" && this.dati.rivincita !== null && this.prontiRivincita()) {
|
|
1588
1460
|
await this.avviaRivincita();
|
|
1589
1461
|
return;
|
|
1590
1462
|
}
|
|
@@ -1683,10 +1555,7 @@ var NucleoStanza = class _NucleoStanza {
|
|
|
1683
1555
|
if (dati.status === "ended") return;
|
|
1684
1556
|
const giaFinita = dati.status === "finished";
|
|
1685
1557
|
dati.status = rematch ? "finished" : "ended";
|
|
1686
|
-
dati.rivincita = rematch ? {
|
|
1687
|
-
keepSetup: typeof rematch === "object" && rematch.keepSetup === true,
|
|
1688
|
-
autoStart: typeof rematch === "object" && rematch.autoStart === true
|
|
1689
|
-
} : null;
|
|
1558
|
+
dati.rivincita = rematch ? { keepSetup: typeof rematch === "object" && rematch.keepSetup === true } : null;
|
|
1690
1559
|
dati.prontiPrecedenti = rematch ? dati.giocatori.filter((p) => p.ready).map((p) => p.id) : [];
|
|
1691
1560
|
dati.rivincitaFinoA = rematch ? this.adattatore.ora() + ATTESA_RIVINCITA_MS : null;
|
|
1692
1561
|
dati.timer = [];
|
|
@@ -1811,6 +1680,78 @@ var NucleoStanza = class _NucleoStanza {
|
|
|
1811
1680
|
}
|
|
1812
1681
|
};
|
|
1813
1682
|
|
|
1683
|
+
// src/runtime/network-probe.ts
|
|
1684
|
+
async function measureNetwork(definition, manifest) {
|
|
1685
|
+
const modes = (manifest.modes.length ? manifest.modes : [{ id: null }]).filter((mode) => !modalitaLocale(manifest, mode.id));
|
|
1686
|
+
const results = [];
|
|
1687
|
+
for (const mode of modes) {
|
|
1688
|
+
const storage = /* @__PURE__ */ new Map();
|
|
1689
|
+
const connections = /* @__PURE__ */ new Set();
|
|
1690
|
+
const meter = new NetworkMeter();
|
|
1691
|
+
let now = Date.now();
|
|
1692
|
+
let interval = definition.tickRate > 0 ? 1e3 / definition.tickRate : 50;
|
|
1693
|
+
let error = null;
|
|
1694
|
+
let playing = false;
|
|
1695
|
+
const adapter = {
|
|
1696
|
+
storage: {
|
|
1697
|
+
async get(key) {
|
|
1698
|
+
return structuredClone(storage.get(key));
|
|
1699
|
+
},
|
|
1700
|
+
async put(key, value) {
|
|
1701
|
+
storage.set(key, structuredClone(value));
|
|
1702
|
+
},
|
|
1703
|
+
async delete(key) {
|
|
1704
|
+
storage.delete(key);
|
|
1705
|
+
},
|
|
1706
|
+
async list(prefix = "") {
|
|
1707
|
+
return new Map([...storage].filter(([key]) => key.startsWith(prefix)));
|
|
1708
|
+
}
|
|
1709
|
+
},
|
|
1710
|
+
invia(connection, message) {
|
|
1711
|
+
if (connections.has(connection)) meter.record(connection, JSON.stringify(message), now);
|
|
1712
|
+
if (message.t === "status" && message.status === "playing") playing = true;
|
|
1713
|
+
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.";
|
|
1714
|
+
},
|
|
1715
|
+
chiudi(connection) {
|
|
1716
|
+
connections.delete(connection);
|
|
1717
|
+
},
|
|
1718
|
+
connessioniAttive: () => [...connections],
|
|
1719
|
+
ora: () => now,
|
|
1720
|
+
misuraCpu: () => performance.now(),
|
|
1721
|
+
rivediVoce() {
|
|
1722
|
+
},
|
|
1723
|
+
programmaTick(ms) {
|
|
1724
|
+
if (ms !== null) interval = ms;
|
|
1725
|
+
},
|
|
1726
|
+
async programmaSveglia() {
|
|
1727
|
+
}
|
|
1728
|
+
};
|
|
1729
|
+
const room = await NucleoStanza.apri(definition, manifest, adapter);
|
|
1730
|
+
const identity = (n) => ({ id: `probe-${n}`, name: `Player ${n + 1}`, guest: true });
|
|
1731
|
+
await room.crea("publication-probe", mode.id, identity(0), "player");
|
|
1732
|
+
const maximum = ("players" in mode ? mode.players?.max : void 0) ?? manifest.players.max;
|
|
1733
|
+
for (let n = 0; n < maximum; n++) {
|
|
1734
|
+
connections.add(`probe-${n}`);
|
|
1735
|
+
const joined = await room.entra(identity(n), `probe-${n}`);
|
|
1736
|
+
if (!joined.ok) throw new Error("The publication probe could not fill the room.");
|
|
1737
|
+
}
|
|
1738
|
+
if (room.info()?.status === "lobby") {
|
|
1739
|
+
for (const connection of connections) await room.ricevi(connection, JSON.stringify({ t: "ready", ready: true }));
|
|
1740
|
+
}
|
|
1741
|
+
const start = now;
|
|
1742
|
+
while (now - start < 2e4) {
|
|
1743
|
+
now += Math.min(interval, 2e4 - (now - start));
|
|
1744
|
+
await room.sveglia();
|
|
1745
|
+
playing ||= room.info()?.status === "playing";
|
|
1746
|
+
await room.eseguiTick();
|
|
1747
|
+
if (error !== null) throw new Error(error);
|
|
1748
|
+
}
|
|
1749
|
+
if (!playing) throw new Error("The publication probe could not start this room mode. Check lobby roles and teams.");
|
|
1750
|
+
results.push({ mode: mode.id, players: maximum, playing, simulatedSeconds: 20, ...meter.sample(now).peak });
|
|
1751
|
+
}
|
|
1752
|
+
return { modes: results };
|
|
1753
|
+
}
|
|
1754
|
+
|
|
1814
1755
|
// src/runtime/index.ts
|
|
1815
1756
|
var CHIAVE_ADATTATORE = "adattatore";
|
|
1816
1757
|
var ArchivioFacet = class {
|
|
@@ -1831,11 +1772,10 @@ var ArchivioFacet = class {
|
|
|
1831
1772
|
}
|
|
1832
1773
|
};
|
|
1833
1774
|
var AdattatoreFacet = class {
|
|
1834
|
-
constructor(archivio, tickRate
|
|
1775
|
+
constructor(archivio, tickRate) {
|
|
1835
1776
|
this.connessioni = /* @__PURE__ */ new Set();
|
|
1836
1777
|
this.cpuMessaggiOltreCento = 0;
|
|
1837
1778
|
this.frames = [];
|
|
1838
|
-
this.pubblici = [];
|
|
1839
1779
|
this.chiusure = [];
|
|
1840
1780
|
this.sveglia = null;
|
|
1841
1781
|
this.tick = false;
|
|
@@ -1843,7 +1783,6 @@ var AdattatoreFacet = class {
|
|
|
1843
1783
|
this.salvato = "";
|
|
1844
1784
|
this.storage = new ArchivioFacet(archivio);
|
|
1845
1785
|
this.tickRate = tickRate;
|
|
1846
|
-
this.deposito = deposito;
|
|
1847
1786
|
}
|
|
1848
1787
|
async ripristina() {
|
|
1849
1788
|
const dati = await this.storage.get(CHIAVE_ADATTATORE);
|
|
@@ -1867,12 +1806,6 @@ var AdattatoreFacet = class {
|
|
|
1867
1806
|
invia(connessione, messaggio) {
|
|
1868
1807
|
this.frames.push({ connessione, testo: JSON.stringify(messaggio) });
|
|
1869
1808
|
}
|
|
1870
|
-
inizioReplay(fotografia) {
|
|
1871
|
-
this.pubblici.push(JSON.stringify({ t: "replay-start", ...fotografia }));
|
|
1872
|
-
}
|
|
1873
|
-
pubblica(messaggio) {
|
|
1874
|
-
this.pubblici.push(JSON.stringify(messaggio));
|
|
1875
|
-
}
|
|
1876
1809
|
chiudi(connessione, codice, motivo) {
|
|
1877
1810
|
this.chiusure.push({ connessione, codice, motivo });
|
|
1878
1811
|
this.connessioni.delete(connessione);
|
|
@@ -1899,7 +1832,6 @@ var AdattatoreFacet = class {
|
|
|
1899
1832
|
esito(status) {
|
|
1900
1833
|
const risultato = {
|
|
1901
1834
|
frames: this.frames,
|
|
1902
|
-
pubblici: this.pubblici,
|
|
1903
1835
|
chiusure: this.chiusure,
|
|
1904
1836
|
sveglia: this.sveglia,
|
|
1905
1837
|
tick: this.tick,
|
|
@@ -1908,7 +1840,6 @@ var AdattatoreFacet = class {
|
|
|
1908
1840
|
voceRivedi: this.voceRivedi
|
|
1909
1841
|
};
|
|
1910
1842
|
this.frames = [];
|
|
1911
|
-
this.pubblici = [];
|
|
1912
1843
|
this.chiusure = [];
|
|
1913
1844
|
this.voceRivedi = false;
|
|
1914
1845
|
return risultato;
|
|
@@ -1923,8 +1854,7 @@ function creaFacetStanza(DurableObject, definizione, manifest) {
|
|
|
1923
1854
|
constructor(stato, ambiente) {
|
|
1924
1855
|
super(stato, ambiente);
|
|
1925
1856
|
this.#roomId = typeof stato.id === "string" ? stato.id : stato.id.name ?? stato.id.toString();
|
|
1926
|
-
const
|
|
1927
|
-
const adattatore = new AdattatoreFacet(stato.storage, definizione.tickRate, deposito);
|
|
1857
|
+
const adattatore = new AdattatoreFacet(stato.storage, definizione.tickRate);
|
|
1928
1858
|
this.#adattatore = adattatore;
|
|
1929
1859
|
const misurata = Object.create(definizione);
|
|
1930
1860
|
if (definizione.onMessage !== void 0) {
|
|
@@ -1959,7 +1889,7 @@ function creaFacetStanza(DurableObject, definizione, manifest) {
|
|
|
1959
1889
|
}
|
|
1960
1890
|
crea(input) {
|
|
1961
1891
|
return this.#serializza(async (nucleo) => {
|
|
1962
|
-
const ok = await nucleo.crea(this.#roomId, input.mode, input.creator);
|
|
1892
|
+
const ok = await nucleo.crea(this.#roomId, input.mode, input.creator, input.origin);
|
|
1963
1893
|
await this.#adattatore.persisti();
|
|
1964
1894
|
return {
|
|
1965
1895
|
...this.#adattatore.esito(nucleo.info()?.status ?? null),
|
|
@@ -2015,8 +1945,15 @@ function creaFacetStanza(DurableObject, definizione, manifest) {
|
|
|
2015
1945
|
flush() {
|
|
2016
1946
|
return this.#serializza((nucleo) => nucleo.flush());
|
|
2017
1947
|
}
|
|
1948
|
+
preparaFlush() {
|
|
1949
|
+
return this.#serializza((nucleo) => nucleo.preparaFlush());
|
|
1950
|
+
}
|
|
1951
|
+
confermaFlush(id) {
|
|
1952
|
+
return this.#serializza((nucleo) => nucleo.confermaFlush(id));
|
|
1953
|
+
}
|
|
2018
1954
|
};
|
|
2019
1955
|
}
|
|
2020
1956
|
export {
|
|
2021
|
-
creaFacetStanza
|
|
1957
|
+
creaFacetStanza,
|
|
1958
|
+
measureNetwork
|
|
2022
1959
|
};
|