コード例 #1
0
class TestPlayer(unittest.TestCase):
    def setUp(self):
        self.game = Game()
        self.player = Player(game=self.game, chat_id=0)
        self.game.add_player(self.player)

    def tearDown(self):
        self.game = Game()
        self.player = Player(game=self.game, chat_id=0)
        self.game.add_player(self.player)

    def test_not_human(self):
        self.assertFalse(self.player.is_human)

    def test_empty_pile(self):
        self.game.deck.cards = list()
        with self.assertRaises(EmptyDeckError):
            self.player.draw()

    def test_draw(self):
        self.card_count = self.player.cards.__len__()
        self.player.draw()
        self.assertGreater(self.player.cards.__len__(), self.card_count)

    def test_wrong_card(self):
        self.player.is_human = True
        self.wrong_card = Card(color="wrong", value="wrong")
        with self.assertRaises(WrongCardError):
            self.player.play(self.wrong_card)
コード例 #2
0
def uno_game_handler(update: Update, chat_id: str, players: list, game: Game):
    bot.send_message(chat_id=chat_id, text="Your opponent: Boss!")
    for player in players:
        game.add_player(player)
    game.started = True
    if game.current_player.is_human:
        uno_play_msg(chat_id=chat_id, game=game)
    else:
        game.current_player.play()
        bot.send_message(chat_id=chat_id,
                         text=f"{game.current_player.name} has {game.current_player.cards.__len__()} cards")
コード例 #3
0
ファイル: game_api.py プロジェクト: uno-project/backend
    def post(self):
        """
        Creates a game
        """
        try:
            game = Game()
            game.addPlayer(current_app.config.players[get_jwt_identity()])
        except UnoRuleException as e:
            return make_response(jsonify(message=str(e)),
                                 HTTPStatus.BAD_REQUEST)

        current_app.config.games[game.id] = game
        return make_response(jsonify(gameId=game.id), HTTPStatus.CREATED)
コード例 #4
0
class GameManager:
    game = Game()
    user = Player(game=game, is_human=False, name='Вася Пупкин')
    computer = Player(game=game, is_human=False, name='Дед')
    game.add_player(user)
    game.add_player(computer)
    game.started = True
    while game.started:
        if game.current_player.is_human:
            print(game.current_player.next.is_human)
            print(f"Computer has {computer.cards.__len__()} cards")
            print(
                f"Play the special card or {game.last_card.color} card or value {game.last_card.value} card. Your deck:"
            )
            print(user.view_deck())
            msg = input("Type index or X (draw 1): ")
            if msg == "X":
                user.draw()
                game.next_turn()
            else:
                user.play(user.cards[int(msg)])
        else:
            game.current_player.play()
            print(
                f"{game.current_player.name} has {game.current_player.cards.__len__()} cards"
            )
コード例 #5
0
def test_game_one_or_ten_players():
    # 1 player: error
    player1 = Player("player1")
    with pytest.raises(UnoRuleException):
        game = Game()
        game.players.append(player1)
        game.start()

    # 11 players: error
    players = [Player(f"player{i}") for i in range(11)]
    with pytest.raises(UnoRuleException):
        game = Game()
        game.players = [player for player in players]
        game.start()
コード例 #6
0
def test_game_initial():
    player1 = Player("player1")
    player2 = Player("player2")

    # start game
    global game
    game = Game()
    game.players.append(player2)
    game.players.append(player1)
    game.start()

    # if game.id is not a UUID, will throw exception
    assert isinstance(UUID(game.id), UUID)

    assert len(game.deck.cards) == int(DECK_SIZE - (2 * STARTING_CARDS))

    for player in game.players:
        assert len(player.cards) == STARTING_CARDS
コード例 #7
0
 def setUp(self):
     self.game = Game()
     self.player = Player(game=self.game, chat_id=0)
     self.game.add_player(self.player)
コード例 #8
0
 def tearDown(self):
     self.game = Game()
     self.player = Player(game=self.game, chat_id=0)
     self.game.add_player(self.player)
コード例 #9
0
 def tearDown(self):
     self.game = Game()
