From 906961fddfa1a3aa5f8f4dc3d52dc2b8b38dad1f Mon Sep 17 00:00:00 2001 From: Matthias Meusburger Date: Mon, 23 Jan 2017 17:35:35 +0100 Subject: [PATCH] Reindent - 2to3 - Add "Don't pretend to be me!" --- __init__.py | 7 +- plugin.py | 1525 ++++++++++++++++++++++++++------------------------- 2 files changed, 773 insertions(+), 759 deletions(-) diff --git a/__init__.py b/__init__.py index ca24579..1d3b20c 100644 --- a/__init__.py +++ b/__init__.py @@ -34,6 +34,7 @@ This is a DuckHunt game for supybot import supybot import supybot.world as world +from imp import reload # Use this for the version of this plugin. You may wish to put a CVS keyword # in here if you're keeping the plugin in CVS or some similar system. @@ -49,14 +50,14 @@ __contributors__ = {} # This is a url where the most recent plugin package can be downloaded. __url__ = 'https://github.com/veggiematts/supybot-duckhunt' -import config -import plugin +from . import config +from . import plugin reload(plugin) # In case we're being reloaded. # Add more reloads here if you add third-party modules and want them to be # reloaded when this plugin is reloaded. Don't forget to import them as well! if world.testing: - import test + from . import test Class = plugin.Class configure = config.configure diff --git a/plugin.py b/plugin.py index 0722bd9..435d3dd 100644 --- a/plugin.py +++ b/plugin.py @@ -94,78 +94,78 @@ class DuckHunt(callbacks.Plugin): def _calc_scores(self, channel): - """ - Adds new scores and times to the already saved ones - """ + """ + Adds new scores and times to the already saved ones + """ - # scores - # Adding current scores to the channel scores - for player in self.scores[channel].keys(): - if not player in self.channelscores[channel]: - # It's a new player - self.channelscores[channel][player] = self.scores[channel][player] - else: - # It's a player that already has a saved score - self.channelscores[channel][player] += self.scores[channel][player] + # scores + # Adding current scores to the channel scores + for player in list(self.scores[channel].keys()): + if not player in self.channelscores[channel]: + # It's a new player + self.channelscores[channel][player] = self.scores[channel][player] + else: + # It's a player that already has a saved score + self.channelscores[channel][player] += self.scores[channel][player] - # times - # Adding times scores to the channel scores - for player in self.toptimes[channel].keys(): - if not player in self.channeltimes[channel]: - # It's a new player - self.channeltimes[channel][player] = self.toptimes[channel][player] - else: - # It's a player that already has a saved score - # And we save the time of the current hunt if it's better than it's previous time - if(self.toptimes[channel][player] < self.channeltimes[channel][player]): - self.channeltimes[channel][player] = self.toptimes[channel][player] + # times + # Adding times scores to the channel scores + for player in list(self.toptimes[channel].keys()): + if not player in self.channeltimes[channel]: + # It's a new player + self.channeltimes[channel][player] = self.toptimes[channel][player] + else: + # It's a player that already has a saved score + # And we save the time of the current hunt if it's better than it's previous time + if(self.toptimes[channel][player] < self.channeltimes[channel][player]): + self.channeltimes[channel][player] = self.toptimes[channel][player] - # worst times - # Adding worst times scores to the channel scores - for player in self.worsttimes[channel].keys(): - if not player in self.channelworsttimes[channel]: - # It's a new player - self.channelworsttimes[channel][player] = self.worsttimes[channel][player] - else: - # It's a player that already has a saved score - # And we save the time of the current hunt if it's worst than it's previous time - if(self.worsttimes[channel][player] > self.channelworsttimes[channel][player]): - self.channelworsttimes[channel][player] = self.worsttimes[channel][player] + # worst times + # Adding worst times scores to the channel scores + for player in list(self.worsttimes[channel].keys()): + if not player in self.channelworsttimes[channel]: + # It's a new player + self.channelworsttimes[channel][player] = self.worsttimes[channel][player] + else: + # It's a player that already has a saved score + # And we save the time of the current hunt if it's worst than it's previous time + if(self.worsttimes[channel][player] > self.channelworsttimes[channel][player]): + self.channelworsttimes[channel][player] = self.worsttimes[channel][player] - # week scores - for player in self.scores[channel].keys(): - #FIXME: If the hunt starts a day and ends the day after, this will produce an error: - if not player in self.channelweek[channel][self.woy][self.dow]: - # It's a new player - self.channelweek[channel][self.woy][self.dow][player] = self.scores[channel][player] - else: - # It's a player that already has a saved score - self.channelweek[channel][self.woy][self.dow][player] += self.scores[channel][player] + # week scores + for player in list(self.scores[channel].keys()): + #FIXME: If the hunt starts a day and ends the day after, this will produce an error: + if not player in self.channelweek[channel][self.woy][self.dow]: + # It's a new player + self.channelweek[channel][self.woy][self.dow][player] = self.scores[channel][player] + else: + # It's a player that already has a saved score + self.channelweek[channel][self.woy][self.dow][player] += self.scores[channel][player] def _write_scores(self, channel): - """ - Write scores and times to the disk - """ + """ + Write scores and times to the disk + """ - # scores + # scores outputfile = open(self.path.dirize(self.fileprefix + channel + ".scores"), "wb") pickle.dump(self.channelscores[channel], outputfile) outputfile.close() - # times + # times outputfile = open(self.path.dirize(self.fileprefix + channel + ".times"), "wb") pickle.dump(self.channeltimes[channel], outputfile) outputfile.close() - # worst times + # worst times outputfile = open(self.path.dirize(self.fileprefix + channel + ".worsttimes"), "wb") pickle.dump(self.channelworsttimes[channel], outputfile) outputfile.close() - # week scores + # week scores outputfile = open(self.path.dirize(self.fileprefix + channel + self.year + ".weekscores"), "wb") pickle.dump(self.channelweek[channel], outputfile) outputfile.close() @@ -177,111 +177,111 @@ class DuckHunt(callbacks.Plugin): def _read_scores(self, channel): - """ - Reads scores and times from disk - """ - filename = self.path.dirize(self.fileprefix + channel) - # scores - if not self.channelscores.get(channel): - if os.path.isfile(filename + ".scores"): - inputfile = open(filename + ".scores", "rb") - self.channelscores[channel] = pickle.load(inputfile) - inputfile.close() + """ + Reads scores and times from disk + """ + filename = self.path.dirize(self.fileprefix + channel) + # scores + if not self.channelscores.get(channel): + if os.path.isfile(filename + ".scores"): + inputfile = open(filename + ".scores", "rb") + self.channelscores[channel] = pickle.load(inputfile) + inputfile.close() - # times - if not self.channeltimes.get(channel): - if os.path.isfile(filename + ".times"): - inputfile = open(filename + ".times", "rb") - self.channeltimes[channel] = pickle.load(inputfile) - inputfile.close() + # times + if not self.channeltimes.get(channel): + if os.path.isfile(filename + ".times"): + inputfile = open(filename + ".times", "rb") + self.channeltimes[channel] = pickle.load(inputfile) + inputfile.close() - # worst times - if not self.channelworsttimes.get(channel): - if os.path.isfile(filename + ".worsttimes"): - inputfile = open(filename + ".worsttimes", "rb") - self.channelworsttimes[channel] = pickle.load(inputfile) - inputfile.close() + # worst times + if not self.channelworsttimes.get(channel): + if os.path.isfile(filename + ".worsttimes"): + inputfile = open(filename + ".worsttimes", "rb") + self.channelworsttimes[channel] = pickle.load(inputfile) + inputfile.close() - # week scores - if not self.channelweek.get(channel): - if os.path.isfile(filename + self.year + ".weekscores"): - inputfile = open(filename + self.year + ".weekscores", "rb") - self.channelweek[channel] = pickle.load(inputfile) - inputfile.close() + # week scores + if not self.channelweek.get(channel): + if os.path.isfile(filename + self.year + ".weekscores"): + inputfile = open(filename + self.year + ".weekscores", "rb") + self.channelweek[channel] = pickle.load(inputfile) + inputfile.close() def _initdayweekyear(self, channel): - self.dow = int(time.strftime("%u")) # Day of week - self.woy = int(time.strftime("%V")) # Week of year - year = time.strftime("%Y") + self.dow = int(time.strftime("%u")) # Day of week + self.woy = int(time.strftime("%V")) # Week of year + year = time.strftime("%Y") - # Init week scores - try: - self.channelweek[channel] - except: - self.channelweek[channel] = {} - try: - self.channelweek[channel][self.woy] - except: - self.channelweek[channel][self.woy] = {} - try: - self.channelweek[channel][self.woy][self.dow] - except: - self.channelweek[channel][self.woy][self.dow] = {} + # Init week scores + try: + self.channelweek[channel] + except: + self.channelweek[channel] = {} + try: + self.channelweek[channel][self.woy] + except: + self.channelweek[channel][self.woy] = {} + try: + self.channelweek[channel][self.woy][self.dow] + except: + self.channelweek[channel][self.woy][self.dow] = {} def _initthrottle(self, irc, msg, args, channel): - self._initdayweekyear(channel) - - if not self.leader.get(channel): - self.leader[channel] = None + self._initdayweekyear(channel) + + if not self.leader.get(channel): + self.leader[channel] = None - # autoFriday? - if (not self.fridayMode.get(channel)): - self.fridayMode[channel] = False + # autoFriday? + if (not self.fridayMode.get(channel)): + self.fridayMode[channel] = False - if (not self.manualFriday.get(channel)): - self.manualFriday[channel] = False + if (not self.manualFriday.get(channel)): + self.manualFriday[channel] = False - if self.registryValue('autoFriday', channel) == True: - if int(time.strftime("%w")) == 5 and int(time.strftime("%H")) > 8 and int(time.strftime("%H")) < 17: - self.fridayMode[channel] = True - else: - self.fridayMode[channel] = False + if self.registryValue('autoFriday', channel) == True: + if int(time.strftime("%w")) == 5 and int(time.strftime("%H")) > 8 and int(time.strftime("%H")) < 17: + self.fridayMode[channel] = True + else: + self.fridayMode[channel] = False - # Miss probability - if self.registryValue('missProbability', channel): - self.missprobability[channel] = self.registryValue('missProbability', channel) - else: - self.missprobability[channel] = 0.2 + # Miss probability + if self.registryValue('missProbability', channel): + self.missprobability[channel] = self.registryValue('missProbability', channel) + else: + self.missprobability[channel] = 0.2 - # Reload time - if self.registryValue('reloadTime', channel): - self.reloadtime[channel] = self.registryValue('reloadTime', channel) - else: - self.reloadtime[channel] = 5 + # Reload time + if self.registryValue('reloadTime', channel): + self.reloadtime[channel] = self.registryValue('reloadTime', channel) + else: + self.reloadtime[channel] = 5 - if self.fridayMode[channel] == False and self.manualFriday[channel] == False: - # Init min throttle[currentChannel] and max throttle[currentChannel] - if self.registryValue('minthrottle', channel): - self.minthrottle[channel] = self.registryValue('minthrottle', channel) - else: - self.minthrottle[channel] = 30 + if self.fridayMode[channel] == False and self.manualFriday[channel] == False: + # Init min throttle[currentChannel] and max throttle[currentChannel] + if self.registryValue('minthrottle', channel): + self.minthrottle[channel] = self.registryValue('minthrottle', channel) + else: + self.minthrottle[channel] = 30 - if self.registryValue('maxthrottle', channel): - self.maxthrottle[channel] = self.registryValue('maxthrottle', channel) - else: - self.maxthrottle[channel] = 300 + if self.registryValue('maxthrottle', channel): + self.maxthrottle[channel] = self.registryValue('maxthrottle', channel) + else: + self.maxthrottle[channel] = 300 - else: - self.minthrottle[channel] = 3 - self.maxthrottle[channel] = 60 + else: + self.minthrottle[channel] = 3 + self.maxthrottle[channel] = 60 - self.throttle[channel] = random.randint(self.minthrottle[channel], self.maxthrottle[channel]) + self.throttle[channel] = random.randint(self.minthrottle[channel], self.maxthrottle[channel]) def start(self, irc, msg, args): @@ -289,95 +289,95 @@ class DuckHunt(callbacks.Plugin): Starts the hunt """ - currentChannel = msg.args[0] - if irc.isChannel(currentChannel): + currentChannel = msg.args[0] + if irc.isChannel(currentChannel): - if(self.started.get(currentChannel) == True): - irc.reply("There is already a hunt right now!") - else: + if(self.started.get(currentChannel) == True): + irc.reply("There is already a hunt right now!") + else: - # First of all, let's read the score if needed - self._read_scores(currentChannel) + # First of all, let's read the score if needed + self._read_scores(currentChannel) - self._initthrottle(irc, msg, args, currentChannel) + self._initthrottle(irc, msg, args, currentChannel) - # Init saved scores - try: - self.channelscores[currentChannel] - except: - self.channelscores[currentChannel] = {} + # Init saved scores + try: + self.channelscores[currentChannel] + except: + self.channelscores[currentChannel] = {} - # Init saved times - try: - self.channeltimes[currentChannel] - except: - self.channeltimes[currentChannel] = {} + # Init saved times + try: + self.channeltimes[currentChannel] + except: + self.channeltimes[currentChannel] = {} - # Init saved times - try: - self.channelworsttimes[currentChannel] - except: - self.channelworsttimes[currentChannel] = {} + # Init saved times + try: + self.channelworsttimes[currentChannel] + except: + self.channelworsttimes[currentChannel] = {} - # Init times - self.toptimes[currentChannel] = {} - self.worsttimes[currentChannel] = {} + # Init times + self.toptimes[currentChannel] = {} + self.worsttimes[currentChannel] = {} - # Init bangdelay - self.times[currentChannel] = False + # Init bangdelay + self.times[currentChannel] = False - # Init lastSpoke - self.lastSpoke[currentChannel] = time.time() + # Init lastSpoke + self.lastSpoke[currentChannel] = time.time() - # Reinit current hunt scores - if self.scores.get(currentChannel): - self.scores[currentChannel] = {} + # Reinit current hunt scores + if self.scores.get(currentChannel): + self.scores[currentChannel] = {} - # Reinit reloading - self.reloading[currentChannel] = {} + # Reinit reloading + self.reloading[currentChannel] = {} - # No duck launched - self.duck[currentChannel] = False + # No duck launched + self.duck[currentChannel] = False - # Hunt started - self.started[currentChannel] = True + # Hunt started + self.started[currentChannel] = True - # Init shoots - self.shoots[currentChannel] = 0 + # Init shoots + self.shoots[currentChannel] = 0 - # Init averagetime - self.averagetime[currentChannel] = 0; + # Init averagetime + self.averagetime[currentChannel] = 0; - # Init schedule + # Init schedule - # First of all, stop the scheduler if it was still running - try: - schedule.removeEvent('DuckHunt_' + currentChannel) - except KeyError: - pass + # First of all, stop the scheduler if it was still running + try: + schedule.removeEvent('DuckHunt_' + currentChannel) + except KeyError: + pass - # Then restart it - def myEventCaller(): - self._launchEvent(irc, msg) - try: - schedule.addPeriodicEvent(myEventCaller, 5, 'DuckHunt_' + currentChannel, False) - except AssertionError: - pass + # Then restart it + def myEventCaller(): + self._launchEvent(irc, msg) + try: + schedule.addPeriodicEvent(myEventCaller, 5, 'DuckHunt_' + currentChannel, False) + except AssertionError: + pass - irc.reply("The hunt starts now!") - else: - irc.error('You have to be on a channel') + irc.reply("The hunt starts now!") + else: + irc.error('You have to be on a channel') start = wrap(start) def _launchEvent(self, irc, msg): - currentChannel = msg.args[0] - now = time.time() - if irc.isChannel(currentChannel): - if(self.started.get(currentChannel) == True): - if (self.duck[currentChannel] == False): - if now > self.lastSpoke[currentChannel] + self.throttle[currentChannel]: - self._launch(irc, msg, '') + currentChannel = msg.args[0] + now = time.time() + if irc.isChannel(currentChannel): + if(self.started.get(currentChannel) == True): + if (self.duck[currentChannel] == False): + if now > self.lastSpoke[currentChannel] + self.throttle[currentChannel]: + self._launch(irc, msg, '') @@ -386,44 +386,44 @@ class DuckHunt(callbacks.Plugin): Stops the current hunt """ - currentChannel = msg.args[0] - if irc.isChannel(currentChannel): - if (self.started.get(currentChannel) == True): - self._end(irc, msg, args) + currentChannel = msg.args[0] + if irc.isChannel(currentChannel): + if (self.started.get(currentChannel) == True): + self._end(irc, msg, args) - # If someone uses the stop command, - # we stop the scheduler, even if autoRestart is enabled - try: - schedule.removeEvent('DuckHunt_' + currentChannel) - except KeyError: - irc.reply('Error: the spammer wasn\'t running! This is a bug.') - else: - irc.reply('Nothing to stop: there\'s no hunt right now.') - else: - irc.error('You have to be on a channel') + # If someone uses the stop command, + # we stop the scheduler, even if autoRestart is enabled + try: + schedule.removeEvent('DuckHunt_' + currentChannel) + except KeyError: + irc.reply('Error: the spammer wasn\'t running! This is a bug.') + else: + irc.reply('Nothing to stop: there\'s no hunt right now.') + else: + irc.error('You have to be on a channel') stop = wrap(stop) def fridaymode(self, irc, msg, args, channel, status): - """ - [] - Enable/disable friday mode! (there are lots of ducks on friday :)) - """ - if irc.isChannel(channel): + """ + [] + Enable/disable friday mode! (there are lots of ducks on friday :)) + """ + if irc.isChannel(channel): - if (status == 'status'): - irc.reply('Manual friday mode for ' + channel + ' is ' + str(self.manualFriday.get(channel))); - irc.reply('Auto friday mode for ' + channel + ' is ' + str(self.fridayMode.get(channel))); - else: - if (self.manualFriday.get(channel) == None or self.manualFriday[channel] == False): - self.manualFriday[channel] = True - irc.reply("Friday mode is now enabled! Shoot alllllllllllll the ducks!") - else: - self.manualFriday[channel] = False - irc.reply("Friday mode is now disabled.") + if (status == 'status'): + irc.reply('Manual friday mode for ' + channel + ' is ' + str(self.manualFriday.get(channel))); + irc.reply('Auto friday mode for ' + channel + ' is ' + str(self.fridayMode.get(channel))); + else: + if (self.manualFriday.get(channel) == None or self.manualFriday[channel] == False): + self.manualFriday[channel] = True + irc.reply("Friday mode is now enabled! Shoot alllllllllllll the ducks!") + else: + self.manualFriday[channel] = False + irc.reply("Friday mode is now disabled.") - self._initthrottle(irc, msg, args, channel) - else: - irc.error('You have to be on a channel') + self._initthrottle(irc, msg, args, channel) + else: + irc.error('You have to be on a channel') fridaymode = wrap(fridaymode, ['channel', 'admin', optional('anything')]) @@ -433,88 +433,88 @@ class DuckHunt(callbacks.Plugin): Is there a duck right now? """ - currentChannel = msg.args[0] - if irc.isChannel(currentChannel): - if(self.started.get(currentChannel) == True): - if(self.duck[currentChannel] == True): - irc.reply("There is currently a duck! You can shoot it with the 'bang' command") - else: - irc.reply("There is no duck right now! Wait for one to be launched!") - else: - irc.reply("There is no hunt right now! You can start a hunt with the 'start' command") - else: - irc.error('You have to be on a channel') + currentChannel = msg.args[0] + if irc.isChannel(currentChannel): + if(self.started.get(currentChannel) == True): + if(self.duck[currentChannel] == True): + irc.reply("There is currently a duck! You can shoot it with the 'bang' command") + else: + irc.reply("There is no duck right now! Wait for one to be launched!") + else: + irc.reply("There is no hunt right now! You can start a hunt with the 'start' command") + else: + irc.error('You have to be on a channel') launched = wrap(launched) def score(self, irc, msg, args, nick): - """ - + """ + - Shows the score for a given nick - """ - currentChannel = msg.args[0] - if irc.isChannel(currentChannel): - self._read_scores(currentChannel) - try: - self.channelscores[currentChannel] - except: - self.channelscores[currentChannel] = {} + Shows the score for a given nick + """ + currentChannel = msg.args[0] + if irc.isChannel(currentChannel): + self._read_scores(currentChannel) + try: + self.channelscores[currentChannel] + except: + self.channelscores[currentChannel] = {} - try: - irc.reply(self.channelscores[currentChannel][nick]) - except: - irc.reply("There is no score for %s on %s" % (nick, currentChannel)) - else: - irc.error('You have to be on a channel') + try: + irc.reply(self.channelscores[currentChannel][nick]) + except: + irc.reply("There is no score for %s on %s" % (nick, currentChannel)) + else: + irc.error('You have to be on a channel') score = wrap(score, ['nick']) def mergescores(self, irc, msg, args, channel, nickto, nickfrom): - """ - [] - - nickto gets the points of nickfrom and nickfrom is removed from the scorelist - """ - if irc.isChannel(channel): - self._read_scores(channel) + """ + [] + + nickto gets the points of nickfrom and nickfrom is removed from the scorelist + """ + if irc.isChannel(channel): + self._read_scores(channel) - # Total scores - try: - self.channelscores[channel][nickto] += self.channelscores[channel][nickfrom] - del self.channelscores[channel][nickfrom] - self._write_scores(channel) - irc.reply("Total scores merged") + # Total scores + try: + self.channelscores[channel][nickto] += self.channelscores[channel][nickfrom] + del self.channelscores[channel][nickfrom] + self._write_scores(channel) + irc.reply("Total scores merged") - except: - irc.error("Can't merge total scores") + except: + irc.error("Can't merge total scores") - # Day scores - try: - self._initdayweekyear(channel) - day = self.dow - week = self.woy + # Day scores + try: + self._initdayweekyear(channel) + day = self.dow + week = self.woy - try: - self.channelweek[channel][week][day][nickto] += self.channelweek[channel][week][day][nickfrom] - except: - self.channelweek[channel][week][day][nickto] = self.channelweek[channel][week][day][nickfrom] + try: + self.channelweek[channel][week][day][nickto] += self.channelweek[channel][week][day][nickfrom] + except: + self.channelweek[channel][week][day][nickto] = self.channelweek[channel][week][day][nickfrom] - del self.channelweek[channel][week][day][nickfrom] - self._write_scores(channel) - irc.reply("Day scores merged") + del self.channelweek[channel][week][day][nickfrom] + self._write_scores(channel) + irc.reply("Day scores merged") - except: - irc.error("Can't merge day scores") + except: + irc.error("Can't merge day scores") - else: - irc.error('You have to be on a channel') + else: + irc.error('You have to be on a channel') mergescores = wrap(mergescores, ['channel', 'nick', 'nick', 'admin']) @@ -522,35 +522,35 @@ class DuckHunt(callbacks.Plugin): def mergetimes(self, irc, msg, args, channel, nickto, nickfrom): - """ - [] - - nickto gets the best time of nickfrom if nickfrom time is better than nickto time, and nickfrom is removed from the timelist. Also works with worst times. - """ - if irc.isChannel(channel): - try: - self._read_scores(channel) + """ + [] + + nickto gets the best time of nickfrom if nickfrom time is better than nickto time, and nickfrom is removed from the timelist. Also works with worst times. + """ + if irc.isChannel(channel): + try: + self._read_scores(channel) - # Merge best times - if self.channeltimes[channel][nickfrom] < self.channeltimes[channel][nickto]: - self.channeltimes[channel][nickto] = self.channeltimes[channel][nickfrom] - del self.channeltimes[channel][nickfrom] + # Merge best times + if self.channeltimes[channel][nickfrom] < self.channeltimes[channel][nickto]: + self.channeltimes[channel][nickto] = self.channeltimes[channel][nickfrom] + del self.channeltimes[channel][nickfrom] - # Merge worst times - if self.channelworsttimes[channel][nickfrom] > self.channelworsttimes[channel][nickto]: - self.channelworsttimes[channel][nickto] = self.channelworsttimes[channel][nickfrom] - del self.channelworsttimes[channel][nickfrom] + # Merge worst times + if self.channelworsttimes[channel][nickfrom] > self.channelworsttimes[channel][nickto]: + self.channelworsttimes[channel][nickto] = self.channelworsttimes[channel][nickfrom] + del self.channelworsttimes[channel][nickfrom] - self._write_scores(channel) + self._write_scores(channel) - irc.replySuccess() + irc.replySuccess() - except: - irc.replyError() + except: + irc.replyError() - else: - irc.error('You have to be on a channel') + else: + irc.error('You have to be on a channel') mergetimes = wrap(mergetimes, ['channel', 'nick', 'nick', 'admin']) @@ -558,42 +558,42 @@ class DuckHunt(callbacks.Plugin): def rmtime(self, irc, msg, args, channel, nick): - """ - [] - - Remove 's best time - """ - if irc.isChannel(channel): - self._read_scores(channel) - del self.channeltimes[channel][nick] - self._write_scores(channel) - irc.replySuccess() + """ + [] + + Remove 's best time + """ + if irc.isChannel(channel): + self._read_scores(channel) + del self.channeltimes[channel][nick] + self._write_scores(channel) + irc.replySuccess() - else: - irc.error('Are you sure ' + str(channel) + ' is a channel?') + else: + irc.error('Are you sure ' + str(channel) + ' is a channel?') rmtime = wrap(rmtime, ['channel', 'nick', 'admin']) def rmscore(self, irc, msg, args, channel, nick): - """ - [] - - Remove 's score - """ - if irc.isChannel(channel): - try: - self._read_scores(channel) - del self.channelscores[channel][nick] - self._write_scores(channel) - irc.replySuccess() + """ + [] + + Remove 's score + """ + if irc.isChannel(channel): + try: + self._read_scores(channel) + del self.channelscores[channel][nick] + self._write_scores(channel) + irc.replySuccess() - except: - irc.replyError() + except: + irc.replyError() - else: - irc.error('Are you sure this is a channel?') + else: + irc.error('Are you sure this is a channel?') rmscore = wrap(rmscore, ['channel', 'nick', 'admin']) @@ -602,248 +602,246 @@ class DuckHunt(callbacks.Plugin): def dayscores(self, irc, msg, args, channel): """ - [] - - Shows the score list of the day for . - """ + [] + + Shows the score list of the day for . + """ - if irc.isChannel(channel): + if irc.isChannel(channel): - self._read_scores(channel) - self._initdayweekyear(channel) - day = self.dow - week = self.woy + self._read_scores(channel) + self._initdayweekyear(channel) + day = self.dow + week = self.woy - if self.channelweek.get(channel): - if self.channelweek[channel].get(week): - if self.channelweek[channel][week].get(day): - # Getting all scores, to get the winner of the week - msgstring = '' - scores = sorted(self.channelweek[channel][week][day].iteritems(), key=lambda (k,v):(v,k), reverse=True) - for item in scores: - msgstring += "x" + item[0] + "x: "+ str(item[1]) + " | " + if self.channelweek.get(channel): + if self.channelweek[channel].get(week): + if self.channelweek[channel][week].get(day): + # Getting all scores, to get the winner of the week + msgstring = '' + scores = sorted(iter(self.channelweek[channel][week][day].items()), key=lambda k_v2:(k_v2[1],k_v2[0]), reverse=True) + for item in scores: + msgstring += "x" + item[0] + "x: "+ str(item[1]) + " | " - if msgstring != "": - irc.reply("Scores for today: " + msgstring) - else: - irc.reply("There aren't any day scores for today yet.") - else: - irc.reply("There aren't any day scores for today yet.") - else: - irc.reply("There aren't any day scores for today yet.") - else: - irc.reply("There aren't any day scores for this channel yet.") - else: - irc.reply("Are you sure this is a channel?") + if msgstring != "": + irc.reply("Scores for today: " + msgstring) + else: + irc.reply("There aren't any day scores for today yet.") + else: + irc.reply("There aren't any day scores for today yet.") + else: + irc.reply("There aren't any day scores for today yet.") + else: + irc.reply("There aren't any day scores for this channel yet.") + else: + irc.reply("Are you sure this is a channel?") dayscores = wrap(dayscores, ['channel']) def weekscores(self, irc, msg, args, week, nick, channel): """ - [] [] [] - - Shows the score list of the week for . If is provided, it will only show 's scores. - """ + [] [] [] + + Shows the score list of the week for . If is provided, it will only show 's scores. + """ - if irc.isChannel(channel): + if irc.isChannel(channel): - self._read_scores(channel) - weekscores = {} + self._read_scores(channel) + weekscores = {} - if (not week): - week = self.woy + if (not week): + week = self.woy - if self.channelweek.get(channel): - if self.channelweek[channel].get(week): - # Showing the winner for each day - if not nick: - msgstring = '' - # for each day of week - for i in (1,2,3,4,5,6,7): - if self.channelweek[channel][week].get(i): - # Getting winner of the day - winnernick, winnerscore = max(self.channelweek[channel][week][i].iteritems(), key=lambda (k,v):(v,k)) - msgstring += self.dayname[i - 1] + ": x" + winnernick + "x ("+ str(winnerscore) + ") | " + if self.channelweek.get(channel): + if self.channelweek[channel].get(week): + # Showing the winner for each day + if not nick: + msgstring = '' + # for each day of week + for i in (1,2,3,4,5,6,7): + if self.channelweek[channel][week].get(i): + # Getting winner of the day + winnernick, winnerscore = max(iter(self.channelweek[channel][week][i].items()), key=lambda k_v:(k_v[1],k_v[0])) + msgstring += self.dayname[i - 1] + ": x" + winnernick + "x ("+ str(winnerscore) + ") | " - # Getting all scores, to get the winner of the week - for player in self.channelweek[channel][week][i].keys(): - try: - weekscores[player] += self.channelweek[channel][week][i][player] - except: - weekscores[player] = self.channelweek[channel][week][i][player] - + # Getting all scores, to get the winner of the week + for player in list(self.channelweek[channel][week][i].keys()): + try: + weekscores[player] += self.channelweek[channel][week][i][player] + except: + weekscores[player] = self.channelweek[channel][week][i][player] + + + if msgstring != "": + irc.reply("Scores for week " + str(week) + ": " + msgstring) + # Who's the winner at this point? + winnernick, winnerscore = max(iter(weekscores.items()), key=lambda k_v1:(k_v1[1],k_v1[0])) + irc.reply("Leader: x%sx with %i points." % (winnernick, winnerscore)) + + else: + irc.reply("There aren't any week scores for this week yet.") + else: + # Showing the scores of + msgstring = '' + total = 0 + for i in (1,2,3,4,5,6,7): + if self.channelweek[channel][week].get(i): + if self.channelweek[channel][week][i].get(nick): + msgstring += self.dayname[i - 1] + ": "+ str(self.channelweek[channel][week][i].get(nick)) + " | " + total += self.channelweek[channel][week][i].get(nick) + + if msgstring != "": + irc.reply(nick + " scores for week " + str(self.woy) + ": " + msgstring) + irc.reply("Total: " + str(total) + " points.") + else: + irc.reply("There aren't any week scores for this nick.") - if msgstring != "": - irc.reply("Scores for week " + str(week) + ": " + msgstring) - # Who's the winner at this point? - winnernick, winnerscore = max(weekscores.iteritems(), key=lambda (k,v):(v,k)) - irc.reply("Leader: x%sx with %i points." % (winnernick, winnerscore)) - - else: - irc.reply("There aren't any week scores for this week yet.") - else: - # Showing the scores of - msgstring = '' - total = 0 - for i in (1,2,3,4,5,6,7): - if self.channelweek[channel][week].get(i): - if self.channelweek[channel][week][i].get(nick): - msgstring += self.dayname[i - 1] + ": "+ str(self.channelweek[channel][week][i].get(nick)) + " | " - total += self.channelweek[channel][week][i].get(nick) - - if msgstring != "": - irc.reply(nick + " scores for week " + str(self.woy) + ": " + msgstring) - irc.reply("Total: " + str(total) + " points.") - else: - irc.reply("There aren't any week scores for this nick.") - - - else: - irc.reply("There aren't any week scores for this week yet.") - else: - irc.reply("There aren't any week scores for this channel yet.") - else: - irc.reply("Are you sure this is a channel?") + else: + irc.reply("There aren't any week scores for this week yet.") + else: + irc.reply("There aren't any week scores for this channel yet.") + else: + irc.reply("Are you sure this is a channel?") weekscores = wrap(weekscores, [optional('int'), optional('nick'), 'channel']) def listscores(self, irc, msg, args, size, channel): """ - [] [] - - Shows the -sized score list for (or for the current channel if no channel is given) - """ + [] [] + + Shows the -sized score list for (or for the current channel if no channel is given) + """ - if irc.isChannel(channel): - try: - self.channelscores[channel] - except: - self.channelscores[channel] = {} + if irc.isChannel(channel): + try: + self.channelscores[channel] + except: + self.channelscores[channel] = {} - self._read_scores(channel) + self._read_scores(channel) - # How many results do we display? - if (not size): - listsize = self.toplist - else: - listsize = size + # How many results do we display? + if (not size): + listsize = self.toplist + else: + listsize = size - # Sort the scores (reversed: the higher the better) - scores = sorted(self.channelscores[channel].iteritems(), key=lambda (k,v):(v,k), reverse=True) - del scores[listsize:] + # Sort the scores (reversed: the higher the better) + scores = sorted(iter(self.channelscores[channel].items()), key=lambda k_v9:(k_v9[1],k_v9[0]), reverse=True) + del scores[listsize:] - msgstring = "" - for item in scores: - # Why do we show the nicks as xnickx? - # Just to prevent everyone that has ever played a hunt in the channel to be pinged every time anyone asks for the score list - msgstring += "x" + item[0] + "x: "+ str(item[1]) + " | " - if msgstring != "": - irc.reply("\_o< ~ DuckHunt top-" + str(listsize) + " scores for " + channel + " ~ >o_/") - irc.reply(msgstring) - else: - irc.reply("There aren't any scores for this channel yet.") - else: - irc.reply("Are you sure this is a channel?") + msgstring = "" + for item in scores: + # Why do we show the nicks as xnickx? + # Just to prevent everyone that has ever played a hunt in the channel to be pinged every time anyone asks for the score list + msgstring += "x" + item[0] + "x: "+ str(item[1]) + " | " + if msgstring != "": + irc.reply("\_o< ~ DuckHunt top-" + str(listsize) + " scores for " + channel + " ~ >o_/") + irc.reply(msgstring) + else: + irc.reply("There aren't any scores for this channel yet.") + else: + irc.reply("Are you sure this is a channel?") listscores = wrap(listscores, [optional('int'), 'channel']) def total(self, irc, msg, args, channel): - """ - Shows the total amount of ducks shot in (or in the current channel if no channel is given) - """ + """ + Shows the total amount of ducks shot in (or in the current channel if no channel is given) + """ - if irc.isChannel(channel): - self._read_scores(channel) - if (self.channelscores.get(channel)): - scores = self.channelscores[channel] - total = 0 - for player in scores.keys(): - total += scores[player] - irc.reply(str(total) + " ducks have been shot in " + channel + "!") - else: - irc.reply("There are no scores for this channel yet") + if irc.isChannel(channel): + self._read_scores(channel) + if (self.channelscores.get(channel)): + scores = self.channelscores[channel] + total = 0 + for player in list(scores.keys()): + total += scores[player] + irc.reply(str(total) + " ducks have been shot in " + channel + "!") + else: + irc.reply("There are no scores for this channel yet") - else: - irc.reply("Are you sure this is a channel?") + else: + irc.reply("Are you sure this is a channel?") total = wrap(total, ['channel']) def listtimes(self, irc, msg, args, size, channel): """ - [] [] - - Shows the -sized time list for (or for the current channel if no channel is given) - """ + [] [] + + Shows the -sized time list for (or for the current channel if no channel is given) + """ - if irc.isChannel(channel): - self._read_scores(channel) + if irc.isChannel(channel): + self._read_scores(channel) - try: - self.channeltimes[channel] - except: - self.channeltimes[channel] = {} + try: + self.channeltimes[channel] + except: + self.channeltimes[channel] = {} - try: - self.channelworsttimes[channel] - except: - self.channelworsttimes[channel] = {} + try: + self.channelworsttimes[channel] + except: + self.channelworsttimes[channel] = {} - # How many results do we display? - if (not size): - listsize = self.toplist - else: - listsize = size + # How many results do we display? + if (not size): + listsize = self.toplist + else: + listsize = size - # Sort the times (not reversed: the lower the better) - times = sorted(self.channeltimes[channel].iteritems(), key=lambda (k,v):(v,k), reverse=False) - del times[listsize:] + # Sort the times (not reversed: the lower the better) + times = sorted(iter(self.channeltimes[channel].items()), key=lambda k_v10:(k_v10[1],k_v10[0]), reverse=False) + del times[listsize:] - msgstring = "" - for item in times: - # Same as in listscores for the xnickx - msgstring += "x" + item[0] + "x: "+ str(round(item[1],2)) + " | " - if msgstring != "": - irc.reply("\_o< ~ DuckHunt top-" + str(listsize) + " times for " + channel + " ~ >o_/") - irc.reply(msgstring) - else: - irc.reply("There aren't any best times for this channel yet.") + msgstring = "" + for item in times: + # Same as in listscores for the xnickx + msgstring += "x" + item[0] + "x: "+ str(round(item[1],2)) + " | " + if msgstring != "": + irc.reply("\_o< ~ DuckHunt top-" + str(listsize) + " times for " + channel + " ~ >o_/") + irc.reply(msgstring) + else: + irc.reply("There aren't any best times for this channel yet.") - times = sorted(self.channelworsttimes[channel].iteritems(), key=lambda (k,v):(v,k), reverse=True) - del times[listsize:] + times = sorted(iter(self.channelworsttimes[channel].items()), key=lambda k_v11:(k_v11[1],k_v11[0]), reverse=True) + del times[listsize:] - msgstring = "" - for item in times: - # Same as in listscores for the xnickx - #msgstring += "x" + item[0] + "x: "+ time.strftime('%H:%M:%S', time.gmtime(item[1])) + ", " - roundseconds = round(item[1]) - delta = datetime.timedelta(seconds=roundseconds) - msgstring += "x" + item[0] + "x: " + str(delta) + " | " - if msgstring != "": - irc.reply("\_o< ~ DuckHunt top-" + str(listsize) + " longest times for " + channel + " ~ >o_/") - irc.reply(msgstring) - else: - irc.reply("There aren't any longest times for this channel yet.") + msgstring = "" + for item in times: + # Same as in listscores for the xnickx + #msgstring += "x" + item[0] + "x: "+ time.strftime('%H:%M:%S', time.gmtime(item[1])) + ", " + roundseconds = round(item[1]) + delta = datetime.timedelta(seconds=roundseconds) + msgstring += "x" + item[0] + "x: " + str(delta) + " | " + if msgstring != "": + irc.reply("\_o< ~ DuckHunt top-" + str(listsize) + " longest times for " + channel + " ~ >o_/") + irc.reply(msgstring) + else: + irc.reply("There aren't any longest times for this channel yet.") - - else: - irc.reply("Are you sure this is a channel?") + else: + irc.reply("Are you sure this is a channel?") listtimes = wrap(listtimes, [optional('int'), 'channel']) def dbg(self, irc, msg, args): - """ - This is a debug command. If debug mode is not enabled, it won't do anything - """ - currentChannel = msg.args[0] - if (self.debug): - if irc.isChannel(currentChannel): - self._launch(irc, msg, '') + """ + This is a debug command. If debug mode is not enabled, it won't do anything + """ + currentChannel = msg.args[0] + if (self.debug): + if irc.isChannel(currentChannel): + self._launch(irc, msg, '') dbg = wrap(dbg) @@ -854,309 +852,324 @@ class DuckHunt(callbacks.Plugin): """ currentChannel = msg.args[0] - if irc.isChannel(currentChannel): - if(self.started.get(currentChannel) == True): + if irc.isChannel(currentChannel): + if(self.started.get(currentChannel) == True): - # bangdelay: how much time between the duck was launched and this shot? - if self.times[currentChannel]: - bangdelay = time.time() - self.times[currentChannel] - else: - bangdelay = False + # bangdelay: how much time between the duck was launched and this shot? + if self.times[currentChannel]: + bangdelay = time.time() - self.times[currentChannel] + else: + bangdelay = False - # Is the player reloading? - if (self.reloading[currentChannel].get(msg.nick) and time.time() - self.reloading[currentChannel][msg.nick] < self.reloadtime[currentChannel]): - irc.reply("%s, you are reloading... (Reloading takes %i seconds)" % (msg.nick, self.reloadtime[currentChannel])) - return 0 - + # Is the player reloading? + if (self.reloading[currentChannel].get(msg.nick) and time.time() - self.reloading[currentChannel][msg.nick] < self.reloadtime[currentChannel]): + irc.reply("%s, you are reloading... (Reloading takes %i seconds)" % (msg.nick, self.reloadtime[currentChannel])) + return 0 + - # This player is now reloading - self.reloading[currentChannel][msg.nick] = time.time(); + # This player is now reloading + self.reloading[currentChannel][msg.nick] = time.time(); - # There was a duck - if (self.duck[currentChannel] == True): + # There was a duck + if (self.duck[currentChannel] == True): - # Did the player missed it? - if (random.random() < self.missprobability[currentChannel]): - irc.reply("%s, you missed the duck!" % (msg.nick)) - else: + # Did the player missed it? + if (random.random() < self.missprobability[currentChannel]): + irc.reply("%s, you missed the duck!" % (msg.nick)) + else: - # Adds one point for the nick that shot the duck - try: - self.scores[currentChannel][msg.nick] += 1 - except: - try: - self.scores[currentChannel][msg.nick] = 1 - except: - self.scores[currentChannel] = {} - self.scores[currentChannel][msg.nick] = 1 + # Adds one point for the nick that shot the duck + try: + self.scores[currentChannel][msg.nick] += 1 + except: + try: + self.scores[currentChannel][msg.nick] = 1 + except: + self.scores[currentChannel] = {} + self.scores[currentChannel][msg.nick] = 1 - irc.reply("\_x< %s: %i (%.2f seconds)" % (msg.nick, self.scores[currentChannel][msg.nick], bangdelay)) + irc.reply("\_x< %s: %i (%.2f seconds)" % (msg.nick, self.scores[currentChannel][msg.nick], bangdelay)) - self.averagetime[currentChannel] += bangdelay + self.averagetime[currentChannel] += bangdelay - # Now save the bang delay for the player (if it's quicker than it's previous bangdelay) - try: - previoustime = self.toptimes[currentChannel][msg.nick] - if(bangdelay < previoustime): - self.toptimes[currentChannel][msg.nick] = bangdelay - except: - self.toptimes[currentChannel][msg.nick] = bangdelay + # Now save the bang delay for the player (if it's quicker than it's previous bangdelay) + try: + previoustime = self.toptimes[currentChannel][msg.nick] + if(bangdelay < previoustime): + self.toptimes[currentChannel][msg.nick] = bangdelay + except: + self.toptimes[currentChannel][msg.nick] = bangdelay - # Now save the bang delay for the player (if it's worst than it's previous bangdelay) - try: - previoustime = self.worsttimes[currentChannel][msg.nick] - if(bangdelay > previoustime): - self.worsttimes[currentChannel][msg.nick] = bangdelay - except: - self.worsttimes[currentChannel][msg.nick] = bangdelay + # Now save the bang delay for the player (if it's worst than it's previous bangdelay) + try: + previoustime = self.worsttimes[currentChannel][msg.nick] + if(bangdelay > previoustime): + self.worsttimes[currentChannel][msg.nick] = bangdelay + except: + self.worsttimes[currentChannel][msg.nick] = bangdelay - self.duck[currentChannel] = False + self.duck[currentChannel] = False - # Reset the basetime for the waiting time before the next duck - self.lastSpoke[currentChannel] = time.time() + # Reset the basetime for the waiting time before the next duck + self.lastSpoke[currentChannel] = time.time() - if self.registryValue('ducks', currentChannel): - maxShoots = self.registryValue('ducks', currentChannel) - else: - maxShoots = 10 + if self.registryValue('ducks', currentChannel): + maxShoots = self.registryValue('ducks', currentChannel) + else: + maxShoots = 10 - # End of Hunt - if (self.shoots[currentChannel] == maxShoots): - self._end(irc, msg, args) + # End of Hunt + if (self.shoots[currentChannel] == maxShoots): + self._end(irc, msg, args) - # If autorestart is enabled, we restart a hunt automatically! - if self.registryValue('autoRestart', currentChannel): - # This code shouldn't be here - self.started[currentChannel] = True - self._initthrottle(irc, msg, args, currentChannel) - if self.scores.get(currentChannel): - self.scores[currentChannel] = {} - if self.reloading.get(currentChannel): - self.reloading[currentChannel] = {} + # If autorestart is enabled, we restart a hunt automatically! + if self.registryValue('autoRestart', currentChannel): + # This code shouldn't be here + self.started[currentChannel] = True + self._initthrottle(irc, msg, args, currentChannel) + if self.scores.get(currentChannel): + self.scores[currentChannel] = {} + if self.reloading.get(currentChannel): + self.reloading[currentChannel] = {} - self.averagetime[currentChannel] = 0 + self.averagetime[currentChannel] = 0 - # There was no duck or the duck has already been shot - else: + # There was no duck or the duck has already been shot + else: - # Removes one point for the nick that shot - try: - self.scores[currentChannel][msg.nick] -= 1 - except: - try: - self.scores[currentChannel][msg.nick] = -1 - except: - self.scores[currentChannel] = {} - self.scores[currentChannel][msg.nick] = -1 + # Removes one point for the nick that shot + try: + self.scores[currentChannel][msg.nick] -= 1 + except: + try: + self.scores[currentChannel][msg.nick] = -1 + except: + self.scores[currentChannel] = {} + self.scores[currentChannel][msg.nick] = -1 - # Base message - message = 'There was no duck!' + # Base message + message = 'There was no duck!' - # Adding additional message if kick - if self.registryValue('kickMode', currentChannel) and irc.nick in irc.state.channels[currentChannel].ops: - message += ' You just shot yourself!' + # Adding additional message if kick + if self.registryValue('kickMode', currentChannel) and irc.nick in irc.state.channels[currentChannel].ops: + message += ' You just shot yourself!' - # Adding nick and score - message += " %s: %i" % (msg.nick, self.scores[currentChannel][msg.nick]) + # Adding nick and score + message += " %s: %i" % (msg.nick, self.scores[currentChannel][msg.nick]) - # If we were able to have a bangdelay (ie: a duck was launched before someone did bang) - if (bangdelay): - # Adding time - message += " (" + str(round(bangdelay,2)) + " seconds)" + # If we were able to have a bangdelay (ie: a duck was launched before someone did bang) + if (bangdelay): + # Adding time + message += " (" + str(round(bangdelay,2)) + " seconds)" - # If kickMode is enabled for this channel, and the bot have op capability, let's kick! - if self.registryValue('kickMode', currentChannel) and irc.nick in irc.state.channels[currentChannel].ops: - irc.queueMsg(ircmsgs.kick(currentChannel, msg.nick, message)) - else: - # Else, just say it - irc.reply(message) + # If kickMode is enabled for this channel, and the bot have op capability, let's kick! + if self.registryValue('kickMode', currentChannel) and irc.nick in irc.state.channels[currentChannel].ops: + irc.queueMsg(ircmsgs.kick(currentChannel, msg.nick, message)) + else: + # Else, just say it + irc.reply(message) - else: - irc.reply("There is no hunt right now! You can start a hunt with the 'start' command") - else: - irc.error('You have to be on a channel') + else: + irc.reply("There is no hunt right now! You can start a hunt with the 'start' command") + else: + irc.error('You have to be on a channel') bang = wrap(bang) + def doPrivmsg(self, irc, msg): + currentChannel = msg.args[0] + if irc.isChannel(msg.args[0]): + if (msg.args[1] == '\_o< quack!'): + message = msg.nick + ", don't pretend to be me!"; + # If kickMode is enabled for this channel, and the bot have op capability, let's kick! + if self.registryValue('kickMode', currentChannel) and irc.nick in irc.state.channels[currentChannel].ops: + irc.queueMsg(ircmsgs.kick(currentChannel, msg.nick, message)) + else: + # Else, just say it + irc.reply(message) + + + def _end(self, irc, msg, args): - """ - End of the hunt (is called when the hunts stop "naturally" or when someone uses the !stop command) - """ + """ + End of the hunt (is called when the hunts stop "naturally" or when someone uses the !stop command) + """ - currentChannel = msg.args[0] + currentChannel = msg.args[0] - # End the hunt - self.started[currentChannel] = False + # End the hunt + self.started[currentChannel] = False - try: - self.channelscores[currentChannel] - except: - self.channelscores[currentChannel] = {} + try: + self.channelscores[currentChannel] + except: + self.channelscores[currentChannel] = {} - if not self.registryValue('autoRestart', currentChannel): - irc.reply("The hunt stops now!") + if not self.registryValue('autoRestart', currentChannel): + irc.reply("The hunt stops now!") - # Showing scores - if (self.scores.get(currentChannel)): + # Showing scores + if (self.scores.get(currentChannel)): - # Getting winner - winnernick, winnerscore = max(self.scores.get(currentChannel).iteritems(), key=lambda (k,v):(v,k)) - if self.registryValue('ducks', currentChannel): - maxShoots = self.registryValue('ducks', currentChannel) - else: - maxShoots = 10 + # Getting winner + winnernick, winnerscore = max(iter(self.scores.get(currentChannel).items()), key=lambda k_v12:(k_v12[1],k_v12[0])) + if self.registryValue('ducks', currentChannel): + maxShoots = self.registryValue('ducks', currentChannel) + else: + maxShoots = 10 - # Is there a perfect? - if (winnerscore == maxShoots): - irc.reply("\o/ %s: %i ducks out of %i: perfect!!! +%i \o/" % (winnernick, winnerscore, maxShoots, self.perfectbonus)) - self.scores[currentChannel][winnernick] += self.perfectbonus - else: - # Showing scores - #irc.reply("Winner: %s with %i points" % (winnernick, winnerscore)) - #irc.reply(self.scores.get(currentChannel)) - #TODO: Better display - irc.reply(sorted(self.scores.get(currentChannel).iteritems(), key=lambda (k,v):(v,k), reverse=True)) + # Is there a perfect? + if (winnerscore == maxShoots): + irc.reply("\o/ %s: %i ducks out of %i: perfect!!! +%i \o/" % (winnernick, winnerscore, maxShoots, self.perfectbonus)) + self.scores[currentChannel][winnernick] += self.perfectbonus + else: + # Showing scores + #irc.reply("Winner: %s with %i points" % (winnernick, winnerscore)) + #irc.reply(self.scores.get(currentChannel)) + #TODO: Better display + irc.reply(sorted(iter(self.scores.get(currentChannel).items()), key=lambda k_v4:(k_v4[1],k_v4[0]), reverse=True)) - # Getting channel best time (to see if the best time of this hunt is better) - channelbestnick = None - channelbesttime = None - if self.channeltimes.get(currentChannel): - channelbestnick, channelbesttime = min(self.channeltimes.get(currentChannel).iteritems(), key=lambda (k,v):(v,k)) + # Getting channel best time (to see if the best time of this hunt is better) + channelbestnick = None + channelbesttime = None + if self.channeltimes.get(currentChannel): + channelbestnick, channelbesttime = min(iter(self.channeltimes.get(currentChannel).items()), key=lambda k_v5:(k_v5[1],k_v5[0])) - # Showing best time - recordmsg = '' - if (self.toptimes.get(currentChannel)): - key,value = min(self.toptimes.get(currentChannel).iteritems(), key=lambda (k,v):(v,k)) - if (channelbesttime and value < channelbesttime): - recordmsg = '. This is the new record for this channel! (previous record was held by ' + channelbestnick + ' with ' + str(round(channelbesttime,2)) + ' seconds)' - else: - try: - if(value < self.channeltimes[currentChannel][key]): - recordmsg = ' (this is your new record in this channel! Your previous record was ' + str(round(self.channeltimes[currentChannel][key],2)) + ')' - except: - recordmsg = '' + # Showing best time + recordmsg = '' + if (self.toptimes.get(currentChannel)): + key,value = min(iter(self.toptimes.get(currentChannel).items()), key=lambda k_v6:(k_v6[1],k_v6[0])) + if (channelbesttime and value < channelbesttime): + recordmsg = '. This is the new record for this channel! (previous record was held by ' + channelbestnick + ' with ' + str(round(channelbesttime,2)) + ' seconds)' + else: + try: + if(value < self.channeltimes[currentChannel][key]): + recordmsg = ' (this is your new record in this channel! Your previous record was ' + str(round(self.channeltimes[currentChannel][key],2)) + ')' + except: + recordmsg = '' - irc.reply("Best time: %s with %.2f seconds%s" % (key, value, recordmsg)) + irc.reply("Best time: %s with %.2f seconds%s" % (key, value, recordmsg)) - # Getting channel worst time (to see if the worst time of this hunt is worst) - channelworstnick = None - channelworsttime = None - if self.channelworsttimes.get(currentChannel): - channelworstnick, channelworsttime = max(self.channelworsttimes.get(currentChannel).iteritems(), key=lambda (k,v):(v,k)) + # Getting channel worst time (to see if the worst time of this hunt is worst) + channelworstnick = None + channelworsttime = None + if self.channelworsttimes.get(currentChannel): + channelworstnick, channelworsttime = max(iter(self.channelworsttimes.get(currentChannel).items()), key=lambda k_v7:(k_v7[1],k_v7[0])) - # Showing worst time - recordmsg = '' - if (self.worsttimes.get(currentChannel)): - key,value = max(self.worsttimes.get(currentChannel).iteritems(), key=lambda (k,v):(v,k)) - if (channelworsttime and value > channelworsttime): - recordmsg = '. This is the new longest time for this channel! (previous longest time was held by ' + channelworstnick + ' with ' + str(round(channelworsttime,2)) + ' seconds)' - else: - try: - if(value > self.channelworsttimes[currentChannel][key]): - recordmsg = ' (this is your new longest time in this channel! Your previous longest time was ' + str(round(self.channelworsttimes[currentChannel][key],2)) + ')' - except: - recordmsg = '' + # Showing worst time + recordmsg = '' + if (self.worsttimes.get(currentChannel)): + key,value = max(iter(self.worsttimes.get(currentChannel).items()), key=lambda k_v8:(k_v8[1],k_v8[0])) + if (channelworsttime and value > channelworsttime): + recordmsg = '. This is the new longest time for this channel! (previous longest time was held by ' + channelworstnick + ' with ' + str(round(channelworsttime,2)) + ' seconds)' + else: + try: + if(value > self.channelworsttimes[currentChannel][key]): + recordmsg = ' (this is your new longest time in this channel! Your previous longest time was ' + str(round(self.channelworsttimes[currentChannel][key],2)) + ')' + except: + recordmsg = '' - # Only display worst time if something new - if (recordmsg != ''): - irc.reply("Longest time: %s with %.2f seconds%s" % (key, value, recordmsg)) + # Only display worst time if something new + if (recordmsg != ''): + irc.reply("Longest time: %s with %.2f seconds%s" % (key, value, recordmsg)) - # Showing average shooting time: - #if (self.shoots[currentChannel] > 1): - #irc.reply("Average shooting time: %.2f seconds" % ((self.averagetime[currentChannel] / self.shoots[currentChannel]))) + # Showing average shooting time: + #if (self.shoots[currentChannel] > 1): + #irc.reply("Average shooting time: %.2f seconds" % ((self.averagetime[currentChannel] / self.shoots[currentChannel]))) - # Write the scores and times to disk - self._calc_scores(currentChannel) - self._write_scores(currentChannel) + # Write the scores and times to disk + self._calc_scores(currentChannel) + self._write_scores(currentChannel) - # Did someone took the lead? - weekscores = {} - if self.channelweek.get(currentChannel): - if self.channelweek[currentChannel].get(self.woy): - msgstring = '' - # for each day of week - for i in (1,2,3,4,5,6,7): - if self.channelweek[currentChannel][self.woy].get(i): - # Getting all scores, to get the winner of the week - for player in self.channelweek[currentChannel][self.woy][i].keys(): - try: - weekscores[player] += self.channelweek[currentChannel][self.woy][i][player] - except: - weekscores[player] = self.channelweek[currentChannel][self.woy][i][player] - winnernick, winnerscore = max(weekscores.iteritems(), key=lambda (k,v):(v,k)) - if (winnernick != self.leader[currentChannel]): - if self.leader[currentChannel] != None: - irc.reply("%s took the lead for the week over %s with %i points." % (winnernick, self.leader[currentChannel], winnerscore)) - else: - irc.reply("%s has the lead for the week with %i points." % (winnernick, winnerscore)) - self.leader[currentChannel] = winnernick + # Did someone took the lead? + weekscores = {} + if self.channelweek.get(currentChannel): + if self.channelweek[currentChannel].get(self.woy): + msgstring = '' + # for each day of week + for i in (1,2,3,4,5,6,7): + if self.channelweek[currentChannel][self.woy].get(i): + # Getting all scores, to get the winner of the week + for player in list(self.channelweek[currentChannel][self.woy][i].keys()): + try: + weekscores[player] += self.channelweek[currentChannel][self.woy][i][player] + except: + weekscores[player] = self.channelweek[currentChannel][self.woy][i][player] + + winnernick, winnerscore = max(iter(weekscores.items()), key=lambda k_v3:(k_v3[1],k_v3[0])) + if (winnernick != self.leader[currentChannel]): + if self.leader[currentChannel] != None: + irc.reply("%s took the lead for the week over %s with %i points." % (winnernick, self.leader[currentChannel], winnerscore)) + else: + irc.reply("%s has the lead for the week with %i points." % (winnernick, winnerscore)) + self.leader[currentChannel] = winnernick - else: - irc.reply("Not a single duck was shot during this hunt!") + else: + irc.reply("Not a single duck was shot during this hunt!") - # Reinit current hunt scores - if self.scores.get(currentChannel): - self.scores[currentChannel] = {} + # Reinit current hunt scores + if self.scores.get(currentChannel): + self.scores[currentChannel] = {} - # Reinit current hunt times - if self.toptimes.get(currentChannel): - self.toptimes[currentChannel] = {} - if self.worsttimes.get(currentChannel): - self.worsttimes[currentChannel] = {} + # Reinit current hunt times + if self.toptimes.get(currentChannel): + self.toptimes[currentChannel] = {} + if self.worsttimes.get(currentChannel): + self.worsttimes[currentChannel] = {} - # No duck lauched - self.duck[currentChannel] = False + # No duck lauched + self.duck[currentChannel] = False - # Reinit number of shoots - self.shoots[currentChannel] = 0 + # Reinit number of shoots + self.shoots[currentChannel] = 0 def _launch(self, irc, msg, args): - """ - Launch a duck - """ - currentChannel = msg.args[0] - if irc.isChannel(currentChannel): - if(self.started[currentChannel] == True): - if (self.duck[currentChannel] == False): + """ + Launch a duck + """ + currentChannel = msg.args[0] + if irc.isChannel(currentChannel): + if(self.started[currentChannel] == True): + if (self.duck[currentChannel] == False): - # Store the time when the duck has been launched - self.times[currentChannel] = time.time() + # Store the time when the duck has been launched + self.times[currentChannel] = time.time() - # Store the fact that there's a duck now - self.duck[currentChannel] = True + # Store the fact that there's a duck now + self.duck[currentChannel] = True - # Send message directly (instead of queuing it with irc.reply) - irc.sendMsg(ircmsgs.privmsg(currentChannel, "\_o< quack!")) + # Send message directly (instead of queuing it with irc.reply) + irc.sendMsg(ircmsgs.privmsg(currentChannel, "\_o< quack!")) - # Define a new throttle[currentChannel] for the next launch - self.throttle[currentChannel] = random.randint(self.minthrottle[currentChannel], self.maxthrottle[currentChannel]) + # Define a new throttle[currentChannel] for the next launch + self.throttle[currentChannel] = random.randint(self.minthrottle[currentChannel], self.maxthrottle[currentChannel]) - try: - self.shoots[currentChannel] += 1 - except: - self.shoots[currentChannel] = 1 - else: + try: + self.shoots[currentChannel] += 1 + except: + self.shoots[currentChannel] = 1 + else: - irc.reply("Already a duck") - else: - irc.reply("The hunt has not started yet!") - else: - irc.error('You have to be on a channel') + irc.reply("Already a duck") + else: + irc.reply("The hunt has not started yet!") + else: + irc.error('You have to be on a channel') Class = DuckHunt