diff --git a/blackjackbot/commands/game/commands.py b/blackjackbot/commands/game/commands.py index 8f33459..428b16e 100644 --- a/blackjackbot/commands/game/commands.py +++ b/blackjackbot/commands/game/commands.py @@ -121,12 +121,12 @@ def join_callback(update, context): # If players are full, replace join keyboard with start keyboard if len(game.players) >= game.MAX_PLAYERS: - update.effective_message.edit_reply_markup(reply_markup=get_start_keyboard(lang_id)) + update.effective_message.edit_reply_markup(reply_markup=get_start_keyboard(game.id, lang_id)) except errors.GameAlreadyRunningException: remove_inline_keyboard(update, context) update.callback_query.answer(translator("mp_game_already_begun_callback")) except errors.MaxPlayersReachedException: - update.effective_message.edit_reply_markup(reply_markup=get_start_keyboard(lang_id)) + update.effective_message.edit_reply_markup(reply_markup=get_start_keyboard(game.id, lang_id)) update.callback_query.answer(translator("mp_max_players_callback")) except errors.PlayerAlreadyExistingException: update.callback_query.answer(translator("mp_already_joined_callback")) diff --git a/blackjackbot/commands/util/functions.py b/blackjackbot/commands/util/functions.py index 07ab356..eddbb34 100644 --- a/blackjackbot/commands/util/functions.py +++ b/blackjackbot/commands/util/functions.py @@ -144,7 +144,13 @@ def get_join_keyboard(game_id, lang_id): return InlineKeyboardMarkup(inline_keyboard=[[join_button, start_button]]) -def get_start_keyboard(lang_id): +def get_start_keyboard(game_id, lang_id): + """ + Generates a keyboard with only a start button, translated into the given language + :param game_id: A unique identifier for each game + :param lang_id: The language identifier for a specific chat + :return: + """ translator = Translator(lang_id) - start_button = InlineKeyboardButton(text=translator("inline_keyboard_start"), callback_data="start") + start_button = InlineKeyboardButton(text=translator("inline_keyboard_start"), callback_data="start_{}".format(game_id)) return InlineKeyboardMarkup(inline_keyboard=[[start_button]]) diff --git a/blackjackbot/commands/util/tests/__init__.py b/blackjackbot/commands/util/tests/__init__.py new file mode 100644 index 0000000..40a96af --- /dev/null +++ b/blackjackbot/commands/util/tests/__init__.py @@ -0,0 +1 @@ +# -*- coding: utf-8 -*- diff --git a/blackjackbot/commands/util/tests/functions_test.py b/blackjackbot/commands/util/tests/functions_test.py new file mode 100644 index 0000000..d70eb1b --- /dev/null +++ b/blackjackbot/commands/util/tests/functions_test.py @@ -0,0 +1,29 @@ +# -*- coding: utf-8 -*- +import unittest + +from blackjackbot import start_callback_handler +from blackjackbot.commands.util import get_join_keyboard, get_start_keyboard + + +class CommandsUtilFunctionsTest(unittest.TestCase): + + def test_start_keyboard_matches_start_handler(self): + """The start button shown when a lobby is full must be handled by the start callback handler""" + game_id = 1234567 + keyboard = get_start_keyboard(game_id, "en") + start_button = keyboard.inline_keyboard[0][0] + + self.assertEqual("start_{}".format(game_id), start_button.callback_data) + self.assertTrue(start_callback_handler.pattern.match(start_button.callback_data)) + + def test_join_keyboard_start_button_matches_start_handler(self): + """The start button of the join keyboard must be handled by the start callback handler""" + game_id = 7654321 + keyboard = get_join_keyboard(game_id, "en") + start_button = keyboard.inline_keyboard[0][1] + + self.assertTrue(start_callback_handler.pattern.match(start_button.callback_data)) + + +if __name__ == '__main__': + unittest.main()