minecraft-cc-tools/inferium-gui.lua
2024-05-23 23:59:41 +02:00

136 lines
3.4 KiB
Lua

local net = require('libs/net')
local utils = require('libs/utils')
local CountersSelector = require('libs/ui/CountersSelector')
local INFERIUM_SERVER = 'inferium.com'
local function centerString(str, width)
width = width or term.getSize()
local padding = (width / 2) - (#str / 2)
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'
end
local function parseSeedName(seedName)
local result, nbReplaced = string.gsub(seedName, 'mysticalagriculture:', '')
if nbReplaced == 0 then
return nil
end
local finalResult, nbFinalReplaced = string.gsub(result, '_seeds', '')
if nbFinalReplaced == 0 then
return nil
end
return finalResult
end
local function getCountersSelectorConfig(counterMax)
local titleFn = function(countersMap, _, win)
local total = 0;
for _, counterPayload in pairs(countersMap) do
if counterPayload and counterPayload.count then
total = total + counterPayload.count
end
end
local width = win.getSize()
return centerString("" .. total .. '/' .. counterMax .. ' used farmlands' .. "", width)
end
local config = {
counterMax = counterMax,
titleFn = titleFn
}
return config
end
local function fetchAllHarvesters(serverName)
local harvesters, listErrMessage = net.sendQuery(serverName, { type = 'list-harvesters' })
if listErrMessage then
error('getAllHarvesters cannot list: ' .. listErrMessage, 0)
end
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
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)