SHARE
TWEET

Lua test

NerdyDragon42 Feb 28th, 2020 73 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. -- Tasks Api File
  2. -- Writes data to two files, as specified in complete and incomplete
  3.  
  4. incomplete = "disk/incomplete.table"
  5. complete = "disk/complete.table"
  6.  
  7. local function save(tab, filename)
  8.     local file = fs.open(filename,"w")
  9.     file.write(textutils.serialize(tab))
  10.     file.close()
  11. end
  12.  
  13. local function retrieve(filename)
  14.     local file = fs.open(filename,"r")
  15.     local data = file.readAll()
  16.     file.close()
  17.     return textutils.unserialize(data)
  18. end
  19.  
  20. function reset()
  21.   save({}, incomplete)
  22.   save({}, complete)
  23. end
  24.  
  25. function addTask(task)
  26.     local data = retrieve(incomplete)
  27.     local id = #data + 1
  28.    
  29.     data[id] = {["Task"] = task, ["Assigned"] = "Unassigned",}
  30.     save(data, incomplete)
  31.     return id
  32. end
  33.  
  34. function getTaskInfo(taskId)
  35.     local data = retrieve(incomplete)
  36.     return data[taskId]
  37. end
  38.  
  39. function assignTask(taskId, person)
  40.     local data = retrieve(incomplete)
  41.     data[taskId]["Assigned"] = person
  42.     save(data, incomplete)
  43.     return true
  44. end
  45.  
  46. function completeTask(taskId)
  47.     local data = retrieve(incomplete)
  48.     local comp = retrieve(complete)
  49.    
  50.     data[id] = {["Task"] = task, ["Assigned"] = "Unassigned",}
  51.     table.remove(data, taskId)
  52.     save(data, incomplete)
  53.     return true
  54. end
  55.  
  56. function getIncomplete()
  57.     return retrieve(incomplete)
  58. end
  59. function getComplete()
  60.     return retrieve(complete)
  61. end
  62.  
  63. function printTasks(tasks)
  64.     for key,value in pairs(tasks) do
  65.         print(key .. ") " .. value["Task"] .. "     Assigned to: " .. value["Assigned"])
  66.     end
  67. end
  68.  
  69. function printCompletedTasks(tasks)
  70.     for key,value in pairs(tasks) do
  71.         print(key .. ") " .. value["Task"] .. "     Assigned to: " .. value["Assigned"])
  72.     end
  73. end
  74.  
  75. function update()
  76.     local monitor = peripheral.wrap("right")
  77.  
  78.     monitor.clear()
  79.  
  80.     monitor.setCursorPos(1,1)
  81.  
  82.     monitor.write("All Incomplete Tasks:")
  83.  
  84.     monitor.setCursorPos(1,3)
  85.  
  86.     for key,value in pairs(getIncomplete()) do
  87.       x,y = monitor.getCursorPos()
  88.       monitor.write(key .. ") " .. value["Task"])
  89.       monitor.setCursorPos(35,y)
  90.       monitor.write("Assigned: " .. value["Assigned"])
  91.       monitor.setCursorPos(1,y+1)
  92.     end
  93. end
  94.  
  95. -- Commands Section - DO NOT IMPORT WHOLE
  96.  
  97. -- addtask (taskname)
  98.  
  99. os.loadAPI("tasks")
  100.  
  101. args = {...}
  102. taskName = args[1]
  103.  
  104. if #args > 1 then
  105.   for i=2,#args do
  106.     taskName = taskName .. " " .. args[i]
  107.   end
  108. end
  109.  
  110. taskId = tasks.addTask(taskName)
  111.  
  112. print("Task Successfully Added with an ID of " .. taskId)
  113. tasks.update()
  114.  
  115. -- completetask (taskid)
  116.  
  117. os.loadAPI("tasks")
  118.  
  119. args = {...}
  120. taskId = tonumber(args[1])
  121.  
  122. tasks.completeTask(taskId)
  123.  
  124. print("Marked Task " .. taskId .. " as Complete")
  125. tasks.update()
  126.  
  127. -- assigntask (taskid) (person)
  128.  
  129. os.loadAPI("tasks")
  130.  
  131. args = {...}
  132.  
  133. taskId = tonumber(args[1])
  134. person = args[2]
  135.  
  136. tasks.assignTask(taskId, person)
  137.  
  138. print("Task " .. taskId .. " has been assigned to " .. person)
  139. tasks.update()
  140.  
  141. -- loadincomplete
  142.  
  143. os.loadAPI("tasks")
  144.  
  145. local monitor = peripheral.wrap("right")
  146.  
  147. monitor.clear()
  148.  
  149. monitor.setCursorPos(1,1)
  150.  
  151. monitor.write("All Incomplete Tasks:")
  152.  
  153. monitor.setCursorPos(1,3)
  154.  
  155. for key,value in pairs(tasks.getIncomplete()) do
  156.   x,y = monitor.getCursorPos()
  157.   monitor.write(key .. ") " .. value["Task"])
  158.   monitor.setCursorPos(35,y)
  159.   monitor.write("Assigned: " .. value["Assigned"])
  160.   monitor.setCursorPos(1,y+1)
  161. end
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Top