Example #1
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()
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"
            )
Example #3
0
    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)
Example #4
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
Example #5
0
 def setUp(self):
     self.game = Game()
     self.player = Player(game=self.game, chat_id=0)
     self.game.add_player(self.player)
Example #6
0
 def tearDown(self):
     self.game = Game()
     self.player = Player(game=self.game, chat_id=0)
     self.game.add_player(self.player)
Example #7
0
 def tearDown(self):
     self.game = Game()
Example #8
0
 def setUp(self):
     self.game = Game()
Example #9
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)