From 90a3ac864e373ef60f4374e4df536f6a18aa4ff1 Mon Sep 17 00:00:00 2001 From: Guillaume ARM Date: Wed, 22 May 2024 18:40:00 +0200 Subject: [PATCH] feat(inferium): replace getplan by getconfig --- config/harvesting.lua | 5 ---- config/inferium-plans.lua | 16 ------------- config/inferium.lua | 21 +++++++++++++++++ inferium-harvester.lua | 45 +++++++++++++++++++----------------- inferium-server.lua | 48 ++++++++++++++++++--------------------- install.lua | 46 +++++++++++++++++++++---------------- 6 files changed, 93 insertions(+), 88 deletions(-) delete mode 100644 config/harvesting.lua delete mode 100644 config/inferium-plans.lua create mode 100644 config/inferium.lua diff --git a/config/harvesting.lua b/config/harvesting.lua deleted file mode 100644 index 598ea2b..0000000 --- a/config/harvesting.lua +++ /dev/null @@ -1,5 +0,0 @@ -return { - fertilizedBoost = false, - length = 8, - firstCropZ = 2 -} diff --git a/config/inferium-plans.lua b/config/inferium-plans.lua deleted file mode 100644 index d2b834b..0000000 --- a/config/inferium-plans.lua +++ /dev/null @@ -1,16 +0,0 @@ -local function mystical(essenceName) - return 'mysticalagriculture:' .. essenceName .. '_seeds' -end - -return { - ['default'] = { - mystical('coal'), - mystical('coal'), - mystical('inferium'), - mystical('inferium'), - mystical('inferium'), - mystical('inferium'), - mystical('inferium'), - mystical('inferium') - } -} diff --git a/config/inferium.lua b/config/inferium.lua new file mode 100644 index 0000000..9577534 --- /dev/null +++ b/config/inferium.lua @@ -0,0 +1,21 @@ +local function mystical(essenceName) + return 'mysticalagriculture:' .. essenceName .. '_seeds' +end + +return { + defaultConfig = { + plan = { + mystical('coal'), + mystical('coal'), + mystical('inferium'), + mystical('inferium'), + mystical('inferium'), + mystical('inferium'), + mystical('inferium'), + mystical('inferium') + }, + fertilizedBoost = false, + length = 8, + firstCropZ = 2 + } +} diff --git a/inferium-harvester.lua b/inferium-harvester.lua index c4a7d1b..aabd894 100644 --- a/inferium-harvester.lua +++ b/inferium-harvester.lua @@ -1,9 +1,8 @@ local net = require('libs/net') local utils = require('libs/utils') local turtleUtils = require('libs/turtle-utils') -local LOCAL_CONFIG = require('config/harvesting') -local VERSION = "2.6.1" +local VERSION = "3.0.0" local INFERIUM_SERVER = 'inferium.com' local IDLE_TIME = 2 local WAIT_ITEM_IDLE_TIME = 5 @@ -18,7 +17,7 @@ local FERTILIZED_ESSENCE = 'mysticalagriculture:fertilized_essence' -- a table with the list of current crops local localPlan = nil -local previouslyFetchedRemotePlan = nil +local previouslyFetchedRemoteConfig = nil -- UTILS @@ -311,28 +310,32 @@ local function retrieveLocalPlan(config) goBackToHome(config) end -local function fetchRemotePlan() - print('> fetch remote plan') - local message = { type = 'getplan' } +local function fetchRemoteConfig() + print('> fetch remote config') + local message = { type = 'getconfig' } local lastErrMessage = nil while true do local replyMessage, errMessage = net.sendQuery(INFERIUM_SERVER, message, NETWORK_TIMEOUT) if replyMessage and replyMessage.payload then - previouslyFetchedRemotePlan = replyMessage.payload - print('> plan fetched') + previouslyFetchedRemoteConfig = replyMessage.payload + print('> config fetched') local payload = replyMessage.payload if not payload then - error('cannot fetch the remote plan') + error('cannot fetch the remote config') + end + + if not payload.plan then + error('no "plan" property found in remote config') end return payload - elseif previouslyFetchedRemotePlan then + elseif previouslyFetchedRemoteConfig then print('> failed to fetch: ' .. tostring(errMessage)) - print('> use the previous recorded remote plan instead') + print('> use the previous recorded remote config instead') - return previouslyFetchedRemotePlan + return previouslyFetchedRemoteConfig elseif not replyMessage and errMessage then if lastErrMessage ~= errMessage then lastErrMessage = errMessage @@ -484,7 +487,9 @@ local function replantSeeds(config) goBackToHome(config) end -local function replantProcedure(remotePlan, config) +local function replantProcedure(config) + local remotePlan = config.plan + if localPlan == nil then retrieveLocalPlan(config) end @@ -538,21 +543,19 @@ local function main() print('> Starting cycle ' .. tostring(cycleNumber)) print() - local remotePlan = fetchRemotePlan() - local config = LOCAL_CONFIG + local remoteConfig = fetchRemoteConfig() - dropAllProcedure(config) + dropAllProcedure(remoteConfig) refuelProcedure() - -- TODO: change parameters to be just config - replantProcedure(remotePlan, config) + replantProcedure(remoteConfig) - if config.fertilizedBoost then + if remoteConfig.fertilizedBoost then print('> fertilized boost enabled') - retrieveFertilizedEssences(config) + retrieveFertilizedEssences(remoteConfig) end - harvestProcedure(config) + harvestProcedure(remoteConfig) cycleNumber = cycleNumber + 1 end diff --git a/inferium-server.lua b/inferium-server.lua index 0713a92..9756f71 100644 --- a/inferium-server.lua +++ b/inferium-server.lua @@ -1,53 +1,49 @@ local net = require('libs/net') -local inferiumPlans = require('config/inferium-plans') -- temporary default plan +local inferium = require('config/inferium') local INFERIUM_SERVER = 'inferium.com' local UPGRADE_SCRIPT = '/upgrade.lua' -local VERSION = "0.5.2" +local VERSION = "1.0.0" -local defaultPlan = inferiumPlans.default or error('no default plan provided in config', 0) +local defaultConfig= inferium.defaultConfig or error('no default config provided in config', 0) -local function getPlanForComputer(computerId) - return inferiumPlans[tostring(computerId)] +-- TODO: persistance +local LOCAL_CONFIGS = {} + +local function getConfigForComputer(computerId) + return LOCAL_CONFIGS[tostring(computerId)] end -local function getPlan(computerId) +local function setConfigForComputer(computerId, config) + LOCAL_CONFIGS[tostring(computerId)] = config +end + +local function getConfig(computerId) if not computerId == nil then - print('getplan error: no computerId found') + print('getconfig error: no computerId found') return nil end - local plan = getPlanForComputer(computerId) + local config = getConfigForComputer(computerId) - if not plan then - print('getplan warning: no plan found for computerID ' .. tostring(computerId)) - plan = defaultPlan + if not config then + print('getconfig warning: no plan found for computerID ' .. tostring(computerId)) + setConfigForComputer(computerId, defaultConfig) + config = defaultConfig end return { - type = "getplan/response", - payload = plan + type = "getconfig/response", + payload = config } end local ROUTES = { - ['getplan'] = function(_, computerId) return getPlan(computerId) end, + ['getconfig'] = function(_, computerId) return getConfig(computerId) end, ['exit-server'] = function(_, _, stopServer) stopServer() ; return true end, ['upgrade-server'] = function() shell.execute(UPGRADE_SCRIPT) ; return true end, } -local function handleMessage(message, computerId) - if message.type == 'getplan' then - return getPlan(computerId) - elseif message.type == 'exit-server' then - stopServer() - return true - elseif message.type == 'upgrade-server' then - shell.execute('/upgrade.lua') - return true - end -end - net.openRednet() print('> inferium server v' .. VERSION .. ' started') diff --git a/install.lua b/install.lua index 9c8fd23..0521a50 100644 --- a/install.lua +++ b/install.lua @@ -17,18 +17,40 @@ local LIST_CONFIG_FILES = { 'startup.lua', 'upgrade.lua', 'config/mining.lua', - 'config/harvesting.lua', - 'config/inferium-plans.lua' + 'config/inferium.lua' } -- old files that need to be cleaned up -local LIST_OLD_FILES = {} +local LIST_OLD_FILES = { + 'config/inferium-plans.lua', + 'config/harvesting.lua' +} local REPO_PREFIX = 'https://git.trapcloud.fr/guillaumearm/minecraft-cc-tools/raw/branch/master/' local removeFiles = function(list) for _, filePath in pairs(list) do - fs.delete(filePath) + if filePath then + fs.delete(filePath) + end + end +end + +local installFiles = function(list) + for _, filePath in pairs(list) do + if filePath then + fs.delete(filePath) + shell.execute('wget', REPO_PREFIX .. filePath, filePath) + end + end +end + +local installConfig = function() + -- do not override existing config files + for _, filePath in pairs(LIST_CONFIG_FILES) do + if filePath and not fs.exists(filePath) then + shell.execute('wget', REPO_PREFIX .. filePath, filePath) + end end end @@ -39,22 +61,6 @@ local prepareDirs = function() fs.makeDir('/data') end -local installFiles = function(list) - for _, filePath in pairs(list) do - fs.delete(filePath) - shell.execute('wget', REPO_PREFIX .. filePath, filePath) - end -end - -local installConfig = function() - -- do not override existing config files - for _, filePath in pairs(LIST_CONFIG_FILES) do - if not fs.exists(filePath) then - shell.execute('wget', REPO_PREFIX .. filePath, filePath) - end - end -end - local mainSetup = function() local previousDir = shell.dir()