Пример #1
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"
            )
Пример #2
0
 def add_player(self, player: Player):
     if self.players.__len__() == 0:
         self.players.append(player)
         self.current_player = player
     else:
         self.players[0].prev = player
         self.players[self.players.__len__() - 1].next = player
         player.prev = self.players[self.players.__len__() - 1]
         player.next = self.players[0]
         self.players.append(player)
Пример #3
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()
Пример #4
0
 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)
Пример #5
0
def test_jump_actions():
    player1 = Player("player1")
    player2 = Player("player2")
    players = [player1, player2]

    card1 = NumberedCard(color="BLUE", number="1")
    card2 = NumberedCard(color="RED", number="1")
    card3 = JumpCard(color="BLUE")

    assert card3.actions(card1, 0, players, Deck()) == 1
    assert card3.actions(card1, 1, players, Deck()) == 0

    with pytest.raises(UnoRuleException):
        card3.actions(card2, 1, players, Deck())
Пример #6
0
 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)
Пример #7
0
def test_plustwo_actions():
    player1 = Player("player1")
    player2 = Player("player2")
    players = [player1, player2]

    card1 = PlusTwoCard(color="BLUE")
    card2 = PlusTwoCard(color="BLUE")
    card3 = PlusTwoCard(color="RED")

    assert card2.actions(card1, 0, players, Deck()) == 0
    assert len(player2.cards) == 2

    with pytest.raises(UnoRuleException):
        card3.actions(card1, 1, players, Deck())
Пример #8
0
 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)
Пример #9
0
 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)
Пример #10
0
def test_joker_actions():
    player1 = Player("player1")
    player2 = Player("player2")
    players = [player1, player2]

    card1 = JokerCard()
    card2 = JokerCard()
    card3 = NumberedCard(color="BLUE", number="1")

    card2.setColor("BLUE")
    assert card2.actions(card1, 0, players, Deck()) == 0
    assert card3.actions(card2, 0, players, Deck()) == 0

    with pytest.raises(UnoRuleException):
        card3.actions(card1, 1, players, Deck())
Пример #11
0
 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)
Пример #12
0
 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)
Пример #13
0
 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)
Пример #14
0
 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)
Пример #15
0
def test_numbered_actions():
    player1 = Player("player1")
    player2 = Player("player2")
    players = [player1, player2]

    card1 = NumberedCard(color="BLUE", number="1")
    card2 = NumberedCard(color="BLUE", number="2")
    card3 = NumberedCard(color="RED", number="2")

    # positive cases
    assert card2.actions(card1, 0, players, Deck()) == 0
    assert card3.actions(card2, 1, players, Deck()) == 1

    # negative case
    with pytest.raises(UnoRuleException):
        card3.actions(card1, 1, players, Deck())
Пример #16
0
def test_inverted_actions():
    player1 = Player("player1")
    player2 = Player("player2")
    players = [player1, player2]

    card1 = InvertedCard(color="BLUE")
    card2 = InvertedCard(color="BLUE")
    card3 = InvertedCard(color="RED")
    card4 = NumberedCard(color="BLUE", number="2")

    assert card2.actions(card1, 0, players, Deck()) == 1
    assert players == [player2, player1]
    assert card3.actions(card1, 1, players, Deck()) == 0
    assert players == [player1, player2]

    with pytest.raises(UnoRuleException):
        card3.actions(card4, 1, players, Deck())
Пример #17
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
Пример #18
0
 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)
Пример #19
0
def test_jokerplusfour_actions():
    player1 = Player("player1")
    player2 = Player("player2")
    players = [player1, player2]

    card1 = JokerPlusFourCard()
    card2 = JokerPlusFourCard()
    card3 = NumberedCard(color="RED", number="1")
    card4 = NumberedCard(color="BLUE", number="1")

    card1.setColor(color="BLUE")
    assert card2.actions(card1, 0, players, Deck()) == 0
    assert len(player2.cards) == 4

    with pytest.raises(UnoRuleException):
        card3.actions(card1, 1, players, Deck())

    assert card4.actions(card1, 0, players, Deck()) == 0
Пример #20
0
 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)
Пример #21
0
 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)
Пример #22
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)
Пример #23
0
    def post(self):
        # parse args
        reqparse = RequestParser()
        reqparse.add_argument('name',
                              type=str,
                              required=True,
                              location='json',
                              help="Player name")
        args = reqparse.parse_args()

        # new player
        new = Player(args.name)
        current_app.config.players[new.id] = new
        access_token = create_access_token(identity=new.id)
        return make_response(jsonify(access_token=access_token))
Пример #24
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)
Пример #25
0
 def setUp(self):
     self.game = Game()
     self.player = Player(game=self.game, chat_id=0)
     self.game.add_player(self.player)
Пример #26
0
 def tearDown(self):
     self.game = Game()
     self.player = Player(game=self.game, chat_id=0)
     self.game.add_player(self.player)