@terrafirmagreg-team/recipe 1.2.12 → 1.2.14
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/package.json +4 -4
- package/src/components/tree.tsx +4 -1
- package/src/components/views/browser/axis-switcher.tsx +5 -2
- package/src/components/views/browser/index.tsx +6 -2
- package/src/components/views/browser/item-panel/index.tsx +5 -2
- package/src/components/views/browser/toolbar.tsx +5 -2
- package/src/components/views/calc/calculator/index.tsx +3 -0
- package/src/components/views/calc/flow/index.tsx +10 -2
- package/src/components/views/calc/flow/picker-dialogs.tsx +122 -5
- package/src/components/views/calc/node-card/full.tsx +0 -1
- package/src/components/views/calc/node-card/menu.tsx +17 -1
- package/src/components/views/calc/node-card/port-cells.tsx +23 -6
- package/src/components/views/calc/node-card/port-rails.tsx +1 -3
- package/src/hooks/calc/flow/use-handlers.ts +14 -6
- package/src/hooks/calc/flow/use-layout.ts +5 -0
- package/src/hooks/calc/flow/use-pickers.ts +15 -0
- package/src/hooks/calc/flow/use-viewport.ts +1 -1
- package/src/hooks/calc/use-actions.ts +144 -4
- package/src/hooks/calc/use-defaults.ts +4 -0
- package/src/hooks/calc/use-resolver.ts +1 -0
- package/src/hooks/calc/use-storage.ts +6 -1
- package/src/hooks/use-index.ts +10 -0
- package/src/lib/calc/defaults.ts +43 -5
- package/src/lib/calc/flow.ts +32 -14
- package/src/lib/calc/scheme.ts +24 -16
- package/src/lib/calc/storage/index.ts +1 -0
- package/src/lib/calc/storage/keys.ts +1 -1
- package/src/lib/calc/storage/store.ts +62 -0
- package/src/lib/calc/tree/recipe.ts +1 -0
- package/src/lib/calc/wire.ts +39 -0
- package/src/lib/calc/worker/resolve.ts +7 -0
- package/src/lib/client-index/chrome.ts +56 -38
- package/src/lib/client-index/index.ts +1 -1
- package/src/lib/links.ts +2 -1
- package/src/lib/og.ts +127 -0
- package/src/lib/tool-names.ts +20 -0
- package/src/types/calc-flow-props.ts +1 -0
- package/src/types/calc.ts +2 -1
- package/styles/calc.css +4 -0
|
@@ -10,6 +10,10 @@ import { useCopyButton } from '@terrafirmagreg-team/ui/hooks/use-copy-button';
|
|
|
10
10
|
import type { DatasetRef } from '@terrafirmagreg-team/sites/location';
|
|
11
11
|
import { recipeCalcHref, recipeStackHref } from '@terrafirmagreg-team/recipe/lib/links';
|
|
12
12
|
import type { Recipe } from '@terrafirmagreg-team/recipe/lib/data/dto';
|
|
13
|
+
import { loadClientRecipe } from '@terrafirmagreg-team/recipe/lib/client/recipe';
|
|
14
|
+
import { resolveClientTag } from '@terrafirmagreg-team/dump/lib/dump/client/tags';
|
|
15
|
+
import { toCalcRecipe } from '@terrafirmagreg-team/recipe/lib/calc/tree';
|
|
16
|
+
import { CALC_NODE_W } from '@terrafirmagreg-team/recipe/lib/calc/flow';
|
|
13
17
|
import type { Lang } from '@terrafirmagreg-team/translate/types/locale';
|
|
14
18
|
import type {
|
|
15
19
|
CalcCard,
|
|
@@ -17,7 +21,9 @@ import type {
|
|
|
17
21
|
CalcNode,
|
|
18
22
|
CalculatorState,
|
|
19
23
|
CalcWire,
|
|
24
|
+
TagResolution,
|
|
20
25
|
} from '@terrafirmagreg-team/recipe/types/calc';
|
|
26
|
+
import type { RecipeRelation } from '@terrafirmagreg-team/recipe/types';
|
|
21
27
|
import type { CalcFlowData } from '@terrafirmagreg-team/recipe/lib/calc/flow';
|
|
22
28
|
|
|
23
29
|
let cardSeq = 0;
|
|
@@ -38,6 +44,8 @@ export function useCalcActions({
|
|
|
38
44
|
flushAmounts,
|
|
39
45
|
setFocusNodeId,
|
|
40
46
|
nodes,
|
|
47
|
+
tagMembers,
|
|
48
|
+
ensureMaterial,
|
|
41
49
|
}: {
|
|
42
50
|
dataset: DatasetRef;
|
|
43
51
|
lang: Lang;
|
|
@@ -52,6 +60,8 @@ export function useCalcActions({
|
|
|
52
60
|
flushAmounts: () => CalcCard[];
|
|
53
61
|
setFocusNodeId: Dispatch<SetStateAction<string | null>>;
|
|
54
62
|
nodes: CalcNode[];
|
|
63
|
+
tagMembers?: (tagId: string) => TagResolution | undefined;
|
|
64
|
+
ensureMaterial?: (materialId: string) => void;
|
|
55
65
|
}) {
|
|
56
66
|
const { checked: shared, onClick: onShare } = useCopyButton(async () => {
|
|
57
67
|
const tree = encodeShare();
|
|
@@ -170,10 +180,105 @@ export function useCalcActions({
|
|
|
170
180
|
[nodes, cards, wires, flushAmounts, persist, tagSelections],
|
|
171
181
|
);
|
|
172
182
|
|
|
183
|
+
const onDemandOutput = useCallback(
|
|
184
|
+
(cardId: string, outputIndex: number, recipeId: string) => {
|
|
185
|
+
const srcNode = nodes.find((n) => n.id === cardId);
|
|
186
|
+
const srcCard = cards.find((row) => row.id === cardId);
|
|
187
|
+
const out = srcNode?.outputs[outputIndex];
|
|
188
|
+
if (!srcNode || !srcCard || !out) return;
|
|
189
|
+
const need =
|
|
190
|
+
out.remaining > 0 ? out.remaining : Math.max(1, out.amount);
|
|
191
|
+
const base = srcCard.pos;
|
|
192
|
+
void (async () => {
|
|
193
|
+
const body = await loadClientRecipe(dataset, recipeId).catch(
|
|
194
|
+
() => undefined,
|
|
195
|
+
);
|
|
196
|
+
const calc = body ? toCalcRecipe(body) : undefined;
|
|
197
|
+
const inputs = calc?.inputs ?? [];
|
|
198
|
+
let inputIndex = inputs.findIndex(
|
|
199
|
+
(slot) =>
|
|
200
|
+
!slot.catalyst && slot.id === out.id && slot.kind === out.kind,
|
|
201
|
+
);
|
|
202
|
+
let pickedTag: string | undefined;
|
|
203
|
+
if (inputIndex < 0) {
|
|
204
|
+
for (let i = 0; i < inputs.length; i += 1) {
|
|
205
|
+
const slot = inputs[i];
|
|
206
|
+
if (!slot?.tagId || slot.catalyst) continue;
|
|
207
|
+
const members =
|
|
208
|
+
tagMembers?.(slot.tagId)?.members ??
|
|
209
|
+
(
|
|
210
|
+
await resolveClientTag(dataset, slot.tagId).catch(
|
|
211
|
+
() => undefined,
|
|
212
|
+
)
|
|
213
|
+
)?.members ??
|
|
214
|
+
[];
|
|
215
|
+
if (members.includes(out.id)) {
|
|
216
|
+
inputIndex = i;
|
|
217
|
+
pickedTag = slot.tagId;
|
|
218
|
+
break;
|
|
219
|
+
}
|
|
220
|
+
}
|
|
221
|
+
}
|
|
222
|
+
const product = calc?.outputs[0];
|
|
223
|
+
const materialId = product?.id ?? out.id;
|
|
224
|
+
const kind = product?.kind ?? out.kind;
|
|
225
|
+
const inputAmount =
|
|
226
|
+
inputIndex >= 0 ? (inputs[inputIndex]?.amount ?? 1) : 1;
|
|
227
|
+
const crafts = Math.max(1, Math.ceil(need / inputAmount));
|
|
228
|
+
const id = newCardId();
|
|
229
|
+
const card: CalcCard = {
|
|
230
|
+
id,
|
|
231
|
+
materialId,
|
|
232
|
+
kind,
|
|
233
|
+
recipeId,
|
|
234
|
+
crafts,
|
|
235
|
+
pos: base
|
|
236
|
+
? {
|
|
237
|
+
x: base.x + CALC_NODE_W + 90,
|
|
238
|
+
y: base.y + 40 + outputIndex * 24,
|
|
239
|
+
}
|
|
240
|
+
: undefined,
|
|
241
|
+
};
|
|
242
|
+
persist({
|
|
243
|
+
cards: [...flushAmounts(), card],
|
|
244
|
+
wires:
|
|
245
|
+
inputIndex >= 0
|
|
246
|
+
? [
|
|
247
|
+
...wires,
|
|
248
|
+
{
|
|
249
|
+
from: cardId,
|
|
250
|
+
to: id,
|
|
251
|
+
input: inputIndex,
|
|
252
|
+
out: outputIndex,
|
|
253
|
+
},
|
|
254
|
+
]
|
|
255
|
+
: wires,
|
|
256
|
+
tagSelections: pickedTag
|
|
257
|
+
? { ...tagSelections, [pickedTag]: out.id }
|
|
258
|
+
: tagSelections,
|
|
259
|
+
});
|
|
260
|
+
ensureMaterial?.(materialId);
|
|
261
|
+
setFocusNodeId(id);
|
|
262
|
+
})();
|
|
263
|
+
},
|
|
264
|
+
[
|
|
265
|
+
nodes,
|
|
266
|
+
cards,
|
|
267
|
+
wires,
|
|
268
|
+
flushAmounts,
|
|
269
|
+
persist,
|
|
270
|
+
tagSelections,
|
|
271
|
+
dataset,
|
|
272
|
+
tagMembers,
|
|
273
|
+
ensureMaterial,
|
|
274
|
+
setFocusNodeId,
|
|
275
|
+
],
|
|
276
|
+
);
|
|
277
|
+
|
|
173
278
|
const onOpenItem = useCallback(
|
|
174
|
-
(materialId: string, kind: CalcKind) => {
|
|
279
|
+
(materialId: string, kind: CalcKind, relation?: RecipeRelation) => {
|
|
175
280
|
window.open(
|
|
176
|
-
recipeStackHref(dataset, lang, materialId, { kind }),
|
|
281
|
+
recipeStackHref(dataset, lang, materialId, { kind, relation }),
|
|
177
282
|
'_blank',
|
|
178
283
|
'noopener',
|
|
179
284
|
);
|
|
@@ -216,9 +321,29 @@ export function useCalcActions({
|
|
|
216
321
|
[flushAmounts, write],
|
|
217
322
|
);
|
|
218
323
|
|
|
324
|
+
const tagPinFor = useCallback(
|
|
325
|
+
(
|
|
326
|
+
to: string,
|
|
327
|
+
input: number,
|
|
328
|
+
from: string,
|
|
329
|
+
out?: number,
|
|
330
|
+
): string | undefined => {
|
|
331
|
+
const slot = nodes.find((n) => n.id === to)?.inputs[input];
|
|
332
|
+
const srcOut = nodes.find((n) => n.id === from)?.outputs[out ?? 0];
|
|
333
|
+
if (!slot?.tagId || !srcOut?.id) return undefined;
|
|
334
|
+
const member = slot.members?.includes(srcOut.id);
|
|
335
|
+
return member ?? slot.id === srcOut.id ? slot.tagId : undefined;
|
|
336
|
+
},
|
|
337
|
+
[nodes],
|
|
338
|
+
);
|
|
339
|
+
|
|
219
340
|
const onWire = useCallback(
|
|
220
341
|
(from: string, to: string, input: number, out?: number) => {
|
|
221
342
|
if (from === to) return;
|
|
343
|
+
const pinTag = tagPinFor(to, input, from, out);
|
|
344
|
+
const srcOut = nodes
|
|
345
|
+
.find((n) => n.id === from)
|
|
346
|
+
?.outputs[out ?? 0];
|
|
222
347
|
write({
|
|
223
348
|
wires: [
|
|
224
349
|
...wires.filter(
|
|
@@ -226,9 +351,13 @@ export function useCalcActions({
|
|
|
226
351
|
),
|
|
227
352
|
{ from, to, input, out: out || undefined },
|
|
228
353
|
],
|
|
354
|
+
tagSelections:
|
|
355
|
+
pinTag && srcOut?.id
|
|
356
|
+
? { ...tagSelections, [pinTag]: srcOut.id }
|
|
357
|
+
: tagSelections,
|
|
229
358
|
});
|
|
230
359
|
},
|
|
231
|
-
[wires, write],
|
|
360
|
+
[wires, write, nodes, tagSelections, tagPinFor],
|
|
232
361
|
);
|
|
233
362
|
|
|
234
363
|
const onUnwire = useCallback(
|
|
@@ -256,6 +385,10 @@ export function useCalcActions({
|
|
|
256
385
|
next: { from: string; to: string; input: number; out?: number },
|
|
257
386
|
) => {
|
|
258
387
|
if (next.from === next.to) return;
|
|
388
|
+
const pinTag = tagPinFor(next.to, next.input, next.from, next.out);
|
|
389
|
+
const srcOut = nodes
|
|
390
|
+
.find((n) => n.id === next.from)
|
|
391
|
+
?.outputs[next.out ?? 0];
|
|
259
392
|
write({
|
|
260
393
|
wires: [
|
|
261
394
|
...wires.filter(
|
|
@@ -265,9 +398,13 @@ export function useCalcActions({
|
|
|
265
398
|
),
|
|
266
399
|
next,
|
|
267
400
|
],
|
|
401
|
+
tagSelections:
|
|
402
|
+
pinTag && srcOut?.id
|
|
403
|
+
? { ...tagSelections, [pinTag]: srcOut.id }
|
|
404
|
+
: tagSelections,
|
|
268
405
|
});
|
|
269
406
|
},
|
|
270
|
-
[wires, write],
|
|
407
|
+
[wires, write, nodes, tagSelections, tagPinFor],
|
|
271
408
|
);
|
|
272
409
|
|
|
273
410
|
const onWireAmount = useCallback(
|
|
@@ -300,6 +437,7 @@ export function useCalcActions({
|
|
|
300
437
|
| 'onRequestPicker'
|
|
301
438
|
| 'onRequestMemberPicker'
|
|
302
439
|
| 'onRequestSupply'
|
|
440
|
+
| 'onRequestDemand'
|
|
303
441
|
| 'onCrafts'
|
|
304
442
|
| 'getRecipesForItem'
|
|
305
443
|
| 'ensureMaterial'
|
|
@@ -311,6 +449,7 @@ export function useCalcActions({
|
|
|
311
449
|
onSelectRecipe,
|
|
312
450
|
onSelectTagMember,
|
|
313
451
|
onSupplyInput,
|
|
452
|
+
onDemandOutput,
|
|
314
453
|
onAddCard,
|
|
315
454
|
onOpenItem,
|
|
316
455
|
onRemoveCard,
|
|
@@ -328,6 +467,7 @@ export function useCalcActions({
|
|
|
328
467
|
onSelectRecipe,
|
|
329
468
|
onSelectTagMember,
|
|
330
469
|
onSupplyInput,
|
|
470
|
+
onDemandOutput,
|
|
331
471
|
onAddCard,
|
|
332
472
|
onOpenItem,
|
|
333
473
|
onRemoveCard,
|
|
@@ -3,12 +3,16 @@ import type { DatasetRef } from '@terrafirmagreg-team/sites/location';
|
|
|
3
3
|
|
|
4
4
|
import { useCallback, useEffect, useState } from 'react';
|
|
5
5
|
import {
|
|
6
|
+
migrateRecipeDefaults,
|
|
6
7
|
readRecipeDefaults,
|
|
7
8
|
subscribeRecipeDefaults,
|
|
8
9
|
writeRecipeDefault,
|
|
9
10
|
} from '@terrafirmagreg-team/recipe/lib/calc/defaults';
|
|
10
11
|
|
|
11
12
|
export function useRecipeDefaults(dataset: DatasetRef) {
|
|
13
|
+
useEffect(() => {
|
|
14
|
+
migrateRecipeDefaults(dataset);
|
|
15
|
+
}, [dataset]);
|
|
12
16
|
const [defaultsState, setDefaultsState] = useState<{
|
|
13
17
|
dataset: DatasetRef;
|
|
14
18
|
map: Record<string, string>;
|
|
@@ -47,6 +47,7 @@ export function useCalcResolver({
|
|
|
47
47
|
const recipes = new Set<string>();
|
|
48
48
|
const tags = new Set<string>();
|
|
49
49
|
for (const card of cards) {
|
|
50
|
+
if (card.materialId.startsWith('#')) tags.add(card.materialId);
|
|
50
51
|
materials.add(card.materialId);
|
|
51
52
|
if (card.recipeId) recipes.add(card.recipeId);
|
|
52
53
|
}
|
|
@@ -1,7 +1,8 @@
|
|
|
1
1
|
'use client';
|
|
2
2
|
|
|
3
|
-
import { useCallback, useSyncExternalStore } from 'react';
|
|
3
|
+
import { useCallback, useEffect, useSyncExternalStore } from 'react';
|
|
4
4
|
import {
|
|
5
|
+
migrateCalcStorage,
|
|
5
6
|
newCalcEntry,
|
|
6
7
|
readCalcActive,
|
|
7
8
|
resolveCalcActive,
|
|
@@ -26,6 +27,10 @@ const EMPTY_CALC: CalculatorState = {
|
|
|
26
27
|
};
|
|
27
28
|
|
|
28
29
|
export function useCalcStorage(storageKey: string) {
|
|
30
|
+
useEffect(() => {
|
|
31
|
+
migrateCalcStorage(storageKey);
|
|
32
|
+
}, [storageKey]);
|
|
33
|
+
|
|
29
34
|
const readSnapshot = useCallback(
|
|
30
35
|
() => readCalcStorage(storageKey),
|
|
31
36
|
[storageKey],
|
package/src/hooks/use-index.ts
CHANGED
|
@@ -8,6 +8,7 @@ import { useDumpRetryNonce } from '@terrafirmagreg-team/kit/hooks/use-dump-statu
|
|
|
8
8
|
import {
|
|
9
9
|
loadRecipeChromeClient,
|
|
10
10
|
loadRecipeCompactIndex,
|
|
11
|
+
loadRecipeEntityNames,
|
|
11
12
|
loadRecipeSkeletonIndex,
|
|
12
13
|
} from '@terrafirmagreg-team/recipe/lib/client-index';
|
|
13
14
|
import { publishRecipeIndex } from '@terrafirmagreg-team/recipe/lib/client-index/store';
|
|
@@ -52,6 +53,15 @@ export function useRecipeIndex(dataset: DatasetRef | undefined, lang: string) {
|
|
|
52
53
|
setLastFull(index);
|
|
53
54
|
});
|
|
54
55
|
publishRecipeIndex(dataset, lang, index);
|
|
56
|
+
const entityNames = await loadRecipeEntityNames(dataset, chrome, lang);
|
|
57
|
+
if (isCancelled()) return;
|
|
58
|
+
publishRecipeIndex(dataset, lang, {
|
|
59
|
+
...index,
|
|
60
|
+
names: {
|
|
61
|
+
...index.names,
|
|
62
|
+
[lang]: { ...entityNames, ...index.names[lang] },
|
|
63
|
+
},
|
|
64
|
+
});
|
|
55
65
|
}, [dataset, lang, nonce]);
|
|
56
66
|
|
|
57
67
|
useAsyncEffect(async (isCancelled) => {
|
package/src/lib/calc/defaults.ts
CHANGED
|
@@ -1,11 +1,9 @@
|
|
|
1
1
|
import type { DatasetRef } from '@terrafirmagreg-team/sites/location';
|
|
2
2
|
const KEY_PREFIX = 'calc:recipe-defaults:';
|
|
3
3
|
|
|
4
|
-
|
|
5
|
-
if (
|
|
4
|
+
function parseDefaults(raw: string | null): Record<string, string> {
|
|
5
|
+
if (!raw) return {};
|
|
6
6
|
try {
|
|
7
|
-
const raw = window.localStorage.getItem(`${KEY_PREFIX}${dataset.id}/${dataset.version ?? ''}`);
|
|
8
|
-
if (!raw) return {};
|
|
9
7
|
const parsed = JSON.parse(raw);
|
|
10
8
|
if (parsed === null || typeof parsed !== 'object' || Array.isArray(parsed))
|
|
11
9
|
return {};
|
|
@@ -20,6 +18,17 @@ export function readRecipeDefaults(dataset: DatasetRef): Record<string, string>
|
|
|
20
18
|
}
|
|
21
19
|
}
|
|
22
20
|
|
|
21
|
+
const defaultsKey = (dataset: DatasetRef) => `${KEY_PREFIX}${dataset.id}`;
|
|
22
|
+
|
|
23
|
+
export function readRecipeDefaults(dataset: DatasetRef): Record<string, string> {
|
|
24
|
+
if (typeof window === 'undefined') return {};
|
|
25
|
+
try {
|
|
26
|
+
return parseDefaults(window.localStorage.getItem(defaultsKey(dataset)));
|
|
27
|
+
} catch {
|
|
28
|
+
return {};
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
|
|
23
32
|
const EVENT = 'tfg:recipe-defaults';
|
|
24
33
|
|
|
25
34
|
export function subscribeRecipeDefaults(listener: () => void): () => void {
|
|
@@ -37,7 +46,7 @@ export function writeRecipeDefault(
|
|
|
37
46
|
if (recipeId === null) delete next[materialId];
|
|
38
47
|
else next[materialId] = recipeId;
|
|
39
48
|
try {
|
|
40
|
-
window.localStorage.setItem(
|
|
49
|
+
window.localStorage.setItem(defaultsKey(dataset), JSON.stringify(next));
|
|
41
50
|
} catch {
|
|
42
51
|
|
|
43
52
|
}
|
|
@@ -45,3 +54,32 @@ export function writeRecipeDefault(
|
|
|
45
54
|
return next;
|
|
46
55
|
}
|
|
47
56
|
|
|
57
|
+
const migrated = new Set<string>();
|
|
58
|
+
|
|
59
|
+
export function migrateRecipeDefaults(dataset: DatasetRef): void {
|
|
60
|
+
if (typeof window === 'undefined') return;
|
|
61
|
+
const canonical = defaultsKey(dataset);
|
|
62
|
+
if (migrated.has(canonical)) return;
|
|
63
|
+
migrated.add(canonical);
|
|
64
|
+
try {
|
|
65
|
+
const legacy: string[] = [];
|
|
66
|
+
for (let i = 0; i < window.localStorage.length; i += 1) {
|
|
67
|
+
const name = window.localStorage.key(i);
|
|
68
|
+
if (name && name.startsWith(`${canonical}/`)) legacy.push(name);
|
|
69
|
+
}
|
|
70
|
+
if (!legacy.length) return;
|
|
71
|
+
const merged: Record<string, string> = {};
|
|
72
|
+
for (const name of legacy) {
|
|
73
|
+
Object.assign(
|
|
74
|
+
merged,
|
|
75
|
+
parseDefaults(window.localStorage.getItem(name)),
|
|
76
|
+
);
|
|
77
|
+
}
|
|
78
|
+
Object.assign(merged, readRecipeDefaults(dataset));
|
|
79
|
+
window.localStorage.setItem(canonical, JSON.stringify(merged));
|
|
80
|
+
for (const name of legacy) window.localStorage.removeItem(name);
|
|
81
|
+
window.dispatchEvent(new Event(EVENT));
|
|
82
|
+
} catch {
|
|
83
|
+
return;
|
|
84
|
+
}
|
|
85
|
+
}
|
package/src/lib/calc/flow.ts
CHANGED
|
@@ -2,12 +2,14 @@ import type { DatasetRef } from '@terrafirmagreg-team/sites/location';
|
|
|
2
2
|
import type { Edge, Node } from '@terrafirmagreg-team/ui/lib/canvas/xyflow';
|
|
3
3
|
import type { Recipe } from '@terrafirmagreg-team/recipe/lib/data/dto';
|
|
4
4
|
import { PORT_IN } from '@terrafirmagreg-team/recipe/lib/calc/ports';
|
|
5
|
+
import { resolveWireEnds } from '@terrafirmagreg-team/recipe/lib/calc/wire';
|
|
5
6
|
import type {
|
|
6
7
|
CalcKind,
|
|
7
8
|
CalcNode,
|
|
8
9
|
CalcRecipeSummary,
|
|
9
10
|
CalcWire,
|
|
10
11
|
} from '@terrafirmagreg-team/recipe/types/calc';
|
|
12
|
+
import type { RecipeRelation } from '@terrafirmagreg-team/recipe/types';
|
|
11
13
|
import type { FlowPoint } from '@terrafirmagreg-team/ui/types/canvas';
|
|
12
14
|
|
|
13
15
|
export const CALC_NODE_W = 232;
|
|
@@ -36,12 +38,18 @@ export type CalcFlowData = {
|
|
|
36
38
|
onRequestPicker: (node: CalcNode) => void;
|
|
37
39
|
onRequestMemberPicker: (node: CalcNode) => void;
|
|
38
40
|
onRequestSupply: (node: CalcNode, input: number) => void;
|
|
41
|
+
onRequestDemand: (node: CalcNode, output: number) => void;
|
|
39
42
|
onSupplyInput: (
|
|
40
43
|
cardId: string,
|
|
41
44
|
inputIndex: number,
|
|
42
45
|
recipeId: string | null,
|
|
43
46
|
outputAmount?: number,
|
|
44
47
|
) => string | null;
|
|
48
|
+
onDemandOutput: (
|
|
49
|
+
cardId: string,
|
|
50
|
+
outputIndex: number,
|
|
51
|
+
recipeId: string,
|
|
52
|
+
) => void;
|
|
45
53
|
getRecipesForItem: (
|
|
46
54
|
materialId: string,
|
|
47
55
|
kind: CalcKind,
|
|
@@ -52,7 +60,11 @@ export type CalcFlowData = {
|
|
|
52
60
|
kind: CalcKind,
|
|
53
61
|
pos?: { x: number; y: number },
|
|
54
62
|
) => string;
|
|
55
|
-
onOpenItem: (
|
|
63
|
+
onOpenItem: (
|
|
64
|
+
materialId: string,
|
|
65
|
+
kind: CalcKind,
|
|
66
|
+
relation?: RecipeRelation,
|
|
67
|
+
) => void;
|
|
56
68
|
onRemoveCard: (cardId: string) => void;
|
|
57
69
|
onMoveCard: (cardId: string, pos: { x: number; y: number }) => void;
|
|
58
70
|
onCrafts: (cardId: string, crafts: number) => void;
|
|
@@ -125,16 +137,12 @@ export function cardsToFlow(
|
|
|
125
137
|
const edges: Edge[] = wires.flatMap((wire, index) => {
|
|
126
138
|
const src = byId.get(wire.from);
|
|
127
139
|
const dst = byId.get(wire.to);
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
const
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
: src.outputs.findIndex(
|
|
135
|
-
(o) => o.id === input.id && o.kind === input.kind,
|
|
136
|
-
);
|
|
137
|
-
if (outIndex < 0) return [];
|
|
140
|
+
if (!src || !dst) return [];
|
|
141
|
+
const ends = resolveWireEnds(src, dst, wire);
|
|
142
|
+
if (!ends) return [];
|
|
143
|
+
const input = dst.inputs[ends.inputIndex];
|
|
144
|
+
if (!input?.wired) return [];
|
|
145
|
+
const outIndex = ends.outIndex;
|
|
138
146
|
const mat = src.outputs[outIndex];
|
|
139
147
|
return [
|
|
140
148
|
{
|
|
@@ -142,7 +150,7 @@ export function cardsToFlow(
|
|
|
142
150
|
source: wire.from,
|
|
143
151
|
sourceHandle: outIndex > 0 ? `out-${outIndex}` : 'out',
|
|
144
152
|
target: wire.to,
|
|
145
|
-
targetHandle: `in-${
|
|
153
|
+
targetHandle: `in-${ends.inputIndex}`,
|
|
146
154
|
type: 'calcEdge',
|
|
147
155
|
className: 'calc-edge--wire',
|
|
148
156
|
deletable: true,
|
|
@@ -176,6 +184,7 @@ function sameCalcNode(a: CalcNode, b: CalcNode): boolean {
|
|
|
176
184
|
out.id === b.outputs[index]?.id &&
|
|
177
185
|
out.kind === b.outputs[index]?.kind &&
|
|
178
186
|
out.amount === b.outputs[index]?.amount &&
|
|
187
|
+
out.tagId === b.outputs[index]?.tagId &&
|
|
179
188
|
out.allocated === b.outputs[index]?.allocated &&
|
|
180
189
|
out.remaining === b.outputs[index]?.remaining,
|
|
181
190
|
) &&
|
|
@@ -192,7 +201,9 @@ function sameCalcNode(a: CalcNode, b: CalcNode): boolean {
|
|
|
192
201
|
inp.amount === b.inputs[index]?.amount &&
|
|
193
202
|
inp.covered === b.inputs[index]?.covered &&
|
|
194
203
|
inp.wired === b.inputs[index]?.wired &&
|
|
195
|
-
inp.catalyst === b.inputs[index]?.catalyst
|
|
204
|
+
inp.catalyst === b.inputs[index]?.catalyst &&
|
|
205
|
+
(inp.members?.join() ?? '') ===
|
|
206
|
+
(b.inputs[index]?.members?.join() ?? ''),
|
|
196
207
|
) &&
|
|
197
208
|
(a.members?.join() ?? '') === (b.members?.join() ?? '') &&
|
|
198
209
|
a.availableRecipes.length === b.availableRecipes.length &&
|
|
@@ -216,7 +227,9 @@ function sameFlowData(a: CalcFlowData, b: CalcFlowData): boolean {
|
|
|
216
227
|
a.onRequestPicker === b.onRequestPicker &&
|
|
217
228
|
a.onRequestMemberPicker === b.onRequestMemberPicker &&
|
|
218
229
|
a.onRequestSupply === b.onRequestSupply &&
|
|
230
|
+
a.onRequestDemand === b.onRequestDemand &&
|
|
219
231
|
a.onSupplyInput === b.onSupplyInput &&
|
|
232
|
+
a.onDemandOutput === b.onDemandOutput &&
|
|
220
233
|
a.getRecipesForItem === b.getRecipesForItem &&
|
|
221
234
|
a.ensureMaterial === b.ensureMaterial &&
|
|
222
235
|
a.onAddCard === b.onAddCard &&
|
|
@@ -270,7 +283,12 @@ export function reconcileCalcNodes(
|
|
|
270
283
|
enters += 1;
|
|
271
284
|
return withDelay(node, enters);
|
|
272
285
|
}
|
|
273
|
-
return
|
|
286
|
+
return {
|
|
287
|
+
...node,
|
|
288
|
+
dragging: kept.dragging,
|
|
289
|
+
selected: kept.selected,
|
|
290
|
+
measured: kept.measured ?? node.measured,
|
|
291
|
+
};
|
|
274
292
|
});
|
|
275
293
|
return changed ? out : prev;
|
|
276
294
|
}
|
package/src/lib/calc/scheme.ts
CHANGED
|
@@ -11,6 +11,7 @@ import type {
|
|
|
11
11
|
TagResolution,
|
|
12
12
|
} from '@terrafirmagreg-team/recipe/types/calc';
|
|
13
13
|
import { mergeAmounts } from './tree/materials';
|
|
14
|
+
import { resolveWireEnds } from './wire';
|
|
14
15
|
|
|
15
16
|
export type SchemeContext = {
|
|
16
17
|
recipeMap: Map<string, CalcRecipe>;
|
|
@@ -34,14 +35,18 @@ function outputAmount(
|
|
|
34
35
|
function inputMember(
|
|
35
36
|
input: CalcRecipe['inputs'][number],
|
|
36
37
|
ctx: SchemeContext,
|
|
37
|
-
): { id?: string; kind: CalcKind } {
|
|
38
|
+
): { id?: string; kind: CalcKind; members?: string[] } {
|
|
38
39
|
if (!input.tagId) return { id: input.id, kind: input.kind };
|
|
39
40
|
const picked = ctx.tagSelections[input.tagId];
|
|
40
41
|
const resolution = ctx.tagMembers(input.tagId);
|
|
41
42
|
const members = resolution?.members ?? [];
|
|
42
43
|
const memberId =
|
|
43
44
|
picked && members.includes(picked) ? picked : members[0];
|
|
44
|
-
return {
|
|
45
|
+
return {
|
|
46
|
+
id: memberId,
|
|
47
|
+
kind: resolution?.kind ?? input.kind,
|
|
48
|
+
members: members.length ? members : undefined,
|
|
49
|
+
};
|
|
45
50
|
}
|
|
46
51
|
|
|
47
52
|
export function buildScheme({
|
|
@@ -81,6 +86,9 @@ export function buildScheme({
|
|
|
81
86
|
id: card.materialId,
|
|
82
87
|
kind: card.kind,
|
|
83
88
|
amount: produced,
|
|
89
|
+
...(card.materialId.startsWith('#')
|
|
90
|
+
? { tagId: card.materialId }
|
|
91
|
+
: {}),
|
|
84
92
|
allocated: 0,
|
|
85
93
|
remaining: produced,
|
|
86
94
|
});
|
|
@@ -91,6 +99,7 @@ export function buildScheme({
|
|
|
91
99
|
kind: o.kind,
|
|
92
100
|
amount: o.amount * card.crafts,
|
|
93
101
|
chance: o.chance,
|
|
102
|
+
...(o.tagId !== undefined ? { tagId: o.tagId } : {}),
|
|
94
103
|
allocated: 0,
|
|
95
104
|
remaining: o.amount * card.crafts,
|
|
96
105
|
});
|
|
@@ -115,11 +124,18 @@ export function buildScheme({
|
|
|
115
124
|
: entry.amount * card.crafts,
|
|
116
125
|
catalyst: entry.catalyst,
|
|
117
126
|
tagId: entry.tagId,
|
|
127
|
+
...(member.members ? { members: member.members } : {}),
|
|
118
128
|
wired: false,
|
|
119
129
|
covered: 0,
|
|
120
130
|
} satisfies CalcNodeInput;
|
|
121
131
|
}),
|
|
122
132
|
availableRecipes,
|
|
133
|
+
...(card.materialId.startsWith('#')
|
|
134
|
+
? {
|
|
135
|
+
tagId: card.materialId,
|
|
136
|
+
members: ctx.tagMembers(card.materialId)?.members,
|
|
137
|
+
}
|
|
138
|
+
: {}),
|
|
123
139
|
cycle: false,
|
|
124
140
|
products: false,
|
|
125
141
|
loading:
|
|
@@ -137,19 +153,11 @@ export function buildScheme({
|
|
|
137
153
|
const src = byId.get(wire.from);
|
|
138
154
|
const dst = byId.get(wire.to);
|
|
139
155
|
if (!src || !dst || src === dst) return;
|
|
140
|
-
const
|
|
141
|
-
|
|
142
|
-
const
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
: input
|
|
146
|
-
? src.outputs.findIndex(
|
|
147
|
-
(o) => o.id === input.id && o.kind === input.kind,
|
|
148
|
-
)
|
|
149
|
-
: -1;
|
|
150
|
-
const srcOut = outIndex >= 0 ? src.outputs[outIndex] : undefined;
|
|
151
|
-
if (!input || !srcOut) return;
|
|
152
|
-
if (!input.id) return;
|
|
156
|
+
const ends = resolveWireEnds(src, dst, wire);
|
|
157
|
+
if (!ends) return;
|
|
158
|
+
const input = dst.inputs[ends.inputIndex];
|
|
159
|
+
const srcOut = src.outputs[ends.outIndex];
|
|
160
|
+
if (!input?.id || !srcOut) return;
|
|
153
161
|
input.wired = true;
|
|
154
162
|
const covered = Math.min(
|
|
155
163
|
Math.max(srcOut.remaining, 0),
|
|
@@ -159,7 +167,7 @@ export function buildScheme({
|
|
|
159
167
|
input.covered += covered;
|
|
160
168
|
srcOut.allocated += covered;
|
|
161
169
|
srcOut.remaining -= covered;
|
|
162
|
-
if (outIndex === 0) {
|
|
170
|
+
if (ends.outIndex === 0) {
|
|
163
171
|
src.allocated += covered;
|
|
164
172
|
src.remaining -= covered;
|
|
165
173
|
}
|
|
@@ -22,7 +22,7 @@ function calcTabId(): string {
|
|
|
22
22
|
|
|
23
23
|
export function calcStorageKey(section: string, dataset: DatasetRef): string {
|
|
24
24
|
void calcTabId();
|
|
25
|
-
return `calc:tabs:${section}:${dataset.id}
|
|
25
|
+
return `calc:tabs:${section}:${dataset.id}`;
|
|
26
26
|
}
|
|
27
27
|
|
|
28
28
|
export function activeKey(key: string): string {
|
|
@@ -186,6 +186,68 @@ export function writeCalcStorage(
|
|
|
186
186
|
writeCalcTabs(key, [...tabs, entry], entry.id);
|
|
187
187
|
}
|
|
188
188
|
|
|
189
|
+
const migrated = new Set<string>();
|
|
190
|
+
|
|
191
|
+
function mergeEntries(lists: CalcTreeEntry[][]): CalcTreeEntry[] {
|
|
192
|
+
const byId = new Map<string, CalcTreeEntry>();
|
|
193
|
+
for (const list of lists) {
|
|
194
|
+
for (const entry of list) {
|
|
195
|
+
const prev = byId.get(entry.id);
|
|
196
|
+
if (!prev || (entry.savedAt ?? 0) > (prev.savedAt ?? 0)) {
|
|
197
|
+
byId.set(entry.id, entry);
|
|
198
|
+
}
|
|
199
|
+
}
|
|
200
|
+
}
|
|
201
|
+
return [...byId.values()];
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
function legacyKeys(prefix: string): string[] {
|
|
205
|
+
const keys: string[] = [];
|
|
206
|
+
for (let i = 0; i < window.localStorage.length; i += 1) {
|
|
207
|
+
const name = window.localStorage.key(i);
|
|
208
|
+
if (name && name.startsWith(prefix)) keys.push(name);
|
|
209
|
+
}
|
|
210
|
+
return keys;
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
export function migrateCalcStorage(key: string): void {
|
|
214
|
+
if (typeof window === 'undefined' || migrated.has(key)) return;
|
|
215
|
+
migrated.add(key);
|
|
216
|
+
try {
|
|
217
|
+
const tabKeys = legacyKeys(`${key}/`);
|
|
218
|
+
const histKeys = legacyKeys(`${historyKey(key)}/`);
|
|
219
|
+
if (!tabKeys.length && !histKeys.length) return;
|
|
220
|
+
if (tabKeys.length) {
|
|
221
|
+
const lists = [readCalcTabs(key)].filter((list) => list.length);
|
|
222
|
+
for (const legacy of tabKeys) {
|
|
223
|
+
const tabs = readCalcTabs(legacy);
|
|
224
|
+
if (tabs.length) lists.push(tabs);
|
|
225
|
+
}
|
|
226
|
+
writeTabsRaw(key, mergeEntries(lists));
|
|
227
|
+
}
|
|
228
|
+
if (histKeys.length) {
|
|
229
|
+
const lists = [readCalcHistory(key)].filter((list) => list.length);
|
|
230
|
+
for (const legacy of histKeys) {
|
|
231
|
+
const entries = readJSON(
|
|
232
|
+
window.localStorage.getItem(legacy),
|
|
233
|
+
isCalcHistory,
|
|
234
|
+
);
|
|
235
|
+
if (entries) {
|
|
236
|
+
migrateEntries(entries);
|
|
237
|
+
lists.push(entries);
|
|
238
|
+
}
|
|
239
|
+
}
|
|
240
|
+
writeHistoryRaw(key, mergeEntries(lists));
|
|
241
|
+
}
|
|
242
|
+
for (const legacy of [...tabKeys, ...histKeys]) {
|
|
243
|
+
window.localStorage.removeItem(legacy);
|
|
244
|
+
}
|
|
245
|
+
emitCalcStorage();
|
|
246
|
+
} catch {
|
|
247
|
+
return;
|
|
248
|
+
}
|
|
249
|
+
}
|
|
250
|
+
|
|
189
251
|
export function subscribeCalcStorage(listener: () => void): () => void {
|
|
190
252
|
if (typeof window === 'undefined') return () => {};
|
|
191
253
|
window.addEventListener(CALC_STATE_EVENT, listener);
|