Advertisement
Guest User

Vote

a guest
Mar 5th, 2013
328
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.88 KB | None | 0 0
  1. local forMsgFile = fs.open("for","r")
  2. local forMsg = forMsgFile.readAll()
  3. forMsgFile.close()
  4. local agnMsgFile = fs.open("against","r")
  5. local agnMsg = agnMsgFile.readAll()
  6. agnMsgFile.close()
  7. local moniter = peripheral.wrap("top")
  8. local votes = { forVotes = 0 , agnVotes = 0 }
  9.  
  10. function screen(cv,msg)
  11.   moniter.clear()
  12.   moniter.setCursorPos(1,1)
  13.   moniter.setTextColor(32)
  14.   if cv then
  15.     moniter.setBackgroundColor(2048)
  16.   else
  17.     moniter.setBackgroundColor(32768)
  18.   end
  19.   moniter.write(forMsg)
  20.   moniter.setBackgroundColor(32768)
  21.   if msg ~= nil then
  22.     moniter.setCursorPos(1,3)
  23.     moniter.setTextColor(1)
  24.     moniter.write(msg)
  25.     moniter.setBackgroundColor(32768)
  26.   end
  27.   moniter.setCursorPos(1,5)
  28.   moniter.setTextColor(16384)
  29.   if not cv then
  30.     moniter.setBackgroundColor(2048)
  31.   else
  32.     moniter.setBackgroundColor(32768)
  33.   end
  34.   moniter.write(agnMsg)  
  35.   moniter.setBackgroundColor(32768)
  36. end
  37.  
  38. if fs.exists("voteFile") == true then
  39.   local vf = fs.open("voteFile","r")
  40.   votes = textutils.unserialize(vf.readAll())
  41. end
  42.  
  43. screen(true)
  44.  
  45. while true do
  46.   local currentVote = not rs.getInput("left")
  47.   local confirmation = false
  48.   screen(currentVote)
  49.   local event = { os.pullEventRaw() }
  50.   print( event[1].." "..event[2])
  51.   if event[1] == "redstone" then
  52.     confirmation = false
  53.     currentVote = not rs.getInput("left")
  54.   end
  55.  
  56.   if event[1] == "player" then
  57.     if v[event[2]] == nil and not confirmation then
  58.       screen(currentVote,event[2].." would you like to vote ".. (currentVote and "for" or "against").. "?")
  59.       confirmation = true
  60.     elseif v[event[2]] == nil and confirmation then
  61.       confirmation = false
  62.       v[event[2]] = currentVote
  63.       save()
  64.       screen(currentVote,"Thank you for voting.")
  65.     end
  66.   end
  67. end
  68.  
  69. function save()
  70.   local s = fs.open("voteFile","w")
  71.   s.write(textutils.serialize(v))
  72.   s.close()
  73. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement