local net = require('libs/net') local inferium = require('config/inferium') local VERSION = "1.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 readPersistedConfigs = function() local file = fs.open(PERSISTED_CONFIGS, 'r') if not file then return nil end local serializedConfigs = file.readAll() file.close() return textutils.unserialize(serializedConfigs) end 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 LOCAL_CONFIGS = readPersistedConfigs() or {} 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('getconfig warning: no plan found for computerID ' .. 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')