Change vote, instead of rejecting, when people vote twice

This commit is contained in:
jesopo 2019-06-17 22:31:27 +01:00
parent 37fd78ff27
commit 312b0af645

View file

@ -1,8 +1,12 @@
import binascii, functools, operator, os, uuid import binascii, enum, os, uuid
from src import ModuleManager, utils from src import ModuleManager, utils
STR_NOVOTE = "Unknown vote '%s'" STR_NOVOTE = "Unknown vote '%s'"
class VoteCastResult(enum.Enum):
Cast = 1
Changed = 2
class Module(ModuleManager.BaseModule): class Module(ModuleManager.BaseModule):
def _get_vote(self, channel, vote_id): def _get_vote(self, channel, vote_id):
return channel.get_setting("vote-%s" % vote_id, None) return channel.get_setting("vote-%s" % vote_id, None)
@ -39,15 +43,18 @@ class Module(ModuleManager.BaseModule):
def _cast_vote(self, channel, vote_id, user, option): def _cast_vote(self, channel, vote_id, user, option):
vote = self._get_vote(channel, vote_id) vote = self._get_vote(channel, vote_id)
option = vote["options"][option] option = vote["options"][option]
voters = functools.reduce(operator.concat,
list(vote["options"].values()))
if user.name in voters: cast_type = VoteCastResult.Cast
return False
for nicks in vote["options"].values():
if user.name in nicks:
nicks.remove(user.name)
cast_type = VoteCastResult.Changed
break
option.append(user.name) option.append(user.name)
self._set_vote(channel, vote_id, vote) self._set_vote(channel, vote_id, vote)
return True return cast_type
def _open_votes(self, channel): def _open_votes(self, channel):
open = [] open = []
@ -107,12 +114,15 @@ class Module(ModuleManager.BaseModule):
raise utils.EventError("Vote options: %s" % raise utils.EventError("Vote options: %s" %
self._format_options(vote)) self._format_options(vote))
if self._cast_vote(event["target"], vote_id, event["user"], choice): cast_result = self._cast_vote(event["target"], vote_id,
event["stdout"].write("%s: your vote has been cast." % event["user"], choice)
event["user"].nickname)
else: cast_desc = "cast"
event["stderr"].write("%s: you have already voted." % if cast_result == VoteCastResult.Changed:
event["user"].nickname) cast_desc = "changed"
event["stdout"].write("%s: your vote has been %s." %
(event["user"].nickname, cast_desc))
@utils.hook("received.command.votes", channel_only=True) @utils.hook("received.command.votes", channel_only=True)
def votes(self, event): def votes(self, event):