@pretty-chitty/core 2.3.1 → 2.3.5

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 (44) hide show
  1. package/dist/components/FullScreenGalleryDisplay.d.ts.map +1 -1
  2. package/dist/components/FullScreenGalleryDisplay.js +1 -1
  3. package/dist/components/FullScreenGalleryDisplay.js.map +1 -1
  4. package/dist/components/Gallery/GalleryViewer.d.ts +6 -1
  5. package/dist/components/Gallery/GalleryViewer.d.ts.map +1 -1
  6. package/dist/components/Gallery/GalleryViewer.js +69 -14
  7. package/dist/components/Gallery/GalleryViewer.js.map +1 -1
  8. package/dist/game/Chit.d.ts.map +1 -1
  9. package/dist/game/Chit.js +12 -2
  10. package/dist/game/Chit.js.map +1 -1
  11. package/dist/game/OrderedOutlet.d.ts +1 -1
  12. package/dist/game/OrderedOutlet.d.ts.map +1 -1
  13. package/dist/game/OrderedOutlet.js +3 -1
  14. package/dist/game/OrderedOutlet.js.map +1 -1
  15. package/dist/game/Turn.d.ts +11 -0
  16. package/dist/game/Turn.d.ts.map +1 -1
  17. package/dist/game/Turn.js +13 -2
  18. package/dist/game/Turn.js.map +1 -1
  19. package/dist/game/tutorial/TutorialController.js +2 -2
  20. package/dist/game/tutorial/TutorialController.js.map +1 -1
  21. package/dist/game/tutorial/allowed.js +32 -8
  22. package/dist/game/tutorial/allowed.js.map +1 -1
  23. package/dist/game/tutorial/defineTutorial.d.ts +11 -0
  24. package/dist/game/tutorial/defineTutorial.d.ts.map +1 -1
  25. package/dist/game/tutorial/defineTutorial.js.map +1 -1
  26. package/dist/utilities/CanvasStack/CanvasStack.d.ts +12 -0
  27. package/dist/utilities/CanvasStack/CanvasStack.d.ts.map +1 -1
  28. package/dist/utilities/CanvasStack/CanvasStack.js +87 -0
  29. package/dist/utilities/CanvasStack/CanvasStack.js.map +1 -1
  30. package/package.json +1 -1
  31. package/src/library/components/FullScreenGalleryDisplay.tsx +1 -0
  32. package/src/library/components/Gallery/GalleryViewer.tsx +73 -13
  33. package/src/library/game/Chit.test.ts +50 -0
  34. package/src/library/game/Chit.ts +13 -3
  35. package/src/library/game/OrderedOutlet.test.ts +28 -0
  36. package/src/library/game/OrderedOutlet.ts +3 -1
  37. package/src/library/game/Turn.ts +14 -2
  38. package/src/library/game/serialize.test.ts +39 -0
  39. package/src/library/game/tutorial/TUTORIAL_GUIDE.md +6 -0
  40. package/src/library/game/tutorial/TutorialController.ts +2 -2
  41. package/src/library/game/tutorial/allowed.test.ts +67 -0
  42. package/src/library/game/tutorial/allowed.ts +34 -8
  43. package/src/library/game/tutorial/defineTutorial.ts +11 -0
  44. package/src/library/utilities/CanvasStack/CanvasStack.ts +85 -0
