Skip to content

Commit

Permalink
新增单工厂总输入输出计算
Browse files Browse the repository at this point in the history
  • Loading branch information
imsfc committed Sep 22, 2024
1 parent 3a9fdc7 commit 04b5f2b
Show file tree
Hide file tree
Showing 5 changed files with 105 additions and 1 deletion.
24 changes: 23 additions & 1 deletion src/components/ModularFactoryDrawerContent.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -306,8 +306,14 @@ export default defineComponent({
return modularFactoryList.getModularFactory(props.modularFactoryId)
})

const modularFactoryComputed = computed(() => {
return modularFactoryList.modularFactoryComputedRecord[
props.modularFactoryId
]
})

return () => (
<NFlex size="large" vertical align="start">
<NFlex size="large" vertical>
<NForm labelPlacement="left" labelWidth="auto" inline>
<NFormItem label={t('name')} path="name">
<NInput
Expand Down Expand Up @@ -343,13 +349,29 @@ export default defineComponent({
</NFlex>

<NDataTable
rowKey={({ id }) => id}
columns={createColumns({
onDelete: (id) => {
modularFactoryList.deleteAssemblyLine(modularFactory.value.id, id)
},
})}
data={modularFactory.value.data}
/>

<NFlex size="large" align="start" wrap={false}>
<NFlex class="flex-[1_1_50%]">
<div class="w-full">{t('totalInputs')}</div>
{modularFactoryComputed.value?.inputs.map(
renderItemQuantityPerMinute,
)}
</NFlex>
<NFlex class="flex-[1_1_50%]">
<div class="w-full">{t('totalOutputs')}</div>
{modularFactoryComputed.value?.outputs.map(
renderItemQuantityPerMinute,
)}
</NFlex>
</NFlex>
</NFlex>
)
},
Expand Down
2 changes: 2 additions & 0 deletions src/locales/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ remark: Remark
power: Power
inputs: Inputs
outputs: Outputs
totalInputs: Total Inputs
totalOutputs: Total Outputs
action: Action

delete: Delete
Expand Down
2 changes: 2 additions & 0 deletions src/locales/zh-CN.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ remark: 备注
power: 功率
inputs: 输入
outputs: 输出
totalInputs: 总输入
totalOutputs: 总输出
action: 操作

delete: 删除
Expand Down
72 changes: 72 additions & 0 deletions src/stores/modularFactoryList.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ import type {
AssemblyLine,
AssemblyLineComputed,
Id,
ItemQuantityPerMinute,
ModularFactory,
ModularFactoryComputed,
} from '@/types'
import { getBuildingById, getRecipeById } from '@/data'
import {
Expand Down Expand Up @@ -132,6 +134,75 @@ export const useModularFactoryList = defineStore('modularFactoryList', () => {
return _data
})

const modularFactoryComputedRecord = computed(() => {
const _data: Record<Id, ModularFactoryComputed | null> = {}
data.value.forEach(({ id, data }) => {
const inputAndOutputs: {
itemId: Id
input: number
output: number
}[] = []
const inputAndOutputsAdd = (
type: 'input' | 'output',
itemId: Id,
quantityPerMinute: number,
) => {
let inputAndOutput = inputAndOutputs.find(
(item) => item.itemId === itemId,
)
if (!inputAndOutput) {
inputAndOutput = {
itemId,
input: 0,
output: 0,
}
inputAndOutputs.push(inputAndOutput)
}
inputAndOutput[type] += quantityPerMinute
}
data.forEach(({ id }) => {
const assemblyLineComputed = assemblyLineComputedRecord.value[id]
if (!assemblyLineComputed) {
return
}
assemblyLineComputed.inputs.forEach(({ itemId, quantityPerMinute }) => {
inputAndOutputsAdd('input', itemId, quantityPerMinute)
})
assemblyLineComputed.outputs.forEach(
({ itemId, quantityPerMinute }) => {
inputAndOutputsAdd('output', itemId, quantityPerMinute)
},
)
})
const inputAndOutputsAbs = inputAndOutputs.map(
({ itemId, input, output }): ItemQuantityPerMinute => {
return {
itemId,
quantityPerMinute: new Decimal(output).sub(input).toNumber(),
}
},
)
_data[id] = {
modularFactoryId: id,
inputs: inputAndOutputsAbs
.filter(({ quantityPerMinute }) => quantityPerMinute < 0)
.map(({ itemId, quantityPerMinute }) => {
return {
itemId,
quantityPerMinute: new Decimal(quantityPerMinute)
.abs()
.toNumber(),
}
})
.sort((a, b) => b.quantityPerMinute - a.quantityPerMinute),
outputs: inputAndOutputsAbs
.filter(({ quantityPerMinute }) => quantityPerMinute > 0.0001)
.sort((a, b) => b.quantityPerMinute - a.quantityPerMinute),
}
})
return _data
})

// 自增ID
let currentId: Id = Math.max(
0,
Expand Down Expand Up @@ -196,6 +267,7 @@ export const useModularFactoryList = defineStore('modularFactoryList', () => {
return {
data,
assemblyLineComputedRecord,
modularFactoryComputedRecord,
newModularFactory,
deleteModularFactory,
getModularFactory,
Expand Down
6 changes: 6 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,12 @@ export interface ModularFactory {
data: AssemblyLine[]
}

export interface ModularFactoryComputed {
modularFactoryId: Id
inputs: ItemQuantityPerMinute[]
outputs: ItemQuantityPerMinute[]
}

export interface AssemblyLine {
id: Id
targetItemId: Id | null
Expand Down

0 comments on commit 04b5f2b

Please sign in to comment.