Location: PHPKode > projects > Fretsweb > fretsweb-1.2/fretsweb/charts.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 language
require_once "lang/$language.php";


// Write header
print_header($lang["home"], $title);


// Write the contest status (opened/closed)
include_once "admin/status.php";
echo "<h3 align=\"center\">" . sprintf($lang['contest_is'] , contest_status()) . "</h3>";


// Write the uploadurl if allowed
if($showuploadurl)
{
	if($_SERVER['HTTPS'] == 'on')
		$protocol = 'https';
	else
		$protocol = 'http';
	if(dirname($_SERVER['PHP_SELF']) == '/')
		$dir = '';
	else
		$dir = dirname($_SERVER['PHP_SELF']);
	echo sprintf( "<h5 align=\"center\">".$lang['uploadurl_is']."</h5>" , $protocol.'://' . $_SERVER['HTTP_HOST'] . $dir . '/upload.php' ); 
}


// Write the table with the highest ranks
$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);
foreach( array('Amazing','Medium','Easy','Supaeasy') as $difficulty )
{
	$i = 0;
	while($i < mysql_numrows($users))
	{
		if( mysql_result($users, $i, $difficulty) )
		// Init only if the user is allowed to upload scores at this 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 ];
			}
		}
	}
	// Set the scores at rank 1, 2, 3
	if( count( $points ) != 0 )
	{
		ksort( $points );
		// Arsort is not stable, but at least this way we always guarantee the same order for players with the same score
		arsort( $points );
		$n = 1;
		foreach ($points as $user => $score)
		{
			if($n > 3)
			{
				break;
			}
			if( $score != 0 )
			{
				$scores[$difficulty][$n] = "<a href=\"player.php?name=$user\">$user</a>";
			}
			else
			{
				$scores[$difficulty][$n] = $lang['nobody'];
			}
			$n++;
		}
	}
}


// Write the scores table
echo '<table class="regular">';

// Write header row
echo "<tr><td><th>{$lang['Amazing']}</th><th>{$lang['Medium']}</th><th>{$lang['Easy']}</th><th>{$lang['Supaeasy']}</th></tr>";
foreach (array(1, 2, 3) as $n)
{
	echo "<tr>";
	if($n == 1)
		echo '<td><img src="images/gold.png" alt="Gold"></td>';
	elseif($n == 2)
		echo '<td><img src="images/silver.png" alt="Silver"></td>';
	elseif($n == 3)
		echo '<td><img src="images/bronze.png" alt="Bronze"></td>';
	foreach ( array('Amazing','Medium','Easy','Supaeasy') as $difficulty )
		echo "<td>" . $scores[$difficulty][$n] . "</td>";
	echo "</tr>";
}

// End the scores table
echo '</table>';


// Begin the table with random informations
echo "<table class=\"regular\">";


// Most played song
$sql = 'SELECT `hash` FROM `contest_scores`';
$query = mysql_query($sql);
if(mysql_num_rows($query) > 0)
{
	echo "<tr>";
	echo "<th>{$lang['most_played_song:']}</th>";
	$rows = array();
	while($row = mysql_fetch_row($query))
	{
		$rows[count($rows)] = $row[0];
	}
	$counts = array_count_values($rows);
	arsort($counts , SORT_NUMERIC);
	$counts = array_flip($counts);
	$hash = current($counts);
	$sql = "SELECT `hash`,`artist`,`title` FROM `contest_songs` WHERE `hash`='$hash'";
	$query = mysql_query($sql);
	if(mysql_num_rows($query) > 0)
	{
		$row = mysql_fetch_assoc($query);
		echo "<td><a href=\"song.php?hash={$row['hash']}\">{$row['artist']} - {$row['title']}</a></td>";
	}
	else
		echo "<td>{$lang['no_information']}</td>";
	echo "</tr>";
}


// Last played song
$sql = "SELECT `hash` FROM `contest_scores` ORDER BY `time` DESC LIMIT 1";
$query = mysql_query($sql);
if(mysql_num_rows($query) > 0)
{
	echo "<tr>";
	echo "<th>{$lang['last_played_song:']}</th>";
	$row = mysql_fetch_row($query);
	$hash = $row[0];
	$sql = "SELECT `hash`,`artist`,`title` FROM `contest_songs` WHERE `hash`='$hash'";
	$query= mysql_query($sql);
	if($row = mysql_fetch_row($query))
	{
		list($hash, $artist, $title) = $row;
		echo "<td><a href=\"song.php?hash=$hash\">$artist - $title</a></td>";
	}
	else
	{
		echo "<td>{$lang['no_information']}</td>";
	}
	echo "</tr>";
}


// Most active player
$sql = 'SELECT `name` FROM `contest_scores` LIMIT 1';
$query = mysql_query($sql);
if(mysql_num_rows($query) > 0)
{
	echo "<tr>";
	echo "<th>{$lang['most_active_player:']}</th>";
	$rows = array();
	while($row = mysql_fetch_row($query))
	{
		$rows[count($rows)] = $row[0];
	}
	$counts = array_count_values($rows);
	arsort($counts , SORT_NUMERIC);
	$counts = array_flip($counts);
	$name = current($counts);
	echo "<td><a href=\"player.php?name=$name\">$name</a></td></tr>";
}


// Last added player
$sql = "SELECT `name` FROM `contest_players` WHERE `joinrequest`='0' ORDER BY `time` DESC LIMIT 1";
$query = mysql_query($sql);
if(mysql_num_rows($query) > 0)
{
	echo "<tr>";
	echo "<th>{$lang['last_added_player:']}</th>";
	$row = mysql_fetch_assoc($query);
	echo "<td><a href=\"player.php?name=".stripslashes($row['name'])."\">".stripslashes($row['name'])."</a></td></tr>";
}


// End table with random informations
echo "</table>";


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


// Close the database link
mysql_close( $db_link );

?>
Return current item: Fretsweb