some refactoring and rule improvements

This commit is contained in:
Jannes Höke 2016-03-01 01:25:26 +01:00
parent 0d381ddf4d
commit b0bdbe0d0f
4 changed files with 191 additions and 123 deletions

123
bot.py
View file

@ -22,7 +22,7 @@ def list_subtract(list1, list2):
for x in list2: for x in list2:
list1.remove(x) list1.remove(x)
return list1 return list(sorted(list1))
def new_game(bot, update): def new_game(bot, update):
@ -55,6 +55,12 @@ def start(bot, update, args):
def inline(bot, update): def inline(bot, update):
if update.inline_query: if update.inline_query:
reply_to_query(bot, update)
else:
chosen_card(bot, update)
def reply_to_query(bot, update):
user_id = update.inline_query.from_user.id user_id = update.inline_query.from_user.id
player = gm.userid_player[user_id] player = gm.userid_player[user_id]
game = gm.userid_game[user_id] game = gm.userid_game[user_id]
@ -72,20 +78,74 @@ def inline(bot, update):
) )
) )
else: else:
playable = player.playable_cards() playable = list(sorted(player.playable_cards()))
if playable is False: if playable is False:
results.append( not_your_turn(game, results)
InlineQueryResultArticle(
"not_your_turn",
title="Not your turn",
description="Tap to see the current player",
message_text="Current player: " +
game.current_player.user.first_name
)
)
elif playable: elif playable:
for card in playable: for card in playable:
play_card(card, results)
elif not game.choosing_color:
draw(player, results)
if player.drew:
pass_(results)
if game.last_card.special == c.DRAW_FOUR and not game.choosing_color:
call_bluff(results)
other_cards(playable, player, results)
bot.answerInlineQuery(update.inline_query.id, results, cache_time=0)
def other_cards(playable, player, results):
results.append(
InlineQueryResultArticle(
"hand",
title="Other cards:",
description=', '.join([repr(card) for card in
list_subtract(player.cards, playable)]),
message_text='Just checking cards'
)
)
def draw(player, results):
results.append(
InlineQueryResultArticle(
"draw",
title="No suitable cards...",
description="Draw!",
message_text='Drawing %d card(s)'
% (player.game.draw_counter or 1)
)
)
def pass_(results):
results.append(
InlineQueryResultArticle(
"pass",
title="Pass",
description="Don't play a card",
message_text='Pass'
)
)
def call_bluff(results):
results.append(
InlineQueryResultArticle(
"call_bluff",
title="Call their bluff!",
description="Risk it!",
message_text="I'm calling your bluff!"
)
)
def play_card(card, results):
results.append( results.append(
InlineQueryResultArticle(str(card), InlineQueryResultArticle(str(card),
title="Play card", title="Play card",
@ -97,46 +157,41 @@ def inline(bot, update):
description=repr(card), description=repr(card),
parse_mode=ParseMode.HTML) parse_mode=ParseMode.HTML)
) )
elif not game.choosing_color:
def not_your_turn(game, results):
results.append( results.append(
InlineQueryResultArticle( InlineQueryResultArticle(
"draw", "not_your_turn",
title="No suitable cards...", title="Not your turn",
description="Draw!", description="Tap to see the current player",
message_text='Drawing %d card(s)' message_text="Current player: " +
% (player.game.draw_counter or 1) game.current_player.user.first_name
) )
) )
results.append(
InlineQueryResultArticle(
"hand",
title="Other cards:",
description=', '.join([repr(card) for card in
list_subtract(player.cards, playable)]),
message_text='Just checking cards'
)
)
[logger.info(str(result)) for result in results] def chosen_card(bot, update):
bot.answerInlineQuery(update.inline_query.id, results, cache_time=0)
else:
user = update.chosen_inline_result.from_user user = update.chosen_inline_result.from_user
game = gm.userid_game[user.id] game = gm.userid_game[user.id]
player = gm.userid_player[user.id] player = gm.userid_player[user.id]
result_id = update.chosen_inline_result.result_id result_id = update.chosen_inline_result.result_id
chat_id = gm.chatid_gameid[game] chat_id = gm.chatid_gameid[game]
logger.info("Selected result: " + result_id) logger.info("Selected result: " + result_id)
if result_id == 'hand': if result_id in ('hand', 'not_your_turn'):
pass return
elif result_id == 'draw': elif result_id == 'draw':
for n in range(game.draw_counter or 1): for n in range(game.draw_counter or 1):
player.cards.append(game.deck.draw()) player.cards.append(game.deck.draw())
game.draw_counter = 0 game.draw_counter = 0
player.drew = True
if game.last_card.value == c.DRAW_TWO or \
not player.card_playable(player.cards[-1], list()):
game.turn()
elif result_id == 'pass':
game.turn()
elif result_id in c.COLORS: elif result_id in c.COLORS:
game.choose_color(result_id) game.choose_color(result_id)
else: else:

