From 96d2a1fae68b443066b4d3269223429623527857 Mon Sep 17 00:00:00 2001 From: jesopo Date: Mon, 17 Jun 2019 17:51:42 +0100 Subject: [PATCH] Support multiple concurrent votes --- modules/vote.py | 133 ++++++++++++++++++++++++++---------------------- 1 file changed, 72 insertions(+), 61 deletions(-) diff --git a/modules/vote.py b/modules/vote.py index f7721270..bdb44b72 100644 --- a/modules/vote.py +++ b/modules/vote.py @@ -1,46 +1,58 @@ -import binascii, os, uuid +import binascii, functools, operator, os, uuid from src import ModuleManager, utils -STR_NOVOTE = "There is currently no vote running." +STR_NOVOTE = "Unknown vote '%s'" class Module(ModuleManager.BaseModule): - def _get_vote(self, channel): - return channel.get_setting("vote", None) - def _set_vote(self, channel, vote): - channel.set_setting("vote", vote) - def _del_vote(self, channel): - channel.del_setting("vote") - - def _add_archive_vote(self, channel, vote, id): - channel.set_setting("vote-%s" % id, vote) - def _get_archive_vote(self, channel, id): - return channel.get_setting("vote-%s" % id, None) + def _get_vote(self, channel, vote_id): + return channel.get_setting("vote-%s" % vote_id, None) + def _set_vote(self, channel, vote_id, vote): + channel.set_setting("vote-%s" % vote_id, vote) def _random_id(self): return binascii.hexlify(os.urandom(4)).decode("ascii") - def _archive_vote(self, channel): - vote = self._get_vote(channel) + + def _close_vote(self, channel, vote_id): + vote = self._get_vote(channel, vote_id) + if vote: + vote["open"] = False + self._set_vote(channel, vote_id, vote) + return True + return False + + def _start_vote(self, channel, description): vote_id = self._random_id() - self._add_archive_vote(channel, vote, vote_id) - self._del_vote(channel) - return vote_id + vote = {"description": description, "options": {"yes": [], "no": []}, + "electorate": [], "open": True, "id": vote_id} + self._set_vote(channel, vote_id, vote) + return vote def _format_vote(self, vote): - return "%s (%s yes, %s no)" % (vote["description"], len(vote["yes"]), - len(vote["no"])) + options = ["%d %s" % (len(v), k) for k, v in vote["options"].items()] + return "%s (%s)" % (vote["description"], ", ".join(options)) + def _format_options(self, vote): + return ", ".join("'%s'" % o for o in vote["options"]) - def _cast_vote(self, channel, user, yes): - vote = self._get_vote(channel) - key = "yes" if yes else "no" - voters = vote["yes"]+vote["no"] + def _cast_vote(self, channel, vote_id, user, option): + vote = self._get_vote(channel, vote_id) + option = vote["options"][option] + voters = functools.reduce(operator.concat, + list(vote["options"].values())) if user.name in voters: return False - vote[key].append(user.name) - self._set_vote(channel, vote) + option.append(user.name) + self._set_vote(channel, vote_id, vote) return True + def _open_votes(self, channel): + open = [] + for setting, vote in channel.find_settings_prefix("vote-"): + if vote["open"]: + open.append(vote) + return open + @utils.hook("received.command.startvote", channel_only=True, min_args=1) def start_vote(self, event): """ @@ -49,66 +61,65 @@ class Module(ModuleManager.BaseModule): :require_mode: o :permission: vote """ - current_vote = self._get_vote(event["target"]) - if not current_vote == None: - raise utils.EventError("There's already a vote running") - - self._set_vote(event["target"], {"description": event["args"], - "yes": [], "no": [], "electorate": []}) + vote = self._start_vote(event["target"], event["args"]) event["stdout"].write( - "Vote started. use '%svote yes' or '%svote no' to vote." % ( - event["command_prefix"], event["command_prefix"])) + "Vote %s started. use '%svote