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