From a13b6b07fa14b52b89cc40bd6dc80bf66aade4b9 Mon Sep 17 00:00:00 2001 From: Andrew Ekstedt Date: Sun, 3 May 2015 18:25:37 -0700 Subject: [PATCH 1/6] Create BLANK constant. --- cah/cah.py | 20 ++++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-) diff --git a/cah/cah.py b/cah/cah.py index 0605931..cd73087 100644 --- a/cah/cah.py +++ b/cah/cah.py @@ -13,6 +13,9 @@ SQLAlchemyBase = declarative_base() +# BLANK is the magic marker used to denote blanks +# in the card descriptions. +BLANK = "_" * 10 class CardsAgainstHumanity(ChatCommandPlugin): """ Play the classic card game Cards Against Humanity """ @@ -262,7 +265,7 @@ def prep_play(self, bot, comm): def colorize(self, txt): - if txt == '_' * 10: + if txt == BLANK: # Returns the light cyan color code return "\x0311" + txt + "\x03" # Returns the light green color code @@ -331,13 +334,14 @@ def format_white(self, card): def init_black(self, card): card = card.strip("\n") if card: - if "__________" not in card: + if BLANK not in card: card += " __________." - return ''.join(map(self.colorize, re.split("(" + '_'*10 + ")", card))) + return ''.join(map(self.colorize, re.split("(" + BLANK + ")", card))) def format_black(self, card): - for x in xrange(card.count("__________")): - card = card.replace("__________", "{" + str(x) + "}", 1) + # FIXME: This is quadratic + for i in xrange(card.count(BLANK)): + card = card.replace(BLANK, "{" + str(i) + "}", 1) return card def show_hand(self, bot, name): @@ -443,7 +447,7 @@ def command(self, bot, comm, groups): num = random.randint(0, len(self.plugin.players[user])) while (num not in indices and groups[1] == 'random' and - len(indices) < self.plugin.prompt.count("_" * 10)): + len(indices) < self.plugin.prompt.count(BLANK)): indices += [num] num = random.randint(0, len(self.plugin.players[user])) @@ -452,7 +456,7 @@ def command(self, bot, comm, groups): return bot.reply(comm, "[*] {0}, you didn't provide hand index(s) for cards!" .format(user)) - if len(indices) != self.plugin.prompt.count("__________"): + if len(indices) != self.plugin.prompt.count(BLANK): return ("[*] {0}, you didn't provide the correct amount" "of cards!".format(user)) @@ -614,7 +618,7 @@ def command(self, bot, comm, groups): elif color == 'black': underscore_re = re.compile('(_+)+') - formatted, num_replacements = underscore_re.subn('_'*10, desc) + formatted, num_replacements = underscore_re.subn(BLANK, desc) if num_replacements == 0 or num_replacements > 3: return bot.reply(comm, "[*] You provided too few or many blanks!") From 4d36f94a0770dff08b6168d0b95f42af3de9dfae Mon Sep 17 00:00:00 2001 From: Andrew Ekstedt Date: Sun, 3 May 2015 18:27:25 -0700 Subject: [PATCH 2/6] Use enumerate instead of xrange. --- cah/cah.py | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/cah/cah.py b/cah/cah.py index cd73087..0ca77a2 100644 --- a/cah/cah.py +++ b/cah/cah.py @@ -287,9 +287,8 @@ def show_top_scores(self, bot, comm, current_players=True): scores_str = '{:^14} {:^14}\n____________________________' bot.reply(comm, scores_str.format('User', 'Score')) scores_str = '{:^14}|{:^14}' - num_top = 5 if len(top) > 4 else len(top) - scores = '\n'.join([scores_str.format(top[x].user, str(top[x].score)) - for x in xrange(num_top)]) + scores = '\n'.join(scores_str.format(x.user, str(x.score)) + for x in top[:5]) bot.reply(comm, scores) def get_score(self, player): @@ -346,8 +345,8 @@ def format_black(self, card): def show_hand(self, bot, name): print "Showing hand for: " + name - cards = '. '.join((str(x + 1) + ": " + self.players[name][x] - for x in xrange(len(self.players[name])))) + cards = '. '.join(str(i + 1) + ": " + card + for i, card in enumerate(self.players[name])) bot.notice(name, "Your hand is: [{0}]".format(cards)) @@ -536,8 +535,8 @@ def command(self, bot, comm, groups): dealer = "Yes" if user == self.plugin.dealer else "No" hand = "None" if user in self.plugin.players: - hand = '. '.join((str(x + 1) + ": " + self.plugin.players[user][x] - for x in xrange(len(self.plugin.players[user])))) + hand = '. '.join(str(i + 1) + ": " + card + for i, card in enumerate(self.plugin.players[user])) # Since we can't print new lines... msgs = zip(msg, [user, score, playing, dealer, hand]) From 6d7f92034dd7eb8e31795c0415d43707a2e2ef63 Mon Sep 17 00:00:00 2001 From: Andrew Ekstedt Date: Sun, 3 May 2015 18:28:20 -0700 Subject: [PATCH 3/6] Remove superfluous key=(lambda x: x) arguments. --- cah/cah.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/cah/cah.py b/cah/cah.py index 0ca77a2..b7dcc7b 100644 --- a/cah/cah.py +++ b/cah/cah.py @@ -272,7 +272,7 @@ def colorize(self, txt): return "\x0309" + txt + "\x03" def get_player_str(self): - return ' '.join(sorted(self.players.keys(), key=lambda x: x)) + return ' '.join(sorted(self.players.keys())) def show_top_scores(self, bot, comm, current_players=True): if current_players: @@ -467,7 +467,7 @@ def command(self, bot, comm, groups): for i in indices] # Don't change index of cards that are being removed.. - for index in reversed(sorted(indices, key=lambda x: x)): + for index in reversed(sorted(indices)): self.plugin.players[user].pop(index - 1) @@ -688,7 +688,7 @@ def command(self, bot, comm, groups): indices = set(map(int, groups[0].split(" "))) # Don't change index of cards that are being removed.. - for index in reversed(sorted(indices, key=lambda x: x)): + for index in reversed(sorted(indices)): exchange = self.plugin.players[user].pop(index - 1) self.plugin.white_discard.append(exchange) From 7ccc983b1e8692a1bd81a29f01062487c0ce2b2a Mon Sep 17 00:00:00 2001 From: Andrew Ekstedt Date: Sun, 3 May 2015 18:28:45 -0700 Subject: [PATCH 4/6] Remove use of filter. Filter has its uses, but this isn't one of them. --- cah/cah.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/cah/cah.py b/cah/cah.py index b7dcc7b..365deb1 100644 --- a/cah/cah.py +++ b/cah/cah.py @@ -233,7 +233,9 @@ def change_state(self, bot, comm, state): self.state = state interval = self.TIME_ALLOWED/self.TIMES_TO_CHECK if state == 'play': - for player in filter(lambda x: x != self.dealer, self.players): + for player in self.players: + if player == self.dealer: + continue reactor.callLater(interval, self.start_afk_watcher, bot, comm, str(self.prompt), str(self.state), str(player)) From 3c8ae554d9806a7dcb19c5e22afe0336d4fea2dc Mon Sep 17 00:00:00 2001 From: Andrew Ekstedt Date: Thu, 30 Apr 2015 22:46:37 -0700 Subject: [PATCH 5/6] del is not a function --- cah/cah.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/cah/cah.py b/cah/cah.py index 365deb1..866d2f4 100644 --- a/cah/cah.py +++ b/cah/cah.py @@ -81,9 +81,9 @@ def remove_player(self, bot, comm, player): self.white_discard += self.players[player] # Remove player - del(self.players[player]) + del self.players[player] if player in self.kick_votes: - del(self.kick_votes[player]) + del self.kick_votes[player] if player in self.player_queue: self.player_queue.remove(player) @@ -95,7 +95,7 @@ def remove_player(self, bot, comm, player): if player in self.avail_players: if player in self.answers: self.white_discard += self.answers[player] - del(self.answers[player]) + del self.answers[player] self.avail_players.remove(player) elif player == self.dealer: bot.reply(comm, "[*] Game restarting... dealer left.") @@ -185,7 +185,7 @@ def reset(self, bot, comm): if p not in self.players: bot.reply(comm, '{0} has joined the game!'.format(p)) self.deal(p) - del(self.player_queue[:]) + del self.player_queue[:] if len(self.players) > 2: self.prep_play(bot, comm) @@ -463,7 +463,7 @@ def command(self, bot, comm, groups): if user in self.plugin.answers: self.plugin.players[user] += self.plugin.answers[user] - del(self.plugin.answers[user]) + del self.plugin.answers[user] self.plugin.answers[user] = [self.plugin.players[user][i - 1] for i in indices] From d826a02701b328bfe0bcc34bb437ad554056e554 Mon Sep 17 00:00:00 2001 From: Andrew Ekstedt Date: Sun, 3 May 2015 18:09:01 -0700 Subject: [PATCH 6/6] Bring CAH into the new age of plugins. In the new plugin system, we need a hamperbot.plugins entry in setup.py to let hamper know about the plugin, and we no longer need to create an instance of the plugin at the bottom of the file. --- cah/cah.py | 3 --- setup.py | 7 +++++-- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/cah/cah.py b/cah/cah.py index 866d2f4..9960832 100644 --- a/cah/cah.py +++ b/cah/cah.py @@ -768,6 +768,3 @@ def __init__(self, user, game, score=0): def __repr__(self): return "%s: %d" % self.user, self.score - - -cah = CardsAgainstHumanity() diff --git a/setup.py b/setup.py index 1b5f3a6..4f8dc4b 100644 --- a/setup.py +++ b/setup.py @@ -1,4 +1,4 @@ -from distutils.core import setup +from setuptools import setup with open('requirements.txt') as f: requirements = [l.strip() for l in f] @@ -11,5 +11,8 @@ author_email='deanjohnson222@gmail.com', url='https://github.com/johnsdea/cah_bot', install_requires=requirements, - package_data={'cah': ['requirements.txt', 'README.md', 'LICENSE']} + package_data={'cah': ['requirements.txt', 'README.md', 'LICENSE']}, + entry_points = { + 'hamperbot.plugins': [ "cah = cah.cah:CardsAgainstHumanity" ], + }, )