Refactor buildCatalogue and storage capacity calculations for improved efficiency
This commit is contained in:
@@ -38,33 +38,15 @@ local function scanInventory(deviceName)
|
|||||||
end
|
end
|
||||||
|
|
||||||
-- Build a full catalogue: itemName -> { {chest=name, total=N}, ... }
|
-- Build a full catalogue: itemName -> { {chest=name, total=N}, ... }
|
||||||
-- Scans all chests in parallel for speed
|
|
||||||
local function buildCatalogue()
|
local function buildCatalogue()
|
||||||
local chests = getChests()
|
|
||||||
local results = {} -- index -> { chest=name, contents={} }
|
|
||||||
|
|
||||||
-- Build parallel scan tasks
|
|
||||||
local tasks = {}
|
|
||||||
for i, chest in ipairs(chests) do
|
|
||||||
results[i] = { chest = chest, contents = {} }
|
|
||||||
tasks[i] = function()
|
|
||||||
results[i].contents = scanInventory(chest)
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
-- Run all scans simultaneously
|
|
||||||
if #tasks > 0 then
|
|
||||||
parallel.waitForAll(table.unpack(tasks))
|
|
||||||
end
|
|
||||||
|
|
||||||
-- Merge results into catalogue
|
|
||||||
local catalogue = {}
|
local catalogue = {}
|
||||||
for _, result in ipairs(results) do
|
for _, chest in ipairs(getChests()) do
|
||||||
for itemName, info in pairs(result.contents) do
|
local contents = scanInventory(chest)
|
||||||
|
for itemName, info in pairs(contents) do
|
||||||
if not catalogue[itemName] then
|
if not catalogue[itemName] then
|
||||||
catalogue[itemName] = {}
|
catalogue[itemName] = {}
|
||||||
end
|
end
|
||||||
table.insert(catalogue[itemName], { chest = result.chest, total = info.total })
|
table.insert(catalogue[itemName], { chest = chest, total = info.total })
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
return catalogue
|
return catalogue
|
||||||
@@ -162,32 +144,18 @@ local function drawDashboard()
|
|||||||
monCenter(3, string.rep("\140", math.min(w - 4, 60)), colors.cyan, colors.lightBlue)
|
monCenter(3, string.rep("\140", math.min(w - 4, 60)), colors.cyan, colors.lightBlue)
|
||||||
|
|
||||||
-- ===== Storage capacity bar =====
|
-- ===== Storage capacity bar =====
|
||||||
-- Count total slots and used slots across all chests (in parallel)
|
-- Count total slots and used slots across all chests
|
||||||
local totalSlots = 0
|
local totalSlots = 0
|
||||||
local usedSlots = 0
|
local usedSlots = 0
|
||||||
local capTasks = {}
|
for _, chestName in ipairs(chests) do
|
||||||
local capResults = {}
|
local inv = peripheral.wrap(chestName)
|
||||||
for i, chestName in ipairs(chests) do
|
if inv then
|
||||||
capResults[i] = { total = 0, used = 0 }
|
totalSlots = totalSlots + inv.size()
|
||||||
capTasks[i] = function()
|
for _ in pairs(inv.list()) do
|
||||||
local inv = peripheral.wrap(chestName)
|
usedSlots = usedSlots + 1
|
||||||
if inv then
|
|
||||||
local size = inv.size()
|
|
||||||
capResults[i].total = size
|
|
||||||
local contents = inv.list()
|
|
||||||
for _ in pairs(contents) do
|
|
||||||
capResults[i].used = capResults[i].used + 1
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
if #capTasks > 0 then
|
|
||||||
parallel.waitForAll(table.unpack(capTasks))
|
|
||||||
end
|
|
||||||
for _, r in ipairs(capResults) do
|
|
||||||
totalSlots = totalSlots + r.total
|
|
||||||
usedSlots = usedSlots + r.used
|
|
||||||
end
|
|
||||||
local freeSlots = totalSlots - usedSlots
|
local freeSlots = totalSlots - usedSlots
|
||||||
local usedRatio = totalSlots > 0 and (usedSlots / totalSlots) or 0
|
local usedRatio = totalSlots > 0 and (usedSlots / totalSlots) or 0
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user