<?php
include ('header.inc.php');

include('classes/PlayerRound.php');
include('classes/Round.php');
include('classes/Song.php');
include('classes/PlayerTotal.php');
include('classes/Ranking.php');
include('classes/GeneralRanking.php');

function score_compare($a, $b)
{
   return strcmp($b["score"], $a["score"]);
}

//Texte des types de pas
$s_text_step_marvelous='Marvelous';
$s_text_step_perfect='Perfect';
$s_text_step_great='Great';
$s_text_step_good='Good';
$s_text_step_boo='Boo';
$s_text_step_miss='Miss';
$s_text_step_ok='OK';
$s_text_step_ng='NG';
$s_text_step_hmine='Mines Touch&eacute;es';
$s_text_step_amine='Mines Evit&eacute;es';

//////////////////////
// Last Round Infos //
//////////////////////

$o_actual_player = new PlayerAccount();
if(isset($_GET["pid"]))
{
	$o_actual_player->SetPlayerID($_GET["pid"]);
}
else
{
	$o_actual_player->SetPlayerID($o_player->GetPlayerID());
}

//Set PlayerRound object
$o_player_round=new PlayerRound();
if(isset($_GET["round_key"]))
{
	$a_round_key[]=$_GET["round_key"];
}
else
{
	$a_round_key = $o_player_round->GetPlayerRound($o_actual_player->GetPlayerID());
}

if(count($a_round_key)==0)
{
	?><b><font face='Arial' color='#FF0000'>Ce joueur n'a joué aucune parties.</font></b><br><br><?php
	include ('footer.inc.php');
	exit;
}

$o_player_round->SetRoundKey($a_round_key[0]);

if($o_player_round->GetPlayerID() != $o_actual_player->GetPlayerID())
{
	?><b><font face='Arial' color='#FF0000'>Ce joueur n'est pas associé à ce round.</font></b><br><br><?php
	include ('footer.inc.php');
	exit;
}

//Ranking Objects
$o_general_rank = new GeneralRanking();
$o_mensual_rank = new Ranking();

//Set Round Object
$o_round = new Round();
$o_round->SetRoundID($o_player_round->GetPlayerRoundID());

//Set Song object
$o_song = new Song();
$o_song->SetSongID($o_round->GetRoundSong());
$a_song_name = $o_song->GetSongName();

//Get round data
$a_round_data = $o_player_round->GetPlayerRoundStats();
$a_percent=array("marvelous"=>0,"perfect"=>0,"great"=>0,"good"=>0,"boo"=>0,"miss"=>0,"ok"=>0,"ng"=>0,"amine"=>0,"hmine"=>0);
$a_dance_points = $o_player_round->GetPlayerRoundDancePoints();

//Calculate percent
if($a_round_data["total_steps"]>0)
{
	$a_percent["marvelous"]=round(($a_round_data["marvelous"]*100)/$a_round_data["total_steps"],2);
	$a_percent["perfect"]=round(($a_round_data["perfect"]*100)/$a_round_data["total_steps"],2);
	$a_percent["great"]=round(($a_round_data["great"]*100)/$a_round_data["total_steps"],2);
	$a_percent["good"]=round(($a_round_data["good"]*100)/$a_round_data["total_steps"],2);
	$a_percent["miss"]=round(($a_round_data["miss"]*100)/$a_round_data["total_steps"],2);
	$a_percent["boo"]=round(($a_round_data["boo"]*100)/$a_round_data["total_steps"],2);
}
if($a_round_data["total_freezes"]>0)
{
	$a_percent["ng"]=round(($a_round_data["ng"]*100)/$a_round_data["total_freezes"],2);
	$a_percent["ok"]=round(($a_round_data["ok"]*100)/$a_round_data["total_freezes"],2);
}
if($a_round_data["total_mines"]>0)
{
	$a_percent["amine"]=round(($a_round_data["amine"]*100)/$a_round_data["total_mines"],2);
	$a_percent["hmine"]=round(($a_round_data["hmine"]*100)/$a_round_data["total_mines"],2);
}
		
?>
					<center><br/><b><font size=+1 face='Arial'>Statistiques du joueur <?php echo $o_actual_player->GetPlayerName();?> :</font></b><br/>
					<hr>
					Classement Mensuel Actuel : <?php echo $o_mensual_rank->GetPlayerRank($o_player_round->GetPlayerID()); ?><br/>
					Classement Général : <?php echo $o_general_rank->GetPlayerGeneralRank($o_player_round->GetPlayerID()); ?><br/><hr/><br/>
					<b><font face='Arial'>Round #<?php echo $o_player_round->GetPlayerRoundID();?></font></b><br/><br/>
					<table width="734" border="0" cellspacing="3">
					<tr align="center">
					  <td colspan="4">
					  	<table border="0" width="100%">
							<tr align="center">
								<td width="50%"><img src="images/Grade<?php echo $o_player_round->GetPlayerRoundGrade();?>.png" /></td><td><td><FONT FACE="Arial"><I><FONT SIZE=+3><?php echo $a_song_name["title"];?></FONT><BR><FONT SIZE=+1><?php echo $a_song_name["subtitle"];?></FONT></I></FONT></td>
							</tr>
						</table>
					  </td>
					</tr>
					<tr align="center">
					  <td colspan="4"><strong>Options : <?php echo $o_player_round->GetPlayerRoundOptions();?></strong></td>
					 </tr>
					<tr align="center">
					  <td colspan="4"><strong>Dance Points : <?php echo $a_dance_points["obtained"];?> / <?php echo $a_dance_points["maximum"];?> (<?php echo $a_dance_points["percent"];?>%)</strong></td>
					 </tr>
					<tr align="center">
					  <td colspan="4"><strong>Nom de la salle : <?php echo $o_round->GetRoundRoomName();?></strong></td>
					 </tr>
