• 🏆 Texturing Contest #33 is OPEN! Contestants must re-texture a SD unit model found in-game (Warcraft 3 Classic), recreating the unit into a peaceful NPC version. 🔗Click here to enter!
  • It's time for the first HD Modeling Contest of 2024. Join the theme discussion for Hive's HD Modeling Contest #6! Click here to post your idea!

[JASS] Simple Vote System

Level 3
Joined
Feb 14, 2013
Messages
26
System code:
JASS:
globals
   boolean isVoteActive
   integer VoteResultY
   integer VoteResultN
   timer timer_VoteTime
   trigger trigger_VoteAction = null
    trigger trigger_VoteDialog = null
    trigger trigger_VoteExpire = null
    dialog dialog_Vote = null
    button array btnVote
    real rDisableVote
endglobals

function VoteDialog_Actions takes nothing returns nothing
    if GetClickedButton() == btnVote[0] then
        set VoteResultY = VoteResultY + 1
        call DisplayTextToForce(bj_FORCE_ALL_PLAYERS, GetPlayerName(GetTriggerPlayer()) + " vote |cFF00FF00Yes|r")
    elseif GetClickedButton() == btnVote[1] then
        set VoteResultN = VoteResultN + 1
        call DisplayTextToForce(bj_FORCE_ALL_PLAYERS, GetPlayerName(GetTriggerPlayer()) + " vote |cFFFF0000No|r")
    endif
endfunction
function Init_VoteDialog takes nothing returns nothing
    set trigger_VoteDialog = CreateTrigger()
    call TriggerRegisterDialogEvent(trigger_VoteDialog, dialog_Vote)
    call TriggerAddAction(trigger_VoteDialog, function VoteDialog_Actions)
endfunction

function VoteExpire_Actions takes nothing returns nothing
    local integer i = 0
    loop
        exitwhen i > 12
        call DialogDisplay(Player(i), dialog_Vote, false)
        set i = i + 1
    endloop
    call DisplayTextToForce(bj_FORCE_ALL_PLAYERS, "|cFFCC99FFVote result: |r|cFF00FF00" + I2S(VoteResultY) + " Yes |r|cFFFF0000" + I2S(VoteResultN) + " No|r")
    if VoteResultY > VoteResultN then
        call ConditionalTriggerExecute(trigger_VoteAction)
    endif
    if rDisableVote > 0 then
        call DisplayTextToForce(bj_FORCE_ALL_PLAYERS, "|cFFCC99FFVote disable in " + I2S(R2I(rDisableVote)) + "s|r")
        call TriggerSleepAction(rDisableVote)
        set rDisableVote = 0
    endif
    set isVoteActive = false
    set VoteResultY = 0
    set VoteResultN = 0
    call TriggerClearActions(trigger_VoteAction)
    call DialogClear(dialog_Vote)
endfunction
function Init_VoteExpire takes nothing returns nothing
    set trigger_VoteExpire = CreateTrigger()
    call TriggerRegisterTimerExpireEvent(trigger_VoteExpire, timer_VoteTime)
    call TriggerAddAction(trigger_VoteExpire, function VoteExpire_Actions)
endfunction

function StartVote takes code func, string skip, string title, real timeout returns nothing
   local integer i = 0
   if isVoteActive == false then
      set btnVote[0] = DialogAddButton(dialog_Vote, "|cFF00FF00Yes|r", 0)
      set btnVote[1] = DialogAddButton(dialog_Vote, "|cFFFF0000No|r", 0)
       set isVoteActive = true
       call TriggerAddAction(trigger_VoteAction, func)
        call DialogSetMessage(dialog_Vote, title)
       call TimerStart(timer_VoteTime, timeout, false, null)
       loop
           exitwhen i > 12
           if GetPlayerSlotState(Player(i)) == PLAYER_SLOT_STATE_PLAYING and GetPlayerController(Player(i)) == MAP_CONTROL_USER then
               if skip == "all" then
                    call DialogDisplay(Player(i), dialog_Vote, true)
               elseif Player(i) != Player(S2I(skip)) then
                    call DialogDisplay(Player(i), dialog_Vote, true)
               endif
           endif
           set i = i + 1
       endloop
   else
       call DisplayTextToPlayer(GetTriggerPlayer(), 0, 0, "|cFFCC99FFVote on active, try again late...|r")
   endif
endfunction

function Init_Vote takes nothing returns nothing
    set timer_VoteTime = CreateTimer()
    set VoteResultY = 0
   set VoteResultN = 0
    set isVoteActive = false
    set trigger_VoteAction = CreateTrigger()
    set dialog_Vote = DialogCreate()
    set rDisableVote = 0
    call Init_VoteDialog()
    call Init_VoteExpire()
endfunction
How to use:
Add on map init
JASS:
call Init_Vote()
Example code:
JASS:
globals
   integer playerToKick = 0
   trigger gg_trg_Kick = null
endglobals
function VoteEndKickPlayer takes nothing returns nothing
   call CustomDefeatBJ(Player(playerToKick))
endfunction
function Trig_KickPlayer_Actions takes nothing returns nothing
   //except = all //if you want allow vote dialog for all player
   local string except = SubString(GetEventPlayerChatString(), 5, StringLength(GetEventPlayerChatString()))
   local real voteTimeout = 15.0
   set playerToKick = S2I(except)
   set rDisableVote = 60.0 //vote can reactive after 1 minute - default = 0
   call StartVote(function VoteEndKickPlayer, except, GetPlayerName(GetTriggerPlayer()) + " active vote kick player " + GetPlayerName(Player(playerToKick)), voteTimeout)
   set except = null
endfunction
function Init_KickPlayer takes nothing returns nothing
   set gg_trg_Kick = CreateTrigger()
   call TriggerRegisterPlayerChatEvent(gg_trg_Kick, Player(0), "-kick", false)
   call TriggerAddAction(gg_trg_Kick, function Trig_KickPlayer_Actions)
endfunction
 
Last edited:
Level 13
Joined
Nov 7, 2014
Messages
571
It is possible that players can choose not to vote, so it might be a good idea to require the "yes votes" to be greater than integer(number-of-playing-players / 2) (democracy / mob rule).

There's probably no point in waiting for the timer to expire when all the players have voted.

For yes-no questions using dialogs/buttons is probably an overkill (simple -y[es]/-n[o] chat commands should suffice) and annoying, i.e imagine while being very focused on the game and someone just decides to start a vote and all the players would get this distracting dialog in their face.


PS: One can also imagine having voting for non-yes-no questions as well, say voting for the game mode (ffa, team, capture the flag, etc.), i.e the selected mode is whichever one got the most votes, for example.
 
Top