ID:257767
 
//Title: Voting System
//Credit to: Android Data
//Contributed by: Android Data

/*
This voting system will allow you to create polls for various things, ranging from
"shall we reboot?" to "do you want pie?".
It's a very easy-to-use system.

See below for an example of a basic vote-to-reboot system. Keep in mind that this
system is unfinished (for one it needs to prevent players from spamming the verb).

Multiple polls can run concurrently and there are many features available.
Well, enough of my feeble attempts at advertising my system and onto the proc which
does it all:

dta_votingsystem(determine,msg,title,choices,timer,default,method,voters)
Args:
determine:
set this to to automatically determine the winner(s) of the vote and
return a list of choice(s) that won rather than a list of players
with their votes.
Default: true (1).
msg:
set this to the message to display to the players when they're voting.
messages like "Would you like to reboot?" are common. Messages like
"Would you like to have sex with me?" are not.
Default: "Please select an option."
title:
set this to the titlebar message.
Default: "Vote"
choices:
this is a list of choices the players may make. These must be 2 or
more and may be no more than 255.
Default: list("Yes","No")
timer:
this is the amount of time that the poll will stay open. After this
time has passed, the results will be returned and nobody will be able
to vote again.
Default: 300 (30 seconds)
default:
this is the default choice which is automatically selected when the
player received the input box.
Default: null (first choice in the list)
method:
this can be set to INPUT (0) or ALERT (1) respectively to change the
appearance of the input box. When this is set to ALERT, the "default"
attribute is disabled and choices may not be more than 3.
Default: INPUT (0)
voters:
this can be a list of players allowed to vote or a reference to a
single mob/client to vote (though that would be useless, don't you
think?). If this is empty, it will automatically generate a list
and populate it with all currently logged in players.
Default: null (all players)
*/


#define INPUT 0
#define ALERT 1
dta_votingsystem
var
list/results[0] //results of the poll
list/choices //list of choices
proc/exec(determine,msg,title,list/choices,timer,default,method,list/voters)
/*
if there is no voters list, check if the voters variable is defined
itself and if so make it a list. otherwise, make the list include
every single client present
*/

if(!istype(voters)||!voters.len)
if(voters) voters=list(voters)
else
voters=list()
for(var/client/C) voters+=C

/*
make sure we can choose something and that we have more than one
thing to choose from
*/

if(!istype(choices,/list)||choices.len<=1){ del src }

/*
convert every choice to text. otherwise, the input/alert boxes
won't hold them
*/

for(var/i=1 to choices.len) choices[i]="[choices[i]]"

/*
begin the actual voting
*/

for(var/X in voters) spawn
var/client/C
if(ismob(X)) C=X:client
else if(istype(X,/client)) C=X
if(!C) continue

var/choice
if(method==ALERT&&choices.len<=3) choice=alert(C,msg,title,choices[1],\
choices[2],(choices.len==3?(choices[3]):))
else choice=input(C,msg,title,default) as null|anything in choices
results[C.key]=choice?("[choice]"):(null)

sleep(timer)
return results //relay the results

proc/dta_votingsystem(determine=1,msg="Please select an option.",title="Vote",\
list/choices=list("Yes","No"),timer=300,default,method=INPUT,list/voters)
var/dta_votingsystem/V=new
.= V.exec(arglist(args))

if(determine) //determine the winner(s)
for(var/X in choices) choices[X]=0
for(var/X in .) choices[ .[X] ]++

var/amnt=0
.=null
for(var/X in choices)
if(!choices[X]) continue
if(choices[X]>amnt){ .=list(); .[X]=choices[X]; amnt=choices[X] }
else if(choices[X]==amnt) .[X]=choices[X]

del V

//Example usage: vote_to_reboot verb.

mob/verb/vote_to_reboot()
world<<"<b>[key] started a vote to reboot! You have 15 seconds to vote!</b>"
var/list/X=dta_votingsystem(msg="Do you want to reboot?",title="Vote to reboot",timer=150,\
method=ALERT)
if(!X) world<<"<b>Results:</b> Nobody voted. No reboot."
else if(X.len==1)
if(X[1]=="Yes")
world<<"<b>Results:</b> Rebooting!"
world.Reboot()
else
world<<"<b>Results:</b> \"No\" won. No reboot."
else
world<<"<b>Results:</b> Draw! No reboot."
This is extremely helpful! nj data.
Thxs really helped me and saved me much needed time. Thxs