Firestorm002

Voting Server Program v1.0

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