Firestorm002

Voting Server Program v2.0

Aug 13th, 2013
69
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. os.pullEvent = os.pullEventRaw
  2. --[[Voting System Program]]-- Coded By:Firestorm002
  3. ---------------------------------------------------
  4. --                 [Side Note]
  5. -- Do not touch any of the code which isn't labeled
  6. --"change this!", you might mess up the program.
  7. --You can change it if you know what you are doing.
  8. --Just maked sure to give me credit. - Fire
  9. ---------------------------------------------------
  10. --Peripherals--
  11. lmon = peripheral.wrap("left")
  12. rmon = peripheral.wrap("right")
  13. ---------------------------------------------
  14. --Startup Scripts--
  15. if fs.exists("votingdata") == false then
  16.   fs.makeDir("votingdata")
  17.   a = fs.open("votingdata/filea","w")
  18.   a.write("0")
  19.   b = fs.open("votingdata/fileb","w")
  20.   b.write("0")
  21.   c = fs.open("votingdata/titlea","w")
  22.   c.write("title1")
  23.   c.close()
  24.   c = fs.open("votingdata/titlea","r")
  25.   d = fs.open("votingdata/titleb","w")
  26.   d.write("title2")
  27.   d.close()
  28.   d = fs.open("votingdata/titleb","r")
  29.   a.close()
  30.   b.close()
  31. else
  32.   c = fs.open("votingdata/titlea","r")
  33.   d = fs.open("votingdata/titleb","r")
  34. end
  35.  
  36. if fs.exists("votinguserdata") == false then
  37.   fs.makeDir("votinguserdata")
  38. end
  39. --------------------------------------------
  40. --Variables--
  41. filename1 = "votingdata/filea"
  42. filename2 = "votingdata/fileb"
  43. w,h = term.getSize()
  44. wml,hml = lmon.getSize()
  45. wmr,hmr = rmon.getSize()
  46. title = c.readAll()
  47. title2 = d.readAll()
  48. --------------------------------------------|
  49. --[Change this pass to whatever you like]---|
  50. overridepass = "baconbits"
  51. --------------------------------------------|
  52. txtscale = 1
  53. version = "2.0"
  54. modemside = "top" --(top,left,bottom,right,back)
  55. h = fs.open(filename1,"r")
  56. t = fs.open(filename2,"r")
  57. msga1 = h.readAll()
  58. msga2 = t.readAll()
  59. --Closes fs.open()'s--
  60. h.close()
  61. t.close()
  62. --
  63. ---------------------------------------------
  64. --[[Functions]]--
  65. --Monitor Functions--
  66. function clear()
  67.   lmon.clear()
  68.   rmon.clear()
  69.   rmon.setCursorPos(1,1)
  70.   lmon.setCursorPos(1,1)
  71. end
  72.  
  73. function lmCenter(str,ypos)
  74.   lmon.setCursorPos(wml/2 - #str/2,ypos)
  75.   lmon.write(str)
  76. end
  77.  
  78. function rmCenter(str,ypos)
  79.   rmon.setCursorPos(wmr/2 - #str/2,ypos)
  80.   rmon.write(str)
  81. end
  82.  
  83. function fillScr(ypos)
  84.   lmon.setTextScale(txtscale)
  85.   rmon.setTextScale(txtscale)
  86.   lmon.setCursorPos(1,ypos)
  87.   lmon.write(string.rep(" ",wml))
  88.   rmon.setCursorPos(1,ypos)
  89.   rmon.write(string.rep(" ",wmr))
  90. end
  91.  
  92. function fillScrs()
  93.   fillScr(1)
  94.   fillScr(2)
  95.   fillScr(3)
  96.   fillScr(4)
  97.   fillScr(5)
  98.   fillScr(6)
  99.   fillScr(7)
  100.   fillScr(8)
  101.   fillScr(9)
  102.   fillScr(10)
  103.   fillScr(11)
  104.   fillScr(12)
  105. end
  106.  
  107. function mUpdate()
  108.   clear()
  109.   --Titles--
  110.   rmon.setTextColor(colors.lime)
  111.   lmon.setTextColor(colors.lime)
  112.   rmCenter(title2,1)
  113.   lmCenter(title,1)
  114.   --Dividing Lines--
  115.   rmon.setCursorPos(1,2)
  116.   rmon.write(string.rep("-",w))
  117.   lmon.setCursorPos(1,2)
  118.   lmon.write(string.rep("-",w))
  119.   --Voting Data--
  120.   rmon.setTextColor(colors.cyan)
  121.   lmon.setTextColor(colors.cyan)
  122.   lmon.setCursorPos(7,6)
  123.   rmon.setCursorPos(7,6)
  124.   lmon.write(msga1)
  125.   rmon.write(msga2)
  126. end
  127.  
  128. ----------------------
  129. --Terminal Functions--
  130. function tClear()
  131.   term.clear()
  132. end
  133.  
  134. function tWrite(str,xpos,ypos)
  135.   term.setCursorPos(xpos,ypos)
  136.   term.write(str)
  137. end
  138.  
  139. function tCenter(str,ypos)
  140.   term.setCursorPos(w/2 - #str/2,ypos)
  141.   term.write(str)  
  142. end
  143.  
  144. function header(str)
  145.   tCenter(string.rep("-",w),1)
  146.   tCenter(str,3)
  147.   tCenter(string.rep("-",w),5)
  148. end
  149.  
  150. --[[Introduction Voice]]------
  151. rednet.open(modemside)
  152. clear()
  153. tClear()
  154. term.setTextColor(colors.red)
  155. lmon.setBackgroundColor(colors.blue)
  156. rmon.setBackgroundColor(colors.blue)
  157. fillScrs()
  158. term.setTextColor(colors.cyan)
  159. tCenter("<Coded By: Firestorm002>",2)
  160. tCenter("<Version "..version..">",4)
  161. term.setTextColor(colors.white)
  162. tCenter("<LOADING>",6)
  163. term.setTextColor(colors.red)
  164. tCenter("[                       ]",7)
  165. sleep(1)
  166. term.setTextColor(colors.orange)
  167. tCenter("[====                   ]",7)
  168. sleep(1)
  169. term.setTextColor(colors.yellow)
  170. tCenter("[========               ]",7)
  171. sleep(1)
  172. tCenter("[============           ]",7)
  173. sleep(1)
  174. term.setTextColor(colors.green)
  175. tCenter("[================       ]",7)
  176. sleep(1)
  177. term.setTextColor(colors.lime)
  178. tCenter("[====================   ]",7)
  179. sleep(1)
  180. tCenter("[=======================]",7)
  181. sleep(1.5)
  182. tClear()
  183. term.setTextColor(colors.white)
  184. tCenter("<LOADING COMPLETE>",6)
  185. term.setTextColor(colors.lime)
  186. tCenter("[=======================]",7)
  187. sleep(1.5)
  188. clear()
  189. tClear()
  190. lmon.setBackgroundColor(colors.black)
  191. rmon.setBackgroundColor(colors.black)
  192. lmon.setTextColor(colors.yellow)
  193. rmon.setTextColor(colors.yellow)
  194. clear()
  195. --Titles--
  196. mUpdate()
  197. ------------------------------
  198. --Core Mechanics-[DO NOT TOUCH UNLESS EXPERIENCED]-
  199. while true do
  200.   term.setTextColor(colors.yellow)
  201.   header("<VOTING SOFTWARE "..version..">")
  202.   term.setTextColor(colors.white)
  203.   tCenter("ADMINS: Press 'o' to override this menu.",9)
  204.   ev,p1,p2,p3 = os.pullEvent()
  205.   if ev == "rednet_message" then
  206.     if p2 == "vote1" then
  207.       h = fs.open(filename1,"w")
  208.       msga1 = msga1 + 1
  209.       h.write(msga1)
  210.       h.close()
  211.       t = fs.open(filename2,"r")
  212.       msga2 = t.readAll()
  213.       t.close()
  214.       clear()
  215.       mUpdate()
  216.     elseif p2 == "vote2" then
  217.       h = fs.open(filename2,"w")
  218.       msga2 = msga2 + 1
  219.       h.write(msga2)
  220.       h.close()
  221.       t = fs.open(filename1,"r")
  222.       msga1 = t.readAll()
  223.       t.close()
  224.       clear()
  225.          mUpdate()
  226.     elseif p2 == "newvoter" then
  227.       ev,p1,p2,p3 = os.pullEvent("rednet_message")
  228.       h = fs.open("votinguserdata/"..p2,"w")
  229.       h.close()
  230.     elseif p2 == "needvoter" then
  231.       ev,p1,p2,p3 = os.pullEvent("rednet_message")
  232.       if fs.exists("votinguserdata/"..p2) then
  233.         rednet.broadcast("yes")
  234.       elseif fs.exists("votinguserdata/"..p2) == false then
  235.         rednet.broadcast("no")
  236.       end
  237.     end
  238.   elseif ev == "char" then
  239.     if p1 == "o" then
  240.       term.setTextColor(colors.lime)
  241.       tClear()
  242.       header("<OVERRIDE INITIATED>")
  243.       term.setTextColor(colors.white)
  244.       tWrite("PASSWORD: ",2,8)
  245.       userinput = read("*")
  246.       if userinput == overridepass then
  247.         while true do
  248.           term.setTextColor(colors.lime)
  249.           tClear()
  250.           header("<CONTROL PANEL>")
  251.           term.setTextColor(colors.white)
  252.           tWrite("rom/admin>",2,6)
  253.           userinput2 = read()
  254.           if userinput2 == "cleardata" then
  255.             h = fs.open(filename1,"w")
  256.             t = fs.open(filename2,"w")
  257.             h.write("0.0")
  258.             t.write("0.0")
  259.             h.close()
  260.             t.close()
  261.             tCenter("<ALL VOTING DATA CLEARED>",9)
  262.             sleep(2)
  263.           elseif userinput2 == "exit" then
  264.             tClear()
  265.             term.setTextColor(colors.lime)
  266.             header("<EXITING>")
  267.             sleep(1.5)
  268.             tClear()
  269.             break
  270.          -- elseif userinput2 == "edit" then
  271.          --   shell.run("edit","startup")
  272.           elseif userinput2 == "reboot" then
  273.             os.reboot()
  274.           elseif userinput2 == "help" then
  275.             tClear()
  276.             term.setTextColor(colors.cyan)
  277.             header("<HELP GUIDE>")
  278.             term.setTextColor(colors.yellow)
  279.             tCenter("reboot - Reboots the program.",8)
  280.             tCenter("help - Brings up the page you are on currently.",9)
  281.             tCenter("renametitles - Let's you rename",10)
  282.             tCenter("the titles of the vote options.",11)
  283.             tCenter("exit - Exits this command prompt area.",12)
  284.             tCenter("cleardata - Clears the number of votes.",13)
  285.             tCenter("clearuserdata - Clears all users names",14)
  286.             tCenter("that have already voted.",15)
  287.             term.setTextColor(colors.orange)
  288.             tCenter("<Press any key to continue.>",18)
  289.             ev,p1,p2,p3 = os.pullEvent("key")
  290.           elseif userinput2 == "renametitles" then
  291.             term.setTextColor(colors.lime)
  292.             header("<RENAME TITLES>")
  293.             term.setTextColor(colors.white)
  294.             tCenter("<Press '1' or '2' to rename them>",9)
  295.             ev,p1,p2,p3 = os.pullEvent("char")
  296.             if p1 == "1" then
  297.               tClear()
  298.               term.setTextColor(colors.lime)
  299.               header("<RENAME TITLE #1>")
  300.               term.setTextColor(colors.white)
  301.               tWrite("New Name: ",2,9)
  302.               newname1 = read()
  303.               h = fs.open("votingdata/titlea","w")
  304.               h.write(newname1)
  305.               h.close()
  306.             elseif p1 == "2" then
  307.               tClear()
  308.               term.setTextColor(colors.lime)
  309.               header("<RENAME TITLE #2>")
  310.               term.setTextColor(colors.white)
  311.               tWrite("New Name: ",2,9)
  312.               newname2 = read()
  313.               h = fs.open("votingdata/titleb","w")
  314.               h.write(newname2)
  315.               h.close()
  316.               tClear()
  317.             end
  318.           elseif userinput2 == "clearuserdata" then
  319.             fs.delete("votinguserdata")
  320.             fs.makeDir("votinguserdata")
  321.             tCenter("<ALL USER DATA CLEARED>",9)
  322.             sleep(2)
  323.           else
  324.             tClear()
  325.             term.setTextColor(colors.red)
  326.             header("<ERROR>")
  327.             term.setTextColor(colors.orange)
  328.             tCenter("Invaild command, type 'help' for,",10)
  329.             tCenter("a list of possible commands.",11)
  330.             sleep(3)
  331.           end
  332.         end
  333.       else
  334.         tClear()
  335.         term.setTextColor(colors.red)
  336.         header("<ACCESS DENIED>")
  337.         sleep(5)
  338.         tClear()
  339.       end
  340.     end
  341.   end
  342. end
  343. tClear()
RAW Paste Data