kinda works now

This commit is contained in:
Jannes Höke 2016-02-29 01:53:59 +01:00
parent 29a6ac8b07
commit 5465fca34f
8 changed files with 119 additions and 17 deletions

89
bot.py
View file

@ -1,15 +1,30 @@
import logging
from telegram import Updater, InlineQueryResultPhoto from telegram import Updater, InlineQueryResultPhoto, InlineQueryResultArticle
from game_manager import GameManager from game_manager import GameManager
import card as c import card as c
from credentials import TOKEN from credentials import TOKEN
logging.basicConfig(
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
level=logging.DEBUG)
logger = logging.getLogger(__name__)
gm = GameManager() gm = GameManager()
u = Updater(TOKEN) u = Updater(TOKEN)
dp = u.dispatcher dp = u.dispatcher
def list_subtract(list1, list2):
list1 = list1.copy()
for x in list2:
list1.remove(x)
return list1
def new_game(bot, update): def new_game(bot, update):
chat_id = update.message.chat_id chat_id = update.message.chat_id
link = gm.generate_invite_link(u.bot.getMe().username, chat_id) link = gm.generate_invite_link(u.bot.getMe().username, chat_id)
@ -31,23 +46,79 @@ def inline(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]
playable = list() results = list()
for card in player.playable_cards(): playable = player.playable_cards()
playable.append(
InlineQueryResultPhoto(str(card), if playable:
card.get_image_link(), for card in playable:
card.get_thumb_link()) results.append(
InlineQueryResultPhoto(str(card),
card.get_image_link(),
card.get_thumb_link(),
title="Play card",
description="<Card type goes here>")
)
results.append(
InlineQueryResultPhoto(
"hand",
c.IMAGE_PATTERN % 'thinking',
c.THUMB_PATTERN % 'thinking',
title="Other cards:",
description=', '.join([repr(card) for card in
list_subtract(player.cards, playable)]),
)
)
else:
results.append(
InlineQueryResultArticle(
"draw",
title="No suitable cards...",
description="Draw!",
message_text='Drawing %d card(s)' % player.game.draw_counter
)
) )
bot.answerInlineQuery(update.inline_query.id, playable) 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]
bot.answerInlineQuery(update.inline_query.id, results, cache_time=0)
else: else:
user_id = update.chosen_inline_result.from_user.id user_id = update.chosen_inline_result.from_user.id
game = gm.userid_game[user_id] game = gm.userid_game[user_id]
game.play_card(c.from_str(update.chosen_inline_result.id)) player = gm.userid_player[user_id]
result_id = update.chosen_inline_result.result_id
logger.info("Selected result: " + result_id)
if result_id is 'hand':
pass
elif result_id is 'draw':
for n in range(game.draw_counter):
player.cards.append(game.deck.draw())
else:
card = c.from_str(result_id)
game.play_card(card)
player.cards.remove(card)
def error(bot, update, error):
logger.exception(error)
dp.addTelegramInlineHandler(inline) dp.addTelegramInlineHandler(inline)
dp.addTelegramCommandHandler('start', start) dp.addTelegramCommandHandler('start', start)
dp.addTelegramCommandHandler('new', new_game) dp.addTelegramCommandHandler('new', new_game)
dp.addErrorHandler(error)
u.start_polling() u.start_polling()
u.idle()

14
card.py
View file

@ -51,8 +51,22 @@ class Card(object):
return '%s_%s' % (self.color, self.value) return '%s_%s' % (self.color, self.value)
def __repr__(self): def __repr__(self):
if self.special:
return self.special
'''
if self.special is CHOOSE:
return "Colorchooser"
elif self.special is DRAW_FOUR:
return "Draw four"
'''
else:
return self.color + " - " + self.value
return str(self) return str(self)
def __eq__(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

@ -24,11 +24,14 @@ class Deck(object):
self.shuffle() self.shuffle()
def shuffle(self): def shuffle(self):
self.cards = shuffle(self.cards) self.logger.debug("Shuffling Deck")
shuffle(self.cards)
def draw(self): def draw(self):
try: try:
return self.cards.pop() card = self.cards.pop()
self.logger.debug("Drawing card " + str(card))
return card
except IndexError: except IndexError:
while len(self.graveyard): while len(self.graveyard):
self.cards.append(self.graveyard.pop()) self.cards.append(self.graveyard.pop())

View file

@ -11,7 +11,7 @@ class Game(object):
""" """
current_player = None current_player = None
reversed = False reversed = False
draw_counter = 0 draw_counter = 1
choosing_color = False choosing_color = False
def __init__(self): def __init__(self):

View file

@ -1,4 +1,6 @@
from uuid import uuid4 from uuid import uuid4
import logging
from game import Game from game import Game
from player import Player from player import Player
@ -13,16 +15,20 @@ class GameManager(object):
self.chatid_gameid = dict() self.chatid_gameid = dict()
self.userid_user = dict() self.userid_user = dict()
self.userid_player = dict() self.userid_player = dict()
self.logger = logging.getLogger(__name__)
def generate_invite_link(self, bot_name, chat_id): def generate_invite_link(self, bot_name, chat_id):
game_id = uuid4() game_id = str(uuid4())
game = Game() game = Game()
self.logger.info("Creating new game with id " + game_id)
self.gameid_game[game_id] = game self.gameid_game[game_id] = game
self.chatid_gameid[chat_id] = game_id self.chatid_gameid[chat_id] = game_id
return LINK_PATTERN % (bot_name, game_id) return LINK_PATTERN % (bot_name, game_id)
def join_game(self, game_id, user): def join_game(self, game_id, user):
self.logger.info("Joining game with id " + game_id)
game = self.gameid_game[game_id] game = self.gameid_game[game_id]
player = Player(game, user) player = Player(game, user)
self.userid_player[user.id] = player self.userid_player[user.id] = player

BIN
images/jpg/thinking.jpg Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.4 KiB

BIN
images/thumb/thinking.jpg Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 894 B

View file

@ -1,4 +1,4 @@
import card as c import logging
class Player(object): class Player(object):
@ -13,6 +13,8 @@ class Player(object):
self.cards = list() self.cards = list()
self.game = game self.game = game
self.user = user self.user = user
self.logger = logging.getLogger(__name__)
if game.current_player: if game.current_player:
self.next = game.current_player self.next = game.current_player
self.prev = game.current_player.prev self.prev = game.current_player.prev
@ -56,15 +58,21 @@ class Player(object):
def playable_cards(self): def playable_cards(self):
if self.game.current_player is not self: if self.game.current_player.user.id is not self.user.id:
self.logger.debug("Player is not current player")
return False return False
playable = list() playable = list()
last = self.game.last_card last = self.game.last_card
self.logger.debug("Last card was" + str(last))
for card in self.cards: for card in self.cards:
if (card.color is last.color or card.value is last.value) and \ self.logger.debug("Checking card " + str(card))
not last.special: if (card.color is last.color or card.value is last.value or
card.special) and \
not last.special and card not in playable:
self.logger.debug("Matching!")
playable.append(card) playable.append(card)
return playable return playable