<?php
$b_first_line=true;
foreach($a_percent as $s_step => $i_value)
{
	?>
					<tr>
					  <td width="40%" align="right">(<?php echo $a_percent[$s_step];?>%) <img src="images/pourcent.jpg" height="15" width="<?php echo ($a_percent[$s_step]*2);?>" /></td>
					  <td width="5%"><?php echo $a_round_data[$s_step];?></td>
					  <td width="15%"><div align="center"><?php echo ${"s_text_step_".$s_step};?></div></td>
					  
					  <?php //Round general infos
					  if($b_first_line==true) { ?>
					  <td width="40%" rowspan="10" align="center" valign="middle">
					  	<img src="images/Diff<?php echo $o_player_round->GetPlayerRoundDifficulty();?>.png" /><br /><br />
					  	<?php for($i=0;$i<$o_player_round->GetPlayerRoundFeet();$i++) { ?>
					  	<img src="images/Foot.png" />
					  	<?php } ?>
					  	<br /><br />
					  	<?php echo date("d/m/Y H:i:s",$o_round->GetRoundPlayTime()-3600);?>
					  </td>
					  <?php $b_first_line=false; } ?>
					</tr>
	<?php
}
?>
					<tr>
					  <td width="40%"><div align="center">Max Combo : <?php echo $o_player_round->GetPlayerRoundMaxCombo();?></div></td>
					  <td colspan="2"><div align="center">Toasty : <?php echo $o_player_round->GetPlayerRoundToasty();?></div></td>
					  <td width="40%"><div align="center">Score : <?php echo $o_player_round->GetPlayerRoundScore();?></div></td>
					</tr>
					</table>
<?php
/////////////////////////////////
// Last Round Opponents + Rank //
/////////////////////////////////
?>
					<br/>
					<b><font size=+1 face='Arial'>Adversaires lors du round #<?php echo $o_round->GetRoundID();?> :</font></b>
					<table width='734' border='1' cellspacing='0' cellpadding='0'>
						<tr>
							<td width="20%" bgcolor="#EEEEEE"><div align="center"><strong>Place</strong></div></td>
						    <td width="20%" bgcolor="#EEEEEE"><div align="center"><strong>Joueur</strong></div></td>
						    <td width="20%" bgcolor="#EEEEEE"><div align="center"><strong>Difficult&eacute;</strong></div></td>
						    <td width="20%" bgcolor="#EEEEEE"><div align="center"><strong>Grade</strong></div></td>
						</tr>
						<?php
						$a_players_round_key=$o_round->GetPlayersRoundKey();
						$o_temp_player_round = new PlayerRound();
						$o_temp_player = new PlayerAccount();
						foreach($a_players_round_key as $i_player_round_key)
						{
							$o_temp_player_round->SetRoundKey($i_player_round_key);
							$o_temp_player->SetPlayerID($o_temp_player_round->GetPlayerID());
							$a_players_list[]=array("score" => $o_temp_player_round->GetPlayerRoundScore(), "difficulty" => $o_temp_player_round->GetPlayerRoundDifficulty(), "grade" => $o_temp_player_round->GetPlayerRoundGrade(), "name" => $o_temp_player->GetPlayerName(), "pid" => $o_temp_player_round->GetPlayerID(), "round_key" => $i_player_round_key);
						}
						usort($a_players_list,"score_compare");
						foreach($a_players_list as $i_order => $a_player_data)
						{
							?>
						<tr>
							<td><div align="center"><strong><?php echo ($i_order+1);?></strong></div></td>
						    <td><div align="center"><strong><a href="player-stats.php?pid=<?php echo $a_player_data["pid"];?>&round_key=<?php echo $a_player_data["round_key"];?>"><?php echo $a_player_data["name"];?></a></strong></div></td>
						    <td><div align="center"><strong><img src="images/Diff<?php echo $a_player_data["difficulty"];?>.png" width="30" height="25" /></strong></div></td>
						    <td><div align="center"><strong><img src="images/Grade<?php echo $a_player_data["grade"];?>.png" height="25" /></strong></div></td>
						</tr>
							<?php
						}
						?>
					</table>
					<br><br>
<?php
///////////////////
// Average Infos //
///////////////////

