From 0ea316c780569ac7740019fd9c908cf4bce2c49f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9=20Gl=C3=BCpker?= Date: Fri, 23 Mar 2018 21:05:44 +0100 Subject: Ask server for connected players --- QueryServer.py | 43 +++++++++++++++++++++++++++++++++++++++---- 1 file changed, 39 insertions(+), 4 deletions(-) mode change 100644 => 100755 QueryServer.py (limited to 'QueryServer.py') diff --git a/QueryServer.py b/QueryServer.py old mode 100644 new mode 100755 index 669e7a3..cd86bfe --- a/QueryServer.py +++ b/QueryServer.py @@ -26,6 +26,7 @@ sourcequeryinc = [ import json import socket +import struct from SteamAPI import SteamAPI if CACHE_TTL > 0: from Caching import Caching @@ -71,16 +72,22 @@ class QueryServer(): if gameid in sourcequeryinc: port += 1 - print(ip, port, gameid) + # print(ip, port, gameid) conn = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) + message = None try: conn.connect( (ip, port) ) conn.sendall( b'\xFF\xFF\xFF\xFF\x54' + b'Source Engine Query' + b'\x00' ) message = conn.recv(4096) - except: - print('Exception in SourceQuery connection') - return None + conn.sendall( b'\xFF\xFF\xFF\xFF\x55\xFF\xFF\xFF\xFF' ) + challenge = conn.recv(1024)[5:] + conn.sendall( b'\xFF\xFF\xFF\xFF\x55' + challenge ) + playerdata = conn.recv(4096) + except Exception as e: + print('Exception in SourceQuery connection:', e) + if not message: + return None finally: conn.close() @@ -159,6 +166,34 @@ class QueryServer(): (message[1] << 8) + (message[0]) message = message[8:] + if playerdata and playerdata[4] == ord('D'): + players = [] + data['player_info'] = players + number = playerdata[5] + playerdata = playerdata[6:] + + index = 0 + while index < number and playerdata: + player = dict() + player['index'] = playerdata[0] + + nullterm = playerdata.index(b'\0', 1) + player_name = playerdata[1:nullterm] + player['name'] = player_name.decode('utf-8', errors='replace') + playerdata = playerdata[nullterm+1:] + + playerscore = (playerdata[3] << 24) + (playerdata[2] << 16) + \ + (playerdata[1] << 8) + (playerdata[0]) + player['score'] = playerscore + playerdata = playerdata[4:] + + duration = int(struct.unpack('f', playerdata[0:4])[0]) + player['duration'] = duration + playerdata = playerdata[4:] + + players.append(player) + index += 1 + # Everything is type bytes, so convert things to utf8? for key, value in data.items(): if isinstance(value, bytes): -- cgit v1.2.3