コード例 #10
0
 def setUp(self):
     self.game = Game()
コード例 #11
0
class TestGame(unittest.TestCase):
    def setUp(self):
        self.game = Game()

    def tearDown(self):
        self.game = Game()

    def test_add_player(self):
        self.player = Player(game=self.game,
                             chat_id=0,
                             is_human=True,
                             name="John Doe")
        self.game.add_player(player=self.player)
        self.assertIn(self.player, self.game.players)

    def test_is_player(self):
        self.player = Player(game=self.game,
                             chat_id=0,
                             is_human=True,
                             name="John Doe")
        self.game.add_player(player=self.player)
        self.assertIsInstance(self.game.current_player, Player)

    @mock.patch.object(game.Player, 'play', lambda self: simple_func())
    def test_next_turn_perfect(self):
        self.player_1 = Player(game=self.game,
                               chat_id=0,
                               is_human=False,
                               name="John Doe")
        self.player_2 = Player(game=self.game,
                               chat_id=0,
                               is_human=False,
                               name="Bot 1")
        self.player_3 = Player(game=self.game,
                               chat_id=0,
                               is_human=False,
                               name="Bot 2")
        self.game.add_player(player=self.player_1)
        self.game.add_player(player=self.player_2)
        self.game.add_player(player=self.player_3)
        for i in range(5):
            self.game.next_turn()
        self.assertEqual(self.game.current_player, self.player_3)

    @mock.patch.object(game.Player, 'play', lambda self: simple_func())
    def test_next_turn_draw(self):
        self.player_1 = Player(game=self.game,
                               chat_id=0,
                               is_human=False,
                               name="John Doe")
        self.player_2 = Player(game=self.game,
                               chat_id=0,
                               is_human=False,
                               name="Bot 1")
        self.game.add_player(player=self.player_1)
        self.game.add_player(player=self.player_2)
        self.game.draw_counter = 1  # first player draws one card
        self.game.next_turn()
        self.assertEqual(self.game.current_player, self.player_2)

    @mock.patch.object(game.Player, 'play', lambda self: simple_func())
    def test_next_turn_human(self):
        self.player_1 = Player(game=self.game,
                               chat_id=0,
                               is_human=True,
                               name="John Doe")
        self.player_2 = Player(game=self.game,
                               chat_id=0,
                               is_human=False,
                               name="Bot 1")
        self.game.add_player(player=self.player_1)
        self.game.add_player(player=self.player_2)
        with mock.patch('telegram_commands.uno_play_msg', return_value=None):
            self.game.next_turn()
            self.assertEqual(self.game.round, 2)

    @mock.patch.object(game.Player, 'play', lambda self: simple_func())
    def test_skip_next(self):
        self.player_1 = Player(game=self.game,
                               chat_id=0,
                               is_human=False,
                               name="John Doe")
        self.player_2 = Player(game=self.game,
                               chat_id=0,
                               is_human=False,
                               name="Bot 1")
        self.player_3 = Player(game=self.game,
                               chat_id=0,
                               is_human=False,
                               name="Bot 2")
        self.game.add_player(player=self.player_1)
        self.game.add_player(player=self.player_2)
        self.game.add_player(player=self.player_3)
        self.game.skip_next()
        self.assertEqual(self.game.current_player, self.player_3)

    def test_skip_next_draw(self):
        self.player_1 = Player(game=self.game,
                               chat_id=0,
                               is_human=False,
                               name="John Doe")
        self.player_2 = Player(game=self.game,
                               chat_id=0,
                               is_human=False,
                               name="Bot 1")
        self.game.add_player(player=self.player_1)
        self.game.add_player(player=self.player_2)
        self.game.draw_counter = 1
        with mock.patch('telegram_commands.uno_play_msg', return_value=None):
            self.game.skip_next()
            self.assertEqual(self.player_2.cards.__len__(), 8)

    def test_skip_next_human(self):
        self.player_1 = Player(game=self.game,
                               chat_id=0,
                               is_human=True,
                               name="John Doe")
        self.player_2 = Player(game=self.game,
                               chat_id=0,
                               is_human=False,
                               name="Bot 1")
        self.game.add_player(player=self.player_1)
        self.game.add_player(player=self.player_2)
        self.game.draw_counter = 1
        with mock.patch('telegram_commands.bot') as mock_bot:
            mock_bot.send_message.return_value = None
            self.game.skip_next()
            self.assertEqual(self.game.players[1].cards.__len__(), 8)

    @mock.patch.object(game.Player, 'play', lambda self: simple_func())
    def test_play_card(self):
        with mock.patch('uno.game.Game.get_board', return_value=None):
            self.player_1 = Player(game=self.game,
                                   chat_id=0,
                                   is_human=False,
                                   name="John Doe")
            self.player_2 = Player(game=self.game,
                                   chat_id=0,
                                   is_human=False,
                                   name="Bot 1")
            self.game.add_player(player=self.player_1)
            self.game.add_player(player=self.player_2)
            self.last = self.game.last_card
            self.game.play_card(self.game.last_card)
            self.assertIn(self.last, self.game.deck.discard_pile)

    @mock.patch.object(game.Player, 'play', lambda self: simple_func())
    def test_choose_color(self):
        with mock.patch('uno.game.bot') as mock_bot:
            mock_bot.send_message.return_value = None
            self.player_1 = Player(game=self.game,
                                   chat_id=0,
                                   is_human=False,
                                   name="John Doe")
            self.game.add_player(player=self.player_1)
            self.game.choose_color(GREEN)
            self.assertIs(self.game.last_card.color, GREEN)

    @mongomock.patch(servers=(('testserver.com', 27017), ))
    def test_get_board(self):
        self.player_1 = Player(game=self.game,
                               chat_id=0,
                               is_human=False,
                               name="John Doe")
        self.game.add_player(player=self.player_1)
        with mock.patch('database.pymongo.MongoClient') as mock_client:
            mock_client.return_value = pymongo.MongoClient('testserver.com')
            self.db = database.UserDataBase()
            with mock.patch('uno.game.Image.new') as mock_new_img:
                mock_new_img.paste.return_value = None
                mock_new_img.save.return_value = None
                with mock.patch('uno.game.Image.open') as mock_img:
                    mock_img.width = 480
                    mock_img.length = 860
                    mock_img.convert.return_value = None
                    self.db.db_user_actions = pymongo.MongoClient(
                        'testserver.com')['user_actions']
                    with mock.patch('auxiliary_functions.db_user_action',
                                    new=self.db):
                        with mock.patch('telegram_commands.bot') as mock_bot:
                            mock_bot.send_message.return_value = None
                            with mock.patch('builtins.open',
                                            return_value=None):
                                with mock.patch('uno.game.bot.send_photo',
                                                return_value=None):
                                    self.assertIn(self.game.get_board(),
                                                  self.game.temp_messages)
