@pretty-chitty/core 1.2.16 → 1.2.18
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/Gallery/GalleryViewer.d.ts.map +1 -1
- package/dist/components/Gallery/GalleryViewer.js +7 -2
- package/dist/components/Gallery/GalleryViewer.js.map +1 -1
- package/dist/components/PanelSpark.js +1 -1
- package/dist/components/PlayerImage.d.ts.map +1 -1
- package/dist/components/PlayerImage.js +21 -1
- package/dist/components/PlayerImage.js.map +1 -1
- package/dist/components/SparkLineChart.d.ts.map +1 -1
- package/dist/components/SparkLineChart.js +37 -64
- package/dist/components/SparkLineChart.js.map +1 -1
- package/dist/components/Viewer.d.ts.map +1 -1
- package/dist/components/Viewer.js +12 -2
- package/dist/components/Viewer.js.map +1 -1
- package/dist/game/Chit.js +1 -1
- package/dist/game/Chit.js.map +1 -1
- package/dist/game/PlayerChit.d.ts +1 -0
- package/dist/game/PlayerChit.d.ts.map +1 -1
- package/dist/game/PlayerChit.js +5 -2
- package/dist/game/PlayerChit.js.map +1 -1
- package/dist/game/Turn.d.ts +6 -2
- package/dist/game/Turn.d.ts.map +1 -1
- package/dist/game/Turn.js +100 -61
- package/dist/game/Turn.js.map +1 -1
- package/dist/hooks/useWebGlRenderer.d.ts +1 -0
- package/dist/hooks/useWebGlRenderer.d.ts.map +1 -1
- package/dist/hooks/useWebGlRenderer.js +44 -22
- package/dist/hooks/useWebGlRenderer.js.map +1 -1
- package/dist/rendering/CameraWrapperPerspective.d.ts.map +1 -1
- package/dist/rendering/CameraWrapperPerspective.js +10 -5
- package/dist/rendering/CameraWrapperPerspective.js.map +1 -1
- package/dist/rendering/LightWrapper.d.ts.map +1 -1
- package/dist/rendering/LightWrapper.js +14 -1
- package/dist/rendering/LightWrapper.js.map +1 -1
- package/dist/rendering/outline/EffectComposer.js +9 -5
- package/dist/rendering/outline/EffectComposer.js.map +1 -1
- package/dist/utilities/CanvasStack/CanvasOperations.d.ts +15 -1
- package/dist/utilities/CanvasStack/CanvasOperations.d.ts.map +1 -1
- package/dist/utilities/CanvasStack/CanvasOperations.js +65 -6
- package/dist/utilities/CanvasStack/CanvasOperations.js.map +1 -1
- package/dist/utilities/CanvasStack/CanvasStack.d.ts +7 -1
- package/dist/utilities/CanvasStack/CanvasStack.d.ts.map +1 -1
- package/dist/utilities/CanvasStack/CanvasStack.js +72 -6
- package/dist/utilities/CanvasStack/CanvasStack.js.map +1 -1
- package/dist/utilities/CanvasStack/PlayerCanvas.d.ts +2 -1
- package/dist/utilities/CanvasStack/PlayerCanvas.d.ts.map +1 -1
- package/dist/utilities/CanvasStack/PlayerCanvas.js +3 -2
- package/dist/utilities/CanvasStack/PlayerCanvas.js.map +1 -1
- package/dist/utilities/CanvasStack/ReactCanvas.d.ts +2 -1
- package/dist/utilities/CanvasStack/ReactCanvas.d.ts.map +1 -1
- package/dist/utilities/CanvasStack/ReactCanvas.js +2 -2
- package/dist/utilities/CanvasStack/ReactCanvas.js.map +1 -1
- package/dist/utilities/ParameterizedCanvas.d.ts +1 -0
- package/dist/utilities/ParameterizedCanvas.d.ts.map +1 -1
- package/dist/utilities/ParameterizedCanvas.js +4 -1
- package/dist/utilities/ParameterizedCanvas.js.map +1 -1
- package/package.json +1 -1
- package/src/library/components/Gallery/GalleryViewer.tsx +7 -2
- package/src/library/components/PanelSpark.tsx +1 -1
- package/src/library/components/PlayerImage.tsx +26 -2
- package/src/library/components/SparkLineChart.tsx +65 -104
- package/src/library/components/Viewer.tsx +11 -2
- package/src/library/game/Chit.ts +1 -1
- package/src/library/game/PlayerChit.ts +6 -2
- package/src/library/game/Turn.ts +112 -69
- package/src/library/hooks/useWebGlRenderer.tsx +56 -25
- package/src/library/rendering/CameraWrapperPerspective.ts +10 -5
- package/src/library/rendering/LightWrapper.ts +20 -1
- package/src/library/rendering/outline/EffectComposer.ts +4 -4
- package/src/library/utilities/CanvasStack/CanvasOperations.tsx +87 -17
- package/src/library/utilities/CanvasStack/CanvasStack.ts +86 -12
- package/src/library/utilities/CanvasStack/PlayerCanvas.tsx +5 -2
- package/src/library/utilities/CanvasStack/ReactCanvas.tsx +2 -2
- package/src/library/utilities/ParameterizedCanvas.ts +5 -1
|
@@ -102,6 +102,7 @@ export function GalleryViewer({
|
|
|
102
102
|
if (!ctx) return;
|
|
103
103
|
|
|
104
104
|
let cancelled = false;
|
|
105
|
+
let needsRedraw = false;
|
|
105
106
|
|
|
106
107
|
const animate = () => {
|
|
107
108
|
if (cancelled) return;
|
|
@@ -109,11 +110,15 @@ export function GalleryViewer({
|
|
|
109
110
|
|
|
110
111
|
const isAnimating = galleryController.render();
|
|
111
112
|
|
|
112
|
-
if (isAnimating) {
|
|
113
|
+
if (isAnimating || needsRedraw) {
|
|
113
114
|
ctx.canvas.width = w * rendererWrapper.pixelRatio;
|
|
114
115
|
ctx.canvas.height = h * rendererWrapper.pixelRatio;
|
|
115
116
|
ctx.clearRect(0, 0, w * rendererWrapper.pixelRatio, h * rendererWrapper.pixelRatio);
|
|
116
|
-
rendererWrapper.render(galleryController.sceneWrapper, galleryController.camera, ctx, theme);
|
|
117
|
+
const drew = rendererWrapper.render(galleryController.sceneWrapper, galleryController.camera, ctx, theme);
|
|
118
|
+
// If render bailed (e.g. WebGL context lost), retry next frame even if
|
|
119
|
+
// the controller has settled — otherwise the last frame of an animation
|
|
120
|
+
// gets stuck on a stale snapshot.
|
|
121
|
+
needsRedraw = !drew;
|
|
117
122
|
}
|
|
118
123
|
};
|
|
119
124
|
|
|
@@ -71,7 +71,7 @@ export default function PanelSpark({ chit, paused, zIndex }: { zIndex: number; c
|
|
|
71
71
|
backgroundColor = Color(color).lighten(0.3).hex();
|
|
72
72
|
}
|
|
73
73
|
|
|
74
|
-
if (Color(backgroundColor).lightness() <
|
|
74
|
+
if (Color(backgroundColor).lightness() < 40) {
|
|
75
75
|
foregroundColor = Color(foregroundColor).lightness(80).hex();
|
|
76
76
|
} else {
|
|
77
77
|
foregroundColor = Color(foregroundColor).lightness(10).hex();
|
|
@@ -1,7 +1,8 @@
|
|
|
1
|
-
import React from "react";
|
|
1
|
+
import React, { useEffect, useRef } from "react";
|
|
2
2
|
import { Box, SxProps } from "@mui/material";
|
|
3
3
|
import { PlayerChit } from "../game/PlayerChit";
|
|
4
4
|
import { useGameTheme } from "../hooks/useGameTheme";
|
|
5
|
+
import { PlayerCanvas } from "../utilities/CanvasStack/PlayerCanvas";
|
|
5
6
|
|
|
6
7
|
export default function PlayerImage({
|
|
7
8
|
player,
|
|
@@ -17,6 +18,29 @@ export default function PlayerImage({
|
|
|
17
18
|
const theme = useGameTheme();
|
|
18
19
|
const BORDER_RATIO = player.color === "transparent" ? 0 : 1 / 12;
|
|
19
20
|
const IMAGE_SIZE = Math.ceil(size * (1 - BORDER_RATIO * 2));
|
|
21
|
+
|
|
22
|
+
const dpr = window.devicePixelRatio ?? 1;
|
|
23
|
+
const CANVAS_SIZE = Math.ceil(IMAGE_SIZE * dpr);
|
|
24
|
+
|
|
25
|
+
const canvasRef = useRef<HTMLCanvasElement>(null);
|
|
26
|
+
|
|
27
|
+
useEffect(() => {
|
|
28
|
+
const icon = player.icon;
|
|
29
|
+
|
|
30
|
+
const draw = () => {
|
|
31
|
+
const target = canvasRef.current;
|
|
32
|
+
const src = icon.canvas;
|
|
33
|
+
if (!target || !src) return;
|
|
34
|
+
const ctx = target.getContext("2d");
|
|
35
|
+
if (!ctx) return;
|
|
36
|
+
ctx.clearRect(0, 0, target.width, target.height);
|
|
37
|
+
ctx.drawImage(src, 0, 0, target.width, target.height);
|
|
38
|
+
};
|
|
39
|
+
|
|
40
|
+
draw();
|
|
41
|
+
return icon.onUpdate(draw);
|
|
42
|
+
}, [player, CANVAS_SIZE]);
|
|
43
|
+
|
|
20
44
|
return (
|
|
21
45
|
<Box
|
|
22
46
|
sx={{
|
|
@@ -33,7 +57,7 @@ export default function PlayerImage({
|
|
|
33
57
|
}}
|
|
34
58
|
>
|
|
35
59
|
<Box sx={{ position: "absolute", top: 0, left: 0 }}>
|
|
36
|
-
<
|
|
60
|
+
<canvas ref={canvasRef} width={CANVAS_SIZE} height={CANVAS_SIZE} style={{ width: IMAGE_SIZE, height: IMAGE_SIZE, display: "block" }} />
|
|
37
61
|
</Box>
|
|
38
62
|
</Box>
|
|
39
63
|
);
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import React, { useMemo } from "react";
|
|
2
2
|
import { Box, Typography } from "@mui/material";
|
|
3
|
+
import Color from "color";
|
|
3
4
|
import { SparkChit } from "../game/SparkChit";
|
|
4
5
|
import { PlayerChit } from "../game/PlayerChit";
|
|
5
6
|
|
|
@@ -29,65 +30,30 @@ interface SparkLineChartProps {
|
|
|
29
30
|
|
|
30
31
|
const HALO_OPACITY = 0.35;
|
|
31
32
|
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
return {
|
|
39
|
-
r: toLin(parseInt(hex.slice(0, 2), 16)),
|
|
40
|
-
g: toLin(parseInt(hex.slice(2, 4), 16)),
|
|
41
|
-
b: toLin(parseInt(hex.slice(4, 6), 16)),
|
|
42
|
-
};
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
function getColorLuminance(color: string): number {
|
|
46
|
-
const { r, g, b } = hexToLinearRgb(color);
|
|
47
|
-
return 0.2126 * r + 0.7152 * g + 0.0722 * b;
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
// Convert sRGB hex → CIE Lab (D65), so we can measure perceptual distance.
|
|
51
|
-
function hexToLab(color: string): { L: number; a: number; b: number } {
|
|
52
|
-
const { r, g, b } = hexToLinearRgb(color);
|
|
53
|
-
const X = (r * 0.4124564 + g * 0.3575761 + b * 0.1804375) / 0.95047;
|
|
54
|
-
const Y = r * 0.2126729 + g * 0.7151522 + b * 0.072175;
|
|
55
|
-
const Z = (r * 0.0193339 + g * 0.119192 + b * 0.9503041) / 1.08883;
|
|
56
|
-
const f = (t: number) => (t > 0.008856 ? Math.cbrt(t) : 7.787 * t + 16 / 116);
|
|
57
|
-
const fx = f(X);
|
|
58
|
-
const fy = f(Y);
|
|
59
|
-
const fz = f(Z);
|
|
60
|
-
return { L: 116 * fy - 16, a: 500 * (fx - fy), b: 200 * (fy - fz) };
|
|
61
|
-
}
|
|
62
|
-
|
|
63
|
-
// Perceptual contrast: a stroke is only needed if both luminance AND hue/chroma
|
|
64
|
-
// are too close to distinguish. Pure red vs pure green have similar luminance
|
|
65
|
-
// but enormous Lab distance, so they should pass without an outline.
|
|
66
|
-
function hasGoodContrast(color1: string, color2: string): boolean {
|
|
67
|
-
const lum1 = getColorLuminance(color1);
|
|
68
|
-
const lum2 = getColorLuminance(color2);
|
|
69
|
-
const lumRatio = (Math.max(lum1, lum2) + 0.05) / (Math.min(lum1, lum2) + 0.05);
|
|
70
|
-
if (lumRatio >= 3) return true;
|
|
71
|
-
|
|
72
|
-
const lab1 = hexToLab(color1);
|
|
73
|
-
const lab2 = hexToLab(color2);
|
|
74
|
-
const dL = lab1.L - lab2.L;
|
|
75
|
-
const da = lab1.a - lab2.a;
|
|
76
|
-
const db = lab1.b - lab2.b;
|
|
77
|
-
const deltaE = Math.sqrt(dL * dL + da * da + db * db);
|
|
78
|
-
return deltaE >= 35;
|
|
33
|
+
// One halo color for every line in the chart, picked as the extreme furthest
|
|
34
|
+
// from the bg — so the halo is always visible against the bg and provides the
|
|
35
|
+
// edge that separates each line from the bg. Per-line halos flipping between
|
|
36
|
+
// black and white read as arbitrary noise.
|
|
37
|
+
function getUnifiedHaloColor(backgroundColor: string): string {
|
|
38
|
+
return Color(backgroundColor).isLight() ? "#000000" : "#ffffff";
|
|
79
39
|
}
|
|
80
40
|
|
|
81
|
-
// The halo
|
|
82
|
-
//
|
|
83
|
-
//
|
|
84
|
-
//
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
const
|
|
89
|
-
const
|
|
90
|
-
|
|
41
|
+
// The halo only does its job when every line clearly contrasts against it —
|
|
42
|
+
// otherwise a dark line drowns into the dark halo and you get fuzz instead of
|
|
43
|
+
// a crisp edge. Push any too-close line toward the halo's opposite (which is
|
|
44
|
+
// also the bg side) in 5% steps until WCAG luminance contrast clears the
|
|
45
|
+
// threshold. This is the "lighten the dark line until it's distinguishable"
|
|
46
|
+
// step — only the dark-red-burger gets touched in a sea of light pastels.
|
|
47
|
+
function adjustLineForHalo(lineColor: string, haloColor: string, minRatio = 7): string {
|
|
48
|
+
const halo = Color(haloColor);
|
|
49
|
+
const line = Color(lineColor);
|
|
50
|
+
if (line.contrast(halo) >= minRatio) return line.hex();
|
|
51
|
+
const target = halo.isDark() ? Color("#ffffff") : Color("#000000");
|
|
52
|
+
for (let t = 0.05; t <= 1; t += 0.05) {
|
|
53
|
+
const candidate = line.mix(target, t);
|
|
54
|
+
if (candidate.contrast(halo) >= minRatio) return candidate.hex();
|
|
55
|
+
}
|
|
56
|
+
return target.hex();
|
|
91
57
|
}
|
|
92
58
|
|
|
93
59
|
export function SparkLineChart({
|
|
@@ -202,9 +168,15 @@ export function SparkLineChart({
|
|
|
202
168
|
const getY = (value: number) => chartHeight - ((value - chartMinValue) / chartValueRange) * chartHeight;
|
|
203
169
|
const chartEndX = chartWidth;
|
|
204
170
|
|
|
205
|
-
//
|
|
206
|
-
//
|
|
207
|
-
|
|
171
|
+
// The chart always gets one unified halo (opposite of bg). Each line is then
|
|
172
|
+
// shifted only if it sits too close to the halo's luminance — so on a light
|
|
173
|
+
// bg the halo is dark, all the light pastels keep their hue, and the lone
|
|
174
|
+
// dark line gets lightened until the dark halo can actually frame it.
|
|
175
|
+
const haloColor = getUnifiedHaloColor(backgroundColor);
|
|
176
|
+
const displayLines = lines.map((line) => ({
|
|
177
|
+
...line,
|
|
178
|
+
color: adjustLineForHalo(line.color, haloColor),
|
|
179
|
+
}));
|
|
208
180
|
|
|
209
181
|
return (
|
|
210
182
|
<Box sx={{ display: "flex", flexDirection: "column", alignItems: "center", my: 4 }}>
|
|
@@ -237,7 +209,7 @@ export function SparkLineChart({
|
|
|
237
209
|
</g>
|
|
238
210
|
|
|
239
211
|
{/* Draw each line */}
|
|
240
|
-
{
|
|
212
|
+
{displayLines.map((line) => {
|
|
241
213
|
// Step interpolation: values change instantaneously in the game (e.g. $0 → $50
|
|
242
214
|
// at t=100), so hold the previous value until the new clock, then jump vertically.
|
|
243
215
|
const pathData = line.points
|
|
@@ -255,28 +227,21 @@ export function SparkLineChart({
|
|
|
255
227
|
const lastY = getY(lastPoint.value);
|
|
256
228
|
const extendedPathData = `${pathData} L ${chartEndX} ${lastY}`;
|
|
257
229
|
|
|
258
|
-
// All lines get the same halo treatment if any line needs one — picked
|
|
259
|
-
// per line so each gets the right black/white for its own color.
|
|
260
|
-
const needsStroke = anyNeedsStroke;
|
|
261
|
-
const strokeColor = needsStroke ? getStrokeColor(line.color, backgroundColor) : undefined;
|
|
262
|
-
|
|
263
230
|
const adjustedLabelY = labelPositions[line.id];
|
|
264
231
|
|
|
265
232
|
return (
|
|
266
233
|
<g key={line.id}>
|
|
267
|
-
{/*
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
/>
|
|
279
|
-
)}
|
|
234
|
+
{/* Unified halo behind every line — soft glow opposite the bg. */}
|
|
235
|
+
<path
|
|
236
|
+
d={extendedPathData}
|
|
237
|
+
fill="none"
|
|
238
|
+
stroke={haloColor}
|
|
239
|
+
strokeWidth={6}
|
|
240
|
+
strokeLinecap="round"
|
|
241
|
+
strokeLinejoin="round"
|
|
242
|
+
filter="url(#sparkline-soften)"
|
|
243
|
+
opacity={HALO_OPACITY}
|
|
244
|
+
/>
|
|
280
245
|
<path
|
|
281
246
|
d={extendedPathData}
|
|
282
247
|
fill="none"
|
|
@@ -320,36 +285,32 @@ export function SparkLineChart({
|
|
|
320
285
|
|
|
321
286
|
{/* Final value label at the adjusted position */}
|
|
322
287
|
<g transform={`translate(${chartEndX + (line.icon ? 30 : 10)}, ${adjustedLabelY})`}>
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
</text>
|
|
336
|
-
)}
|
|
288
|
+
<text
|
|
289
|
+
fontSize={20}
|
|
290
|
+
fontWeight="bold"
|
|
291
|
+
dominantBaseline="middle"
|
|
292
|
+
fill={haloColor}
|
|
293
|
+
stroke={haloColor}
|
|
294
|
+
strokeWidth={5}
|
|
295
|
+
filter="url(#sparkline-soften)"
|
|
296
|
+
opacity={HALO_OPACITY}
|
|
297
|
+
>
|
|
298
|
+
{line.finalValue}
|
|
299
|
+
</text>
|
|
337
300
|
<text fill={line.color} fontSize={20} fontWeight="bold" dominantBaseline="middle">
|
|
338
301
|
{line.finalValue}
|
|
339
302
|
</text>
|
|
340
303
|
</g>
|
|
341
304
|
|
|
342
|
-
{/* End point marker */}
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
/>
|
|
352
|
-
)}
|
|
305
|
+
{/* End point marker halo + dot */}
|
|
306
|
+
<circle
|
|
307
|
+
cx={chartEndX}
|
|
308
|
+
cy={lastY}
|
|
309
|
+
r={6}
|
|
310
|
+
fill={haloColor}
|
|
311
|
+
filter="url(#sparkline-soften)"
|
|
312
|
+
opacity={HALO_OPACITY}
|
|
313
|
+
/>
|
|
353
314
|
<circle cx={chartEndX} cy={lastY} r={3} fill={line.color} />
|
|
354
315
|
</g>
|
|
355
316
|
);
|
|
@@ -170,9 +170,10 @@ export default function Viewer({
|
|
|
170
170
|
rendererWrapper &&
|
|
171
171
|
(paused || prevRenderNextFrame === undefined || prevRenderNextFrame || chitRenderInstance.dirty)
|
|
172
172
|
) {
|
|
173
|
+
let drew: boolean | undefined;
|
|
173
174
|
if (!hardPaused) {
|
|
174
175
|
// Clear canvas and render
|
|
175
|
-
|
|
176
|
+
drew = rendererWrapper.render(chitRenderInstance.sceneWrapper, chitRenderInstance.camera, context, theme, id);
|
|
176
177
|
|
|
177
178
|
loadingState.setLoading(id, false);
|
|
178
179
|
|
|
@@ -189,7 +190,15 @@ export default function Viewer({
|
|
|
189
190
|
loadingState.setLoading(id, false);
|
|
190
191
|
}
|
|
191
192
|
|
|
192
|
-
|
|
193
|
+
// If render bailed, keep dirty=true so the next iteration retries.
|
|
194
|
+
// When paused, no rAF is queued, so schedule one explicitly.
|
|
195
|
+
if (drew === false) {
|
|
196
|
+
if (paused) {
|
|
197
|
+
requestSharedAnimationFrame(animate);
|
|
198
|
+
}
|
|
199
|
+
} else {
|
|
200
|
+
chitRenderInstance.resetDirty();
|
|
201
|
+
}
|
|
193
202
|
if (!hardPaused) {
|
|
194
203
|
timeState.setAnimationState(id, !paused);
|
|
195
204
|
}
|
package/src/library/game/Chit.ts
CHANGED
|
@@ -44,7 +44,7 @@ function wrapWithDirtyProxy(value: any, chit: Chit): any {
|
|
|
44
44
|
},
|
|
45
45
|
get(target, prop) {
|
|
46
46
|
const val = target[prop];
|
|
47
|
-
if (typeof val === "function") return val.bind(
|
|
47
|
+
if (typeof val === "function") return val.bind(proxy);
|
|
48
48
|
if (typeof val === "object" && val !== null && !(val instanceof Chit) && !(val instanceof OrderedOutlet)) {
|
|
49
49
|
return wrapWithDirtyProxy(val, chit);
|
|
50
50
|
}
|
|
@@ -33,12 +33,16 @@ export class PlayerChit extends Chit {
|
|
|
33
33
|
public get panelTab() {
|
|
34
34
|
return {
|
|
35
35
|
color: this.color,
|
|
36
|
-
icon: new PlayerCanvas(this).get(),
|
|
36
|
+
icon: new PlayerCanvas(this, this.colorBlend).get(),
|
|
37
37
|
};
|
|
38
38
|
}
|
|
39
39
|
|
|
40
40
|
public get icon() {
|
|
41
|
-
return new PlayerCanvas(this).get();
|
|
41
|
+
return new PlayerCanvas(this, this.colorBlend).get();
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
public get colorBlend() {
|
|
45
|
+
return 0;
|
|
42
46
|
}
|
|
43
47
|
|
|
44
48
|
public get logKey() {
|
package/src/library/game/Turn.ts
CHANGED
|
@@ -94,6 +94,16 @@ export class Turn<T, P extends PlayerChit, R extends RootChit<P>> {
|
|
|
94
94
|
private dirtyChitIds = new Set<string>();
|
|
95
95
|
private pendingNewChits = new Set<Chit>();
|
|
96
96
|
private _statePool?: Map<string, string>;
|
|
97
|
+
private _serializeCache?: SerializeCache;
|
|
98
|
+
|
|
99
|
+
/**
|
|
100
|
+
* Drop any cached serialize() reconstruction on this turn and every ancestor,
|
|
101
|
+
* since their snapshots include our contribution.
|
|
102
|
+
*/
|
|
103
|
+
private invalidateSerializeCache() {
|
|
104
|
+
this._serializeCache = undefined;
|
|
105
|
+
this.parent?.invalidateSerializeCache();
|
|
106
|
+
}
|
|
97
107
|
|
|
98
108
|
private get statePool(): Map<string, string> {
|
|
99
109
|
if (this.parent) return this.parent.statePool;
|
|
@@ -305,8 +315,12 @@ export class Turn<T, P extends PlayerChit, R extends RootChit<P>> {
|
|
|
305
315
|
}
|
|
306
316
|
|
|
307
317
|
if (hasChange) {
|
|
318
|
+
// Pops and in-place merges can leave any cached walk pointing into invalid territory.
|
|
319
|
+
this.invalidateSerializeCache();
|
|
308
320
|
this.pass++;
|
|
309
|
-
|
|
321
|
+
// No trailing force-flush: it would only push an empty FlushClockStep (no chit
|
|
322
|
+
// diff, no log) that the client has to walk through for no visual change. The
|
|
323
|
+
// bumped `pass` is read live from `clockDetails` on the next notify.
|
|
310
324
|
}
|
|
311
325
|
}
|
|
312
326
|
|
|
@@ -360,7 +374,8 @@ export class Turn<T, P extends PlayerChit, R extends RootChit<P>> {
|
|
|
360
374
|
}
|
|
361
375
|
}
|
|
362
376
|
|
|
363
|
-
// 2. Serialize only dirty chits
|
|
377
|
+
// 2. Serialize only dirty chits. `newStates` now stores only the diff against the
|
|
378
|
+
// previous flush — unchanged-after-dirty-check chits are dropped from the step.
|
|
364
379
|
for (const id of this.dirtyChitIds) {
|
|
365
380
|
const chit = this.chitLookup[id];
|
|
366
381
|
if (!chit) continue;
|
|
@@ -369,20 +384,11 @@ export class Turn<T, P extends PlayerChit, R extends RootChit<P>> {
|
|
|
369
384
|
if (serialized !== lastState) {
|
|
370
385
|
this.lastChitStates[id] = newStates[id] = serialized;
|
|
371
386
|
sawChange = true;
|
|
372
|
-
} else {
|
|
373
|
-
newStates[id] = serialized;
|
|
374
387
|
}
|
|
375
388
|
}
|
|
376
389
|
this.dirtyChitIds.clear();
|
|
377
390
|
|
|
378
|
-
//
|
|
379
|
-
for (const id of Object.keys(this.lastChitStates)) {
|
|
380
|
-
if (newStates[id] === undefined) {
|
|
381
|
-
newStates[id] = this.lastChitStates[id];
|
|
382
|
-
}
|
|
383
|
-
}
|
|
384
|
-
|
|
385
|
-
// 5. Detect deleted chits — orphans in lastChitStates that weren't serialized above
|
|
391
|
+
// 3. Detect deleted chits — orphans in lastChitStates that weren't serialized above
|
|
386
392
|
const deletedRoots: Chit[] = [];
|
|
387
393
|
for (const id of Object.keys(this.lastChitStates)) {
|
|
388
394
|
if (this.lastChitStates[id] === Chit.deletedIfSerialized()) continue;
|
|
@@ -416,6 +422,8 @@ export class Turn<T, P extends PlayerChit, R extends RootChit<P>> {
|
|
|
416
422
|
}
|
|
417
423
|
|
|
418
424
|
this.clockSteps.push(new FlushClockStep(this.clock, newStates));
|
|
425
|
+
// Ancestors' caches included our prior contribution; the new step makes them stale.
|
|
426
|
+
this.parent?.invalidateSerializeCache();
|
|
419
427
|
}
|
|
420
428
|
}
|
|
421
429
|
|
|
@@ -479,6 +487,8 @@ export class Turn<T, P extends PlayerChit, R extends RootChit<P>> {
|
|
|
479
487
|
}
|
|
480
488
|
lastStep.turns.push(turn);
|
|
481
489
|
}
|
|
490
|
+
// Either case mutates a step our own cache (or an ancestor's) may have already walked past.
|
|
491
|
+
this.invalidateSerializeCache();
|
|
482
492
|
|
|
483
493
|
this.activeSubTurns.push(turn);
|
|
484
494
|
|
|
@@ -501,10 +511,27 @@ export class Turn<T, P extends PlayerChit, R extends RootChit<P>> {
|
|
|
501
511
|
});
|
|
502
512
|
|
|
503
513
|
this.lastChitStates = { ...this.lastChitStates, ...turn.lastChitStates };
|
|
514
|
+
turn.releasePostAbsorptionRefs();
|
|
504
515
|
|
|
505
516
|
return result;
|
|
506
517
|
}
|
|
507
518
|
|
|
519
|
+
/**
|
|
520
|
+
* Called by the parent right after it has absorbed our `chitLookup` and `lastChitStates`.
|
|
521
|
+
* After this we will only ever be walked via `serialize` / `chitsHistory` / `gameLog`,
|
|
522
|
+
* which only need `clockSteps` and `lockedChitStates` — so drop the heavy per-chit
|
|
523
|
+
* bookkeeping to keep finished sub-turns from pinning hundreds of chit references.
|
|
524
|
+
* @internal
|
|
525
|
+
*/
|
|
526
|
+
private releasePostAbsorptionRefs() {
|
|
527
|
+
this.chitLookup = {};
|
|
528
|
+
this.chitsToLock = [];
|
|
529
|
+
this.lastChitStates = {};
|
|
530
|
+
this.pendingNewChits.clear();
|
|
531
|
+
this.dirtyChitIds.clear();
|
|
532
|
+
this._serializeCache = undefined;
|
|
533
|
+
}
|
|
534
|
+
|
|
508
535
|
public async runParallelTurns<A>(
|
|
509
536
|
players: P[],
|
|
510
537
|
chits: (p: P) => Chit[],
|
|
@@ -761,31 +788,6 @@ export class Turn<T, P extends PlayerChit, R extends RootChit<P>> {
|
|
|
761
788
|
return result;
|
|
762
789
|
}
|
|
763
790
|
|
|
764
|
-
private findIndexOfLastFlushStepBefore(clock: number): number {
|
|
765
|
-
for (let j = this.clockSteps.length - 1; j >= 0; j--) {
|
|
766
|
-
if (this.clockSteps[j] instanceof FlushClockStep && this.clockSteps[j].startClock < clock) {
|
|
767
|
-
return j;
|
|
768
|
-
}
|
|
769
|
-
}
|
|
770
|
-
return 0;
|
|
771
|
-
}
|
|
772
|
-
private findIndexOfLastFlushStepWithLogMessageBefore(clock: number): number {
|
|
773
|
-
for (let j = this.clockSteps.length - 1; j >= 0; j--) {
|
|
774
|
-
if (
|
|
775
|
-
this.clockSteps[j] instanceof FlushClockStep &&
|
|
776
|
-
this.clockSteps[j].startClock < clock &&
|
|
777
|
-
(this.clockSteps[j] as FlushClockStep).log
|
|
778
|
-
) {
|
|
779
|
-
return j;
|
|
780
|
-
}
|
|
781
|
-
if (this.clockSteps[j].startClock < clock && this.clockSteps[j] instanceof SubTurnsClockStep) {
|
|
782
|
-
// logs cannot be inside subturns
|
|
783
|
-
break;
|
|
784
|
-
}
|
|
785
|
-
}
|
|
786
|
-
return -1;
|
|
787
|
-
}
|
|
788
|
-
|
|
789
791
|
/** @internal */
|
|
790
792
|
chitsHistory(playerId: string, ids: string[]): ChitHistoryResponse {
|
|
791
793
|
const result: ChitHistoryResponse = {};
|
|
@@ -803,6 +805,15 @@ export class Turn<T, P extends PlayerChit, R extends RootChit<P>> {
|
|
|
803
805
|
}
|
|
804
806
|
};
|
|
805
807
|
|
|
808
|
+
// Seed with the initial locked state so chits that never diff in this turn still
|
|
809
|
+
// surface their starting form (flush steps now hold diffs only).
|
|
810
|
+
ids.forEach((id) => {
|
|
811
|
+
const initial = this.lockedChitStates[id];
|
|
812
|
+
if (initial !== undefined) {
|
|
813
|
+
appendLog(id, 0, initial);
|
|
814
|
+
}
|
|
815
|
+
});
|
|
816
|
+
|
|
806
817
|
this.clockSteps.forEach((step) => {
|
|
807
818
|
if (step instanceof FlushClockStep) {
|
|
808
819
|
ids.forEach((id) => appendLog(id, step.startClock, step.state[id]));
|
|
@@ -824,51 +835,51 @@ export class Turn<T, P extends PlayerChit, R extends RootChit<P>> {
|
|
|
824
835
|
serialize(playerId: string, clock: number): ChitSerializationResponse {
|
|
825
836
|
clock = Math.max(0, Math.min(clock, this.playerVisibleClockTime(playerId)));
|
|
826
837
|
|
|
827
|
-
let chits
|
|
828
|
-
let
|
|
829
|
-
|
|
830
|
-
// going forwards
|
|
831
|
-
let index = this.findIndexOfLastFlushStepBefore(clock);
|
|
832
|
-
const lastLogIndex = this.findIndexOfLastFlushStepWithLogMessageBefore(clock);
|
|
833
|
-
|
|
834
|
-
// set up logs
|
|
838
|
+
let chits: ChitStateLookup;
|
|
839
|
+
let subTurns: { [turnId: string]: ClockDetails } | undefined;
|
|
835
840
|
let log: LogMessageSerializationResponse | undefined;
|
|
836
|
-
|
|
837
|
-
|
|
838
|
-
|
|
839
|
-
|
|
840
|
-
|
|
841
|
-
};
|
|
841
|
+
let resultingClock: number;
|
|
842
|
+
let index: number;
|
|
843
|
+
|
|
844
|
+
const cache = this._serializeCache;
|
|
845
|
+
if (cache && cache.playerId === playerId && clock >= cache.resultingClock) {
|
|
846
|
+
chits = { ...cache.chits };
|
|
847
|
+
subTurns = cache.subTurns ? { ...cache.subTurns } : undefined;
|
|
848
|
+
log = cache.log ? { ...cache.log } : undefined;
|
|
849
|
+
resultingClock = cache.resultingClock;
|
|
850
|
+
index = cache.nextIndex;
|
|
851
|
+
} else {
|
|
852
|
+
// Diff-replay: seed with the initial locked snapshot and merge every flush diff
|
|
853
|
+
// up to (but not including) the target clock.
|
|
854
|
+
chits = { ...this.lockedChitStates };
|
|
855
|
+
subTurns = undefined;
|
|
856
|
+
log = undefined;
|
|
857
|
+
resultingClock = -1;
|
|
858
|
+
index = 0;
|
|
842
859
|
}
|
|
843
|
-
let subTurns: { [turnId: string]: ClockDetails } | undefined = undefined;
|
|
844
860
|
|
|
845
|
-
|
|
846
|
-
while (
|
|
861
|
+
let cacheable = true;
|
|
862
|
+
while (index < this.clockSteps.length) {
|
|
847
863
|
const clockStep = this.clockSteps[index];
|
|
848
|
-
|
|
849
|
-
if (!clockStep) {
|
|
850
|
-
break;
|
|
851
|
-
}
|
|
852
|
-
|
|
853
|
-
if (clockStep.startClock >= clock) {
|
|
854
|
-
break;
|
|
855
|
-
}
|
|
864
|
+
if (clockStep.startClock >= clock) break;
|
|
856
865
|
|
|
857
866
|
if (clockStep instanceof FlushClockStep) {
|
|
858
867
|
subTurns = undefined;
|
|
859
|
-
chits
|
|
868
|
+
Object.assign(chits, clockStep.state);
|
|
860
869
|
resultingClock = clockStep.endClock();
|
|
870
|
+
if (clockStep.log) {
|
|
871
|
+
log = { message: clockStep.log, clock: clockStep.startClock };
|
|
872
|
+
}
|
|
873
|
+
index++;
|
|
861
874
|
} else if (clockStep instanceof SubTurnsClockStep) {
|
|
862
875
|
subTurns = {};
|
|
863
876
|
resultingClock = clockStep.startClock;
|
|
864
877
|
let remainingClocksToSpend = clock - clockStep.startClock;
|
|
865
878
|
for (const turn of clockStep.visibleTurns(playerId)) {
|
|
866
879
|
const time = Math.max(remainingClocksToSpend, 0);
|
|
867
|
-
if (time <= 0)
|
|
868
|
-
break;
|
|
869
|
-
}
|
|
880
|
+
if (time <= 0) break;
|
|
870
881
|
|
|
871
|
-
// we are going forward so we never want to have a turn go backwards.
|
|
882
|
+
// we are going forward so we never want to have a turn go backwards. ever.
|
|
872
883
|
const serialized = turn.serialize(playerId, time);
|
|
873
884
|
Object.assign(chits, serialized.chits);
|
|
874
885
|
|
|
@@ -876,13 +887,35 @@ export class Turn<T, P extends PlayerChit, R extends RootChit<P>> {
|
|
|
876
887
|
remainingClocksToSpend -= serialized.clockDetails.clock;
|
|
877
888
|
subTurns[turn.id] = serialized.clockDetails;
|
|
878
889
|
if (serialized.log) {
|
|
879
|
-
log = serialized.log;
|
|
880
|
-
log.clock += clockStep.startClock;
|
|
890
|
+
log = { message: serialized.log.message, clock: serialized.log.clock + clockStep.startClock };
|
|
881
891
|
}
|
|
882
892
|
}
|
|
893
|
+
// We can only advance `index` past this step (and cache from here) if the
|
|
894
|
+
// requested `clock` consumed this step's *entire* extent — i.e. every visible
|
|
895
|
+
// sub-turn was asked for at least its full clock time. Otherwise a future
|
|
896
|
+
// request at a higher `clock` needs to re-ask the sub-turn for more time,
|
|
897
|
+
// which means re-processing this same step.
|
|
898
|
+
if (clock < clockStep.endClock(playerId)) {
|
|
899
|
+
cacheable = false;
|
|
900
|
+
break;
|
|
901
|
+
}
|
|
902
|
+
index++;
|
|
903
|
+
} else {
|
|
904
|
+
index++;
|
|
883
905
|
}
|
|
884
906
|
}
|
|
885
907
|
|
|
908
|
+
if (cacheable) {
|
|
909
|
+
this._serializeCache = {
|
|
910
|
+
playerId,
|
|
911
|
+
nextIndex: index,
|
|
912
|
+
chits: { ...chits },
|
|
913
|
+
subTurns: subTurns ? { ...subTurns } : undefined,
|
|
914
|
+
log: log ? { ...log } : undefined,
|
|
915
|
+
resultingClock: resultingClock === -1 ? clock : resultingClock,
|
|
916
|
+
};
|
|
917
|
+
}
|
|
918
|
+
|
|
886
919
|
return {
|
|
887
920
|
log,
|
|
888
921
|
chits,
|
|
@@ -1202,6 +1235,7 @@ export class Turn<T, P extends PlayerChit, R extends RootChit<P>> {
|
|
|
1202
1235
|
this.unresolvedPrompt = undefined;
|
|
1203
1236
|
this.activeSubTurns = [];
|
|
1204
1237
|
this.pass++;
|
|
1238
|
+
this.invalidateSerializeCache();
|
|
1205
1239
|
}
|
|
1206
1240
|
}
|
|
1207
1241
|
|
|
@@ -1212,6 +1246,15 @@ export class Turn<T, P extends PlayerChit, R extends RootChit<P>> {
|
|
|
1212
1246
|
type ChitLookup = { [id: string]: Chit };
|
|
1213
1247
|
type ChitStateLookup = { [id: string]: string };
|
|
1214
1248
|
|
|
1249
|
+
type SerializeCache = {
|
|
1250
|
+
playerId: string;
|
|
1251
|
+
nextIndex: number;
|
|
1252
|
+
chits: ChitStateLookup;
|
|
1253
|
+
subTurns?: { [turnId: string]: ClockDetails };
|
|
1254
|
+
log?: { message: string; clock: number };
|
|
1255
|
+
resultingClock: number;
|
|
1256
|
+
};
|
|
1257
|
+
|
|
1215
1258
|
abstract class ClockStep {
|
|
1216
1259
|
abstract get startClock(): number;
|
|
1217
1260
|
abstract endClock(playerId?: string): number;
|