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 utils = require('libs/utils')
|
||||
local inferium = require('libs/inferium')
|
||||
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 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 titleFn = function(countersMap, _, win)
|
||||
local total = 0;
|
||||
@ -148,7 +119,7 @@ local function getInitialCountersMap(availableSeeds)
|
||||
local result = {}
|
||||
for k, seedName in pairs(availableSeeds) do
|
||||
result[k] = {
|
||||
name = parseSeedName(seedName),
|
||||
name = inferium.parseSeedName(seedName),
|
||||
count = 0
|
||||
}
|
||||
end
|
||||
@ -160,7 +131,7 @@ local function createCountersMap(harvesters)
|
||||
|
||||
for _, harvester in pairs(harvesters) do
|
||||
for _, seedName in pairs(harvester.config.plan) do
|
||||
local name = parseSeedName(seedName)
|
||||
local name = inferium.parseSeedName(seedName)
|
||||
if name then
|
||||
local currentCounter = countersMapIndexed[name] or 0
|
||||
countersMapIndexed[name] = currentCounter + 1
|
||||
@ -192,7 +163,7 @@ local function pickSeedNameFromCountersMap(countersMap)
|
||||
newCountersMap[k] = newCounterPayload
|
||||
end
|
||||
|
||||
return formatSeedName(pickedCounterName), newCountersMap
|
||||
return inferium.formatSeedName(pickedCounterName), newCountersMap
|
||||
end
|
||||
|
||||
local function preparePayloads(countersMap, maxCounter, harvesters)
|
||||
@ -222,9 +193,9 @@ local function preparePayloads(countersMap, maxCounter, harvesters)
|
||||
return payloads
|
||||
end
|
||||
|
||||
local function saveAllConfigs(payloads)
|
||||
local function saveAllConfigs(serverName, payloads)
|
||||
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
|
||||
return false, errMessage
|
||||
@ -266,7 +237,7 @@ local function main(serverName)
|
||||
end
|
||||
|
||||
print('> saving...')
|
||||
local saveOk, saveErrMessage = saveAllConfigs(payloads)
|
||||
local saveOk, saveErrMessage = saveAllConfigs(serverName, payloads)
|
||||
|
||||
if saveOk then
|
||||
print('> done')
|
||||
@ -277,4 +248,4 @@ local function main(serverName)
|
||||
net.closeRednet()
|
||||
end
|
||||
|
||||
main(INFERIUM_SERVER)
|
||||
main(inferium.SERVER)
|
||||
@ -1,5 +1,6 @@
|
||||
local net = require('libs/net')
|
||||
local utils = require('libs/utils')
|
||||
local inferium = require('libs/inferium')
|
||||
local turtleUtils = require('libs/turtle-utils')
|
||||
|
||||
local VERSION = "4.0.1"
|
||||
@ -12,8 +13,6 @@ local ADDITIONAL_FUEL_MARGIN = 100
|
||||
local MAX_FERTILIZED_ESSENCE = 32
|
||||
local MIN_FREE_SLOTS_BEFORE_COMPACT = 4
|
||||
|
||||
local FERTILIZED_ESSENCE = 'mysticalagriculture:fertilized_essence'
|
||||
|
||||
-- a table with the list of current crops
|
||||
local localPlan = nil
|
||||
|
||||
@ -81,11 +80,6 @@ local function getSeedNameFromCropName(cropName)
|
||||
return string.gsub(cropName, 'crop', 'seeds')
|
||||
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
|
||||
local function getItemSlot(inventory, itemName)
|
||||
for slot, item in pairs(inventory.list()) do
|
||||
@ -206,7 +200,7 @@ local function compactIfNeeded()
|
||||
end
|
||||
|
||||
local function applyFertilizedEssenceIfAny()
|
||||
if turtleUtils.selectItemByName(FERTILIZED_ESSENCE) then
|
||||
if turtleUtils.selectItemByName(inferium.FERTILIZED_ESSENCE) then
|
||||
while turtle.placeDown() do end
|
||||
end
|
||||
end
|
||||
@ -319,14 +313,14 @@ local function retrieveLocalPlan(config)
|
||||
goBackToHome(config)
|
||||
end
|
||||
|
||||
local function fetchRemoteConfig()
|
||||
local function fetchRemoteConfig(serverName)
|
||||
print('> fetch remote config')
|
||||
local message = { type = 'register-and-get-config' }
|
||||
|
||||
local lastErrMessage = nil
|
||||
|
||||
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
|
||||
previouslyFetchedRemoteConfig = replyMessage.payload
|
||||
print('> config fetched')
|
||||
@ -443,10 +437,10 @@ local function retrieveFertilizedEssences(_)
|
||||
local storageInventory = turtleUtils.waitForInventory('bottom', 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
|
||||
local inventoryCount = getCountItem(storageInventory, FERTILIZED_ESSENCE)
|
||||
local inventoryCount = getCountItem(storageInventory, inferium.FERTILIZED_ESSENCE)
|
||||
local realCount = math.min(MAX_FERTILIZED_ESSENCE, inventoryCount)
|
||||
|
||||
print('> retrieve ' .. tostring(realCount) .. ' fertilized essences from storage')
|
||||
@ -559,7 +553,7 @@ local function main()
|
||||
print('> Starting cycle ' .. tostring(cycleNumber))
|
||||
print()
|
||||
|
||||
local remoteConfig = fetchRemoteConfig()
|
||||
local remoteConfig = fetchRemoteConfig(inferium.SERVER)
|
||||
|
||||
dropAllProcedure(remoteConfig)
|
||||
refuelProcedure(remoteConfig)
|
||||
|
||||
@ -1,27 +1,20 @@
|
||||
local net = require('libs/net')
|
||||
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 VERSION = "2.1.0"
|
||||
local INFERIUM_SERVER = 'inferium.com'
|
||||
|
||||
local PERSISTED_CONFIGS = '/data/inferium-configs'
|
||||
local UPGRADE_SCRIPT = '/upgrade.lua'
|
||||
|
||||
local defaultConfig = inferium.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 defaultConfig = inferiumConfig.defaultConfig or error('no default config provided in config', 0)
|
||||
|
||||
local defaultAvailableSeeds = {
|
||||
formatSeedName('inferium'),
|
||||
formatSeedName('coal')
|
||||
inferium.formatSeedName('inferium'),
|
||||
inferium.formatSeedName('coal')
|
||||
}
|
||||
|
||||
-- Persistance utils
|
||||
@ -187,31 +180,6 @@ local function listHarvesters()
|
||||
return result
|
||||
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 inv = peripheral.find('inventory')
|
||||
|
||||
@ -222,7 +190,7 @@ local function listAvailableSeeds()
|
||||
|
||||
local seeds = {}
|
||||
for _, item in pairs(inv.list()) do
|
||||
if isMysticalSeed(item.name) then
|
||||
if inferium.isMysticalSeed(item.name) then
|
||||
table.insert(seeds, item.name)
|
||||
end
|
||||
end
|
||||
@ -254,7 +222,7 @@ local ROUTES = {
|
||||
net.openRednet()
|
||||
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
|
||||
print('error: malformed message received', textutils.serialize(message))
|
||||
return {}
|
||||
|
||||
@ -29,6 +29,7 @@ local LIST_LIBS_FILES = {
|
||||
'libs/utils.lua',
|
||||
'libs/turtle-utils.lua',
|
||||
'libs/robot.lua',
|
||||
'libs/inferium.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