Skip to content

Commit

Permalink
show construction usage on items
Browse files Browse the repository at this point in the history
  • Loading branch information
nornagon committed Jul 26, 2023
1 parent 15b4199 commit 11894d3
Show file tree
Hide file tree
Showing 3 changed files with 144 additions and 79 deletions.
41 changes: 30 additions & 11 deletions src/data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1065,7 +1065,6 @@ export class CddaData {
_itemComponentCache: {
byTool: Map<string, Set<string>>;
byComponent: Map<string, Set<string>>;
byConstruction: Map<string, Set<string>>;
} | null = null;
getItemComponents() {
if (this._itemComponentCache) return this._itemComponentCache;
Expand Down Expand Up @@ -1107,22 +1106,42 @@ export class CddaData {
itemsByComponent.get(component.id)!.add(recipe.result);
}
});
const itemsByConstruction = new Map<string, Set<string>>();
this._itemComponentCache = {
byTool: itemsByTool,
byComponent: itemsByComponent,
};
return this._itemComponentCache;
}

_constructionComponentCache: {
byTool: Map<string, Set<string>>;
byComponent: Map<string, Set<string>>;
} | null = null;
getConstructionComponents() {
if (this._constructionComponentCache)
return this._constructionComponentCache;
const constructionsByComponent = new Map<string, Set<string>>();
const constructionsByTool = new Map<string, Set<string>>();
for (const c of this.byType("construction")) {
const { components } = this.normalizeRequirements(c);
const { components, tools } = this.normalizeRequirements(c);
for (const componentOptions of components)
for (const [component] of componentOptions) {
if (!itemsByConstruction.has(component))
itemsByConstruction.set(component, new Set());
itemsByConstruction.get(component)!.add(c.id);
if (!constructionsByComponent.has(component))
constructionsByComponent.set(component, new Set());
constructionsByComponent.get(component)!.add(c.id);
}
for (const toolOptions of tools)
for (const [tool] of toolOptions) {
if (!constructionsByTool.has(tool))
constructionsByTool.set(tool, new Set());
constructionsByTool.get(tool)!.add(c.id);
}
}
this._itemComponentCache = {
byTool: itemsByTool,
byComponent: itemsByComponent,
byConstruction: itemsByConstruction,
this._constructionComponentCache = {
byTool: constructionsByTool,
byComponent: constructionsByComponent,
};
return this._itemComponentCache;
return this._constructionComponentCache;
}

normalizeItemGroup(
Expand Down
35 changes: 19 additions & 16 deletions src/types/Construction.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,10 @@ if (construction.pre_flags)
preFlags.push({ flag });
} else preFlags.push(flag);
}
function terrainOrFurniture(id: string) {
return data.byId(id.startsWith("f_") ? "furniture" : "terrain", id);
}
</script>

