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 turtleUtils = require('libs/turtle-utils')
|
||||
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 WAIT_ITEM_IDLE_TIME = 5
|
||||
local NETWORK_TIMEOUT = 4
|
||||
local MIN_FUEL_NEEDED = (100 + config.firstCropZ + config.length) * 2
|
||||
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
|
||||
local localPlan = nil
|
||||
|
||||
local previouslyFetchedRemotePlan = nil
|
||||
|
||||
-- UTILS
|
||||
|
||||
local function removeFirst(t, x)
|
||||
@ -73,7 +76,7 @@ local function getSeedNameFromCropName(cropName)
|
||||
return string.gsub(cropName, 'crop', 'seeds')
|
||||
end
|
||||
|
||||
function isSeed(item)
|
||||
local function isSeed(item)
|
||||
local tags = item and item.tags or {}
|
||||
return tags['forge:seeds'] or tags['mysticalagriculture:seeds'] or false
|
||||
end
|
||||
@ -263,37 +266,21 @@ local function retrieveLocalPlan()
|
||||
goBackToHome()
|
||||
end
|
||||
|
||||
-- TODO: fetch retry + timeout ?
|
||||
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() } }
|
||||
modem.transmit(INFERIUM_QUERY_PORT, INFERIUM_REPLY_PORT, textutils.serialize(message))
|
||||
|
||||
local channel, replyRawMessage
|
||||
repeat
|
||||
_, _, channel, _, replyRawMessage = os.pullEvent("modem_message")
|
||||
until channel == INFERIUM_REPLY_PORT
|
||||
|
||||
modem.close(INFERIUM_REPLY_PORT)
|
||||
|
||||
if not replyRawMessage then
|
||||
error('empty response from the server')
|
||||
end
|
||||
|
||||
local replyMessage = textutils.unserialize(replyRawMessage)
|
||||
while true do
|
||||
local replyMessage, _ = net.sendQuery(INFERIUM_SERVER, message, NETWORK_TIMEOUT)
|
||||
|
||||
if replyMessage and replyMessage.payload then
|
||||
previouslyFetchedRemotePlan = replyMessage.payload
|
||||
return replyMessage.payload
|
||||
elseif previouslyFetchedRemotePlan then
|
||||
return previouslyFetchedRemotePlan
|
||||
end
|
||||
|
||||
error('message received but there is no payload: ' .. replyRawMessage)
|
||||
os.sleep(IDLE_TIME)
|
||||
end
|
||||
end
|
||||
|
||||
local function removeSeeds(seeds)
|
||||
@ -434,6 +421,8 @@ end
|
||||
|
||||
-- Main procedure
|
||||
local function main()
|
||||
net.openRednet()
|
||||
|
||||
print("Starting Trap's inferium harvester v" .. VERSION)
|
||||
|
||||
turtleUtils.refuelAllFromInventory()
|
||||
@ -454,6 +443,8 @@ local function main()
|
||||
harvestProcedure()
|
||||
goBackToHome()
|
||||
end
|
||||
|
||||
net.closeRednet()
|
||||
end
|
||||
|
||||
main()
|
||||
@ -1,27 +1,19 @@
|
||||
local net = require('libs/net')
|
||||
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 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 defaultPlan = inferiumPlans.default or error('no default plan provided in config', 0)
|
||||
|
||||
local function getPlanForComputer(computerId)
|
||||
return inferiumPlans[tostring(computerId)]
|
||||
end
|
||||
|
||||
local function messageReceived(message)
|
||||
if message.type == 'getplan' then
|
||||
local payload = message.payload or {}
|
||||
local computerId = payload.computerId
|
||||
|
||||
local function getPlan(computerId)
|
||||
if not computerId == nil then
|
||||
print('getplan error: no computerId found in received payload')
|
||||
print('getplan error: no computerId found')
|
||||
return nil
|
||||
end
|
||||
|
||||
@ -37,22 +29,44 @@ local function messageReceived(message)
|
||||
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
|
||||
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 .. ' listening on port ' .. INFERIUM_QUERY_PORT)
|
||||
|
||||
while true do
|
||||
local _, _, channel, replyChannel, rawMessage, _ = os.pullEvent("modem_message")
|
||||
net.listenQuery(INFERIUM_SERVER, function(message, computerId, stopServer)
|
||||
if type(message) ~= table then
|
||||
print('error: malformed message received', textutils.serialize(message))
|
||||
return {}
|
||||
end
|
||||
|
||||
if channel == INFERIUM_QUERY_PORT and replyChannel == INFERIUM_REPLY_PORT then
|
||||
local message = textutils.unserialize(rawMessage)
|
||||
local replyMessage = messageReceived(message)
|
||||
local router = ROUTES[message.type]
|
||||
|
||||
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
|
||||
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