コード例 #12
0
    def handle_keyboard_callback(update: Update, context=None):  # Gets callback_data from the pushed button
        query = update.callback_query  # Gets query from callback
        data = query.data  # callback_data of pushed button
        chat_id = update.effective_message.chat_id  # chat id for sending messages

        if data == CALLBACK_BUTTON_01:
            InlineCallback.change_contrast_level(factor=0.1, base_img='initial_user_images/initial.jpg',
                                                 res_img='initial_user_images/initial.jpg', chat_id=chat_id,
                                                 send='initial_user_images/initial.jpg')
            return CALLBACK_BUTTON_01

        elif data == CALLBACK_BUTTON_05:
            InlineCallback.change_contrast_level(factor=0.5, base_img='initial_user_images/initial.jpg',
                                                 res_img='initial_user_images/initial.jpg', chat_id=chat_id,
                                                 send='initial_user_images/initial.jpg')
            return CALLBACK_BUTTON_05

        elif data == CALLBACK_BUTTON_m01:
            InlineCallback.change_contrast_level(factor=-0.1, base_img='initial_user_images/initial.jpg',
                                                 res_img='initial_user_images/initial.jpg', chat_id=chat_id,
                                                 send='initial_user_images/initial.jpg')
            return CALLBACK_BUTTON_m01

        elif data == CALLBACK_BUTTON_m05:
            InlineCallback.change_contrast_level(factor=-0.5, base_img='initial_user_images/initial.jpg',
                                                 res_img='initial_user_images/initial.jpg', chat_id=chat_id,
                                                 send='initial_user_images/initial.jpg')
            return CALLBACK_BUTTON_m05

        elif data == CALLBACK_BUTTON_FIN:
            InlineCallback.change_contrast_level(factor=0.0, base_img='initial_user_images/initial.jpg',
                                                 res_img='result_user_images/res.jpg', chat_id=chat_id,
                                                 send='result_user_images/res.jpg')
            reply_markup = ReplyKeyboardRemove()  # Remove keyboard
            bot.send_message(chat_id=chat_id,
                             text='Here you are!',
                             reply_markup=reply_markup)

        elif data == CALLBACK_BUTTON_COVID19_RU:
            page = requests.get("https://yandex.ru/maps/covid19?ll=41.775580%2C54.894027&z=3")
            tree = html.fromstring(page.content)
            rows = tree.xpath('//div[@class="covid-panel-view__item"]')
            place = 0
            for row in rows:
                region = row.xpath('//div[@class="covid-panel-view__item-name"]/text()')[place]
                cases = row.xpath('//div[@class="covid-panel-view__item-cases"]/text()')[place].replace("\xa0", "")
                place += 1
                bot.send_message(chat_id=chat_id, text=f'<b>{place}.</b> '
                                                       f'{region}: {cases} cases\n',
                                 parse_mode=telegram.ParseMode.HTML)
                if place == 5:
                    break

        elif data == CALLBACK_BUTTON_NEWS_01:

            Covid.send_message(bot=bot, chat_id=chat_id, value=0)

            bot.send_message(chat_id=update.effective_message.chat_id,
                             text="Do you want to read more?",
                             reply_markup=InlineKeyboardFactory.get_inline_keyboard_more_information())

        elif data == CALLBACK_BUTTON_NEWS_02:  # Choose second news

            Covid.send_message(bot=bot, chat_id=chat_id, value=1)

            bot.send_message(chat_id=update.effective_message.chat_id,
                             text="Do you want to read more?",
                             reply_markup=InlineKeyboardFactory.get_inline_keyboard_more_information())

        elif data == CALLBACK_BUTTON_NEWS_03:  # Choose second news

            Covid.send_message(bot=bot, chat_id=chat_id, value=2)

            bot.send_message(chat_id=update.effective_message.chat_id,
                             text="Do you want to read more?",
                             reply_markup=InlineKeyboardFactory.get_inline_keyboard_more_information())

        elif data == CALLBACK_BUTTON_NEWS_04:  # Choose other news

            InlineKeyboardFactory.get_inline_news_keyboard()
            temp = bot.send_message(chat_id=update.effective_message.chat_id,
                                    text='Choose news',
                                    reply_markup=InlineKeyboardFactory.get_inline_news_keyboard())
            bot.delete_message(chat_id, temp.message_id - 1)

        elif data == CALLBACK_BUTTON_NEWS_06:  # Choose read more about certain news

            temp = bot.send_message(chat_id=chat_id,
                                    text=Covid.get_href_news())

            bot.delete_message(chat_id, temp.message_id - 1)

        elif data == CALLBACK_BUTTON_NEWS_07:
            temp = bot.send_message(chat_id=chat_id,
                                    text="I will be waiting for you here")
            bot.delete_message(chat_id, temp.message_id - 1)

        elif data == CALLBACK_BUTTON_STAYHOME:
            InlineCallback.update_data({"at_home": True}, f"personal_{chat_id}.json")
            bot.send_message(chat_id=chat_id, text="Perfect! Now, select your blood type...",
                             reply_markup=InlineKeyboardFactory.get_inline_bloodtype())

        elif data == CALLBACK_BUTTON_NOSTAY:
            InlineCallback.update_data({"at_home": False}, f"personal_{chat_id}.json")
            bot.send_message(chat_id=chat_id,
                             text="I strongly recommend you to stay home! Now, select your blood type...",
                             reply_markup=InlineKeyboardFactory.get_inline_bloodtype())

        elif data == CALLBACK_BUTTON_BLOOD_I:
            InlineCallback.update_data({"blood": 1}, f"personal_{chat_id}.json")
            bot.send_message(chat_id=chat_id,
                             text="Thanks! Now I can calculate the coronavirus pick up probability for you.")
            bot.send_message(chat_id=chat_id,
                             text=f"The probability of you getting COVID-19 is around {tg.calc_probability(chat_id)}%")

        elif data == CALLBACK_BUTTON_BLOOD_II:
            InlineCallback.update_data({"blood": 2}, f"personal_{chat_id}.json")
            bot.send_message(chat_id=chat_id,
                             text="Thanks! Now I can calculate the coronavirus pick up probability for you.")
            bot.send_message(chat_id=chat_id,
                             text=f"The probability of you getting COVID-19 is around {tg.calc_probability(chat_id)}%")

        elif data == CALLBACK_BUTTON_BLOOD_III or data == CALLBACK_BUTTON_BLOOD_IV:
            InlineCallback.update_data({"blood": 3}, f"personal_{chat_id}.json")
            bot.send_message(chat_id=chat_id,
                             text="Thanks! Now I can calculate the coronavirus pick up probability for you.")
            bot.send_message(chat_id=chat_id,
                             text=f"The probability of you getting COVID-19 is around {tg.calc_probability(chat_id)}%")

        elif data == CALLBACK_BUTTON_UNO_BOT_1:
            game = Game()
            tg.GAME = game
            tg.CHAT_ID = chat_id
            tg.uno_game_handler(update=update, chat_id=chat_id, players=[Player(chat_id=chat_id,
                                                                                game=game, is_human=True, name='You'),
                                                                         Player(chat_id=chat_id,
                                                                                game=game, is_human=False,
                                                                                name='Boss')], game=game)

        elif data == CALLBACK_BUTTON_UNO_BOT_2:
            game = Game()
            tg.GAME = game
            tg.CHAT_ID = chat_id
            tg.uno_game_handler(update=update, chat_id=chat_id, players=[Player(chat_id=chat_id,
                                                                                game=game, is_human=True, name='You'),
                                                                         Player(chat_id=chat_id,
                                                                                game=game, is_human=False,
                                                                                name='Boss Intel'),
                                                                         Player(chat_id=chat_id,
                                                                                game=game, is_human=False,
                                                                                name='Boss AMD')], game=game)

        elif data == CALLBACK_BUTTON_UNO_DRAW_ONE:
            tg.GAME.current_player.draw()
            tg.GAME.next_turn()

        elif data.__len__() < 3:
            for temp_msg in tg.GAME.temp_messages:
                if temp_msg:
                    bot.delete_message(chat_id=chat_id, message_id=temp_msg.message_id)
            tg.GAME.temp_messages = []
            if tg.GAME.current_player.cards[int(data)].value == "draw_2" or tg.GAME.current_player.cards[int(data)].value == "skip":
                tg.GAME.current_player.play(tg.GAME.current_player.cards[int(data)])
                '''if tg.GAME.current_player.is_human:
                    tg.uno_play_msg(chat_id=chat_id, game=tg.GAME)
                else:
                    tg.GAME.current_player.play()'''
            else:
                tg.GAME.current_player.play(tg.GAME.current_player.cards[int(data)])

        elif data == CALLBACK_BUTTON_UNO_RED:
            tg.GAME.choose_color_static(tg.GAME, RED)

        elif data == CALLBACK_BUTTON_UNO_GREEN:
            tg.GAME.choose_color_static(tg.GAME, GREEN)

        elif data == CALLBACK_BUTTON_UNO_BLUE:
            tg.GAME.choose_color_static(tg.GAME, BLUE)

        elif data == CALLBACK_BUTTON_UNO_YELLOW:
            tg.GAME.choose_color_static(tg.GAME, YELLOW)