@terrafirmagreg-team/recipe 1.2.41 → 1.2.43

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (47) hide show
  1. package/package.json +4 -4
  2. package/src/components/cards/calc-add-item.tsx +55 -0
  3. package/src/components/cards/card/index.tsx +2 -0
  4. package/src/components/cards/card/text.tsx +20 -4
  5. package/src/components/views/browser/card-grid.tsx +11 -23
  6. package/src/components/views/browser/frame.tsx +8 -33
  7. package/src/components/views/browser/item-view/index.tsx +8 -10
  8. package/src/components/views/browser/settings.tsx +6 -0
  9. package/src/components/views/browser/type-tabs.tsx +5 -0
  10. package/src/components/views/browser/type-view.tsx +9 -47
  11. package/src/components/views/calc/calculator/index.tsx +4 -0
  12. package/src/components/views/calc/flow/index.tsx +42 -1
  13. package/src/components/views/calc/flow/picker-dialogs.tsx +15 -8
  14. package/src/components/views/calc/flow/toolbar.tsx +2 -0
  15. package/src/components/views/calc/flow/view-menu.tsx +13 -0
  16. package/src/components/views/calc/member-picker.tsx +7 -2
  17. package/src/components/views/calc/node-card/actions.tsx +1 -1
  18. package/src/components/views/calc/node-card/badges.tsx +1 -1
  19. package/src/components/views/calc/node-card/far.tsx +1 -1
  20. package/src/components/views/calc/node-card/full.tsx +34 -5
  21. package/src/components/views/calc/node-card/menu.tsx +32 -0
  22. package/src/components/views/calc/recipe-picker/body.tsx +10 -36
  23. package/src/hooks/calc/flow/use-layout.ts +1 -1
  24. package/src/hooks/calc/flow/use-pickers.ts +14 -10
  25. package/src/hooks/calc/use-actions.ts +54 -31
  26. package/src/hooks/calc/use-recipe-ids.ts +33 -0
  27. package/src/hooks/use-card-grid.ts +14 -0
  28. package/src/lib/alloy/parse.ts +6 -3
  29. package/src/lib/browser/card-dim.test.ts +39 -0
  30. package/src/lib/browser/card-dim.ts +38 -0
  31. package/src/lib/browser/lookup.ts +3 -2
  32. package/src/lib/browser/row-match.test.ts +34 -0
  33. package/src/lib/browser/row-match.ts +8 -0
  34. package/src/lib/calc/flow.ts +3 -1
  35. package/src/lib/calc/member.test.ts +178 -0
  36. package/src/lib/calc/member.ts +90 -0
  37. package/src/lib/calc/scheme.test.ts +213 -0
  38. package/src/lib/calc/scheme.ts +8 -3
  39. package/src/lib/calc/tree/index.ts +5 -1
  40. package/src/lib/calc/tree/recipe.test.ts +88 -0
  41. package/src/lib/calc/tree/recipe.ts +19 -3
  42. package/src/lib/calc/wire.test.ts +70 -0
  43. package/src/lib/calc/wire.ts +6 -2
  44. package/src/lib/cards/model.test.ts +61 -1
  45. package/src/lib/cards/model.ts +9 -2
  46. package/src/types/calc-flow-props.ts +2 -0
  47. package/styles/calc.css +4 -0