//Set PlayerTotal object
$o_player_total = new PlayerTotal(); 
$o_player_total->SetPlayerId($o_player_round->GetPlayerID());

//Get round data
$a_total_data = $o_player_total->GetPlayerTotalStats();
$a_total_percent=array("marvelous"=>0,"perfect"=>0,"good"=>0,"miss"=>0,"boo"=>0,"great"=>0,"ok"=>0,"ng"=>0,"amine"=>0,"hmine"=>0);
$a_total_dance_points = $o_player_total->GetPlayerTotalDancePoints();

//Calculate percent
if($a_total_data["total_steps"]>0)
{
	$a_total_percent["marvelous"]=round(($a_total_data["marvelous"]*100)/$a_total_data["total_steps"],2);
	$a_total_percent["perfect"]=round(($a_total_data["perfect"]*100)/$a_total_data["total_steps"],2);
	$a_total_percent["good"]=round(($a_total_data["good"]*100)/$a_total_data["total_steps"],2);
	$a_total_percent["miss"]=round(($a_total_data["miss"]*100)/$a_total_data["total_steps"],2);
	$a_total_percent["boo"]=round(($a_total_data["boo"]*100)/$a_total_data["total_steps"],2);
	$a_total_percent["great"]=round(($a_total_data["great"]*100)/$a_total_data["total_steps"],2);
	
}
if($a_total_data["total_freezes"]>0)
{
	$a_total_percent["ng"]=round(($a_total_data["ng"]*100)/$a_total_data["total_freezes"],2);
	$a_total_percent["ok"]=round(($a_total_data["ok"]*100)/$a_total_data["total_freezes"],2);
}
if($a_total_data["total_mines"]>0)
{
	$a_total_percent["amine"]=round(($a_total_data["amine"]*100)/$a_total_data["total_mines"],2);
	$a_total_percent["hmine"]=round(($a_total_data["hmine"]*100)/$a_total_data["total_mines"],2);
}
?>
					<hr>
					<table border='0'>
						<tr>
							<td width='50%' align='center'>
								<b><font size=+1 face='Arial'>Moyenne du mois du joueur <?php echo $o_actual_player->GetPlayerName();?> :</font></b><br/>
								(Moyenne des <?php echo $o_player_total->GetPlayerTotalRounds();?> musiques jouées)<br/><br/>
								<table width="734" border="0" cellspacing="3">
<?php
$b_first_line=true;
foreach($a_total_percent as $s_step => $i_value)
{
	?>
								<tr>
								  <td width="40%" align="right">(<?php echo $a_total_percent[$s_step];?>%) <img src="images/pourcent.jpg" height="15" width="<?php echo ($a_total_percent[$s_step]*2);?>" /></td>
								  <td width="5%"><?php echo $a_total_data[$s_step];?></td>
								  <td width="15%"><div align="center"><?php echo ${"s_text_step_".$s_step};?></div></td>
								  
								  <?php //Round general infos
								  if($b_first_line==true) { ?>
								  <td width="40%" rowspan="10" align="center" valign="middle">
								  	<img src="images/Diff<?php echo $o_player_total->GetPlayerTotalDifficulty();?>.png" /><br /><br />
								  	<?php for($i=0;$i<$o_player_total->GetPlayerTotalFeet();$i++) { ?>
								  	<img src="images/Foot.png" />
								  	<?php } ?>
								  </td>
								  <?php $b_first_line=false; } ?>
								</tr>
				<?php
			}
			?>
								<tr>
								  <td width="40%"><div align="center">Max Combo : <?php echo $o_player_total->GetPlayerTotalMaxCombo();?></div></td>
								  <td colspan="2"><div align="center">Toasty : <?php echo $o_player_total->GetPlayerTotalToasty();?> (Total : <?php echo $o_player_total->GetPlayerTotalToasty(false);?>)</div></td>
								  <td width="40%"><div align="center">Score : <?php echo $o_player_total->GetPlayerTotalScore();?></div></td>
								</tr>
								</table>
							</td>
							<td>
								<b><font size=+1 face='Arial'>Les 15 dernières musique du joueur :</font></b>
								<ul>
								
<?php
$a_last_rounds=$o_player_round->GetPlayerRound($o_actual_player->GetPlayerID(),1,15);
foreach($a_last_rounds as $i_round)
{
	$o_player_round->SetRoundKey($i_round);
	$o_round->SetRoundID($o_player_round->GetPlayerRoundID());
	$o_song->SetSongID($o_round->GetRoundSong());
	$a_song_name=$o_song->GetSongName();
	?><li><a href="player-stats.php?pid=<?php echo $o_actual_player->GetPlayerID();?>&round_key=<?php echo $i_round;?>"><?php echo $a_song_name["title"];?> (<img src="images/Grade<?php echo $o_player_round->GetPlayerRoundGrade();?>.png" height="15" />)</a></li><?php
}
?>
								</ul>
								(<a href="player-music.php?pid=<?php echo $o_actual_player->GetPlayerID();?>">Liste complète</a>)
							</td>
						</tr>
					</table>
					</center>
<?php
include ('footer.inc.php');
?>