@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.
Files changed (39) hide show
  1. package/package.json +4 -4
  2. package/src/components/tree.tsx +4 -1
  3. package/src/components/views/browser/axis-switcher.tsx +5 -2
  4. package/src/components/views/browser/index.tsx +6 -2
  5. package/src/components/views/browser/item-panel/index.tsx +5 -2
  6. package/src/components/views/browser/toolbar.tsx +5 -2
  7. package/src/components/views/calc/calculator/index.tsx +3 -0
  8. package/src/components/views/calc/flow/index.tsx +10 -2
  9. package/src/components/views/calc/flow/picker-dialogs.tsx +122 -5
  10. package/src/components/views/calc/node-card/full.tsx +0 -1
  11. package/src/components/views/calc/node-card/menu.tsx +17 -1
  12. package/src/components/views/calc/node-card/port-cells.tsx +23 -6
  13. package/src/components/views/calc/node-card/port-rails.tsx +1 -3
  14. package/src/hooks/calc/flow/use-handlers.ts +14 -6
  15. package/src/hooks/calc/flow/use-layout.ts +5 -0
  16. package/src/hooks/calc/flow/use-pickers.ts +15 -0
  17. package/src/hooks/calc/flow/use-viewport.ts +1 -1
  18. package/src/hooks/calc/use-actions.ts +144 -4
  19. package/src/hooks/calc/use-defaults.ts +4 -0
  20. package/src/hooks/calc/use-resolver.ts +1 -0
  21. package/src/hooks/calc/use-storage.ts +6 -1
  22. package/src/hooks/use-index.ts +10 -0
  23. package/src/lib/calc/defaults.ts +43 -5
  24. package/src/lib/calc/flow.ts +32 -14
  25. package/src/lib/calc/scheme.ts +24 -16
  26. package/src/lib/calc/storage/index.ts +1 -0
  27. package/src/lib/calc/storage/keys.ts +1 -1
  28. package/src/lib/calc/storage/store.ts +62 -0
  29. package/src/lib/calc/tree/recipe.ts +1 -0
  30. package/src/lib/calc/wire.ts +39 -0
  31. package/src/lib/calc/worker/resolve.ts +7 -0
  32. package/src/lib/client-index/chrome.ts +56 -38
  33. package/src/lib/client-index/index.ts +1 -1
  34. package/src/lib/links.ts +2 -1
  35. package/src/lib/og.ts +127 -0
  36. package/src/lib/tool-names.ts +20 -0
  37. package/src/types/calc-flow-props.ts +1 -0
  38. package/src/types/calc.ts +2 -1
  39. package/styles/calc.css +4 -0
@@ -80,6 +80,7 @@ export function toCalcRecipe(recipe: Recipe): CalcRecipe {
80
80
  kind: slotKind(slot),
81
81
  amount: slot.count && slot.count > 0 ? slot.count : 1,
82
82
  ...(slot.chance !== undefined ? { chance: slot.chance } : {}),
83
+ ...(slot.kind === 'tag' ? { tagId: slot.id } : {}),
83
84
  }));
84
85
  const tier = recipeTier(recipe);
