96 lines
2.4 KiB
Lua
96 lines
2.4 KiB
Lua
local net = require('libs/net')
|
|
local inferium = require('config/inferium')
|
|
|
|
local VERSION = "1.1.1"
|
|
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 savePersistedConfigs = function(configs)
|
|
local file = fs.open(PERSISTED_CONFIGS, 'w')
|
|
|
|
if not file then
|
|
error('savePersistedConfigs: cannot open .minerstate file!')
|
|
end
|
|
|
|
file.write(textutils.serialize(configs))
|
|
file.close()
|
|
end
|
|
|
|
local readPersistedConfigs = function()
|
|
local file = fs.open(PERSISTED_CONFIGS, 'r')
|
|
|
|
if not file then
|
|
local initialConfig = {}
|
|
savePersistedConfigs(initialConfig)
|
|
return initialConfig
|
|
end
|
|
|
|
local serializedConfigs = file.readAll()
|
|
file.close()
|
|
|
|
return textutils.unserialize(serializedConfigs)
|
|
end
|
|
|
|
local LOCAL_CONFIGS = readPersistedConfigs()
|
|
|
|
local function getConfigForComputer(computerId)
|
|
return LOCAL_CONFIGS[tostring(computerId)]
|
|
end
|
|
|
|
local function setConfigForComputer(computerId, config)
|
|
LOCAL_CONFIGS[tostring(computerId)] = config
|
|
savePersistedConfigs(LOCAL_CONFIGS)
|
|
end
|
|
|
|
local function getConfig(computerId)
|
|
if not computerId == nil then
|
|
print('getconfig error: no computerId found')
|
|
return nil
|
|
end
|
|
|
|
local config = getConfigForComputer(computerId)
|
|
|
|
if not config then
|
|
print('new harvester detected: ' .. tostring(computerId))
|
|
setConfigForComputer(computerId, defaultConfig)
|
|
config = defaultConfig
|
|
end
|
|
|
|
return {
|
|
type = "getconfig/response",
|
|
payload = config
|
|
}
|
|
end
|
|
|
|
local ROUTES = {
|
|
['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,
|
|
}
|
|
|
|
net.openRednet()
|
|
print('> inferium server v' .. VERSION .. ' started')
|
|
|
|
net.listenQuery(INFERIUM_SERVER, function(message, computerId, stopServer)
|
|
if type(message) ~= 'table' then
|
|
print('error: malformed message received', textutils.serialize(message))
|
|
return {}
|
|
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') |