<commit_before>/**
 * This code is free software; you can redistribute it and/or modify it under
 * the terms of the new BSD License.
 *
 * Copyright (c) 2008-2011, <NAME>
 */

package com.github.koraktor.steamcondenser.steam.servers;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.concurrent.TimeoutException;

import com.github.koraktor.steamcondenser.exceptions.SteamCondenserException;
import com.github.koraktor.steamcondenser.steam.SteamPlayer;
import com.github.koraktor.steamcondenser.steam.packets.A2S_INFO_Packet;
import com.github.koraktor.steamcondenser.steam.packets.S2A_INFO_BasePacket;
import com.github.koraktor.steamcondenser.steam.packets.A2S_PLAYER_Packet;
import com.github.koraktor.steamcondenser.steam.packets.S2A_PLAYER_Packet;
import com.github.koraktor.steamcondenser.steam.packets.A2S_RULES_Packet;
import com.github.koraktor.steamcondenser.steam.packets.S2A_RULES_Packet;
import com.github.koraktor.steamcondenser.steam.packets.A2S_SERVERQUERY_GETCHALLENGE_Packet;
import com.github.koraktor.steamcondenser.steam.packets.S2C_CHALLENGE_Packet;
import com.github.koraktor.steamcondenser.steam.packets.SteamPacket;
import com.github.koraktor.steamcondenser.steam.sockets.QuerySocket;

/**
 * @author <NAME>
 */
public abstract class GameServer extends Server {

	private static final int REQUEST_CHALLENGE = 0;
	private static final int REQUEST_INFO = 1;
	private static final int REQUEST_PLAYER = 2;
	private static final int REQUEST_RULES = 3;
	protected int challengeNumber = 0xFFFFFFFF;
	protected int ping;
	protected HashMap<String, SteamPlayer> playerHash;
	protected int rconRequestId;
	protected HashMap<String, String> rulesHash;
	protected HashMap<String, Object> serverInfo;
	protected QuerySocket socket;

    /**
     * Creates a new game server instance with the given address and port
     *
     * @param address
     * @param port
     */
    protected GameServer(String address, Integer port)
            throws IOException, SteamCondenserException {
        super(address, port);
    }

    /**
     * Parses the player attribute names supplied by +rcon status+
     *
     * @param statusHeader
     * @return Split player attribute names
     */
    private static List<String> getPlayerStatusAttributes(String statusHeader) {
        List<String> statusAttributes = new ArrayList<String>();
        for(String attribute : statusHeader.split("\\s+")) {
            if(attribute.equals("connected")) {
                statusAttributes.add("time");
            } else if(attribute.equals("frag")) {
                statusAttributes.add("score");
            } else {
                statusAttributes.add(attribute);
            }
        }

        return statusAttributes;
    }

