@puzzmo/sdk 1.0.71 → 1.0.73

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.
Files changed (39) hide show
  1. package/README.md +20 -6
  2. package/dist/{createSimulator-uk8H5md8.cjs → createSimulator-Bb1LTB5P.cjs} +44 -44
  3. package/dist/createSimulator-Bb1LTB5P.cjs.map +1 -0
  4. package/dist/{createSimulator-treFHkmV.js → createSimulator-CYo2KWWM.js} +480 -440
  5. package/dist/createSimulator-CYo2KWWM.js.map +1 -0
  6. package/dist/index.cjs +1 -1
  7. package/dist/index.cjs.map +1 -1
  8. package/dist/index.d.ts +1 -1
  9. package/dist/index.d.ts.map +1 -1
  10. package/dist/index.global.js +1 -1
  11. package/dist/index.global.js.map +1 -1
  12. package/dist/index.js +154 -91
  13. package/dist/index.js.map +1 -1
  14. package/dist/sdk.d.ts +66 -4
  15. package/dist/sdk.d.ts.map +1 -1
  16. package/dist/sdk.gameReady.spec.d.ts +2 -0
  17. package/dist/sdk.gameReady.spec.d.ts.map +1 -0
  18. package/dist/simulator/createSimulator.d.ts.map +1 -1
  19. package/dist/simulator/index.cjs +1 -1
  20. package/dist/simulator/index.js +1 -1
  21. package/dist/simulator/standalone.cjs +1 -1
  22. package/dist/simulator/standalone.js +1 -1
  23. package/dist/simulator/state.d.ts.map +1 -1
  24. package/dist/simulator/types.d.ts +5 -0
  25. package/dist/simulator/types.d.ts.map +1 -1
  26. package/dist/simulator/views/ThumbView.d.ts.map +1 -1
  27. package/dist/thumbnailMotion.cjs +5 -0
  28. package/dist/thumbnailMotion.cjs.map +1 -0
  29. package/dist/thumbnailMotion.d.ts +119 -0
  30. package/dist/thumbnailMotion.d.ts.map +1 -0
  31. package/dist/thumbnailMotion.js +453 -0
  32. package/dist/thumbnailMotion.js.map +1 -0
  33. package/dist/thumbnailMotion.spec.d.ts +2 -0
  34. package/dist/thumbnailMotion.spec.d.ts.map +1 -0
  35. package/dist/types.d.ts +55 -0
  36. package/dist/types.d.ts.map +1 -1
  37. package/package.json +6 -1
  38. package/dist/createSimulator-treFHkmV.js.map +0 -1
  39. package/dist/createSimulator-uk8H5md8.cjs.map +0 -1
