Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions draw.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,13 +77,12 @@ def render_card_friend(image, x, y, color, value):
# if color == 'black': text_fill = (255, 255, 255)
image.text((x + width/2.5, y + height/8), value, font=text_font, fill=text_fill)

def draw_board_state(game, player_viewing, filename):
def draw_board_state(game, player_viewing, filename, background=(20, 20, 20)):
width = int(400 / size)
height = (width * 16) // 9
if len(game.players) == 4:
height += 100

background = (20, 20, 20)
image = Image.new('RGB', (width, height), background)
draw = ImageDraw.Draw(image)
text_fill = (200, 200, 200)
Expand Down
19 changes: 17 additions & 2 deletions main.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,19 @@
import time
import telepot
from telepot.loop import MessageLoop
import itertools
import sys
import hanabi
import draw
from telepot.namedtuple import InlineKeyboardMarkup, InlineKeyboardButton

BACKGROUND_COLORS_RGB = itertools.cycle((
(20, 20, 20), # black
(100, 20, 20), # red
(10, 50, 10), # green
(15, 30, 74), # blue
(75, 0, 70), # purple
))

class ChatGame:
def __init__(self, chat_id, admin):
Expand All @@ -15,6 +23,7 @@ def __init__(self, chat_id, admin):
self.user_to_message = {}
self.current_action = ''
self.chat_id = chat_id
self.background_color = next(BACKGROUND_COLORS_RGB)

class BotServer(object):
def __init__(self, token):
Expand Down Expand Up @@ -56,7 +65,7 @@ def send_game_views(bot, chat_game, last_player=''):
for name, user_id in chat_game.player_to_user.items():
# TODO: Send directly generated image, without write to disk.
filename = str(user_id) + '.png'
draw.draw_board_state(chat_game.game, name, filename)
draw.draw_board_state(chat_game.game, name, filename, background=chat_game.background_color)
try:
with open(filename, 'rb') as image:
bot.sendPhoto(user_id, image)
Expand Down Expand Up @@ -115,7 +124,11 @@ def send_keyboard(bot, chat_id, keyboard_type):

keyboard = InlineKeyboardMarkup(inline_keyboard=keyboard)
if chat_game.user_to_message[user_id] is not None:
edit_message(chat_game, bot, user_id, player + ", choose an action", keyboard)
# quick fix for unkown crash when user try to /refresh
try:
edit_message(chat_game, bot, user_id, player + ", choose an action", keyboard)
except telepot.exception.TelegramError:
chat_game.user_to_message[user_id] = bot.sendMessage(user_id, player + ", it's your turn", reply_markup=keyboard)
else:
chat_game.user_to_message[user_id] = bot.sendMessage(user_id, player + ", it's your turn", reply_markup=keyboard)

Expand Down Expand Up @@ -316,6 +329,8 @@ def handle_message(message_object):
for name in test_players[:int(n)]:
add_player(server, chat_id, user_id, name, allow_repeated_players=True)
start_game(server, chat_id, user_id)
elif text == '/refresh':
pass
else:
return

Expand Down