Beispiel #1
0
    async def test_get_average_poms_does_not_consider_bribes(self):
        """Test get_average_poms only averages attacks and defends."""
        Pomwars.AVERAGING_PERIOD_DAYS = 1
        Pomwars.MAX_FORGIVEN_DAYS = 0

        actions_from_db = [
            self.create_action(type=action_type) for action_type in flatten((
                (normal, heavy, defend, bribe)
                for normal, heavy, defend, bribe in zip(
                    [ActionType.NORMAL_ATTACK] * 10,
                    [ActionType.HEAVY_ATTACK] * 10,
                    [ActionType.DEFEND] * 10,
                    [ActionType.BRIBE] * 10,
                )))
        ]

        Storage.get_actions.return_value = actions_from_db

        actual_average = await get_average_poms(
            user=self.ctx.author,
            timestamp=datetime.now(),
        )

        # The SUT will add one placeholder action to the list to be averaged
        # which represents the action currently being processed as it won't
        # yet be in the DB.
        expected_average = 31

        self.assertEqual(expected_average, actual_average)
Beispiel #2
0
    def get_random(
        self,
        *,
        timestamp: datetime,
        team: Team,
        average_daily_actions: int,
        outcome: Outcome,
        heavy: bool,
    ) -> Attack:
        """Return a random Attack from the XMLs."""
        tags = {False: _XMLTags.NORMAL_ATTACK, True: _XMLTags.HEAVY_ATTACK}
        tier = self._get_tier_from_average_actions(average_daily_actions)

        choice = random.choice([
            action for action in flatten(
                xml.xpath(
                    f".//team[@name='{team}']/tier[@level='{tier}']/{tags[heavy]}"
                ) for xml in self._xmls)
            if action.attrib.get("outcome", Outcome.REGULAR) == outcome
        ])

        return Attack(
            team=team,
            timestamp=timestamp,
            story=choice.text.strip(),
            outcome=outcome,
            is_heavy=heavy,
        )
Beispiel #3
0
    def get_random(
        self,
        *,
        team: Union[str, Team],
        average_daily_actions: int,
        critical: bool,
        heavy: bool,
    ) -> Attack:
        """Return a random Attack from the XMLs."""
        tags = {False: _XMLTags.NORMAL_ATTACK, True: _XMLTags.HEAVY_ATTACK}
        tier = self._get_tier_from_average_actions(average_daily_actions)

        choice = random.choice([
            e for e in flatten(
                x.xpath(
                    f".//team[@name='{team}']/tier[@level='{tier}']/{tags[heavy]}"
                ) for x in self._xmls)
            if str2bool(e.attrib.get("critical", "false")) == critical
        ])

        return Attack(
            story=choice.text.strip(),
            is_heavy=heavy,
            is_critical=critical,
        )
Beispiel #4
0
    def get_random(self, team: Union[str, Team], average_daily_actions: int):
        """Return a random Defend from the XMLs."""
        tier = self._get_tier_from_average_actions(average_daily_actions)

        choice = random.choice(
            flatten(
                x.xpath(
                    f".//team[@name='{team}']/tier[@level='{tier}']/{_XMLTags.DEFEND}"
                ) for x in self._xmls))

        return Defend(story=choice.text.strip())
Beispiel #5
0
    def get_random(
        self,
        user: BotUser,
        team: Team,
        average_daily_actions: int,
        outcome: Outcome,
    ):
        """Return a random Defend from the XMLs."""
        tag = _XMLTags.DEFEND
        tier = self._get_tier_from_average_actions(average_daily_actions)

        choice = random.choice([
            action for action in flatten(
                x.xpath(f".//team[@name='{team}']/tier[@level='{tier}']/{tag}")
                for x in self._xmls)
            if action.attrib.get("outcome", Outcome.REGULAR) == outcome
        ])

        return Defend(user, team, outcome, story=choice.text.strip())
Beispiel #6
0
    def get_random(self):
        """Return a random Bribe from the XMLs."""
        choice = random.choice(
            flatten(x.xpath(f".//{_XMLTags.BRIBE}") for x in self._xmls))

        return Bribe(story=choice.text.strip())