Skip to content

Commit 0c6e1d7

Browse files
Update gamespy1.py
1 parent 7251e7f commit 0c6e1d7

File tree

1 file changed

+27
-29
lines changed

1 file changed

+27
-29
lines changed

opengsq/protocols/gamespy1.py

+27-29
Original file line numberDiff line numberDiff line change
@@ -62,12 +62,10 @@ async def get_status(self, xserverquery: bool = True) -> dict:
6262
data = xserverquery and self.__Request.STATUS or self.__Request.STATUS.replace(b'xserverquery', b'')
6363
br = await self.__connect_and_send(data)
6464

65-
info = self.__parse_as_key_values(br, is_status=True)
66-
players = self.__parse_as_object(br)
67-
6865
status = {}
69-
status['info'] = info
70-
status['players'] = players
66+
status['info'] = self.__parse_as_key_values(br, is_status=True)
67+
status['players'] = self.__parse_as_object(br, is_player=True)
68+
status['teams'] = [] if br.is_end() else self.__parse_as_object(br)
7169

7270
return status
7371

@@ -76,33 +74,28 @@ async def get_teams(self) -> list:
7674
return self.__parse_as_object(await self.__connect_and_send(self.__Request.TEAMS))
7775

7876
# Receive packets and sort it
79-
async def __get_packets_response(self, sock):
77+
async def __get_packets_response(self, sock: SocketAsync):
8078
payloads = {}
8179
packet_count = -1
8280

8381
# Loop until received all packets
8482
while packet_count == -1 or len(payloads) < packet_count:
8583
packet = await sock.recv()
8684

87-
# If it is the last packet, it will contain b'\\final\\' at the end of the response
88-
if packet.rsplit(b'\\', 2)[1] == b'final':
89-
# Split to payload, "queryid", query_id
90-
payload, _, query_id = packet[:-7].rsplit(b'\\', 2)
91-
92-
# Get the packet number from query_id
93-
number = re.search(rb'\d+.(\d+)', query_id).group(1)
85+
# Get the packet number from query_id
86+
r = re.compile(rb'\\queryid\\\d+\.(\d+)')
87+
number, payload = int(r.search(packet).group(1)), r.sub(b'', packet)
9488

89+
# If it is the last packet, it will contain b'\\final\\' at the end of the response
90+
if payload.endswith(b'\\final\\'):
9591
# Save the packet count
96-
packet_count = int(number)
97-
else:
98-
# Split to payload, "queryid", query_id
99-
payload, _, query_id = packet.rsplit(b'\\', 2)
92+
packet_count = number
10093

101-
# Get the packet number from query_id
102-
number = re.search(rb'\d+.(\d+)', query_id).group(1)
94+
# Remove the last b'\\final\\'
95+
payload = payload[:-7]
10396

10497
# Save the payload, remove the first byte if it is the first packet
105-
payloads[number] = int(number) == 1 and payload[1:] or payload
98+
payloads[number] = payload[1:] if number == 1 else payload
10699

107100
# Sort the payload and return as bytes
108101
response = b''.join(payloads[number] for number in sorted(payloads))
@@ -125,9 +118,9 @@ def __parse_as_key_values(self, br: BinaryReader, is_status=False):
125118

126119
# Bind key value
127120
while br.length() > 0:
128-
key = br.read_string(b'\\')
121+
key = br.read_string(b'\\').lower()
129122

130-
if is_status and key.lower().startswith('player_'):
123+
if is_status and (key.startswith('player_') or key.startswith('playername_')):
131124
# Read already, so add it back
132125
br.prepend_bytes(key.encode() + b'\\')
133126
break
@@ -137,16 +130,23 @@ def __parse_as_key_values(self, br: BinaryReader, is_status=False):
137130

138131
return kv
139132

140-
def __parse_as_object(self, br: BinaryReader):
133+
def __parse_as_object(self, br: BinaryReader, is_player=False):
141134
items = []
142135

143136
while br.length() > 0:
144137
# Get the key, for example player_1, frags_1, ping_1, etc...
145-
key = br.read_string(b'\\')
138+
key = br.read_string(b'\\').lower()
139+
140+
if is_player and key.startswith('teamname_'):
141+
# Read already, so add it back
142+
br.prepend_bytes(key.encode() + b'\\')
143+
break
146144

147145
# Extract to name and index, for example name=player, index=1
148146
matches = re.search(r'(.+?)_(\d+)', key)
149147
name = matches.group(1)
148+
name = 'player' if name == 'playername' else name
149+
name = 'team' if name == 'teamname' else name
150150
index = int(matches.group(2))
151151

152152
# Append a dict to items if next index appears
@@ -167,11 +167,9 @@ def __parse_as_object(self, br: BinaryReader):
167167
import json
168168

169169
async def main_async():
170-
gs1 = GameSpy1(
171-
address='139.162.235.20',
172-
query_port=7778,
173-
timeout=5.0
174-
)
170+
gs1 = GameSpy1(address='51.81.48.224', query_port=23000, timeout=5.0) # bfield1942
171+
#gs1 = GameSpy1(address='139.162.235.20', query_port=7778, timeout=5.0) # ut
172+
#gs1 = GameSpy1(address='192.223.24.6', query_port=7778, timeout=5.0) # ut
175173
status = await gs1.get_status()
176174
print(json.dumps(status, indent=None) + '\n')
177175

0 commit comments

Comments
 (0)