geek85.net Wiki

wiki associé au site www.geek85.net

Outils pour utilisateurs

Outils du site


blog:dev:image_dynamique_pour_serveur_terraria
terraria-status.php
<?php
    //--- init for avoid reporting errors ---
    ini_set('default_socket_timeout',5);
    ini_set('display_errors', 'Off');
    ini_set('log_errors', 'On');
    ini_set('html_errors', 'Off');
    ini_set('error_log', '/tmp/terraria-status.log');
    error_reporting(E_ERROR);
    //--- variables ---
    $ip = 'domain.tld';
    $port = '7878'; //port of rest server, not terraria server
    $login = 'user'; //user with SuperAdmin permissions
    $password = 'password';
    $json = json_decode(file_get_contents('http://'.$ip.':'.$port.'/token/create/'.$login.'/'.$password),true);
    $token = $json['token'];
    $json = json_decode(file_get_contents('http://'.$ip.':'.$port.'/world/read?token='.$token), true);
    $daytime = $json['daytime'];
    $json = json_decode(file_get_contents('http://'.$ip.':'.$port.'/status?token='.$token), true);
    header('Content-Type: image/png');
    $im = @imagecreatefrompng('Images/terraria_online.png'); //make your own
    $font = 'fonts/visitor1.ttf'; //use your own too
    $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);
    //--- end variables ---
    if ($json['status'] == '200') //server found and running
    {
        //daytime
        if ($daytime != True)
        {
            $ok = imagefilter($im, IMG_FILTER_BRIGHTNESS, -100); //darken the image for night time on server
        }
        //--- text in the box ---
        $playerarray = explode(', ',$json['players']);
        $playerarray = array_map('strtolower',$playerarray);
        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']); //yeah, it's in French! ^^
        imagettftext($im, 14, 0, 8, 70, $blue, $font, 'Joueurs en ligne: '.$json['playercount']);
        if ($json['playercount'] > 0)
        {
            //--- show VIPs' avatar ---
            $filename = '/srv/http/vip.txt'; //the file is just lines of names and two coordinates separated with spaces
            $vips = file($filename, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
            foreach ($vips as $vip_num => $vip)
            {
                $vip = explode(' ',$vip); //if you want to use another separator than space, you must change it here
                  $name = strtolower($vip[0]);
                if (in_array($name, $playerarray))
                {
                        $player = @imagecreatefrompng('Images/'.$name.'.png'); //each vip has his own png pre-created
                        $long = imageSX($player);
                        $haut = imageSY($player);
                        $ok = imagecopy($im,$player,$vip[1],$vip[2]-$haut,0,0,$long,$haut);
                }
            }
            //--- print name of all players ---
            $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, $font2, $player);
                imagettftext($im, 11, 0, 10+($j*75), 100+($i*15), $white, $font2, $player);
            }
        }
    }
    // if offline OR statut != 200 - considerated offline
    else
    {
        $im = @imagecreatefrompng('Images/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);
?>

exemple online

exemple offline

after code

blog/dev/image_dynamique_pour_serveur_terraria.txt · Dernière modification: 20/02/2014 11:20 par geek85