Advertisement
dadragon84

Rules

Nov 17th, 2013 (edited)
160
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 8.10 KB | None | 0 0
  1. -- Find File and Rename
  2. local sFile = "Server_Rules"
  3. if fs.exists(sFile) then
  4.     shell.run("rename",sFile, "startup")
  5.     os.reboot()
  6. end
  7.  
  8. local serverName = nil
  9.  
  10. -- Determine where monitor is.
  11. monitorSide = nil
  12. if peripheral.isPresent("left") and peripheral.getType("left")=="monitor" then
  13.   monitorSide = "left"
  14. elseif peripheral.isPresent("right") and peripheral.getType("right")=="monitor" then
  15.   monitorSide = "right"
  16. elseif peripheral.isPresent("top") and peripheral.getType("top")=="monitor" then
  17.   monitorSide = "top"
  18. elseif peripheral.isPresent("back") and peripheral.getType("back")=="monitor" then
  19.   monitorSide = "back"
  20. elseif peripheral.isPresent("bottom") and peripheral.getType("bottom")=="monitor" then
  21.   monitorSide = "bottom"
  22. end
  23. if monitorSide ~= nil then
  24.   monitor = peripheral.wrap(monitorSide)
  25.   print("Monitor found on "..monitorSide)
  26.   monitor.setTextScale(.7)
  27. else
  28.   monitor = nil
  29. end
  30.  
  31. -- Peripheral Wraps
  32. todo = monitor
  33.  
  34. --Check for Monitors
  35. if not todo then
  36. error("No Monitor Found",0)
  37. end
  38. -- Text Scale
  39. todo.setTextScale(.5)
  40.  
  41. -- Define Tables
  42. todoData = {}
  43.  
  44. -- make directory
  45. if fs.exists("data") == false then
  46. fs.makeDir("data")
  47. end
  48. if fs.exists("data/todo") == false then
  49. file = fs.open("data/todo","w")
  50. file.close()
  51. end
  52. -- check for ServerName
  53.  
  54. local sPath = "serverName"
  55. if fs.exists(sPath) == false then
  56.     print("What is the name of your server")
  57.     sResponse = read()
  58.  
  59.     local file = fs.open( sPath, "w" )
  60.     file.write( sResponse )
  61.     file.close()
  62.     os.reboot()
  63.  else
  64.  
  65.     file = fs.open(sPath, "r")
  66.     serverName = file.readLine()
  67.    
  68.  end
  69.  
  70.  
  71. -- Check for Advanced Computer Monitors
  72. if not term.isColor() then
  73. error("This Program Must be Run on an Advanced Computer",0)
  74. end
  75. if not todo.isColor() then
  76. error("you must Have Color Monitors",0)
  77. end
  78.  
  79. -- Check Monitor Size
  80. w,h = todo.getSize()
  81. if w~= 36 or h ~= 38 then
  82. error("Monitor is not the Correct Size,\n3 High and 2 Wide is Correct Size.",0)
  83. end
  84.  
  85.  
  86.  
  87. term.clear()
  88. term.setCursorPos(1,1)
  89. print( "Downloaded as "..sFile )
  90. sleep(1)
  91.  
  92. -- Text Functions
  93. function center(mon,text,line,colour)
  94.   w,h = todo.getSize()
  95.   if mon == "todo" then
  96.     todo.setCursorPos((w-string.len(text))/2+1,line)
  97.     todo.setTextColor(colour)
  98.     todo.write(text)
  99.     todo.setTextColor(colors.white)
  100. end
  101. end
  102.  
  103. function wr(mon,x,y,text,colour)
  104.   if mon == "todo" then
  105.     todo.setCursorPos(x,y)
  106.     todo.setTextColor(colour)
  107.     todo.write(text)
  108.     todo.setTextColor(colors.white)
  109. end
  110. end
  111.  
  112. -- table functions
  113. function tableW(name,text)
  114.   if name == "todo" then
  115.     table.insert(todoData, text)
  116. end
  117. end
  118.  
  119. --File Functions
  120. function getData()
  121.   local file = fs.open("data/todo","r")
  122.   local i = 1
  123.   while true do
  124.     todoData[i] = file.readLine()
  125.     if todoData[i] == nil then
  126.       break
  127.     else
  128.       i = i + 1            
  129.     end
  130.   end
  131.   file.close()
  132. end
  133. function saveData()
  134.   local file = fs.open("data/todo","w")
  135.   for i = 1, #todoData do
  136.     file.writeLine(todoData[i])
  137.   end
  138.   file.close()
  139. end
  140.  
  141. -- Monitors
  142. function todoMon()
  143.   todo.clear()
  144.   local nameServer = serverName .. " Rules"
  145.   center("todo",nameServer,1,colors.red)
  146.   posx = 1
  147.   posy = 3
  148.   for i=1, #todoData do
  149.     wr("todo",posx,posy,tostring(i),colors.lime)
  150.     posx = posx + 2
  151.     wr("todo",posx,posy,todoData[i],colors.white)
  152.     posx = posx - 2
  153.     posy = posy + 2
  154.   end
  155. end
  156.  
  157. -- Table functions
  158. function clear(name)
  159.   if name == "todo" then
  160.     for i=1, #todoData do
  161.       table.remove(todoData)
  162.     end
  163.   end
  164. end
  165.  
  166. -- GUI Things
  167. -- Main Menu
  168. local menu_options = {
  169.   [1] = {text="List", color = colors.lightBlue},
  170.   [2] = {text=serverName, color = colors.lightBlue}
  171. }
  172.  
  173. -- List Menu
  174. local todo_options = {
  175. [1] = {text="Add", color = colors.lime},
  176. [2] = {text="Delete", color = colors.magenta}
  177. }
  178. -- Find Terminal X Y
  179. local termX, termY = term.getSize()
  180.  
  181. -- Draw Main Menu
  182. function mainMenuDraw(selected)
  183.   local yPos = termY/2 - #menu_options/2
  184.   for index, data in pairs(menu_options) do
  185.     menu_options[index].bounds = {
  186.       x1 = termX/2 - (#data.text+4)/2,
  187.       x2 = termX/2 + (#data.text+4)/2,
  188.       y = yPos
  189.     }
  190.     term.setTextColor(data.color)
  191.     term.setCursorPos(data.bounds.x1, data.bounds.y)
  192.    
  193.     local text =
  194.       index==selected and "[ "..data.text.." ]" or
  195.       "  "..data.text.."  "
  196.     term.write(text)
  197.     yPos = yPos + 2
  198.   end
  199. end                      
  200.  
  201. -- Draw ToDo Menu
  202. function todoMenuDraw(selected)
  203.   local yPos = termY/2 - #todo_options/2
  204.   for index, data in pairs(todo_options) do
  205.     todo_options[index].bounds  = {
  206.       x = termX/2 - (#data.text+4)/2,
  207.       y = yPos
  208.     }
  209.     term.setTextColor(data.color)
  210.     term.setCursorPos(data.bounds.x,data.bounds.y)
  211.      
  212.     local text =
  213.       index == selected and "[ "..data.text.." ]" or
  214.       "  "..data.text.."  "
  215.      
  216.     term.write(text)
  217.     yPos = yPos + 2                                              
  218.   end    
  219. end
  220.  
  221. -- Check click Position
  222. function checkClick(x,y)
  223. for index, data in pairs(menu_options) do
  224. if x >= data.bounds.x1 and x <= data.bounds.x2 and y == data.bounds.y then
  225. return index
  226. end
  227. end
  228. return false
  229. end
  230.  
  231. --Display Main Menu
  232. function mainMenu()
  233.   todoMon()
  234.    term.clear()
  235.   term.setCursorPos(termX/2-14,1)
  236.   term.setTextColor(colors.magenta)
  237.   term.write(serverName)
  238.  
  239.  
  240.   local selector = 1
  241.   while true do
  242.     mainMenuDraw(selector)
  243.     local e = {os.pullEvent()}
  244.     if e[1] == "key" then
  245.       if e[2] == keys.down then
  246.         selector = selector < #menu_options and selector+1 or 1
  247.       elseif e[2] == keys.up then
  248.         selector = selector > 1 and selector-1 or #menu_options
  249.       elseif e[2] == keys.enter then
  250.         break  
  251.       end            
  252.     end    
  253.   end  
  254.  
  255.   term.clear()
  256.   if selector == 1 then
  257.     todoMenu()
  258.    end  
  259. end
  260.  
  261.  
  262. -- Display Menu 2
  263. function todoMenu()
  264. todoMon()
  265.  
  266. term.clear()
  267.  
  268. term.setCursorPos(termX/2-5,1)
  269. term.setTextColor(colors.magenta)
  270. term.write("Rules")
  271.  
  272. term.setCursorPos(termX/2-19 ,2)
  273. term.setTextColor(colors.green)
  274. term.write("Press Backspace to Go Back to the Main Menu")
  275.  
  276. local selector = 1
  277. while true do
  278. todoMenuDraw(selector)
  279. local e = {os.pullEvent()}
  280. if e[1] == "key" then
  281. if e[2] == keys.down then
  282. selector = selector < #todo_options and selector+1 or 1
  283. elseif e[2] == keys.up then
  284. slector = selector > 1 and selector-1 or #todo_options
  285. elseif e[2] == keys.backspace then
  286. mainMenu()
  287. elseif e[2] == keys.enter then
  288. break
  289. end
  290. end
  291. end
  292.  
  293. term.clear()
  294. term.setCursorPos(1,1)
  295. if selector == 1 then
  296. addTaskMenu()
  297. elseif selector == 2 then
  298. deleteTaskMenu()
  299. end
  300. end
  301.  
  302. --Add Task Menu
  303. function addTaskMenu()
  304.   term.clear()
  305.   term.setCursorPos(termX/2-13 ,1)
  306.   term.setTextColor(colors.white)
  307.   term.write("Type Task Then Press Enter")
  308.  
  309.   term.setCursorPos(2,3)
  310.   term.setTextColor(colors.lime)
  311.   term.write("> ")
  312.  
  313.   term.setTextColor(colors.white)
  314.   local task = read()
  315.  
  316.   if string.len(task) > 32 then
  317.     term.clear()
  318.     term.setCursorPos(termX/2-10,1)
  319.     term.setTextColor(colors.red)
  320.     term.write("Error: Task too long!")
  321.     sleep(2)
  322.     todoMenu()
  323.   elseif #todoData == 18 then
  324.     term.clear()
  325.     term.setCursorPos(termX/2-11,1)
  326.     term.setTextColor(colors.red)
  327.     term.write("Error: ToDo List Full!")
  328.     sleep(2)
  329.     todoMenu()    
  330.   else  
  331.     tableW("todo", task)
  332.     saveData()
  333.     todoMenu()
  334.   end  
  335. end
  336.  
  337. -- Delete Task Menu
  338. function deleteTaskMenu()
  339.   term.clear()
  340.   term.setCursorPos(termX/2-14 ,1)
  341.   term.setTextColor(colors.white)
  342.   term.write("Type Number of Task To Delete")
  343.   term.setCursorPos(termX/2-11 ,2)
  344.   term.write("Or Type 'all' to clear")
  345.   term.setCursorPos(2,3)
  346.   term.setTextColor(colors.lime)
  347.   term.write("> ")
  348.   term.setTextColor(colors.white)
  349.   number = read()
  350.   if number == "all" then
  351.     clear("todo")
  352.   elseif tonumber(number) == nil then
  353.     term.clear()
  354.     term.setCursorPos(termX/2-10 ,1)
  355.     term.setTextColor(colors.red)
  356.     term.write("Error: Not A Number!")
  357.     sleep(2)
  358.   else  
  359.     table.remove(todoData, number)
  360.   end
  361.   saveData()
  362.   todoMenu()
  363. end  
  364.  
  365. getData()
  366. mainMenu()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement