From 3a3501066d1e05ae7a604f37482438f292c01cc3 Mon Sep 17 00:00:00 2001 From: francesconazzaro Date: Thu, 24 Mar 2022 13:41:03 +0100 Subject: [PATCH 1/3] add different background colors --- draw.py | 3 +-- main.py | 11 ++++++++++- 2 files changed, 11 insertions(+), 3 deletions(-) diff --git a/draw.py b/draw.py index 4618d44..8bdf02b 100644 --- a/draw.py +++ b/draw.py @@ -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) diff --git a/main.py b/main.py index b1c6df3..630f660 100644 --- a/main.py +++ b/main.py @@ -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): @@ -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): @@ -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) From 64d77d7c3cee8ab09954428b7683c3c4d4239079 Mon Sep 17 00:00:00 2001 From: francesconazzaro Date: Thu, 24 Mar 2022 14:57:50 +0100 Subject: [PATCH 2/3] add refresh command when bot is stuck --- main.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/main.py b/main.py index 630f660..264afd1 100644 --- a/main.py +++ b/main.py @@ -325,6 +325,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 From 95b40d61198ae21fdfbd3b870a4a1cb14c8db9a0 Mon Sep 17 00:00:00 2001 From: francesconazzaro Date: Thu, 31 Mar 2022 10:38:48 +0200 Subject: [PATCH 3/3] fix --- main.py | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/main.py b/main.py index 264afd1..63e9a3e 100644 --- a/main.py +++ b/main.py @@ -7,13 +7,13 @@ import draw from telepot.namedtuple import InlineKeyboardMarkup, InlineKeyboardButton -BACKGROUND_COLORS_RGB = itertools.cycle( +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): @@ -124,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)