feat(robot): add config with autoload/autosave props
This commit is contained in:
parent
4062c87f48
commit
f6ab6d6f77
115
libs/robot.lua
115
libs/robot.lua
@ -4,6 +4,20 @@ local api = {}
|
|||||||
|
|
||||||
local ROBOT_DEFAULT_STATE_FILE = '/.robotstate'
|
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()
|
local function createDefaultState()
|
||||||
return {
|
return {
|
||||||
y = 0,
|
y = 0,
|
||||||
@ -14,9 +28,62 @@ local function createDefaultState()
|
|||||||
}
|
}
|
||||||
end
|
end
|
||||||
|
|
||||||
api.create = function(state, defaultStateFile)
|
local saveRobotState = function(stateFile, state)
|
||||||
defaultStateFile = defaultStateFile or ROBOT_DEFAULT_STATE_FILE
|
stateFile = stateFile or ROBOT_DEFAULT_STATE_FILE
|
||||||
state = state or createDefaultState()
|
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 mutateRobotPosition = function(isBackward)
|
||||||
local incValue = 1
|
local incValue = 1
|
||||||
@ -40,6 +107,7 @@ api.create = function(state, defaultStateFile)
|
|||||||
|
|
||||||
if ok then
|
if ok then
|
||||||
mutateRobotPosition(false)
|
mutateRobotPosition(false)
|
||||||
|
autoSave()
|
||||||
end
|
end
|
||||||
|
|
||||||
return ok, err
|
return ok, err
|
||||||
@ -50,6 +118,7 @@ api.create = function(state, defaultStateFile)
|
|||||||
|
|
||||||
if ok then
|
if ok then
|
||||||
mutateRobotPosition(true)
|
mutateRobotPosition(true)
|
||||||
|
autoSave()
|
||||||
end
|
end
|
||||||
|
|
||||||
return ok, err
|
return ok, err
|
||||||
@ -61,6 +130,7 @@ api.create = function(state, defaultStateFile)
|
|||||||
|
|
||||||
if ok then
|
if ok then
|
||||||
state.y = state.y + 1
|
state.y = state.y + 1
|
||||||
|
autoSave()
|
||||||
end
|
end
|
||||||
|
|
||||||
return ok, err
|
return ok, err
|
||||||
@ -70,7 +140,8 @@ api.create = function(state, defaultStateFile)
|
|||||||
local ok, err = turtle.down()
|
local ok, err = turtle.down()
|
||||||
|
|
||||||
if ok then
|
if ok then
|
||||||
state.y = state.y - 1
|
state.y = state.y - 1
|
||||||
|
autoSave()
|
||||||
end
|
end
|
||||||
|
|
||||||
return ok, err
|
return ok, err
|
||||||
@ -89,6 +160,8 @@ api.create = function(state, defaultStateFile)
|
|||||||
elseif state.dir == 'BACKWARD' then
|
elseif state.dir == 'BACKWARD' then
|
||||||
state.dir = 'RIGHT'
|
state.dir = 'RIGHT'
|
||||||
end
|
end
|
||||||
|
|
||||||
|
autoSave()
|
||||||
end
|
end
|
||||||
|
|
||||||
return ok, err
|
return ok, err
|
||||||
@ -107,6 +180,8 @@ api.create = function(state, defaultStateFile)
|
|||||||
elseif state.dir == 'BACKWARD' then
|
elseif state.dir == 'BACKWARD' then
|
||||||
state.dir = 'LEFT'
|
state.dir = 'LEFT'
|
||||||
end
|
end
|
||||||
|
|
||||||
|
autoSave()
|
||||||
end
|
end
|
||||||
|
|
||||||
return ok, err
|
return ok, err
|
||||||
@ -116,38 +191,12 @@ api.create = function(state, defaultStateFile)
|
|||||||
return state
|
return state
|
||||||
end
|
end
|
||||||
|
|
||||||
robot.saveRobotState = function(stateFile)
|
robot.saveState = function()
|
||||||
stateFile = stateFile or defaultStateFile
|
return saveRobotState(config.stateFilePath, state)
|
||||||
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
|
end
|
||||||
|
|
||||||
robot.loadRobotState = function(stateFile)
|
robot.loadState = function(stateFile)
|
||||||
stateFile = stateFile or defaultStateFile
|
return loadRobotState(config.stateFilePath)
|
||||||
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
|
end
|
||||||
|
|
||||||
return robot
|
return robot
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user