feat(inferium-gui): fetch all configs
This commit is contained in:
parent
28ea977b86
commit
491d85c951
115
inferium-gui.lua
115
inferium-gui.lua
@ -1,6 +1,8 @@
|
|||||||
|
local net = require('libs/net')
|
||||||
|
local utils = require('libs/utils')
|
||||||
local CountersSelector = require('libs/ui/CountersSelector')
|
local CountersSelector = require('libs/ui/CountersSelector')
|
||||||
|
|
||||||
local COUNTER_MAX = 8
|
local INFERIUM_SERVER = 'inferium.com'
|
||||||
|
|
||||||
local function centerString(str, width)
|
local function centerString(str, width)
|
||||||
width = width or term.getSize()
|
width = width or term.getSize()
|
||||||
@ -8,6 +10,17 @@ local function centerString(str, width)
|
|||||||
return string.rep(' ', padding) .. str
|
return string.rep(' ', padding) .. str
|
||||||
end
|
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)
|
local function formatSeedName(essenceName)
|
||||||
return 'mysticalagriculture:' .. essenceName .. '_seeds'
|
return 'mysticalagriculture:' .. essenceName .. '_seeds'
|
||||||
@ -51,41 +64,73 @@ local function getCountersSelectorConfig(counterMax)
|
|||||||
return config
|
return config
|
||||||
end
|
end
|
||||||
|
|
||||||
local countersMap = {
|
local function fetchAllHarvesters(serverName)
|
||||||
{ name = "iron", count = 1 },
|
local harvesters, listErrMessage = net.sendQuery(serverName, { type = 'list-harvesters' })
|
||||||
{ 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 },
|
|
||||||
}
|
|
||||||
|
|
||||||
|
if listErrMessage then
|
||||||
|
error('getAllHarvesters cannot list: ' .. listErrMessage, 0)
|
||||||
|
end
|
||||||
|
|
||||||
local function main()
|
if utils.sizeof(harvesters) == 0 then
|
||||||
local result = CountersSelector(countersMap, getCountersSelectorConfig(COUNTER_MAX))
|
error('getAllHarvesters get no harvesters from the remote server', 0)
|
||||||
print(textutils.serialize(result))
|
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
|
end
|
||||||
|
|
||||||
main()
|
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)
|
||||||
Loading…
Reference in New Issue
Block a user