felixms / arma-rcon-class-php

A lightweight client for sending commands easily to a BattlEye server.
MIT License
46 stars 22 forks source link

feature request: have get_players return array #4

Closed nerdalertdk closed 8 years ago

nerdalertdk commented 8 years ago

Hi,

Working on an control panel for my servers and right now i just get one big text string with player info and i have to hack with regex

felixms commented 8 years ago

I was thinking about doing this a time ago, but I hadn't the time for it. I will add this feature asap, I found an Altis Life forum post, where it's already done.

nerdalertdk commented 8 years ago

Well like my version better :)

try
{
    $rcon = new \Nizarii\ArmaRConClass\ARC($server->ip,(int)$server->port,$server->password);
    $players = preg_replace("/[\x00-\x09\x0B\x0C\x0E-\x1F\x7F]/", "", $rcon->get_players() );
    if($players)
    {
        preg_match_all("#(\d+)\s+(\b\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}:\d+\b)\s+(\d+)\s+([0-9a-fA-F]+)\(\w+\)\s([\S ]+)$#im",$players, $str);

        // Remove first array
        array_shift($str);
        // Create return array
        $result = array();
        // Loop true the main arrays, each holding a value
        foreach($str as $key => $value)
        {
            // Combine's each main vaule in to new array 
            foreach($value as $keyLine => $line)
            {
                $result[$keyLine][$key] = $line;
            }
        }
        return $result;

    }
    else
    {
        echo "Error getting players";
    }
}
catch(Exception $e)
{
    echo $e->getMessage();
}
nerdalertdk commented 8 years ago

updatede script for a better return array :)

felixms commented 8 years ago

@nerdalertdk Seems pretty good to me, is it ok, if I'll add this to ARC with your credits?

nerdalertdk commented 8 years ago

sure, but maybe make it so you have both one called get_players() and one get_players_array()

this line is important, since it removes weird chars $players = preg_replace("/[\x00-\x09\x0B\x0C\x0E-\x1F\x7F]/", "", $rcon->get_players() );

also my scripts works with get_bans

alexcroox commented 8 years ago

I get

Warning: preg_replace(): Null byte in regex in rcon-test/index.php on line 8

Querying an empty server

Line in question

$players = preg_replace("/[\x00-\x09\x0B\x0C\x0E-\x1F\x7F]/", "", $rcon->get_players() );