PHP Json Terraria服务器状态

时间:2013-04-23 03:37:34

标签: php imagettftext

我在google / stackoverflow上搜索了很长时间,但没有找到答案。 我试图把图像看看我的Terraria服务器是否在线,如果是的话,谁连接等...:p我在php中真的很棒,但我正在努力学习!我在tshock.co上从geek85获得了这个源代码。这是来源:

<?php
    error_reporting(E_ERROR);
    //variables
    $ip = 'XXX.XXX.XXX.XXX';
    $port = '7878'; //port of rest server, not terraria server
    $json = json_decode(file_get_contents('http://'.$ip.':'.$port.'/status/'), true);
    header('Content-Type: image/png');
    $im = @imagecreatefrompng('terraria_online.png');
    $font = 'visitor1.ttf';
    //$font2 = 'fonts/visitor2.ttf';
    $red = imagecolorallocate($im, 255, 24, 24);
    $blue = imagecolorallocate($im, 50,50,255);
    $black = imagecolorallocate($im, 10,10,10);
    $purple = imagecolorallocate($im, 127,0,127);
    $grey = imagecolorallocate($im, 50, 50, 50);
    $white = imagecolorallocate($im, 255, 255, 255);

    //server found and running
    if ($json['status'] == '200')
    {
        // text in the grey box
        $playerarray = explode(', ',$json['players']);
        imagettftext($im, 18, 0, 31, 16, $grey, $font, $json['name']);
        imagettftext($im, 18, 0, 30, 15, $red, $font, $json['name']);
        imagettftext($im, 14, 0, 31, 34, $grey, $font, 'IP: '.$ip);
        imagettftext($im, 14, 0, 30, 33, $black, $font, 'IP: '.$ip);
        imagettftext($im, 14, 0, 31, 49, $grey, $font, 'Port: '.$json['port']);
        imagettftext($im, 14, 0, 30, 48, $black, $font, 'Port: '.$json['port']);
        imagettftext($im, 14, 0, 9, 71, $grey, $font, 'Joueurs en ligne: '.$json['playercount']);
        imagettftext($im, 14, 0, 8, 70, $blue, $font, 'Joueurs en ligne: '.$json['playercount']);
        // show VIPs' avatar
        if ($json['playercount'] > 0)
        {
            //array of VIP players - (next step : a txt file with names and coordinates to parse...)
            $vip = array('QcSeb','Marlin','Twingo','Gaston');
            $num = count($vip);
            for ($i=0; $i<$num; $i++)
            {
                if (in_array($vip[$i], $playerarray))
                {
                    $player = @imagecreatefrompng('Images/'.strtolower($vip[$i]).'.png');//This line doesn't work for now
                    $long = imageSX($player);
                    $haut = imageSY($player);
                    switch ($vip[$i])
                    {
                        //here, it's a bit difficult: these are the coordinate where to place VIP players in the image.
                        case 'QcSeb':
                            $x = 350;
                            $y = 133;
                            break;
                        case 'Gaston':
                            $x = 300;
                            $y = 68;
                            break;
                        case 'Marlin':
                            $x = 261;
                            $y = 68;
                            break;
                        case 'Twingo':
                            $x = 307;
                            $y = 133;
                            break;
                    }
                    $ok = imagecopy($im,$player,$x,$y-$haut,0,0,$long,$haut);
                }
            }
            // name of players (VIP or not)
            $i = -1;
            $j = 0;
            // since my server only accept 8 players, I don't have any overflow control on the number of player's name to write but this code can do the work for any number of player (< to width of image / 75)
            foreach( $playerarray as $player )
            {
                if ($i++ >= 6)
                {
                    $i = 0;
                    $j++;
                }
                imagettftext($im, 11, 0, 11+($j*75), 101+($i*15), $grey, $font, $player);
                imagettftext($im, 11, 0, 10+($j*75), 100+($i*15), $purple, $font, $player);
            }
      }
    }
    // offline or not "statut == 200"
    else
    {
        $im = @imagecreatefrompng('terraria_offline.png');
        imagettftext($im, 18, 0, 11, 31, $purple, $font, 'IP:'.$ip.':'.$port);
        imagettftext($im, 18, 0, 10, 30, $grey, $font, 'IP:'.$ip.':'.$port);
        imagettftext($im, 42, 17, 16, 131, $purple, $font, 'offline');
        imagettftext($im, 42, 17, 15, 130, $black, $font, 'offline');
    }
    imagepng($im);
    imagedestroy($im);
    ?>

我正在使用000webhost:sebserver.comuf.com(/ServerStatus1.php)。 当我继续 http://XXX.XXX.XXX.XXX:7878/status/我明白了:

{
  "status": "200",
  "name": "TShock Server",
  "port": "7777",
  "playercount": "1",
  "players": "QcSeb"
}

因为实际上只有我在服务器上。

(XXX.XXX.XXX.XXX =我的隐藏IP:P)

以下是我正在尝试做的一个示例:http://renagadez.square7.de/gen.php?ip=111.111.111.111&port=1111

提前感谢您的帮助!

0 个答案:

没有答案