All writeins

This commit is contained in:
dave 2016-11-07 00:28:25 +00:00
parent 3c1d604d4e
commit 9aff4f3f2b
3 changed files with 28 additions and 19 deletions

View File

@ -1,6 +1,7 @@
{ {
"channelWhitelistOn": false, "channelWhitelistOn": false,
"choices": ["hillary", "trump"], "choices": ["hillary", "trump"],
"duration": 3600, "allow_writeins": true,
"cooldown": 900 "duration": 900,
"cooldown": 1800
} }

View File

@ -57,7 +57,7 @@ class IRCCore(asynchat.async_chat):
self.OUTPUT_BUFFER_SIZE = 1000 self.OUTPUT_BUFFER_SIZE = 1000
self.SEND_WAIT = 0.800 self.SEND_WAIT = 0.800
self.outputQueue = queue.PriorityQueue(self.OUTPUT_BUFFER_SIZE) self.outputQueue = queue.Queue(self.OUTPUT_BUFFER_SIZE)
self.outputQueueRunner = OutputQueueRunner(self) self.outputQueueRunner = OutputQueueRunner(self)
self.outputQueueRunner.start() self.outputQueueRunner.start()

View File

@ -13,7 +13,7 @@ import os
import time import time
import math import math
from threading import Timer from threading import Timer
from collections import namedtuple from collections import namedtuple, defaultdict
class Election(ModuleBase): class Election(ModuleBase):
@ -30,8 +30,6 @@ class Election(ModuleBase):
self.games[event.args[0]].gotMsg(event) self.games[event.args[0]].gotMsg(event)
def join_ch(self, event): def join_ch(self, event):
print("---------")
if event.prefix.nick == self.bot.get_nick(): if event.prefix.nick == self.bot.get_nick():
joined_ch = event.trailing joined_ch = event.trailing
if joined_ch not in self.games: if joined_ch not in self.games:
@ -68,7 +66,7 @@ class ElectionGame:
def start_election(self): def start_election(self):
self.cooldown_timer = None self.cooldown_timer = None
self.votes = dict(**{i: [] for i in self.master.config["choices"]}) self.votes = defaultdict(list, dict(**{i: [] for i in self.master.config["choices"]}))
self.state = game_phases.election self.state = game_phases.election
self.end_timer = Timer(self.election_length_s, self.end_election) self.end_timer = Timer(self.election_length_s, self.end_election)
self.end_timer.start() self.end_timer.start()
@ -79,7 +77,7 @@ class ElectionGame:
def end_election(self): def end_election(self):
self.end_timer = None self.end_timer = None
self.state = game_phases.cooldown self.state = game_phases.cooldown
self.print_results(prefix="[Election finished] ") self.print_results(prefix="[Election finished] ", show_writeins=True)
# Only start a new game if someone voted # Only start a new game if someone voted
if sum([len(i) for i in self.votes.values()]) > 0: if sum([len(i) for i in self.votes.values()]) > 0:
@ -87,6 +85,7 @@ class ElectionGame:
self.cooldown_timer.start() self.cooldown_timer.start()
self.bot.act_PRIVMSG(self.channel, "Next election starts in: {}".format(self.format_seconds(self.cooldown_length_s))) self.bot.act_PRIVMSG(self.channel, "Next election starts in: {}".format(self.format_seconds(self.cooldown_length_s)))
else: else:
self.bot.act_PRIVMSG(self.channel, "Nobody voted! To turn me on again, type .election")
self.state = game_phases.halted self.state = game_phases.halted
def gotMsg(self, event): def gotMsg(self, event):
@ -98,34 +97,44 @@ class ElectionGame:
if self.state == game_phases.election: if self.state == game_phases.election:
cmd = self.master.bot.messageHasCommand(".vote", event.trailing, requireArgs=True) cmd = self.master.bot.messageHasCommand(".vote", event.trailing, requireArgs=True)
if cmd: if cmd:
if cmd.args[0] in self.votes.keys(): if self.master.config.get('allow_writeins', True) or cmd.args[0] in self.votes.keys():
for target in self.votes: self.annul_voter(event.prefix.nick)
try:
self.votes[target].remove(event.prefix.nick)
except ValueError:
pass
self.votes[cmd.args[0]].append(event.prefix.nick) self.votes[cmd.args[0]].append(event.prefix.nick)
else: else:
# Invalid candidate # Invalid candidate
pass pass
cmd = self.master.bot.messageHasCommand(".votes", event.trailing) cmd = self.master.bot.messageHasCommand(".votes", event.trailing)
if cmd: if cmd:
self.print_results(show_remaining_time=True) self.print_results(show_remaining_time=True, show_writeins=True)
else: else:
# No election running, pass # No election running, pass
pass pass
def print_results(self, prefix='', postfix='', show_remaining_time=False): def annul_voter(self, voter_nick):
results = ['{} - {}'.format(candidate, len(voters)) for candidate, voters in self.votes.items()] for target in self.votes:
try:
self.votes[target].remove(voter_nick)
except ValueError:
pass
def print_results(self, prefix='', postfix='', show_remaining_time=False, show_writeins=False):
results = ['{} - {}'.format(candidate, len(voters)) for candidate, voters in self.votes.items() if candidate in self.master.config["choices"]]
writeins = [(candidate, len(voters)) for candidate, voters in self.votes.items()
if candidate not in self.master.config["choices"]]
writeins = sorted(writeins, reverse=True, key=lambda item: item[1])
writein_results = ['{} - {}'.format(candidate, voters) for candidate, voters in writeins if voters > 0]
togo_info = "" togo_info = ""
if show_remaining_time: if show_remaining_time:
togo_info = " | ends in {}".format(self.format_seconds(self.ends_at - time.time())) togo_info = " | ends in {}".format(self.format_seconds(self.ends_at - time.time()))
self.bot.act_PRIVMSG(self.channel, "{}{}{}{}".format(prefix, ', '.join(results), postfix, togo_info)) self.bot.act_PRIVMSG(self.channel, "{}{}{}{}".format(prefix, ', '.join(results), postfix, togo_info))
if show_writeins and writein_results:
self.bot.act_PRIVMSG(self.channel, "Top write-ins: {}".format(', '.join(writein_results[0:5])))
def gameover(self): def gameover(self):
for t in [self.end_timer, self.cooldown_timer]: for t in [self.end_timer, self.cooldown_timer]:
@ -145,6 +154,5 @@ class ElectionGame:
output += "{}s".format(round(secs)) output += "{}s".format(round(secs))
return output return output