@@ -0,0 +1,67 @@
1
+ import { describe, expect, test } from "vitest";
2
+ import { Chit } from "../Chit";
3
+ import { PlayerChit } from "../PlayerChit";
4
+ import { RootChit } from "../RootChit";
5
+ import { AllowedAction } from "./defineTutorial";
6
+ import { isAllowed, MoveShape } from "./allowed";
7
+
8
+ class Spot extends Chit {
9
+ constructor(
10
+ public x = 0,
11
+ public y = 0,
12
+ ) {
13
+ super();
14
+ }
15
+ }
16
+
17
+ type R = RootChit<PlayerChit>;
18
+ const root = new RootChit<PlayerChit>();
19
+ const me = new PlayerChit();
20
+ const piece = new Spot();
21
+ const near = new Spot(12, 0);
22
+ const far = new Spot(3, 4);
23
+ const drag = (target: Spot | undefined, key = "place.drag"): MoveShape => ({
24
+ kind: "drag",
25
+ key,
26
+ chitId: "piece",
27
+ targetId: "spot",
28
+ chit: piece,
29
+ target,
30
+ });
31
+
32
+ describe("isAllowed with chitIs and targetIs", () => {
33
+ const allow: AllowedAction<PlayerChit, R> = {
34
+ key: "place.drag",
35
+ targetIs: (t: Spot) => t.x === 12 && t.y === 0,
36
+ };
37
+
38
+ test("targetIs tests the target actually dropped on", () => {
39
+ expect(isAllowed(allow, drag(near), root, me)).toBe(true);
40
+ expect(isAllowed(allow, drag(far), root, me)).toBe(false);
41
+ });
42
+
43
+ test("other fields still have to match", () => {
44
+ expect(isAllowed(allow, drag(near, "other.drag"), root, me)).toBe(false);
45
+ });
46
+
47
+ test("a move whose target could not be looked up is refused, as is a tap", () => {
48
+ expect(isAllowed(allow, drag(undefined), root, me)).toBe(false);
49
+ expect(isAllowed(allow, { kind: "chit", key: "place.drag", chitId: "spot", chit: near }, root, me)).toBe(false);
50
+ });
51
+
52
+ test("a test that throws refuses the move", () => {
53
+ const throwing: AllowedAction<PlayerChit, R> = {
54
+ targetIs: () => {
55
+ throw new Error("boom");
56
+ },
57
+ };
58
+ expect(isAllowed(throwing, drag(near), root, me)).toBe(false);
59
+ });
60
+
61
+ test("chitIs tests the chit played, for taps and drags; never for buttons", () => {
62
+ const red: AllowedAction<PlayerChit, R> = { chitIs: (c: Spot) => c.x === 12 };
63
+ expect(isAllowed(red, { kind: "chit", chitId: "spot", chit: near }, root, me)).toBe(true);
64
+ expect(isAllowed(red, { kind: "chit", chitId: "spot", chit: far }, root, me)).toBe(false);
65
+ expect(isAllowed(red, { kind: "button", buttonLabel: "Done" }, root, me)).toBe(false);
66
+ });
67
+ });
@@ -13,19 +13,34 @@ export type MoveShape = {
13
13
  chitId?: string;
14
14
  targetId?: string;
15
15
  buttonLabel?: string;
16
+ /** The played chit and the drop target themselves, when known: what `chitIs` and `targetIs` test */
17
+ chit?: Chit;
18
+ target?: Chit;
16
19
  };
17
20
 
