@caisual/kit 0.10.0 → 0.13.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/node.js CHANGED
@@ -94,6 +94,9 @@ var LimiteMessaggiStanza = class {
94
94
  };
95
95
 
96
96
  // src/daily.ts
97
+ function prossimaMezzanotteUtc(ora) {
98
+ return (Math.floor(ora / 864e5) + 1) * 864e5;
99
+ }
97
100
  function giornoUtc(ora) {
98
101
  return new Date(ora).toISOString().slice(0, 10);
99
102
  }
@@ -341,6 +344,7 @@ var GRAZIA_MS = 6e4;
341
344
  var STANZA_VUOTA_MS = 5 * 6e4;
342
345
  var ATTESA_RIVINCITA_MS = 2 * 6e4;
343
346
  var COUNTDOWN_MS = 3e3;
347
+ var GRAZIA_GIORNALIERA_MS = 10 * 6e4;
344
348
  var RIPOSO_TICK_MS = 3e4;
345
349
  var INATTIVITA_MS = 10 * 6e4;
346
350
  var SCADENZA_PERSISTENTE_MS = 30 * 24 * 60 * 6e4;
@@ -403,10 +407,13 @@ var NucleoStanza = class _NucleoStanza {
403
407
  const nucleo = this;
404
408
  const daily = {
405
409
  get day() {
406
- return nucleo.giornata();
410
+ return nucleo.richiediDati().daily.day;
407
411
  },
408
412
  get seed() {
409
- return seedGiornata(nucleo.manifest.id, nucleo.giornata());
413
+ return nucleo.richiediDati().daily.seed;
414
+ },
415
+ get expiresAt() {
416
+ return nucleo.richiediDati().daily.expiresAt;
410
417
  }
411
418
  };
412
419
  this.room = {
@@ -461,7 +468,7 @@ var NucleoStanza = class _NucleoStanza {
461
468
  nucleo.impostaSquadraDalServer(idGiocatore(player), team);
462
469
  },
463
470
  end(result, options) {
464
- nucleo.richiediFine(result, options?.rematch === true);
471
+ nucleo.richiediFine(result, options?.rematch ?? false);
465
472
  },
466
473
  save(key, value) {
467
474
  return nucleo.salva(key, value);
@@ -491,7 +498,11 @@ var NucleoStanza = class _NucleoStanza {
491
498
  },
492
499
  board: {
493
500
  submit(player, board, score, options = {}) {
494
- nucleo.accodaPunteggio(idGiocatore(player), board, score, options.daily === true);
501
+ if (typeof board === "number") {
502
+ const active = nucleo.richiediDati().giocatori.filter((p) => p.role !== "spectator");
503
+ if (nucleo.configurazione.players.max !== 1 || active.length !== 1) throw erroreConCodice("board_player_required", "Specify the player when submitting a multiplayer score.");
504
+ nucleo.accodaPunteggio(active[0].id, player, board, typeof score === "object" ? score : {});
505
+ } else nucleo.accodaPunteggio(idGiocatore(player), board, score, options);
495
506
  }
496
507
  },
497
508
  daily,
@@ -522,7 +533,8 @@ var NucleoStanza = class _NucleoStanza {
522
533
  if (salvato !== void 0) {
523
534
  nucleo.dati = salvato;
524
535
  nucleo.tickRateSincronizzato = salvato.tickRate;
525
- const daAggiornare = salvato.ultimoInputAt === void 0 || salvato.ultimoCambioStatoAt === void 0;
536
+ const daAggiornare = salvato.ultimoInputAt === void 0 || salvato.ultimoCambioStatoAt === void 0 || salvato.daily === void 0;
537
+ salvato.daily ??= nucleo.contestoGiornaliero();
526
538
  salvato.ultimoInputAt ??= adattatore.ora();
527
539
  salvato.ultimoCambioStatoAt ??= salvato.ultimoInputAt;
528
540
  nucleo.ultimoStatoOsservato = JSON.stringify(salvato.state);
@@ -554,6 +566,7 @@ var NucleoStanza = class _NucleoStanza {
554
566
  const ora = this.adattatore.ora();
555
567
  this.dati = {
556
568
  versione: 1,
569
+ daily: this.contestoGiornaliero(ora),
557
570
  id,
558
571
  mode,
559
572
  status: "lobby",
@@ -596,7 +609,8 @@ var NucleoStanza = class _NucleoStanza {
596
609
  return true;
597
610
  }
598
611
  get configurazione() {
599
- return risolviModalita(this.manifest, this.dati?.mode ?? null);
612
+ const mode = risolviModalita(this.manifest, this.dati?.mode ?? null);
613
+ return mode.players.max === 1 ? { ...mode, lobby: false } : mode;
600
614
  }
601
615
  info(playerId) {
602
616
  if (this.dati === null) return null;
@@ -919,7 +933,7 @@ var NucleoStanza = class _NucleoStanza {
919
933
  else await this.rimuoviGiocatore(player, "timeout");
920
934
  }
921
935
  if (this.dati.status === "countdown" && this.dati.countdownAt !== null && this.dati.countdownAt <= ora) {
922
- const errore = this.erroreMinimi();
936
+ const errore = this.erroreMinimi(this.dati.countdownRivincita);
923
937
  if (errore !== null) {
924
938
  this.dati.status = "lobby";
925
939
  this.dati.countdownAt = null;
@@ -1087,13 +1101,13 @@ var NucleoStanza = class _NucleoStanza {
1087
1101
  if (player === void 0) throw erroreConCodice("player_not_found", "Player not found.");
1088
1102
  return player;
1089
1103
  }
1090
- erroreMinimi() {
1104
+ erroreMinimi(saltaPronti = false) {
1091
1105
  const connessi = this.richiediDati().giocatori.filter((player) => player.connected);
1092
1106
  const attivi = connessi.filter((player) => player.role !== "spectator");
1093
1107
  if (attivi.length < this.configurazione.players.min) {
1094
1108
  return { code: "not_enough_players", message: "The room does not have enough players." };
1095
1109
  }
1096
- if (connessi.some((player) => !player.ready)) {
1110
+ if (!saltaPronti && connessi.some((player) => !player.ready)) {
1097
1111
  return { code: "players_not_ready", message: "Every connected player must be ready." };
1098
1112
  }
1099
1113
  for (const role of this.manifest.roles) {
@@ -1115,31 +1129,48 @@ var NucleoStanza = class _NucleoStanza {
1115
1129
  this.inviaErrore(player, "rematch_unavailable", "This room is not waiting for a rematch.");
1116
1130
  return;
1117
1131
  }
1132
+ if (this.configurazione.players.max === 1) {
1133
+ await this.avviaRivincita();
1134
+ return;
1135
+ }
1118
1136
  if (player.role !== "spectator" && !player.ready) {
1119
1137
  player.ready = true;
1120
1138
  this.inviaGiocatori();
1121
- await this.persistiEProgramma();
1139
+ if (dati.rivincita?.autoStart && this.prontiRivincita()) await this.avviaRivincita();
1140
+ else await this.persistiEProgramma();
1122
1141
  return;
1123
1142
  }
1124
1143
  if (dati.hostId !== player.id) return;
1125
- const attivi = dati.giocatori.filter((item) => item.connected && item.role !== "spectator");
1126
- if (attivi.length < this.configurazione.players.min || attivi.some((item) => !item.ready)) {
1144
+ if (!this.prontiRivincita(dati.rivincita?.autoStart === true)) {
1127
1145
  this.inviaErrore(player, "players_not_ready", "Enough connected players must be ready for the rematch.");
1128
1146
  return;
1129
1147
  }
1130
- dati.status = this.configurazione.lobby ? "lobby" : "playing";
1148
+ await this.avviaRivincita();
1149
+ }
1150
+ prontiRivincita(anticipata = false) {
1151
+ const attivi = this.richiediDati().giocatori.filter((item) => item.connected && item.role !== "spectator");
1152
+ return attivi.filter((item) => item.ready).length >= this.configurazione.players.min && (anticipata || attivi.every((item) => item.ready));
1153
+ }
1154
+ async avviaRivincita() {
1155
+ const dati = this.richiediDati(), solo = this.configurazione.players.max === 1;
1156
+ const keepSetup = dati.rivincita?.keepSetup === true && !solo;
1157
+ dati.status = solo ? "playing" : keepSetup ? "countdown" : this.configurazione.lobby ? "lobby" : "playing";
1158
+ dati.countdownRivincita = keepSetup;
1159
+ dati.countdownAt = keepSetup ? this.adattatore.ora() + COUNTDOWN_MS : null;
1131
1160
  dati.rivincitaFinoA = null;
1161
+ dati.rivincita = null;
1132
1162
  dati.result = null;
1133
1163
  dati.resultAt = null;
1134
1164
  dati.ultimoInputAt = this.adattatore.ora();
1135
- for (const item of dati.giocatori) item.ready = false;
1165
+ for (const item of dati.giocatori) item.ready = keepSetup && (dati.prontiPrecedenti?.includes(item.id) ?? false);
1166
+ dati.prontiPrecedenti = [];
1136
1167
  await this.chiama(this.definizione.onRestart, this.room);
1137
1168
  await this.applicaAzioni();
1138
1169
  if (dati.status === "playing") {
1139
1170
  await this.chiama(this.definizione.onStart, this.room);
1140
1171
  }
1141
1172
  await this.concludiEvento();
1142
- if (dati.status === "lobby" || dati.status === "playing") {
1173
+ if (dati.status === "lobby" || dati.status === "playing" || dati.status === "countdown") {
1143
1174
  this.inviaSnapshotTutti();
1144
1175
  this.inviaGiocatori();
1145
1176
  this.inviaStatus(this.adattatore.ora());
@@ -1158,13 +1189,14 @@ var NucleoStanza = class _NucleoStanza {
1158
1189
  return;
1159
1190
  }
1160
1191
  dati.status = "countdown";
1192
+ dati.countdownRivincita = false;
1161
1193
  dati.countdownAt = this.adattatore.ora() + COUNTDOWN_MS;
1162
1194
  this.inviaStatus(dati.countdownAt);
1163
1195
  await this.persistiEProgramma();
1164
1196
  }
1165
1197
  verificaCountdown() {
1166
1198
  const dati = this.richiediDati();
1167
- if (dati.status !== "countdown" || this.erroreMinimi() === null) return;
1199
+ if (dati.status !== "countdown" || this.erroreMinimi(dati.countdownRivincita) === null) return;
1168
1200
  dati.status = "lobby";
1169
1201
  dati.countdownAt = null;
1170
1202
  this.inviaStatus(this.adattatore.ora());
@@ -1233,7 +1265,11 @@ var NucleoStanza = class _NucleoStanza {
1233
1265
  giornata(ora = this.adattatore.ora()) {
1234
1266
  return this.adattatore.giorno?.(ora) ?? giornoUtc2(ora);
1235
1267
  }
1236
- accodaPunteggio(playerId, board, score, daily) {
1268
+ contestoGiornaliero(ora = this.adattatore.ora()) {
1269
+ const day = this.giornata(ora);
1270
+ return { day, seed: seedGiornata(this.manifest.id, day), expiresAt: prossimaMezzanotteUtc(ora) };
1271
+ }
1272
+ accodaPunteggio(playerId, board, score, options) {
1237
1273
  if (!this.richiediDati().giocatori.some((player2) => player2.id === playerId)) {
1238
1274
  throw new Error("Player not found.");
1239
1275
  }
@@ -1244,7 +1280,19 @@ var NucleoStanza = class _NucleoStanza {
1244
1280
  throw new TypeError("Score must be a non-negative safe integer.");
1245
1281
  }
1246
1282
  const submittedAt = this.adattatore.ora();
1247
- const day = daily ? this.giornata(submittedAt) : null;
1283
+ const daily = options.daily === true || options.day !== void 0;
1284
+ let day = daily ? this.giornata(submittedAt) : null;
1285
+ const start = this.manifest.boards?.[board]?.day === "start";
1286
+ if (options.day !== void 0 && (!start || options.daily === false)) {
1287
+ throw erroreConCodice("board_day_policy", "An explicit day requires a start-day board and a daily score.");
1288
+ }
1289
+ if (daily && start) {
1290
+ const run = this.richiediDati().daily;
1291
+ if (options.day === void 0) throw erroreConCodice("board_day_required", "Submit the daily score with room.daily.day.");
1292
+ if (options.day !== run.day) throw erroreConCodice("board_day_mismatch", "The score day must match room.daily.day.");
1293
+ if (submittedAt > run.expiresAt + GRAZIA_GIORNALIERA_MS) throw erroreConCodice("board_day_expired", "The starting day grace period has expired.");
1294
+ day = run.day;
1295
+ }
1248
1296
  this.richiediDati().punteggi.push({
1249
1297
  playerId,
1250
1298
  board,
@@ -1432,6 +1480,7 @@ var NucleoStanza = class _NucleoStanza {
1432
1480
  seed: seedStanza(dati.id),
1433
1481
  status: dati.status,
1434
1482
  result: dati.result,
1483
+ rematch: dati.status === "finished" ? dati.rivincita ?? null : null,
1435
1484
  mode: dati.mode,
1436
1485
  tick: dati.tick,
1437
1486
  tickRate: dati.tickRate,
@@ -1453,6 +1502,7 @@ var NucleoStanza = class _NucleoStanza {
1453
1502
  const dati = this.richiediDati();
1454
1503
  this.broadcast({
1455
1504
  t: "status",
1505
+ rematch: dati.status === "finished" ? dati.rivincita ?? null : null,
1456
1506
  status: dati.status,
1457
1507
  host: dati.hostId,
1458
1508
  countdownAt: dati.countdownAt,
@@ -1525,6 +1575,10 @@ var NucleoStanza = class _NucleoStanza {
1525
1575
  async concludiEvento() {
1526
1576
  await this.applicaAzioni();
1527
1577
  if (this.dati === null || this.dati.status === "ended") return;
1578
+ if (this.dati.status === "finished" && this.dati.rivincita?.autoStart && this.prontiRivincita()) {
1579
+ await this.avviaRivincita();
1580
+ return;
1581
+ }
1528
1582
  this.inviaGuadagniCambiati();
1529
1583
  const stato = await this.verificaStato();
1530
1584
  if (stato === null || this.stanzaTerminata()) return;
@@ -1620,6 +1674,11 @@ var NucleoStanza = class _NucleoStanza {
1620
1674
  if (dati.status === "ended") return;
1621
1675
  const giaFinita = dati.status === "finished";
1622
1676
  dati.status = rematch ? "finished" : "ended";
1677
+ dati.rivincita = rematch ? {
1678
+ keepSetup: typeof rematch === "object" && rematch.keepSetup === true,
1679
+ autoStart: typeof rematch === "object" && rematch.autoStart === true
1680
+ } : null;
1681
+ dati.prontiPrecedenti = rematch ? dati.giocatori.filter((p) => p.ready).map((p) => p.id) : [];
1623
1682
  dati.rivincitaFinoA = rematch ? this.adattatore.ora() + ATTESA_RIVINCITA_MS : null;
1624
1683
  dati.timer = [];
1625
1684
  if (rematch) for (const player of dati.giocatori) player.ready = false;