@puzzmo/sdk 1.0.86 → 1.0.88

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/index.js CHANGED
@@ -302,12 +302,12 @@ const D = (r = {}) => {
302
302
  }, m = () => {
303
303
  var e;
304
304
  return (e = a == null ? void 0 : a.theme) == null ? null : e;
305
- }, h = null, g = 0, _ = 0, v = !1, b = !1, x = () => {
305
+ }, h = null, g = 0, _ = null, v = null, b = 0, x = !1, C = !1, D = () => {
306
306
  var e, t;
307
307
  return (e = (t = c()) == null ? void 0 : t.completed) == null ? !1 : e;
308
- }, C = !1, D = (e) => {
309
- if (C) return;
310
- C = !0;
308
+ }, A = !1, j = (e) => {
309
+ if (A) return;
310
+ A = !0;
311
311
  let t = a ? "the host sent no play session, so there is no gameplay to save to" : "it was called before gameReady() resolved";
312
312
  console.warn(`[Puzzmo SDK] ${e}() was ignored: ${t}`);
313
313
  }, M = /* @__PURE__ */ new Map(), N = null, P = [], F = {}, I = () => t(t({}, N), F), L = null, R = !1, z = (e, t) => {
@@ -327,27 +327,52 @@ const D = (r = {}) => {
327
327
  n && n.forEach((e) => e(t));
328
328
  };
329
329
  T.onMessage("START_GAME", () => {
330
- B._init(), U(), G("start");
330
+ B._init(), U(), G("start"), q();
331
331
  });
332
- let K = () => {
332
+ let ne = () => {
333
333
  let e = B.isPaused();
334
334
  B._pause(), B.isPaused() !== e && G("pause");
335
- }, q = () => {
335
+ }, re = () => {
336
336
  let e = B.isPaused();
337
337
  B._resume(), B.isPaused() !== e && G("resume");
338
+ }, K = (e) => {
339
+ var n;
340
+ let r = B.timeWithoutPenaltySecs(), i = (n = _) == null ? g : n;
341
+ return t({
342
+ secs: Math.max(0, Math.floor(r >= i ? r - i : r)),
343
+ running: !B.isPaused()
344
+ }, e ? { stat: {
345
+ value: e.value,
346
+ label: e.label
347
+ } } : {});
348
+ }, q = () => {
349
+ v && T.sendMessage("UPDATE_TABLE_GAME_STATE", {
350
+ inputString: v.inputString,
351
+ status: K(v.stat)
352
+ });
353
+ }, J = () => {
354
+ ne(), q();
355
+ }, Y = () => {
356
+ re(), q();
338
357
  };
339
- T.onMessage("PAUSE_GAME", K), T.onMessage("RESUME_GAME", q), T.onMessage("SETTINGS_UPDATE", (e) => {
358
+ T.onMessage("PAUSE_GAME", J), T.onMessage("RESUME_GAME", Y), T.onMessage("SETTINGS_UPDATE", (e) => {
340
359
  N = t(t({}, N), e), G("settingsUpdate", I());
341
360
  }), T.onMessage("KEYBOARD_KEY_PRESS", (e) => G("keyboardKeyPress", e)), T.onMessage("KEYBOARD_CURSOR_CHANGE", (e) => G("keyboardCursorChange", e)), T.onMessage("KEYBOARD_CURSOR_END", () => G("keyboardCursorEnd")), T.onMessage("ACHIEVEMENTS_UNLOCKED", (e) => G("achievementsUnlocked", e)), T.onMessage("REPEATABLE_ROUND_LOST", () => {
342
- v && (v = !1, G("roundLost"));
361
+ x && (x = !1, G("roundLost"));
343
362
  });
344
- let J = /* @__PURE__ */ new Map(), Y = 0;
363
+ let X = /* @__PURE__ */ new Map(), ie = 0;
345
364
  T.onMessage("PUZZLE_DATA", (e) => {
346
- let t = J.get(e.nonce);
347
- t && (J.delete(e.nonce), "failed" in e ? t.reject(Error(e.error)) : t.resolve({
365
+ let t = X.get(e.nonce);
366
+ t && (X.delete(e.nonce), "failed" in e ? t.reject(Error(e.error)) : t.resolve({
348
367
  puzzleKey: e.puzzle.key,
349
368
  puzzleString: e.puzzle.puzzle
350
369
  }));
370
+ });
371
+ let Z = null, ae = 0;
372
+ T.onMessage("CONFIRM_ANSWER", (e) => {
373
+ if (!Z || Z.nonce !== e.nonce) return;
374
+ let { resolve: t } = Z;
375
+ Z = null, t(e.confirmed === !0);
351
376
  }), T.onMessage("RETRY_PUZZLE", () => {
352
377
  B._reset(), W(), G("retry");
353
378
  }), T.onMessage("READY_DATA", (e) => {
@@ -376,7 +401,7 @@ const D = (r = {}) => {
376
401
  }
377
402
  s && (s(n), s = null);
378
403
  });
379
- let X = {
404
+ let Q = {
380
405
  timeMs: () => B.timeMs(),
381
406
  timeSecs: () => B.timeSecs(),
382
407
  addedTimeMs: () => B.addedTimeMs(),
@@ -387,19 +412,19 @@ const D = (r = {}) => {
387
412
  setPenalty: (e) => B.setPenalty(e),
388
413
  isPaused: () => B.isPaused(),
389
414
  isRunning: () => B.isRunning(),
390
- pause: K,
391
- resume: q
392
- }, Z = {
415
+ pause: J,
416
+ resume: Y
417
+ }, oe = {
393
418
  send: (e, t) => T.sendMessage(e, t),
394
419
  on: (e, t) => T.onMessage(e, t),
395
420
  onSend: T.onSend,
396
- timer: X,
421
+ timer: Q,
397
422
  bootstrap: () => a
398
- }, Q = {};
399
- for (let e of (i = r.plugins) == null ? [] : i) Q[e.name] = e.setup(Z);
423
+ }, $ = {};
424
+ for (let e of (i = r.plugins) == null ? [] : i) $[e.name] = e.setup(oe);
400
425
  return {
401
- timer: X,
402
- plugins: Q,
426
+ timer: Q,
427
+ plugins: $,
403
428
  gameReady: function() {
404
429
  var e = n(function* () {
405
430
  var e, n;
@@ -419,7 +444,7 @@ const D = (r = {}) => {
419
444
  puzzleKey: a != null && a.repeatable ? (e = (n = a.repeatablePuzzle) == null ? void 0 : n.key) == null ? null : e : null,
420
445
  inputString: o,
421
446
  theme: m(),
422
- completed: x(),
447
+ completed: D(),
423
448
  readyData: a
424
449
  }), {}, { boardState: o });
425
450
  });
@@ -445,7 +470,7 @@ const D = (r = {}) => {
445
470
  updateGameState: (e, t) => {
446
471
  var n, r;
447
472
  let i = l();
448
- if (!i) return D("updateGameState");
473
+ if (!i) return j("updateGameState");
449
474
  T.sendMessage("UPLOAD_NEW_GAME_STATE", {
450
475
  id: i,
451
476
  input: {
@@ -476,7 +501,7 @@ const D = (r = {}) => {
476
501
  id: m == null ? void 0 : m,
477
502
  input: f,
478
503
  config: r
479
- }), z("GAME_COMPLETED", { input: f })) : D("gameCompleted");
504
+ }), z("GAME_COMPLETED", { input: f })) : j("gameCompleted");
480
505
  },
481
506
  showCompletionScreen: (e) => {
482
507
  T.sendMessage("SHOW_GAME_COMPLETE_SCREEN", {
@@ -486,7 +511,7 @@ const D = (r = {}) => {
486
511
  },
487
512
  hitCheckpoint: (e, t, n) => {
488
513
  var r;
489
- if (!l()) return D("hitCheckpoint");
514
+ if (!l()) return j("hitCheckpoint");
490
515
  let i = (r = p()) == null ? "" : r, a = {
491
516
  elapsedTimeSecs: B.timeWithoutPenaltySecs(),
492
517
  additionalTimeAddedSecs: B.addedTimeSecs()
@@ -516,10 +541,13 @@ const D = (r = {}) => {
516
541
  },
517
542
  rounds: {
518
543
  start: (e) => {
519
- _ = 0, v = !1, T.sendMessage("START_REPEATABLE_ROUND", t({ reason: e.reason }, e.puzzleKey ? { puzzleKey: e.puzzleKey } : {}));
544
+ b = 0, _ = B.timeWithoutPenaltySecs(), x = !1, T.sendMessage("START_REPEATABLE_ROUND", t({ reason: e.reason }, e.puzzleKey ? { puzzleKey: e.puzzleKey } : {}));
520
545
  },
521
546
  resume: (e) => {
522
- _ = e.events.length, v = !0, T.sendMessage("RESUME_REPEATABLE_ROUND", t({ events: e.events.map((e) => [...e]) }, e.puzzleKey ? { puzzleKey: e.puzzleKey } : {}));
547
+ var n;
548
+ b = e.events.length;
549
+ let r = Number((n = e.events[e.events.length - 1]) == null ? void 0 : n[0]) || 0;
550
+ _ = B.timeWithoutPenaltySecs() - r / 1e3, x = !0, T.sendMessage("RESUME_REPEATABLE_ROUND", t({ events: e.events.map((e) => [...e]) }, e.puzzleKey ? { puzzleKey: e.puzzleKey } : {}));
523
551
  },
524
552
  inProgress: () => {
525
553
  let e = a == null ? void 0 : a.repeatableOpenRound;
@@ -532,21 +560,21 @@ const D = (r = {}) => {
532
560
  },
533
561
  log: (...e) => {
534
562
  e.length && (T.sendMessage("APPEND_REPEATABLE_ROUND_LOG", {
535
- from: _,
563
+ from: b,
536
564
  events: e.map((e) => [...e])
537
- }), _ += e.length);
565
+ }), b += e.length);
538
566
  }
539
567
  },
540
568
  table: { updateGameState: (e, n) => {
541
- T.sendMessage("UPDATE_TABLE_GAME_STATE", t({ inputString: e }, (n == null ? void 0 : n.puzzleString) === void 0 ? {} : { puzzleString: n.puzzleString }));
569
+ v = t({ inputString: e }, n != null && n.stat ? { stat: n.stat } : {}), T.sendMessage("UPDATE_TABLE_GAME_STATE", t(t({ inputString: e }, (n == null ? void 0 : n.puzzleString) === void 0 ? {} : { puzzleString: n.puzzleString }), {}, { status: K(n == null ? void 0 : n.stat) }));
542
570
  } },
543
571
  requestPuzzle: (e) => new Promise((t, n) => {
544
- let r = `puzzle-${++Y}`, i = setTimeout(() => {
545
- J.delete(r) && n(/* @__PURE__ */ Error(`Timeout waiting for PUZZLE_DATA after ${E}ms`));
572
+ let r = `puzzle-${++ie}`, i = setTimeout(() => {
573
+ X.delete(r) && n(/* @__PURE__ */ Error(`Timeout waiting for PUZZLE_DATA after ${E}ms`));
546
574
  }, E), a = (e) => (t) => {
547
575
  clearTimeout(i), e(t);
548
576
  };
549
- J.set(r, {
577
+ X.set(r, {
550
578
  resolve: a(t),
551
579
  reject: a(n)
552
580
  }), T.sendMessage("REQUEST_PUZZLE", {
@@ -555,6 +583,31 @@ const D = (r = {}) => {
555
583
  exclude: e != null && e.exclude ? [...e.exclude] : void 0
556
584
  });
557
585
  }),
586
+ canConfirm: () => {
587
+ var e;
588
+ return (a == null || (e = a.hostDialogs) == null ? void 0 : e.confirm) === !0;
589
+ },
590
+ confirm: (e) => new Promise((t) => {
591
+ var n;
592
+ if ((a == null || (n = a.hostDialogs) == null ? void 0 : n.confirm) !== !0) {
593
+ console.warn("[Puzzmo SDK] confirm() was answered no: this host draws no confirm dialog, check canConfirm() first"), t(!1);
594
+ return;
595
+ }
596
+ Z == null || Z.resolve(!1);
597
+ let r = `confirm-${++ae}`;
598
+ Z = {
599
+ nonce: r,
600
+ resolve: t
601
+ };
602
+ let { title: i, text: o, confirmLabel: s, cancelLabel: c } = e;
603
+ T.sendMessage("REQUEST_CONFIRM", {
604
+ nonce: r,
605
+ title: i,
606
+ text: o,
607
+ confirmLabel: s,
608
+ cancelLabel: c
609
+ });
610
+ }),
558
611
  storage: {
559
612
  get: () => {
560
613
  if (h) return h;
@@ -566,9 +619,9 @@ const D = (r = {}) => {
566
619
  },
567
620
  set: (e, t = 1) => {
568
621
  let n = new TextEncoder().encode(e).length;
569
- if (n > j) throw Error(`[sdk.storage] State is ${n} bytes, over the ${j} byte cap`);
622
+ if (n > k) throw Error(`[sdk.storage] State is ${n} bytes, over the ${k} byte cap`);
570
623
  if (!Number.isInteger(t) || t < 1) throw Error("[sdk.storage] stateVersion must be a positive integer");
571
- a && !a.repeatable && !b && (b = !0, console.warn("[sdk.storage] Only repeat play saves storage for now; this write lasts until the page reloads")), h = {
624
+ a && !a.repeatable && !C && (C = !0, console.warn("[sdk.storage] Only repeat play saves storage for now; this write lasts until the page reloads")), h = {
572
625
  state: e,
573
626
  stateVersion: t
574
627
  }, T.sendMessage("UPDATE_GAME_USER_STATE", {
@@ -579,7 +632,7 @@ const D = (r = {}) => {
579
632
  },
580
633
  settings: {
581
634
  initialize: (e) => {
582
- let n = A(e, k(a));
635
+ let n = te(e, ee(a));
583
636
  P = n.components, F = n.locked, N = t(t({}, O(P)), N);
584
637
  let r = I();
585
638
  return T.sendMessage("INITIALIZE_SETTINGS", {
@@ -627,7 +680,7 @@ function O(e) {
627
680
  return t;
628
681
  }
629
682
  /** The settings overrides an embed's host context holds for this game, under the game's slug */
630
- function k(e) {
683
+ function ee(e) {
631
684
  var t, n;
632
685
  let r = e == null || (t = e.hostContext) == null ? void 0 : t.find((e) => e.type === "embed"), i = e == null || (n = e.game) == null ? void 0 : n.slug;
633
686
  if (!r || !i) return [];
@@ -638,7 +691,7 @@ function k(e) {
638
691
  * Applies an embed's overrides to the components they match by `id`, recursing into split groups.
639
692
  * `omit` marks the component `hidden`, and `locked` holds the value each omitted setting is kept at.
640
693
  */
641
- function A(n, r) {
694
+ function te(n, r) {
642
695
  let i = {};
643
696
  if (!r.length) return {
644
697
  components: n,
@@ -655,7 +708,7 @@ function A(n, r) {
655
708
  };
656
709
  }
657
710
  /** The largest state `sdk.storage.set()` takes, matching the API's cap */
658
- var j = 64 * 1024;
711
+ var k = 64 * 1024;
659
712
  /**
660
713
  * A standard QWERTY layout with Enter and Backspace — a reasonable default for
661
714
  * any game that needs text input. Customize from here by spreading and overriding.
@@ -668,7 +721,7 @@ var j = 64 * 1024;
668
721
  * // Extend with dynamic disabled letters
669
722
  * sdk.keyboard.show({ ...defaultKeyboardConfig, disabled: usedLetters })
670
723
  */
671
- const M = {
724
+ const A = {
672
725
  layout: [
673
726
  "qwertyuiop",
674
727
  "asdfghjkl",
@@ -684,7 +737,7 @@ const M = {
684
737
  xl: [],
685
738
  l: ["↵", "⌫"],
686
739
  supportsDragCursor: !1
687
- }, N = (e) => {
740
+ }, j = (e) => {
688
741
  let n = {};
689
742
  for (let a of e) for (let e of a.keys) {
690
743
  var r, i;
@@ -694,11 +747,11 @@ const M = {
694
747
  return n;
695
748
  };
696
749
  /** Custom error class for workshop import failures */
697
- var P = class extends Error {
750
+ var M = class extends Error {
698
751
  constructor(e, t, n) {
699
752
  super(t), this.type = e, this.originalError = n, this.name = "EditorImportError";
700
753
  }
701
754
  };
702
- export { P as EditorImportError, D as createPuzzmoSDK, M as defaultKeyboardConfig, N as keyStylesFromRules };
755
+ export { M as EditorImportError, D as createPuzzmoSDK, A as defaultKeyboardConfig, j as keyStylesFromRules };
703
756
 
704
757
  //# sourceMappingURL=index.js.map