From 24ffcce68d813f508ac0312b7e86f9565218f25f Mon Sep 17 00:00:00 2001 From: Guillaume ARM Date: Wed, 22 May 2024 19:31:53 +0200 Subject: [PATCH] feat(inferium-server): configs are persisted --- inferium-server.lua | 34 ++++++++++++++++++++++++++++++---- 1 file changed, 30 insertions(+), 4 deletions(-) diff --git a/inferium-server.lua b/inferium-server.lua index 9756f71..3be923a 100644 --- a/inferium-server.lua +++ b/inferium-server.lua @@ -1,14 +1,39 @@ 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 VERSION = "1.0.0" -local defaultConfig= inferium.defaultConfig or error('no default config provided in config', 0) +local defaultConfig = inferium.defaultConfig or error('no default config provided in config', 0) --- TODO: persistance -local LOCAL_CONFIGS = {} +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)] @@ -16,6 +41,7 @@ end local function setConfigForComputer(computerId, config) LOCAL_CONFIGS[tostring(computerId)] = config + savePersistedConfigs(LOCAL_CONFIGS) end local function getConfig(computerId)