goreae

CC VOTE/BALLOTSERVER

Jan 3rd, 2013
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.22 KB | None | 0 0
  1. term.clear()
  2. term.setCursorPos(1,1)
  3. print("Running Ballot Server.")
  4. while true do
  5.   rednet.open("top")
  6.   id,command = rednet.receive()
  7.   if command == "list" then
  8.     local list = fs.list("disk")
  9.     rednet.send(id,textutils.serialize(list))
  10.   end
  11.   if command == "info" then
  12.     local id,ballot = rednet.receive(10)
  13.     hBallot = fs.open("disk/"..ballot,"r")
  14.     rednet.send(id,hBallot.readLine())
  15.     hBallot.close()
  16.   end
  17.   if command == "vote" then
  18.     local id,ballot = rednet.receive()
  19.     local id,Uid = rednet.receive()
  20.     if not fs.exists("disk/idlists/"..ballot) then
  21.       T = fs.open("disk/idlists/"..ballot,"w")
  22.       T.close()
  23.     end
  24.     local hList = fs.open("disk/idlists/"..ballot,"r")
  25.     for i=1,1000 do
  26.       duplicate = hList.readLine()
  27.       if duplicate == nil then
  28.         hList.close()
  29.         hList = fs.open("disk/idlists/"..ballot,"a")
  30.         hList.writeLine(Uid)
  31.         rednet.send(id,"accepted")
  32.         hList.close()
  33.         break
  34.       end
  35.       if duplicate == Uid then
  36.         rednet.send(id,"denied")
  37.       end
  38.     end
  39.     FileDir = "disk/"..ballot
  40.     hMsg = fs.open(FileDir,"r")    
  41.     info = hMsg.readLine()
  42.     rednet.send(id,info)
  43.     hMsg.close()
  44.   end
  45. end
Advertisement
Add Comment
Please, Sign In to add comment