Ejemplo n.º 1
0
    async def get_user_by_id(user_id: int) -> Optional[PombotUser]:
        """Return a single user by its userID."""
        query = f"""
            SELECT * FROM {Config.USERS_TABLE}
            WHERE userID=%s;
        """

        async with _mysql_database_cursor() as cursor:
            await cursor.execute(query, (user_id, ))
            row = await cursor.fetchone()

        if not row:
            raise war_crimes.UserDoesNotExistError()

        return PombotUser(*row)
Ejemplo n.º 2
0
    async def get_users_by_id(user_ids: List[int]) -> Set[PombotUser]:
        """Return a list of users from a list of userID's.

        This is a small optimization function to call the storage a single
        time to return multiple unique users, instead of calling it one time
        for each user.
        """
        if not user_ids:
            return []

        query = [f"SELECT * FROM {Config.USERS_TABLE}"]
        values = []

        for user_id in user_ids:
            query += ["WHERE userID=%s"]
            values += [user_id]

        query_str = _replace_further_occurances(" ".join(query), "WHERE", "OR")

        async with _mysql_database_cursor() as cursor:
            await cursor.execute(query_str, values)
            rows = await cursor.fetchall()

        return {PombotUser(*r) for r in rows}
    async def test_heavy_attack_success_rate(
        self,
        random_mock: Mock,
        get_user_by_id_mock: Mock,
        get_actions_mock: Mock,
    ):
        """Generically test is_action_successful when doing a heavy attack."""
        class MockPom(MagicMock):
            """A fake Pom object.

            When gathering chances for heavy attacks, the previous heavy
            attack is consulted. For each previous unsuccesful attack, there
            is a configurable increase in the chance for the next attack.

            For this test, we expect the future chances to remain constant, so
            each previous attack must be successful.
            """
            @staticmethod
            def was_successful():
                """Report this action to be successful."""
                return True

        get_user_by_id_mock.return_value = PombotUser(
            user_id=1234,
            timezone=timezone(timedelta()),
            team="Team",
            inventory_string="inventory",
            player_level=1,
            attack_level=1,
            heavy_attack_level=1,
            defend_level=1,
        )

        dice_rolls_and_expected_outcomes = {
            MockPom(1):  [(0.1, 0.2, 0.3),
                          (TRU, TRU, FLS)],
            MockPom(2):  [(0.1, 0.2, 0.3),
                          (TRU, TRU, FLS)],
            MockPom(3):  [(0.1, 0.2, 0.3),
                          (TRU, TRU, FLS)],
            MockPom(4):  [(0.1, 0.2, 0.3),
                          (TRU, TRU, FLS)],
            MockPom(5):  [(0.1, 0.2, 0.3),
                          (TRU, TRU, FLS)],
            MockPom(6):  [(0.1, 0.2, 0.3),
                          (TRU, TRU, FLS)],
            MockPom(7):  [(0.1, 0.2, 0.3),
                          (TRU, TRU, FLS)],
            MockPom(8):  [(0.1, 0.2, 0.3),
                          (TRU, TRU, FLS)],
            MockPom(9):  [(0.1, 0.2, 0.3),
                          (TRU, FLS, FLS)],
            MockPom(10): [(0.1, 0.2, 0.3),
                          (TRU, FLS, FLS)],
            MockPom(11): [(0.1, 0.2, 0.3),
                          (FLS, FLS, FLS)],
            MockPom(12): [(0.1, 0.2, 0.3),
                          (FLS, FLS, FLS)],
        }
        user = MagicMock()
        is_heavy_attack = True
        timestamp = datetime.now()
        actions = []

        for pom_number, settings in dice_rolls_and_expected_outcomes.items():
            actions.append(pom_number)
            get_actions_mock.return_value = actions
            print(f"len(actions) = {len(actions)}")

            for dice_roll, expected_outcome in zip(*settings):
                random_mock.return_value = dice_roll
                actual_outcome = await is_action_successful(
                    user, timestamp, is_heavy_attack)

                self.assertEqual(
                    expected_outcome, actual_outcome,
                    f"pom_number: {pom_number}, dice_roll: {dice_roll}")