Guest User

tdmp

a guest
Aug 24th, 2016
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 12.30 KB | None | 0 0
  1. -- Rob's ToDo List and Message board
  2.  
  3. -- NB: I realise my code is very messy
  4. --     But im not 100% sure what im doing!
  5.  
  6.  
  7.  
  8.  
  9.  
  10.  
  11. --Peripheral Wraps
  12. todo = peripheral.wrap("right")
  13. mes = peripheral.wrap("left")
  14.  
  15. --Check For Monitors
  16. if not todo then
  17.   error("No Monitor on Right Side",0)
  18. end
  19. if not mes then
  20.   error("No Monitor on Left Side",0)
  21. end    
  22.  
  23. -- Text Scale
  24. todo.setTextScale(.5)
  25. mes.setTextScale(.5)
  26.  
  27. --Define Tables
  28. todoData = {}
  29. mesData = {}
  30.  
  31. --Make Directory and Files
  32. if fs.exists("data") == false then
  33.   fs.makeDir("data")
  34. end
  35. if fs.exists("data/todo") == false then  
  36.   file = fs.open("data/todo","w")
  37.   file.close()
  38. end
  39. if fs.exists("data/mes") == false then  
  40.   file = fs.open("data/mes","w")
  41.   file.close()
  42. end  
  43.  
  44. --Check for Advanced Computers & Monitors
  45. if not term.isColor() then
  46.   error("This Program Must Be Run on an Advanced Computer",0)
  47. end
  48. if not todo.isColor() or not mes.isColor() then
  49.   error("You Must Have Color Monitors on Either Side",0)
  50. end
  51.  
  52. --Check Monitor Size
  53. w,h = mes.getSize()
  54. if w ~= 36 or h ~= 38 then
  55.   error("Left Monitor Not Correct Size,\n3 High and 2 Wide is the Correct Size.", 0)
  56. end
  57. w,h = todo.getSize()
  58. if w ~= 36 or h ~= 38 then
  59.   error("Right Monitor Not Correct Size,\n3 High and 2 Wide is Correct Size.",0)
  60. end  
  61.  
  62.  
  63. -- Text Functions
  64. function center(mon,text,line,colour)
  65.   w,h = todo.getSize()
  66.   if mon == "todo" then
  67.     todo.setCursorPos((w-string.len(text))/2+1,line)
  68.     todo.setTextColor(colour)
  69.     todo.write(text)
  70.     todo.setTextColor(colors.white)
  71.   else
  72.     mes.setCursorPos((w-string.len(text))/2+1,line)
  73.     mes.setTextColor(colour)
  74.     mes.write(text)
  75.     mes.setTextColor(colors.white)
  76.   end
  77. end
  78.  
  79. function wr(mon,x,y,text,colour)
  80.   if mon == "todo" then
  81.     todo.setCursorPos(x,y)
  82.     todo.setTextColor(colour)
  83.     todo.write(text)
  84.     todo.setTextColor(colors.white)
  85.   else
  86.     mes.setCursorPos(x,y)
  87.     mes.setTextColor(colour)
  88.     mes.write(text)
  89.     mes.setTextColor(colors.white)
  90.   end
  91. end
  92.  
  93. -- table functions
  94. function tableW(name,text)
  95.   if name == "todo" then
  96.     table.insert(todoData, text)
  97.   else
  98.     table.insert(mesData,text)
  99.   end
  100. end
  101.  
  102. --File Functions
  103. function getData()
  104.   local file = fs.open("data/todo","r")
  105.   local i = 1
  106.   while true do
  107.     todoData[i] = file.readLine()
  108.     if todoData[i] == nil then
  109.       break
  110.     else
  111.       i = i + 1            
  112.     end
  113.   end
  114.   file.close()
  115.  
  116.   local file = fs.open("data/mes","r")
  117.   for line in file.readLine do
  118.     local location = (line:find(":",1))
  119.     if location then
  120.       local usr = line:sub(1, location - 1)
  121.       local msg = line:sub(location + 1)
  122.       table.insert(mesData, {user = usr, message = msg})
  123.     end
  124.   end    
  125.  
  126.   file.close()
  127. end
  128.  
  129. function saveData()
  130.   local file = fs.open("data/todo","w")
  131.   for i = 1, #todoData do
  132.     file.writeLine(todoData[i])
  133.   end
  134.   file.close()
  135.    
  136.   local file = fs.open("data/mes","w")
  137.     for index, data in pairs(mesData) do      
  138.       file.writeLine(data.user..":"..data.message)
  139.     end  
  140.   file.close()
  141. end    
  142.                                
  143. -- Monitors
  144. function todoMon()
  145.   todo.clear()
  146.   center("todo","To-Do List",1,colors.magenta)
  147.   posx = 1
  148.   posy = 3
  149.   for i=1, #todoData do
  150.     wr("todo",posx,posy,tostring(i),colors.lime)
  151.     posx = posx + 2
  152.     wr("todo",posx,posy,todoData[i],colors.white)
  153.     posx = posx - 2
  154.     posy = posy + 2
  155.   end
  156. end
  157.  
  158. function mesMon()
  159.   mes.clear()
  160.   center("mes","Message Board",1,colors.magenta)
  161.   pX = 2
  162.   pY = 3
  163.   for i=1, #mesData do
  164.     wr("mes",pX,pY,tostring(i),colors.lime)
  165.     pX = pX + 3
  166.     wr("mes",pX,pY,mesData[i].user..":",colors.lightBlue)
  167.     pY = pY + 1
  168.     wr("mes",pX,pY,mesData[i].message,colors.white)
  169.     pX = pX - 3
  170.     pY = pY + 2
  171.   end
  172. end
  173.  
  174. -- Table functions
  175. function clear(name)
  176.   if name == "todo" then
  177.     for i=1, #todoData do
  178.       table.remove(todoData)
  179.     end
  180.   end
  181.   if name == "mes" then
  182.     for i=1, #mesData do
  183.       table.remove(mesData)    
  184.     end
  185.   end
  186. end
  187.  
  188.  
  189. -- Gui Things
  190. -- Main Menu Table
  191. local menu_options = {
  192.   [1] = {text="To-Do List", color = colors.lightBlue},
  193.   [2] = {text="Message Board", color = colors.lightBlue}
  194. }
  195.  
  196. --ToDo Menu Table
  197. local todo_options = {
  198.   [1] = {text="Add Task", color = colors.lime},
  199.   [2] = {text="Delete Task", color = colors.red}
  200. }
  201.  
  202. --Mes Menu Table
  203. local mes_options = {
  204.   [1] = {text="Add Message", color = colors.lime},
  205.   [2] = {text="Delete  Message", color = colors.red}
  206. }
  207. -- Find Terminal X Y
  208. local termX, termY = term.getSize()
  209.  
  210. -- Draw Main Menu
  211. function mainMenuDraw(selected)
  212.   local yPos = termY/2 - #menu_options/2
  213.   for index, data in pairs(menu_options) do
  214.     menu_options[index].bounds = {
  215.       x1 = termX/2 - (#data.text+4)/2,
  216.       x2 = termX/2 + (#data.text+4)/2,
  217.       y = yPos
  218.     }
  219.     term.setTextColor(data.color)
  220.     term.setCursorPos(data.bounds.x1, data.bounds.y)
  221.    
  222.     local text =
  223.       index==selected and "[ "..data.text.." ]" or
  224.       "  "..data.text.."  "
  225.     term.write(text)
  226.     yPos = yPos + 2
  227.   end
  228. end                    
  229.    
  230. -- Draw ToDo Menu
  231. function todoMenuDraw(selected)
  232.   local yPos = termY/2 - #todo_options/2
  233.   for index, data in pairs(todo_options) do
  234.     todo_options[index].bounds  = {
  235.       x = termX/2 - (#data.text+4)/2,
  236.       y = yPos
  237.     }
  238.     term.setTextColor(data.color)
  239.     term.setCursorPos(data.bounds.x,data.bounds.y)
  240.      
  241.     local text =
  242.       index == selected and "[ "..data.text.." ]" or
  243.       "  "..data.text.."  "
  244.      
  245.     term.write(text)
  246.     yPos = yPos + 2                                              
  247.   end    
  248. end
  249.  
  250. -- Draw Message Menu
  251. function mesMenuDraw(selected)
  252.   local yPos = termY/2 - #mes_options/2
  253.   for index, data in pairs(mes_options) do
  254.     mes_options[index].bounds  = {
  255.       x = termX/2 - (#data.text+4)/2,
  256.       y = yPos
  257.     }
  258.     term.setTextColor(data.color)
  259.     term.setCursorPos(data.bounds.x,data.bounds.y)
  260.      
  261.     local text =
  262.       index == selected and "[ "..data.text.." ]" or
  263.       "  "..data.text.."  "
  264.      
  265.     term.write(text)
  266.     yPos = yPos + 2                                              
  267.   end    
  268. end
  269.      
  270. -- Check Click Position                
  271. function checkClick(x,y)
  272.   for index, data in pairs(menu_options) do
  273.     if x >= data.bounds.x1 and x <= data.bounds.x2 and y == data.bounds.y then
  274.       return index
  275.     end
  276.   end
  277.   return false
  278. end      
  279.  
  280. --Display Main Menu
  281. function mainMenu()
  282.   todoMon()
  283.   mesMon()
  284.   term.clear()
  285.   term.setCursorPos(termX/2-14,1)
  286.   term.setTextColor(colors.magenta)
  287.   term.write("Message Board and To-Do List")
  288.  
  289.  
  290.   local selector = 1
  291.   while true do
  292.     mainMenuDraw(selector)
  293.     local e = {os.pullEvent()}
  294.     if e[1] == "key" then
  295.       if e[2] == keys.down then
  296.         selector = selector < #menu_options and selector+1 or 1
  297.       elseif e[2] == keys.up then
  298.         selector = selector > 1 and selector-1 or #menu_options
  299.       elseif e[2] == keys.enter then
  300.         break  
  301.       end            
  302.     end    
  303.   end  
  304.  
  305.   term.clear()
  306.   if selector == 1 then
  307.     todoMenu()
  308.   elseif selector == 2 then
  309.     mesMenu()
  310.   end  
  311. end
  312.  
  313. --Display ToDo Menu
  314. function todoMenu()
  315.   todoMon()
  316.   mesMon()
  317.  
  318.   term.clear()
  319.  
  320.   term.setCursorPos(termX/2-5,1)
  321.   term.setTextColor(colors.magenta)
  322.   term.write("To-Do List")
  323.  
  324.   term.setCursorPos(termX/2-19 ,2)
  325.   term.setTextColor(colors.white)
  326.   term.write("Press Backspace To Go Back To Main Menu")
  327.  
  328.   local selector = 1
  329.   while true do
  330.     todoMenuDraw(selector)
  331.     local e = {os.pullEvent()}
  332.     if e[1] == "key" then
  333.       if e[2] == keys.down then
  334.         selector = selector < #todo_options and selector+1 or 1
  335.       elseif e[2] == keys.up then
  336.         selector = selector > 1 and selector-1 or #todo_options
  337.       elseif e[2] == keys.backspace then
  338.         mainMenu()
  339.       elseif e[2] == keys.enter then
  340.         break
  341.       end
  342.     end
  343.   end
  344.  
  345.   term.clear()
  346.   term.setCursorPos(1,1)
  347.   if selector == 1 then
  348.     addTaskMenu()
  349.   elseif selector == 2 then
  350.     deleteTaskMenu()
  351.   end
  352. end
  353.  
  354. --Display Message Menu
  355. function mesMenu()
  356.   todoMon()
  357.   mesMon()
  358.  
  359.   term.clear()
  360.  
  361.   term.setCursorPos(termX/2-6,1)
  362.   term.setTextColor(colors.magenta)
  363.   term.write("Message Board")
  364.  
  365.   term.setCursorPos(termX/2-19 ,2)
  366.   term.setTextColor(colors.white)
  367.   term.write("Press Backspace To Go Back To Main Menu")
  368.  
  369.   local selector = 1
  370.   while true do
  371.     mesMenuDraw(selector)
  372.     local e = {os.pullEvent()}
  373.     if e[1] == "key" then
  374.       if e[2] == keys.down then
  375.         selector = selector < #mes_options and selector+1 or 1
  376.       elseif e[2] == keys.up then
  377.         selector = selector > 1 and selector-1 or #mes_options
  378.       elseif e[2] == keys.backspace then
  379.         mainMenu()
  380.       elseif e[2] == keys.enter then
  381.         break
  382.       end
  383.     end
  384.   end
  385.  
  386.   term.clear()
  387.   term.setCursorPos(1,1)
  388.   if selector == 1 then
  389.     addMesMenu()
  390.   elseif selector == 2 then
  391.     deleteMesMenu()
  392.   end
  393. end
  394.  
  395. --Add Task Menu
  396. function addTaskMenu()
  397.   term.clear()
  398.   term.setCursorPos(termX/2-13 ,1)
  399.   term.setTextColor(colors.white)
  400.   term.write("Type Task Then Press Enter")
  401.  
  402.   term.setCursorPos(2,3)
  403.   term.setTextColor(colors.lime)
  404.   term.write("> ")
  405.  
  406.   term.setTextColor(colors.white)
  407.   local task = read()
  408.  
  409.   if string.len(task) > 32 then
  410.     term.clear()
  411.     term.setCursorPos(termX/2-10,1)
  412.     term.setTextColor(colors.red)
  413.     term.write("Error: Task too long!")
  414.     sleep(2)
  415.     todoMenu()
  416.   elseif #todoData == 18 then
  417.     term.clear()
  418.     term.setCursorPos(termX/2-11,1)
  419.     term.setTextColor(colors.red)
  420.     term.write("Error: ToDo List Full!")
  421.     sleep(2)
  422.     todoMenu()    
  423.   else  
  424.     tableW("todo", task)
  425.     saveData()
  426.     todoMenu()
  427.   end  
  428. end
  429.  
  430. -- Delete Task Menu
  431. function deleteTaskMenu()
  432.   term.clear()
  433.   term.setCursorPos(termX/2-14 ,1)
  434.   term.setTextColor(colors.white)
  435.   term.write("Type Number of Task To Delete")
  436.   term.setCursorPos(termX/2-11 ,2)
  437.   term.write("Or Type 'all' to clear")
  438.   term.setCursorPos(2,3)
  439.   term.setTextColor(colors.lime)
  440.   term.write("> ")
  441.   term.setTextColor(colors.white)
  442.   number = read()
  443.   if number == "all" then
  444.     clear("todo")
  445.   elseif tonumber(number) == nil then
  446.     term.clear()
  447.     term.setCursorPos(termX/2-10 ,1)
  448.     term.setTextColor(colors.red)
  449.     term.write("Error: Not A Number!")
  450.     sleep(2)
  451.   else  
  452.     table.remove(todoData, number)
  453.   end
  454.   saveData()
  455.   todoMenu()
  456. end  
  457.  
  458. -- Add Message Menu
  459. function addMesMenu()
  460.   term.clear()
  461.   term.setCursorPos(termX/2-11 ,1)
  462.   term.setTextColor(colors.white)
  463.   term.write("Please Type Your Name:")
  464.   term.setCursorPos(2,3)
  465.   term.setTextColor(colors.lime)
  466.   term.write("> ")
  467.   term.setTextColor(colors.white)
  468.   local username = read()
  469.  
  470.   term.clear()
  471.   term.setCursorPos(termX/2-14 ,1)
  472.   term.setTextColor(colors.white)
  473.   term.write("Type Message Then Press Enter")
  474.   term.setCursorPos(2,3)
  475.   term.setTextColor(colors.lime)
  476.   term.write("> ")
  477.   term.setTextColor(colors.white)
  478.   local mes = read()
  479.  
  480.   if string.len(mes) > 32 then
  481.     term.clear()
  482.     term.setCursorPos(termX/2-10,1)
  483.     term.setTextColor(colors.red)
  484.     term.write("Error: Message Too Long!")
  485.     sleep(2)
  486.     mesMenu()
  487.   elseif #mesData == 12 then
  488.     term.clear()
  489.     term.setCursorPos(termX/2-13 ,1)
  490.     term.setTextColor(colors.red)
  491.     term.write("Error: Message Board Full!" )
  492.     sleep(2)
  493.     mesMenu()
  494.   else
  495.     n = #mesData + 1  
  496.     mesData[n] = {
  497.       user = username,
  498.       message = mes
  499.     }  
  500.      
  501.     saveData()
  502.     mesMenu()
  503.   end  
  504. end
  505.  
  506. -- Delete Message Menu
  507. function deleteMesMenu()
  508.   term.clear()
  509.   term.setCursorPos(termX/2-16 ,1)
  510.   term.setTextColor(colors.white)
  511.   term.write("Type Number of Message To Delete")
  512.   term.setCursorPos(termX/2-11 ,2)
  513.   term.write("Or Type 'all' To Clear")
  514.   term.setCursorPos(2,3)
  515.   term.setTextColor(colors.lime)
  516.   term.write("> ")
  517.   term.setTextColor(colors.white)
  518.   number = read()
  519.   if number == "all" then
  520.     clear("mes")
  521.   elseif tonumber(number) == nil then
  522.     term.clear()
  523.     term.setCursorPos(termX/2-10 ,1)
  524.     term.setTextColor(colors.red)
  525.     term.write("Error: Not a Number!")
  526.     sleep(2)
  527.   else  
  528.     table.remove(mesData, number)
  529.   end  
  530.   saveData()
  531.   mesMenu()
  532. end  
  533.  
  534. getData()
  535. mainMenu()
Add Comment
Please, Sign In to add comment