Пример #1
0
def adapt_server_list(qstat_string, game, game_name, qstat_master_type,
                      qstat_server_type, server_game_name, server_game_type):
    server_table = []

    server_table_dict = json.loads(json.dumps(xmltodict.parse(qstat_string)))
    server_table_dict['qstat']['server'] = helpers.enforce_array(
        server_table_dict['qstat']
        ['server'])  # Enforce server array even if n=1

    for qstat_entry in server_table_dict['qstat'][
            'server']:  # For every server...
        try:
            if server_table_dict['qstat']['server'][
                    '@type'] == qstat_master_type:
                debug_msg(
                    game_name,
                    i18n.
                    _("No valid masters specified. Please check your master server settings."
                      ))
                break

        except TypeError:
            try:
                response = adapt_qstat_entry(qstat_entry, game,
                                             qstat_master_type,
                                             qstat_server_type)

                server_dict = response['server_dict']
                msg = response['debug_msg']
                debug_msg(game_name, msg)

                if server_dict is not None:
                    appendable = True
                    for filter_rule in ((server_game_name, "game_name"),
                                        (server_game_type, "game_type")):
                        if filter_rule[0] is not None:
                            if filter_rule[1] not in server_dict.keys():
                                appendable = False
                                break
                            else:
                                if server_dict[
                                        filter_rule[1]] != filter_rule[0]:
                                    appendable = False
                                    break
                    if appendable:
                        server_table.append(server_dict)

            except Exception as e:
                debug_msg(game_name, str(e.args[0]))

    return server_table
Пример #2
0
def adapt_server_list(
    qstat_string, game, game_name, qstat_master_type, qstat_server_type, server_game_name, server_game_type
):
    server_table = []

    server_table_dict = json.loads(json.dumps(xmltodict.parse(qstat_string)))
    server_table_dict["qstat"]["server"] = helpers.enforce_array(
        server_table_dict["qstat"]["server"]
    )  # Enforce server array even if n=1

    for qstat_entry in server_table_dict["qstat"]["server"]:  # For every server...
        try:
            if server_table_dict["qstat"]["server"]["@type"] == qstat_master_type:
                debug_msg(game_name, i18n._("No valid masters specified. Please check your master server settings."))
                break

        except TypeError:
            try:
                response = adapt_qstat_entry(qstat_entry, game, qstat_master_type, qstat_server_type)

                server_dict = response["server_dict"]
                msg = response["debug_msg"]
                debug_msg(game_name, msg)

                if server_dict is not None:
                    appendable = True
                    for filter_rule in ((server_game_name, "game_name"), (server_game_type, "game_type")):
                        if filter_rule[0] is not None:
                            if filter_rule[1] not in server_dict.keys():
                                appendable = False
                                break
                            else:
                                if server_dict[filter_rule[1]] != filter_rule[0]:
                                    appendable = False
                                    break
                    if appendable:
                        server_table.append(server_dict)

            except Exception as e:
                debug_msg(game_name, str(e.args[0]))

    return server_table
Пример #3
0
def adapt_server_entry(qstat_entry, game):
    """Parses server entry returned by QStat"""
    color_code_pattern = '[\\^](.)'

    debug_message = None
    server_dict = None

    server_status = qstat_entry['@status']
    if server_status == 'UP':
        server_dict = {}
        server_dict['host'] = qstat_entry['hostname']
        server_dict['password'] = False
        server_dict['secure'] = False
        server_dict['game_id'] = game
        server_dict['game_mod'] = ""

        if qstat_entry['name'] is None:
            server_dict['name'] = ""
        else:
            server_dict['name'] = re.sub(color_code_pattern, '',
                                         str(qstat_entry['name']))
        server_dict['game_type'] = re.sub(color_code_pattern, '',
                                          str(qstat_entry['gametype']))
        server_dict['terrain'] = str(qstat_entry['map'])
        try:
            server_dict['player_count'] = int(qstat_entry['numplayers'])
        except (KeyError, TypeError):
            server_dict['player_count'] = 0
        try:
            server_dict['player_limit'] = int(qstat_entry['maxplayers'])
        except (KeyError, TypeError):
            server_dict['player_limit'] = 0
        try:
            server_dict['ping'] = int(qstat_entry['ping'])
        except (KeyError, TypeError):
            server_dict['ping'] = 9999
        server_dict['rules'] = {}
        server_dict['players'] = []

        if qstat_entry['rules'] is not None:
            rules_list = helpers.enforce_array(qstat_entry['rules']['rule'])

            for rule in rules_list:
                rule_name = rule['@name']
                if '#text' in rule.keys():
                    server_dict['rules'][rule_name] = rule['#text']
                else:
                    server_dict['rules'][rule_name] = None

                rule_text = server_dict['rules'][rule_name]

                if rule_name in ('gamename'):
                    server_dict['game_name'] = str(rule_text)
                elif rule_name in ('punkbuster', 'sv_punkbuster', 'secure'):
                    server_dict['secure'] = bool(int(rule_text))
                elif rule_name in ('game'):
                    server_dict['game_mod'] = str(rule_text)
                elif rule_name in ('g_needpass', 'needpass', 'si_usepass',
                                   'pswrd', 'password'):
                    try:
                        server_dict['password'] = bool(int(rule_text))
                    except TypeError:
                        server_dict['password'] = False

        if qstat_entry['players'] is not None:
            player_list = helpers.enforce_array(
                qstat_entry['players']['player'])

            for player_entry in player_list:
                server_dict['players'].append(
                    parse_player_entry(player_entry, color_code_pattern))

    return {'server_dict': server_dict, 'debug_msg': debug_message}
