kinda works now
This commit is contained in:
parent
29a6ac8b07
commit
5465fca34f
8 changed files with 119 additions and 17 deletions
85
bot.py
85
bot.py
|
@ -1,15 +1,30 @@
|
|||
import logging
|
||||
|
||||
from telegram import Updater, InlineQueryResultPhoto
|
||||
from telegram import Updater, InlineQueryResultPhoto, InlineQueryResultArticle
|
||||
|
||||
from game_manager import GameManager
|
||||
import card as c
|
||||
from credentials import TOKEN
|
||||
|
||||
logging.basicConfig(
|
||||
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
|
||||
level=logging.DEBUG)
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
gm = GameManager()
|
||||
u = Updater(TOKEN)
|
||||
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):
|
||||
chat_id = update.message.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
|
||||
player = gm.userid_player[user_id]
|
||||
|
||||
playable = list()
|
||||
for card in player.playable_cards():
|
||||
playable.append(
|
||||
results = list()
|
||||
playable = player.playable_cards()
|
||||
|
||||
if playable:
|
||||
for card in playable:
|
||||
results.append(
|
||||
InlineQueryResultPhoto(str(card),
|
||||
card.get_image_link(),
|
||||
card.get_thumb_link())
|
||||
card.get_thumb_link(),
|
||||
title="Play card",
|
||||
description="<Card type goes here>")
|
||||
)
|
||||
|
||||
bot.answerInlineQuery(update.inline_query.id, playable)
|
||||
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
|
||||
)
|
||||
)
|
||||
|
||||
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:
|
||||
user_id = update.chosen_inline_result.from_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.addTelegramCommandHandler('start', start)
|
||||
dp.addTelegramCommandHandler('new', new_game)
|
||||
dp.addErrorHandler(error)
|
||||
|
||||
u.start_polling()
|
||||
u.idle()
|
||||
|
|
14
card.py
14
card.py
|
@ -51,8 +51,22 @@ class Card(object):
|
|||
return '%s_%s' % (self.color, self.value)
|
||||
|
||||
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)
|
||||
|
||||
def __eq__(self, other):
|
||||
return str(self) == str(other)
|
||||
|
||||
def get_image_link(self):
|
||||
return IMAGE_PATTERN % str(self)
|
||||
|
||||
|
|
7
deck.py
7
deck.py
|
@ -24,11 +24,14 @@ class Deck(object):
|
|||
self.shuffle()
|
||||
|
||||
def shuffle(self):
|
||||
self.cards = shuffle(self.cards)
|
||||
self.logger.debug("Shuffling Deck")
|
||||
shuffle(self.cards)
|
||||
|
||||
def draw(self):
|
||||
try:
|
||||
return self.cards.pop()
|
||||
card = self.cards.pop()
|
||||
self.logger.debug("Drawing card " + str(card))
|
||||
return card
|
||||
except IndexError:
|
||||
while len(self.graveyard):
|
||||
self.cards.append(self.graveyard.pop())
|
||||
|
|
2
game.py
2
game.py
|
@ -11,7 +11,7 @@ class Game(object):
|
|||
"""
|
||||
current_player = None
|
||||
reversed = False
|
||||
draw_counter = 0
|
||||
draw_counter = 1
|
||||
choosing_color = False
|
||||
|
||||
def __init__(self):
|
||||
|
|
|
@ -1,4 +1,6 @@
|
|||
from uuid import uuid4
|
||||
import logging
|
||||
|
||||
from game import Game
|
||||
from player import Player
|
||||
|
||||
|
@ -13,16 +15,20 @@ class GameManager(object):
|
|||
self.chatid_gameid = dict()
|
||||
self.userid_user = dict()
|
||||
self.userid_player = dict()
|
||||
self.logger = logging.getLogger(__name__)
|
||||
|
||||
def generate_invite_link(self, bot_name, chat_id):
|
||||
game_id = uuid4()
|
||||
game_id = str(uuid4())
|
||||
game = Game()
|
||||
|
||||
self.logger.info("Creating new game with id " + game_id)
|
||||
self.gameid_game[game_id] = game
|
||||
self.chatid_gameid[chat_id] = game_id
|
||||
|
||||
return LINK_PATTERN % (bot_name, game_id)
|
||||
|
||||
def join_game(self, game_id, user):
|
||||
self.logger.info("Joining game with id " + game_id)
|
||||
game = self.gameid_game[game_id]
|
||||
player = Player(game, user)
|
||||
self.userid_player[user.id] = player
|
||||
|
|
BIN
images/jpg/thinking.jpg
Normal file
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
BIN
images/thumb/thinking.jpg
Normal file
Binary file not shown.
After Width: | Height: | Size: 894 B |
16
player.py
16
player.py
|
@ -1,4 +1,4 @@
|
|||
import card as c
|
||||
import logging
|
||||
|
||||
|
||||
class Player(object):
|
||||
|
@ -13,6 +13,8 @@ class Player(object):
|
|||
self.cards = list()
|
||||
self.game = game
|
||||
self.user = user
|
||||
self.logger = logging.getLogger(__name__)
|
||||
|
||||
if game.current_player:
|
||||
self.next = game.current_player
|
||||
self.prev = game.current_player.prev
|
||||
|
@ -56,15 +58,21 @@ class Player(object):
|
|||
|
||||
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
|
||||
|
||||
playable = list()
|
||||
last = self.game.last_card
|
||||
|
||||
self.logger.debug("Last card was" + str(last))
|
||||
|
||||
for card in self.cards:
|
||||
if (card.color is last.color or card.value is last.value) and \
|
||||
not last.special:
|
||||
self.logger.debug("Checking card " + str(card))
|
||||
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)
|
||||
|
||||
return playable
|
||||
|
|
Loading…
Reference in a new issue