@pretty-chitty/core 1.2.18 → 2.0.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.
Files changed (139) hide show
  1. package/dist/components/ClientTrustMatchViewer.d.ts +14 -1
  2. package/dist/components/ClientTrustMatchViewer.d.ts.map +1 -1
  3. package/dist/components/ClientTrustMatchViewer.js +25 -2
  4. package/dist/components/ClientTrustMatchViewer.js.map +1 -1
  5. package/dist/components/DemoWrapper.d.ts.map +1 -1
  6. package/dist/components/DemoWrapper.js +4 -11
  7. package/dist/components/DemoWrapper.js.map +1 -1
  8. package/dist/components/Gallery/BuiltItem.d.ts.map +1 -1
  9. package/dist/components/Gallery/BuiltItem.js +3 -0
  10. package/dist/components/Gallery/BuiltItem.js.map +1 -1
  11. package/dist/components/Gallery/GalleryController.d.ts.map +1 -1
  12. package/dist/components/Gallery/GalleryController.js +3 -1
  13. package/dist/components/Gallery/GalleryController.js.map +1 -1
  14. package/dist/components/Playground.d.ts.map +1 -1
  15. package/dist/components/Playground.js +37 -11
  16. package/dist/components/Playground.js.map +1 -1
  17. package/dist/components/TokenizedMessage.d.ts +2 -6
  18. package/dist/components/TokenizedMessage.d.ts.map +1 -1
  19. package/dist/components/TokenizedMessage.js +1 -35
  20. package/dist/components/TokenizedMessage.js.map +1 -1
  21. package/dist/components/TopBarPlayers.d.ts.map +1 -1
  22. package/dist/components/TopBarPlayers.js +1 -1
  23. package/dist/components/TopBarPlayers.js.map +1 -1
  24. package/dist/game/Chit.js +2 -2
  25. package/dist/game/Chit.js.map +1 -1
  26. package/dist/game/ConnectionTransport.d.ts +3 -0
  27. package/dist/game/ConnectionTransport.d.ts.map +1 -1
  28. package/dist/game/ConnectionTransport.js +13 -4
  29. package/dist/game/ConnectionTransport.js.map +1 -1
  30. package/dist/game/Game.d.ts +8 -1
  31. package/dist/game/Game.d.ts.map +1 -1
  32. package/dist/game/Match.d.ts +66 -0
  33. package/dist/game/Match.d.ts.map +1 -1
  34. package/dist/game/Match.js +151 -3
  35. package/dist/game/Match.js.map +1 -1
  36. package/dist/game/MatchStorage.d.ts +8 -1
  37. package/dist/game/MatchStorage.d.ts.map +1 -1
  38. package/dist/game/MatchStorage.js +11 -3
  39. package/dist/game/MatchStorage.js.map +1 -1
  40. package/dist/game/Pick.d.ts +14 -0
  41. package/dist/game/Pick.d.ts.map +1 -1
  42. package/dist/game/Pick.js +15 -0
  43. package/dist/game/Pick.js.map +1 -1
  44. package/dist/game/PlayerChit.d.ts +3 -0
  45. package/dist/game/PlayerChit.d.ts.map +1 -1
  46. package/dist/game/PlayerChit.js +3 -0
  47. package/dist/game/PlayerChit.js.map +1 -1
  48. package/dist/game/PlayerInfo.d.ts +8 -0
  49. package/dist/game/PlayerInfo.d.ts.map +1 -1
  50. package/dist/game/PlayerInfo.js +6 -3
  51. package/dist/game/PlayerInfo.js.map +1 -1
  52. package/dist/game/PlayerPromptStatusChit.d.ts.map +1 -1
  53. package/dist/game/PlayerPromptStatusChit.js +3 -1
  54. package/dist/game/PlayerPromptStatusChit.js.map +1 -1
  55. package/dist/game/Turn.d.ts +5 -0
  56. package/dist/game/Turn.d.ts.map +1 -1
  57. package/dist/game/Turn.js +18 -7
  58. package/dist/game/Turn.js.map +1 -1
  59. package/dist/game/TurnState.d.ts +8 -0
  60. package/dist/game/TurnState.d.ts.map +1 -1
  61. package/dist/game/TurnState.js +7 -0
  62. package/dist/game/TurnState.js.map +1 -1
  63. package/dist/game/ai/AiClientPrompts.d.ts +160 -0
  64. package/dist/game/ai/AiClientPrompts.d.ts.map +1 -0
  65. package/dist/game/ai/AiClientPrompts.js +525 -0
  66. package/dist/game/ai/AiClientPrompts.js.map +1 -0
  67. package/dist/game/ai/AiLiveView.d.ts +2 -0
  68. package/dist/game/ai/AiLiveView.d.ts.map +1 -0
  69. package/dist/game/ai/AiLiveView.js +48 -0
  70. package/dist/game/ai/AiLiveView.js.map +1 -0
  71. package/dist/game/ai/AiPlayerView.d.ts +2 -0
  72. package/dist/game/ai/AiPlayerView.d.ts.map +1 -0
  73. package/dist/game/ai/AiPlayerView.js +89 -0
  74. package/dist/game/ai/AiPlayerView.js.map +1 -0
  75. package/dist/game/ai/connectAiPlayer.d.ts +43 -0
  76. package/dist/game/ai/connectAiPlayer.d.ts.map +1 -0
  77. package/dist/game/ai/connectAiPlayer.js +58 -0
  78. package/dist/game/ai/connectAiPlayer.js.map +1 -0
  79. package/dist/game/ai/defineAi.d.ts +119 -0
  80. package/dist/game/ai/defineAi.d.ts.map +1 -0
  81. package/dist/game/ai/defineAi.js +122 -0
  82. package/dist/game/ai/defineAi.js.map +1 -0
  83. package/dist/game/ai/simulateMatches.d.ts +62 -0
  84. package/dist/game/ai/simulateMatches.d.ts.map +1 -0
  85. package/dist/game/ai/simulateMatches.js +108 -0
  86. package/dist/game/ai/simulateMatches.js.map +1 -0
  87. package/dist/game/badAiTransport/BadAIClientPrompts.d.ts +3 -0
  88. package/dist/game/badAiTransport/BadAIClientPrompts.d.ts.map +1 -1
  89. package/dist/game/badAiTransport/BadAIClientPrompts.js +3 -0
  90. package/dist/game/badAiTransport/BadAIClientPrompts.js.map +1 -1
  91. package/dist/hooks/useTokenMap.d.ts +2 -4
  92. package/dist/hooks/useTokenMap.d.ts.map +1 -1
  93. package/dist/hooks/useTokenMap.js +2 -5
  94. package/dist/hooks/useTokenMap.js.map +1 -1
  95. package/dist/index.d.ts +21 -1
  96. package/dist/index.d.ts.map +1 -1
  97. package/dist/index.js +8 -1
  98. package/dist/index.js.map +1 -1
  99. package/dist/utilities/CanvasStack/CanvasStack.d.ts +11 -0
  100. package/dist/utilities/CanvasStack/CanvasStack.d.ts.map +1 -1
  101. package/dist/utilities/CanvasStack/CanvasStack.js +62 -15
  102. package/dist/utilities/CanvasStack/CanvasStack.js.map +1 -1
  103. package/dist/utilities/TokenizedText.d.ts +49 -0
  104. package/dist/utilities/TokenizedText.d.ts.map +1 -0
  105. package/dist/utilities/TokenizedText.js +47 -0
  106. package/dist/utilities/TokenizedText.js.map +1 -0
  107. package/package.json +1 -1
  108. package/src/library/components/ClientTrustMatchViewer.tsx +32 -2
  109. package/src/library/components/DemoWrapper.tsx +4 -12
  110. package/src/library/components/Gallery/BuiltItem.ts +4 -0
  111. package/src/library/components/Gallery/GalleryController.ts +3 -1
  112. package/src/library/components/Playground.tsx +77 -12
  113. package/src/library/components/TokenizedMessage.tsx +2 -63
  114. package/src/library/components/TopBarPlayers.tsx +6 -1
  115. package/src/library/game/Chit.ts +2 -2
  116. package/src/library/game/ConnectionTransport.ts +12 -3
  117. package/src/library/game/Game.ts +9 -1
  118. package/src/library/game/Match.ts +162 -3
  119. package/src/library/game/MatchStorage.ts +14 -4
  120. package/src/library/game/Pick.ts +20 -0
  121. package/src/library/game/PlayerChit.ts +4 -1
  122. package/src/library/game/PlayerInfo.ts +13 -3
  123. package/src/library/game/PlayerPromptStatusChit.ts +3 -1
  124. package/src/library/game/Turn.ts +19 -7
  125. package/src/library/game/TurnState.ts +8 -0
  126. package/src/library/game/ai/AI_GUIDE.md +434 -0
  127. package/src/library/game/ai/AiClientPrompts.ts +681 -0
  128. package/src/library/game/ai/AiLiveView.ts +63 -0
  129. package/src/library/game/ai/AiPlayerView.ts +112 -0
  130. package/src/library/game/ai/ai.test.ts +475 -0
  131. package/src/library/game/ai/connectAiPlayer.ts +88 -0
  132. package/src/library/game/ai/defineAi.ts +230 -0
  133. package/src/library/game/ai/simulateMatches.ts +172 -0
  134. package/src/library/game/badAiTransport/BadAIClientPrompts.ts +3 -0
  135. package/src/library/hooks/useTokenMap.ts +2 -6
  136. package/src/library/index.ts +34 -2
  137. package/src/library/utilities/CanvasStack/CanvasStack.ts +68 -28
  138. package/src/library/utilities/TokenizedText.test.ts +32 -0
  139. package/src/library/utilities/TokenizedText.ts +97 -0
