rules might be wonky but works
This commit is contained in:
parent
5465fca34f
commit
0d381ddf4d
5 changed files with 138 additions and 61 deletions
117
bot.py
117
bot.py
|
@ -1,14 +1,14 @@
|
||||||
import logging
|
import logging
|
||||||
|
|
||||||
from telegram import Updater, InlineQueryResultPhoto, InlineQueryResultArticle
|
from telegram import Updater, InlineQueryResultPhoto, InlineQueryResultArticle, ParseMode
|
||||||
|
|
||||||
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(
|
logging.basicConfig(
|
||||||
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
|
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
|
||||||
level=logging.DEBUG)
|
level=logging.DEBUG)
|
||||||
logger = logging.getLogger(__name__)
|
logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
gm = GameManager()
|
gm = GameManager()
|
||||||
|
@ -34,7 +34,19 @@ def new_game(bot, update):
|
||||||
|
|
||||||
def start(bot, update, args):
|
def start(bot, update, args):
|
||||||
if args:
|
if args:
|
||||||
gm.join_game(args[0], update.message.from_user)
|
game_id = args[0]
|
||||||
|
gm.join_game(game_id, update.message.from_user)
|
||||||
|
game = gm.gameid_game[game_id]
|
||||||
|
groupchat = gm.chatid_gameid[game_id]
|
||||||
|
bot.sendMessage(update.message.chat_id, text="Joined game!")
|
||||||
|
bot.sendMessage(groupchat, text=update.message.from_user.first_name +
|
||||||
|
" joined the game!")
|
||||||
|
|
||||||
|
if game.current_player is game.current_player.next:
|
||||||
|
game.play_card(game.last_card)
|
||||||
|
bot.sendPhoto(groupchat,
|
||||||
|
photo=game.last_card.get_image_link(),
|
||||||
|
caption="First Card")
|
||||||
else:
|
else:
|
||||||
bot.sendMessage(update.message.chat_id,
|
bot.sendMessage(update.message.chat_id,
|
||||||
text="Please invite me to a group and "
|
text="Please invite me to a group and "
|
||||||
|
@ -45,70 +57,103 @@ def inline(bot, update):
|
||||||
if update.inline_query:
|
if update.inline_query:
|
||||||
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]
|
||||||
results = list()
|
results = list()
|
||||||
playable = player.playable_cards()
|
playable = list()
|
||||||
|
|
||||||
if playable:
|
if game.choosing_color:
|
||||||
for card in playable:
|
for color in c.COLORS:
|
||||||
results.append(
|
results.append(
|
||||||
InlineQueryResultPhoto(str(card),
|
InlineQueryResultArticle(
|
||||||
card.get_image_link(),
|
id=color,
|
||||||
card.get_thumb_link(),
|
title="Choose Color",
|
||||||
title="Play card",
|
message_text=color,
|
||||||
description="<Card type goes here>")
|
description=color.upper()
|
||||||
|
)
|
||||||
)
|
)
|
||||||
|
else:
|
||||||
|
playable = player.playable_cards()
|
||||||
|
|
||||||
|
if playable is False:
|
||||||
results.append(
|
results.append(
|
||||||
InlineQueryResultPhoto(
|
InlineQueryResultArticle(
|
||||||
"hand",
|
"not_your_turn",
|
||||||
c.IMAGE_PATTERN % 'thinking',
|
title="Not your turn",
|
||||||
c.THUMB_PATTERN % 'thinking',
|
description="Tap to see the current player",
|
||||||
title="Other cards:",
|
message_text="Current player: " +
|
||||||
description=', '.join([repr(card) for card in
|
game.current_player.user.first_name
|
||||||
list_subtract(player.cards, playable)]),
|
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
else:
|
elif playable:
|
||||||
|
for card in playable:
|
||||||
|
results.append(
|
||||||
|
InlineQueryResultArticle(str(card),
|
||||||
|
title="Play card",
|
||||||
|
message_text=
|
||||||
|
('<a href="%s">\xad</a>'
|
||||||
|
'Played card ' + repr(card))
|
||||||
|
% card.get_image_link(),
|
||||||
|
thumb_url=card.get_thumb_link(),
|
||||||
|
description=repr(card),
|
||||||
|
parse_mode=ParseMode.HTML)
|
||||||
|
)
|
||||||
|
elif not game.choosing_color:
|
||||||
results.append(
|
results.append(
|
||||||
InlineQueryResultArticle(
|
InlineQueryResultArticle(
|
||||||
"draw",
|
"draw",
|
||||||
title="No suitable cards...",
|
title="No suitable cards...",
|
||||||
description="Draw!",
|
description="Draw!",
|
||||||
message_text='Drawing %d card(s)' % player.game.draw_counter
|
message_text='Drawing %d card(s)'
|
||||||
|
% (player.game.draw_counter or 1)
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
|
|
||||||
results.append(
|
results.append(
|
||||||
InlineQueryResultArticle(
|
InlineQueryResultArticle(
|
||||||
"hand",
|
"hand",
|
||||||
title="Other cards:",
|
title="Other cards:",
|
||||||
description=', '.join([repr(card) for card in
|
description=', '.join([repr(card) for card in
|
||||||
list_subtract(player.cards, playable)]),
|
list_subtract(player.cards, playable)]),
|
||||||
message_text='Just checking cards'
|
message_text='Just checking cards'
|
||||||
)
|
|
||||||
)
|
)
|
||||||
|
)
|
||||||
|
|
||||||
[logger.info(str(result)) for result in results]
|
[logger.info(str(result)) for result in results]
|
||||||
|
|
||||||
bot.answerInlineQuery(update.inline_query.id, results, cache_time=0)
|
bot.answerInlineQuery(update.inline_query.id, results, cache_time=0)
|
||||||
|
|
||||||
else:
|
else:
|
||||||
user_id = update.chosen_inline_result.from_user.id
|
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]
|
||||||
|
|
||||||
logger.info("Selected result: " + result_id)
|
logger.info("Selected result: " + result_id)
|
||||||
|
|
||||||
if result_id is 'hand':
|
if result_id == 'hand':
|
||||||
pass
|
pass
|
||||||
elif result_id is 'draw':
|
elif result_id == 'draw':
|
||||||
for n in range(game.draw_counter):
|
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
|
||||||
|
elif result_id in c.COLORS:
|
||||||
|
game.choose_color(result_id)
|
||||||
else:
|
else:
|
||||||
card = c.from_str(result_id)
|
card = c.from_str(result_id)
|
||||||
game.play_card(card)
|
game.play_card(card)
|
||||||
player.cards.remove(card)
|
player.cards.remove(card)
|
||||||
|
if game.choosing_color:
|
||||||
|
bot.sendMessage(chat_id, text="Please choose a color")
|
||||||
|
elif len(player.cards) == 1:
|
||||||
|
bot.sendMessage(chat_id, text="Last Card!")
|
||||||
|
elif len(player.cards) == 0:
|
||||||
|
gm.leave_game(user)
|
||||||
|
bot.sendMessage(chat_id, text="Player won!")
|
||||||
|
|
||||||
|
bot.sendMessage(chat_id,
|
||||||
|
text="Next player: " +
|
||||||
|
game.current_player.user.first_name)
|
||||||
|
|
||||||
|
|
||||||
def error(bot, update, error):
|
def error(bot, update, error):
|
||||||
|
|
17
card.py
17
card.py
|
@ -51,18 +51,7 @@ 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 ' '.join([s.capitalize() for s in str(self).split('_')])
|
||||||
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):
|
def __eq__(self, other):
|
||||||
return str(self) == str(other)
|
return str(self) == str(other)
|
||||||
|
@ -75,8 +64,8 @@ class Card(object):
|
||||||
|
|
||||||
|
|
||||||
def from_str(string):
|
def from_str(string):
|
||||||
if '_' in string:
|
if string not in SPECIALS:
|
||||||
color, value = string.split('_')
|
color, value = string.split('_')
|
||||||
return Card(color, value)
|
return Card(color, value)
|
||||||
else:
|
else:
|
||||||
return Card(None, None, string)
|
return Card(None, None, string)
|
||||||
|
|
18
game.py
18
game.py
|
@ -1,3 +1,5 @@
|
||||||
|
import logging
|
||||||
|
|
||||||
from deck import Deck
|
from deck import Deck
|
||||||
from card import Card
|
from card import Card
|
||||||
import card as c
|
import card as c
|
||||||
|
@ -11,12 +13,13 @@ class Game(object):
|
||||||
"""
|
"""
|
||||||
current_player = None
|
current_player = None
|
||||||
reversed = False
|
reversed = False
|
||||||
draw_counter = 1
|
draw_counter = 0
|
||||||
choosing_color = False
|
choosing_color = False
|
||||||
|
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
self.deck = Deck()
|
self.deck = Deck()
|
||||||
self.last_card = self.deck.draw()
|
self.last_card = self.deck.draw()
|
||||||
|
self.logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
def reverse(self):
|
def reverse(self):
|
||||||
self.reversed = not self.reversed
|
self.reversed = not self.reversed
|
||||||
|
@ -33,18 +36,23 @@ class Game(object):
|
||||||
"""
|
"""
|
||||||
self.deck.dismiss(self.last_card)
|
self.deck.dismiss(self.last_card)
|
||||||
self.last_card = card
|
self.last_card = card
|
||||||
if card.value is c.SKIP:
|
|
||||||
|
self.logger.info("Playing card " + repr(card))
|
||||||
|
if card.value == c.SKIP:
|
||||||
self.current_player = self.current_player.next.next
|
self.current_player = self.current_player.next.next
|
||||||
elif card.special is c.DRAW_FOUR:
|
elif card.special == c.DRAW_FOUR:
|
||||||
self.draw_counter += 4
|
self.draw_counter += 4
|
||||||
elif card.value is c.DRAW_TWO:
|
self.logger.debug("Draw counter increased by 4")
|
||||||
|
elif card.value == c.DRAW_TWO:
|
||||||
self.draw_counter += 2
|
self.draw_counter += 2
|
||||||
elif card.value is c.REVERSE:
|
self.logger.debug("Draw counter increased by 2")
|
||||||
|
elif card.value == c.REVERSE:
|
||||||
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.current_player = self.current_player.next
|
||||||
else:
|
else:
|
||||||
|
self.logger.debug("Choosing Color...")
|
||||||
self.choosing_color = True
|
self.choosing_color = True
|
||||||
|
|
||||||
def choose_color(self, color):
|
def choose_color(self, color):
|
||||||
|
|
|
@ -13,7 +13,6 @@ class GameManager(object):
|
||||||
self.gameid_game = dict()
|
self.gameid_game = dict()
|
||||||
self.userid_game = dict()
|
self.userid_game = dict()
|
||||||
self.chatid_gameid = dict()
|
self.chatid_gameid = dict()
|
||||||
self.userid_user = dict()
|
|
||||||
self.userid_player = dict()
|
self.userid_player = dict()
|
||||||
self.logger = logging.getLogger(__name__)
|
self.logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
@ -24,6 +23,8 @@ class GameManager(object):
|
||||||
self.logger.info("Creating new game with id " + game_id)
|
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
|
||||||
|
self.chatid_gameid[game_id] = chat_id
|
||||||
|
self.chatid_gameid[game] = chat_id
|
||||||
|
|
||||||
return LINK_PATTERN % (bot_name, game_id)
|
return LINK_PATTERN % (bot_name, game_id)
|
||||||
|
|
||||||
|
@ -33,3 +34,11 @@ class GameManager(object):
|
||||||
player = Player(game, user)
|
player = Player(game, user)
|
||||||
self.userid_player[user.id] = player
|
self.userid_player[user.id] = player
|
||||||
self.userid_game[user.id] = game
|
self.userid_game[user.id] = game
|
||||||
|
|
||||||
|
def leave_game(self, user):
|
||||||
|
player = self.userid_player[user.id]
|
||||||
|
|
||||||
|
player.leave()
|
||||||
|
del self.userid_player[user.id]
|
||||||
|
del self.userid_game[user.id]
|
||||||
|
|
||||||
|
|
36
player.py
36
player.py
|
@ -1,5 +1,7 @@
|
||||||
import logging
|
import logging
|
||||||
|
|
||||||
|
import card as c
|
||||||
|
|
||||||
|
|
||||||
class Player(object):
|
class Player(object):
|
||||||
|
|
||||||
|
@ -28,6 +30,12 @@ class Player(object):
|
||||||
for i in range(6):
|
for i in range(6):
|
||||||
self.cards.append(self.game.deck.draw())
|
self.cards.append(self.game.deck.draw())
|
||||||
|
|
||||||
|
def leave(self):
|
||||||
|
self.next.prev = self.prev
|
||||||
|
self.prev.next = self.next
|
||||||
|
self.next = None
|
||||||
|
self.prev = None
|
||||||
|
|
||||||
def __repr__(self):
|
def __repr__(self):
|
||||||
return repr(self.user)
|
return repr(self.user)
|
||||||
|
|
||||||
|
@ -69,10 +77,28 @@ class Player(object):
|
||||||
|
|
||||||
for card in self.cards:
|
for card in self.cards:
|
||||||
self.logger.debug("Checking card " + str(card))
|
self.logger.debug("Checking card " + str(card))
|
||||||
if (card.color is last.color or card.value is last.value or
|
if (card.color != last.color and card.value != last.value and
|
||||||
card.special) and \
|
not card.special):
|
||||||
not last.special and card not in playable:
|
self.logger.debug("Card's color or value doesn't match")
|
||||||
self.logger.debug("Matching!")
|
continue
|
||||||
playable.append(card)
|
|
||||||
|
if last.value == c.DRAW_TWO and not \
|
||||||
|
(card.value == c.DRAW_TWO or
|
||||||
|
card.special == c.DRAW_FOUR or
|
||||||
|
not self.game.draw_counter):
|
||||||
|
self.logger.debug("Player has to draw and can't counter")
|
||||||
|
continue
|
||||||
|
|
||||||
|
if last.special == c.DRAW_FOUR and self.game.draw_counter:
|
||||||
|
self.logger.debug("Player has to draw and can't counter")
|
||||||
|
continue
|
||||||
|
|
||||||
|
if not last.color or card in playable:
|
||||||
|
self.logger.debug("Last card has no color or the card was "
|
||||||
|
"already added to the list")
|
||||||
|
continue
|
||||||
|
|
||||||
|
self.logger.debug("Matching!")
|
||||||
|
playable.append(card)
|
||||||
|
|
||||||
return playable
|
return playable
|
||||||
|
|
Loading…
Reference in a new issue