@caisual/kit 0.4.0 → 0.5.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/kit.d.ts +18 -0
- package/dist/kit.js +123 -13
- package/dist/node.d.ts +11 -0
- package/dist/node.js +214 -12
- package/dist/runtime.d.ts +3 -0
- package/dist/runtime.js +32 -10
- package/package.json +1 -1
package/dist/kit.d.ts
CHANGED
|
@@ -102,6 +102,23 @@ export interface Room {
|
|
|
102
102
|
leave(): void;
|
|
103
103
|
serverTime(): number;
|
|
104
104
|
}
|
|
105
|
+
export interface Spectate {
|
|
106
|
+
readonly state: JsonValue;
|
|
107
|
+
readonly tick: number;
|
|
108
|
+
readonly seed: number;
|
|
109
|
+
readonly status: RoomStatus;
|
|
110
|
+
readonly players: readonly RoomPlayer[];
|
|
111
|
+
readonly host: string | null;
|
|
112
|
+
readonly code: string;
|
|
113
|
+
readonly result: JsonValue;
|
|
114
|
+
readonly delayMs: number;
|
|
115
|
+
onState(listener: (state: JsonValue, tick: number, serverTime: number) => void): () => void;
|
|
116
|
+
onPlayers(listener: (players: readonly RoomPlayer[]) => void): () => void;
|
|
117
|
+
onStatus(listener: (status: RoomStatus, result: JsonValue, at: number) => void): () => void;
|
|
118
|
+
onMessage(listener: (message: JsonValue) => void): () => void;
|
|
119
|
+
leave(): void;
|
|
120
|
+
serverTime(): number;
|
|
121
|
+
}
|
|
105
122
|
export interface MatchOptions {
|
|
106
123
|
mode: string;
|
|
107
124
|
key: Record<string, string | number>;
|
|
@@ -118,6 +135,7 @@ export interface Rooms {
|
|
|
118
135
|
mode: string | null;
|
|
119
136
|
}): Promise<Room>;
|
|
120
137
|
join(code?: string): Promise<Room>;
|
|
138
|
+
watch(code: string): Promise<Spectate>;
|
|
121
139
|
match(options: MatchOptions): Promise<Room>;
|
|
122
140
|
}
|
|
123
141
|
export interface CaisualConnection {
|
package/dist/kit.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
// Caisual game kit v0.
|
|
1
|
+
// Caisual game kit v0.5.0
|
|
2
2
|
|
|
3
3
|
// ../contracts/src/slug.ts
|
|
4
4
|
var NOMI_RISERVATI = [
|
|
@@ -1264,6 +1264,7 @@ var INTERVALLO_PING = 5e3;
|
|
|
1264
1264
|
var RITARDO_FLUSH = 500;
|
|
1265
1265
|
var ATTESA_ROSTER = 2e3;
|
|
1266
1266
|
var CHIUSURE_DEFINITIVE = /* @__PURE__ */ new Set([4003, 4004, 4005, 4006]);
|
|
1267
|
+
var CHIUSURE_DEFINITIVE_SPETTATORE = /* @__PURE__ */ new Set([4008, 4009]);
|
|
1267
1268
|
function record2(value) {
|
|
1268
1269
|
return typeof value === "object" && value !== null && !Array.isArray(value) ? value : null;
|
|
1269
1270
|
}
|
|
@@ -1271,6 +1272,10 @@ function ingressoValido(value) {
|
|
|
1271
1272
|
const dati = record2(value);
|
|
1272
1273
|
return dati !== null && typeof dati.roomId === "string" && typeof dati.code === "string" && typeof dati.join === "string" && typeof dati.url === "string";
|
|
1273
1274
|
}
|
|
1275
|
+
function visioneValida(value) {
|
|
1276
|
+
const dati = record2(value);
|
|
1277
|
+
return dati !== null && typeof dati.roomId === "string" && typeof dati.code === "string" && typeof dati.watch === "string" && typeof dati.url === "string";
|
|
1278
|
+
}
|
|
1274
1279
|
function rispostaMatchValida(value) {
|
|
1275
1280
|
const dati = record2(value);
|
|
1276
1281
|
const players = record2(dati?.players);
|
|
@@ -1343,10 +1348,19 @@ function creaApiLive(input) {
|
|
|
1343
1348
|
}
|
|
1344
1349
|
return value;
|
|
1345
1350
|
}
|
|
1351
|
+
async function visione(body, rinnova = false) {
|
|
1352
|
+
const value = await richiesta("/rooms/watch", "POST", body, rinnova);
|
|
1353
|
+
if (!visioneValida(value)) {
|
|
1354
|
+
throw creaErrore("internal_error", "The room service returned an invalid response.");
|
|
1355
|
+
}
|
|
1356
|
+
return value;
|
|
1357
|
+
}
|
|
1346
1358
|
return {
|
|
1347
1359
|
create: (mode) => ingresso("/rooms", { mode }),
|
|
1348
1360
|
joinCode: (code) => ingresso("/rooms/join", { code }),
|
|
1349
1361
|
joinRoom: (roomId) => ingresso("/rooms/join", { roomId }, true),
|
|
1362
|
+
watchCode: (code) => visione({ code }),
|
|
1363
|
+
watchRoom: (roomId) => visione({ roomId }, true),
|
|
1350
1364
|
match,
|
|
1351
1365
|
flush: (roomId) => richiesta(
|
|
1352
1366
|
`/rooms/${encodeURIComponent(roomId)}/flush`,
|
|
@@ -1355,12 +1369,13 @@ function creaApiLive(input) {
|
|
|
1355
1369
|
};
|
|
1356
1370
|
}
|
|
1357
1371
|
var StanzaClient = class {
|
|
1358
|
-
constructor(roomId, codice, url, input, api, segnalaStanza) {
|
|
1372
|
+
constructor(roomId, codice, url, input, api, segnalaStanza, spettatore = false) {
|
|
1359
1373
|
this.roomId = roomId;
|
|
1360
1374
|
this.codice = codice;
|
|
1361
1375
|
this.input = input;
|
|
1362
1376
|
this.api = api;
|
|
1363
1377
|
this.segnalaStanza = segnalaStanza;
|
|
1378
|
+
this.spettatore = spettatore;
|
|
1364
1379
|
this.statoPubblico = null;
|
|
1365
1380
|
this.statoSincronizzato = null;
|
|
1366
1381
|
this.tickCorrente = 0;
|
|
@@ -1370,6 +1385,7 @@ var StanzaClient = class {
|
|
|
1370
1385
|
this.youCorrente = "";
|
|
1371
1386
|
this.hostCorrente = null;
|
|
1372
1387
|
this.resultCorrente = null;
|
|
1388
|
+
this.delaySpettatore = 0;
|
|
1373
1389
|
this.socket = null;
|
|
1374
1390
|
this.seq = 0;
|
|
1375
1391
|
this.scartoOrario = 0;
|
|
@@ -1407,6 +1423,7 @@ var StanzaClient = class {
|
|
|
1407
1423
|
this.risolviProntaSePossibile();
|
|
1408
1424
|
}
|
|
1409
1425
|
}, input, input.voce);
|
|
1426
|
+
if (spettatore) this.rosterRicevuto = true;
|
|
1410
1427
|
this.apri(url);
|
|
1411
1428
|
}
|
|
1412
1429
|
get state() {
|
|
@@ -1436,6 +1453,9 @@ var StanzaClient = class {
|
|
|
1436
1453
|
get result() {
|
|
1437
1454
|
return this.resultCorrente;
|
|
1438
1455
|
}
|
|
1456
|
+
get delayMs() {
|
|
1457
|
+
return this.delaySpettatore;
|
|
1458
|
+
}
|
|
1439
1459
|
pronta() {
|
|
1440
1460
|
return this.promessaPronta;
|
|
1441
1461
|
}
|
|
@@ -1485,10 +1505,14 @@ var StanzaClient = class {
|
|
|
1485
1505
|
}
|
|
1486
1506
|
leave() {
|
|
1487
1507
|
if (this.lasciata) return;
|
|
1488
|
-
this.voice.leave();
|
|
1508
|
+
if (!this.spettatore) this.voice.leave();
|
|
1489
1509
|
this.lasciata = true;
|
|
1490
1510
|
this.segnalaStanza(null);
|
|
1491
|
-
if (this.socket?.readyState === APERTO)
|
|
1511
|
+
if (this.socket?.readyState === APERTO) {
|
|
1512
|
+
const socket = this.socket;
|
|
1513
|
+
this.invia({ t: "leave" });
|
|
1514
|
+
if (this.spettatore) socket.close(1e3);
|
|
1515
|
+
}
|
|
1492
1516
|
this.termina(1e3);
|
|
1493
1517
|
}
|
|
1494
1518
|
serverTime() {
|
|
@@ -1562,7 +1586,8 @@ var StanzaClient = class {
|
|
|
1562
1586
|
return;
|
|
1563
1587
|
}
|
|
1564
1588
|
try {
|
|
1565
|
-
if (dati.t === "
|
|
1589
|
+
if (dati.t === "watching") this.riceviWatching(dati);
|
|
1590
|
+
else if (dati.t === "welcome") this.riceviWelcome(dati);
|
|
1566
1591
|
else if (dati.t === "players") this.riceviGiocatori(dati.players);
|
|
1567
1592
|
else if (dati.t === "status") this.riceviStatus(dati);
|
|
1568
1593
|
else if (dati.t === "state") this.riceviDiff(dati);
|
|
@@ -1575,6 +1600,29 @@ var StanzaClient = class {
|
|
|
1575
1600
|
if (dati.t === "state" || dati.t === "snapshot") this.chiediResync();
|
|
1576
1601
|
}
|
|
1577
1602
|
}
|
|
1603
|
+
riceviWatching(dati) {
|
|
1604
|
+
const room = dati.room;
|
|
1605
|
+
if (!this.spettatore || room.id !== this.roomId) return;
|
|
1606
|
+
this.seedCorrente = room.seed;
|
|
1607
|
+
this.hostCorrente = room.host;
|
|
1608
|
+
this.statusCorrente = room.status;
|
|
1609
|
+
this.giocatoriCorrenti = dati.players.map((player) => ({ ...player }));
|
|
1610
|
+
this.delaySpettatore = dati.delayMs;
|
|
1611
|
+
this.aggiornaStato(dati.state, room.tick, room.serverTime);
|
|
1612
|
+
this.scartoOrario = room.serverTime - this.input.ora();
|
|
1613
|
+
this.resyncRichiesto = false;
|
|
1614
|
+
this.welcomeRicevuto = true;
|
|
1615
|
+
this.ritardoIndice = 0;
|
|
1616
|
+
this.tempoRiconnessione = 0;
|
|
1617
|
+
this.notifica(this.ascoltatoriGiocatori, this.copiaGiocatori());
|
|
1618
|
+
this.notifica(
|
|
1619
|
+
this.ascoltatoriStatus,
|
|
1620
|
+
this.statusCorrente,
|
|
1621
|
+
this.resultCorrente,
|
|
1622
|
+
room.serverTime
|
|
1623
|
+
);
|
|
1624
|
+
this.risolviProntaSePossibile();
|
|
1625
|
+
}
|
|
1578
1626
|
riceviWelcome(dati) {
|
|
1579
1627
|
const room = dati.room;
|
|
1580
1628
|
if (room.id !== this.roomId) return;
|
|
@@ -1623,7 +1671,7 @@ var StanzaClient = class {
|
|
|
1623
1671
|
if (dati.status === "ended") {
|
|
1624
1672
|
this.terminata = true;
|
|
1625
1673
|
this.segnalaStanza(null);
|
|
1626
|
-
this.voice.termina();
|
|
1674
|
+
if (!this.spettatore) this.voice.termina();
|
|
1627
1675
|
this.fermaPing();
|
|
1628
1676
|
this.fermaRiconnessione();
|
|
1629
1677
|
}
|
|
@@ -1669,12 +1717,12 @@ var StanzaClient = class {
|
|
|
1669
1717
|
this.socket = null;
|
|
1670
1718
|
this.welcomeRicevuto = false;
|
|
1671
1719
|
this.fermaPing();
|
|
1672
|
-
if (
|
|
1720
|
+
if (this.lasciata || this.terminata) return;
|
|
1721
|
+
if (CHIUSURE_DEFINITIVE.has(code) || this.spettatore && CHIUSURE_DEFINITIVE_SPETTATORE.has(code)) {
|
|
1673
1722
|
this.termina(code);
|
|
1674
1723
|
return;
|
|
1675
1724
|
}
|
|
1676
|
-
if (this.
|
|
1677
|
-
this.voice.socketDisconnesso();
|
|
1725
|
+
if (!this.spettatore) this.voice.socketDisconnesso();
|
|
1678
1726
|
this.programmaRiconnessione();
|
|
1679
1727
|
}
|
|
1680
1728
|
programmaRiconnessione() {
|
|
@@ -1695,7 +1743,7 @@ var StanzaClient = class {
|
|
|
1695
1743
|
async riconnetti() {
|
|
1696
1744
|
if (this.terminata || this.lasciata) return;
|
|
1697
1745
|
try {
|
|
1698
|
-
const ingresso = await this.api.joinRoom(this.roomId);
|
|
1746
|
+
const ingresso = this.spettatore ? await this.api.watchRoom(this.roomId) : await this.api.joinRoom(this.roomId);
|
|
1699
1747
|
const codiceCambiato = this.codice !== ingresso.code;
|
|
1700
1748
|
this.codice = ingresso.code;
|
|
1701
1749
|
if (codiceCambiato && this.prontaRisolta && !this.terminata && !this.lasciata) {
|
|
@@ -1718,7 +1766,7 @@ var StanzaClient = class {
|
|
|
1718
1766
|
this.segnalaStanza(null);
|
|
1719
1767
|
this.statusCorrente = "ended";
|
|
1720
1768
|
this.resultCorrente = risultato;
|
|
1721
|
-
this.voice.termina();
|
|
1769
|
+
if (!this.spettatore) this.voice.termina();
|
|
1722
1770
|
this.fermaPing();
|
|
1723
1771
|
this.fermaRiconnessione();
|
|
1724
1772
|
if (cambiato) this.notifica(this.ascoltatoriStatus, "ended", risultato, this.serverTime());
|
|
@@ -1728,7 +1776,9 @@ var StanzaClient = class {
|
|
|
1728
1776
|
4003: "kicked",
|
|
1729
1777
|
4004: "room_ended",
|
|
1730
1778
|
4005: "version_closed",
|
|
1731
|
-
4006: "replaced"
|
|
1779
|
+
4006: "replaced",
|
|
1780
|
+
4008: "rate_limited",
|
|
1781
|
+
4009: "invalid_request"
|
|
1732
1782
|
};
|
|
1733
1783
|
const erroreCode = typeof code === "number" ? codici[code] ?? "offline" : "offline";
|
|
1734
1784
|
this.rifiutaPronta(creaErrore(erroreCode, "The room connection ended."));
|
|
@@ -1741,7 +1791,9 @@ var StanzaClient = class {
|
|
|
1741
1791
|
this.timerRoster = null;
|
|
1742
1792
|
}
|
|
1743
1793
|
this.prontaRisolta = true;
|
|
1744
|
-
if (!this.terminata && !this.lasciata)
|
|
1794
|
+
if (!this.spettatore && !this.terminata && !this.lasciata) {
|
|
1795
|
+
this.segnalaStanza({ code: this.codice });
|
|
1796
|
+
}
|
|
1745
1797
|
this.risolviPronta();
|
|
1746
1798
|
}
|
|
1747
1799
|
richiediFlush() {
|
|
@@ -1774,6 +1826,9 @@ function creaStanzeOffline(invited = null) {
|
|
|
1774
1826
|
async join() {
|
|
1775
1827
|
throw erroreOffline();
|
|
1776
1828
|
},
|
|
1829
|
+
async watch() {
|
|
1830
|
+
throw erroreOffline();
|
|
1831
|
+
},
|
|
1777
1832
|
async match() {
|
|
1778
1833
|
throw erroreOffline();
|
|
1779
1834
|
}
|
|
@@ -1802,6 +1857,55 @@ function creaGestoreStanze(input, invited) {
|
|
|
1802
1857
|
await stanza.pronta();
|
|
1803
1858
|
return stanza;
|
|
1804
1859
|
};
|
|
1860
|
+
const guarda = async (ingresso) => {
|
|
1861
|
+
const stanza = new StanzaClient(
|
|
1862
|
+
ingresso.roomId,
|
|
1863
|
+
ingresso.code,
|
|
1864
|
+
ingresso.url,
|
|
1865
|
+
input,
|
|
1866
|
+
api,
|
|
1867
|
+
() => void 0,
|
|
1868
|
+
true
|
|
1869
|
+
);
|
|
1870
|
+
await stanza.pronta();
|
|
1871
|
+
return {
|
|
1872
|
+
get state() {
|
|
1873
|
+
return stanza.state;
|
|
1874
|
+
},
|
|
1875
|
+
get tick() {
|
|
1876
|
+
return stanza.tick;
|
|
1877
|
+
},
|
|
1878
|
+
get seed() {
|
|
1879
|
+
return stanza.seed;
|
|
1880
|
+
},
|
|
1881
|
+
get status() {
|
|
1882
|
+
return stanza.status;
|
|
1883
|
+
},
|
|
1884
|
+
get players() {
|
|
1885
|
+
return stanza.players;
|
|
1886
|
+
},
|
|
1887
|
+
get host() {
|
|
1888
|
+
return stanza.host;
|
|
1889
|
+
},
|
|
1890
|
+
get code() {
|
|
1891
|
+
return stanza.code;
|
|
1892
|
+
},
|
|
1893
|
+
get result() {
|
|
1894
|
+
return stanza.result;
|
|
1895
|
+
},
|
|
1896
|
+
get delayMs() {
|
|
1897
|
+
return stanza.delayMs;
|
|
1898
|
+
},
|
|
1899
|
+
onState: (listener) => stanza.onState(listener),
|
|
1900
|
+
onPlayers: (listener) => stanza.onPlayers(listener),
|
|
1901
|
+
onStatus: (listener) => stanza.onStatus(listener),
|
|
1902
|
+
onMessage: (listener) => stanza.onMessage(listener),
|
|
1903
|
+
leave: () => {
|
|
1904
|
+
stanza.leave();
|
|
1905
|
+
},
|
|
1906
|
+
serverTime: () => stanza.serverTime()
|
|
1907
|
+
};
|
|
1908
|
+
};
|
|
1805
1909
|
const attendiMatch = (url, options) => new Promise((resolve, reject) => {
|
|
1806
1910
|
let socket;
|
|
1807
1911
|
let conclusa = false;
|
|
@@ -1911,6 +2015,12 @@ function creaGestoreStanze(input, invited) {
|
|
|
1911
2015
|
}
|
|
1912
2016
|
return collega(await api.joinCode(scelto));
|
|
1913
2017
|
},
|
|
2018
|
+
async watch(code) {
|
|
2019
|
+
if (typeof code !== "string" || code.length === 0) {
|
|
2020
|
+
throw creaErrore("invalid_request", "A room invitation code is required.");
|
|
2021
|
+
}
|
|
2022
|
+
return guarda(await api.watchCode(code));
|
|
2023
|
+
},
|
|
1914
2024
|
async match(options) {
|
|
1915
2025
|
const annullata = () => options.signal?.aborted === true;
|
|
1916
2026
|
if (annullata()) {
|
package/dist/node.d.ts
CHANGED
|
@@ -21,6 +21,9 @@ export interface NodeRoomManifest {
|
|
|
21
21
|
};
|
|
22
22
|
lobby: boolean;
|
|
23
23
|
persistent?: boolean;
|
|
24
|
+
spectators?: {
|
|
25
|
+
delayMs: number;
|
|
26
|
+
} | null;
|
|
24
27
|
roles: Array<{
|
|
25
28
|
id: string;
|
|
26
29
|
min: number;
|
|
@@ -41,6 +44,12 @@ export type NodeRoomJoinResult = {
|
|
|
41
44
|
ok: false;
|
|
42
45
|
code: 'room_not_found' | 'room_full' | 'room_ended' | 'room_playing';
|
|
43
46
|
};
|
|
47
|
+
export type NodeRoomWatchResult = {
|
|
48
|
+
ok: true;
|
|
49
|
+
} | {
|
|
50
|
+
ok: false;
|
|
51
|
+
code: 'room_not_found' | 'room_ended' | 'spectators_disabled' | 'spectators_full';
|
|
52
|
+
};
|
|
44
53
|
export interface NodeRoomInfo {
|
|
45
54
|
roomId: string;
|
|
46
55
|
status: RoomStatus;
|
|
@@ -68,7 +77,9 @@ export interface NodeRoom {
|
|
|
68
77
|
create(roomId: string, mode: string | null, creator: NodePlayerIdentity): Promise<boolean>;
|
|
69
78
|
info(): Promise<NodeRoomInfo | null>;
|
|
70
79
|
canJoin(identity: NodePlayerIdentity): Promise<NodeRoomJoinResult>;
|
|
80
|
+
canWatch(): Promise<NodeRoomWatchResult>;
|
|
71
81
|
connect(socket: NodeWebSocket, identity: NodePlayerIdentity): Promise<NodeRoomJoinResult>;
|
|
82
|
+
watch(socket: NodeWebSocket, identity: NodePlayerIdentity): Promise<NodeRoomWatchResult>;
|
|
72
83
|
flush(): Promise<NodeRoomFlush>;
|
|
73
84
|
wake(): Promise<void>;
|
|
74
85
|
close(): Promise<void>;
|
package/dist/node.js
CHANGED
|
@@ -4,6 +4,45 @@ import { mkdir, readFile, rename, rm, writeFile } from "node:fs/promises";
|
|
|
4
4
|
import { dirname } from "node:path";
|
|
5
5
|
import { performance } from "node:perf_hooks";
|
|
6
6
|
|
|
7
|
+
// ../contracts/src/slug.ts
|
|
8
|
+
var NOMI_RISERVATI = [
|
|
9
|
+
"www",
|
|
10
|
+
"api",
|
|
11
|
+
"app",
|
|
12
|
+
"play",
|
|
13
|
+
"live",
|
|
14
|
+
"multi",
|
|
15
|
+
"cdn",
|
|
16
|
+
"assets",
|
|
17
|
+
"static",
|
|
18
|
+
"mail",
|
|
19
|
+
"mx",
|
|
20
|
+
"ns1",
|
|
21
|
+
"ns2",
|
|
22
|
+
"autodiscover",
|
|
23
|
+
"_dmarc",
|
|
24
|
+
"admin",
|
|
25
|
+
"login",
|
|
26
|
+
"account",
|
|
27
|
+
"auth",
|
|
28
|
+
"pay",
|
|
29
|
+
"secure",
|
|
30
|
+
"support",
|
|
31
|
+
"help",
|
|
32
|
+
"blog",
|
|
33
|
+
"status",
|
|
34
|
+
"dev",
|
|
35
|
+
"staging",
|
|
36
|
+
"test",
|
|
37
|
+
"caisual",
|
|
38
|
+
"shipz"
|
|
39
|
+
];
|
|
40
|
+
var RISERVATI = new Set(NOMI_RISERVATI);
|
|
41
|
+
|
|
42
|
+
// ../contracts/src/manifest.ts
|
|
43
|
+
var MASSIMO_SPETTATORI = 100;
|
|
44
|
+
var RITARDO_SPETTATORI_MS = 3e3;
|
|
45
|
+
|
|
7
46
|
// src/stanza/json.ts
|
|
8
47
|
function isPlainObject(value) {
|
|
9
48
|
const prototype = Object.getPrototypeOf(value);
|
|
@@ -506,6 +545,14 @@ var NucleoStanza = class _NucleoStanza {
|
|
|
506
545
|
member: playerId === void 0 ? false : this.dati.giocatori.some((player) => player.id === playerId)
|
|
507
546
|
};
|
|
508
547
|
}
|
|
548
|
+
fotografia() {
|
|
549
|
+
if (this.dati === null) return null;
|
|
550
|
+
return {
|
|
551
|
+
room: this.stanzaProtocollo(),
|
|
552
|
+
players: this.giocatoriProtocollo(),
|
|
553
|
+
state: this.dati.state
|
|
554
|
+
};
|
|
555
|
+
}
|
|
509
556
|
giocatoreConnesso(connessione) {
|
|
510
557
|
const player = this.dati?.giocatori.find(
|
|
511
558
|
(item) => item.connected && item.connessione === connessione
|
|
@@ -1165,6 +1212,7 @@ var NucleoStanza = class _NucleoStanza {
|
|
|
1165
1212
|
this.adattatore.invia(player.connessione, message);
|
|
1166
1213
|
}
|
|
1167
1214
|
}
|
|
1215
|
+
if (message.t !== "flush") this.adattatore.pubblica(message);
|
|
1168
1216
|
}
|
|
1169
1217
|
inviaErrore(player, code, message) {
|
|
1170
1218
|
if (player.connected && player.connessione !== null) {
|
|
@@ -1188,16 +1236,7 @@ var NucleoStanza = class _NucleoStanza {
|
|
|
1188
1236
|
this.adattatore.invia(player.connessione, {
|
|
1189
1237
|
t: "welcome",
|
|
1190
1238
|
you: player.id,
|
|
1191
|
-
room:
|
|
1192
|
-
id: dati.id,
|
|
1193
|
-
seed: seedStanza(dati.id),
|
|
1194
|
-
status: dati.status,
|
|
1195
|
-
mode: dati.mode,
|
|
1196
|
-
tick: dati.tick,
|
|
1197
|
-
tickRate: dati.tickRate,
|
|
1198
|
-
serverTime: this.adattatore.ora(),
|
|
1199
|
-
host: dati.hostId
|
|
1200
|
-
},
|
|
1239
|
+
room: this.stanzaProtocollo(),
|
|
1201
1240
|
players: this.giocatoriProtocollo(),
|
|
1202
1241
|
state: dati.state
|
|
1203
1242
|
});
|
|
@@ -1206,6 +1245,19 @@ var NucleoStanza = class _NucleoStanza {
|
|
|
1206
1245
|
this.adattatore.invia(player.connessione, { t: "voice", op: "gain", gains: { ...gains } });
|
|
1207
1246
|
}
|
|
1208
1247
|
}
|
|
1248
|
+
stanzaProtocollo() {
|
|
1249
|
+
const dati = this.richiediDati();
|
|
1250
|
+
return {
|
|
1251
|
+
id: dati.id,
|
|
1252
|
+
seed: seedStanza(dati.id),
|
|
1253
|
+
status: dati.status,
|
|
1254
|
+
mode: dati.mode,
|
|
1255
|
+
tick: dati.tick,
|
|
1256
|
+
tickRate: dati.tickRate,
|
|
1257
|
+
serverTime: this.adattatore.ora(),
|
|
1258
|
+
host: dati.hostId
|
|
1259
|
+
};
|
|
1260
|
+
}
|
|
1209
1261
|
inviaGiocatori() {
|
|
1210
1262
|
this.broadcast({ t: "players", players: this.giocatoriProtocollo() });
|
|
1211
1263
|
}
|
|
@@ -1729,10 +1781,13 @@ var ArchivioNode = class _ArchivioNode {
|
|
|
1729
1781
|
}
|
|
1730
1782
|
};
|
|
1731
1783
|
var AdattatoreNode = class {
|
|
1732
|
-
constructor(storage, deposito) {
|
|
1784
|
+
constructor(storage, deposito, ritardoSpettatori) {
|
|
1733
1785
|
this.storage = storage;
|
|
1734
1786
|
this.deposito = deposito;
|
|
1787
|
+
this.ritardoSpettatori = ritardoSpettatori;
|
|
1735
1788
|
this.connessioni = /* @__PURE__ */ new Map();
|
|
1789
|
+
this.spettatori = /* @__PURE__ */ new Map();
|
|
1790
|
+
this.timerSpettatori = /* @__PURE__ */ new Set();
|
|
1736
1791
|
this.tickTimer = null;
|
|
1737
1792
|
this.tickIntervallo = null;
|
|
1738
1793
|
this.tickGenerazione = 0;
|
|
@@ -1751,12 +1806,66 @@ var AdattatoreNode = class {
|
|
|
1751
1806
|
rimuovi(id) {
|
|
1752
1807
|
this.connessioni.delete(id);
|
|
1753
1808
|
}
|
|
1809
|
+
aggiungiSpettatore(id, socket) {
|
|
1810
|
+
this.spettatori.set(id, socket);
|
|
1811
|
+
}
|
|
1812
|
+
rimuoviSpettatore(id) {
|
|
1813
|
+
this.spettatori.delete(id);
|
|
1814
|
+
}
|
|
1815
|
+
numeroSpettatori() {
|
|
1816
|
+
return this.spettatori.size;
|
|
1817
|
+
}
|
|
1818
|
+
elencoSpettatori() {
|
|
1819
|
+
return [...this.spettatori];
|
|
1820
|
+
}
|
|
1754
1821
|
elencoConnessioni() {
|
|
1755
1822
|
return [...this.connessioni];
|
|
1756
1823
|
}
|
|
1757
1824
|
invia(connessione, messaggio) {
|
|
1758
1825
|
this.connessioni.get(connessione)?.send(JSON.stringify(messaggio));
|
|
1759
1826
|
}
|
|
1827
|
+
pubblica(messaggio) {
|
|
1828
|
+
const testo = JSON.stringify(messaggio);
|
|
1829
|
+
for (const [connessione] of this.spettatori) {
|
|
1830
|
+
this.inviaSpettatore(connessione, testo);
|
|
1831
|
+
if (messaggio.t === "status" && messaggio.status === "ended") {
|
|
1832
|
+
this.chiudiSpettatoreRitardato(connessione);
|
|
1833
|
+
}
|
|
1834
|
+
}
|
|
1835
|
+
}
|
|
1836
|
+
inviaSpettatore(connessione, testo) {
|
|
1837
|
+
this.accodaSpettatore(connessione, (socket) => {
|
|
1838
|
+
socket.send(testo);
|
|
1839
|
+
});
|
|
1840
|
+
}
|
|
1841
|
+
inviaSpettatoreSubito(connessione, testo) {
|
|
1842
|
+
this.spettatori.get(connessione)?.send(testo);
|
|
1843
|
+
}
|
|
1844
|
+
chiudiSpettatore(connessione, codice, motivo) {
|
|
1845
|
+
const socket = this.spettatori.get(connessione);
|
|
1846
|
+
this.spettatori.delete(connessione);
|
|
1847
|
+
socket?.close(codice, motivo);
|
|
1848
|
+
}
|
|
1849
|
+
chiudiSpettatoreRitardato(connessione) {
|
|
1850
|
+
this.accodaSpettatore(connessione, (socket) => {
|
|
1851
|
+
this.spettatori.delete(connessione);
|
|
1852
|
+
socket.close(4004, "room_ended");
|
|
1853
|
+
});
|
|
1854
|
+
}
|
|
1855
|
+
accodaSpettatore(connessione, azione) {
|
|
1856
|
+
if (this.ritardoSpettatori === 0) {
|
|
1857
|
+
const socket = this.spettatori.get(connessione);
|
|
1858
|
+
if (socket !== void 0) azione(socket);
|
|
1859
|
+
return;
|
|
1860
|
+
}
|
|
1861
|
+
const timer = setTimeout(() => {
|
|
1862
|
+
this.timerSpettatori.delete(timer);
|
|
1863
|
+
const socket = this.spettatori.get(connessione);
|
|
1864
|
+
if (socket !== void 0) azione(socket);
|
|
1865
|
+
}, this.ritardoSpettatori ?? RITARDO_SPETTATORI_MS);
|
|
1866
|
+
this.timerSpettatori.add(timer);
|
|
1867
|
+
timer.unref();
|
|
1868
|
+
}
|
|
1760
1869
|
chiudi(connessione, codice, motivo) {
|
|
1761
1870
|
this.connessioni.get(connessione)?.close(codice, motivo);
|
|
1762
1871
|
}
|
|
@@ -1789,6 +1898,8 @@ var AdattatoreNode = class {
|
|
|
1789
1898
|
this.tickGenerazione += 1;
|
|
1790
1899
|
if (this.tickTimer !== null) clearTimeout(this.tickTimer);
|
|
1791
1900
|
if (this.svegliaTimer !== null) clearTimeout(this.svegliaTimer);
|
|
1901
|
+
for (const timer of this.timerSpettatori) clearTimeout(timer);
|
|
1902
|
+
this.timerSpettatori.clear();
|
|
1792
1903
|
this.tickTimer = null;
|
|
1793
1904
|
this.svegliaTimer = null;
|
|
1794
1905
|
}
|
|
@@ -1847,6 +1958,9 @@ var StanzaNode = class {
|
|
|
1847
1958
|
canJoin(identity) {
|
|
1848
1959
|
return this.serializza(() => Promise.resolve(this.nucleo.puoEntrare(identity)));
|
|
1849
1960
|
}
|
|
1961
|
+
canWatch() {
|
|
1962
|
+
return this.serializza(() => Promise.resolve(this.permessoSpettatore()));
|
|
1963
|
+
}
|
|
1850
1964
|
async connect(socket, identity) {
|
|
1851
1965
|
const connessione = randomUUID();
|
|
1852
1966
|
this.adattatore.aggiungi(connessione, socket);
|
|
@@ -1877,6 +1991,28 @@ var StanzaNode = class {
|
|
|
1877
1991
|
throw cause;
|
|
1878
1992
|
}
|
|
1879
1993
|
}
|
|
1994
|
+
watch(socket, _identity) {
|
|
1995
|
+
return this.serializza(async () => {
|
|
1996
|
+
const permesso = this.permessoSpettatore();
|
|
1997
|
+
if (!permesso.ok) return permesso;
|
|
1998
|
+
const connessione = randomUUID();
|
|
1999
|
+
this.adattatore.aggiungiSpettatore(connessione, socket);
|
|
2000
|
+
socket.on("message", (message) => {
|
|
2001
|
+
void this.serializza(() => this.riceviSpettatore(connessione, message));
|
|
2002
|
+
});
|
|
2003
|
+
socket.on("close", () => {
|
|
2004
|
+
this.adattatore.rimuoviSpettatore(connessione);
|
|
2005
|
+
this.frameFrequenza.delete(connessione);
|
|
2006
|
+
});
|
|
2007
|
+
const fotografia = this.nucleo.fotografia();
|
|
2008
|
+
if (fotografia === null) {
|
|
2009
|
+
this.adattatore.rimuoviSpettatore(connessione);
|
|
2010
|
+
return { ok: false, code: "room_not_found" };
|
|
2011
|
+
}
|
|
2012
|
+
this.inviaWatching(connessione, fotografia);
|
|
2013
|
+
return { ok: true };
|
|
2014
|
+
});
|
|
2015
|
+
}
|
|
1880
2016
|
flush() {
|
|
1881
2017
|
return this.serializza(() => this.nucleo.flush());
|
|
1882
2018
|
}
|
|
@@ -1891,6 +2027,11 @@ var StanzaNode = class {
|
|
|
1891
2027
|
socket.close(1001, "server_shutdown");
|
|
1892
2028
|
await this.serializza(() => this.nucleo.disconnetti(connessione));
|
|
1893
2029
|
}
|
|
2030
|
+
for (const [connessione, socket] of this.adattatore.elencoSpettatori()) {
|
|
2031
|
+
this.adattatore.rimuoviSpettatore(connessione);
|
|
2032
|
+
this.frameFrequenza.delete(connessione);
|
|
2033
|
+
socket.close(1001, "server_shutdown");
|
|
2034
|
+
}
|
|
1894
2035
|
}
|
|
1895
2036
|
serializza(operazione) {
|
|
1896
2037
|
const risultato = this.coda.then(operazione);
|
|
@@ -1931,6 +2072,66 @@ var StanzaNode = class {
|
|
|
1931
2072
|
}
|
|
1932
2073
|
await this.riceviVoce(connessione, player.id, player.role, message);
|
|
1933
2074
|
}
|
|
2075
|
+
permessoSpettatore() {
|
|
2076
|
+
const info = this.nucleo.info();
|
|
2077
|
+
if (info === null) return { ok: false, code: "room_not_found" };
|
|
2078
|
+
if (info.status === "ended") return { ok: false, code: "room_ended" };
|
|
2079
|
+
if (this.manifest.spectators === null) {
|
|
2080
|
+
return { ok: false, code: "spectators_disabled" };
|
|
2081
|
+
}
|
|
2082
|
+
if (this.adattatore.numeroSpettatori() >= MASSIMO_SPETTATORI) {
|
|
2083
|
+
return { ok: false, code: "spectators_full" };
|
|
2084
|
+
}
|
|
2085
|
+
return { ok: true };
|
|
2086
|
+
}
|
|
2087
|
+
inviaWatching(connessione, fotografia) {
|
|
2088
|
+
this.adattatore.inviaSpettatore(connessione, JSON.stringify({
|
|
2089
|
+
t: "watching",
|
|
2090
|
+
...fotografia,
|
|
2091
|
+
delayMs: this.manifest.spectators?.delayMs ?? RITARDO_SPETTATORI_MS
|
|
2092
|
+
}));
|
|
2093
|
+
}
|
|
2094
|
+
async riceviSpettatore(connessione, frame) {
|
|
2095
|
+
if (Buffer.byteLength(frame, "utf8") > 16 * 1024) {
|
|
2096
|
+
this.adattatore.chiudiSpettatore(connessione, 4008, "rate_limited");
|
|
2097
|
+
this.frameFrequenza.delete(connessione);
|
|
2098
|
+
return;
|
|
2099
|
+
}
|
|
2100
|
+
const ora = Date.now();
|
|
2101
|
+
const frames = (this.frameFrequenza.get(connessione) ?? []).filter((at) => ora - at < 1e3);
|
|
2102
|
+
if (frames.length >= 20) {
|
|
2103
|
+
this.adattatore.chiudiSpettatore(connessione, 4008, "rate_limited");
|
|
2104
|
+
this.frameFrequenza.delete(connessione);
|
|
2105
|
+
return;
|
|
2106
|
+
}
|
|
2107
|
+
frames.push(ora);
|
|
2108
|
+
this.frameFrequenza.set(connessione, frames);
|
|
2109
|
+
let message = null;
|
|
2110
|
+
try {
|
|
2111
|
+
const value = JSON.parse(frame);
|
|
2112
|
+
message = typeof value === "object" && value !== null && !Array.isArray(value) ? value : null;
|
|
2113
|
+
} catch {
|
|
2114
|
+
}
|
|
2115
|
+
if (message?.t === "ping" && typeof message.c === "number" && Number.isFinite(message.c)) {
|
|
2116
|
+
this.adattatore.inviaSpettatoreSubito(
|
|
2117
|
+
connessione,
|
|
2118
|
+
JSON.stringify({ t: "pong", c: message.c, s: ora })
|
|
2119
|
+
);
|
|
2120
|
+
return;
|
|
2121
|
+
}
|
|
2122
|
+
if (message?.t === "resync") {
|
|
2123
|
+
const fotografia = this.nucleo.fotografia();
|
|
2124
|
+
if (fotografia !== null) this.inviaWatching(connessione, fotografia);
|
|
2125
|
+
return;
|
|
2126
|
+
}
|
|
2127
|
+
if (message?.t === "leave") {
|
|
2128
|
+
this.adattatore.chiudiSpettatore(connessione, 1e3, "left");
|
|
2129
|
+
this.frameFrequenza.delete(connessione);
|
|
2130
|
+
return;
|
|
2131
|
+
}
|
|
2132
|
+
this.adattatore.chiudiSpettatore(connessione, 4009, "bad_message");
|
|
2133
|
+
this.frameFrequenza.delete(connessione);
|
|
2134
|
+
}
|
|
1934
2135
|
async riceviVoce(connessione, playerId, role, value) {
|
|
1935
2136
|
const richiesta = this.richiestaVoce(value);
|
|
1936
2137
|
if (richiesta === null) {
|
|
@@ -2121,7 +2322,8 @@ var StanzaNode = class {
|
|
|
2121
2322
|
};
|
|
2122
2323
|
async function createNodeRoom(definition, manifest, options = {}) {
|
|
2123
2324
|
const storage = await ArchivioNode.apri(options.storageFile ?? null);
|
|
2124
|
-
const
|
|
2325
|
+
const ritardoSpettatori = manifest.spectators === null ? null : manifest.spectators?.delayMs ?? RITARDO_SPETTATORI_MS;
|
|
2326
|
+
const adattatore = new AdattatoreNode(storage, options.deposito ?? null, ritardoSpettatori);
|
|
2125
2327
|
const nucleo = await NucleoStanza.apri(definition, manifest, adattatore);
|
|
2126
2328
|
return new StanzaNode(nucleo, adattatore, manifest);
|
|
2127
2329
|
}
|
package/dist/runtime.d.ts
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import type { FotografiaStanza } from '../protocollo.js';
|
|
1
2
|
import type { GameDefinition, JsonValue, RoomStatus } from './server.js';
|
|
2
3
|
export interface RoomManifest {
|
|
3
4
|
id: string;
|
|
@@ -31,6 +32,7 @@ export interface Esito {
|
|
|
31
32
|
connessione: string;
|
|
32
33
|
testo: string;
|
|
33
34
|
}>;
|
|
35
|
+
pubblici: string[];
|
|
34
36
|
chiusure: Array<{
|
|
35
37
|
connessione: string;
|
|
36
38
|
codice: number;
|
|
@@ -75,6 +77,7 @@ export interface FacetStanza {
|
|
|
75
77
|
ok: boolean;
|
|
76
78
|
}>;
|
|
77
79
|
info(playerId?: string): Promise<InfoStanza | null>;
|
|
80
|
+
fotografia(): Promise<FotografiaStanza | null>;
|
|
78
81
|
puoEntrare(identita: IdentitaGiocatore): Promise<EsitoIngresso>;
|
|
79
82
|
entra(identita: IdentitaGiocatore, connessione: string): Promise<Esito>;
|
|
80
83
|
ricevi(connessione: string, testo: string): Promise<Esito>;
|
package/dist/runtime.js
CHANGED
|
@@ -500,6 +500,14 @@ var NucleoStanza = class _NucleoStanza {
|
|
|
500
500
|
member: playerId === void 0 ? false : this.dati.giocatori.some((player) => player.id === playerId)
|
|
501
501
|
};
|
|
502
502
|
}
|
|
503
|
+
fotografia() {
|
|
504
|
+
if (this.dati === null) return null;
|
|
505
|
+
return {
|
|
506
|
+
room: this.stanzaProtocollo(),
|
|
507
|
+
players: this.giocatoriProtocollo(),
|
|
508
|
+
state: this.dati.state
|
|
509
|
+
};
|
|
510
|
+
}
|
|
503
511
|
giocatoreConnesso(connessione) {
|
|
504
512
|
const player = this.dati?.giocatori.find(
|
|
505
513
|
(item) => item.connected && item.connessione === connessione
|
|
@@ -1159,6 +1167,7 @@ var NucleoStanza = class _NucleoStanza {
|
|
|
1159
1167
|
this.adattatore.invia(player.connessione, message);
|
|
1160
1168
|
}
|
|
1161
1169
|
}
|
|
1170
|
+
if (message.t !== "flush") this.adattatore.pubblica(message);
|
|
1162
1171
|
}
|
|
1163
1172
|
inviaErrore(player, code, message) {
|
|
1164
1173
|
if (player.connected && player.connessione !== null) {
|
|
@@ -1182,16 +1191,7 @@ var NucleoStanza = class _NucleoStanza {
|
|
|
1182
1191
|
this.adattatore.invia(player.connessione, {
|
|
1183
1192
|
t: "welcome",
|
|
1184
1193
|
you: player.id,
|
|
1185
|
-
room:
|
|
1186
|
-
id: dati.id,
|
|
1187
|
-
seed: seedStanza(dati.id),
|
|
1188
|
-
status: dati.status,
|
|
1189
|
-
mode: dati.mode,
|
|
1190
|
-
tick: dati.tick,
|
|
1191
|
-
tickRate: dati.tickRate,
|
|
1192
|
-
serverTime: this.adattatore.ora(),
|
|
1193
|
-
host: dati.hostId
|
|
1194
|
-
},
|
|
1194
|
+
room: this.stanzaProtocollo(),
|
|
1195
1195
|
players: this.giocatoriProtocollo(),
|
|
1196
1196
|
state: dati.state
|
|
1197
1197
|
});
|
|
@@ -1200,6 +1200,19 @@ var NucleoStanza = class _NucleoStanza {
|
|
|
1200
1200
|
this.adattatore.invia(player.connessione, { t: "voice", op: "gain", gains: { ...gains } });
|
|
1201
1201
|
}
|
|
1202
1202
|
}
|
|
1203
|
+
stanzaProtocollo() {
|
|
1204
|
+
const dati = this.richiediDati();
|
|
1205
|
+
return {
|
|
1206
|
+
id: dati.id,
|
|
1207
|
+
seed: seedStanza(dati.id),
|
|
1208
|
+
status: dati.status,
|
|
1209
|
+
mode: dati.mode,
|
|
1210
|
+
tick: dati.tick,
|
|
1211
|
+
tickRate: dati.tickRate,
|
|
1212
|
+
serverTime: this.adattatore.ora(),
|
|
1213
|
+
host: dati.hostId
|
|
1214
|
+
};
|
|
1215
|
+
}
|
|
1203
1216
|
inviaGiocatori() {
|
|
1204
1217
|
this.broadcast({ t: "players", players: this.giocatoriProtocollo() });
|
|
1205
1218
|
}
|
|
@@ -1479,6 +1492,7 @@ var AdattatoreFacet = class {
|
|
|
1479
1492
|
this.connessioni = /* @__PURE__ */ new Set();
|
|
1480
1493
|
this.cpuMessaggiOltreCento = 0;
|
|
1481
1494
|
this.frames = [];
|
|
1495
|
+
this.pubblici = [];
|
|
1482
1496
|
this.chiusure = [];
|
|
1483
1497
|
this.sveglia = null;
|
|
1484
1498
|
this.tick = false;
|
|
@@ -1509,6 +1523,9 @@ var AdattatoreFacet = class {
|
|
|
1509
1523
|
invia(connessione, messaggio) {
|
|
1510
1524
|
this.frames.push({ connessione, testo: JSON.stringify(messaggio) });
|
|
1511
1525
|
}
|
|
1526
|
+
pubblica(messaggio) {
|
|
1527
|
+
this.pubblici.push(JSON.stringify(messaggio));
|
|
1528
|
+
}
|
|
1512
1529
|
chiudi(connessione, codice, motivo) {
|
|
1513
1530
|
this.chiusure.push({ connessione, codice, motivo });
|
|
1514
1531
|
this.connessioni.delete(connessione);
|
|
@@ -1532,6 +1549,7 @@ var AdattatoreFacet = class {
|
|
|
1532
1549
|
esito(status) {
|
|
1533
1550
|
const risultato = {
|
|
1534
1551
|
frames: this.frames,
|
|
1552
|
+
pubblici: this.pubblici,
|
|
1535
1553
|
chiusure: this.chiusure,
|
|
1536
1554
|
sveglia: this.sveglia,
|
|
1537
1555
|
tick: this.tick,
|
|
@@ -1539,6 +1557,7 @@ var AdattatoreFacet = class {
|
|
|
1539
1557
|
status
|
|
1540
1558
|
};
|
|
1541
1559
|
this.frames = [];
|
|
1560
|
+
this.pubblici = [];
|
|
1542
1561
|
this.chiusure = [];
|
|
1543
1562
|
return risultato;
|
|
1544
1563
|
}
|
|
@@ -1599,6 +1618,9 @@ function creaFacetStanza(DurableObject, definizione, manifest) {
|
|
|
1599
1618
|
info(playerId) {
|
|
1600
1619
|
return this.#serializza((nucleo) => nucleo.info(playerId));
|
|
1601
1620
|
}
|
|
1621
|
+
fotografia() {
|
|
1622
|
+
return this.#serializza((nucleo) => nucleo.fotografia());
|
|
1623
|
+
}
|
|
1602
1624
|
puoEntrare(identita) {
|
|
1603
1625
|
return this.#serializza((nucleo) => nucleo.puoEntrare(identita));
|
|
1604
1626
|
}
|