diff options
author | André Glüpker <git@wgmd.de> | 2018-03-23 21:05:44 +0100 |
---|---|---|
committer | André Glüpker <git@wgmd.de> | 2018-03-23 22:26:08 +0100 |
commit | 0ea316c780569ac7740019fd9c908cf4bce2c49f (patch) | |
tree | bc7e72f9f7b6aba5d1b4c74de51d37143dff9705 /QueryServer.py | |
parent | 2431f3ad79bcb76320ffc500e2f7b9efcf09fe89 (diff) | |
download | steam-0ea316c780569ac7740019fd9c908cf4bce2c49f.tar.gz steam-0ea316c780569ac7740019fd9c908cf4bce2c49f.tar.bz2 steam-0ea316c780569ac7740019fd9c908cf4bce2c49f.zip |
Ask server for connected players
Diffstat (limited to 'QueryServer.py')
-rwxr-xr-x[-rw-r--r--] | QueryServer.py | 43 |
1 files changed, 39 insertions, 4 deletions
diff --git a/QueryServer.py b/QueryServer.py index 669e7a3..cd86bfe 100644..100755 --- 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): |