diff --git a/cah/cah.py b/cah/cah.py index 0605931..9960832 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 """ @@ -78,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) @@ -92,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.") @@ -182,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) @@ -230,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)) @@ -262,14 +267,14 @@ 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 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: @@ -284,9 +289,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): @@ -331,19 +335,20 @@ 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): 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)) @@ -443,7 +448,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,19 +457,19 @@ 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)) 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] # 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) @@ -532,8 +537,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]) @@ -614,7 +619,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!") @@ -685,7 +690,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) @@ -763,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" ], + }, )