<section>
Expand Down Expand Up @@ -74,11 +78,7 @@ if (construction.pre_flags)
{#if construction.pre_terrain}
<dt>{t("Requires", { _context })}</dt>
<dd>
<ItemSymbol
item={data.byId(
construction.pre_terrain.startsWith("f_") ? "furniture" : "terrain",
construction.pre_terrain
)} />
<ItemSymbol item={terrainOrFurniture(construction.pre_terrain)} />
<ThingLink
type={construction.pre_terrain.startsWith("f_")
? "furniture"
Expand Down Expand Up @@ -127,18 +127,21 @@ if (construction.pre_flags)
{#if !includeTitle && construction.post_terrain}
<dt>{t("Creates", { _context })}</dt>
<dd>
<ItemSymbol
item={data.byId(
construction.post_terrain.startsWith("f_")
{#if construction.post_terrain === "f_null"}
<em
>{t("nothing", {
_context,
_comment:
'The furniture/terrain "created" by a deconstruction is...',
})}</em>
{:else}
<ItemSymbol item={terrainOrFurniture(construction.post_terrain)} />
<ThingLink
type={construction.post_terrain.startsWith("f_")
? "furniture"
: "terrain",
construction.post_terrain
)} />
<ThingLink
type={construction.post_terrain.startsWith("f_")
? "furniture"
: "terrain"}
id={construction.post_terrain} />
: "terrain"}
id={construction.post_terrain} />
{/if}
</dd>
{/if}
</dl>
Expand Down
147 changes: 95 additions & 52 deletions src/types/item/ComponentOf.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,32 @@ const _context = "Item Basic Info";
const data = getContext<CddaData>("data");
const { byTool, byComponent, byConstruction } = data.getItemComponents();
const itemComponents = data.getItemComponents();
const recipes: Set<string> = byComponent.get(item_id) ?? new Set();
const toolRecipes: Set<string> = byTool.get(item_id) ?? new Set();
const recipes: Set<string> =
itemComponents.byComponent.get(item_id) ?? new Set();
const toolRecipes: Set<string> =
itemComponents.byTool.get(item_id) ?? new Set();
const constructionComponents = data.getConstructionComponents();
const constructions = [
...(constructionComponents.byComponent.get(item_id) ?? new Set()),
]
.map((id) => data.byId("construction", id))
.sort((a, b) =>
singularName(data.byId("construction_group", a.group)).localeCompare(
singularName(data.byId("construction_group", b.group))
)
);
const toolConstructions = [
...(constructionComponents.byTool.get(item_id) ?? new Set()),
]
.map((id) => data.byId("construction", id))
.sort((a, b) =>
singularName(data.byId("construction_group", a.group)).localeCompare(
singularName(data.byId("construction_group", b.group))
)
);
const providedByVparts = data
.byType("vehicle_part")
Expand All @@ -32,14 +54,6 @@ const toolResults = [...toolRecipes].sort((a, b) =>
singularName(data.byId("item", b))
)
);
const constructions = [...(byConstruction.get(item_id) ?? new Set())]
.map((id) => data.byId("construction", id))
.sort((a, b) =>
singularName(data.byId("construction_group", a.group)).localeCompare(
singularName(data.byId("construction_group", b.group))
)
);
</script>

{#if providedByVparts.length}
Expand Down Expand Up @@ -69,47 +83,76 @@ const constructions = [...(byConstruction.get(item_id) ?? new Set())]
</section>
{/if}

<div class="side-by-side">
{#if results.length}
<section>
<h1>{t("Component Of", { _context, _comment: "Section heading" })}</h1>
<LimitedList items={results} let:item>
<ItemSymbol item={data.byId("item", item)} />
<ThingLink type="item" id={item} />
</LimitedList>
</section>
{/if}
{#if results.length || toolResults.length}
<div class="side-by-side">
{#if results.length}
<section>
<h1>{t("Component Of", { _context, _comment: "Section heading" })}</h1>
<LimitedList items={results} let:item>
<ItemSymbol item={data.byId("item", item)} />
<ThingLink type="item" id={item} />
</LimitedList>
</section>
{/if}

{#if toolResults.length}
<section>
<h1>
{t("Tool For Crafting", { _context, _comment: "Section heading" })}
</h1>
<LimitedList items={toolResults} let:item>
<ItemSymbol item={data.byId("item", item)} />
<ThingLink type="item" id={item} />
</LimitedList>
</section>
{/if}
</div>
{#if toolResults.length}
<section>
<h1>
{t("Tool For Crafting", { _context, _comment: "Section heading" })}
</h1>
<LimitedList items={toolResults} let:item>
<ItemSymbol item={data.byId("item", item)} />
<ThingLink type="item" id={item} />
</LimitedList>
</section>
{/if}
</div>
{/if}

{#if constructions.length}
<section>
<h1>
{t("Used In Construction", { _context, _comment: "Section heading" })}
</h1>
<LimitedList items={constructions} let:item={f}>
<ThingLink id={f.group} type="construction_group" />
{#if f.pre_terrain}
on <ItemSymbol
item={data.byId(
f.pre_terrain.startsWith("f_") ? "furniture" : "terrain",
f.pre_terrain
)} />
<ThingLink
type={f.pre_terrain.startsWith("f_") ? "furniture" : "terrain"}
id={f.pre_terrain} />
{/if}
</LimitedList>
</section>
{#if constructions.length || toolConstructions.length}
<div class="side-by-side">
{#if constructions.length}
<section>
<h1>
{t("Used In Construction", { _context, _comment: "Section heading" })}
</h1>
<LimitedList items={constructions} let:item={f}>
<ThingLink id={f.group} type="construction_group" />
{#if f.pre_terrain}
on <ItemSymbol
item={data.byId(
f.pre_terrain.startsWith("f_") ? "furniture" : "terrain",
f.pre_terrain
)} />
<ThingLink
type={f.pre_terrain.startsWith("f_") ? "furniture" : "terrain"}
id={f.pre_terrain} />
{/if}
</LimitedList>
</section>
{/if}
{#if toolConstructions.length}
<section>
<h1>
{t("Tool For Construction", {
_context,
_comment: "Section heading",
})}
</h1>
<LimitedList items={toolConstructions} let:item={f}>
<ThingLink id={f.group} type="construction_group" />
{#if f.pre_terrain}
on <ItemSymbol
item={data.byId(
f.pre_terrain.startsWith("f_") ? "furniture" : "terrain",
f.pre_terrain
)} />
<ThingLink
type={f.pre_terrain.startsWith("f_") ? "furniture" : "terrain"}
id={f.pre_terrain} />
{/if}
</LimitedList>
</section>
{/if}
</div>
{/if}

0 comments on commit 11894d3

Please sign in to comment.