@pretty-chitty/core 1.2.10 → 1.2.16
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/README.md +17 -3
- package/dist/components/DemoWrapper.js +1 -1
- package/dist/components/DemoWrapper.js.map +1 -1
- package/dist/components/FullScreenGalleryDisplay.d.ts.map +1 -1
- package/dist/components/FullScreenGalleryDisplay.js +5 -1
- package/dist/components/FullScreenGalleryDisplay.js.map +1 -1
- package/dist/components/Gallery/AnimationController.d.ts +1 -0
- package/dist/components/Gallery/AnimationController.d.ts.map +1 -1
- package/dist/components/Gallery/AnimationController.js +9 -0
- package/dist/components/Gallery/AnimationController.js.map +1 -1
- package/dist/components/Gallery/BuiltItem.d.ts +2 -1
- package/dist/components/Gallery/BuiltItem.d.ts.map +1 -1
- package/dist/components/Gallery/BuiltItem.js +15 -3
- package/dist/components/Gallery/BuiltItem.js.map +1 -1
- package/dist/components/Gallery/GalleryController.d.ts +1 -0
- package/dist/components/Gallery/GalleryController.d.ts.map +1 -1
- package/dist/components/Gallery/GalleryController.js +15 -4
- package/dist/components/Gallery/GalleryController.js.map +1 -1
- package/dist/components/Gallery/GalleryViewer.d.ts +2 -1
- package/dist/components/Gallery/GalleryViewer.d.ts.map +1 -1
- package/dist/components/Gallery/GalleryViewer.js +13 -4
- package/dist/components/Gallery/GalleryViewer.js.map +1 -1
- package/dist/components/InlineGalleryDisplay.d.ts.map +1 -1
- package/dist/components/InlineGalleryDisplay.js +5 -1
- package/dist/components/InlineGalleryDisplay.js.map +1 -1
- package/dist/components/Playground.d.ts.map +1 -1
- package/dist/components/Playground.js +59 -21
- package/dist/components/Playground.js.map +1 -1
- package/dist/components/ServerTrustMatchViewer.d.ts.map +1 -1
- package/dist/components/ServerTrustMatchViewer.js +10 -2
- package/dist/components/ServerTrustMatchViewer.js.map +1 -1
- package/dist/components/SparkLineChart.d.ts.map +1 -1
- package/dist/components/SparkLineChart.js +62 -22
- package/dist/components/SparkLineChart.js.map +1 -1
- package/dist/components/Viewer.d.ts.map +1 -1
- package/dist/components/Viewer.js +15 -6
- package/dist/components/Viewer.js.map +1 -1
- package/dist/game/Chit.d.ts +10 -1
- package/dist/game/Chit.d.ts.map +1 -1
- package/dist/game/Chit.js +125 -4
- package/dist/game/Chit.js.map +1 -1
- package/dist/game/Connection.d.ts +1 -0
- package/dist/game/Connection.d.ts.map +1 -1
- package/dist/game/Connection.js +27 -8
- package/dist/game/Connection.js.map +1 -1
- package/dist/game/ConnectionTransport.d.ts +5 -0
- package/dist/game/ConnectionTransport.d.ts.map +1 -1
- package/dist/game/ConnectionTransport.js +32 -0
- package/dist/game/ConnectionTransport.js.map +1 -1
- package/dist/game/GalleryItemChitChildrenSource.d.ts.map +1 -1
- package/dist/game/GalleryItemChitChildrenSource.js +9 -1
- package/dist/game/GalleryItemChitChildrenSource.js.map +1 -1
- package/dist/game/Game.d.ts +6 -0
- package/dist/game/Game.d.ts.map +1 -1
- package/dist/game/OrderedOutlet.d.ts +4 -1
- package/dist/game/OrderedOutlet.d.ts.map +1 -1
- package/dist/game/OrderedOutlet.js +25 -4
- package/dist/game/OrderedOutlet.js.map +1 -1
- package/dist/game/Pick.d.ts +1 -0
- package/dist/game/Pick.d.ts.map +1 -1
- package/dist/game/Pick.js +40 -1
- package/dist/game/Pick.js.map +1 -1
- package/dist/game/Prompt.d.ts +11 -1
- package/dist/game/Prompt.d.ts.map +1 -1
- package/dist/game/Prompt.js +34 -1
- package/dist/game/Prompt.js.map +1 -1
- package/dist/game/Turn.d.ts +17 -0
- package/dist/game/Turn.d.ts.map +1 -1
- package/dist/game/Turn.js +114 -63
- package/dist/game/Turn.js.map +1 -1
- package/dist/game/badAiTransport/BadAIClientPrompts.d.ts +3 -1
- package/dist/game/badAiTransport/BadAIClientPrompts.d.ts.map +1 -1
- package/dist/game/badAiTransport/BadAIClientPrompts.js +31 -5
- package/dist/game/badAiTransport/BadAIClientPrompts.js.map +1 -1
- package/dist/game/clientTransport/ClientPrompts.d.ts +2 -0
- package/dist/game/clientTransport/ClientPrompts.d.ts.map +1 -1
- package/dist/game/clientTransport/ClientPrompts.js +48 -16
- package/dist/game/clientTransport/ClientPrompts.js.map +1 -1
- package/dist/game/clientTransport/ClientTime.d.ts.map +1 -1
- package/dist/game/clientTransport/ClientTime.js +58 -52
- package/dist/game/clientTransport/ClientTime.js.map +1 -1
- package/dist/game/serverTransport/ServerPrompts.d.ts +1 -1
- package/dist/game/serverTransport/ServerPrompts.d.ts.map +1 -1
- package/dist/game/serverTransport/ServerPrompts.js +14 -3
- package/dist/game/serverTransport/ServerPrompts.js.map +1 -1
- package/dist/game/serverTransport/ServerStatus.d.ts.map +1 -1
- package/dist/game/serverTransport/ServerStatus.js +6 -2
- package/dist/game/serverTransport/ServerStatus.js.map +1 -1
- package/dist/game/serverTransport/ServerTime.d.ts.map +1 -1
- package/dist/game/serverTransport/ServerTime.js +6 -2
- package/dist/game/serverTransport/ServerTime.js.map +1 -1
- package/dist/hooks/useTimeController.d.ts.map +1 -1
- package/dist/hooks/useTimeController.js +3 -0
- package/dist/hooks/useTimeController.js.map +1 -1
- package/dist/hooks/useWebGlRenderer.d.ts +27 -1
- package/dist/hooks/useWebGlRenderer.d.ts.map +1 -1
- package/dist/hooks/useWebGlRenderer.js +191 -7
- package/dist/hooks/useWebGlRenderer.js.map +1 -1
- package/dist/index.d.ts +1 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +1 -1
- package/dist/index.js.map +1 -1
- package/dist/rendering/ChitGalleryItemInstance.js +1 -1
- package/dist/rendering/ChitGalleryItemInstance.js.map +1 -1
- package/dist/rendering/ChitRenderInstance.d.ts.map +1 -1
- package/dist/rendering/ChitRenderInstance.js +30 -9
- package/dist/rendering/ChitRenderInstance.js.map +1 -1
- package/dist/rendering/ChitRenderSpec.js +1 -1
- package/dist/rendering/ChitRenderSpec.js.map +1 -1
- package/dist/rendering/outline/IDBasedOutlinePass.d.ts.map +1 -1
- package/dist/rendering/outline/IDBasedOutlinePass.js +6 -2
- package/dist/rendering/outline/IDBasedOutlinePass.js.map +1 -1
- package/dist/rendering/outline/passes/InterMeshEdgeDetectionPass.d.ts.map +1 -1
- package/dist/rendering/outline/passes/InterMeshEdgeDetectionPass.js +7 -3
- package/dist/rendering/outline/passes/InterMeshEdgeDetectionPass.js.map +1 -1
- package/dist/utilities/CanvasStack/CanvasOperations.d.ts.map +1 -1
- package/dist/utilities/CanvasStack/CanvasOperations.js +34 -13
- package/dist/utilities/CanvasStack/CanvasOperations.js.map +1 -1
- package/dist/utilities/CanvasStack/CanvasStack.d.ts +9 -0
- package/dist/utilities/CanvasStack/CanvasStack.d.ts.map +1 -1
- package/dist/utilities/CanvasStack/CanvasStack.js +37 -2
- package/dist/utilities/CanvasStack/CanvasStack.js.map +1 -1
- package/dist/utilities/CanvasStack/RichTextRenderer.d.ts +2 -0
- package/dist/utilities/CanvasStack/RichTextRenderer.d.ts.map +1 -1
- package/dist/utilities/CanvasStack/RichTextRenderer.js +47 -4
- package/dist/utilities/CanvasStack/RichTextRenderer.js.map +1 -1
- package/package.json +1 -1
- package/src/library/components/DemoWrapper.tsx +1 -1
- package/src/library/components/FullScreenGalleryDisplay.tsx +5 -0
- package/src/library/components/Gallery/AnimationController.ts +10 -0
- package/src/library/components/Gallery/BuiltItem.ts +16 -3
- package/src/library/components/Gallery/GalleryController.ts +15 -4
- package/src/library/components/Gallery/GalleryViewer.tsx +17 -3
- package/src/library/components/InlineGalleryDisplay.tsx +5 -0
- package/src/library/components/Playground.tsx +76 -20
- package/src/library/components/ServerTrustMatchViewer.tsx +10 -2
- package/src/library/components/SparkLineChart.tsx +85 -24
- package/src/library/components/Viewer.tsx +15 -6
- package/src/library/game/Chit.test.ts +91 -0
- package/src/library/game/Chit.ts +122 -9
- package/src/library/game/Connection.ts +27 -8
- package/src/library/game/ConnectionTransport.ts +38 -0
- package/src/library/game/GalleryItemChitChildrenSource.ts +9 -1
- package/src/library/game/Game.ts +7 -0
- package/src/library/game/OrderedOutlet.ts +27 -4
- package/src/library/game/Pick.ts +46 -4
- package/src/library/game/Prompt.ts +43 -3
- package/src/library/game/Turn.ts +127 -78
- package/src/library/game/badAiTransport/BadAIClientPrompts.ts +33 -4
- package/src/library/game/clientTransport/ClientPrompts.ts +50 -16
- package/src/library/game/clientTransport/ClientTime.ts +61 -56
- package/src/library/game/serverTransport/ServerPrompts.ts +25 -7
- package/src/library/game/serverTransport/ServerStatus.ts +6 -2
- package/src/library/game/serverTransport/ServerTime.ts +6 -2
- package/src/library/hooks/useTimeController.tsx +3 -0
- package/src/library/hooks/useWebGlRenderer.tsx +225 -7
- package/src/library/index.ts +1 -1
- package/src/library/rendering/ChitGalleryItemInstance.ts +1 -1
- package/src/library/rendering/ChitRenderInstance.ts +32 -9
- package/src/library/rendering/ChitRenderSpec.ts +1 -1
- package/src/library/rendering/outline/IDBasedOutlinePass.ts +6 -2
- package/src/library/rendering/outline/passes/InterMeshEdgeDetectionPass.ts +7 -3
- package/src/library/utilities/CanvasStack/CanvasOperations.tsx +44 -13
- package/src/library/utilities/CanvasStack/CanvasStack.ts +38 -1
- package/src/library/utilities/CanvasStack/RichTextRenderer.ts +66 -5
package/src/library/game/Turn.ts
CHANGED
|
@@ -28,6 +28,8 @@ type ChitHistoryResponse = {
|
|
|
28
28
|
[id: string]: ChitHistoryItem[];
|
|
29
29
|
};
|
|
30
30
|
|
|
31
|
+
export type ConfirmContext = "turn_end" | "roll" | "switch_player";
|
|
32
|
+
|
|
31
33
|
type ValidPick = undefined | false | Pick | Pick[] | ButtonPick | ButtonPick[] | GameButton | GameButton[];
|
|
32
34
|
export type Picks = ValidPick | ValidPick[];
|
|
33
35
|
|
|
@@ -89,6 +91,38 @@ export class Turn<T, P extends PlayerChit, R extends RootChit<P>> {
|
|
|
89
91
|
private chitLookup: ChitLookup = {};
|
|
90
92
|
private lockedChitStates: ChitStateLookup = {};
|
|
91
93
|
private lastChitStates: ChitStateLookup = {};
|
|
94
|
+
private dirtyChitIds = new Set<string>();
|
|
95
|
+
private pendingNewChits = new Set<Chit>();
|
|
96
|
+
private _statePool?: Map<string, string>;
|
|
97
|
+
|
|
98
|
+
private get statePool(): Map<string, string> {
|
|
99
|
+
if (this.parent) return this.parent.statePool;
|
|
100
|
+
if (!this._statePool) this._statePool = new Map();
|
|
101
|
+
return this._statePool;
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
/**
|
|
105
|
+
* Intern a serialized state string. If `hint` is provided (e.g. the last known state),
|
|
106
|
+
* a cheap content comparison is done first to avoid the Map lookup entirely — which matters
|
|
107
|
+
* because Map.get with large string keys is O(n) on string length.
|
|
108
|
+
*/
|
|
109
|
+
private internState(s: string, hint?: string): string {
|
|
110
|
+
if (hint !== undefined && s === hint) return hint;
|
|
111
|
+
const pool = this.statePool;
|
|
112
|
+
const existing = pool.get(s);
|
|
113
|
+
if (existing !== undefined) return existing;
|
|
114
|
+
pool.set(s, s);
|
|
115
|
+
return s;
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
/** @internal */
|
|
119
|
+
public notifyChitDirty(chit: Chit) {
|
|
120
|
+
if (chit.id) {
|
|
121
|
+
this.dirtyChitIds.add(chit.id);
|
|
122
|
+
} else {
|
|
123
|
+
this.pendingNewChits.add(chit);
|
|
124
|
+
}
|
|
125
|
+
}
|
|
92
126
|
|
|
93
127
|
/**
|
|
94
128
|
* Locates a chit by its ID. If not found, will throw.
|
|
@@ -160,7 +194,7 @@ export class Turn<T, P extends PlayerChit, R extends RootChit<P>> {
|
|
|
160
194
|
if (!c.id) {
|
|
161
195
|
throw new Error("Cannot serialize a chit without an id");
|
|
162
196
|
}
|
|
163
|
-
this.lastChitStates[c.id] = this.lockedChitStates[c.id] = c.serialize(this._playerIds);
|
|
197
|
+
this.lastChitStates[c.id] = this.lockedChitStates[c.id] = this.internState(c.serialize(this._playerIds));
|
|
164
198
|
});
|
|
165
199
|
}
|
|
166
200
|
|
|
@@ -245,6 +279,8 @@ export class Turn<T, P extends PlayerChit, R extends RootChit<P>> {
|
|
|
245
279
|
step.log = message;
|
|
246
280
|
}
|
|
247
281
|
|
|
282
|
+
_zipStartPoint = 0;
|
|
283
|
+
|
|
248
284
|
/**
|
|
249
285
|
* Compress the history in the timeline such that a user with maybe many moves (and changing their minds) won't
|
|
250
286
|
* clock up the replay timeline. Basically takes all of the flushes and smushes them together into a single flush.
|
|
@@ -256,7 +292,7 @@ export class Turn<T, P extends PlayerChit, R extends RootChit<P>> {
|
|
|
256
292
|
zip() {
|
|
257
293
|
this.flush();
|
|
258
294
|
let hasChange = false;
|
|
259
|
-
while (this.clockSteps.length >= 2) {
|
|
295
|
+
while (this.clockSteps.length >= this._zipStartPoint + 2) {
|
|
260
296
|
const lastStep = this.clockSteps[this.clockSteps.length - 1];
|
|
261
297
|
const previousStep = this.clockSteps[this.clockSteps.length - 2];
|
|
262
298
|
if (!(lastStep instanceof FlushClockStep) || !(previousStep instanceof FlushClockStep)) {
|
|
@@ -270,101 +306,107 @@ export class Turn<T, P extends PlayerChit, R extends RootChit<P>> {
|
|
|
270
306
|
|
|
271
307
|
if (hasChange) {
|
|
272
308
|
this.pass++;
|
|
273
|
-
this.flush();
|
|
309
|
+
this.flush(true);
|
|
274
310
|
}
|
|
275
311
|
}
|
|
276
312
|
|
|
313
|
+
/**
|
|
314
|
+
* Marks the current point in the clock steps as a "zip start point". This does nothing by itself, but when you later call zip(),
|
|
315
|
+
* it will not zip past this point
|
|
316
|
+
*/
|
|
317
|
+
markZipStartPoint() {
|
|
318
|
+
this._zipStartPoint = this.clockSteps.length;
|
|
319
|
+
}
|
|
320
|
+
|
|
277
321
|
/**
|
|
278
322
|
* Scan all of the chits managed by this turn. If any of them have changed, group them together into a
|
|
279
323
|
* "ClockStep". If any new chits appear, then add them to our lookup. If any chits are deleted (orphaned),
|
|
280
324
|
* then we have to identify those as well.
|
|
281
325
|
*/
|
|
282
326
|
flush(force = false) {
|
|
283
|
-
const seenIds = new Set<string>();
|
|
284
327
|
const newStates: ChitStateLookup = {};
|
|
285
328
|
let sawChange = force;
|
|
286
329
|
|
|
287
|
-
//
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
330
|
+
// 1. Assign IDs to pending new chits and their children
|
|
331
|
+
if (this.pendingNewChits.size > 0) {
|
|
332
|
+
// collect all chits needing IDs by walking each new chit's subtree
|
|
333
|
+
const chitsToAddIdsTo: Chit[] = [];
|
|
334
|
+
for (const c of this.pendingNewChits) {
|
|
335
|
+
if (!c.parent) continue; // created then deleted before flush — skip
|
|
336
|
+
c.walk((child) => {
|
|
337
|
+
if (!child.id) {
|
|
338
|
+
chitsToAddIdsTo.push(child);
|
|
339
|
+
}
|
|
340
|
+
});
|
|
292
341
|
}
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
existing
|
|
307
|
-
|
|
342
|
+
this.pendingNewChits.clear();
|
|
343
|
+
|
|
344
|
+
chitsToAddIdsTo.sort((a, b) => a.createdOrder - b.createdOrder);
|
|
345
|
+
for (const c of chitsToAddIdsTo) {
|
|
346
|
+
if (c.id) continue; // may have been assigned by a prior iteration
|
|
347
|
+
const type = c.chitTypeName();
|
|
348
|
+
const counter = (this.newChitCounter[type] || 0) + 1;
|
|
349
|
+
this.newChitCounter[type] = counter;
|
|
350
|
+
c.id = `${this.id}.${type}.${counter}`;
|
|
351
|
+
c.lock(this);
|
|
352
|
+
|
|
353
|
+
const existing = this.chitLookup[c.id];
|
|
354
|
+
this.chitLookup[c.id] = c;
|
|
355
|
+
if (existing) {
|
|
356
|
+
existing.removeFromParent();
|
|
357
|
+
existing.unlock(this);
|
|
358
|
+
}
|
|
359
|
+
this.dirtyChitIds.add(c.id);
|
|
308
360
|
}
|
|
309
|
-
}
|
|
361
|
+
}
|
|
310
362
|
|
|
311
|
-
//
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
if (serialized !== lastState) {
|
|
321
|
-
this.lastChitStates[c.id] = newStates[c.id] = serialized;
|
|
322
|
-
sawChange = true;
|
|
323
|
-
} else {
|
|
324
|
-
newStates[c.id] = serialized;
|
|
325
|
-
}
|
|
363
|
+
// 2. Serialize only dirty chits
|
|
364
|
+
for (const id of this.dirtyChitIds) {
|
|
365
|
+
const chit = this.chitLookup[id];
|
|
366
|
+
if (!chit) continue;
|
|
367
|
+
const lastState = this.lastChitStates[id];
|
|
368
|
+
const serialized = this.internState(chit.serialize(this._playerIds), lastState);
|
|
369
|
+
if (serialized !== lastState) {
|
|
370
|
+
this.lastChitStates[id] = newStates[id] = serialized;
|
|
371
|
+
sawChange = true;
|
|
326
372
|
} else {
|
|
327
|
-
|
|
373
|
+
newStates[id] = serialized;
|
|
328
374
|
}
|
|
329
|
-
}
|
|
375
|
+
}
|
|
376
|
+
this.dirtyChitIds.clear();
|
|
330
377
|
|
|
331
|
-
//
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
.forEach((chit) => {
|
|
348
|
-
if (chit.id) {
|
|
349
|
-
sawChange = true;
|
|
350
|
-
chit.unlock(this);
|
|
351
|
-
newStates[chit.id] = Chit.deletedIfSerialized();
|
|
352
|
-
// do not store this new state in lastChitStates, but rather delete this record from it altogether
|
|
353
|
-
chit.walk((c) => {
|
|
354
|
-
if (c.id) {
|
|
355
|
-
seenIds.add(c.id);
|
|
356
|
-
this.lastChitStates[c.id] = Chit.deletedIfSerialized();
|
|
357
|
-
newStates[c.id] = Chit.deletedIfSerialized();
|
|
358
|
-
chit.unlock(this);
|
|
359
|
-
}
|
|
360
|
-
});
|
|
361
|
-
}
|
|
362
|
-
});
|
|
378
|
+
// 4. Carry forward all non-dirty chit states
|
|
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
|
|
386
|
+
const deletedRoots: Chit[] = [];
|
|
387
|
+
for (const id of Object.keys(this.lastChitStates)) {
|
|
388
|
+
if (this.lastChitStates[id] === Chit.deletedIfSerialized()) continue;
|
|
389
|
+
const chit = this.chitLookup[id];
|
|
390
|
+
if (chit && !chit.parent && !this.chitsToLock.includes(chit)) {
|
|
391
|
+
deletedRoots.push(chit);
|
|
392
|
+
}
|
|
393
|
+
}
|
|
363
394
|
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
395
|
+
for (const chit of deletedRoots) {
|
|
396
|
+
if (chit.id) {
|
|
397
|
+
sawChange = true;
|
|
398
|
+
chit.unlock(this);
|
|
399
|
+
newStates[chit.id] = Chit.deletedIfSerialized();
|
|
400
|
+
this.lastChitStates[chit.id] = Chit.deletedIfSerialized();
|
|
401
|
+
// walk only the deleted subtree to mark descendants as deleted
|
|
402
|
+
chit.walk((c) => {
|
|
403
|
+
if (c.id) {
|
|
404
|
+
this.lastChitStates[c.id] = Chit.deletedIfSerialized();
|
|
405
|
+
newStates[c.id] = Chit.deletedIfSerialized();
|
|
406
|
+
chit.unlock(this);
|
|
407
|
+
}
|
|
408
|
+
});
|
|
409
|
+
}
|
|
368
410
|
}
|
|
369
411
|
|
|
370
412
|
if (sawChange) {
|
|
@@ -1147,6 +1189,13 @@ export class Turn<T, P extends PlayerChit, R extends RootChit<P>> {
|
|
|
1147
1189
|
});
|
|
1148
1190
|
|
|
1149
1191
|
this.lastChitStates = { ...this.lockedChitStates }; // reset our known chit states
|
|
1192
|
+
this.dirtyChitIds.clear();
|
|
1193
|
+
this.pendingNewChits.clear();
|
|
1194
|
+
// chits were deserialized back to locked state — mark them clean so future
|
|
1195
|
+
// modifications correctly trigger markDirty() → notifyChitDirty()
|
|
1196
|
+
Chit.walk(this.chitsToLock, (c) => {
|
|
1197
|
+
c._dirty = false;
|
|
1198
|
+
});
|
|
1150
1199
|
this.clockSteps = [];
|
|
1151
1200
|
this.decisionIndex = 0;
|
|
1152
1201
|
this.newChitCounter = {};
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { Match } from "../..";
|
|
1
2
|
import { ClockDetails } from "../ClockDetails";
|
|
2
3
|
import { Connection } from "../Connection";
|
|
3
4
|
import { ConnectionObject } from "../ConnectionObject";
|
|
@@ -13,6 +14,7 @@ export class BadAIClientPrompts<P extends PlayerChit, R extends RootChit<P>> ext
|
|
|
13
14
|
constructor(
|
|
14
15
|
private playerId: string,
|
|
15
16
|
private connection: Connection,
|
|
17
|
+
private match: Match<P, R>,
|
|
16
18
|
) {
|
|
17
19
|
super();
|
|
18
20
|
|
|
@@ -24,26 +26,53 @@ export class BadAIClientPrompts<P extends PlayerChit, R extends RootChit<P>> ext
|
|
|
24
26
|
if (prompt && playerId === this.playerId) {
|
|
25
27
|
if (prompt.type === "PickPrompt") {
|
|
26
28
|
const picks = prompt.details.picks;
|
|
27
|
-
const
|
|
29
|
+
const weights = picks.map((p: any) => {
|
|
30
|
+
const multiplier = this.match.game.demoPickWeight ? this.match.game.demoPickWeight(picks, p) : 1;
|
|
31
|
+
switch (p.type as PickType) {
|
|
32
|
+
case "ButtonPick":
|
|
33
|
+
return 1 * multiplier;
|
|
34
|
+
case "ChitPick":
|
|
35
|
+
return p.details.c.length * multiplier;
|
|
36
|
+
case "DragPick":
|
|
37
|
+
return (
|
|
38
|
+
(p.details.d as any[][]).reduce((sum: number, targets: any[]) => sum + targets.length, 0) * multiplier
|
|
39
|
+
);
|
|
40
|
+
default:
|
|
41
|
+
return 1 * multiplier;
|
|
42
|
+
}
|
|
43
|
+
});
|
|
44
|
+
const totalWeight = weights.reduce((sum: number, w: number) => sum + w, 0);
|
|
45
|
+
let roll = Math.random() * totalWeight;
|
|
46
|
+
let pickIndex = 0;
|
|
47
|
+
for (let i = 0; i < weights.length; i++) {
|
|
48
|
+
roll -= weights[i];
|
|
49
|
+
if (roll <= 0) {
|
|
50
|
+
pickIndex = i;
|
|
51
|
+
break;
|
|
52
|
+
}
|
|
53
|
+
}
|
|
28
54
|
const pick = picks[pickIndex];
|
|
29
55
|
switch (pick.type as PickType) {
|
|
30
56
|
case "ButtonPick": {
|
|
31
|
-
await this.serverPrompts.resolvePrompt({
|
|
57
|
+
await this.serverPrompts.resolvePrompt(prompt.id, {
|
|
32
58
|
idx: pickIndex,
|
|
59
|
+
pickType: "ButtonPick",
|
|
33
60
|
});
|
|
34
61
|
break;
|
|
35
62
|
}
|
|
36
63
|
case "ChitPick": {
|
|
37
|
-
await this.serverPrompts.resolvePrompt({
|
|
64
|
+
await this.serverPrompts.resolvePrompt(prompt.id, {
|
|
38
65
|
idx: pickIndex,
|
|
66
|
+
pickType: "ChitPick",
|
|
39
67
|
value: pick.details.c[Math.floor(Math.random() * pick.details.c.length)],
|
|
40
68
|
});
|
|
41
69
|
break;
|
|
42
70
|
}
|
|
43
71
|
case "DragPick": {
|
|
44
72
|
const target = pick.details.d[Math.floor(Math.random() * pick.details.d.length)];
|
|
45
|
-
await this.serverPrompts.resolvePrompt({
|
|
73
|
+
await this.serverPrompts.resolvePrompt(prompt.id, {
|
|
46
74
|
idx: pickIndex,
|
|
75
|
+
pickType: "DragPick",
|
|
47
76
|
value: {
|
|
48
77
|
chitId: pick.details.c[Math.floor(Math.random() * pick.details.c.length)],
|
|
49
78
|
targetChitId: target[Math.floor(Math.random() * target.length)],
|
|
@@ -37,6 +37,30 @@ export class ClientPrompts<P extends PlayerChit, R extends RootChit<P>> extends
|
|
|
37
37
|
}
|
|
38
38
|
}
|
|
39
39
|
|
|
40
|
+
private stageInPrompt(promptSpec: PromptSerialization) {
|
|
41
|
+
this._currentPromptSpec = promptSpec;
|
|
42
|
+
const prompt = Prompt.deserialize(promptSpec, this.clientTime.findChit, this.clientTime.game.buttonLibrary);
|
|
43
|
+
prompt.onResolve(async (success) => {
|
|
44
|
+
try {
|
|
45
|
+
this.fixActiveLog();
|
|
46
|
+
if (success) {
|
|
47
|
+
const newPromptSpec = await this.serverPrompts.resolvePrompt(prompt.id, prompt.response);
|
|
48
|
+
this.getPromptEventChannelForPlayer(this.playerId).value = newPromptSpec ?? undefined;
|
|
49
|
+
} else if (prompt.shouldStepBack) {
|
|
50
|
+
this.getPromptEventChannelForPlayer(this.playerId).value = undefined;
|
|
51
|
+
await this.serverPrompts.stepBackPrompt(prompt.shouldReset ?? false);
|
|
52
|
+
}
|
|
53
|
+
} catch (e) {
|
|
54
|
+
prompt.stageOut();
|
|
55
|
+
this.currentPrompt.value = undefined;
|
|
56
|
+
}
|
|
57
|
+
});
|
|
58
|
+
prompt.stageIn();
|
|
59
|
+
|
|
60
|
+
this.currentPrompt.value = prompt;
|
|
61
|
+
this.fixActiveLog();
|
|
62
|
+
}
|
|
63
|
+
|
|
40
64
|
private checkIfPromptCanBeInflated() {
|
|
41
65
|
const isLive = this.clientTime.clientTimeState.live.value;
|
|
42
66
|
const currentTime = this.clientTime.currentClock.value.clock;
|
|
@@ -59,22 +83,23 @@ export class ClientPrompts<P extends PlayerChit, R extends RootChit<P>> extends
|
|
|
59
83
|
this.currentPrompt.value.stageOut();
|
|
60
84
|
}
|
|
61
85
|
|
|
62
|
-
this.
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
86
|
+
this.stageInPrompt(promptSpec);
|
|
87
|
+
} else if (
|
|
88
|
+
isLive &&
|
|
89
|
+
promptSpec &&
|
|
90
|
+
currentTime > 0 &&
|
|
91
|
+
promptSpec.canStageInEarly > 0 &&
|
|
92
|
+
currentTime >= maxTime - promptSpec.canStageInEarly &&
|
|
93
|
+
samePasses(this.clientTime.currentClock.value, this.clientTime.maxClock.value)
|
|
94
|
+
) {
|
|
95
|
+
// if we are live and have a prompt spec, but our current time is greater than our max time, then we should stage in the prompt, but not mark it as the current prompt spec, so that if we go back in time we can stage out the prompt and stage it back in when we return to the current time
|
|
96
|
+
if (this.currentPrompt.value && this.currentPrompt.value.isSameSerialization(promptSpec)) {
|
|
97
|
+
return;
|
|
98
|
+
}
|
|
99
|
+
if (this.currentPrompt.value) {
|
|
100
|
+
this.currentPrompt.value.stageOut();
|
|
101
|
+
}
|
|
102
|
+
this.stageInPrompt(promptSpec);
|
|
78
103
|
} else {
|
|
79
104
|
this._currentPromptSpec = undefined;
|
|
80
105
|
if (this.currentPrompt.value) {
|
|
@@ -85,6 +110,15 @@ export class ClientPrompts<P extends PlayerChit, R extends RootChit<P>> extends
|
|
|
85
110
|
}
|
|
86
111
|
}
|
|
87
112
|
|
|
113
|
+
disconnect() {
|
|
114
|
+
this._currentPromptSpec = undefined;
|
|
115
|
+
if (this.currentPrompt.value) {
|
|
116
|
+
this.currentPrompt.value.stageOut();
|
|
117
|
+
}
|
|
118
|
+
this.currentPrompt.value = undefined;
|
|
119
|
+
this.fixActiveLog();
|
|
120
|
+
}
|
|
121
|
+
|
|
88
122
|
public async setPromptForPlayer(playerId: string, prompt?: PromptSerialization, clockDetails?: ClockDetails) {
|
|
89
123
|
this.getPromptEventChannelForPlayer(playerId).value = prompt;
|
|
90
124
|
if (clockDetails) {
|
|
@@ -148,74 +148,79 @@ export class ClientTime extends ConnectionObject {
|
|
|
148
148
|
const animationKey = `minimumAnimationDuration${Date.now()}`;
|
|
149
149
|
this.clientTimeState.setAnimationState(animationKey, true);
|
|
150
150
|
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
if (newTargetClock
|
|
157
|
-
this.
|
|
158
|
-
|
|
151
|
+
try {
|
|
152
|
+
const response = await this.serverTime.serializeDelta(this.clientTimeState.targetClock.value);
|
|
153
|
+
const serializedChits = this.inflateSerializedResponse(response.newStates, response.chits);
|
|
154
|
+
|
|
155
|
+
// make sure nothing changed while we were waiting...
|
|
156
|
+
if (this.clientTimeState.targetClock.value === newTargetClock && currentClock === this.currentClock.value) {
|
|
157
|
+
if (newTargetClock >= this.startTime) {
|
|
158
|
+
this.clientTimeState.isLoading.value = false;
|
|
159
|
+
}
|
|
159
160
|
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
161
|
+
// track the active log message
|
|
162
|
+
this.activeLog.value = response.log?.message;
|
|
163
|
+
this.clientPrompt?.fixActiveLog();
|
|
164
|
+
|
|
165
|
+
// first make sure all chits exist (because they may link to each other)
|
|
166
|
+
Object.entries(serializedChits).forEach(([id, value]) => {
|
|
167
|
+
let chit = this.chitLookup[id];
|
|
168
|
+
if (!chit) {
|
|
169
|
+
const c = Chit.deflate(value, this.game);
|
|
170
|
+
if (c) {
|
|
171
|
+
chit = this.chitLookup[id] = c;
|
|
172
|
+
}
|
|
171
173
|
}
|
|
172
|
-
}
|
|
173
|
-
});
|
|
174
|
+
});
|
|
174
175
|
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
176
|
+
Object.values(this.chitLookup).forEach((chit) => {
|
|
177
|
+
if (chit.id && !serializedChits[chit.id]) {
|
|
178
|
+
if (chit.parentFallback) {
|
|
179
|
+
chit.beginDeserializing();
|
|
180
|
+
chit.setParent(chit.parentFallback, chit.parentOutlet ?? "graveyard");
|
|
181
|
+
chit.doneDeserializing();
|
|
182
|
+
} else {
|
|
183
|
+
chit.removeFromParent();
|
|
184
|
+
}
|
|
185
|
+
delete this.lastSerializedState[chit.id];
|
|
183
186
|
}
|
|
184
|
-
|
|
185
|
-
}
|
|
186
|
-
});
|
|
187
|
+
});
|
|
187
188
|
|
|
188
|
-
|
|
189
|
+
this.currentClock.value = response.clockDetails;
|
|
189
190
|
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
191
|
+
// now actually load the new state
|
|
192
|
+
const changedIds = new Set(
|
|
193
|
+
Object.entries(serializedChits)
|
|
194
|
+
.filter(([id, value]) => this.chitLookup[id] && this.lastSerializedState[id] !== value)
|
|
195
|
+
.map(([id]) => id),
|
|
196
|
+
);
|
|
196
197
|
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
198
|
+
const chits: Chit[] = Object.entries(serializedChits)
|
|
199
|
+
.filter(([id, value]) => this.chitLookup[id] && this.lastSerializedState[id] !== value)
|
|
200
|
+
.map(([id]) => this.chitLookup[id]);
|
|
200
201
|
|
|
201
|
-
|
|
202
|
+
chits.forEach((chit) => chit.beginDeserializing());
|
|
202
203
|
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
204
|
+
Object.entries(serializedChits)
|
|
205
|
+
.filter(([id]) => changedIds.has(id))
|
|
206
|
+
.forEach(([id, value]) => {
|
|
207
|
+
const chit = this.chitLookup[id];
|
|
208
|
+
chit.deserialize(value, this.findChit);
|
|
209
|
+
this.lastSerializedState[id] = value;
|
|
210
|
+
});
|
|
210
211
|
|
|
211
|
-
|
|
212
|
+
chits.forEach((chit) => chit.doneDeserializing());
|
|
212
213
|
|
|
213
|
-
|
|
214
|
+
this.rootChit.value = this.findChit("root") as RootChit<any>;
|
|
214
215
|
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
216
|
+
// sometimes deserializing chits does not result in animations (maybe a pure texture change?)
|
|
217
|
+
// in that case, we need to make sure that the clock moves forward
|
|
218
|
+
setTimeout(() => this.clientTimeState.setAnimationState(animationKey, false), 100);
|
|
219
|
+
} else {
|
|
220
|
+
this.clientTimeState.setAnimationState(animationKey, false);
|
|
221
|
+
}
|
|
222
|
+
} catch (e) {
|
|
223
|
+
console.error(e);
|
|
219
224
|
this.clientTimeState.setAnimationState(animationKey, false);
|
|
220
225
|
}
|
|
221
226
|
}
|
|
@@ -29,11 +29,13 @@ export class ServerPrompts<P extends PlayerChit, R extends RootChit<P>> extends
|
|
|
29
29
|
if (newTurn) {
|
|
30
30
|
unsubs = newTurn.rootChit.players.map((player) =>
|
|
31
31
|
player.promptStatus.latestPrompt.on((latestPrompt: any) =>
|
|
32
|
-
this.clientPrompts
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
32
|
+
this.clientPrompts
|
|
33
|
+
.setPromptForPlayer(
|
|
34
|
+
player.playerId,
|
|
35
|
+
latestPrompt?.serialize(),
|
|
36
|
+
this.match.turn.value?.clockDetails(playerId),
|
|
37
|
+
)
|
|
38
|
+
.catch((e: any) => console.error("Failed to send prompt to client:", e)),
|
|
37
39
|
),
|
|
38
40
|
);
|
|
39
41
|
}
|
|
@@ -51,14 +53,30 @@ export class ServerPrompts<P extends PlayerChit, R extends RootChit<P>> extends
|
|
|
51
53
|
return this.match.turn.value?.rootChit.players;
|
|
52
54
|
}
|
|
53
55
|
|
|
54
|
-
async resolvePrompt(response: any): Promise<void | PromptSerialization> {
|
|
56
|
+
async resolvePrompt(promptId: string, response: any): Promise<void | PromptSerialization> {
|
|
55
57
|
const player = this.playerChits?.find((p) => p.playerId === this.playerId);
|
|
56
58
|
if (player && player.promptStatus.latestPrompt.value) {
|
|
59
|
+
const currentPrompt = player.promptStatus.latestPrompt.value;
|
|
60
|
+
|
|
61
|
+
if (currentPrompt.id !== promptId) {
|
|
62
|
+
console.warn(
|
|
63
|
+
`[ServerPrompts] Prompt ID mismatch for player ${this.playerId}: received "${promptId}", expected "${currentPrompt.id}". Ignoring resolution.`,
|
|
64
|
+
);
|
|
65
|
+
return currentPrompt.serialize();
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
if (!currentPrompt.canResolveResponse(response)) {
|
|
69
|
+
console.warn(
|
|
70
|
+
`[ServerPrompts] Prompt "${currentPrompt.id}" for player ${this.playerId} cannot resolve response (type=${currentPrompt.type}). Ignoring resolution.`,
|
|
71
|
+
);
|
|
72
|
+
return currentPrompt.serialize();
|
|
73
|
+
}
|
|
74
|
+
|
|
57
75
|
let cb: (() => void) | undefined;
|
|
58
76
|
const p = new Promise((resolve) => {
|
|
59
77
|
cb = this.match.onChange(() => resolve(0), false);
|
|
60
78
|
});
|
|
61
|
-
|
|
79
|
+
currentPrompt.resolve(response);
|
|
62
80
|
await p;
|
|
63
81
|
if (cb) {
|
|
64
82
|
cb();
|
|
@@ -18,13 +18,17 @@ export class ServerStatus<P extends PlayerChit, R extends RootChit<P>> extends C
|
|
|
18
18
|
|
|
19
19
|
this.register(
|
|
20
20
|
this.match.errorState.on((errorMessage) => {
|
|
21
|
-
this.clientStatus
|
|
21
|
+
this.clientStatus
|
|
22
|
+
.setErrorMessage(errorMessage)
|
|
23
|
+
.catch((e: any) => console.error("Failed to send error message to client:", e));
|
|
22
24
|
}),
|
|
23
25
|
);
|
|
24
26
|
|
|
25
27
|
this.register(
|
|
26
28
|
this.match.result.on((result) => {
|
|
27
|
-
this.clientStatus
|
|
29
|
+
this.clientStatus
|
|
30
|
+
.setMatchResult(result?.winners?.map((winner) => winner.id ?? ""))
|
|
31
|
+
.catch((e: any) => console.error("Failed to send match result to client:", e));
|
|
28
32
|
}),
|
|
29
33
|
);
|
|
30
34
|
}
|
|
@@ -20,7 +20,9 @@ export class ServerTime<P extends PlayerChit, R extends RootChit<P>> extends Con
|
|
|
20
20
|
this.register(
|
|
21
21
|
match.onChange(() => {
|
|
22
22
|
if (match.turn.value) {
|
|
23
|
-
this.clientTime
|
|
23
|
+
this.clientTime
|
|
24
|
+
.newMaxClock(match.turn.value.clockDetails(this.playerId))
|
|
25
|
+
.catch((e: any) => console.error("Failed to send newMaxClock to client:", e));
|
|
24
26
|
}
|
|
25
27
|
|
|
26
28
|
if (!this.hasSentLastActionTime) {
|
|
@@ -28,7 +30,9 @@ export class ServerTime<P extends PlayerChit, R extends RootChit<P>> extends Con
|
|
|
28
30
|
|
|
29
31
|
const player = match.turn.value?.rootChit.players.find((p) => p.id === playerId);
|
|
30
32
|
if (player) {
|
|
31
|
-
this.clientTime
|
|
33
|
+
this.clientTime
|
|
34
|
+
.setStartTime(player.promptStatus.latestPromptResponseTime)
|
|
35
|
+
.catch((e: any) => console.error("Failed to send setStartTime to client:", e));
|
|
32
36
|
}
|
|
33
37
|
}
|
|
34
38
|
}),
|