Author Topic: Parsing server status data for player team colors  (Read 1005 times)

sk89q

  • Global Moderator
  • Autococker
  • Posts: 1049
Parsing server status data for player team colors
« on: November 25, 2007, 04:16:05 PM »
It's not working quite right...

Code: [Select]
function update_status_cache($force_update = false)
{
    if($force_update || count($this->status_cache) == 0)
    {
        $this->send_ctrl("status");
        $data = $this->get_ctrl();
       
        // Example data:
        // \TimeLeft\14:32\pr\!0\mapname\arenaball\_scores\Red:0 Blue:0
        // 0 204 "JoeJoe"
        $result = explode("\n", $data);
        $status = explode("\\", $result[0]);
        // Knock off the status data (so we can get the player list)
        array_shift($result);
        // Knock off the first blank entry
        array_shift($status);
       
        // Get the key/value statusness
        $info = array();
       
        // We skip two, so we set:
        // key = i
        // value = i+1
        for($i = 0; $i < count($status); $i += 2)
        {
            $info[$status[$i]] = trim($status[$i+1]);
        }
       
        $this->status_cache = $info;
       
        // Get the player list
        $players = array();
       
        $index = 0;
        foreach($result as $line)
        {
            $line = trim($line);
            $line = explode(" ", $line, 3);
           
            if(!$line[2]) continue;
           
            if(strpos($info['pr']."!", "!$index!") !== false)
                $team = TEAM_RED;
            else if(strpos($info['pb']."!", "!$index!") !== false)
                $team = TEAM_BLUE;
            else if(strpos($info['py']."!", "!$index!") !== false)
                $team = TEAM_YELLOW;
            else if(strpos($info['pp']."!", "!$index!") !== false)
                $team = TEAM_PURPLE;
            else
                $team = TEAM_OBSERVER;
           
            $players[] = array('name' => substr($line[2], 1, -1),
                               'ping' => $line[1],
                               'score' => $line[0],
                               'team' => $team);
           
            $index++;
        }
       
        $this->players_cache = $players;
    }
}

Or if you don't want to read that code, mind telling me how it's done?

b00nlander

  • Autococker
  • Posts: 784
Re: Parsing server status data for player team colors
« Reply #1 on: November 25, 2007, 04:44:43 PM »
hm, this is what I did with my java bot, and it seems to work fine:

Code: [Select]
String[] players = new String[5];
String[] pb = null;
String[] pr = null;
String[] py = null;
String[] pp = null;
String[] po = null;
try {
String response = QueryServer.send("XXXX");
String[] message = response.split("\\\\");

for(int i = 0; i < message.length ; i++){
if(message[i].equals("pb")){
pb = message[i+1].split("!");
}
else if(message[i].equals("pr")){
pr = message[i+1].split("!");
}
else if(message[i].equals("py")){
py = message[i+1].split("!");
}
else if(message[i].equals("pp")){
pp = message[i+1].split("!");
}
else if(message[i].equals("po")){
po = message[i+1].split("!");
}
}

message[message.length-1].concat("\"");
message = message[message.length-1].split("\"");

if(pb != null){
for(int i = 0; i < pb.length-1; i++){
players[0] = message[Integer.parseInt(pb[i+1])*2 + 1] + ", " + players[0];
}
players[0] = players[0].substring(0,players[0].length()-6);
}
if(pr != null){
for(int i = 0; i < pr.length-1; i++){
players[1] = message[Integer.parseInt(pr[i+1])*2 + 1] + ", " + players[1];
}
players[1] = players[1].substring(0,players[1].length()-6);
}
if(py != null){
for(int i = 0; i < py.length-1; i++){
players[2] = message[Integer.parseInt(py[i+1])*2 + 1] + ", " + players[2];
}
players[2] = players[2].substring(0,players[2].length()-6);
}
if(pp != null){
for(int i = 0; i < pp.length-1; i++){
players[3] = message[Integer.parseInt(pp[i+1])*2 + 1] + ", " + players[3];
}
players[3] = players[3].substring(0,players[3].length()-6);
}
if(po != null){
for(int i = 0; i < po.length-1; i++){
players[4] = message[Integer.parseInt(po[i+1])*2 + 1] + ", " + players[4];
}
players[4] = players[4].substring(0,players[4].length()-6);
}


}

quick & dirty & functional :)
players[] is returned, of course

jitspoe

  • Administrator
  • Autococker
  • Posts: 18802
Re: Parsing server status data for player team colors
« Reply #2 on: November 27, 2007, 10:02:34 PM »
Players that are connecting but aren't in the game yet (downloading maps) can throw it off.  There's code in the serverbrowser to do it if you need it.