giants-maps-gui/lib/giantsenumresponseparser.py

90 lines
3.1 KiB
Python
Raw Normal View History

2021-12-23 00:25:10 +01:00
from lib.enumresponse import EnumResponse, Packet
from lib.constants import Teams, GameTypes, MapID
class GiantsEnumResponseParser:
def __init__(self):
super().__init__()
self.server_name = None
self.max_players = None
self.current_players = None
self.game_type = None
self.teams = None
self.version = None
self.points_per_capture = None
self.points_per_kill = None
self.base_level = None
self.allow_joiners = None
self.damage_teammates = None
self.lock_teams = None
self.is_dedicated = None
self.vimps_disabled = None
self.weap_bits = None
self.high_bandwidth = None
self.no_voting = None
self.smartie_difficulty = None
self.vimp_difficulty = None
self.capture_prevent_count = None
self.current_cap_prev_time = None
self.map_id = None
self.map_checksum = None
self.map_name = None
self.build = None
self.revision = None
@classmethod
def parse(cls, enumresp: EnumResponse):
g_enumresponse = cls()
g_enumresponse.max_players = enumresp.MaxPlayers
g_enumresponse.current_players = enumresp.CurrentPlayers
g_enumresponse.server_name = enumresp.SessionName
game_data = enumresp.ApplicationReservedData
# print(game_data)
p = Packet(game_data)
g_enumresponse.map_id = p.get_byte()
g_enumresponse.game_type = p.get_byte()
g_enumresponse.teams = p.get_byte()
g_enumresponse.base_level = p.get_byte()
g_enumresponse.version = p.get_short()
bitmask = p.get_short()
b = bin(bitmask)[2:].zfill(16)
g_enumresponse.allow_joiners = b[0]
g_enumresponse.damage_teammates = b[1]
g_enumresponse.lock_teams = b[2]
g_enumresponse.is_dedicated = b[3]
g_enumresponse.vimps_disabled = b[4]
g_enumresponse.weap_bits = b[5]
g_enumresponse.high_bandwidth = b[6]
g_enumresponse.no_voting = b[7]
g_enumresponse.smartie_difficulty = b[8:12]
g_enumresponse.vimp_difficulty = b[12:16]
g_enumresponse.points_per_capture = p.get_short()
g_enumresponse.points_per_kill = p.get_short()
g_enumresponse.capture_prevent_count = p.get_short()
g_enumresponse.current_cap_prev_time = p.get_short()
g_enumresponse.map_checksum = p.get_ulong()
g_enumresponse.map_name = p.get_string(32)
if g_enumresponse.version >= 1498:
g_enumresponse.build = p.get_byte()
g_enumresponse.revision = p.get_byte()
if map_name := MapID.get_name(g_enumresponse.map_id):
g_enumresponse.map_name = map_name
return g_enumresponse
def __str__(self):
return "[%s] Players:%d/%d, Map: %s, Type: %s, Teams: %s, Version: %d, Build: %s, AllowJoiners: %s" % \
(self.server_name, self.current_players, self.max_players, self.map_name, GameTypes.get_name_by_id(self.game_type), Teams.get_name_by_id(self.teams), self.version, self.build, self.allow_joiners)