diff --git a/inferium-gui.lua b/inferium-gui.lua index 009d3f2..41a99ff 100644 --- a/inferium-gui.lua +++ b/inferium-gui.lua @@ -1,6 +1,8 @@ +local net = require('libs/net') +local utils = require('libs/utils') local CountersSelector = require('libs/ui/CountersSelector') -local COUNTER_MAX = 8 +local INFERIUM_SERVER = 'inferium.com' local function centerString(str, width) width = width or term.getSize() @@ -8,6 +10,17 @@ local function centerString(str, width) return string.rep(' ', padding) .. str end +local function objectValues(t) + local result = {} + + local i = 1 + for _, v in pairs(t) do + result[i] = v + i = i + 1 + end + + return result +end local function formatSeedName(essenceName) return 'mysticalagriculture:' .. essenceName .. '_seeds' @@ -51,41 +64,73 @@ local function getCountersSelectorConfig(counterMax) return config end -local countersMap = { - { name = "iron", count = 1 }, - { name = "dye", count = 2 }, - { name = "experience", count = 3 }, - { name = "test_1", count = 0 }, - { name = "test_2", count = 0 }, - { name = "test_3", count = 0 }, - { name = "test_4", count = 0 }, - { name = "test_5", count = 0 }, - { name = "test_6", count = 0 }, - { name = "test_7", count = 0 }, - { name = "test_8", count = 0 }, - { name = "test_9", count = 0 }, - { name = "test_10", count = 0 }, - { name = "test_11", count = 0 }, - { name = "test_12", count = 0 }, - { name = "test_13", count = 0 }, - { name = "test_14", count = 0 }, - { name = "test_15", count = 0 }, - { name = "test_16", count = 0 }, - { name = "test_17", count = 0 }, - { name = "test_18", count = 0 }, - { name = "test_19", count = 0 }, - { name = "test_20", count = 0 }, - { name = "test_21", count = 0 }, - { name = "test_22", count = 0 }, - { name = "test_23", count = 0 }, - { name = "test_24", count = 0 }, - { name = "test_25", count = 0 }, -} +local function fetchAllHarvesters(serverName) + local harvesters, listErrMessage = net.sendQuery(serverName, { type = 'list-harvesters' }) + if listErrMessage then + error('getAllHarvesters cannot list: ' .. listErrMessage, 0) + end -local function main() - local result = CountersSelector(countersMap, getCountersSelectorConfig(COUNTER_MAX)) - print(textutils.serialize(result)) + if utils.sizeof(harvesters) == 0 then + error('getAllHarvesters get no harvesters from the remote server', 0) + end + + for k, harvester in pairs(harvesters) do + local payload, errMessage = net.sendQuery(serverName, { type = 'get-config', payload = { id = harvester.id } }) + + if errMessage then + error('getAllHarvesters cannot get-config: ' .. errMessage, 0) + end + + if not payload then + error('getAllHarvesters no payload returned for harvester ' .. harvester.id, 0) + end + + if not payload.plan then + error('getAllHarvesters no plan returned for the harvester ' .. harvester.id, 0) + end + + harvesters[k].config = payload + end + + return harvesters end -main() \ No newline at end of file +local function getMaxCounter(harvesters) + local maxCounter = 0 + + for _, harvester in pairs(harvesters) do + maxCounter = maxCounter + utils.sizeof(harvester.plan) + end + + return maxCounter +end + +local function createCountersMap(harvesters) + local countersMapIndexed = {} + + for _, harvester in pairs(harvesters) do + for _, seedName in pairs(harvester.plan) do + local name = parseSeedName(seedName) + if name then + local currentCounter = countersMapIndexed[name] or 0 + countersMapIndexed[name] = currentCounter + 1 + end + end + end + + return objectValues(countersMapIndexed) +end + +local function main(serverName) + print('> fetching all configs from ' .. serverName) + local harvesters = fetchAllHarvesters(serverName) + print('> ' .. utils.sizeof(harvesters) .. ' harvesters fetched') + + local maxCounter = getMaxCounter(harvesters) + local countersMap = createCountersMap(harvesters) + local countersMapResult = CountersSelector(countersMap, getCountersSelectorConfig(maxCounter)) + print(textutils.serialize(countersMapResult)) +end + +main(INFERIUM_SERVER) \ No newline at end of file