Skip to content

Commit

Permalink
feat: Add server game port to Server(Info) if available
Browse files Browse the repository at this point in the history
  • Loading branch information
cetteup committed Feb 5, 2023
1 parent 403fd44 commit 2f69e50
Showing 1 changed file with 17 additions and 3 deletions.
20 changes: 17 additions & 3 deletions pyvpsq/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,32 +30,37 @@ class ServerInfo:
password: bool
secure: bool
version: str
game_port: Optional[int] = None


class Server:
ip: str
query_port: int
game_port: Optional[int]

def __init__(self, ip: str, query_port: int):
def __init__(self, ip: str, query_port: int, game_port: Optional[int] = None):
self.ip = ip
self.query_port = query_port
self.game_port = game_port

def __iter__(self):
yield 'ip', self.ip
yield 'query_port', self.query_port
yield 'game_port', self.game_port

def __repr__(self):
return f'{self.ip}:{self.query_port}'

def __eq__(self, other: Any) -> bool:
return isinstance(other, type(self)) and \
other.ip == self.ip and \
other.query_port == self.query_port
other.query_port == self.query_port and \
other.game_port == self.game_port

def get_info(self, timeout: float = 1.0):
buffer, *_ = self.query(ServerQueryType.Info, timeout=timeout)

return ServerInfo(
info = ServerInfo(
protocol=buffer.read_uchar(),
name=buffer.read_c_string(),
map=buffer.read_c_string(),
Expand All @@ -72,6 +77,15 @@ def get_info(self, timeout: float = 1.0):
version=buffer.read_c_string()
)

extra_flag = buffer.read_uchar()
if extra_flag & 0x80:
game_port = buffer.read_ushort(byte_order=ByteOrder.LittleEndian)
self.game_port = game_port
info.game_port = game_port

return info


def query(self, *args: ServerQueryType, timeout: float) -> List[Buffer]:
connection = Connection(self.ip, self.query_port, ServerPacket, timeout=timeout)

Expand Down

0 comments on commit 2f69e50

Please sign in to comment.