Пример #4
0
def adapt_server_entry(qstat_entry, game):
    """Parses server entry returned by QStat"""
    color_code_pattern = "[\\^](.)"

    debug_message = None
    server_dict = None

    server_status = qstat_entry["@status"]
    if server_status == "UP":
        server_dict = {}
        server_dict["host"] = qstat_entry["hostname"]
        server_dict["password"] = False
        server_dict["secure"] = False
        server_dict["game_id"] = game
        server_dict["game_mod"] = ""

        if qstat_entry["name"] is None:
            server_dict["name"] = ""
        else:
            server_dict["name"] = re.sub(color_code_pattern, "", str(qstat_entry["name"]))
        server_dict["game_type"] = re.sub(color_code_pattern, "", str(qstat_entry["gametype"]))
        server_dict["terrain"] = str(qstat_entry["map"])
        try:
            server_dict["player_count"] = int(qstat_entry["numplayers"])
        except (KeyError, TypeError):
            server_dict["player_count"] = 0
        try:
            server_dict["player_limit"] = int(qstat_entry["maxplayers"])
        except (KeyError, TypeError):
            server_dict["player_limit"] = 0
        try:
            server_dict["ping"] = int(qstat_entry["ping"])
        except (KeyError, TypeError):
            server_dict["ping"] = 9999
        server_dict["rules"] = {}
        server_dict["players"] = []

        if qstat_entry["rules"] is not None:
            rules_list = helpers.enforce_array(qstat_entry["rules"]["rule"])

            for rule in rules_list:
                rule_name = rule["@name"]
                if "#text" in rule.keys():
                    server_dict["rules"][rule_name] = rule["#text"]
                else:
                    server_dict["rules"][rule_name] = None

                rule_text = server_dict["rules"][rule_name]

                if rule_name in ("gamename"):
                    server_dict["game_name"] = str(rule_text)
                elif rule_name in ("punkbuster", "sv_punkbuster", "secure"):
                    server_dict["secure"] = bool(int(rule_text))
                elif rule_name in ("game"):
                    server_dict["game_mod"] = str(rule_text)
                elif rule_name in ("g_needpass", "needpass", "si_usepass", "pswrd", "password"):
                    try:
                        server_dict["password"] = bool(int(rule_text))
                    except TypeError:
                        server_dict["password"] = False

        if qstat_entry["players"] is not None:
            player_list = helpers.enforce_array(qstat_entry["players"]["player"])

            for player_entry in player_list:
                server_dict["players"].append(parse_player_entry(player_entry, color_code_pattern))

    return {"server_dict": server_dict, "debug_msg": debug_message}
Пример #5
0
def adapt_server_entry(qstat_entry, game):
    """Parses server entry returned by QStat"""
    color_code_pattern = '[\\^](.)'

    debug_message = None
    server_dict = None

    server_status = qstat_entry['@status']
    if server_status == 'UP':
        server_dict = {}
        server_dict['host'] = qstat_entry['hostname']
        server_dict['password'] = False
        server_dict['secure'] = False
        server_dict['game_id'] = game
        server_dict['game_mod'] = ""

        if qstat_entry['name'] is None:
            server_dict['name'] = ""
        else:
            server_dict['name'] = re.sub(color_code_pattern, '', str(qstat_entry['name']))
        server_dict['game_type'] = re.sub(color_code_pattern, '', str(qstat_entry['gametype']))
        server_dict['terrain'] = str(qstat_entry['map'])
        try:
            server_dict['player_count'] = int(qstat_entry['numplayers'])
        except (KeyError, TypeError):
            server_dict['player_count'] = 0
        try:
            server_dict['player_limit'] = int(qstat_entry['maxplayers'])
        except (KeyError, TypeError):
            server_dict['player_limit'] = 0
        try:
            server_dict['ping'] = int(qstat_entry['ping'])
        except (KeyError, TypeError):
            server_dict['ping'] = 9999
        server_dict['rules'] = {}
        server_dict['players'] = []

        if qstat_entry['rules'] is not None:
            rules_list = helpers.enforce_array(qstat_entry['rules']['rule'])

            for rule in rules_list:
                rule_name = rule['@name']
                if '#text' in rule.keys():
                    server_dict['rules'][rule_name] = rule['#text']
                else:
                    server_dict['rules'][rule_name] = None

                rule_text = server_dict['rules'][rule_name]

                if rule_name in ('gamename'):
                    server_dict['game_name'] = str(rule_text)
                elif rule_name in ('punkbuster', 'sv_punkbuster', 'secure'):
                    server_dict['secure'] = bool(int(rule_text))
                elif rule_name in ('game'):
                    server_dict['game_mod'] = str(rule_text)
                elif rule_name in ('g_needpass', 'needpass', 'si_usepass', 'pswrd', 'password'):
                    try:
                        server_dict['password'] = bool(int(rule_text))
                    except TypeError:
                        server_dict['password'] = False

        if qstat_entry['players'] is not None:
            player_list = helpers.enforce_array(qstat_entry['players']['player'])

            for player_entry in player_list:
                server_dict['players'].append(parse_player_entry(player_entry, color_code_pattern))

    return {'server_dict': server_dict, 'debug_msg': debug_message}