@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.
- package/dist/components/ClientTrustMatchViewer.d.ts +14 -1
- package/dist/components/ClientTrustMatchViewer.d.ts.map +1 -1
- package/dist/components/ClientTrustMatchViewer.js +25 -2
- package/dist/components/ClientTrustMatchViewer.js.map +1 -1
- package/dist/components/DemoWrapper.d.ts.map +1 -1
- package/dist/components/DemoWrapper.js +4 -11
- package/dist/components/DemoWrapper.js.map +1 -1
- package/dist/components/Gallery/BuiltItem.d.ts.map +1 -1
- package/dist/components/Gallery/BuiltItem.js +3 -0
- package/dist/components/Gallery/BuiltItem.js.map +1 -1
- package/dist/components/Gallery/GalleryController.d.ts.map +1 -1
- package/dist/components/Gallery/GalleryController.js +3 -1
- package/dist/components/Gallery/GalleryController.js.map +1 -1
- package/dist/components/Playground.d.ts.map +1 -1
- package/dist/components/Playground.js +37 -11
- package/dist/components/Playground.js.map +1 -1
- package/dist/components/TokenizedMessage.d.ts +2 -6
- package/dist/components/TokenizedMessage.d.ts.map +1 -1
- package/dist/components/TokenizedMessage.js +1 -35
- package/dist/components/TokenizedMessage.js.map +1 -1
- package/dist/components/TopBarPlayers.d.ts.map +1 -1
- package/dist/components/TopBarPlayers.js +1 -1
- package/dist/components/TopBarPlayers.js.map +1 -1
- package/dist/game/Chit.js +2 -2
- package/dist/game/Chit.js.map +1 -1
- package/dist/game/ConnectionTransport.d.ts +3 -0
- package/dist/game/ConnectionTransport.d.ts.map +1 -1
- package/dist/game/ConnectionTransport.js +13 -4
- package/dist/game/ConnectionTransport.js.map +1 -1
- package/dist/game/Game.d.ts +8 -1
- package/dist/game/Game.d.ts.map +1 -1
- package/dist/game/Match.d.ts +66 -0
- package/dist/game/Match.d.ts.map +1 -1
- package/dist/game/Match.js +151 -3
- package/dist/game/Match.js.map +1 -1
- package/dist/game/MatchStorage.d.ts +8 -1
- package/dist/game/MatchStorage.d.ts.map +1 -1
- package/dist/game/MatchStorage.js +11 -3
- package/dist/game/MatchStorage.js.map +1 -1
- package/dist/game/Pick.d.ts +14 -0
- package/dist/game/Pick.d.ts.map +1 -1
- package/dist/game/Pick.js +15 -0
- package/dist/game/Pick.js.map +1 -1
- package/dist/game/PlayerChit.d.ts +3 -0
- package/dist/game/PlayerChit.d.ts.map +1 -1
- package/dist/game/PlayerChit.js +3 -0
- package/dist/game/PlayerChit.js.map +1 -1
- package/dist/game/PlayerInfo.d.ts +8 -0
- package/dist/game/PlayerInfo.d.ts.map +1 -1
- package/dist/game/PlayerInfo.js +6 -3
- package/dist/game/PlayerInfo.js.map +1 -1
- package/dist/game/PlayerPromptStatusChit.d.ts.map +1 -1
- package/dist/game/PlayerPromptStatusChit.js +3 -1
- package/dist/game/PlayerPromptStatusChit.js.map +1 -1
- package/dist/game/Turn.d.ts +5 -0
- package/dist/game/Turn.d.ts.map +1 -1
- package/dist/game/Turn.js +18 -7
- package/dist/game/Turn.js.map +1 -1
- package/dist/game/TurnState.d.ts +8 -0
- package/dist/game/TurnState.d.ts.map +1 -1
- package/dist/game/TurnState.js +7 -0
- package/dist/game/TurnState.js.map +1 -1
- package/dist/game/ai/AiClientPrompts.d.ts +160 -0
- package/dist/game/ai/AiClientPrompts.d.ts.map +1 -0
- package/dist/game/ai/AiClientPrompts.js +525 -0
- package/dist/game/ai/AiClientPrompts.js.map +1 -0
- package/dist/game/ai/AiLiveView.d.ts +2 -0
- package/dist/game/ai/AiLiveView.d.ts.map +1 -0
- package/dist/game/ai/AiLiveView.js +48 -0
- package/dist/game/ai/AiLiveView.js.map +1 -0
- package/dist/game/ai/AiPlayerView.d.ts +2 -0
- package/dist/game/ai/AiPlayerView.d.ts.map +1 -0
- package/dist/game/ai/AiPlayerView.js +89 -0
- package/dist/game/ai/AiPlayerView.js.map +1 -0
- package/dist/game/ai/connectAiPlayer.d.ts +43 -0
- package/dist/game/ai/connectAiPlayer.d.ts.map +1 -0
- package/dist/game/ai/connectAiPlayer.js +58 -0
- package/dist/game/ai/connectAiPlayer.js.map +1 -0
- package/dist/game/ai/defineAi.d.ts +119 -0
- package/dist/game/ai/defineAi.d.ts.map +1 -0
- package/dist/game/ai/defineAi.js +122 -0
- package/dist/game/ai/defineAi.js.map +1 -0
- package/dist/game/ai/simulateMatches.d.ts +62 -0
- package/dist/game/ai/simulateMatches.d.ts.map +1 -0
- package/dist/game/ai/simulateMatches.js +108 -0
- package/dist/game/ai/simulateMatches.js.map +1 -0
- package/dist/game/badAiTransport/BadAIClientPrompts.d.ts +3 -0
- package/dist/game/badAiTransport/BadAIClientPrompts.d.ts.map +1 -1
- package/dist/game/badAiTransport/BadAIClientPrompts.js +3 -0
- package/dist/game/badAiTransport/BadAIClientPrompts.js.map +1 -1
- package/dist/hooks/useTokenMap.d.ts +2 -4
- package/dist/hooks/useTokenMap.d.ts.map +1 -1
- package/dist/hooks/useTokenMap.js +2 -5
- package/dist/hooks/useTokenMap.js.map +1 -1
- package/dist/index.d.ts +21 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +8 -1
- package/dist/index.js.map +1 -1
- package/dist/utilities/CanvasStack/CanvasStack.d.ts +11 -0
- package/dist/utilities/CanvasStack/CanvasStack.d.ts.map +1 -1
- package/dist/utilities/CanvasStack/CanvasStack.js +62 -15
- package/dist/utilities/CanvasStack/CanvasStack.js.map +1 -1
- package/dist/utilities/TokenizedText.d.ts +49 -0
- package/dist/utilities/TokenizedText.d.ts.map +1 -0
- package/dist/utilities/TokenizedText.js +47 -0
- package/dist/utilities/TokenizedText.js.map +1 -0
- package/package.json +1 -1
- package/src/library/components/ClientTrustMatchViewer.tsx +32 -2
- package/src/library/components/DemoWrapper.tsx +4 -12
- package/src/library/components/Gallery/BuiltItem.ts +4 -0
- package/src/library/components/Gallery/GalleryController.ts +3 -1
- package/src/library/components/Playground.tsx +77 -12
- package/src/library/components/TokenizedMessage.tsx +2 -63
- package/src/library/components/TopBarPlayers.tsx +6 -1
- package/src/library/game/Chit.ts +2 -2
- package/src/library/game/ConnectionTransport.ts +12 -3
- package/src/library/game/Game.ts +9 -1
- package/src/library/game/Match.ts +162 -3
- package/src/library/game/MatchStorage.ts +14 -4
- package/src/library/game/Pick.ts +20 -0
- package/src/library/game/PlayerChit.ts +4 -1
- package/src/library/game/PlayerInfo.ts +13 -3
- package/src/library/game/PlayerPromptStatusChit.ts +3 -1
- package/src/library/game/Turn.ts +19 -7
- package/src/library/game/TurnState.ts +8 -0
- package/src/library/game/ai/AI_GUIDE.md +434 -0
- package/src/library/game/ai/AiClientPrompts.ts +681 -0
- package/src/library/game/ai/AiLiveView.ts +63 -0
- package/src/library/game/ai/AiPlayerView.ts +112 -0
- package/src/library/game/ai/ai.test.ts +475 -0
- package/src/library/game/ai/connectAiPlayer.ts +88 -0
- package/src/library/game/ai/defineAi.ts +230 -0
- package/src/library/game/ai/simulateMatches.ts +172 -0
- package/src/library/game/badAiTransport/BadAIClientPrompts.ts +3 -0
- package/src/library/hooks/useTokenMap.ts +2 -6
- package/src/library/index.ts +34 -2
- package/src/library/utilities/CanvasStack/CanvasStack.ts +68 -28
- package/src/library/utilities/TokenizedText.test.ts +32 -0
- 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.
|
|
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
|
-
|
|
237
|
-
this.
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
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
|
+
}
|