Subspace Hockey/Football Zone Forums

Please login or register.

Login with username, password and session length
Advanced search  


Team Forums: If you need a RSHL, MSHL, or RSFL team forum, forum PM Kilo Dylie with the name of your team, and who will moderate the board.

Author Topic: Doobie made... a poll module!  (Read 239 times)


  • Local Moderator
  • Registered Posting Whore
  • *****
  • Posts: 2011
Doobie made... a poll module!
« on: January 16, 2017, 03:14:03 PM »

Posting here because I still don't have access to Public Development.  I am still assuming it will be moved later.

Next up on the list of things to make an event/tournament/fun work is a way for users to vote on... something.  The voting also needs testing... so why not a famous puker poll? 

Mods can type a questions of the format <question>?:<answer1>,<answer2>,...

Example: ?createpoll Is this leading somewhere?:Yes,No

Players can then type vote on the option # by typing ?p 1, ?p 2, ?p 3, and so on

I still can't enable the commands or install the modules myself.
default commands: ?p
Mod+ Commands: ?createpoll

Hopefully you will be able to try this poll module and the timer module... soon?

Vote utility needs more work to handle input of multiple votes at once for example... a single arena with 8 games going on.  Not so hard for the players, interesting for the spectators.  Also need to load the blacklist from configs, because you know, violence exists.

Code for doob_vote_util:
Code: [Select]

import asss


# Get ASSS interfaces
ichat = asss.get_interface(asss.I_CHAT)

class DoobVote:

    def __init__(self, arena, callback, vid, options, time, frequencies):
        self.arena = arena
        self.vid = vid
        self.callback = callback
        self.options = options
        self.time = time
        self.frequencies = frequencies
        self.results = {option: [] for option in options}
        self.timer = self.make_vote_timer(time * 100, callback)

    def add_ballot(self, player, option):

    def make_vote_timer(self, time, callback):
        def vote_timer():
            callback(self.arena, self.results)
            self.timer = None
            return 0
        return asss.set_timer(vote_timer, time, time)

    def cancel(self):
        if self.timer is not None:
            self.timer = None

    def __del__(self):
        if self.timer is not None:
            self.timer = None

def start_vote(arena, callback, vid, options, time=30, frequencies=None):
    # validate vid
    if vid is None:
        return 0

    # validate options
    if options is None or len(options) <= 0:
        return 0

    # validate time
    if time < MIN_TIME_SECONDS:
        return 0

    if time > MAX_TIME_SECONDS:
        return 0

    if vid in arena.voteDict:
        return 0

    arena.voteDict[vid] = DoobVote(arena, callback, vid, options, time, frequencies)

    return 1

def try_vote(cmd, params, player, targ):
    voteDict = player.arena.voteDict
    if len(voteDict) <= 0:
        ichat.SendMessage(player, "Nothing to vote on")

    playerVotes = []
    for pvote in voteDict.itervalues():
        if pvote.frequencies is not None:
            if player.p_freq in pvote.frequencies:

    numVotes = len(playerVotes)
    if numVotes == 0:
        ichat.SendMessage(player, "Nothing to vote on")
    elif numVotes > 1:
        ichat.SendMessage(player, "Multiple votes available, need to specify")

    vote = playerVotes[0]

    if params not in vote.options:
        ichat.SendMessage(player, "%s is an invalid vote.  Valid votes=%s" % (params, ', '.join(vote.options)));

    for voters in vote.results.values():
        if in voters:
            ichat.SendMessage(player, "You already voted");

    vote.add_ballot(player, params)
    ichat.SendMessage(player, "You voted for option %s" % params)

def mm_attach(a):
    #load blacklist from cfg
    a.blacklist = []
    a.voteDict = {}

def mm_detach(a):
    for vote in a.voteDict.itervalues():

    a.voteDict = None
    a.blacklist = None

Code for doob_poll:
Code: [Select]

import asss
import doob_vote_util

ichat = asss.get_interface(asss.I_CHAT)

POLL_VID = "poll"
POLL_FORMAT = "?createpoll question: answer1, answer2, ...\n Example: ?createpoll Was this helpeful?:Yes,No,Maybe"

def c_create_poll(cmd, params, player, targ):
    if params is None:
        ichat.SendMessage(player, "Invalid parameters")
        ichat.SendMessage(player, POLL_FORMAT)

    qa = params.split(":")
    if len(qa) != 2:
        ichat.SendMessage(player, "Invalid parameters> missing :")
        ichat.SendMessage(player, POLL_FORMAT)

    answers = qa[1].split(",")
    if len(answers) <= 1:
        ichat.SendMessage(player, "Invalid parameters: only 1 answer")
        ichat.SendMessage(player, POLL_FORMAT)

    if player.arena.doob_poll_question is not None:
        ichat.SendMessage(player, "Existing poll has not finished, please wait")

    opts = []

    ichat.SendArenaMessage(player.arena, "Poll: %s" % qa[0])
    ichat.SendArenaMessage(player.arena, "Options:")
    for i, answer in enumerate(answers):
        opts.append("%d" % (i+1))
        ichat.SendArenaMessage(player.arena, "        %d) %s" % ((i+1), answer))

    player.arena.doob_poll_question = qa[0]
    doob_vote_util.start_vote(player.arena, c_poll_complete, POLL_VID, opts, 30)

def c_poll_complete(arena, results):

    ichat.SendArenaMessage(arena, "Poll Results: %s" % arena.doob_poll_question)

    s = sorted(results.items(), key=lambda x: (len(x[1]), x[0]), reverse=True)
    for k, v in s:
        ichat.SendArenaMessage(arena, "        %s) %d votes {%s}" % (k, len(v), ", ".join(v)))

    arena.doob_poll_question = None

def mm_attach(a):
    a.doob_poll_question = None
    a.doob_poll_cmd = asss.add_command("createpoll", c_create_poll, a)
    a.doob_p_cmd = asss.add_command("p", doob_vote_util.try_vote, a)

def mm_detach(a):
    a.doob_poll_question = None
    for attr in ['p_cmd', 'poll_cmd', 'poll_question']:
            delattr(a, 'doob_' + attr)

Page created in 0.056 seconds with 19 queries.