GM Command Request and Response#

The acore_soap.gm module provides a set of more advanced class to send the GM command and parse the response message.

Each module in acore_soap/gm/ folder (Not include api.py and base.py) implements one Azerothcore GM command.

Here are two examples:

server_info.py
  1# -*- coding: utf-8 -*-
  2
  3"""
  4Usage:
  5
  6    >>> import acore_soap.api as acore_soap
  7    >>> request = acore_soap.gm.ServerInfoRequest()
  8    >>> soap_response = request.send()
  9    >>> response = acore_soap.gm.ServerInfoResponse.from_soap_response(soap_response)
 10    >>> response.connected_players
 11    1
 12    >>> response.characters_in_world
 13    1
 14    >>> response.server_uptime
 15    3600
 16"""
 17
 18import re
 19import dataclasses
 20
 21from ..request import SOAPResponse
 22from ..exc import SOAPResponseParseError
 23
 24from .base import GMCommandRequest, GMCommandResponse
 25
 26
 27@dataclasses.dataclass
 28class ServerInfoResponse(GMCommandResponse):
 29    """
 30    Parse the response message of ``.server info`` command, extract
 31
 32    :param connected_players: how many players are logged in.
 33    :param characters_in_world: how many player
 34    :param server_uptime: how long the server has been running (in seconds)
 35    """
 36
 37    connected_players: int = dataclasses.field()
 38    characters_in_world: int = dataclasses.field()
 39    server_uptime: int = dataclasses.field()
 40
 41    @staticmethod
 42    def extract_connected_players(message: str) -> int:
 43        """
 44        Extract the number of connected players from the response message.
 45        """
 46        res = re.findall(r"Connected players: (\d+)", message)
 47        if len(res) == 1:
 48            return int(res[0])
 49        else:  # pragma: no cover
 50            raise SOAPResponseParseError(message)
 51
 52    @staticmethod
 53    def extract_characters_in_world(message: str) -> int:
 54        """
 55        Extract the number of characters in the world from the response message.
 56        """
 57        res = re.findall(r"Characters in world: (\d+)", message)
 58        if len(res) == 1:
 59            return int(res[0])
 60        else:  # pragma: no cover
 61            raise SOAPResponseParseError(message)
 62
 63    @staticmethod
 64    def extract_server_uptime(message: str) -> int:
 65        """
 66        Extract the server uptime from the response message.
 67        """
 68        P_SECOND = re.compile(r"(\d+\s+second\(s\))")
 69        P_MINUTE = re.compile(r"(\d+\s+minute\(s\))")
 70        P_HOUR = re.compile(r"(\d+\s+hour\(s\))")
 71        P_DAY = re.compile(r"(\d+\s+day\(s\))")
 72
 73        def extract(p: re.Pattern, message: str) -> int:
 74            res = re.findall(p, message)
 75            if len(res) == 1:
 76                return int(res[0].split()[0])
 77            elif len(res) == 0:
 78                return 0
 79            else:  # pragma: no cover
 80                raise ValueError()
 81
 82        seconds = extract(P_SECOND, message)
 83        minutes = extract(P_MINUTE, message)
 84        hours = extract(P_HOUR, message)
 85        days = extract(P_DAY, message)
 86        total_seconds = seconds + minutes * 60 + hours * 3600 + days * 86400
 87        return total_seconds
 88
 89    @classmethod
 90    def from_soap_response(cls, res: SOAPResponse):
 91        connected_players = cls.extract_connected_players(res.message)
 92        characters_in_world = cls.extract_characters_in_world(res.message)
 93        server_uptime = cls.extract_server_uptime(res.message)
 94        return cls(
 95            connected_players=connected_players,
 96            characters_in_world=characters_in_world,
 97            server_uptime=server_uptime,
 98        )
 99
100
101@dataclasses.dataclass
102class ServerInfoRequest(GMCommandRequest):
103    """
104    Generate the command to get server information.
105    """
106
107    def to_command(self) -> str:
108        return ".server info"
create_account.py
 1# -*- coding: utf-8 -*-
 2
 3"""
 4Usage:
 5
 6    >>> import acore_soap.api as acore_soap
 7    >>> request = acore_soap.gm.CreateAccountRequest(
 8    ...     account="testacc",
 9    ...     password="testpass",
10    ... )
11    >>> soap_response = request.send()
12    >>> response = acore_soap.gm.CreateAccountResponse.from_soap_response(soap_response)
13"""
14
15import re
16import dataclasses
17
18from ..request import SOAPResponse
19from ..exc import SOAPResponseParseError
20
21from .base import GMCommandRequest, GMCommandResponse
22
23
24@dataclasses.dataclass
25class CreateAccountResponse(GMCommandResponse):
26    """
27    Parse the response message of ``.account create ...`` command.
28    """
29
30    @classmethod
31    def from_soap_response(cls, res: SOAPResponse):
32        raise NotImplementedError
33
34
35@dataclasses.dataclass
36class CreateAccountRequest(GMCommandRequest):
37    """
38    todo: docstring
39    """
40
41    account: str = dataclasses.field()
42    password: str = dataclasses.field()
43
44    def to_command(self) -> str:
45        return f".account create {self.account} {self.password}"