Location: PHPKode > projects > Fretsweb > fretsweb-1.2/fretsweb/player.php
<?php

/*
Fretsweb - A Frets on Fire chart server
Copyright (C) 2009  Daan Sprenkels

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program 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 General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/


// Include the common file
require_once "admin/common.php";

// Send headers for content-type
header('Content-Type: text/html; charset=utf-8');

// Include the language
require_once "lang/$language.php";


function is_valid($mixed)
/*
bool is_valid(mixed $mixed)
	checks if $mixed is valid information
*/
{
	if(is_numeric($mixed))
	{
		if($mixed > 0)
		{
			return True;
		}
		else
		{
			return False;
		}
	}
	else
	{
		if($mixed == 'None')
		{
			return False;
		}
		elseif(strlen($mixed) > 0)
		{
			return True;
		}
		else
		{
			return False;
		}
	}
}


// Check if a player name isset, otherwise go to players.php
if(!isset($_GET['name']))
{
	header('location: players.php');
	die();
}
elseif(mysql_num_rows(mysql_query("SELECT * FROM `contest_players` WHERE `name`='{$_GET['name']}'")) == 0)
{
	header('location: players.php');
	die();
}


// Get the informations about the player
$sql = "SELECT `name`,`Amazing`,`Medium`,`Easy`,`Supaeasy`,`time` FROM `contest_players` WHERE `name`='{$_GET['name']}' LIMIT 1";
$query = mysql_query($sql);
$player = mysql_fetch_assoc($query);


// Create $difficulties witch is an array with the allowed difficulties
$difficulties = array();
foreach( array( "Amazing", "Medium", "Easy", "Supaeasy" ) as $difficulty )
	if($player[$difficulty] == 1)
		$difficulties[count($difficulties)] = $difficulty;


// Write header
print_header($lang["player"], $lang['player'] . ' ' . $player['name']);

// Get scores
$users = mysql_query( "SELECT * FROM contest_players" ) or die( "DB error (get players in DCC)" );
$dbbig = mysql_query( "SELECT `hash`,`difficulty`,`name`,`score` FROM `contest_scores`" );
while( $row = mysql_fetch_assoc( $dbbig ) ){
	$data[ $row["difficulty"] ][ $row["hash"] ][ $row["name"] ] = $row["score"];
}
mysql_free_result($dbbig);


// Calculate
echo "<table class=\"regular\"><tr>";
foreach( $difficulties as $difficulty )
	echo "<th>{$lang[$difficulty]}</th>";
echo "</tr><tr>";

foreach( $difficulties as $difficulty )
{
	$i = 0;
	while($i < mysql_numrows( $users ))
	{
		// Check if the user is allowed to upload at this difficulty
		if( mysql_result($users, $i, $difficulty) )
		{
			$points[ mysql_result($users, $i, "name") ] = 0;
		}
		$i++;
	}
	// Calculate
	if(isset($data[$difficulty]))
	{
		foreach( $data[$difficulty] as $hash => $name_score )
		{
			arsort( $name_score );
			$counter = -1;
			$oldscore = -1;
			foreach( $name_score as $name => $score )
			{
				if( $score != $oldscore )
				{
					$counter++;
				}
				$oldscore = $score;
				if( $counter < count( $ContestPoints ) )
					$points[ $name ] += $ContestPoints[ $counter ];
			}
		}
	}
	// Write player scores
	if($points[$player['name']] != '')
	{
		echo "<td>{$points[$player['name']]} {$lang['points']}</td>";
	}
	else
	{
		echo "<td>0 {$lang['points']}</td>";
	
	}
}
echo "</tr></table>";


// For each difficulty: write a table with scores
$sql = "SELECT `hash`, `difficulty`, `score`, `stars`, `notes_hit`, `notes_all`, `note_streak`, `original_score` FROM `contest_scores` WHERE `name`='{$player['name']}' ORDER BY `score` DESC" ;
$query = mysql_query($sql);
if(mysql_num_rows($query) > 0)
{
	echo "<h2>{$lang['scores']}</h2>";
	while($row = mysql_fetch_assoc($query))
	{
		// Check if the score is double
		if(!isset($scores[$row['hash']][$row['difficulty']]))
		{
			$scores[$row['difficulty']][$row['hash']] = $row;
		}
		// Pick the highest of them
		else
		{
			if($scores[$row['difficulty']][$row['hash']] < $row['score'])
			{
				$scores[$row['difficulty']][$row['hash']] = $row;
			}
		}
	}
	// Fetch the song information
	$sql = "SELECT `hash`, `artist`, `title` FROM `contest_songs`";
	$query = mysql_query($sql);
	while($row = mysql_fetch_assoc($query))
	{
		$songs[$row['hash']] = $row;
	}
	foreach($difficulties as $difficulty)
	{
		// If the player has scores on this difficulty
		if(isset($scores[$difficulty]))
		{
			// Table title
			echo "<h3>$difficulty</h3>";
			echo "<table class=\"regular\">";
			echo "<tr><th>{$lang['song']}</th><th>{$lang['score']}<!-- Percent, streak --></th>";
			foreach($scores[$difficulty] as $hash => $score)
			// The writing will be like the FoFiX way
			{
				echo "<tr>";
				// Song
				echo "<td class=\"amiddle\"><a href=\"song.php?hash=$hash\">" . $songs[$hash]['artist'] . " - " . $songs[$hash]['title'] . "</a></td>";
				// Make an extra small table to align the information right
				echo "<td class=\"amiddle\">";
				echo "<table><tr>";
				// Score
				echo "<td class=\"amiddle\">" . $score['score'] . "</td>";
				// Percent and streak
				if(is_valid($score['notes_hit']) && is_valid($score['notes_all']))
				{
					$percent = round(100 / $score['notes_all'] * $score['notes_hit'], 1);
				}
				else
				{
					$percent = False;
				}
				if(is_valid($score['note_streak']))
				{
					$note_streak = $score['note_streak'];
				}
				else
				{
					$note_streak = False;
				}
				if($percent && $note_streak)
				{
					echo "<td class=\"amiddle\">$percent% ($note_streak)</td>";
				}
				else
				{
					echo "<td>";
				}
				// Stars
				if(is_valid($score['stars']))
				{
					if($score['stars'] == 6)
					{
						$stars = "<img src=\"images/star3-small.png\"><img src=\"images/star3-small.png\"><img src=\"images/star3-small.png\"><img src=\"images/star3-small.png\"><img src=\"images/star3-small.png\">";
					}
					else
					{
						$i = 1;
						$stars = "";
						while($i <= 5)
						{
							if($score['stars'] >= $i)
							{
								$stars .= "<img src=\"images/star2-small.png\">";
							}
							else
							{
								$stars .= "<img src=\"images/star1-small.png\">";
							}
							$i++;
						}
					}
					echo "<td class=\"amiddle\">$stars</td>";
				}
				else
				{
					echo "<td>";
				}
				echo "</tr></table>";
				echo "</td>";
				echo "</tr>\n";
				unset($percent, $note_streak, $stars);
			}
			echo "</table>";
		}
	}
}


// Write footer
echo "</div>";
include "admin/pagefooter.php";
echo "
</div>
</body>
</html>";


// Close database link
mysql_close( $db_link );

?>
Return current item: Fretsweb