View file

@ -56,6 +56,9 @@ class Card(object):
def __eq__(self, other): def __eq__(self, other):
return str(self) == str(other) return str(self) == str(other)
def __lt__(self, other):
return str(self) < str(other)
def get_image_link(self): def get_image_link(self):
return IMAGE_PATTERN % str(self) return IMAGE_PATTERN % str(self)

View file

@ -25,7 +25,9 @@ class Game(object):
self.reversed = not self.reversed self.reversed = not self.reversed
def turn(self): def turn(self):
self.logger.debug("Next Player")
self.current_player = self.current_player.next self.current_player = self.current_player.next
self.current_player.drew = False
def play_card(self, card): def play_card(self, card):
""" """
@ -39,7 +41,7 @@ class Game(object):
self.logger.info("Playing card " + repr(card)) self.logger.info("Playing card " + repr(card))
if card.value == c.SKIP: if card.value == c.SKIP:
self.current_player = self.current_player.next.next self.turn()
elif card.special == c.DRAW_FOUR: elif card.special == c.DRAW_FOUR:
self.draw_counter += 4 self.draw_counter += 4
self.logger.debug("Draw counter increased by 4") self.logger.debug("Draw counter increased by 4")
@ -50,12 +52,12 @@ class Game(object):
self.reverse() self.reverse()
if card.special not in (c.CHOOSE, c.DRAW_FOUR): if card.special not in (c.CHOOSE, c.DRAW_FOUR):
self.current_player = self.current_player.next self.turn()
else: else:
self.logger.debug("Choosing Color...") self.logger.debug("Choosing Color...")
self.choosing_color = True self.choosing_color = True
def choose_color(self, color): def choose_color(self, color):
self.last_card.color = color self.last_card.color = color
self.current_player = self.current_player.next self.turn()
self.choosing_color = False self.choosing_color = False

View file

@ -27,9 +27,12 @@ class Player(object):
self._prev = self self._prev = self
game.current_player = self game.current_player = self
for i in range(6): for i in range(7):
self.cards.append(self.game.deck.draw()) self.cards.append(self.game.deck.draw())
self.bluffing = False
self.drew = False
def leave(self): def leave(self):
self.next.prev = self.prev self.next.prev = self.prev
self.prev.next = self.next self.prev.next = self.next
@ -76,29 +79,34 @@ class Player(object):
self.logger.debug("Last card was" + str(last)) self.logger.debug("Last card was" + str(last))
for card in self.cards: for card in self.cards:
if self.card_playable(card, playable):
self.logger.debug("Matching!")
playable.append(card)
self.bluffing = bool(len(playable) - 1)
return playable
def card_playable(self, card, playable):
is_playable = True
last = self.game.last_card
self.logger.debug("Checking card " + str(card)) self.logger.debug("Checking card " + str(card))
if (card.color != last.color and card.value != last.value and if (card.color != last.color and card.value != last.value and
not card.special): not card.special):
self.logger.debug("Card's color or value doesn't match") self.logger.debug("Card's color or value doesn't match")
continue is_playable = False
if last.value == c.DRAW_TWO and not \ if last.value == c.DRAW_TWO and not \
(card.value == c.DRAW_TWO or (card.value == c.DRAW_TWO or
card.special == c.DRAW_FOUR or card.special == c.DRAW_FOUR or
not self.game.draw_counter): not self.game.draw_counter):
self.logger.debug("Player has to draw and can't counter") self.logger.debug("Player has to draw and can't counter")
continue is_playable = False
if last.special == c.DRAW_FOUR and self.game.draw_counter: if last.special == c.DRAW_FOUR and self.game.draw_counter:
self.logger.debug("Player has to draw and can't counter") self.logger.debug("Player has to draw and can't counter")
continue is_playable = False
if not last.color or card in playable: if not last.color or card in playable:
self.logger.debug("Last card has no color or the card was " self.logger.debug("Last card has no color or the card was "
"already added to the list") "already added to the list")
continue is_playable = False
self.logger.debug("Matching!") return is_playable
playable.append(card)
return playable