@@ -0,0 +1,33 @@
1
+ 'use client';
2
+
3
+ import {
4
+ createContext,
5
+ startTransition,
6
+ useCallback,
7
+ useContext,
8
+ } from 'react';
9
+ import {
10
+ boolCodec,
11
+ createStoredValue,
12
+ } from '@terrafirmagreg-team/ui/lib/utils/stored-value';
13
+
14
+ const useStoredRecipeIds = createStoredValue(
15
+ 'calc:recipeIds',
16
+ boolCodec(false),
17
+ );
18
+
19
+ const CalcRecipeIdsContext = createContext(false);
20
+
21
+ export const CalcRecipeIdsProvider = CalcRecipeIdsContext.Provider;
22
+
23
+ export function useCalcRecipeIds(): boolean {
24
+ return useContext(CalcRecipeIdsContext);
25
+ }
26
+
27
+ export function useCalcRecipeIdsMode() {
28
+ const [show, setStored] = useStoredRecipeIds();
29
+ const setShow = useCallback((next: boolean) => {
30
+ startTransition(() => setStored(next));
31
+ }, [setStored]);
32
+ return [show, setShow] as const;
33
+ }
@@ -0,0 +1,14 @@
1
+ import {
2
+ boolCodec,
3
+ createStoredValue,
4
+ } from '@terrafirmagreg-team/ui/lib/utils/stored-value';
5
+
6
+ const useStoredCardGrid = createStoredValue(
7
+ 'wiki.recipe.cardGrid',
8
+ boolCodec(false),
9
+ );
10
+
11
+ export function useCardGrid() {
12
+ const [grid, setGrid] = useStoredCardGrid();
13
+ return { grid, setGrid };
14
+ }
@@ -1,6 +1,7 @@
1
- import type { Recipe } from '@terrafirmagreg-team/recipe/lib/data/dto';
1
+ import type { Recipe, RecipeTextWidget } from '@terrafirmagreg-team/recipe/lib/data/dto';
2
2
  import type { AlloyComponent, AlloyRecipe } from '@terrafirmagreg-team/recipe/types/alloy';
3
3
  import { cleanForgeText } from '@terrafirmagreg-team/recipe/lib/forge/actions';
4
+ import { widgetText } from '@terrafirmagreg-team/recipe/lib/cards/model';
4
5
 
5
6
  export function parseAlloyPercent(
6
7
  text: string,
@@ -20,11 +21,13 @@ export function toAlloyRecipe(recipe: Recipe): AlloyRecipe {
20
21
  .sort(([a], [b]) => a.localeCompare(b, undefined, { numeric: true }))
21
22
  .map(([, slot]) => slot);
22
23
  const ranges = recipe.widgets
23
- .filter((widget) => widget.type === 'text')
24
+ .filter(
25
+ (widget): widget is RecipeTextWidget => widget.type === 'text',
26
+ )
24
27
  .map((widget) => ({
25
28
  x: widget.x,
26
29
  y: widget.y,
27
- range: parseAlloyPercent(widget.text ?? ''),
30
+ range: parseAlloyPercent(widgetText(widget)),
28
31
  }))
29
32
  .filter((widget) => widget.range !== undefined)
30
33
  .sort((a, b) => a.y - b.y || a.x - b.x);
@@ -0,0 +1,39 @@
1
+ import assert from 'node:assert/strict';
2
+ import { test } from 'node:test';
3
+ import {
4
+ recipeCardDim,
5
+ recipeCardFitDim,
6
+ } from '@terrafirmagreg-team/recipe/lib/browser/card-dim';
7
+
8
+ const ROW = { d: { w: 320, h: 170 }, type: 0, id: 'a:b', out: [], in: [] };
9
+
10
+ test('recipeCardDim returns declared dims', () => {
11
+ assert.deepEqual(recipeCardDim(null, new Map(), ROW as never), {
12
+ w: 320,
13
+ h: 170,
14
+ });
15
+ });
16
+
17
+ test('recipeCardFitDim keeps dims when the card fits the box', () => {
18
+ const d = recipeCardFitDim(null, new Map(), ROW as never, {
19
+ w: 800,
20
+ h: 600,
21
+ });
22
+ assert.deepEqual(d, { w: 320, h: 170 });
23
+ });
24
+
25
+ test('recipeCardFitDim scales uniformly when wider than the box', () => {
26
+ const d = recipeCardFitDim(null, new Map(), ROW as never, {
27
+ w: 160,
28
+ h: 600,
29
+ });
30
+ assert.deepEqual(d, { w: 160, h: 85 });
31
+ });
32
+
33
+ test('recipeCardFitDim caps height to the box', () => {
34
+ const d = recipeCardFitDim(null, new Map(), ROW as never, {
35
+ w: 800,
36
+ h: 100,
37
+ });
38
+ assert.deepEqual(d, { w: 320, h: 100 });
39
+ });
@@ -0,0 +1,38 @@
1
+ import type { FitDim } from '@terrafirmagreg-team/ui/hooks/page/use-fit-pages';
2
+ import type {
3
+ RecipeCategory,
4
+ RecipeCompactIndex,
5
+ RecipeCompactRow,
6
+ } from '@terrafirmagreg-team/recipe/types';
7
+
8
+ export const FALLBACK_DIM: FitDim = { w: 320, h: 80 };
9
+
10
+ export function recipeCardDim(
11
+ index: RecipeCompactIndex | null,
12
+ categoriesById: Map<string, RecipeCategory>,
13
+ row: RecipeCompactRow,
14
+ ): FitDim {
15
+ if (row.d) return { w: row.d.w, h: row.d.h };
16
+ const typeId = index?.types[row.type];
17
+ const layout = typeId ? categoriesById.get(typeId)?.layout : undefined;
18
+ if (!layout) return FALLBACK_DIM;
19
+ const scale = layout.scale ?? 2;
20
+ const margin = layout.margin ?? 0;
21
+ return {
22
+ w: (layout.w + margin * 2) * scale,
23
+ h: (layout.h + margin * 2) * scale,
24
+ };
25
+ }
26
+
27
+ export function recipeCardFitDim(
28
+ index: RecipeCompactIndex | null,
29
+ categoriesById: Map<string, RecipeCategory>,
30
+ row: RecipeCompactRow,
31
+ box: FitDim,
32
+ ): FitDim {
33
+ const d = recipeCardDim(index, categoriesById, row);
34
+ const fit = box.w > 0 && d.w > box.w ? box.w / d.w : 1;
35
+ const w = d.w * fit;
36
+ const h = d.h * fit;
37
+ return h > box.h ? { w, h: box.h } : { w, h };
38
+ }
@@ -10,6 +10,7 @@ import {
10
10
  resolveRecipeRows,
11
11
  } from '@terrafirmagreg-team/recipe/lib/client-index';
12
12
  import { stackIdFromRef } from '@terrafirmagreg-team/dump/lib/dump/contract/ref';
13
+ import { refsMatchBase } from '@terrafirmagreg-team/recipe/lib/browser/row-match';
13
14
  import { loadFluidIoIndex } from '@terrafirmagreg-team/recipe/lib/browser/fluid-outputs';
14
15
 
15
16
  export { registryId, tagPseudoId } from '@terrafirmagreg-team/dump/lib/dump/paths';
@@ -59,7 +60,7 @@ export async function recipesForOutput(
59
60
  const keepTagRows = opts?.includeTagRows ?? false;
60
61
  return (await resolveRecipeRows(dataset, index, ids, opts)).filter(
61
62
  (row) =>
62
- row.out.includes(base) ||
63
+ refsMatchBase(row.out, base) ||
63
64
  (keepTagRows &&
64
65
  (row.cat ?? index.types[row.type]) === 'emi:tag'),
65
66
  );
@@ -130,7 +131,7 @@ export async function usesForInput(
130
131
  }),
131
132
  ]);
