Dodanie jednej opcji do skryptu

0
<?php
        /******************************************************************\
        |* Copyright (c) by starlex.de, Alexander Becker
        |*
        |* Visit:
        |*              [url="http://starlex.de"]http://starlex.de[/url]
        |*
        |* License:
        |*              [url="http://creativecommons.org/licenses/by-sa/3.0/deed.en"]http://creativecommons.org/licenses/by-sa/3.0/deed.en[/url]
        |*
        \******************************************************************/
       
       
        $serverstatus = mc_status("176.31.240.28:25567"); // <- your serveraddress here
        echo ("motd: ".$serverstatus["motd"]);
        echo ("\n<br/>\n");
        echo ("players: ".$serverstatus["cur_players"]."/".$serverstatus["max_players"]);
       
        function mc_status( $server ) {
                if($socket = stream_socket_client('tcp://'.$server , $errno, $errstr, 1)) // connect to the server
                {
                        fwrite($socket, "\xfe");                        // writes the query
                        $tmp = fread($socket, 2048);            // reads the serverinfo
                        $tmp = str_replace("\x00","",$tmp); // removes all 0x00
                        $tmp = substr ($tmp,2);                         // removes the first 2 bytes
                       
                        $info = explode("§",$tmp);                     // splits the sting into a array
                        unset($tmp);
                       
                        if(count($info) == 3)                           // check array (if modt contains invalid chars(§))
                        {
                       
                                return(                                                 // returns a new Array (END)
                                        Array(                                 
                                                "motd" => $info[0],
                                                "cur_players" => $info[1],
                                                "max_players" => $info[2]
                                        )
                                );
                       
                        } else if(count($info) > 3){            // DEAL ERRORS - INVALID CHARS : !!! not added in the minecraft client !!!
                       
                                $tmp = "";
                                for ($i = 0; $i < count($info) - 2; $i++) {
                                        $tmp .= $info[$i];
                                        if(!($i == count($info) - 2 - 1))
                                        {
                                                $tmp .= "§";
                                        }
                                }
                               
                                return(                                                 // returns a new Array (END)
                                        Array(                                 
                                                "motd" => $tmp,
                                                "cur_players" => $info[count($info) - 2],
                                                "max_players" => $info[count($info) - 1]
                                        )
                                );
                               
                        } else {
                                return false;                                   // return false (if syntax is wrong)
                        }
                       
                } else {
                        return false;                                           // return false (if connection failed in first step)
                }
        }
?>

Witam, powyższy skrypt pokazuję na stronie ile graczy jest online w danym momencie na serwerze Minecraft.
Chciałbym żeby w momencie wyłączenia serwera pokazywał:
-"/".$serverstatus["max_players"]);

np. -/40

Ewentualnie mogę na sztywno zamienić wartość max_players na liczbę slotów.
Ponieważ w tym momencie kiedy serwer jest wyłączony pokazuje on błędy.

Pozdrawiam

0

Warning: stream_socket_client() [function.stream-socket-client]: unable to connect to tcp://176.31.240.28:25567 (Connection refused) in /home[...]

To ta linia: if($socket = stream_socket_client('tcp:*'.$server , $errno, $errstr, 1)) * connect to the server -

0

Serwer odrzuca połączenie - to nie ma nic wspólnego ze skryptem.

Edit: wybacz, przeoczyłem. Odpowiedź niżej.

0

no nic dziwnego skoro nie działa.

Tak jak napisałem kiedy serwer jest ON to pokazuję ile graczy jest online ale jeżeli jest OFF to wyskakują błędy.
A chciałbym żeby skrypt to uwzględniał i wyświetlał wtedy informację którą np. sam sobie ustawię.

0

to trochę "brzydkie" rozwiązanie, ale powinno działać

 if($socket = @stream_socket_client('tcp://'.$server , $errno, $errstr, 1)) // connect to the server
0

No to przecież ta funkcja zwraca false. Do wywołania funkcji połączenia dodaj @:

if($socket = @stream_socket_client('tcp://'.$server , $errno, $errstr, 1))

I potem dodaj sobie odpowiednią instrukcję if:

  $serverstatus = mc_status("176.31.240.28:25567"); // <- your serveraddress here
  if ($serverstatus != false) {
    echo ("motd: ".$serverstatus["motd"]);
    echo ("\n<br/>\n");
    echo ("players: ".$serverstatus["cur_players"]."/".$serverstatus["max_players"]);
  } else {
    echo 'Klops.';
  }
0

dokładnie, o to mi chodziło :)
Dzięki wielkie

1 użytkowników online, w tym zalogowanych: 0, gości: 1