Added the producing graph

This commit is contained in:
Sebastian Seedorf
2022-08-13 02:13:47 +02:00
parent 185f39cb8a
commit 7682aeaea1
14 changed files with 312 additions and 47 deletions

View File

@@ -22,6 +22,11 @@
margin-inline-end: 0.2em;
}
.noAmount {
margin-inline: 0.1em;
margin-block: 0.1em -0.1em;
}
@media (prefers-color-scheme: dark) {
.span {
border-color: #111111;

View File

@@ -2,28 +2,36 @@ import {FC, HTMLProps, useMemo} from "react"
import {Entity} from "../../../src/types"
import {useFactories} from "../../../src/hooks/useFactories"
import styles from './EntityIcon.module.css'
import cx from "classnames";
interface Props extends Omit<HTMLProps<HTMLSpanElement>, 'value'> {
value: Entity|string
amount?: number
}
export const EntityIcon: FC<Props> = ({value, amount, ...rest}) => {
export const EntityIcon: FC<Props> = ({className, value, amount, ...rest}) => {
const {findFactory} = useFactories()
const entity = useMemo<Entity>(() => {
return typeof value === "object"
? value
: findFactory(value) ?? {
href: value,
name: value,
image: value,
recipe: undefined
}
: value === '/Time'
? {
href: '/Time',
name: 'Time',
image: '/images/Time.png',
recipe: undefined
}
: findFactory(value) ?? {
href: value,
name: value,
image: value,
recipe: undefined
}
}, [findFactory, value])
return <span className={styles.span} {...rest}>
return <span {...rest} className={cx(className, styles.span)} title={entity.name}>
{/* eslint-disable-next-line @next/next/no-img-element */}
<img className={styles.img} src={`https://wiki.factorio.com${entity.image}`} alt={entity.name}/>
<img className={cx(styles.img, amount === undefined ? styles.noAmount : undefined)} src={`https://wiki.factorio.com${entity.image}`} alt={entity.name}/>
{amount !== undefined && <span className={styles.amount}>{amount}</span>}
</span>
}

View File

@@ -1,10 +1,11 @@
import {FC, memo, useCallback, useEffect, useMemo, useState} from "react";
import {FC, memo, useCallback, useMemo, useState} from "react";
import {FactorySelect} from "../FactorySelect/FactorySelect";
import {useFactories} from "../../../src/hooks/useFactories";
import {EnrichedEntity, Entity, Group} from "../../../src/types";
import styles from "./Group.module.css"
import {EnrichedEntity, Group} from "../../../src/types";
import styles from "./GroupBox.module.css"
import {EntitySpan} from "../EntitySpan/EntitySpan";
import {useGroups} from "../../contexts/GroupProvider";
import {calculateInputs} from "../../../src/calculateInputs";
interface Props {
group: Group
@@ -31,30 +32,15 @@ const GroupBoxBase: FC<Props> = ({ group }) => {
const [isDeleteConfirm, setDeleteConfirm] = useState(false)
const [inputs, intermediates] = useMemo<[string[], string[]]>(() => {
const [inputs, intermediates] = useMemo(() => {
const allProducingFactories = [...exports, ...malls]
const prducingSet = new Set(allProducingFactories)
const ignored = new Set(ignoredFactories)
const base = new Set(baseFactories)
const inputs = new Set<string>()
const intermediates = new Set<string>()
let next: string|undefined
while (next = allProducingFactories.pop()) {
const pres = Object.keys(findFactory(next)?.recipe?.prerequisites ?? {})
for (const pre of pres) {
if (exportedFactories.has(pre) || base.has(pre)) {
if (!prducingSet.has(pre)) inputs.add(pre)
} else if (!intermediates.has(pre)) {
if (!prducingSet.has(pre)) intermediates.add(pre)
allProducingFactories.push(pre)
}
}
}
return [
Array.from(inputs).sort((a, b) => a.localeCompare(b)),
Array.from(intermediates).sort((a, b) => a.localeCompare(b))
]
return calculateInputs(
allProducingFactories,
ignoredFactories,
baseFactories,
exportedFactories,
findFactory
)
}, [exports, malls, ignoredFactories, baseFactories, findFactory, exportedFactories])
const [suggestionsExport, suggestionMall] = useMemo<[EnrichedEntity[], EnrichedEntity[]]>(() => {
@@ -99,7 +85,7 @@ const GroupBoxBase: FC<Props> = ({ group }) => {
onBlur={() => setDeleteConfirm(false)}
onClick={() => !isDeleteConfirm ? setDeleteConfirm(true) : removeGroup(name)} style={{display: 'block'}}
>
{isDeleteConfirm ? 'Delete Group?' : 'X'}
{isDeleteConfirm ? 'Delete GroupBox?' : 'X'}
</button>
<h4>Exported Factories</h4>
<FactorySelect

View File

@@ -1,5 +1,5 @@
import {FC, useMemo, useRef, useState} from "react";
import {GroupBox} from "./Group/Group";
import {GroupBox} from "./GroupBox/GroupBox";
import styles from "./Home.module.css"
import {useFactories} from "../../src/hooks/useFactories";
import {EnrichedEntity} from "../../src/types";
@@ -9,7 +9,7 @@ import {Preferences} from "./Preferences/Preferences";
import {download, streamToArrayBuffer} from "../../src/download";
import Link from "next/link";
export const HomeComponent: FC = () => {
export const Home: FC = () => {
const {factories} = useFactories()
const {
groups,
@@ -48,6 +48,7 @@ export const HomeComponent: FC = () => {
if (inputRef.current) inputRef.current.value = null as unknown as string
}
}}/>
<Link href={'/visualize'}>Visualize</Link>
<Preferences />
<fieldset>
<legend>Missing export factories</legend>

View File

@@ -8,6 +8,7 @@ interface Props {
}
export const RecipeSpan: FC<Props> = ({recipe}) => {
const toEntityIcon = ([key, amount]: [string, number]) => <EntityIcon key={key} value={key} amount={amount} />
const joinByPlus = (elems: JSX.Element[]) => elems.reduce((acc, curr) => {
if (acc.length) {
return [...acc, '+', curr]
@@ -15,9 +16,9 @@ export const RecipeSpan: FC<Props> = ({recipe}) => {
return [curr]
}
}, [] as (JSX.Element|string)[])
const before = Object.entries({...recipe.prerequisites}).map(([key, amount]) => <EntityIcon key={key} value={key} amount={amount} />)
const after = Object.entries({...recipe.output}).map(([key, amount]) => <EntityIcon key={key} value={key} amount={amount} />)
const before = Object.entries({...recipe.prerequisites}).map(toEntityIcon)
const after = Object.entries({...recipe.output}).map(toEntityIcon)
return <span className={styles.recipe}>
{joinByPlus(before)} {joinByPlus(after)}
{joinByPlus([toEntityIcon(['/Time', recipe.time]), ...before])} {joinByPlus(after)}
</span>
}

View File

@@ -0,0 +1,32 @@
.plane {
position: relative;
overflow: scroll;
overflow-scrolling: touch;
background-color: lightsalmon;
padding: 2em;
height: 80vh;
}
.tiny {
font-size: 0.5em;
margin-top: -1.6em;
}
.small {
font-size: 0.8em;
}
.node {
display: inline-block;
position: absolute;
width: 200px;
height: fit-content;
padding: 0.2em;
background-color: red;
}
.linkOut {
position: absolute;
top: 0.5em;
right: 0.5em;
}

View File

@@ -0,0 +1,85 @@
import {FC, useMemo} from "react";
import {EnrichedEntity, Recipe} from "../../src/types";
import styles from './ProducingGraph.module.css'
import {EntityIcon} from "../home/EntityIcon/EntityIcon";
import {sortByProperty} from "../../src/utils";
import Link from "next/link";
import {RecipeSpan} from "../home/Recipe/Recipe";
export interface ProducingNode {
inputs: string[]
outputs: string[]
name: string
icons?: (EnrichedEntity|string)[]
linkOut?: string
recipe?: Recipe
}
interface Props {
nodes: ProducingNode[]
inputs: string[]
}
export const ProducingGraph: FC<Props> = ({nodes, inputs}) => {
const rows: ProducingNode[][] = useMemo(() => {
const available = new Set(inputs)
let todo = [...nodes]
const result: ProducingNode[][] = []
while (todo.length) {
const amount = todo.length
const thisRow: string[] = []
result.push([])
todo = todo.filter((node) => {
if (node.inputs.every(input => available.has(input))) {
result[result.length - 1].push(node)
thisRow.push(...node.outputs)
return false
}
return true
})
thisRow.map(uid => available.add(uid))
result[result.length - 1].sort(sortByProperty(val => -val.outputs.length * 1000 + -val.inputs.length))
if (amount === todo.length) {
console.warn("Loop detected! Left over:", todo)
result.pop()
break
}
}
return result
}, [inputs, nodes])
return <div className={styles.plane}>
{inputs.map((input, idx) => <EntityIcon className={styles.input} style={{left: 75 * idx}} key={input} value={input} />)}
{rows.map((row, colIdx) => row.map((node, idx) => (
<div
className={styles.node}
key={node.name}
style={{left: 220*idx, top: 320*colIdx+100}}
>
{ node.linkOut && <Link className={styles.linkOut} href={node.linkOut}>🔗</Link> }
<h3>{node.name}</h3>
{ node.icons?.length ? <div className={styles.tiny}>
{node.icons.map((input) => <EntityIcon key={typeof input === "string" ? input : input.href} value={input} />)}
</div> : null }
{
node.recipe
? <RecipeSpan recipe={node.recipe}/>
: <>
<h4>Inputs</h4>
<div className={styles.small}>
{node.inputs.map((input) => <EntityIcon key={input} value={input} />)}
</div>
{node.outputs.length ? <>
<h4>Outputs</h4>
<div className={styles.small}>
{node.outputs.map((input) => <EntityIcon key={input} value={input} />)}
</div>
</>: null}
</>
}
</div>
)))}
</div>
}