132
133
  return uniqueRows([
133
- ...refRows.filter((row) => row.in.includes(base)),
134
+ ...refRows.filter((row) => refsMatchBase(row.in, base)),
134
135
  ...tagRows.filter((row) =>
135
136
  row.in.some((id) => acceptedTags.has(id)),
136
137
  ),
@@ -0,0 +1,34 @@
1
+ import assert from 'node:assert/strict';
2
+ import { test } from 'node:test';
3
+ import { refsMatchBase } from './row-match';
4
+
5
+ const NBT_OUT = 'gtceu:duranium_hammer{"Damage":0,"GT.Tool":{}}';
6
+ const NBT_IN = 'gtceu:duranium_hammer{"Damage":10239,"GT.Tool":{}}';
7
+
8
+ test('NBT-carrying refs match the queried base id', () => {
9
+ assert.equal(
10
+ refsMatchBase([NBT_OUT, 'minecraft:amethyst_shard'], 'gtceu:duranium_hammer'),
11
+ true,
12
+ );
13
+ assert.equal(
14
+ refsMatchBase([NBT_IN], 'gtceu:duranium_hammer'),
15
+ true,
16
+ );
17
+ });
18
+
19
+ test('base, plain and unrelated ids behave as expected', () => {
20
+ assert.equal(
21
+ refsMatchBase(['gtceu:duranium_hammer'], 'gtceu:duranium_hammer'),
22
+ true,
23
+ );
24
+ assert.equal(
25
+ refsMatchBase([NBT_OUT], 'gtceu:copper_hammer'),
26
+ false,
27
+ );
28
+ assert.equal(
29
+ refsMatchBase(['#forge:tools/hammers'], 'gtceu:duranium_hammer'),
30
+ false,
31
+ );
32
+ assert.equal(refsMatchBase([], 'gtceu:duranium_hammer'), false);
33
+ assert.equal(refsMatchBase(undefined, 'gtceu:duranium_hammer'), false);
34
+ });
@@ -0,0 +1,8 @@
1
+ import { stackIdBase } from '@terrafirmagreg-team/dump/lib/dump/contract/ref';
2
+
3
+ export function refsMatchBase(
4
+ refs: readonly string[] | undefined,
5
+ base: string,
6
+ ): boolean {
7
+ return !!refs?.some((ref) => stackIdBase(ref) === base);
8
+ }
@@ -35,8 +35,9 @@ export type CalcFlowData = {
35
35
  recipeBody: (recipeId: string) => Recipe | undefined;
36
36
  onSelectRecipe: (cardId: string, recipeId: string | null) => void;
37
37
  onSelectTagMember: (tagId: string, memberId: string) => void;
38
+ tagSelections: Record<string, string>;
38
39
  onRequestPicker: (node: CalcNode) => void;
39
- onRequestMemberPicker: (node: CalcNode) => void;
40
+ onRequestMemberPicker: (node: CalcNode, input?: number) => void;
40
41
  onRequestSupply: (node: CalcNode, input: number) => void;
41
42
  onRequestDemand: (node: CalcNode, output: number) => void;
42
43
  onSupplyInput: (
@@ -228,6 +229,7 @@ function sameFlowData(a: CalcFlowData, b: CalcFlowData): boolean {
228
229
  a.recipeBody === b.recipeBody &&
229
230
  a.onSelectRecipe === b.onSelectRecipe &&
230
231
  a.onSelectTagMember === b.onSelectTagMember &&
232
+ a.tagSelections === b.tagSelections &&
231
233
  a.onRequestPicker === b.onRequestPicker &&
232
234
  a.onRequestMemberPicker === b.onRequestMemberPicker &&
233
235
  a.onRequestSupply === b.onRequestSupply &&
@@ -0,0 +1,178 @@
1
+ import assert from 'node:assert/strict';
2
+ import test from 'node:test';
3
+ import type { Recipe, RecipeSlotWidget } from '@terrafirmagreg-team/recipe/lib/data/dto';
4
+ import type { CalcNode } from '@terrafirmagreg-team/recipe/types/calc';
5
+ import { pinnedRecipe } from './member';
6
+
7
+ function tagSlot(over: Partial<RecipeSlotWidget> = {}): RecipeSlotWidget {
8
+ return {
9
+ type: 'slot',
10
+ x: 0,
11
+ y: 0,
12
+ w: 16,
13
+ h: 16,
14
+ role: 'input',
15
+ kind: 'tag',
16
+ id: 'forge:rods/amethyst',
17
+ alternatives: ['minecraft:amethyst_shard', 'gtceu:amethyst_rod'],
18
+ ...over,
19
+ };
20
+ }
21
+
22
+ function recipe(widgets: Recipe['widgets']): Recipe {
23
+ return {
24
+ id: 'gtceu:lathe/lathe_amethyst_to_rod',
25
+ type: 'gtceu:lathe',
26
+ category: 'gtceu:lathe',
27
+ modId: 'gtceu',
28
+ slots: {},
29
+ properties: {},
30
+ inputs: [],
31
+ outputs: [],
32
+ widgets,
33
+ bodyUrl: '',
34
+ extra: {},
35
+ } as Recipe;
36
+ }
37
+
38
+ function node(inputs: CalcNode['inputs']): CalcNode {
39
+ return {
40
+ id: 'n1',
41
+ materialId: 'gtceu:amethyst_rod',
42
+ kind: 'item',
43
+ crafts: 1,
44
+ produced: 2,
45
+ allocated: 0,
46
+ remaining: 2,
47
+ outputs: [],
48
+ recipe: null,
49
+ inputs,
50
+ availableRecipes: [],
51
+ cycle: false,
52
+ products: false,
53
+ };
54
+ }
55
+
56
+ test('pinnedRecipe pins a tag slot to the selected member', () => {
57
+ const src = recipe([tagSlot()]);
58
+ const target = node([
59
+ {
60
+ id: 'gtceu:amethyst_rod',
61
+ kind: 'item',
62
+ amount: 1,
63
+ catalyst: false,
64
+ tagId: 'forge:rods/amethyst',
65
+ members: ['minecraft:amethyst_shard', 'gtceu:amethyst_rod'],
66
+ wired: false,
67
+ covered: 0,
68
+ },
69
+ ]);
70
+ const out = pinnedRecipe(src, target, {
71
+ 'forge:rods/amethyst': 'gtceu:amethyst_rod',
72
+ });
73
+ const slot = out.widgets[0] as RecipeSlotWidget;
74
+ assert.deepEqual(slot.alternatives, ['gtceu:amethyst_rod']);
75
+ });
76
+
77
+ test('pinnedRecipe matches hash-prefixed selections and widget ids', () => {
78
+ const src = recipe([tagSlot({ id: '#forge:rods/amethyst' })]);
79
+ const target = node([
80
+ {
81
+ id: 'gtceu:amethyst_rod',
82
+ kind: 'item',
83
+ amount: 1,
84
+ catalyst: false,
85
+ tagId: '#forge:rods/amethyst',
86
+ wired: false,
87
+ covered: 0,
88
+ },
89
+ ]);
90
+ const out = pinnedRecipe(src, target, {
91
+ 'forge:rods/amethyst': 'gtceu:amethyst_rod',
92
+ });
93
+ const slot = out.widgets[0] as RecipeSlotWidget;
94
+ assert.deepEqual(slot.alternatives, ['gtceu:amethyst_rod']);
95
+ });
96
+
97
+ test('pinnedRecipe keeps cycling when no selection exists', () => {
98
+ const src = recipe([tagSlot()]);
99
+ const target = node([
100
+ {
101
+ id: 'minecraft:amethyst_shard',
102
+ kind: 'item',
103
+ amount: 1,
104
+ catalyst: false,
105
+ tagId: 'forge:rods/amethyst',
106
+ members: ['minecraft:amethyst_shard', 'gtceu:amethyst_rod'],
107
+ wired: false,
108
+ covered: 0,
109
+ },
110
+ ]);
111
+ const out = pinnedRecipe(src, target, {});
112
+ assert.equal(out, src);
113
+ });
114
+
115
+ test('pinnedRecipe matches members by base id against NBT alternatives', () => {
116
+ const src = recipe([
117
+ tagSlot({
118
+ id: '#forge:tools/hammers',
119
+ alternatives: [
120
+ 'gtceu:stone_hammer{Damage:0,GT.Tool:{}}',
121
+ 'gtceu:copper_hammer{Damage:0,GT.Tool:{}}',
122
+ ],
123
+ }),
124
+ ]);
125
+ const target = node([
126
+ {
127
+ id: 'gtceu:copper_hammer',
128
+ kind: 'item',
129
+ amount: 1,
130
+ catalyst: false,
131
+ tagId: '#forge:tools/hammers',
132
+ wired: false,
133
+ covered: 0,
134
+ },
135
+ ]);
136
+ const out = pinnedRecipe(src, target, {
137
+ 'forge:tools/hammers': 'gtceu:copper_hammer',
138
+ });
139
+ const slot = out.widgets[0] as RecipeSlotWidget;
140
+ assert.deepEqual(slot.alternatives, [
141
+ 'gtceu:copper_hammer{Damage:0,GT.Tool:{}}',
142
+ ]);
143
+ });
144
+
145
+ test('pinnedRecipe pins the picked member outside alternatives and skips non-tag widgets', () => {
146
+ const src = recipe([
147
+ tagSlot(),
148
+ {
149
+ type: 'slot',
150
+ x: 0,
151
+ y: 0,
152
+ w: 16,
153
+ h: 16,
154
+ role: 'input',
155
+ kind: 'item',
156
+ id: 'minecraft:stick',
157
+ },
158
+ ]);
159
+ const target = node([
160
+ {
161
+ id: 'minecraft:planks',
162
+ kind: 'item',
163
+ amount: 1,
164
+ catalyst: false,
165
+ tagId: 'forge:rods/amethyst',
166
+ wired: false,
167
+ covered: 0,
168
+ },
169
+ ]);
170
+ const out = pinnedRecipe(src, target, {
171
+ 'forge:rods/amethyst': 'minecraft:planks',
172
+ });
173
+ const tag = out.widgets[0] as RecipeSlotWidget;
174
+ const item = out.widgets[1] as RecipeSlotWidget;
175
+ assert.deepEqual(tag.alternatives, ['minecraft:planks']);
176
+ assert.equal(item.id, 'minecraft:stick');
177
+ assert.equal(item.alternatives, undefined);
178
+ });
@@ -0,0 +1,90 @@
1
+ import { stackIdBase } from '@terrafirmagreg-team/dump/lib/dump/contract/ref';
2
+ import type {
3
+ CalcKind,
4
+ CalcNode,
5
+ } from '@terrafirmagreg-team/recipe/types/calc';
6
+ import type {
7
+ Recipe,
8
+ RecipeWidget,
9
+ } from '@terrafirmagreg-team/recipe/lib/data/dto';
10
+
11
+ export type MemberRequest = { node: CalcNode; input?: number };
12
+
13
+ export type MemberTarget = {
14
+ tagId: string;
15
+ members: string[];
16
+ selected: string | undefined;
17
+ kind: CalcKind;
18
+ };
19
+
20
+ export function memberTarget(
21
+ req: MemberRequest,
22
+ selections: Record<string, string> = {},
23
+ ): MemberTarget | undefined {
24
+ const { node, input } = req;
25
+ if (input == null) {
26
+ if (!node.tagId || !node.members?.length) return undefined;
27
+ const picked = selectionFor(node.tagId, selections);
28
+ return {
29
+ tagId: node.tagId,
30
+ members: node.members,
31
+ selected:
32
+ (picked
33
+ ? node.members.find(
34
+ (m) => stackIdBase(m) === stackIdBase(picked),
35
+ )
36
+ : undefined) ?? node.materialId,
37
+ kind: node.kind,
38
+ };
39
+ }
40
+ const slot = node.inputs[input];
41
+ if (!slot?.tagId || !slot.members?.length) return undefined;
42
+ return {
43
+ tagId: slot.tagId,
44
+ members: slot.members,
45
+ selected: slot.id,
46
+ kind: slot.kind,
47
+ };
48
+ }
49
+
50
+ function selectionFor(
51
+ tagId: string,
52
+ selections: Record<string, string>,
53
+ ): string | undefined {
54
+ const key = tagId.replace(/^#+/, '');
55
+ return selections[key] ?? selections[tagId] ?? selections[`#${key}`];
56
+ }
57
+
58
+ export function pinnedRecipe(
59
+ recipe: Recipe,
60
+ node: CalcNode,
61
+ selections: Record<string, string>,
62
+ ): Recipe {
63
+ const picked = new Map<string, string>();
64
+ for (const input of node.inputs) {
65
+ if (!input.tagId) continue;
66
+ const member = selectionFor(input.tagId, selections);
67
+ if (member) picked.set(input.tagId, member);
68
+ }
69
+ if (!picked.size) return recipe;
70
+ let changed = false;
71
+ const source: RecipeWidget[] = recipe.widgets;
72
+ const widgets = source.map((widget) => {
73
+ if (widget.type !== 'slot' || widget.kind !== 'tag') return widget;
74
+ const member =
75
+ picked.get(widget.id) ??
76
+ picked.get(`#${widget.id.replace(/^#+/, '')}`);
77
+ if (!member) return widget;
78
+ const memberBase = stackIdBase(member);
79
+ const pinned =
80
+ widget.alternatives?.find(
81
+ (alt) => stackIdBase(alt) === memberBase,
82
+ ) ?? member;
83
+ if (widget.alternatives?.length === 1 && widget.alternatives[0] === pinned) {
84
+ return widget;
85
+ }
86
+ changed = true;
87
+ return { ...widget, alternatives: [pinned] };
88
+ });
89
+ return changed ? { ...recipe, widgets } : recipe;
90
+ }