refactor: introduce new shared inferium lib
This commit is contained in:
parent
58bb9d6d24
commit
43d931994a
@ -1,43 +1,14 @@
|
|||||||
local net = require('libs/net')
|
local net = require('libs/net')
|
||||||
local utils = require('libs/utils')
|
local utils = require('libs/utils')
|
||||||
|
local inferium = require('libs/inferium')
|
||||||
local CountersSelector = require('libs/ui/CountersSelector')
|
local CountersSelector = require('libs/ui/CountersSelector')
|
||||||
|
|
||||||
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()
|
||||||
local padding = (width / 2) - (#str / 2)
|
local padding = (width / 2) - (#str / 2)
|
||||||
return string.rep(' ', padding) .. str
|
return string.rep(' ', padding) .. str
|
||||||
end
|
end
|
||||||
|
|
||||||
local function formatSeedName(essenceName)
|
|
||||||
if not essenceName then
|
|
||||||
return false
|
|
||||||
end
|
|
||||||
|
|
||||||
return 'mysticalagriculture:' .. essenceName .. '_seeds'
|
|
||||||
end
|
|
||||||
|
|
||||||
local function parseSeedName(seedName)
|
|
||||||
if not seedName then
|
|
||||||
return nil
|
|
||||||
end
|
|
||||||
|
|
||||||
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 function getCountersSelectorConfig(counterMax)
|
||||||
local titleFn = function(countersMap, _, win)
|
local titleFn = function(countersMap, _, win)
|
||||||
local total = 0;
|
local total = 0;
|
||||||
@ -148,7 +119,7 @@ local function getInitialCountersMap(availableSeeds)
|
|||||||
local result = {}
|
local result = {}
|
||||||
for k, seedName in pairs(availableSeeds) do
|
for k, seedName in pairs(availableSeeds) do
|
||||||
result[k] = {
|
result[k] = {
|
||||||
name = parseSeedName(seedName),
|
name = inferium.parseSeedName(seedName),
|
||||||
count = 0
|
count = 0
|
||||||
}
|
}
|
||||||
end
|
end
|
||||||
@ -160,7 +131,7 @@ local function createCountersMap(harvesters)
|
|||||||
|
|
||||||
for _, harvester in pairs(harvesters) do
|
for _, harvester in pairs(harvesters) do
|
||||||
for _, seedName in pairs(harvester.config.plan) do
|
for _, seedName in pairs(harvester.config.plan) do
|
||||||
local name = parseSeedName(seedName)
|
local name = inferium.parseSeedName(seedName)
|
||||||
if name then
|
if name then
|
||||||
local currentCounter = countersMapIndexed[name] or 0
|
local currentCounter = countersMapIndexed[name] or 0
|
||||||
countersMapIndexed[name] = currentCounter + 1
|
countersMapIndexed[name] = currentCounter + 1
|
||||||
@ -192,7 +163,7 @@ local function pickSeedNameFromCountersMap(countersMap)
|
|||||||
newCountersMap[k] = newCounterPayload
|
newCountersMap[k] = newCounterPayload
|
||||||
end
|
end
|
||||||
|
|
||||||
return formatSeedName(pickedCounterName), newCountersMap
|
return inferium.formatSeedName(pickedCounterName), newCountersMap
|
||||||
end
|
end
|
||||||
|
|
||||||
local function preparePayloads(countersMap, maxCounter, harvesters)
|
local function preparePayloads(countersMap, maxCounter, harvesters)
|
||||||
@ -222,9 +193,9 @@ local function preparePayloads(countersMap, maxCounter, harvesters)
|
|||||||
return payloads
|
return payloads
|
||||||
end
|
end
|
||||||
|
|
||||||
local function saveAllConfigs(payloads)
|
local function saveAllConfigs(serverName, payloads)
|
||||||
for _, payload in pairs(payloads) do
|
for _, payload in pairs(payloads) do
|
||||||
local setConfigResponse, errMessage = net.sendQuery(INFERIUM_SERVER, { type = 'set-config', payload = payload })
|
local setConfigResponse, errMessage = net.sendQuery(serverName, { type = 'set-config', payload = payload })
|
||||||
|
|
||||||
if errMessage then
|
if errMessage then
|
||||||
return false, errMessage
|
return false, errMessage
|
||||||
@ -266,7 +237,7 @@ local function main(serverName)
|
|||||||
end
|
end
|
||||||
|
|
||||||
print('> saving...')
|
print('> saving...')
|
||||||
local saveOk, saveErrMessage = saveAllConfigs(payloads)
|
local saveOk, saveErrMessage = saveAllConfigs(serverName, payloads)
|
||||||
|
|
||||||
if saveOk then
|
if saveOk then
|
||||||
print('> done')
|
print('> done')
|
||||||
@ -277,4 +248,4 @@ local function main(serverName)
|
|||||||
net.closeRednet()
|
net.closeRednet()
|
||||||
end
|
end
|
||||||
|
|
||||||
main(INFERIUM_SERVER)
|
main(inferium.SERVER)
|
||||||
@ -1,5 +1,6 @@
|
|||||||
local net = require('libs/net')
|
local net = require('libs/net')
|
||||||
local utils = require('libs/utils')
|
local utils = require('libs/utils')
|
||||||
|
local inferium = require('libs/inferium')
|
||||||
local turtleUtils = require('libs/turtle-utils')
|
local turtleUtils = require('libs/turtle-utils')
|
||||||
|
|
||||||
local VERSION = "4.0.1"
|
local VERSION = "4.0.1"
|
||||||
@ -12,8 +13,6 @@ local ADDITIONAL_FUEL_MARGIN = 100
|
|||||||
local MAX_FERTILIZED_ESSENCE = 32
|
local MAX_FERTILIZED_ESSENCE = 32
|
||||||
local MIN_FREE_SLOTS_BEFORE_COMPACT = 4
|
local MIN_FREE_SLOTS_BEFORE_COMPACT = 4
|
||||||
|
|
||||||
local FERTILIZED_ESSENCE = 'mysticalagriculture:fertilized_essence'
|
|
||||||
|
|
||||||
-- a table with the list of current crops
|
-- a table with the list of current crops
|
||||||
local localPlan = nil
|
local localPlan = nil
|
||||||
|
|
||||||
@ -81,11 +80,6 @@ local function getSeedNameFromCropName(cropName)
|
|||||||
return string.gsub(cropName, 'crop', 'seeds')
|
return string.gsub(cropName, 'crop', 'seeds')
|
||||||
end
|
end
|
||||||
|
|
||||||
local function isSeed(item)
|
|
||||||
local tags = item and item.tags or {}
|
|
||||||
return tags['forge:seeds'] or tags['mysticalagriculture:seeds'] or false
|
|
||||||
end
|
|
||||||
|
|
||||||
-- Inventory utils
|
-- Inventory utils
|
||||||
local function getItemSlot(inventory, itemName)
|
local function getItemSlot(inventory, itemName)
|
||||||
for slot, item in pairs(inventory.list()) do
|
for slot, item in pairs(inventory.list()) do
|
||||||
@ -206,7 +200,7 @@ local function compactIfNeeded()
|
|||||||
end
|
end
|
||||||
|
|
||||||
local function applyFertilizedEssenceIfAny()
|
local function applyFertilizedEssenceIfAny()
|
||||||
if turtleUtils.selectItemByName(FERTILIZED_ESSENCE) then
|
if turtleUtils.selectItemByName(inferium.FERTILIZED_ESSENCE) then
|
||||||
while turtle.placeDown() do end
|
while turtle.placeDown() do end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
@ -319,14 +313,14 @@ local function retrieveLocalPlan(config)
|
|||||||
goBackToHome(config)
|
goBackToHome(config)
|
||||||
end
|
end
|
||||||
|
|
||||||
local function fetchRemoteConfig()
|
local function fetchRemoteConfig(serverName)
|
||||||
print('> fetch remote config')
|
print('> fetch remote config')
|
||||||
local message = { type = 'register-and-get-config' }
|
local message = { type = 'register-and-get-config' }
|
||||||
|
|
||||||
local lastErrMessage = nil
|
local lastErrMessage = nil
|
||||||
|
|
||||||
while true do
|
while true do
|
||||||
local replyMessage, errMessage = net.sendQuery(INFERIUM_SERVER, message, NETWORK_TIMEOUT)
|
local replyMessage, errMessage = net.sendQuery(serverName, message, NETWORK_TIMEOUT)
|
||||||
if replyMessage and replyMessage.payload then
|
if replyMessage and replyMessage.payload then
|
||||||
previouslyFetchedRemoteConfig = replyMessage.payload
|
previouslyFetchedRemoteConfig = replyMessage.payload
|
||||||
print('> config fetched')
|
print('> config fetched')
|
||||||
@ -443,10 +437,10 @@ local function retrieveFertilizedEssences(_)
|
|||||||
local storageInventory = turtleUtils.waitForInventory('bottom', WAIT_ITEM_IDLE_TIME)
|
local storageInventory = turtleUtils.waitForInventory('bottom', WAIT_ITEM_IDLE_TIME)
|
||||||
local bufferInventory = turtleUtils.waitForInventory('front', WAIT_ITEM_IDLE_TIME)
|
local bufferInventory = turtleUtils.waitForInventory('front', WAIT_ITEM_IDLE_TIME)
|
||||||
|
|
||||||
local slot = getItemSlot(storageInventory, FERTILIZED_ESSENCE)
|
local slot = getItemSlot(storageInventory, inferium.FERTILIZED_ESSENCE)
|
||||||
|
|
||||||
if slot then
|
if slot then
|
||||||
local inventoryCount = getCountItem(storageInventory, FERTILIZED_ESSENCE)
|
local inventoryCount = getCountItem(storageInventory, inferium.FERTILIZED_ESSENCE)
|
||||||
local realCount = math.min(MAX_FERTILIZED_ESSENCE, inventoryCount)
|
local realCount = math.min(MAX_FERTILIZED_ESSENCE, inventoryCount)
|
||||||
|
|
||||||
print('> retrieve ' .. tostring(realCount) .. ' fertilized essences from storage')
|
print('> retrieve ' .. tostring(realCount) .. ' fertilized essences from storage')
|
||||||
@ -559,7 +553,7 @@ local function main()
|
|||||||
print('> Starting cycle ' .. tostring(cycleNumber))
|
print('> Starting cycle ' .. tostring(cycleNumber))
|
||||||
print()
|
print()
|
||||||
|
|
||||||
local remoteConfig = fetchRemoteConfig()
|
local remoteConfig = fetchRemoteConfig(inferium.SERVER)
|
||||||
|
|
||||||
dropAllProcedure(remoteConfig)
|
dropAllProcedure(remoteConfig)
|
||||||
refuelProcedure(remoteConfig)
|
refuelProcedure(remoteConfig)
|
||||||
|
|||||||
@ -1,27 +1,20 @@
|
|||||||
local net = require('libs/net')
|
local net = require('libs/net')
|
||||||
local utils = require('libs/utils')
|
local utils = require('libs/utils')
|
||||||
local inferium = require('config/inferium')
|
local inferium = require('libs/inferium')
|
||||||
|
|
||||||
|
local inferiumConfig = require('config/inferium')
|
||||||
|
|
||||||
local DEFAULT_HARVESTER_NAME = 'harvester'
|
local DEFAULT_HARVESTER_NAME = 'harvester'
|
||||||
local VERSION = "2.1.0"
|
local VERSION = "2.1.0"
|
||||||
local INFERIUM_SERVER = 'inferium.com'
|
|
||||||
|
|
||||||
local PERSISTED_CONFIGS = '/data/inferium-configs'
|
local PERSISTED_CONFIGS = '/data/inferium-configs'
|
||||||
local UPGRADE_SCRIPT = '/upgrade.lua'
|
local UPGRADE_SCRIPT = '/upgrade.lua'
|
||||||
|
|
||||||
local defaultConfig = inferium.defaultConfig or error('no default config provided in config', 0)
|
local defaultConfig = inferiumConfig.defaultConfig or error('no default config provided in config', 0)
|
||||||
|
|
||||||
local function formatSeedName(essenceName)
|
|
||||||
if not essenceName then
|
|
||||||
return false
|
|
||||||
end
|
|
||||||
|
|
||||||
return 'mysticalagriculture:' .. essenceName .. '_seeds'
|
|
||||||
end
|
|
||||||
|
|
||||||
local defaultAvailableSeeds = {
|
local defaultAvailableSeeds = {
|
||||||
formatSeedName('inferium'),
|
inferium.formatSeedName('inferium'),
|
||||||
formatSeedName('coal')
|
inferium.formatSeedName('coal')
|
||||||
}
|
}
|
||||||
|
|
||||||
-- Persistance utils
|
-- Persistance utils
|
||||||
@ -187,31 +180,6 @@ local function listHarvesters()
|
|||||||
return result
|
return result
|
||||||
end
|
end
|
||||||
|
|
||||||
-- TODO: share this utils with inferium-gui (create libs/inferium)
|
|
||||||
local function parseSeedName(seedName)
|
|
||||||
if not seedName then
|
|
||||||
return nil
|
|
||||||
end
|
|
||||||
|
|
||||||
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 isMysticalSeed(seedName)
|
|
||||||
return not not parseSeedName(seedName)
|
|
||||||
end
|
|
||||||
|
|
||||||
local function listAvailableSeeds()
|
local function listAvailableSeeds()
|
||||||
local inv = peripheral.find('inventory')
|
local inv = peripheral.find('inventory')
|
||||||
|
|
||||||
@ -222,7 +190,7 @@ local function listAvailableSeeds()
|
|||||||
|
|
||||||
local seeds = {}
|
local seeds = {}
|
||||||
for _, item in pairs(inv.list()) do
|
for _, item in pairs(inv.list()) do
|
||||||
if isMysticalSeed(item.name) then
|
if inferium.isMysticalSeed(item.name) then
|
||||||
table.insert(seeds, item.name)
|
table.insert(seeds, item.name)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
@ -254,7 +222,7 @@ local ROUTES = {
|
|||||||
net.openRednet()
|
net.openRednet()
|
||||||
print('> inferium server v' .. VERSION .. ' started')
|
print('> inferium server v' .. VERSION .. ' started')
|
||||||
|
|
||||||
net.listenQuery(INFERIUM_SERVER, function(message, computerId, stopServer)
|
net.listenQuery(inferium.SERVER, function(message, computerId, stopServer)
|
||||||
if type(message) ~= 'table' then
|
if type(message) ~= 'table' then
|
||||||
print('error: malformed message received', textutils.serialize(message))
|
print('error: malformed message received', textutils.serialize(message))
|
||||||
return {}
|
return {}
|
||||||
|
|||||||
@ -29,6 +29,7 @@ local LIST_LIBS_FILES = {
|
|||||||
'libs/utils.lua',
|
'libs/utils.lua',
|
||||||
'libs/turtle-utils.lua',
|
'libs/turtle-utils.lua',
|
||||||
'libs/robot.lua',
|
'libs/robot.lua',
|
||||||
|
'libs/inferium.lua',
|
||||||
'libs/ui/CountersSelector.lua'
|
'libs/ui/CountersSelector.lua'
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
42
libs/inferium.lua
Normal file
42
libs/inferium.lua
Normal file
@ -0,0 +1,42 @@
|
|||||||
|
local inferium = {}
|
||||||
|
|
||||||
|
inferium.SERVER = 'inferium.com'
|
||||||
|
inferium.FERTILIZED_ESSENCE = 'mysticalagriculture:fertilized_essence'
|
||||||
|
|
||||||
|
inferium.formatSeedName = function(essenceName)
|
||||||
|
if not essenceName then
|
||||||
|
return false
|
||||||
|
end
|
||||||
|
|
||||||
|
return 'mysticalagriculture:' .. essenceName .. '_seeds'
|
||||||
|
end
|
||||||
|
|
||||||
|
inferium.parseSeedName = function(seedName)
|
||||||
|
if not seedName then
|
||||||
|
return nil
|
||||||
|
end
|
||||||
|
|
||||||
|
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
|
||||||
|
|
||||||
|
inferium.isMysticalSeed = function(seedName)
|
||||||
|
if inferium.parseSeedName(seedName) then
|
||||||
|
return true
|
||||||
|
end
|
||||||
|
|
||||||
|
return false
|
||||||
|
end
|
||||||
|
|
||||||
|
return inferium
|
||||||
Loading…
Reference in New Issue
Block a user