Skip to content

Commit 997d655

Browse files
authored
Merge pull request #115 from d-Rickyy-b/claude/project-thread-gbpez2
fix(game): don't freeze group games when the next player has blackjack
2 parents 3d93215 + 948dce2 commit 997d655

2 files changed

Lines changed: 54 additions & 8 deletions

File tree

‎blackjackbot/commands/game/functions.py‎

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -46,11 +46,11 @@ def players_turn(update, context):
4646
if player.has_blackjack():
4747
text = (translator("your_cards_are") + "\n\n" + translator("got_blackjack")).format(user_mention, player.cardvalue, player_cards)
4848
update.effective_message.reply_text(text=text, parse_mode=ParseMode.HTML, reply_markup=None)
49-
next_player(update, context)
49+
_advance_turn(update, context)
5050
elif player.cardvalue == 21:
5151
text = (translator("your_cards_are") + "\n\n" + translator("got_21")).format(user_mention, player.cardvalue, player_cards)
5252
update.effective_message.reply_text(text=text, parse_mode=ParseMode.HTML, reply_markup=None)
53-
next_player(update, context)
53+
_advance_turn(update, context)
5454
else:
5555
text = translator("your_cards_are").format(user_mention, player.cardvalue, player_cards)
5656
update.effective_message.reply_text(text=text, parse_mode=ParseMode.HTML, reply_markup=get_game_keyboard(game.id, lang_id))
@@ -65,11 +65,23 @@ def next_player(update, context):
6565

6666
game = GameStore().get_game(chat.id)
6767

68-
try:
69-
if user.id != game.get_current_player().user_id:
70-
update.callback_query.answer(translator("mp_not_your_turn_callback").format(user.first_name))
71-
return
68+
if user.id != game.get_current_player().user_id:
69+
update.callback_query.answer(translator("mp_not_your_turn_callback").format(user.first_name))
70+
return
71+
72+
_advance_turn(update, context)
7273

74+
75+
def _advance_turn(update, context):
76+
"""Hand the turn to the next player (or the dealer) without checking who triggered the update.
77+
Used when a player's turn ends automatically, e.g. because they were dealt a blackjack."""
78+
chat = update.effective_chat
79+
lang_id = Database().get_lang_id(chat.id)
80+
translator = Translator(lang_id=lang_id)
81+
82+
game = GameStore().get_game(chat.id)
83+
84+
try:
7385
remove_inline_keyboard(update, context)
7486
game.next_player()
7587
except NoPlayersLeftException:

‎blackjackbot/commands/game/tests/functions_test.py‎

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
# -*- coding: utf-8 -*-
22
import unittest
3-
from unittest.mock import Mock
3+
from unittest.mock import Mock, patch
44

5-
from blackjackbot.commands.game.functions import is_button_affiliated
5+
from blackjack.game import BlackJackGame, Card
6+
from blackjackbot.commands.game.functions import is_button_affiliated, next_player
7+
from blackjackbot.gamestore import GameStore
68

79

810
class GameCommandsFunctionsTest(unittest.TestCase):
@@ -32,6 +34,38 @@ def test_is_button_affiliated_negative(self):
3234
self.assertFalse(result)
3335
update.callback_query.answer.assert_called_once()
3436

37+
@patch("blackjackbot.commands.util.decorators.Database")
38+
@patch("blackjackbot.commands.game.functions.Database")
39+
def test_next_player_skips_player_with_blackjack(self, db_mock, decorator_db_mock):
40+
"""When the next player was dealt a blackjack, their turn must be skipped even though another user triggered the update"""
41+
db_mock.return_value.get_lang_id.return_value = "en"
42+
decorator_db_mock.return_value.get_lang_id.return_value = "en"
43+
chat_id = -1001234
44+
45+
game = BlackJackGame(gametype=BlackJackGame.Type.MULTIPLAYER_GROUP)
46+
game.add_player(1, "A")
47+
game.add_player(2, "B")
48+
game.add_player(3, "C")
49+
game.running = True
50+
game.players[0]._cards = [Card(0), Card(1)] # 2 + 3
51+
game.players[1]._cards = [Card(12), Card(9)] # Ace + Jack -> blackjack
52+
game.players[2]._cards = [Card(2), Card(3)] # 4 + 5
53+
game.dealer._cards = [Card(8), Card(7)]
54+
55+
game_store = GameStore()
56+
game_store.add_game(chat_id, game)
57+
self.addCleanup(game_store.remove_game, chat_id)
58+
59+
update = Mock()
60+
update.effective_chat.id = chat_id
61+
update.effective_user.id = 1 # Player A presses "stand"
62+
63+
next_player(update, Mock())
64+
65+
# B was skipped automatically, so it's now C's turn
66+
self.assertEqual(game.players[2], game.get_current_player())
67+
update.callback_query.answer.assert_not_called()
68+
3569

3670
if __name__ == '__main__':
3771
unittest.main()

0 commit comments

Comments
 (0)