@@ -105,6 +105,7 @@ export class CanvasStack implements IUpdatingCanvas {
105
105
  this._mipCanvases = [];
106
106
  this._mipContexts = [];
107
107
  CanvasStack._liveStacks.delete(this);
108
+ CanvasStack._pendingMips.delete(this);
108
109
 
109
110
  if (this._material) {
110
111
  this._material.dispose();
@@ -136,7 +137,7 @@ export class CanvasStack implements IUpdatingCanvas {
136
137
  CanvasStack.ensureContextRestoreSubscription();
137
138
 
138
139
  this.render();
139
- this._initMipCanvases();
140
+ if (this.mipMaps > 0) CanvasStack.scheduleMips(this);
140
141
  }
141
142
 
142
143
  get mipMaps(): number {
@@ -144,6 +145,63 @@ export class CanvasStack implements IUpdatingCanvas {
144
145
  return this.operation.benefitsFromMipMap() ? 2 : 0;
145
146
  }
146
147
 
148
+ // Faces with text get hand-drawn mip levels: text rasterised at the small size is sharper than GPU box-filtering.
149
+ // Building that chain (a canvas per level, down to 1×1) for every face while the table loads is what stalled
150
+ // phones — Safari queues 2D drawing in its GPU process and the first texture upload waits for all of it (8 s on an
151
+ // iPhone for ~950 canvases). So every texture starts with GPU-generated mipmaps, and once canvas creation has gone
152
+ // quiet the faces that benefit get their custom chain a few per frame, swapped in by a re-upload. Plain images
153
+ // (mipMaps 0) never used the chain: the GPU builds theirs.
154
+ private static _pendingMips = new Set<CanvasStack>();
155
+ private static _mipTimer: ReturnType<typeof setTimeout> | undefined;
156
+ private static _firstPendingAt = 0;
157
+ private static readonly MIP_QUIET_MS = 1000; // build once no new canvas has appeared for this long…
158
+ private static readonly MIP_MAX_WAIT_MS = 5000; // …or this long after the first one started waiting
159
+ private static readonly MIP_FRAME_BUDGET_MS = 6;
160
+
161
+ private static scheduleMips(stack: CanvasStack) {
162
+ if (CanvasStack._pendingMips.size === 0) CanvasStack._firstPendingAt = Date.now();
163
+ CanvasStack._pendingMips.add(stack);
164
+ if (CanvasStack._mipTimer) clearTimeout(CanvasStack._mipTimer);
165
+ const waited = Date.now() - CanvasStack._firstPendingAt;
166
+ const delay = Math.max(0, Math.min(CanvasStack.MIP_QUIET_MS, CanvasStack.MIP_MAX_WAIT_MS - waited));
167
+ CanvasStack._mipTimer = setTimeout(() => {
168
+ CanvasStack._mipTimer = undefined;
169
+ requestAnimationFrame(CanvasStack.buildPendingMips);
170
+ }, delay);
171
+ }
172
+
173
+ private static buildPendingMips = () => {
174
+ const start = performance.now();
175
+ for (const stack of CanvasStack._pendingMips) {
176
+ CanvasStack._pendingMips.delete(stack);
177
+ if (stack.canvas) stack._buildMips();
178
+ if (performance.now() - start > CanvasStack.MIP_FRAME_BUDGET_MS) break;
179
+ }
180
+ if (CanvasStack._pendingMips.size > 0) requestAnimationFrame(CanvasStack.buildPendingMips);
181
+ };
182
+
183
+ private _buildMips() {
184
+ this._initMipCanvases();
185
+ if (this._texture) {
186
+ this._applyMipmaps(this._texture);
187
+ this._texture.needsUpdate = true;
188
+ CanvasStack.disposer.notifyChange(this._texture.uuid);
189
+ }
190
+ }
191
+
192
+ /** Custom levels once the chain is built (same level count as GPU generation, so the re-upload reuses the storage). */
193
+ private _applyMipmaps(texture: Texture) {
194
+ if (this._mipCanvases.length > 0) {
195
+ texture.generateMipmaps = false;
196
+ // mipmaps[i] is uploaded as WebGL level i. Level 0 must be the base
197
+ // canvas; levels 1..N are the custom-rendered smaller canvases.
198
+ texture.mipmaps = [this.canvas!, ...this._mipCanvases];
199
+ } else {
200
+ texture.generateMipmaps = true;
201
+ texture.mipmaps = [];
202
+ }
203
+ }
204
+
147
205
  private _initMipCanvases() {
148
206
  this._mipCanvases = [];
149
207
  this._mipContexts = [];
@@ -233,33 +291,15 @@ export class CanvasStack implements IUpdatingCanvas {
233
291
 
234
292
  if (!this._texture) {
235
293
  const wrapper = getWebGlRendererInstance();
236
- if (this.mipMaps > 0) {
237
- this._texture = new Texture(
238
- this.canvas,
239
- UVMapping,
240
- ClampToEdgeWrapping,
241
- ClampToEdgeWrapping,
242
- LinearFilter,
243
- LinearMipmapLinearFilter,
244
- );
245
- this._texture.generateMipmaps = false;
246
- // mipmaps[i] is uploaded as WebGL level i. Level 0 must be the base
247
- // canvas; levels 1..N are the custom-rendered smaller canvases.
248
- // Rendering text natively at each mip size is sharper than GPU box-filtering.
249
- this._texture.mipmaps = [this.canvas, ...this._mipCanvases];
250
- } else {
251
- // WebGL1 only supports mipmaps on power-of-two textures, and these
252
- // canvases are arbitrarily sized. Skip mipmap generation outside WebGL2.
253
- this._texture = new Texture(
254
- this.canvas,
255
- UVMapping,
256
- ClampToEdgeWrapping,
257
- ClampToEdgeWrapping,
258
- LinearFilter,
259
- LinearMipmapLinearFilter,
260
- );
261
- this._texture.generateMipmaps = true;
262
- }
294
+ this._texture = new Texture(
295
+ this.canvas,
296
+ UVMapping,
297
+ ClampToEdgeWrapping,
298
+ ClampToEdgeWrapping,
299
+ LinearFilter,
300
+ LinearMipmapLinearFilter,
301
+ );
302
+ this._applyMipmaps(this._texture);
263
303
  this._texture.anisotropy = wrapper?.maxAnisotropy ?? 1;
264
304
  this._texture.needsUpdate = true;
265
305
  this._texture.colorSpace = SRGBColorSpace;
@@ -0,0 +1,32 @@
1
+ import { describe, expect, it } from "vitest";
2
+ import { buildTokenMap, parseMessageTokens, tokenizedMessageToText } from "./TokenizedText";
3
+
4
+ describe("tokenizedMessageToText", () => {
5
+ const tokenMap = buildTokenMap(
6
+ { gold: { image: "gold.png", text: "gold" }, vp: { label: "VP", color: "#fff" }, iconOnly: { image: "x.png" } },
7
+ [{ playerId: "player0", name: "Josh", color: "#f00", imageUrl: "josh.png" }],
8
+ );
9
+
10
+ it("replaces player tokens with the player's name", () => {
11
+ expect(tokenizedMessageToText("Follow :player0:'s action", tokenMap)).toBe("Follow Josh's action");
12
+ });
13
+
14
+ it("prefers text, then label, and keeps unknown or image-only tokens as-is", () => {
15
+ expect(tokenizedMessageToText("Pay 2 :gold: for 1 :vp:", tokenMap)).toBe("Pay 2 gold for 1 VP");
16
+ expect(tokenizedMessageToText(":iconOnly: and :nope:", tokenMap)).toBe(":iconOnly: and :nope:");
17
+ });
18
+
19
+ it("gives the warning token a text form", () => {
20
+ expect(tokenizedMessageToText(":warning: Last round", tokenMap)).toBe("Warning: Last round");
21
+ });
22
+
23
+ it("leaves plain messages untouched", () => {
24
+ expect(tokenizedMessageToText("Waiting for turn to complete", tokenMap)).toBe("Waiting for turn to complete");
25
+ expect(tokenizedMessageToText("", tokenMap)).toBe("");
26
+ });
27
+
28
+ it("parses text runs and tokens in order", () => {
29
+ const parts = parseMessageTokens("a :player0: b", tokenMap);
30
+ expect(parts.map((p) => p.type)).toEqual(["text", "token", "text"]);
31
+ });
32
+ });
@@ -0,0 +1,97 @@
1
+ import { ImageSpec } from "./CanvasStack/CanvasOperations";
2
+
3
+ /**
4
+ * A `:token:` that can appear in game messages (prompts, log lines, etc.).
5
+ *
6
+ * `image`, `label` and `color` drive the "pretty" rendering in the UI. `text` is the plain-text stand-in used anywhere
7
+ * a message has to be pure text (match storage, match lists, notifications). When `text` is omitted the `label` is
8
+ * used, and when there is no label either the raw `:token:` is kept.
9
+ */
10
+ export interface TokenDefinition {
11
+ image?: string | ImageSpec;
12
+ label?: string;
13
+ color?: string;
14
+ /** Plain-text representation of this token, for places that cannot render images or styling. */
15
+ text?: string;
16
+ }
17
+
18
+ export type TokenMap = Record<string, TokenDefinition>;
19
+
20
+ export interface ParsedToken {
21
+ type: "token";
22
+ name: string;
23
+ definition?: TokenDefinition;
24
+ key: string;
25
+ }
26
+
27
+ export interface ParsedText {
28
+ type: "text";
29
+ content: string;
30
+ key: string;
31
+ }
32
+
33
+ export type ParsedElement = ParsedToken | ParsedText;
34
+
35
+ const TOKEN_REGEX = /:([a-zA-Z0-9_-]+):/g;
36
+
37
+ /** Splits a message into literal text runs and `:token:` references. */
38
+ export function parseMessageTokens(message: string, tokenMap: TokenMap): ParsedElement[] {
39
+ const elements: ParsedElement[] = [];
40
+ const regex = new RegExp(TOKEN_REGEX.source, "g");
41
+ let lastIndex = 0;
42
+ let match;
43
+
44
+ while ((match = regex.exec(message)) !== null) {
45
+ const tokenName = match[1];
46
+
47
+ if (match.index > lastIndex) {
48
+ elements.push({ type: "text", content: message.substring(lastIndex, match.index), key: `text-${lastIndex}` });
49
+ }
50
+
51
+ elements.push({ type: "token", name: tokenName, definition: tokenMap[tokenName], key: `token-${match.index}` });
52
+
53
+ lastIndex = match.index + match[0].length;
54
+ }
55
+
56
+ if (lastIndex < message.length) {
57
+ elements.push({ type: "text", content: message.substring(lastIndex), key: `text-${lastIndex}` });
58
+ }
59
+
60
+ return elements;
61
+ }
62
+
63
+ /** The plain-text form of a single token. */
64
+ export function tokenToText(name: string, definition?: TokenDefinition): string {
65
+ return definition?.text ?? definition?.label ?? `:${name}:`;
66
+ }
67
+
68
+ /**
69
+ * Converts a tokenized message into plain text by replacing every `:token:` with its text representation.
70
+ * Unknown tokens are left as-is.
71
+ */
72
+ export function tokenizedMessageToText(message: string, tokenMap: TokenMap): string {
73
+ return parseMessageTokens(message, tokenMap)
74
+ .map((el) => (el.type === "token" ? tokenToText(el.name, el.definition) : el.content))
75
+ .join("");
76
+ }
77
+
78
+ export interface TokenMapPlayer {
79
+ playerId: string;
80
+ name?: string;
81
+ color?: string;
82
+ imageUrl?: string;
83
+ }
84
+
85
+ /**
86
+ * Builds the full token map for a match: the game's own tokens plus one token per player (keyed by player id) and the
87
+ * built-in `:warning:` token. Shared by the UI hook and by server-side code so both agree on what a token means.
88
+ */
89
+ export function buildTokenMap(gameTokenMap: TokenMap | undefined, players: TokenMapPlayer[], defaultColor?: string): TokenMap {
90
+ const tokenMap: TokenMap = { ...(gameTokenMap ?? {}) };
91
+ players.forEach((p) => {
92
+ const name = p.name ?? "??";
93
+ tokenMap[p.playerId] = { label: name, text: name, color: p.color ?? defaultColor, image: p.imageUrl };
94
+ });
95
+ tokenMap["warning"] = { label: "⚠️", text: "Warning:" };
96
+ return tokenMap;
97
+ }