    /**
     * Splits the player status obtained with "rcon status"
     *
     * @param attributes
     * @param playerStatus
     * @return Split player data
     */
    private static Map<String, String> splitPlayerStatus(List<String> attributes, String playerStatus) {
        if(!attributes.get(0).equals("userid")) {
            playerStatus = playerStatus.replaceAll("^\\d+ +", "");
        }

        List<String> tmpData = Arrays.asList(playerStatus.split("\""));
        List<String> data = new ArrayList<String>();
        data.addAll(Arrays.asList(tmpData.get(0).trim().split("\\s+")));
        data.add(tmpData.get(1));
        data.addAll(Arrays.asList(tmpData.get(2).trim().split("\\s+")));
        data.remove("");

        if(attributes.size() > data.size() + 1 &&
           attributes.contains("state")) {
            data.add(3, null);<commit_msg>Fixed status parsing for names containing quotes<commit_after>/**
 * This code is free software; you can redistribute it and/or modify it under
 * the terms of the new BSD License.
 *
 * Copyright (c) 2008-2011, <NAME>
 */

package com.github.koraktor.steamcondenser.steam.servers;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.concurrent.TimeoutException;

import com.github.koraktor.steamcondenser.exceptions.SteamCondenserException;
import com.github.koraktor.steamcondenser.steam.SteamPlayer;
import com.github.koraktor.steamcondenser.steam.packets.A2S_INFO_Packet;
import com.github.koraktor.steamcondenser.steam.packets.S2A_INFO_BasePacket;
import com.github.koraktor.steamcondenser.steam.packets.A2S_PLAYER_Packet;
import com.github.koraktor.steamcondenser.steam.packets.S2A_PLAYER_Packet;
import com.github.koraktor.steamcondenser.steam.packets.A2S_RULES_Packet;
import com.github.koraktor.steamcondenser.steam.packets.S2A_RULES_Packet;
import com.github.koraktor.steamcondenser.steam.packets.A2S_SERVERQUERY_GETCHALLENGE_Packet;
import com.github.koraktor.steamcondenser.steam.packets.S2C_CHALLENGE_Packet;
import com.github.koraktor.steamcondenser.steam.packets.SteamPacket;
import com.github.koraktor.steamcondenser.steam.sockets.QuerySocket;

/**
 * @author <NAME>
 */
public abstract class GameServer extends Server {

	private static final int REQUEST_CHALLENGE = 0;
	private static final int REQUEST_INFO = 1;
	private static final int REQUEST_PLAYER = 2;
	private static final int REQUEST_RULES = 3;
	protected int challengeNumber = 0xFFFFFFFF;
	protected int ping;
	protected HashMap<String, SteamPlayer> playerHash;
	protected int rconRequestId;
	protected HashMap<String, String> rulesHash;
	protected HashMap<String, Object> serverInfo;
	protected QuerySocket socket;

    /**
     * Creates a new game server instance with the given address and port
     *
     * @param address
     * @param port
     */
    protected GameServer(String address, Integer port)
            throws IOException, SteamCondenserException {
        super(address, port);
    }

    /**
     * Parses the player attribute names supplied by +rcon status+
     *
     * @param statusHeader
     * @return Split player attribute names
     */
    private static List<String> getPlayerStatusAttributes(String statusHeader) {
        List<String> statusAttributes = new ArrayList<String>();
        for(String attribute : statusHeader.split("\\s+")) {
            if(attribute.equals("connected")) {
                statusAttributes.add("time");
            } else if(attribute.equals("frag")) {
                statusAttributes.add("score");
            } else {
                statusAttributes.add(attribute);
            }
        }

        return statusAttributes;
    }

    /**
     * Splits the player status obtained with "rcon status"
     *
     * @param attributes
     * @param playerStatus
     * @return Split player data
     */
    private static Map<String, String> splitPlayerStatus(List<String> attributes, String playerStatus) {
        if(!attributes.get(0).equals("userid")) {
            playerStatus = playerStatus.replaceAll("^\\d+ +", "");
        }

        int firstQuote = playerStatus.indexOf('"');
        int lastQuote  = playerStatus.lastIndexOf('"');
        List<String> tmpData = new ArrayList<String>();
        tmpData.add(playerStatus.substring(0, firstQuote));
        tmpData.add(playerStatus.substring(firstQuote + 1, lastQuote));
        tmpData.add(playerStatus.substring(lastQuote + 1));

        List<String> data = new ArrayList<String>();
        data.addAll(Arrays.asList(tmpData.get(0).trim().split("\\s+")));
        data.add(tmpData.get(1));
        data.addAll(Arrays.asList(tmpData.get(2).trim().split("\\s+")));
        data.remove("");

        if(attributes.size() > data.size() + 1 &&
           attributes.contains("state")) {
            data.add(3, null);
{'fraction_non_alphanumeric': 0.11209326159364591, 'fraction_numerical': 0.007302075326671791, 'mean_word_length': 3.517939814814815, 'pattern_counts': {'":': 0, '<': 30, '<?xml version=': 0, '>': 32, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 2, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5566307', 'n_tokens_mistral': 2426, 'n_tokens_neox': 2395, 'n_words': 481}