85
86
  return {
@@ -0,0 +1,39 @@
1
+ import type {
2
+ CalcMaterial,
3
+ CalcNode,
4
+ CalcNodeInput,
5
+ CalcWire,
6
+ } from '@terrafirmagreg-team/recipe/types/calc';
7
+
8
+ export function wireEndMatches(
9
+ input: CalcNodeInput,
10
+ out: CalcMaterial,
11
+ ): boolean {
12
+ if (input.kind !== out.kind || !out.id) return false;
13
+ if (input.id === out.id) return true;
14
+ return !!input.tagId && !!input.members?.includes(out.id);
15
+ }
16
+
17
+ export function resolveWireEnds(
18
+ src: CalcNode,
19
+ dst: CalcNode,
20
+ wire: CalcWire,
21
+ ): { inputIndex: number; outIndex: number } | null {
22
+ const hinted = dst.inputs[wire.input];
23
+ if (hinted?.id) {
24
+ const outIndex = src.outputs.findIndex((out) =>
25
+ wireEndMatches(hinted, out),
26
+ );
27
+ if (outIndex >= 0) return { inputIndex: wire.input, outIndex };
28
+ }
29
+ for (let i = 0; i < dst.inputs.length; i += 1) {
30
+ if (i === wire.input) continue;
31
+ const input = dst.inputs[i];
32
+ if (!input?.id) continue;
33
+ const outIndex = src.outputs.findIndex((out) =>
34
+ wireEndMatches(input, out),
35
+ );
36
+ if (outIndex >= 0) return { inputIndex: i, outIndex };
37
+ }
38
+ return null;
39
+ }
@@ -32,6 +32,13 @@ export async function runResolve(input: ResolveInput) {
32
32
  for (const id of input.tags ?? []) {
33
33
  if (id && !state.tagMap.has(id)) wantedTag.add(id);
34
34
  }
35
+ for (const recipe of state.recipeMap.values()) {
36
+ for (const slot of recipe.inputs) {
37
+ if (slot.tagId && !state.tagMap.has(slot.tagId)) {
38
+ wantedTag.add(slot.tagId);
39
+ }
40
+ }
41
+ }
35
42
  wantedRecipes.forEach((id) => {
36
43
  if (state.recipeMap.has(id) || state.deadIds.has(id)) {
37
44
  wantedRecipes.delete(id);
@@ -112,49 +112,30 @@ async function clientRecipeGroups(
112
112
 
113
113
  const loadChrome = createDatasetCache(
114
114
  async (dataset: DatasetRef, lang: string): Promise<RecipeChrome> => {
115
- const [items, fluids, creative, categories, usedIds, itemTags, blockTags] =
116
- await Promise.all([
117
- loadClientRegistryOrder(dataset, 'item'),
118
- loadClientRegistryOrder(dataset, 'fluid'),
119
- loadClientTabs(dataset),
120
- loadDatasetRecipeCategories(dataset),
121
- clientUsedIds(dataset),
122
- loadClientTagMembers(dataset, 'item'),
123
- loadClientTagMembers(dataset, 'block'),
124
- ]);
115
+ const [items, fluids, creative, categories, usedIds] = await Promise.all([
116
+ loadClientRegistryOrder(dataset, 'item'),
117
+ loadClientRegistryOrder(dataset, 'fluid'),
118
+ loadClientTabs(dataset),
119
+ loadDatasetRecipeCategories(dataset),
120
+ clientUsedIds(dataset),
121
+ ]);
125
122
  const tabs = creative.tabs;
126
123
  const tabItems = Object.keys(creative.members).length
127
124
  ? creative.members
128
125
  : undefined;
129
126
  const types = categories.map((row) => row.id);
130
- const tagIds = new Set([
131
- ...Object.keys(itemTags ?? {}),
132
- ...Object.keys(blockTags ?? {}),
127
+ const [groups, mods, categoryNames, tabNames] = await Promise.all([
128
+ clientRecipeGroups(dataset, lang),
129
+ loadClientModNames(dataset, lang),
130
+ clientNamedCategories(dataset, lang, types, categories),
131
+ clientDumpLang(dataset, lang).then((map) => {
132
+ const names: Record<string, string> = {};
133
+ for (const tab of tabs) {
134
+ names[tab.id] = creativeTabName(map, tab.display.name ?? tab.id);
135
+ }
136
+ return names;
137
+ }),
133
138
  ]);
134
- const nameIds = [
135
- ...items,
136
- ...fluids,
137
- ...tagIds,
138
- ...(usedIds ?? []),
139
- ...Object.values(tabItems ?? {}).flat(),
140
- ...tabs.flatMap((tab) =>
141
- tab.icon ? [stackIdFromRef(tab.icon)] : [],
142
- ),
143
- ];
144
- const [groups, mods, entityNames, categoryNames, tabNames] =
145
- await Promise.all([
146
- clientRecipeGroups(dataset, lang),
147
- loadClientModNames(dataset, lang),
148
- clientEntityNames(dataset, lang, nameIds),
149
- clientNamedCategories(dataset, lang, types, categories),
150
- clientDumpLang(dataset, lang).then((map) => {
151
- const names: Record<string, string> = {};
152
- for (const tab of tabs) {
153
- names[tab.id] = creativeTabName(map, tab.display.name ?? tab.id);
154
- }
155
- return names;
156
- }),
157
- ]);
158
139
  return {
159
140
  items,
160
141
  ...(usedIds.length ? { usedIds } : {}),
@@ -165,12 +146,41 @@ const loadChrome = createDatasetCache(
165
146
  ...(groups.length ? { groups } : {}),
166
147
  mods,
167
148
  names: {
168
- [lang]: { ...tabNames, ...categoryNames, ...entityNames },
149
+ [lang]: { ...tabNames, ...categoryNames },
169
150
  },
170
151
  };
171
152
  },
172
153
  );
173
154
 
155
+ const loadEntityNames = createDatasetCache(
156
+ async (
157
+ dataset: DatasetRef,
158
+ chrome: RecipeChrome,
159
+ lang: string,
160
+ ): Promise<Record<string, string>> => {
161
+ const [itemTags, blockTags] = await Promise.all([
162
+ loadClientTagMembers(dataset, 'item'),
163
+ loadClientTagMembers(dataset, 'block'),
164
+ ]);
165
+ const tagIds = new Set([
166
+ ...Object.keys(itemTags ?? {}),
167
+ ...Object.keys(blockTags ?? {}),
168
+ ]);
169
+ const nameIds = [
170
+ ...chrome.items,
171
+ ...(chrome.fluids ?? []),
172
+ ...tagIds,
173
+ ...(chrome.usedIds ?? []),
174
+ ...Object.values(chrome.tabItems ?? {}).flat(),
175
+ ...(chrome.tabs ?? []).flatMap((tab) =>
176
+ tab.icon ? [stackIdFromRef(tab.icon)] : [],
177
+ ),
178
+ ];
179
+ return clientEntityNames(dataset, lang, nameIds);
180
+ },
181
+ (dataset, _chrome, lang) => `${dataset.id}/${dataset.version ?? ''} ${lang}`,
182
+ );
183
+
174
184
  export function loadRecipeChromeClient(
175
185
  dataset: DatasetRef,
176
186
  lang: string,
@@ -178,3 +188,11 @@ export function loadRecipeChromeClient(
178
188
  return loadChrome(dataset, lang);
179
189
  }
180
190
 
191
+ export function loadRecipeEntityNames(
192
+ dataset: DatasetRef,
193
+ chrome: RecipeChrome,
194
+ lang: string,
195
+ ): Promise<Record<string, string>> {
196
+ return loadEntityNames(dataset, chrome, lang);
197
+ }
198
+
@@ -3,7 +3,7 @@ export {
3
3
  loadRegistryEntrySlice,
4
4
  loadTagSlice,
5
5
  } from './registry';
6
- export { loadRecipeChromeClient } from './chrome';
6
+ export { loadRecipeChromeClient, loadRecipeEntityNames } from './chrome';
7
7
  export {
8
8
  loadCategoryRecipes,
9
9
  loadTypeRows,
package/src/lib/links.ts CHANGED
@@ -92,7 +92,7 @@ export function recipeStackHref(
92
92
  dataset: DatasetRef,
93
93
  lang: Lang,
94
94
  id: string,
95
- opts?: Pick<RecipeHrefOpts, 'tab' | 'search'> & {
95
+ opts?: Pick<RecipeHrefOpts, 'tab' | 'search' | 'relation'> & {
96
96
  kind?: 'item' | 'fluid' | 'tag';
97
97
  tagKind?: string;
98
98
  },
@@ -100,6 +100,7 @@ export function recipeStackHref(
100
100
  const kept = {
101
101
  tab: opts?.tab,
102
102
  search: opts?.search,
103
+ relation: opts?.relation,
103
104
  };
104
105
  if (opts?.kind === 'tag' || id.startsWith('#')) {
105
106
  const bare = id.startsWith('#') ? id.slice(1) : id;
package/src/lib/og.ts ADDED
@@ -0,0 +1,127 @@
1
+ import type { DatasetRef } from '@terrafirmagreg-team/sites/location';
2
+ import type { Lang } from '@terrafirmagreg-team/translate/types/locale';
3
+ import { clientDumpLang } from '@terrafirmagreg-team/dump/lib/dump/client/dump';
4
+ import { loadClientTagMembers } from '@terrafirmagreg-team/dump/lib/dump/client/tags';
5
+ import type { ReferenceKind } from '@terrafirmagreg-team/dump/lib/dump/contract/ref';
6
+ import { stripMcFormat } from '@terrafirmagreg-team/dump/lib/dump/assets/lang/names';
7
+ import {
8
+ ogLocalized,
9
+ ogRefSprite,
10
+ ogStackName,
11
+ } from '@terrafirmagreg-team/dump/lib/dump/assets/og';
12
+ import {
13
+ ogName,
14
+ registerOgResolver,
15
+ type OgContext,
16
+ } from '@terrafirmagreg-team/preview/lib';
17
+ import { loadDatasetRecipeCategories } from './data/category/load';
18
+ import { loadClientRecipe } from './client/recipe';
19
+ import { recipeToolName } from './tool-names';
20
+
21
+ async function recipeOgContext(
22
+ dataset: DatasetRef,
23
+ search: URLSearchParams,
24
+ lang: Lang,
25
+ ): Promise<OgContext> {
26
+ const tool = search.get('tool');
27
+ if (tool) {
28
+ return {
29
+ entity: { kind: 'tool', name: recipeToolName(tool, lang) },
30
+ imageQuery: `tool=${encodeURIComponent(tool)}`,
31
+ };
32
+ }
33
+ const item = search.get('item');
34
+ const fluid = search.get('fluid');
35
+ if (item || fluid) {
36
+ const kind = fluid ? 'fluid' : 'item';
37
+ const id = (item ?? fluid)!;
38
+ return {
39
+ entity: {
40
+ kind,
41
+ name: await ogStackName(dataset, id, kind, lang),
42
+ id,
43
+ },
44
+ icon: await ogRefSprite(dataset, { kind, id }),
45
+ description: id,
46
+ imageQuery: `${kind}=${encodeURIComponent(id)}`,
47
+ };
48
+ }
49
+ const type = search.get('type');
50
+ const id = search.get('id');
51
+ if (type === 'emi:tag' && id) {
52
+ const [, tagKind = 'item', ns = '', ...rest] = id.split('/');
53
+ const path = rest.join('/');
54
+ const tagPath = path.startsWith(`${tagKind}s/`)
55
+ ? path.slice(tagKind.length + 2)
56
+ : path;
57
+ const tagId = rest.length ? `${ns}:${tagPath}` : ns;
58
+ const members = await loadClientTagMembers(dataset, tagKind);
59
+ const list = tagId ? members?.[`#${tagId}`] : undefined;
60
+ const first = list?.[0];
61
+ return {
62
+ entity: { kind: 'tag', name: `#${tagId || id}`, id },
63
+ icon: first
64
+ ? await ogRefSprite(dataset, {
65
+ kind: tagKind as ReferenceKind,
66
+ id: first,
67
+ })
68
+ : undefined,
69
+ description:
70
+ list != null
71
+ ? `${list.length} member${list.length === 1 ? '' : 's'}`
72
+ : `${tagKind} tag`,
73
+ imageQuery: `type=emi:tag&id=${encodeURIComponent(id)}`,
74
+ };
75
+ }
76
+ if (id) {
77
+ const recipe = await loadClientRecipe(dataset, id);
78
+ if (!recipe) return {};
79
+ const out = recipe.outputs[0];
80
+ const [categories, map] = await Promise.all([
81
+ loadDatasetRecipeCategories(dataset),
82
+ clientDumpLang(dataset, lang),
83
+ ]);
84
+ const category = categories.find((row) => row.id === recipe.type);
85
+ const catName = category
86
+ ? stripMcFormat(
87
+ ogLocalized(map, category.display?.name) ?? category.id,
88
+ ).trim()
89
+ : undefined;
90
+ const name = out
91
+ ? await ogStackName(
92
+ dataset,
93
+ out.id,
94
+ out.kind === 'fluid' ? 'fluid' : 'item',
95
+ lang,
96
+ ).catch(() => recipe.id)
97
+ : recipe.id;
98
+ return {
99
+ entity: { kind: 'recipe', name, id: recipe.id },
100
+ icon: out
101
+ ? await ogRefSprite(dataset, {
102
+ kind: out.kind === 'tag' ? 'itemTag' : out.kind,
103
+ id: out.id,
104
+ })
105
+ : undefined,
106
+ description: catName ?? recipe.id,
107
+ imageQuery: `id=${encodeURIComponent(id)}`,
108
+ };
109
+ }
110
+ if (!type || type === 'all') return {};
111
+ const [categories, map] = await Promise.all([
112
+ loadDatasetRecipeCategories(dataset),
113
+ clientDumpLang(dataset, lang),
114
+ ]);
115
+ const category = categories.find((row) => row.id === type);
116
+ if (!category) return {};
117
+ const localized =
118
+ ogLocalized(map, category.display?.name) ?? category.id;
119
+ return {
120
+ entity: { kind: 'category', ...ogName(localized), id: category.id },
121
+ icon: await ogRefSprite(dataset, category.icon),
122
+ description: category.id,
123
+ imageQuery: `type=${encodeURIComponent(type)}`,
124
+ };
125
+ }
126
+
127
+ registerOgResolver('recipe', recipeOgContext);
@@ -0,0 +1,20 @@
1
+ import { humanizeKey } from '@terrafirmagreg-team/dump/lib/dump/assets/lang/names';
2
+ import { loadLocaleRegistry } from '@terrafirmagreg-team/translate/server';
3
+ import type { Lang } from '@terrafirmagreg-team/translate/types/locale';
4
+
5
+ const TOOL_KEYS: Record<string, [string, string]> = {
6
+ calc: ['Calculator', 'recipe.calculator'],
7
+ forge: ['Forging', 'recipe.forge.title'],
8
+ alloy: ['Alloys', 'recipe.alloy.title'],
9
+ };
10
+
11
+ export function recipeToolName(tool: string, lang: Lang): string {
12
+ const [source, note] = TOOL_KEYS[tool] ?? [];
13
+ if (!source || !note) return humanizeKey(tool);
14
+ const registry = loadLocaleRegistry();
15
+ const messages =
16
+ registry.messages[lang] ??
17
+ registry.messages[registry.defaultLanguage] ??
18
+ {};
19
+ return messages[`${source}(${note})`] ?? source;
20
+ }
@@ -26,6 +26,7 @@ export type CalcFlowProps = {
26
26
  | 'onRequestPicker'
27
27
  | 'onRequestMemberPicker'
28
28
  | 'onRequestSupply'
29
+ | 'onRequestDemand'
29
30
  >;
30
31
  mode: CalcViewMode;
31
32
  onModeChange: (mode: CalcViewMode) => void;
package/src/types/calc.ts CHANGED
@@ -10,11 +10,11 @@ export type CalcMaterial = {
10
10
  amount: number;
11
11
 
12
12
  chance?: number;
13
+ tagId?: string;
13
14
  };
14
15
 
15
16
  export type CalcRecipeInput = CalcMaterial & {
16
17
  catalyst: boolean;
17
- tagId?: string;
18
18
  };
19
19
 
20
20
  export type CalcRecipe = {
@@ -62,6 +62,7 @@ export type CalcNodeInput = {
62
62
  amount: number;
63
63
  catalyst: boolean;
64
64
  tagId?: string;
65
+ members?: string[];
65
66
  wired: boolean;
66
67
  covered: number;
67
68
  };
package/styles/calc.css CHANGED
@@ -197,6 +197,10 @@
197
197
  transition: transform 240ms ease;
198
198
  }
199
199
 
200
+ .calc-flow:has(.react-flow__node.dragging) .react-flow__node {
201
+ transition: none;
202
+ }
203
+
200
204
  .calc-flow .react-flow__node-calcMaterial[style*='--calc-delay'] {
201
205
  animation: calc-node-in 200ms ease-out both;
202
206
  animation-delay: var(--calc-delay, 0ms);