Source

Selador / banner.php

Full commit
<?php

/* banner.php - Will draw a small banner containing a users stats
 *
 * Copyright (C) 2006, 2007, 2008 Kevin Read, Simone Schaefer
 *
 * This file is part of Selador, a browser-based fantasy strategy game
 *
 * This program is distributed under the terms of the GNU Affero General Public License.
 *
 *
 *   Selador is free software: you can redistribute it and/or modify
 *   it under the terms of the GNU Affero General Public License as published by
 *   the Free Software Foundation, either version 3 of the License, or
 *   any later version.
 *
 *   Selador is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU Affero General Public License for more details.
 *
 *   You should have received a copy of the GNU Affero General Public License
 *   along with Selador.  If not, see <http://www.gnu.org/licenses/>.
 **/

require_once ("php/base/config.inc.php");
require_once ("php/base/GameDB.inc.php");

if (!isset ($_REQUEST['u']))
{
	echo "ultrafail";
	die ();
}

$debug = false;

$uid = (int)$_REQUEST['u'];

$db = new GameDB;
$fromtop = 4;
$fromleft = 275;
$height = 36;
$width = 100;
$rankfromtop = $scorefromtop = 0;

$query = "select user,floor(score/1000) as score,class,race from user where uid=".$uid;

if ($res = $db->query ($query))
{
	if ($userrow = mysql_fetch_array ($res))
	{
		if (!$debug)
			header("Content-Type: image/png");
		// Set the enviroment variable for GD
		// putenv('GDFONTPATH=' . realpath('.'));
		putenv('GDFONTPATH=' . realpath('.')."/fonts");

		$font2="fonts/CelticHand.ttf";
		$font="Vera";
		$fontsize=8;

		$img = imagecreatefromjpeg ("gfx/weniger-baum.jpg");
		$black = ImageColorAllocate($img, 0,0,0);  // black
		$blacktr = ImageColorAllocateAlpha ($img, 0,0,0, 90);  // black
		$light = ImageColorAllocate($img, 200, 200, 200);

		$gray = ImageColorAllocate ($img, 140, 90, 90);
		$graytr = ImageColorAllocateAlpha ($img, 140, 90, 90, 90);

		$query = "select ranking,score,heroranking,heroscore from rankinghistory where uid=".$uid." order by date asc";

		if ($debug)
			echo "<pre>";

		if (($res = $db->query ($query)))
		{
			$maxrank = $maxhero = $maxscore = $maxheroscore = 0;
			$minrank = $minhero = $minscore = $minheroscore = 1000000;
			while ($row = mysql_fetch_array ($res))
			{
				$minrank = min ($minrank, $row['ranking']);
				$minscore = min ($minscore, $row['score']);
				$maxrank = max ($maxrank, $row['ranking']);
				$maxscore = max ($maxscore, $row['score']);

				$statrows[] = $row;
			}

			$maxrank -= $minrank;
			$realmaxscore = $maxscore;
			$maxscore -= $minscore;

			if ($maxrank == 0)
			{
				$maxrank = 1;
				$rankfromtop = (int)($height / 2)-1;
			}

			if ($maxscore == 0)
			{
				$maxscore = 1;
				$scorefromtop = (int)($height / 2) + 1;
			}

			if ($debug)
				echo "MAXS: ".$maxscore.", RMAXS: ".$realmaxscore.", MINS: ".$minscore."\n";

			$maxscore = max (1, $maxscore);

			if (count ($statrows) == 1)
				$statrows[] = reset ($statrows);

			if ($debug)
				print_r ($statrows);

			$maxcols = count ($statrows) - 1;
			for ($idx = 0; $idx <= $maxcols; $idx++)
			{
				$row = $statrows[$idx];
				if (isset ($lastrow))
				{
					// rank
					imageline ($img, (int) (($idx-1) / $maxcols * $width) + $fromleft, (int) (($lastrow['ranking'] - $minrank) / $maxrank * $height) + $fromtop + $rankfromtop,
						(int) ($idx / $maxcols * $width) + $fromleft, (int) (($row['ranking'] - $minrank) / $maxrank * $height) + $fromtop + $rankfromtop, $black);
					imageline ($img, (int) (($idx-1) / $maxcols * $width) + $fromleft + 1, (int) (($lastrow['ranking'] - $minrank) / $maxrank * $height) + $fromtop + $rankfromtop,
						(int) ($idx / $maxcols * $width) + $fromleft + 1, (int) (($row['ranking'] - $minrank) / $maxrank * $height) + $fromtop + $rankfromtop, $blacktr);
					imageline ($img, (int) (($idx-1) / $maxcols * $width) + $fromleft - 1, (int) (($lastrow['ranking'] - $minrank) / $maxrank * $height) + $fromtop + $rankfromtop,
						(int) ($idx / $maxcols * $width) + $fromleft - 1, (int) (($row['ranking'] - $minrank) / $maxrank * $height) + $fromtop + $rankfromtop, $blacktr);

					// score
					imageline ($img, (int) (($idx-1) / $maxcols * $width) + $fromleft, (int) (($realmaxscore - $lastrow['score']) / $maxscore * $height) + $fromtop + $scorefromtop,
						(int) ($idx / $maxcols * $width) + $fromleft, (int) (($realmaxscore - $row['score']) / $maxscore * $height) + $fromtop + $scorefromtop, $gray);
						if ($debug)
							echo "Y1 ".((int)(($realmaxscore - $lastrow['score']) / $maxscore * $height) + $fromtop).", Y2 ".((int) (($realmaxscore - $row['score']) / $maxscore * $height) + $fromtop)."\n";

					imageline ($img, (int) (($idx-1) / $maxcols * $width) + $fromleft + 1, (int) (($realmaxscore - $lastrow['score']) / $maxscore * $height) + $fromtop + $scorefromtop,
						(int) ($idx / $maxcols * $width) + $fromleft + 1, (int) (($realmaxscore - $row['score']) / $maxscore * $height) + $fromtop + $scorefromtop, $graytr);
					imageline ($img, (int) (($idx-1) / $maxcols * $width) + $fromleft - 1, (int) (($realmaxscore - $lastrow['score']) / $maxscore * $height) + $fromtop,
						(int) ($idx / $maxcols * $width) + $fromleft - 1, (int) (($realmaxscore - $row['score']) / $maxscore * $height) + $fromtop, $graytr);
				}
				$lastrow = $row;
			}
		}

		ImageTTFText ($img, $fontsize, 0, 220, 14, $black, $font, $userrow['user']);
		ImageTTFText ($img, $fontsize, 0, 220, 28, $black, $font, $classes_byid[$userrow['class']]);
		ImageTTFText ($img, $fontsize, 0, 220, 42, $black, $font, $races_byid[$userrow['race']]);
		ImageTTFText ($img, $fontsize, 0, 382, 14, $black, $font, "Rang: ".$lastrow['ranking']);
		ImageTTFText ($img, $fontsize, 0, 382, 28, $gray, $font, "Punkte: ".$lastrow['score']);
		ImageTTFText ($img, $fontsize+2, 0, 115, 42, $black, $font, WORLDTITLE);
		// $size = imagettfbbox ($img, $fontsize, 0, $font, (int)$num);

		if (!$debug)
			imagepng($img);
	}
	else
		echo "fail";
}
?>