Advertisement
Pirnogion

[OC]VOTE

Mar 9th, 2016
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.44 KB | None | 0 0
  1. local component = require "component"
  2. local unicode = require "unicode"
  3. local event = require "event"
  4.  
  5. local ser = require "serialization"
  6.  
  7. local gpu = component.gpu
  8. local cb = component.command_block
  9. local chat = component.chat_box
  10. --local debug = component.debug
  11.  
  12. local running = true
  13.  
  14. local voteTimer = nil
  15.  
  16. local voting = false
  17.  
  18. local maxPlayers = 10
  19. local currentVote = nil
  20.  
  21. local voteInitiaterPlayer = nil
  22. local positiveVote = 0
  23. local negativeVote = 0
  24. local votePlayers = {}
  25.  
  26. local positiveWords = {
  27.   ["+"] = true,
  28.   ["плюс"] = true,
  29.   ["да"] = true,
  30.   ["за"] = true,
  31.   ["угу"] = true
  32. }
  33.  
  34. local negativeWords = {
  35.   ["-"] = true,
  36.   ["минус"] = true,
  37.   ["негативно"] = true,
  38.   ["нет"] = true
  39. }
  40.  
  41. local function reset()
  42.   if (voteTimer) then event.cancel(voteTimer) end
  43.  
  44.   voting = false
  45.   currentVote = nil
  46.   voteInitiaterPlayer = nil
  47.   positiveVotePlayers = 0
  48.   negativeVotePlayers = 0
  49.   votePlayers = {}
  50.  
  51.   running = false
  52. end
  53.  
  54. local offRainVote = {
  55.   voteName = "ОТКЛЮЧЕНИЕ ДОЖДЯ",
  56.   voteAcceptText = "Дождь отключен по решению большинства игроков.",
  57.   voteRejectText = "Недостаточно голосов за отключение дождя.",
  58.  
  59.   cmd = '/tellraw @a {text:"Дождь типа отключен", color:dark_red}',
  60.  
  61.   voteTime = 20
  62. }
  63.  
  64. local setDayVote = {
  65.   voteName = "ВКЛЮЧИТЬ ДЕНЬ",
  66.   voteAcceptText = "День включен по решению большинства игроков.",
  67.   voteRejectText = "Недостаточно голосов для включения дня.",
  68.  
  69.   cmd = '/tellraw @a {text:"Как пройти в библиотеку?", color:dark_red}',
  70.  
  71.   voteTime = 20
  72. }
  73.  
  74. local function checkResult()
  75.   if ( positiveVote > negativeVote ) then
  76.     cb.setCommand(currentVote.cmd)
  77.     cb.executeCommand()
  78.  
  79.     chat.say(currentVote.voteAcceptText .. ".")
  80.   else
  81.     chat.say("Голосование отменено: " .. currentVote.voteRejectText .. ".")
  82.   end
  83.  
  84.   reset()
  85. end
  86.  
  87. local function startVoteTimer(time)  
  88.   voteTimer = event.timer(time, checkResult)
  89. end
  90.  
  91. local function stopVoteTimer()
  92.   if (voteTimer) then event.cancel(voteTimer) end
  93. end
  94.  
  95. local topics = {
  96.   ["начать голосование: отключить дождь"] = offRainVote,
  97.   ["начать голосование: включить день"] = setDayVote
  98. }
  99.  
  100. while ( running ) do
  101.   local e = {event.pull()}
  102.  
  103.   if ( e[1] == "chat_message" ) then
  104.     local msg = unicode.lower(e[4])
  105.  
  106.     if ( not voting ) then
  107.       currentVote = topics[msg]
  108.       voteInitiaterPlayer = e[3]
  109.  
  110.       if ( currentVote ) then
  111.         voting = true
  112.         startVoteTimer(currentVote.voteTime)
  113.    
  114.         chat.say('Началось голосование: "' .. currentVote.voteName .. '". Инициатор: ' .. voteInitiaterPlayer .. ".")
  115.       end
  116.     else
  117.       if ( positiveWords[msg] and not votePlayers[ e[3] ] ) then
  118.         positiveVote = positiveVote + 1
  119.         votePlayers[ e[3] ] = true
  120.         chat.say('Голосов "ЗА/ПРОТИВ": ' .. positiveVote .. "/" .. negativeVote)
  121.       elseif ( negativeWords[msg] and not votePlayers[ e[3] ] ) then
  122.         negativeVote = negativeVote + 1
  123.         votePlayers[ e[3] ] = true
  124.         chat.say('Голосов "ЗА/ПРОТИВ": ' .. positiveVote .. "/" .. negativeVote)
  125.       end
  126.     end
  127.   end
  128. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement