Advertisement
Blackhome

test TaskWork

Jun 5th, 2025 (edited)
174
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.36 KB | None | 0 0
  1. local DISK_NAMES = {
  2.     incoming = "Incoming Task",
  3.     user = "User Task",
  4.     outgoing = "Outgoing Task",
  5.     finished = "Finished Task",
  6.     settings = "Settings",
  7.     storage = "Data Storage",
  8.     init = "Turtle Initializing"
  9. }
  10.  
  11. local TASK_FILE = "task_list.txt"
  12.  
  13. function getDiskByLabel(label)
  14.     for _, side in ipairs(peripheral.getNames()) do
  15.         if peripheral.getType(side) == "drive" then
  16.             local disk = peripheral.wrap(side)
  17.             if disk.getDiskLabel() == label then
  18.                 return disk, disk.getMountPath()
  19.             end
  20.         end
  21.     end
  22.     return nil, nil
  23. end
  24.  
  25. function getTaskList(path)
  26.     if not fs.exists(path) then return {} end
  27.     local file = fs.open(path, "r")
  28.     local data = textutils.unserialize(file.readAll())
  29.     file.close()
  30.     return data or {}
  31. end
  32.  
  33. function saveTaskList(path, taskList)
  34.     local file = fs.open(path, "w")
  35.     file.write(textutils.serialize(taskList))
  36.     file.close()
  37. end
  38.  
  39. function addTaskToList(path, task)
  40.     local tasks = getTaskList(path)
  41.     table.insert(tasks, task)
  42.     saveTaskList(path, tasks)
  43. end
  44.  
  45. function getNextTask()
  46.     local disk, path = getDiskByLabel(DISK_NAMES.outgoing)
  47.     local outgoingPath = fs.combine(path, TASK_FILE)
  48.  
  49.     local taskList = getTaskList(outgoingPath)
  50.     if #taskList == 0 then return false end
  51.  
  52.     local task = taskList[1]
  53.     local index = 1
  54.  
  55.     print("Get task " .. task.taskType .. " with ID " .. tostring(task.taskId))
  56.     -- add local
  57.     addTaskToList(TASK_FILE, task)
  58.    
  59.     -- delete outgoing task
  60.     table.remove(taskList, index)
  61.     saveTaskList(outgoingPath, taskList)
  62.  
  63.     return true
  64. end
  65.  
  66. function finishTask()
  67.     local localTaskList = getTaskList(TASK_FILE)
  68.  
  69.     local task = localTaskList[1]
  70.     local index = 1
  71.  
  72.     print("Finish task " .. task.taskType .. " with ID " .. tostring(task.taskId))
  73.    
  74.     -- add finished Task
  75.     local disk, path = getDiskByLabel(DISK_NAMES.finished)
  76.     local finishedPath = fs.combine(path, TASK_FILE)
  77.    
  78.     addTaskToList(finishedPath, task)
  79.  
  80.     -- delete local
  81.     table.remove(localTaskList, index)
  82.     saveTaskList(TASK_FILE, taskList)
  83. end
  84.  
  85. while true do
  86.     print("Press Enter to take a task")
  87.     read()
  88.     if getNextTask() then
  89.         print("Press Enter to finish the task")
  90.         read()
  91.         finishTask()
  92.     end
  93. end
  94.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement