feat! migrate inferium-harvester and inferium-server to rednet
This commit is contained in:
parent
cfea9f575f
commit
9dd106763e
@ -1,18 +1,21 @@
|
|||||||
|
local net = require('libs/net')
|
||||||
local utils = require('libs/utils')
|
local utils = require('libs/utils')
|
||||||
local turtleUtils = require('libs/turtle-utils')
|
local turtleUtils = require('libs/turtle-utils')
|
||||||
local config = require('config/harvesting')
|
local config = require('config/harvesting')
|
||||||
|
|
||||||
local VERSION = "1.0.1"
|
local VERSION = "2.0.0"
|
||||||
|
local INFERIUM_SERVER = 'inferium.com'
|
||||||
local IDLE_TIME = 2
|
local IDLE_TIME = 2
|
||||||
local WAIT_ITEM_IDLE_TIME = 5
|
local WAIT_ITEM_IDLE_TIME = 5
|
||||||
|
local NETWORK_TIMEOUT = 4
|
||||||
local MIN_FUEL_NEEDED = (100 + config.firstCropZ + config.length) * 2
|
local MIN_FUEL_NEEDED = (100 + config.firstCropZ + config.length) * 2
|
||||||
local MIN_FREE_SLOTS_BEFORE_COMPACT = 4
|
local MIN_FREE_SLOTS_BEFORE_COMPACT = 4
|
||||||
local INFERIUM_QUERY_PORT = 111
|
|
||||||
local INFERIUM_REPLY_PORT = 112
|
|
||||||
|
|
||||||
-- a table with the list of current crops
|
-- a table with the list of current crops
|
||||||
local localPlan = nil
|
local localPlan = nil
|
||||||
|
|
||||||
|
local previouslyFetchedRemotePlan = nil
|
||||||
|
|
||||||
-- UTILS
|
-- UTILS
|
||||||
|
|
||||||
local function removeFirst(t, x)
|
local function removeFirst(t, x)
|
||||||
@ -73,7 +76,7 @@ local function getSeedNameFromCropName(cropName)
|
|||||||
return string.gsub(cropName, 'crop', 'seeds')
|
return string.gsub(cropName, 'crop', 'seeds')
|
||||||
end
|
end
|
||||||
|
|
||||||
function isSeed(item)
|
local function isSeed(item)
|
||||||
local tags = item and item.tags or {}
|
local tags = item and item.tags or {}
|
||||||
return tags['forge:seeds'] or tags['mysticalagriculture:seeds'] or false
|
return tags['forge:seeds'] or tags['mysticalagriculture:seeds'] or false
|
||||||
end
|
end
|
||||||
@ -263,37 +266,21 @@ local function retrieveLocalPlan()
|
|||||||
goBackToHome()
|
goBackToHome()
|
||||||
end
|
end
|
||||||
|
|
||||||
-- TODO: fetch retry + timeout ?
|
|
||||||
local function fetchRemotePlan()
|
local function fetchRemotePlan()
|
||||||
local modem = peripheral.find("modem")
|
|
||||||
|
|
||||||
if not modem then
|
|
||||||
error('no modem found')
|
|
||||||
end
|
|
||||||
|
|
||||||
modem.open(INFERIUM_REPLY_PORT)
|
|
||||||
|
|
||||||
local message = { type = 'getplan', payload = { computerId = os.getComputerID() } }
|
local message = { type = 'getplan', payload = { computerId = os.getComputerID() } }
|
||||||
modem.transmit(INFERIUM_QUERY_PORT, INFERIUM_REPLY_PORT, textutils.serialize(message))
|
|
||||||
|
|
||||||
local channel, replyRawMessage
|
while true do
|
||||||
repeat
|
local replyMessage, _ = net.sendQuery(INFERIUM_SERVER, message, NETWORK_TIMEOUT)
|
||||||
_, _, channel, _, replyRawMessage = os.pullEvent("modem_message")
|
|
||||||
until channel == INFERIUM_REPLY_PORT
|
|
||||||
|
|
||||||
modem.close(INFERIUM_REPLY_PORT)
|
if replyMessage and replyMessage.payload then
|
||||||
|
previouslyFetchedRemotePlan = replyMessage.payload
|
||||||
|
return replyMessage.payload
|
||||||
|
elseif previouslyFetchedRemotePlan then
|
||||||
|
return previouslyFetchedRemotePlan
|
||||||
|
end
|
||||||
|
|
||||||
if not replyRawMessage then
|
os.sleep(IDLE_TIME)
|
||||||
error('empty response from the server')
|
|
||||||
end
|
end
|
||||||
|
|
||||||
local replyMessage = textutils.unserialize(replyRawMessage)
|
|
||||||
|
|
||||||
if replyMessage and replyMessage.payload then
|
|
||||||
return replyMessage.payload
|
|
||||||
end
|
|
||||||
|
|
||||||
error('message received but there is no payload: ' .. replyRawMessage)
|
|
||||||
end
|
end
|
||||||
|
|
||||||
local function removeSeeds(seeds)
|
local function removeSeeds(seeds)
|
||||||
@ -434,6 +421,8 @@ end
|
|||||||
|
|
||||||
-- Main procedure
|
-- Main procedure
|
||||||
local function main()
|
local function main()
|
||||||
|
net.openRednet()
|
||||||
|
|
||||||
print("Starting Trap's inferium harvester v" .. VERSION)
|
print("Starting Trap's inferium harvester v" .. VERSION)
|
||||||
|
|
||||||
turtleUtils.refuelAllFromInventory()
|
turtleUtils.refuelAllFromInventory()
|
||||||
@ -454,6 +443,8 @@ local function main()
|
|||||||
harvestProcedure()
|
harvestProcedure()
|
||||||
goBackToHome()
|
goBackToHome()
|
||||||
end
|
end
|
||||||
|
|
||||||
|
net.closeRednet()
|
||||||
end
|
end
|
||||||
|
|
||||||
main()
|
main()
|
||||||
@ -1,58 +1,72 @@
|
|||||||
|
local net = require('libs/net')
|
||||||
local inferiumPlans = require('config/inferium-plans') -- temporary default plan
|
local inferiumPlans = require('config/inferium-plans') -- temporary default plan
|
||||||
|
|
||||||
local VERSION = "0.3.0"
|
local INFERIUM_SERVER = 'inferium.com'
|
||||||
|
local UPGRADE_SCRIPT = '/upgrade.lua'
|
||||||
|
local VERSION = "0.5.0"
|
||||||
|
|
||||||
local INFERIUM_QUERY_PORT = 111
|
local defaultPlan = inferiumPlans.default or error('no default plan provided in config', 0)
|
||||||
local INFERIUM_REPLY_PORT = 112
|
|
||||||
|
|
||||||
local defaultPlan = inferiumPlans.default or error('no default plan provided in config')
|
|
||||||
|
|
||||||
local modem = peripheral.find("modem") or error("No modem attached", 0)
|
|
||||||
|
|
||||||
modem.open(INFERIUM_QUERY_PORT)
|
|
||||||
|
|
||||||
local function getPlanForComputer(computerId)
|
local function getPlanForComputer(computerId)
|
||||||
return inferiumPlans[tostring(computerId)]
|
return inferiumPlans[tostring(computerId)]
|
||||||
end
|
end
|
||||||
|
|
||||||
local function messageReceived(message)
|
local function getPlan(computerId)
|
||||||
|
if not computerId == nil then
|
||||||
|
print('getplan error: no computerId found')
|
||||||
|
return nil
|
||||||
|
end
|
||||||
|
|
||||||
|
local plan = getPlanForComputer(computerId)
|
||||||
|
|
||||||
|
if not plan then
|
||||||
|
print('getplan warning: no plan found for computerID ' .. tostring(computerId))
|
||||||
|
plan = defaultPlan
|
||||||
|
end
|
||||||
|
|
||||||
|
return {
|
||||||
|
type = "getplan/response",
|
||||||
|
payload = plan
|
||||||
|
}
|
||||||
|
end
|
||||||
|
|
||||||
|
local ROUTES = {
|
||||||
|
['getplan'] = function(_, computerId) return getPlan(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
|
if message.type == 'getplan' then
|
||||||
local payload = message.payload or {}
|
return getPlan(computerId)
|
||||||
local computerId = payload.computerId
|
elseif message.type == 'exit-server' then
|
||||||
|
stopServer()
|
||||||
if not computerId == nil then
|
return true
|
||||||
print('getplan error: no computerId found in received payload')
|
elseif message.type == 'upgrade-server' then
|
||||||
return nil
|
shell.execute('/upgrade.lua')
|
||||||
end
|
return true
|
||||||
|
|
||||||
local plan = getPlanForComputer(computerId)
|
|
||||||
|
|
||||||
if not plan then
|
|
||||||
print('getplan warning: no plan found for computerID ' .. tostring(computerId))
|
|
||||||
plan = defaultPlan
|
|
||||||
end
|
|
||||||
|
|
||||||
return {
|
|
||||||
type = "getplan/response",
|
|
||||||
payload = plan
|
|
||||||
}
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
net.openRednet()
|
||||||
print('> inferium server v' .. VERSION .. ' listening on port ' .. INFERIUM_QUERY_PORT)
|
print('> inferium server v' .. VERSION .. ' listening on port ' .. INFERIUM_QUERY_PORT)
|
||||||
|
|
||||||
while true do
|
net.listenQuery(INFERIUM_SERVER, function(message, computerId, stopServer)
|
||||||
local _, _, channel, replyChannel, rawMessage, _ = os.pullEvent("modem_message")
|
if type(message) ~= table then
|
||||||
|
print('error: malformed message received', textutils.serialize(message))
|
||||||
if channel == INFERIUM_QUERY_PORT and replyChannel == INFERIUM_REPLY_PORT then
|
return {}
|
||||||
local message = textutils.unserialize(rawMessage)
|
|
||||||
local replyMessage = messageReceived(message)
|
|
||||||
|
|
||||||
if replyMessage then
|
|
||||||
local rawReplyMessage = textutils.serialize(replyMessage)
|
|
||||||
modem.transmit(INFERIUM_REPLY_PORT, INFERIUM_QUERY_PORT, rawReplyMessage)
|
|
||||||
else
|
|
||||||
print('warning: unknown message received')
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
end
|
|
||||||
|
local router = ROUTES[message.type]
|
||||||
|
|
||||||
|
if not router then
|
||||||
|
print('warning: unknown type of message received', message.type)
|
||||||
|
return {}
|
||||||
|
end
|
||||||
|
|
||||||
|
return router(message, computerId, stopServer)
|
||||||
|
end)
|
||||||
|
print('> server stopped')
|
||||||
|
|
||||||
|
net.closeRednet()
|
||||||
|
print('> rednet closed')
|
||||||
Loading…
Reference in New Issue
Block a user