Example #1
0
    def test_matches_endpoint_with_since(self, matches_since_payload):
        responses.add(
            responses.GET,
            "https://aoe2.net/api/matches",
            json=matches_since_payload,
            status=200,
        )

        result = self.client.matches(since=1596775000)
        assert isinstance(result, list)
        assert isinstance(result[0], MatchLobby)
        assert result == [
            MatchLobby(**lobby) for lobby in matches_since_payload
        ]

        assert len(responses.calls) == 1
        assert responses.calls[0].request.params == {
            "game": "aoe2de",
            "count": "10",
            "since": "1596775000",
        }
        assert (
            responses.calls[0].request.url ==
            "https://aoe2.net/api/matches?game=aoe2de&count=10&since=1596775000"
        )
Example #2
0
    def test_match_history_endpoint_with_profileid(
            self, match_history_profileid_payload):
        responses.add(
            responses.GET,
            "https://aoe2.net/api/player/matches",
            json=match_history_profileid_payload,
            status=200,
        )

        result = self.client.match_history(profile_id=459658)
        assert isinstance(result, list)
        assert isinstance(result[0], MatchLobby)
        assert result == [
            MatchLobby(**lobby) for lobby in match_history_profileid_payload
        ]

        assert len(responses.calls) == 1
        assert responses.calls[0].request.params == {
            "game": "aoe2de",
            "start": "0",
            "count": "10",
            "profile_id": "459658",
        }
        assert (
            responses.calls[0].request.url ==
            "https://aoe2.net/api/player/matches?game=aoe2de&start=0&count=10&profile_id=459658"
        )
Example #3
0
    def test_match_endpoint_with_matchid(self, match_matchid_payload):
        responses.add(
            responses.GET,
            "https://aoe2.net/api/match",
            json=match_matchid_payload,
            status=200,
        )

        result = self.client.match(match_id=32435313)
        assert isinstance(result, MatchLobby)
        assert result == MatchLobby(**match_matchid_payload)

        assert len(responses.calls) == 1
        assert responses.calls[0].request.params == {
            "game": "aoe2de",
            "match_id": "32435313",
        }
        assert responses.calls[
            0].request.url == "https://aoe2.net/api/match?game=aoe2de&match_id=32435313"
Example #4
0
    def test_lobbies_endpoint(self, lobbies_defaults_payload):
        responses.add(
            responses.GET,
            "https://aoe2.net/api/lobbies",
            json=lobbies_defaults_payload,
            status=200,
        )

        result = self.client.lobbies()
        assert isinstance(result, list)
        assert isinstance(result[0], MatchLobby)
        assert result == [
            MatchLobby(**lobby) for lobby in lobbies_defaults_payload
        ]

        assert len(responses.calls) == 1
        assert responses.calls[0].request.params == {"game": "aoe2de"}
        assert responses.calls[
            0].request.url == "https://aoe2.net/api/lobbies?game=aoe2de"
Example #5
0
    def test_match_endpoint_with_uuid(self, match_uuid_payload):
        responses.add(
            responses.GET,
            "https://aoe2.net/api/match",
            json=match_uuid_payload,
            status=200,
        )

        result = self.client.match(uuid="66ec2575-5ee4-d241-a1fc-d7ffeffb48b6")
        assert isinstance(result, MatchLobby)
        assert result == MatchLobby(**match_uuid_payload)

        assert len(responses.calls) == 1
        assert responses.calls[0].request.params == {
            "game": "aoe2de",
            "uuid": "66ec2575-5ee4-d241-a1fc-d7ffeffb48b6",
        }
        assert (
            responses.calls[0].request.url ==
            "https://aoe2.net/api/match?game=aoe2de&uuid=66ec2575-5ee4-d241-a1fc-d7ffeffb48b6"
        )
Example #6
0
def _unfold_match_lobby_to_dataframe(match_lobby: MatchLobby) -> pd.DataFrame:
    """
    Convert the content of a MatchLobby to a pandas DataFrame. The resulting DataFrame will have as many
    rows as there are players in the lobby, and all global attributes will be broadcasted to columns of the
    same length, making them duplicates.

    Args:
        match_lobby (MatchLobby): a MatchLobby object.

    Returns:
        A pandas DataFrame from the MatchLobby attributes, each row being global information from the
        MatchLobby as well as one of the players in the lobby.
    """
    if not isinstance(match_lobby, MatchLobby):
        logger.error(
            "Tried to use method with a parameter of type != MatchLobby")
        raise TypeError(
            "Provided parameter should be an instance of 'MatchLobby'")

    logger.trace("Unfolding MatchLobby.players contents to DataFrame")
    dframe = pd.DataFrame(match_lobby.players)
    dframe = _export_tuple_elements_to_column_values_format(dframe)
    dframe = dframe.rename(columns={"name": "player"})

    logger.trace("Broadcasting global MatchLobby attributes")
    attributes_df = pd.DataFrame()
    for attribute, value in match_lobby.dict().items():
        if attribute != "players":
            attributes_df[attribute] = [value] * len(dframe)
    dframe = attributes_df.join(dframe, how="outer")

    logger.trace("Converting timestamps to datetime objects")
    dframe["opened"] = pd.to_datetime(dframe["opened"], unit="s")
    dframe["started"] = pd.to_datetime(dframe["started"], unit="s")
    dframe["finished"] = pd.to_datetime(dframe["finished"], unit="s")

    return dframe
Example #7
0
    def match(self, game: str = "aoe2de", uuid: str = None, match_id: int = None) -> MatchLobby:
        """
        Request details about a match. Either 'uuid' or 'match_id' required.

        Args:
            game (str): The game for which to extract the list of strings. Defaults to 'aoe2de'.
                Possibilities are 'aoe2hd' (Age of Empires 2: HD Edition) and 'aoe2de' (Age of
                Empires 2: Definitive Edition).
            uuid (str): match UUID (ex: '66ec2575-5ee4-d241-a1fc-d7ffeffb48b6').
            match_id (int): match ID.

        Raises:
            Aoe2NetException: if the not one of 'uuid' or 'match_id' are provided.

        Returns:
            A MatchLobby validated object with the information of the specific match, including.
        """
        if not uuid and not match_id:
            logger.error("Missing one of 'uuid', 'match_id'.")
            raise Aoe2NetException("Either 'uuid' or 'match_id' required, please provide one.")

        logger.debug("Preparing parameters for single match query")
        query_params = {
            "game": game,
            "uuid": uuid,
            "match_id": match_id,
        }

        processed_response = _get_request_response_json(
            session=self.session,
            url=self.MATCH_ENDPOINT,
            params=query_params,
            timeout=self.timeout,
        )
        logger.trace(f"Validating response from '{self.MATCH_ENDPOINT}'")
        return MatchLobby(**processed_response)