diff --git a/libs/robot.lua b/libs/robot.lua index 70afb15..faa4739 100644 --- a/libs/robot.lua +++ b/libs/robot.lua @@ -4,6 +4,20 @@ local api = {} local ROBOT_DEFAULT_STATE_FILE = '/.robotstate' +local DEFAULT_CONFIG = { + autosave = false, + autoload = false, + stateFilePath = ROBOT_DEFAULT_STATE_FILE +} + +local function getConfig(givenConfig) + return { + autosave = givenConfig.autosave or DEFAULT_CONFIG.autosave, + autoload = givenConfig.autoload or DEFAULT_CONFIG.autoload, + stateFilePath = givenConfig.stateFilePath or DEFAULT_CONFIG.stateFilePath + } +end + local function createDefaultState() return { y = 0, @@ -14,9 +28,62 @@ local function createDefaultState() } end -api.create = function(state, defaultStateFile) - defaultStateFile = defaultStateFile or ROBOT_DEFAULT_STATE_FILE +local saveRobotState = function(stateFile, state) + stateFile = stateFile or ROBOT_DEFAULT_STATE_FILE state = state or createDefaultState() + local file = fs.open(stateFile, 'w') + + if not file then + error('saveRobotState: cannot open ' .. stateFile .. ' file!') + end + + file.write(textutils.serializeJSON(state)) + file.close() + + return state +end + +local loadRobotState = function(stateFile) + stateFile = stateFile or ROBOT_DEFAULT_STATE_FILE + local file = fs.open(stateFile, 'r') + + if not file then + return robot.saveRobotState(stateFile) + end + + local serializedRobotState = file.readAll() + file.close() + + local parsedResult = textutils.unserializeJSON(serializedRobotState) + + if not parsedResult then + return robot.saveRobotState(stateFile) + end + + return parsedResult +end + +local getAutoSave = function(config, state) + local autoSave = function() end + + if config.autosave then + autoSave = function() + saveRobotState(config.stateFilePath, state) + end + end + + return autoSave +end + +api.create = function(state, givenConfig) + state = state or createDefaultState() + local config = getConfig(givenConfig) + + if config.autoload then + state = loadRobotState(config.stateFilePath) + end + + local autoSave = getAutoSave(config, state) local mutateRobotPosition = function(isBackward) local incValue = 1 @@ -40,6 +107,7 @@ api.create = function(state, defaultStateFile) if ok then mutateRobotPosition(false) + autoSave() end return ok, err @@ -50,6 +118,7 @@ api.create = function(state, defaultStateFile) if ok then mutateRobotPosition(true) + autoSave() end return ok, err @@ -61,6 +130,7 @@ api.create = function(state, defaultStateFile) if ok then state.y = state.y + 1 + autoSave() end return ok, err @@ -70,7 +140,8 @@ api.create = function(state, defaultStateFile) local ok, err = turtle.down() if ok then - state.y = state.y - 1 + state.y = state.y - 1 + autoSave() end return ok, err @@ -89,6 +160,8 @@ api.create = function(state, defaultStateFile) elseif state.dir == 'BACKWARD' then state.dir = 'RIGHT' end + + autoSave() end return ok, err @@ -107,6 +180,8 @@ api.create = function(state, defaultStateFile) elseif state.dir == 'BACKWARD' then state.dir = 'LEFT' end + + autoSave() end return ok, err @@ -116,38 +191,12 @@ api.create = function(state, defaultStateFile) return state end - robot.saveRobotState = function(stateFile) - stateFile = stateFile or defaultStateFile - local file = fs.open(stateFile, 'w') - - if not file then - error('saveRobotState: cannot open ' .. stateFile .. ' file!') - end - - file.write(textutils.serializeJSON(state)) - file.close() - - return state + robot.saveState = function() + return saveRobotState(config.stateFilePath, state) end - robot.loadRobotState = function(stateFile) - stateFile = stateFile or defaultStateFile - local file = fs.open(stateFile, 'r') - - if not file then - return robot.saveRobotState(stateFile) - end - - local serializedRobotState = file.readAll() - file.close() - - local parsedResult = textutils.unserializeJSON(serializedRobotState) - - if not parsedResult then - return robot.saveRobotState(stateFile) - end - - return parsedResult + robot.loadState = function(stateFile) + return loadRobotState(config.stateFilePath) end return robot