package/dist/index.js CHANGED
@@ -273,9 +273,9 @@ function S() {
273
273
  * The host<->game contract this SDK speaks, declared in READY and READY_GAME_LOADED: games read
274
274
  * `game` and `session`, and handle `session` being absent. Hosts on "1.0" always send a session.
275
275
  */
276
- var C = "1.1", w = S();
276
+ var C = "1.1", w = S(), T = 1e4;
277
277
  /** Creates a Puzzmo SDK instance for communicating with the Puzzmo host */
278
- const T = (r = {}) => {
278
+ const E = (r = {}) => {
279
279
  var i;
280
280
  let a = null, s = null, c = () => {
281
281
  var e;
@@ -284,58 +284,66 @@ const T = (r = {}) => {
284
284
  var e, t;
285
285
  return (e = (t = c()) == null ? void 0 : t.id) == null ? null : e;
286
286
  }, u = () => {
287
- var e, t;
288
- return (e = a == null || (t = a.session) == null ? void 0 : t.puzzle.puzzle) == null ? null : e;
287
+ var e, t, n, r;
288
+ return (e = (t = a == null || (n = a.session) == null ? void 0 : n.puzzle.puzzle) == null ? a == null || (r = a.repeatablePuzzle) == null ? void 0 : r.puzzle : t) == null ? null : e;
289
289
  }, p = () => {
290
290
  var e, t;
291
291
  return (e = (t = c()) == null ? void 0 : t.boardState) == null ? null : e;
292
292
  }, m = () => {
293
293
  var e;
294
294
  return (e = a == null ? void 0 : a.theme) == null ? null : e;
295
- }, h = () => {
295
+ }, h = null, g = 0, _ = !1, v = () => {
296
296
  var e, t;
297
297
  return (e = (t = c()) == null ? void 0 : t.completed) == null ? !1 : e;
298
- }, g = !1, _ = (e) => {
299
- if (g) return;
300
- g = !0;
298
+ }, b = !1, S = (e) => {
299
+ if (b) return;
300
+ b = !0;
301
301
  let t = a ? "the host sent no play session, so there is no gameplay to save to" : "it was called before gameReady() resolved";
302
302
  console.warn(`[Puzzmo SDK] ${e}() was ignored: ${t}`);
303
- }, v = /* @__PURE__ */ new Map(), b = null, S = null, T = !1, D = (e, t) => {
304
- T || S == null || S.trackEvent(e, t);
305
- }, O = x(), k = null, A = null, j = () => {
306
- k || (k = setInterval(() => {
307
- if (O.isPaused()) return;
308
- let [e, t] = O.display();
303
+ }, E = /* @__PURE__ */ new Map(), k = null, A = null, j = !1, M = (e, t) => {
304
+ j || A == null || A.trackEvent(e, t);
305
+ }, N = x(), P = null, F = null, I = () => {
306
+ P || (P = setInterval(() => {
307
+ if (N.isPaused()) return;
308
+ let [e, t] = N.display();
309
309
  w.sendMessage("TIMER_TICK", { display: [e, t] });
310
- }, 500), A = setInterval(() => {
311
- O.isPaused() || w.sendMessage("TIMER_SYNC", Math.floor(O.timeWithoutPenaltySecs()));
310
+ }, 500), F = setInterval(() => {
311
+ N.isPaused() || w.sendMessage("TIMER_SYNC", Math.floor(N.timeWithoutPenaltySecs()));
312
312
  }, 1e4));
313
- }, M = () => {
314
- k && (clearInterval(k), k = null), A && (clearInterval(A), A = null);
315
- }, N = (e, t) => {
316
- let n = v.get(e);
313
+ }, L = () => {
314
+ P && (clearInterval(P), P = null), F && (clearInterval(F), F = null);
315
+ }, R = (e, t) => {
316
+ let n = E.get(e);
317
317
  n && n.forEach((e) => e(t));
318
318
  };
319
319
  w.onMessage("START_GAME", () => {
320
- O._init(), j(), N("start");
320
+ N._init(), I(), R("start");
321
321
  });
322
- let P = () => {
323
- let e = O.isPaused();
324
- O._pause(), O.isPaused() !== e && N("pause");
325
- }, F = () => {
326
- let e = O.isPaused();
327
- O._resume(), O.isPaused() !== e && N("resume");
322
+ let z = () => {
323
+ let e = N.isPaused();
324
+ N._pause(), N.isPaused() !== e && R("pause");
325
+ }, B = () => {
326
+ let e = N.isPaused();
327
+ N._resume(), N.isPaused() !== e && R("resume");
328
328
  };
329
- w.onMessage("PAUSE_GAME", P), w.onMessage("RESUME_GAME", F), w.onMessage("SETTINGS_UPDATE", (e) => {
330
- b = t(t({}, b), e), N("settingsUpdate", b);
331
- }), w.onMessage("KEYBOARD_KEY_PRESS", (e) => N("keyboardKeyPress", e)), w.onMessage("KEYBOARD_CURSOR_CHANGE", (e) => N("keyboardCursorChange", e)), w.onMessage("KEYBOARD_CURSOR_END", () => N("keyboardCursorEnd")), w.onMessage("RETRY_PUZZLE", () => {
332
- O._reset(), M(), N("retry");
329
+ w.onMessage("PAUSE_GAME", z), w.onMessage("RESUME_GAME", B), w.onMessage("SETTINGS_UPDATE", (e) => {
330
+ k = t(t({}, k), e), R("settingsUpdate", k);
331
+ }), w.onMessage("KEYBOARD_KEY_PRESS", (e) => R("keyboardKeyPress", e)), w.onMessage("KEYBOARD_CURSOR_CHANGE", (e) => R("keyboardCursorChange", e)), w.onMessage("KEYBOARD_CURSOR_END", () => R("keyboardCursorEnd"));
332
+ let V = /* @__PURE__ */ new Map(), H = 0;
333
+ w.onMessage("PUZZLE_DATA", (e) => {
334
+ let t = V.get(e.nonce);
335
+ t && (V.delete(e.nonce), "failed" in e ? t.reject(Error(e.error)) : t.resolve({
336
+ puzzleKey: e.puzzle.key,
337
+ puzzleString: e.puzzle.puzzle
338
+ }));
339
+ }), w.onMessage("RETRY_PUZZLE", () => {
340
+ N._reset(), L(), R("retry");
333
341
  }), w.onMessage("READY_DATA", (e) => {
334
342
  var t;
335
343
  let n = o(e);
336
344
  a = n;
337
345
  let r = (t = n.session) == null ? void 0 : t.gameplay;
338
- if (b === null) {
346
+ if (k === null) {
339
347
  var i, c;
340
348
  let e = n.game.slug, t = e ? (i = n.userState) == null || (i = i.gameSettings) == null ? void 0 : i[e] : null;
341
349
  if (typeof t == "string") try {
@@ -343,61 +351,64 @@ const T = (r = {}) => {
343
351
  } catch (e) {
344
352
  t = null;
345
353
  }
346
- b = (c = t) == null ? {} : c;
354
+ k = (c = t) == null ? {} : c;
347
355
  }
348
356
  if (r) {
349
357
  var l, u;
350
358
  let e = ((l = r.elapsedTimeSecs) == null ? 0 : l) * 1e3, t = ((u = r.additionalTimeAddedSecs) == null ? 0 : u) * 1e3;
351
- O._reset(e, t);
359
+ N._reset(e, t);
352
360
  }
353
- if (!S) {
361
+ if (!A) {
354
362
  var p;
355
- S = d(n), T = (p = r == null ? void 0 : r.completed) == null ? !1 : p, S && f((e) => S.trackLinkClick(e));
363
+ A = d(n), j = (p = r == null ? void 0 : r.completed) == null ? !1 : p, A && f((e) => A.trackLinkClick(e));
356
364
  }
357
365
  s && (s(n), s = null);
358
366
  });
359
- let I = {
360
- timeMs: () => O.timeMs(),
361
- timeSecs: () => O.timeSecs(),
362
- addedTimeMs: () => O.addedTimeMs(),
363
- addedTimeSecs: () => O.addedTimeSecs(),
364
- timeWithoutPenaltySecs: () => O.timeWithoutPenaltySecs(),
365
- display: () => O.display(),
366
- addPenalty: (e) => O.addPenalty(e),
367
- setPenalty: (e) => O.setPenalty(e),
368
- isPaused: () => O.isPaused(),
369
- isRunning: () => O.isRunning(),
370
- pause: P,
371
- resume: F
372
- }, L = {
367
+ let U = {
368
+ timeMs: () => N.timeMs(),
369
+ timeSecs: () => N.timeSecs(),
370
+ addedTimeMs: () => N.addedTimeMs(),
371
+ addedTimeSecs: () => N.addedTimeSecs(),
372
+ timeWithoutPenaltySecs: () => N.timeWithoutPenaltySecs(),
373
+ display: () => N.display(),
374
+ addPenalty: (e) => N.addPenalty(e),
375
+ setPenalty: (e) => N.setPenalty(e),
376
+ isPaused: () => N.isPaused(),
377
+ isRunning: () => N.isRunning(),
378
+ pause: z,
379
+ resume: B
380
+ }, W = {
373
381
  send: (e, t) => w.sendMessage(e, t),
374
382
  on: (e, t) => w.onMessage(e, t),
375
- timer: I,
383
+ timer: U,
376
384
  bootstrap: () => a
377
- }, R = {};
378
- for (let e of (i = r.plugins) == null ? [] : i) R[e.name] = e.setup(L);
385
+ }, G = {};
386
+ for (let e of (i = r.plugins) == null ? [] : i) G[e.name] = e.setup(W);
379
387
  return {
380
- timer: I,
381
- plugins: R,
388
+ timer: U,
389
+ plugins: G,
382
390
  gameReady: function() {
383
391
  var e = n(function* () {
392
+ var e, n;
384
393
  if (w.sendMessage("READY", { runtimeContract: C }), !a) {
385
- var e;
386
- let t = (e = r.timeout) == null ? 5e3 : e;
387
- yield new Promise((e, n) => {
388
- s = e, setTimeout(() => {
389
- s && (s = null, n(/* @__PURE__ */ Error(`Timeout waiting for READY_DATA after ${t}ms`)));
390
- }, t);
394
+ var i;
395
+ let e = (i = r.timeout) == null ? 5e3 : i;
396
+ yield new Promise((t, n) => {
397
+ s = t, setTimeout(() => {
398
+ s && (s = null, n(/* @__PURE__ */ Error(`Timeout waiting for READY_DATA after ${e}ms`)));
399
+ }, e);
391
400
  });
392
401
  }
393
- let n = p();
402
+ let o = p();
394
403
  return t(t({}, {
404
+ mode: a != null && a.repeatable ? "repeatable" : "puzzle",
395
405
  puzzleString: u(),
396
- inputString: n,
406
+ puzzleKey: a != null && a.repeatable ? (e = (n = a.repeatablePuzzle) == null ? void 0 : n.key) == null ? null : e : null,
407
+ inputString: o,
397
408
  theme: m(),
398
- completed: h(),
409
+ completed: v(),
399
410
  readyData: a
400
- }), {}, { boardState: n });
411
+ }), {}, { boardState: o });
401
412
  });
402
413
  return function() {
403
414
  return e.apply(this, arguments);
@@ -408,37 +419,37 @@ const T = (r = {}) => {
408
419
  state: e,
409
420
  gameRuntimeContract: C,
410
421
  embedRuntimeContract: "1.0"
411
- }), D("READY_GAME_LOADED");
422
+ }), M("READY_GAME_LOADED");
412
423
  },
413
- on: (e, t) => (v.has(e) || v.set(e, /* @__PURE__ */ new Set()), v.get(e).add(t), () => {
424
+ on: (e, t) => (E.has(e) || E.set(e, /* @__PURE__ */ new Set()), E.get(e).add(t), () => {
414
425
  var n;
415
- (n = v.get(e)) == null || n.delete(t);
426
+ (n = E.get(e)) == null || n.delete(t);
416
427
  }),
417
428
  off: (e, t) => {
418
429
  var n;
419
- (n = v.get(e)) == null || n.delete(t);
430
+ (n = E.get(e)) == null || n.delete(t);
420
431
  },
421
432
  updateGameState: (e, t) => {
422
433
  var n, r;
423
434
  let i = l();
424
- if (!i) return _("updateGameState");
435
+ if (!i) return S("updateGameState");
425
436
  w.sendMessage("UPLOAD_NEW_GAME_STATE", {
426
437
  id: i,
427
438
  input: {
428
439
  boardState: e,
429
- elapsedTimeSecs: (n = t == null ? void 0 : t.elapsedTimeSecs) == null ? O.timeWithoutPenaltySecs() : n,
430
- additionalTimeAddedSecs: (r = t == null ? void 0 : t.additionalTimeAddedSecs) == null ? O.addedTimeSecs() : r,
440
+ elapsedTimeSecs: (n = t == null ? void 0 : t.elapsedTimeSecs) == null ? N.timeWithoutPenaltySecs() : n,
441
+ additionalTimeAddedSecs: (r = t == null ? void 0 : t.additionalTimeAddedSecs) == null ? N.addedTimeSecs() : r,
431
442
  collabUserReferences: []
432
443
  }
433
- }), D("UPLOAD_NEW_GAME_STATE");
444
+ }), M("UPLOAD_NEW_GAME_STATE");
434
445
  },
435
446
  gameCompleted: (n, r) => {
436
447
  var i, o, s, c, u;
437
- O._conclude(), M();
448
+ N._conclude(), L();
438
449
  let { inputString: d } = n, f = t(t({}, e(n, y)), {}, {
439
450
  boardState: d == null ? n.boardState : d,
440
- elapsedTimeSecs: (i = n.elapsedTimeSecs) == null ? O.timeWithoutPenaltySecs() : i,
441
- additionalTimeAddedSecs: (o = n.additionalTimeAddedSecs) == null ? O.addedTimeSecs() : o
451
+ elapsedTimeSecs: (i = n.elapsedTimeSecs) == null ? N.timeWithoutPenaltySecs() : i,
452
+ additionalTimeAddedSecs: (o = n.additionalTimeAddedSecs) == null ? N.addedTimeSecs() : o
442
453
  }), p = (s = r == null ? void 0 : r.deeds) == null ? [] : s;
443
454
  p.push({
444
455
  id: "points",
@@ -452,7 +463,7 @@ const T = (r = {}) => {
452
463
  id: m == null ? void 0 : m,
453
464
  input: f,
454
465
  config: r
455
- }), D("GAME_COMPLETED", { input: f })) : _("gameCompleted");
466
+ }), M("GAME_COMPLETED", { input: f })) : S("gameCompleted");
456
467
  },
457
468
  showCompletionScreen: (e) => {
458
469
  w.sendMessage("SHOW_GAME_COMPLETE_SCREEN", {
@@ -462,10 +473,10 @@ const T = (r = {}) => {
462
473
  },
463
474
  hitCheckpoint: (e, t, n) => {
464
475
  var r;
465
- if (!l()) return _("hitCheckpoint");
476
+ if (!l()) return S("hitCheckpoint");
466
477
  let i = (r = p()) == null ? "" : r, a = {
467
- elapsedTimeSecs: O.timeWithoutPenaltySecs(),
468
- additionalTimeAddedSecs: O.addedTimeSecs()
478
+ elapsedTimeSecs: N.timeWithoutPenaltySecs(),
479
+ additionalTimeAddedSecs: N.addedTimeSecs()
469
480
  };
470
481
  w.sendMessage("HIT_CHECKPOINT", {
471
482
  checkpointName: e,
@@ -477,16 +488,66 @@ const T = (r = {}) => {
477
488
  augConfig: n == null ? {} : n
478
489
  });
479
490
  },
491
+ completeRepeatableRun: (e, n) => {
492
+ var r, i;
493
+ let a = N.timeWithoutPenaltySecs(), o = a >= g ? a - g : a;
494
+ g = a;
495
+ let s = Math.round((r = e == null ? void 0 : e.elapsedTimeSecs) == null ? o : r), c = [...((i = n == null ? void 0 : n.deeds) == null ? [] : i).filter((e) => e.id !== "time"), {
496
+ id: "time",
497
+ value: s
498
+ }], l = e == null ? void 0 : e.puzzleKey;
499
+ w.sendMessage("COMPLETE_REPEATABLE_RUN", { input: t({
500
+ deedValues: c,
501
+ elapsedTimeSecs: s
502
+ }, l ? { puzzleKey: l } : {}) }), M("COMPLETE_REPEATABLE_RUN", { elapsedTimeSecs: s });
503
+ },
504
+ requestPuzzle: (e) => new Promise((t, n) => {
505
+ let r = `puzzle-${++H}`, i = setTimeout(() => {
506
+ V.delete(r) && n(/* @__PURE__ */ Error(`Timeout waiting for PUZZLE_DATA after ${T}ms`));
507
+ }, T), a = (e) => (t) => {
508
+ clearTimeout(i), e(t);
509
+ };
510
+ V.set(r, {
511
+ resolve: a(t),
512
+ reject: a(n)
513
+ }), w.sendMessage("REQUEST_PUZZLE", {
514
+ nonce: r,
515
+ buckets: e != null && e.buckets ? [...e.buckets] : void 0,
516
+ exclude: e != null && e.exclude ? [...e.exclude] : void 0
517
+ });
518
+ }),
519
+ storage: {
520
+ get: () => {
521
+ if (h) return h;
522
+ let e = a == null ? void 0 : a.viewerStorage;
523
+ return e && e.state != null ? {
524
+ state: e.state,
525
+ stateVersion: e.stateVersion
526
+ } : null;
527
+ },
528
+ set: (e, t = 1) => {
529
+ let n = new TextEncoder().encode(e).length;
530
+ if (n > O) throw Error(`[sdk.storage] State is ${n} bytes, over the ${O} byte cap`);
531
+ if (!Number.isInteger(t) || t < 1) throw Error("[sdk.storage] stateVersion must be a positive integer");
532
+ a && !a.repeatable && !_ && (_ = !0, console.warn("[sdk.storage] Only repeat play saves storage for now; this write lasts until the page reloads")), h = {
533
+ state: e,
534
+ stateVersion: t
535
+ }, w.sendMessage("UPDATE_GAME_USER_STATE", {
536
+ state: e,
537
+ stateVersion: t
538
+ });
539
+ }
540
+ },
480
541
  settings: {
481
- initialize: (e) => (b = t(t({}, E(e)), b), w.sendMessage("INITIALIZE_SETTINGS", {
542
+ initialize: (e) => (k = t(t({}, D(e)), k), w.sendMessage("INITIALIZE_SETTINGS", {
482
543
  components: e,
483
- settings: b
484
- }), b),
544
+ settings: k
545
+ }), k),
485
546
  get: () => {
486
547
  var e;
487
- return (e = b) == null ? {} : e;
548
+ return (e = k) == null ? {} : e;
488
549
  },
489
- update: (e) => (b = t(t({}, b), e), w.sendMessage("UPDATE_SETTINGS_FROM_EMBED", { settings: b }), b)
550
+ update: (e) => (k = t(t({}, k), e), w.sendMessage("UPDATE_SETTINGS_FROM_EMBED", { settings: k }), k)
490
551
  },
491
552
  keyboard: {
492
553
  show: (e) => {
@@ -515,11 +576,13 @@ const T = (r = {}) => {
515
576
  };
516
577
  };
517
578
  /** Walks settings UI components (recursing into split groups) collecting name → defaultValue pairs */
518
- function E(e) {
579
+ function D(e) {
519
580
  let t = {};
520
- for (let n of e) n.type === "split" ? Object.assign(t, E(n.content)) : "name" in n && (t[n.name] = n.defaultValue);
581
+ for (let n of e) n.type === "split" ? Object.assign(t, D(n.content)) : "name" in n && (t[n.name] = n.defaultValue);
521
582
  return t;
522
583
  }
584
+ /** The largest state `sdk.storage.set()` takes, matching the API's cap */
585
+ var O = 64 * 1024;
523
586
  /**
524
587
  * A standard QWERTY layout with Enter and Backspace — a reasonable default for
525
588
  * any game that needs text input. Customize from here by spreading and overriding.
@@ -532,7 +595,7 @@ function E(e) {
532
595
  * // Extend with dynamic disabled letters
533
596
  * sdk.keyboard.show({ ...defaultKeyboardConfig, disabled: usedLetters })
534
597
  */
535
- const D = {
598
+ const k = {
536
599
  layout: [
537
600
  "qwertyuiop",
538
601
  "asdfghjkl",
@@ -548,7 +611,7 @@ const D = {
548
611
  xl: [],
549
612
  l: ["↵", "⌫"],
550
613
  supportsDragCursor: !1
551
- }, O = (e) => {
614
+ }, A = (e) => {
552
615
  let n = {};
553
616
  for (let a of e) for (let e of a.keys) {
554
617
  var r, i;
@@ -558,11 +621,11 @@ const D = {
558
621
  return n;
559
622
  };
560
623
  /** Custom error class for workshop import failures */
561
- var k = class extends Error {
624
+ var j = class extends Error {
562
625
  constructor(e, t, n) {
563
626
  super(t), this.type = e, this.originalError = n, this.name = "EditorImportError";
564
627
  }
565
628
  };
566
- export { k as EditorImportError, T as createPuzzmoSDK, D as defaultKeyboardConfig, O as keyStylesFromRules };
629
+ export { j as EditorImportError, E as createPuzzmoSDK, k as defaultKeyboardConfig, A as keyStylesFromRules };
567
630
 
568
631
  //# sourceMappingURL=index.js.map