From f8a12d224040bcd6d1fe619e434ae4eb3586ee23 Mon Sep 17 00:00:00 2001 From: Claude Date: Thu, 8 Oct 2026 21:28:00 +0000 Subject: [PATCH] fix(stats): store players who join a group game via the join button Only /start added users to the database. Players who joined a group game with the join button had no users row, so their played and won games were never recorded and /stats showed "No statistics found!". Co-Authored-By: Claude Opus 5.5 (1M context) Claude-Session: https://claude.ai/code/session_01KouGQtM6VBedfBdNr8WFX5 --- blackjackbot/commands/game/commands.py | 2 + .../commands/game/tests/commands_test.py | 43 +++++++++++++++++++ 2 files changed, 45 insertions(+) create mode 100644 blackjackbot/commands/game/tests/commands_test.py diff --git a/blackjackbot/commands/game/commands.py b/blackjackbot/commands/game/commands.py index 428b16e..5b25a09 100644 --- a/blackjackbot/commands/game/commands.py +++ b/blackjackbot/commands/game/commands.py @@ -115,6 +115,8 @@ def join_callback(update, context): try: game.add_player(user.id, user.first_name) + # Players joining a group game never sent /start, so they might not be stored in the database yet + Database().add_user(user.id, user.language_code, user.first_name, user.last_name, user.username) update.effective_message.edit_text(text=translator("mp_request_join").format(game.get_player_list()), reply_markup=get_join_keyboard(game.id, lang_id)) update.callback_query.answer(translator("mp_join_callback").format(user.first_name)) diff --git a/blackjackbot/commands/game/tests/commands_test.py b/blackjackbot/commands/game/tests/commands_test.py new file mode 100644 index 0000000..82c34f1 --- /dev/null +++ b/blackjackbot/commands/game/tests/commands_test.py @@ -0,0 +1,43 @@ +# -*- coding: utf-8 -*- +import unittest +from unittest.mock import Mock, patch + +from blackjack.game import BlackJackGame +from blackjackbot.commands.game.commands import join_callback +from blackjackbot.gamestore import GameStore + + +class GameCommandsTest(unittest.TestCase): + + @patch("blackjackbot.commands.util.decorators.Database") + @patch("blackjackbot.commands.game.commands.Database") + def test_join_callback_stores_user(self, db_mock, decorator_db_mock): + """Players joining a group game via the join button must be stored in the database, so their stats are tracked""" + db_mock.return_value.get_lang_id.return_value = "en" + decorator_db_mock.return_value.get_lang_id.return_value = "en" + chat_id = -1004321 + + game = BlackJackGame(gametype=BlackJackGame.Type.MULTIPLAYER_GROUP) + game.add_player(1, "A") + + game_store = GameStore() + game_store.add_game(chat_id, game) + self.addCleanup(game_store.remove_game, chat_id) + + update = Mock() + update.effective_chat.id = chat_id + update.effective_user.id = 2 + update.effective_user.first_name = "B" + update.effective_user.last_name = "Bee" + update.effective_user.username = "bbb" + update.effective_user.language_code = "de" + update.callback_query.data = "join_{}".format(game.id) + + join_callback(update, Mock()) + + self.assertEqual(2, len(game.players)) + db_mock.return_value.add_user.assert_called_once_with(2, "de", "B", "Bee", "bbb") + + +if __name__ == '__main__': + unittest.main()