@caisual/kit 0.3.0 → 0.4.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/runtime.js CHANGED
@@ -152,9 +152,8 @@ var COSTANTI_SHA256 = [
152
152
  function ruotaDestra(value, bits) {
153
153
  return value >>> bits | value << 32 - bits;
154
154
  }
155
- function seedGiornata(slug, day) {
156
- const testo = `caisual:${slug}:${day}`;
157
- const bytes = Array.from(testo, (carattere) => carattere.charCodeAt(0));
155
+ function hashSeed(testo) {
156
+ const bytes = Array.from(new TextEncoder().encode(testo));
158
157
  const bitLength = bytes.length * 8;
159
158
  bytes.push(128);
160
159
  while (bytes.length % 64 !== 56) bytes.push(0);
@@ -217,6 +216,12 @@ function seedGiornata(slug, day) {
217
216
  }
218
217
  return h0 >>> 0;
219
218
  }
219
+ function seedGiornata(slug, day) {
220
+ return hashSeed(`caisual:${slug}:${day}`);
221
+ }
222
+ function seedStanza(roomId) {
223
+ return hashSeed(`caisual:room:${roomId}`);
224
+ }
220
225
 
221
226
  // src/stanza/nucleo.ts
222
227
  var CHIAVE_NUCLEO = "nucleo";
@@ -224,13 +229,25 @@ var PREFISSO_SAVE = "save:";
224
229
  var LIMITE_FRAME = 16 * 1024;
225
230
  var LIMITE_STATO = 256 * 1024;
226
231
  var LIMITE_SAVE = 128 * 1024;
232
+ var LIMITE_DEPOSITO = 64 * 1024;
233
+ var LIMITE_OPERAZIONI_DEPOSITO = 120;
227
234
  var GRAZIA_MS = 6e4;
228
235
  var STANZA_VUOTA_MS = 5 * 6e4;
229
236
  var COUNTDOWN_MS = 3e3;
230
237
  var RIPOSO_TICK_MS = 3e4;
231
238
  var INATTIVITA_MS = 10 * 6e4;
239
+ var SCADENZA_PERSISTENTE_MS = 30 * 24 * 60 * 6e4;
232
240
  var CHIAVE = /^[a-z0-9][a-z0-9_-]{0,31}$/;
241
+ var PREFISSO_CHIAVE = /^[a-z0-9_-]{0,32}$/;
233
242
  var GAME_DEFINITION = /* @__PURE__ */ Symbol.for("@caisual/kit/game-definition");
243
+ var CODICI_DEPOSITO = /* @__PURE__ */ new Set([
244
+ "store_invalid_key",
245
+ "store_too_large",
246
+ "store_full",
247
+ "store_not_integer",
248
+ "store_unavailable",
249
+ "store_rate_limited"
250
+ ]);
234
251
  function record(value) {
235
252
  return typeof value === "object" && value !== null && !Array.isArray(value) ? value : null;
236
253
  }
@@ -253,6 +270,9 @@ function copiaGiocatore(player) {
253
270
  function copiaJson(value) {
254
271
  return JSON.parse(JSON.stringify(value));
255
272
  }
273
+ function erroreConCodice(code, message) {
274
+ return Object.assign(new Error(message), { code });
275
+ }
256
276
  function codiceIngresso(code) {
257
277
  if (code === "room_not_found") return 4001;
258
278
  if (code === "room_full") return 4e3;
@@ -265,6 +285,7 @@ var NucleoStanza = class _NucleoStanza {
265
285
  this.adattatore = adattatore;
266
286
  this.dati = null;
267
287
  this.frequenza = /* @__PURE__ */ new Map();
288
+ this.frequenzaDeposito = [];
268
289
  this.kickRichiesti = /* @__PURE__ */ new Set();
269
290
  this.voceGuadagniCambiati = /* @__PURE__ */ new Map();
270
291
  this.fineRichiesta = null;
@@ -284,6 +305,9 @@ var NucleoStanza = class _NucleoStanza {
284
305
  get id() {
285
306
  return nucleo.richiediDati().id;
286
307
  },
308
+ get seed() {
309
+ return seedStanza(nucleo.richiediDati().id);
310
+ },
287
311
  get mode() {
288
312
  return nucleo.richiediDati().mode;
289
313
  },
@@ -293,6 +317,12 @@ var NucleoStanza = class _NucleoStanza {
293
317
  get tick() {
294
318
  return nucleo.richiediDati().tick;
295
319
  },
320
+ get tickRate() {
321
+ return nucleo.richiediDati().tickRate;
322
+ },
323
+ get result() {
324
+ return nucleo.richiediDati().result;
325
+ },
296
326
  get state() {
297
327
  return nucleo.richiediDati().state;
298
328
  },
@@ -316,6 +346,12 @@ var NucleoStanza = class _NucleoStanza {
316
346
  kick(player) {
317
347
  nucleo.kickRichiesti.add(idGiocatore(player));
318
348
  },
349
+ setRole(player, role) {
350
+ nucleo.impostaRuoloDalServer(idGiocatore(player), role);
351
+ },
352
+ setTeam(player, team) {
353
+ nucleo.impostaSquadraDalServer(idGiocatore(player), team);
354
+ },
319
355
  end(result) {
320
356
  nucleo.richiediFine(result);
321
357
  },
@@ -325,6 +361,23 @@ var NucleoStanza = class _NucleoStanza {
325
361
  load(key) {
326
362
  return nucleo.caricaSave(key);
327
363
  },
364
+ shared: {
365
+ get(key) {
366
+ return nucleo.leggiDeposito(key);
367
+ },
368
+ set(key, value) {
369
+ return nucleo.scriviDeposito(key, value);
370
+ },
371
+ delete(key) {
372
+ return nucleo.eliminaDeposito(key);
373
+ },
374
+ list(prefix) {
375
+ return nucleo.elencaDeposito(prefix);
376
+ },
377
+ increment(key, amount = 1) {
378
+ return nucleo.incrementaDeposito(key, amount);
379
+ }
380
+ },
328
381
  schedule(milliseconds, handler, payload) {
329
382
  nucleo.pianifica(milliseconds, handler, payload);
330
383
  },
@@ -339,8 +392,14 @@ var NucleoStanza = class _NucleoStanza {
339
392
  get mode() {
340
393
  return nucleo.manifest.voice ?? "none";
341
394
  },
395
+ setGain(listener, speaker, gain) {
396
+ nucleo.impostaGuadagno(idGiocatore(listener), idGiocatore(speaker), gain);
397
+ },
342
398
  setProximity(a, b, gain) {
343
- nucleo.impostaProssimita(idGiocatore(a), idGiocatore(b), gain);
399
+ const primo = idGiocatore(a);
400
+ const secondo = idGiocatore(b);
401
+ nucleo.impostaGuadagno(primo, secondo, gain);
402
+ nucleo.impostaGuadagno(secondo, primo, gain);
344
403
  }
345
404
  }
346
405
  };
@@ -365,7 +424,7 @@ var NucleoStanza = class _NucleoStanza {
365
424
  return nucleo;
366
425
  }
367
426
  static verificaManifest(manifest) {
368
- if (typeof manifest?.id !== "string" || !Number.isInteger(manifest.players?.min) || !Number.isInteger(manifest.players?.max) || manifest.players.min < 1 || manifest.players.max < manifest.players.min || typeof manifest.lobby !== "boolean" || !Array.isArray(manifest.roles) || !Array.isArray(manifest.modes) || manifest.voice !== void 0 && !["none", "room", "team", "proximity"].includes(manifest.voice)) {
427
+ if (typeof manifest?.id !== "string" || !Number.isInteger(manifest.players?.min) || !Number.isInteger(manifest.players?.max) || manifest.players.min < 1 || manifest.players.max < manifest.players.min || typeof manifest.lobby !== "boolean" || manifest.persistent !== void 0 && typeof manifest.persistent !== "boolean" || !Array.isArray(manifest.roles) || !Array.isArray(manifest.modes) || manifest.voice !== void 0 && !["none", "room", "team", "proximity"].includes(manifest.voice)) {
369
428
  throw new TypeError("The room manifest is invalid.");
370
429
  }
371
430
  }
@@ -428,16 +487,17 @@ var NucleoStanza = class _NucleoStanza {
428
487
  await this.persistiEProgramma();
429
488
  return true;
430
489
  }
431
- info() {
490
+ info(playerId) {
432
491
  if (this.dati === null) return null;
433
492
  return {
434
493
  roomId: this.dati.id,
435
494
  status: this.dati.status,
436
- players: this.dati.giocatori.filter(
495
+ players: this.manifest.persistent === true ? this.dati.giocatori.length : this.dati.giocatori.filter(
437
496
  (player) => player.connected || player.graziaFinoA !== null
438
497
  ).length,
439
498
  max: this.manifest.players.max,
440
- mode: this.dati.mode
499
+ mode: this.dati.mode,
500
+ member: playerId === void 0 ? false : this.dati.giocatori.some((player) => player.id === playerId)
441
501
  };
442
502
  }
443
503
  giocatoreConnesso(connessione) {
@@ -483,7 +543,6 @@ var NucleoStanza = class _NucleoStanza {
483
543
  seq: 0
484
544
  };
485
545
  dati.giocatori.push(player);
486
- dati.hostId ??= player.id;
487
546
  } else {
488
547
  if (player.connected && player.connessione !== null && player.connessione !== connessione) {
489
548
  this.adattatore.chiudi(player.connessione, 4006, "replaced");
@@ -495,6 +554,7 @@ var NucleoStanza = class _NucleoStanza {
495
554
  player.connessione = connessione;
496
555
  player.seq = 0;
497
556
  }
557
+ dati.hostId ??= player.id;
498
558
  dati.vuotaDa = null;
499
559
  dati.ultimoInputAt = ora;
500
560
  const primaConnessione = !this.manifest.lobby && dati.status === "lobby";
@@ -528,6 +588,7 @@ var NucleoStanza = class _NucleoStanza {
528
588
  player.connected = false;
529
589
  player.connessione = null;
530
590
  player.graziaFinoA = this.adattatore.ora() + GRAZIA_MS;
591
+ if (this.manifest.persistent === true && this.dati.status === "lobby") player.ready = false;
531
592
  this.frequenza.delete(connessione);
532
593
  if (this.dati.hostId === player.id) this.assegnaHost();
533
594
  this.verificaCountdown();
@@ -597,7 +658,8 @@ var NucleoStanza = class _NucleoStanza {
597
658
  if (typeof message.role !== "string") return this.messaggioErrato(player);
598
659
  if (!this.inLobby(player)) return;
599
660
  this.dati.ultimoInputAt = ora;
600
- this.scegliRuolo(player, message.role);
661
+ const errore = this.cambiaRuolo(player, message.role);
662
+ if (errore !== null) this.inviaErrore(player, errore.code, errore.message);
601
663
  await this.persistiEProgramma();
602
664
  return;
603
665
  }
@@ -605,7 +667,8 @@ var NucleoStanza = class _NucleoStanza {
605
667
  if (!Number.isInteger(message.team)) return this.messaggioErrato(player);
606
668
  if (!this.inLobby(player)) return;
607
669
  this.dati.ultimoInputAt = ora;
608
- this.scegliSquadra(player, message.team);
670
+ const errore = this.cambiaSquadra(player, message.team);
671
+ if (errore !== null) this.inviaErrore(player, errore.code, errore.message);
609
672
  await this.persistiEProgramma();
610
673
  return;
611
674
  }
@@ -695,7 +758,10 @@ var NucleoStanza = class _NucleoStanza {
695
758
  const scaduti = this.dati.giocatori.filter(
696
759
  (player) => !player.connected && player.graziaFinoA !== null && player.graziaFinoA <= ora
697
760
  );
698
- for (const player of scaduti) await this.rimuoviGiocatore(player, "timeout");
761
+ for (const player of scaduti) {
762
+ if (this.manifest.persistent === true) player.graziaFinoA = null;
763
+ else await this.rimuoviGiocatore(player, "timeout");
764
+ }
699
765
  if (this.dati.status === "countdown" && this.dati.countdownAt !== null && this.dati.countdownAt <= ora) {
700
766
  const errore = this.erroreMinimi();
701
767
  if (errore !== null) {
@@ -755,6 +821,7 @@ var NucleoStanza = class _NucleoStanza {
755
821
  player.connected = false;
756
822
  player.connessione = null;
757
823
  player.graziaFinoA = ora + GRAZIA_MS;
824
+ if (this.manifest.persistent === true && dati.status === "lobby") player.ready = false;
758
825
  cambiato = true;
759
826
  }
760
827
  }
@@ -798,37 +865,52 @@ var NucleoStanza = class _NucleoStanza {
798
865
  this.inviaErrore(player, "not_in_lobby", "This action is only available in the lobby.");
799
866
  return false;
800
867
  }
801
- scegliRuolo(player, roleId) {
868
+ cambiaRuolo(player, roleId) {
802
869
  const ruolo = this.manifest.roles.find((item) => item.id === roleId);
803
870
  if (ruolo === void 0 && roleId !== "spectator") {
804
- this.inviaErrore(player, "invalid_role", "This role does not exist.");
805
- return;
871
+ return { code: "invalid_role", message: "This role does not exist." };
806
872
  }
807
873
  const occupati = this.richiediDati().giocatori.filter(
808
874
  (item) => item.id !== player.id && item.role === roleId
809
875
  ).length;
810
876
  if (ruolo?.max !== void 0 && occupati >= ruolo.max) {
811
- this.inviaErrore(player, "role_full", "This role is full.");
812
- return;
877
+ return { code: "role_full", message: "This role is full." };
813
878
  }
814
879
  player.role = roleId;
815
880
  if (roleId === "spectator") player.team = null;
816
881
  else if (player.team === null) player.team = this.squadraAutomatica();
817
- player.ready = false;
882
+ if (this.richiediDati().status === "lobby") player.ready = false;
818
883
  this.inviaGiocatori();
884
+ return null;
819
885
  }
820
- scegliSquadra(player, team) {
821
- if (this.manifest.teams === null || team < 1 || team > this.manifest.teams.max) {
822
- this.inviaErrore(player, "invalid_team", "This team does not exist.");
823
- return;
886
+ cambiaSquadra(player, team) {
887
+ if (this.manifest.teams === null || !Number.isInteger(team) || team < 1 || team > this.manifest.teams.max) {
888
+ return { code: "invalid_team", message: "This team does not exist." };
824
889
  }
825
890
  if (player.role === "spectator") {
826
- this.inviaErrore(player, "spectator", "Spectators cannot join a team.");
827
- return;
891
+ return { code: "spectator", message: "Spectators cannot join a team." };
828
892
  }
829
893
  player.team = team;
830
- player.ready = false;
894
+ if (this.richiediDati().status === "lobby") player.ready = false;
831
895
  this.inviaGiocatori();
896
+ return null;
897
+ }
898
+ impostaRuoloDalServer(playerId, roleId) {
899
+ const player = this.giocatorePerModificaDalServer(playerId);
900
+ const errore = this.cambiaRuolo(player, roleId);
901
+ if (errore !== null) throw erroreConCodice(errore.code, errore.message);
902
+ }
903
+ impostaSquadraDalServer(playerId, team) {
904
+ const player = this.giocatorePerModificaDalServer(playerId);
905
+ const errore = this.cambiaSquadra(player, team);
906
+ if (errore !== null) throw erroreConCodice(errore.code, errore.message);
907
+ }
908
+ giocatorePerModificaDalServer(playerId) {
909
+ const dati = this.richiediDati();
910
+ if (dati.status === "ended") throw new Error("The room has ended.");
911
+ const player = dati.giocatori.find((item) => item.id === playerId);
912
+ if (player === void 0) throw erroreConCodice("player_not_found", "Player not found.");
913
+ return player;
832
914
  }
833
915
  erroreMinimi() {
834
916
  const connessi = this.richiediDati().giocatori.filter((player) => player.connected);
@@ -972,6 +1054,91 @@ var NucleoStanza = class _NucleoStanza {
972
1054
  if (!json.ok) throw new Error("The saved value is invalid.");
973
1055
  return json.valore;
974
1056
  }
1057
+ verificaChiaveDeposito(key) {
1058
+ if (typeof key !== "string" || !CHIAVE.test(key)) {
1059
+ throw erroreConCodice(
1060
+ "store_invalid_key",
1061
+ "Shared store keys must use lowercase letters, numbers, underscores, or hyphens."
1062
+ );
1063
+ }
1064
+ }
1065
+ verificaPrefissoDeposito(prefix) {
1066
+ if (prefix !== void 0 && (typeof prefix !== "string" || !PREFISSO_CHIAVE.test(prefix))) {
1067
+ throw erroreConCodice(
1068
+ "store_invalid_key",
1069
+ "Shared store prefixes may contain lowercase letters, numbers, underscores, or hyphens."
1070
+ );
1071
+ }
1072
+ }
1073
+ contaOperazioneDeposito() {
1074
+ const ora = this.adattatore.ora();
1075
+ this.frequenzaDeposito = this.frequenzaDeposito.filter((at) => ora - at < 6e4);
1076
+ if (this.frequenzaDeposito.length >= LIMITE_OPERAZIONI_DEPOSITO) {
1077
+ throw erroreConCodice(
1078
+ "store_rate_limited",
1079
+ "The shared store allows at most 120 operations per minute for each room."
1080
+ );
1081
+ }
1082
+ this.frequenzaDeposito.push(ora);
1083
+ }
1084
+ async usaDeposito(operazione) {
1085
+ this.contaOperazioneDeposito();
1086
+ const deposito = this.adattatore.deposito;
1087
+ if (deposito === null) {
1088
+ throw erroreConCodice("store_unavailable", "The shared store is unavailable.");
1089
+ }
1090
+ try {
1091
+ return await operazione(deposito);
1092
+ } catch (cause) {
1093
+ const code = cause instanceof Error ? cause.code : void 0;
1094
+ if (typeof code === "string" && CODICI_DEPOSITO.has(code)) throw cause;
1095
+ throw erroreConCodice("store_unavailable", "The shared store is unavailable.");
1096
+ }
1097
+ }
1098
+ async leggiDeposito(key) {
1099
+ this.verificaChiaveDeposito(key);
1100
+ const value = await this.usaDeposito((deposito) => deposito.get(key));
1101
+ if (value === null) return null;
1102
+ try {
1103
+ return JSON.parse(JSON.stringify(value));
1104
+ } catch {
1105
+ throw erroreConCodice("store_unavailable", "The shared store returned invalid data.");
1106
+ }
1107
+ }
1108
+ async scriviDeposito(key, value) {
1109
+ this.verificaChiaveDeposito(key);
1110
+ const json = analizzaJson(value);
1111
+ if (!json.ok || json.bytes > LIMITE_DEPOSITO) {
1112
+ throw erroreConCodice(
1113
+ "store_too_large",
1114
+ "Shared store values must be valid JSON of at most 65536 bytes."
1115
+ );
1116
+ }
1117
+ await this.usaDeposito((deposito) => deposito.set(key, json.valore));
1118
+ }
1119
+ async eliminaDeposito(key) {
1120
+ this.verificaChiaveDeposito(key);
1121
+ await this.usaDeposito((deposito) => deposito.delete(key));
1122
+ }
1123
+ async elencaDeposito(prefix) {
1124
+ this.verificaPrefissoDeposito(prefix);
1125
+ const keys = await this.usaDeposito((deposito) => deposito.list(prefix));
1126
+ if (!Array.isArray(keys) || keys.some((key) => typeof key !== "string" || !CHIAVE.test(key))) {
1127
+ throw erroreConCodice("store_unavailable", "The shared store returned invalid keys.");
1128
+ }
1129
+ return [...keys].sort().slice(0, 1024);
1130
+ }
1131
+ async incrementaDeposito(key, amount) {
1132
+ this.verificaChiaveDeposito(key);
1133
+ if (!Number.isSafeInteger(amount)) {
1134
+ throw erroreConCodice("store_not_integer", "Shared store increments must be safe integers.");
1135
+ }
1136
+ const value = await this.usaDeposito((deposito) => deposito.increment(key, amount));
1137
+ if (!Number.isSafeInteger(value)) {
1138
+ throw erroreConCodice("store_unavailable", "The shared store returned an invalid integer.");
1139
+ }
1140
+ return value;
1141
+ }
975
1142
  broadcastCreatore(message) {
976
1143
  const json = analizzaJson(message);
977
1144
  if (!json.ok) throw new TypeError("Messages must be valid JSON.");
@@ -1017,6 +1184,7 @@ var NucleoStanza = class _NucleoStanza {
1017
1184
  you: player.id,
1018
1185
  room: {
1019
1186
  id: dati.id,
1187
+ seed: seedStanza(dati.id),
1020
1188
  status: dati.status,
1021
1189
  mode: dati.mode,
1022
1190
  tick: dati.tick,
@@ -1117,16 +1285,15 @@ var NucleoStanza = class _NucleoStanza {
1117
1285
  }
1118
1286
  return stato;
1119
1287
  }
1120
- impostaProssimita(a, b, gain) {
1121
- if ((this.manifest.voice ?? "none") !== "proximity") return;
1288
+ impostaGuadagno(listener, speaker, gain) {
1289
+ if ((this.manifest.voice ?? "none") === "none") return;
1122
1290
  const dati = this.richiediDati();
1123
- if (!dati.giocatori.some((player) => player.id === a) || !dati.giocatori.some((player) => player.id === b)) throw new Error("Player not found.");
1291
+ if (!dati.giocatori.some((player) => player.id === listener) || !dati.giocatori.some((player) => player.id === speaker)) throw new Error("Player not found.");
1124
1292
  if (Number.isNaN(gain)) throw new TypeError("Voice gain must be a number.");
1125
- if (a === b) return;
1293
+ if (listener === speaker) return;
1126
1294
  const valore = Math.round(Math.min(1, Math.max(0, gain)) * 100) / 100;
1127
1295
  dati.voceGuadagni ??= {};
1128
- this.salvaGuadagno(a, b, valore);
1129
- this.salvaGuadagno(b, a, valore);
1296
+ this.salvaGuadagno(listener, speaker, valore);
1130
1297
  }
1131
1298
  salvaGuadagno(playerId, altroId, valore) {
1132
1299
  const dati = this.richiediDati();
@@ -1250,6 +1417,11 @@ var NucleoStanza = class _NucleoStanza {
1250
1417
  return dati !== null && dati.status === "playing" && dati.tickRate > 0 && dati.giocatori.some((player) => player.connected) && this.adattatore.ora() - Math.max(dati.ultimoInputAt, dati.ultimoCambioStatoAt) < RIPOSO_TICK_MS;
1251
1418
  }
1252
1419
  async terminaSeInattiva() {
1420
+ if (this.manifest.persistent === true) {
1421
+ if (this.dati?.status === "ended" || this.dati === null || this.adattatore.ora() < Math.max(this.dati.ultimoInputAt, this.dati.ultimoCambioStatoAt) + SCADENZA_PERSISTENTE_MS) return false;
1422
+ await this.terminaInterna({ error: "expired" });
1423
+ return true;
1424
+ }
1253
1425
  if (this.dati?.status !== "playing" || this.adattatore.ora() < this.dati.ultimoInputAt + INATTIVITA_MS) return false;
1254
1426
  await this.terminaInterna({ error: "idle" });
1255
1427
  return true;
@@ -1264,7 +1436,13 @@ var NucleoStanza = class _NucleoStanza {
1264
1436
  this.serveTick() ? 1e3 / this.dati.tickRate : null
1265
1437
  );
1266
1438
  const prossime = [];
1267
- if (this.dati.status === "playing") prossime.push(this.dati.ultimoInputAt + INATTIVITA_MS);
1439
+ if (this.manifest.persistent === true) {
1440
+ prossime.push(
1441
+ Math.max(this.dati.ultimoInputAt, this.dati.ultimoCambioStatoAt) + SCADENZA_PERSISTENTE_MS
1442
+ );
1443
+ } else if (this.dati.status === "playing") {
1444
+ prossime.push(this.dati.ultimoInputAt + INATTIVITA_MS);
1445
+ }
1268
1446
  if (this.dati.countdownAt !== null) prossime.push(this.dati.countdownAt);
1269
1447
  for (const player of this.dati.giocatori) {
1270
1448
  if (!player.connected && player.graziaFinoA !== null) prossime.push(player.graziaFinoA);
@@ -1297,7 +1475,7 @@ var ArchivioFacet = class {
1297
1475
  }
1298
1476
  };
1299
1477
  var AdattatoreFacet = class {
1300
- constructor(archivio, tickRate) {
1478
+ constructor(archivio, tickRate, deposito) {
1301
1479
  this.connessioni = /* @__PURE__ */ new Set();
1302
1480
  this.cpuMessaggiOltreCento = 0;
1303
1481
  this.frames = [];
@@ -1307,6 +1485,7 @@ var AdattatoreFacet = class {
1307
1485
  this.salvato = "";
1308
1486
  this.storage = new ArchivioFacet(archivio);
1309
1487
  this.tickRate = tickRate;
1488
+ this.deposito = deposito;
1310
1489
  }
1311
1490
  async ripristina() {
1312
1491
  const dati = await this.storage.get(CHIAVE_ADATTATORE);
@@ -1350,13 +1529,14 @@ var AdattatoreFacet = class {
1350
1529
  async programmaSveglia(ora) {
1351
1530
  this.sveglia = ora;
1352
1531
  }
1353
- esito() {
1532
+ esito(status) {
1354
1533
  const risultato = {
1355
1534
  frames: this.frames,
1356
1535
  chiusure: this.chiusure,
1357
1536
  sveglia: this.sveglia,
1358
1537
  tick: this.tick,
1359
- tickRate: this.tickRate
1538
+ tickRate: this.tickRate,
1539
+ status
1360
1540
  };
1361
1541
  this.frames = [];
1362
1542
  this.chiusure = [];
@@ -1372,7 +1552,8 @@ function creaFacetStanza(DurableObject, definizione, manifest) {
1372
1552
  constructor(stato, ambiente) {
1373
1553
  super(stato, ambiente);
1374
1554
  this.#roomId = typeof stato.id === "string" ? stato.id : stato.id.name ?? stato.id.toString();
1375
- const adattatore = new AdattatoreFacet(stato.storage, definizione.tickRate);
1555
+ const deposito = ambiente?.DEPOSITO ?? null;
1556
+ const adattatore = new AdattatoreFacet(stato.storage, definizione.tickRate, deposito);
1376
1557
  this.#adattatore = adattatore;
1377
1558
  const misurata = Object.create(definizione);
1378
1559
  if (definizione.onMessage !== void 0) {
@@ -1402,7 +1583,7 @@ function creaFacetStanza(DurableObject, definizione, manifest) {
1402
1583
  return this.#serializza(async (nucleo) => {
1403
1584
  await operazione(nucleo);
1404
1585
  await this.#adattatore.persisti();
1405
- return this.#adattatore.esito();
1586
+ return this.#adattatore.esito(nucleo.info()?.status ?? null);
1406
1587
  });
1407
1588
  }
1408
1589
  crea(input) {
@@ -1410,14 +1591,13 @@ function creaFacetStanza(DurableObject, definizione, manifest) {
1410
1591
  const ok = await nucleo.crea(this.#roomId, input.mode, input.creator);
1411
1592
  await this.#adattatore.persisti();
1412
1593
  return {
1413
- ...this.#adattatore.esito(),
1414
- ok,
1415
- status: nucleo.info()?.status === "playing" ? "playing" : "lobby"
1594
+ ...this.#adattatore.esito(nucleo.info()?.status ?? null),
1595
+ ok
1416
1596
  };
1417
1597
  });
1418
1598
  }
1419
- info() {
1420
- return this.#serializza((nucleo) => nucleo.info());
1599
+ info(playerId) {
1600
+ return this.#serializza((nucleo) => nucleo.info(playerId));
1421
1601
  }
1422
1602
  puoEntrare(identita) {
1423
1603
  return this.#serializza((nucleo) => nucleo.puoEntrare(identita));
package/dist/server.d.ts CHANGED
@@ -4,6 +4,13 @@ export type JsonValue = null | boolean | number | string | JsonValue[] | {
4
4
  export type RoomStatus = 'lobby' | 'countdown' | 'playing' | 'ended';
5
5
  export type VoiceMode = 'none' | 'room' | 'team' | 'proximity';
6
6
  export type LeaveReason = 'left' | 'timeout' | 'kicked';
7
+ export interface SharedGameStore {
8
+ get(key: string): Promise<JsonValue | null>;
9
+ set(key: string, value: JsonValue): Promise<void>;
10
+ delete(key: string): Promise<void>;
11
+ list(prefix?: string): Promise<string[]>;
12
+ increment(key: string, amount?: number): Promise<number>;
13
+ }
7
14
  export interface Player {
8
15
  readonly id: string;
9
16
  readonly name: string;
@@ -14,18 +21,24 @@ export interface Player {
14
21
  }
15
22
  export interface Room<State extends JsonValue = JsonValue> {
16
23
  readonly id: string;
24
+ readonly seed: number;
17
25
  readonly mode: string | null;
18
26
  readonly status: RoomStatus;
19
27
  readonly tick: number;
28
+ readonly tickRate: number;
29
+ readonly result: JsonValue | null;
20
30
  state: State;
21
31
  readonly players: readonly Player[];
22
32
  readonly host: Player | null;
23
33
  broadcast(message: JsonValue): void;
24
34
  send(player: Player | string, message: JsonValue): void;
25
35
  kick(player: Player | string): void;
36
+ setRole(player: Player | string, role: string): void;
37
+ setTeam(player: Player | string, team: number): void;
26
38
  end(result: JsonValue): void;
27
39
  save(key: string, value: JsonValue): Promise<void>;
28
40
  load(key: string): Promise<JsonValue | null>;
41
+ readonly shared: SharedGameStore;
29
42
  schedule(milliseconds: number, handler: string, payload: JsonValue): void;
30
43
  readonly board: {
31
44
  submit(player: Player | string, board: string, score: number, options?: {
@@ -41,6 +54,7 @@ export interface Room<State extends JsonValue = JsonValue> {
41
54
  };
42
55
  readonly voice: {
43
56
  readonly mode: VoiceMode;
57
+ setGain(listener: Player | string, speaker: Player | string, gain: number): void;
44
58
  setProximity(a: Player | string, b: Player | string, gain: number): void;
45
59
  };
46
60
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@caisual/kit",
3
- "version": "0.3.0",
3
+ "version": "0.4.0",
4
4
  "description": "Client and server kit for Caisual games",
5
5
  "type": "module",
6
6
  "files": [
@@ -43,6 +43,7 @@
43
43
  "test": "pnpm build && vitest run && WRANGLER_LOG_PATH=/tmp/caisual-kit-wrangler.log vitest run --config vitest.workers.config.ts"
44
44
  },
45
45
  "devDependencies": {
46
+ "@caisual/contracts": "workspace:*",
46
47
  "@cloudflare/vitest-pool-workers": "^0.20.3",
47
48
  "@types/node": "^26.2.0",
48
49
  "esbuild": "^0.28.1",