18
- /** @internal */
19
- export function shapeOfPromptAction(action: PromptAction): MoveShape | undefined {
21
+ /** `find` looks the played chits up by id (the view's own copies), for `chitIs` and `targetIs`. @internal */
22
+ export function shapeOfPromptAction(
23
+ action: PromptAction,
24
+ find?: (id: string) => Chit | undefined,
25
+ ): MoveShape | undefined {
20
26
  if (action.type !== "pick") return undefined;
21
27
  const pick: Pick = action.pick;
22
28
  const key = pick.aiTag?.k;
29
+ const lookUp = (id: string | undefined) => (id && find ? find(id) : undefined);
23
30
  if (pick instanceof ChitPick) {
24
- return { kind: "chit", key, chitId: action.value as string };
31
+ const chitId = action.value as string;
32
+ return { kind: "chit", key, chitId, chit: lookUp(chitId) };
25
33
  }
26
34
  if (pick instanceof DragPick) {
27
35
  const v = action.value as { chitId: string; targetChitId: string };
28
- return { kind: "drag", key, chitId: v?.chitId, targetId: v?.targetChitId };
36
+ return {
37
+ kind: "drag",
38
+ key,
39
+ chitId: v?.chitId,
40
+ targetId: v?.targetChitId,
41
+ chit: lookUp(v?.chitId),
42
+ target: lookUp(v?.targetChitId),
43
+ };
29
44
  }
30
45
  if (pick instanceof ButtonPick) {
31
46
  return { kind: "button", key, buttonLabel: pick.button?.label };
@@ -37,9 +52,9 @@ export function shapeOfPromptAction(action: PromptAction): MoveShape | undefined
37
52
  export function shapeOfAiOption(o: AiOption): MoveShape {
38
53
  switch (o.kind) {
39
54
  case "chit":
40
- return { kind: "chit", key: o.key, chitId: o.chit.id };
55
+ return { kind: "chit", key: o.key, chitId: o.chit.id, chit: o.chit };
41
56
  case "drag":
42
- return { kind: "drag", key: o.key, chitId: o.chit.id, targetId: o.target.id };
57
+ return { kind: "drag", key: o.key, chitId: o.chit.id, targetId: o.target.id, chit: o.chit, target: o.target };
43
58
  case "button":
44
59
  return { kind: "button", key: o.key, buttonLabel: o.button.label };
45
60
  }
@@ -55,12 +70,23 @@ export function isAllowed<P extends PlayerChit, R extends RootChit<P>>(
55
70
  if (!allow) return false;
56
71
  const list = Array.isArray(allow) ? allow : [allow];
57
72
  const ids = (sel: ChitSelector<P, R>) => new Set(selectChits<P, R>(sel, root, me).map((c: Chit) => c.id));
73
+ // A test of the chit actually played: false when there is none, or when the test throws
74
+ const passes = (test: (chit: Chit, root: R, me: P) => boolean, chit: Chit | undefined) => {
75
+ if (!chit || !root || !me) return false;
76
+ try {
77
+ return !!test(chit, root, me);
78
+ } catch {
79
+ return false;
80
+ }
81
+ };
58
82
  return list.some((a) => {
59
83
  if (a === "any") return true;
60
84
  if (a.ui !== undefined) return false; // a UI control, never a game move
61
85
  if (a.key !== undefined && move.key !== a.key) return false;
62
86
  if (a.chit && (move.kind === "button" || !ids(a.chit).has(move.chitId))) return false;
63
87
  if (a.target && (move.kind !== "drag" || !ids(a.target).has(move.targetId))) return false;
88
+ if (a.chitIs && (move.kind === "button" || !passes(a.chitIs, move.chit))) return false;
89
+ if (a.targetIs && (move.kind !== "drag" || !passes(a.targetIs, move.target))) return false;
64
90
  if (
65
91
  a.button !== undefined &&
66
92
  (move.kind !== "button" || (move.buttonLabel ?? "").toLowerCase() !== a.button.toLowerCase())
@@ -102,7 +128,7 @@ export function promptMoves(prompt: PickPrompt): PromptMove[] {
102
128
  const key = pick.aiTag?.k;
103
129
  if (pick instanceof ChitPick) {
104
130
  for (const chit of pick.chits as Chit[]) {
105
- if (chit.id) moves.push({ pick, value: chit.id, shape: { kind: "chit", key, chitId: chit.id } });
131
+ if (chit.id) moves.push({ pick, value: chit.id, shape: { kind: "chit", key, chitId: chit.id, chit } });
106
132
  }
107
133
  } else if (pick instanceof DragPick) {
108
134
  const targets = pick.dropTargets.flatMap((t) => t.chits as Chit[]);
@@ -112,7 +138,7 @@ export function promptMoves(prompt: PickPrompt): PromptMove[] {
112
138
  moves.push({
113
139
  pick,
114
140
  value: { chitId: chit.id, targetChitId: target.id },
115
- shape: { kind: "drag", key, chitId: chit.id, targetId: target.id },
141
+ shape: { kind: "drag", key, chitId: chit.id, targetId: target.id, chit, target },
116
142
  });
117
143
  }
118
144
  }
@@ -217,6 +217,17 @@ export type AllowedAction<P extends PlayerChit, R extends RootChit<P>> =
217
217
  chit?: ChitSelector<P, R>;
218
218
  /** For drags: where it may be dropped */
219
219
  target?: ChitSelector<P, R>;
220
+ /**
221
+ * For taps and drags: a test of the chit the player actually played. Use it when the chit is easier
222
+ * to recognise than to find (`(c: Worker) => c.color === "red"`). With `chit` as well, both must pass.
223
+ */
224
+ chitIs?(chit: Chit, root: R, me: P): boolean;
225
+ /**
226
+ * For drags: a test of the target the player actually dropped on (`(t: Square) => t.x === 3 && t.y === 0`).
227
+ * Nothing has to be found in the tree first, so it also works where a list on the client is incomplete.
228
+ * With `target` as well, both must pass.
229
+ */
230
+ targetIs?(target: Chit, root: R, me: P): boolean;
220
231
  /** For buttons (the engine's Confirm included): the label */
221
232
  button?: string;
222
233
  /**
@@ -84,6 +84,7 @@ export class CanvasStack implements IUpdatingCanvas {
84
84
  }
85
85
 
86
86
  this._outlets = {};
87
+ this._fontsUsed.clear();
87
88
  this.context.clearRect(0, 0, this.width, this.height);
88
89
  this.operation.render(
89
90
  this.context,
@@ -93,6 +94,7 @@ export class CanvasStack implements IUpdatingCanvas {
93
94
  this._outlets[id] = coords;
94
95
  },
95
96
  );
97
+ this._noteUnloadedFonts();
96
98
  this._renderMipCanvases();
97
99
  this.cbs.forEach((cb) => cb());
98
100
  }
@@ -132,6 +134,7 @@ export class CanvasStack implements IUpdatingCanvas {
132
134
  throw "Invalid Context";
133
135
  }
134
136
  this.context = context;
137
+ this._trackFonts(context);
135
138
 
136
139
  CanvasStack._liveStacks.add(this);
137
140
  CanvasStack.ensureContextRestoreSubscription();
@@ -145,6 +148,81 @@ export class CanvasStack implements IUpdatingCanvas {
145
148
  return this.operation.benefitsFromMipMap() ? 2 : 0;
146
149
  }
147
150
 
151
+ // Webfonts load lazily, so a face drawn before its font arrives is rasterised in the fallback font, and nothing
152
+ // redrew it. Its custom mips, built a second or more later, came out in the real font, and trilinear filtering
153
+ // blended the two layouts into doubled, ghosted text. So each stack records the fonts it measured or drew with;
154
+ // any not loaded yet get loaded, and when one arrives every stack that used it redraws (base and custom mips) and
155
+ // re-uploads its texture, a few per frame.
156
+ private _fontsUsed = new Set<string>();
157
+ private _unloadedFonts = new Set<string>();
158
+ private static _fontLoads = new Set<string>();
159
+ private static _pendingFontRedraws = new Set<CanvasStack>();
160
+ private static _fontRedrawScheduled = false;
161
+
162
+ private _trackFonts(ctx: CanvasRenderingContext2D) {
163
+ for (const name of ["fillText", "strokeText", "measureText"] as const) {
164
+ const original = ctx[name] as (...args: unknown[]) => unknown;
165
+ (ctx as any)[name] = (...args: unknown[]) => {
166
+ this._fontsUsed.add(ctx.font);
167
+ return original.apply(ctx, args);
168
+ };
169
+ }
170
+ }
171
+
172
+ private _noteUnloadedFonts() {
173
+ this._unloadedFonts.clear();
174
+ const fonts = typeof document !== "undefined" ? document.fonts : undefined;
175
+ if (!fonts) return;
176
+ for (const font of this._fontsUsed) {
177
+ if (fonts.check(font)) continue;
178
+ this._unloadedFonts.add(font);
179
+ CanvasStack.whenFontLoads(font);
180
+ }
181
+ }
182
+
183
+ /** A font this stack drew with before it loaded has loaded since: its pixels are stale. */
184
+ private _fontsArrived(): boolean {
185
+ for (const font of this._unloadedFonts) if (document.fonts.check(font)) return true;
186
+ return false;
187
+ }
188
+
189
+ private static whenFontLoads(font: string) {
190
+ if (CanvasStack._fontLoads.has(font)) return; // once per font: a font that never loads doesn't loop
191
+ CanvasStack._fontLoads.add(font);
192
+ document.fonts.load(font).then(
193
+ () => {
194
+ for (const stack of CanvasStack._liveStacks) {
195
+ if (stack._unloadedFonts.has(font)) CanvasStack._pendingFontRedraws.add(stack);
196
+ }
197
+ if (CanvasStack._pendingFontRedraws.size > 0 && !CanvasStack._fontRedrawScheduled) {
198
+ CanvasStack._fontRedrawScheduled = true;
199
+ requestAnimationFrame(CanvasStack.redrawPendingFonts);
200
+ }
201
+ },
202
+ () => {},
203
+ );
204
+ }
205
+
206
+ private static redrawPendingFonts = () => {
207
+ const start = performance.now();
208
+ for (const stack of CanvasStack._pendingFontRedraws) {
209
+ CanvasStack._pendingFontRedraws.delete(stack);
210
+ stack._redrawForFonts();
211
+ if (performance.now() - start > CanvasStack.MIP_FRAME_BUDGET_MS) break;
212
+ }
213
+ if (CanvasStack._pendingFontRedraws.size > 0) requestAnimationFrame(CanvasStack.redrawPendingFonts);
214
+ else CanvasStack._fontRedrawScheduled = false;
215
+ };
216
+
217
+ private _redrawForFonts() {
218
+ if (!this.context) return;
219
+ this.render(); // redraws the custom mips too, when they're built
220
+ if (this._texture) {
221
+ this._texture.needsUpdate = true;
222
+ CanvasStack.disposer.notifyChange(this._texture.uuid);
223
+ }
224
+ }
225
+
148
226
  // Faces with text get hand-drawn mip levels: text rasterised at the small size is sharper than GPU box-filtering.
149
227
  // Building that chain (a canvas per level, down to 1×1) for every face while the table loads is what stalled
150
228
  // phones — Safari queues 2D drawing in its GPU process and the first texture upload waits for all of it (8 s on an
@@ -182,6 +260,9 @@ export class CanvasStack implements IUpdatingCanvas {
182
260
 
183
261
  private _buildMips() {
184
262
  this._initMipCanvases();
263
+ // the base was drawn in a fallback font that has loaded since: redraw it (and these mips) in the real one,
264
+ // or the chain would hold two different layouts
265
+ if (this._fontsArrived()) this.render();
185
266
  if (this._texture) {
186
267
  this._applyMipmaps(this._texture);
187
268
  this._texture.needsUpdate = true;
@@ -290,6 +371,10 @@ export class CanvasStack implements IUpdatingCanvas {
290
371
  }
291
372
 
292
373
  if (!this._texture) {
374
+ // Rebuilt after the disposer reclaimed it: the disposer also dropped the stack from the live set, so a font
375
+ // that arrived meanwhile never redrew it. Redraw now, and rejoin the live set for later fonts and context loss.
376
+ if (this._fontsArrived()) this.render();
377
+ CanvasStack._liveStacks.add(this);
293
378
  const wrapper = getWebGlRendererInstance();
294
379
  this._texture = new Texture(
295
380
  this.canvas,