Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local DISK_NAMES = {
- incoming = "Incoming Task",
- user = "User Task",
- outgoing = "Outgoing Task",
- finished = "Finished Task",
- settings = "Settings",
- storage = "Data Storage",
- init = "Turtle Initializing"
- }
- local TASK_FILE = "task_list.txt"
- function getDiskByLabel(label)
- for _, side in ipairs(peripheral.getNames()) do
- if peripheral.getType(side) == "drive" then
- local disk = peripheral.wrap(side)
- if disk.getDiskLabel() == label then
- return disk, disk.getMountPath()
- end
- end
- end
- return nil, nil
- end
- function getTaskList(path)
- if not fs.exists(path) then return {} end
- local file = fs.open(path, "r")
- local data = textutils.unserialize(file.readAll())
- file.close()
- return data or {}
- end
- function saveTaskList(path, taskList)
- local file = fs.open(path, "w")
- file.write(textutils.serialize(taskList))
- file.close()
- end
- function addTaskToList(path, task)
- local tasks = getTaskList(path)
- table.insert(tasks, task)
- saveTaskList(path, tasks)
- end
- function getNextTask()
- local disk, path = getDiskByLabel(DISK_NAMES.outgoing)
- local outgoingPath = fs.combine(path, TASK_FILE)
- local taskList = getTaskList(outgoingPath)
- if #taskList == 0 then return false end
- local task = taskList[1]
- local index = 1
- print("Get task " .. task.taskType .. " with ID " .. tostring(task.taskId))
- -- add local
- addTaskToList(TASK_FILE, task)
- -- delete outgoing task
- table.remove(taskList, index)
- saveTaskList(outgoingPath, taskList)
- return true
- end
- function finishTask()
- local localTaskList = getTaskList(TASK_FILE)
- local task = localTaskList[1]
- local index = 1
- print("Finish task " .. task.taskType .. " with ID " .. tostring(task.taskId))
- -- add finished Task
- local disk, path = getDiskByLabel(DISK_NAMES.finished)
- local finishedPath = fs.combine(path, TASK_FILE)
- addTaskToList(finishedPath, task)
- -- delete local
- table.remove(localTaskList, index)
- saveTaskList(TASK_FILE, taskList)
- end
- while true do
- print("Press Enter to take a task")
- read()
- if getNextTask() then
- print("Press Enter to finish the task")
- read()
- finishTask()
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement