<commit_before><?php
	
	$champsFolder = PATH."img/champions/";

	$Iname = $_GET["name"];
	$Iid = $_GET/*27*/["id"];
	$Iregion = $_GET["region"];
	
	/* If we have recieved valid arguments */
	if ( (isset($Iname) OR isset($Iid)) AND isset($Iregion)) {
		$region = strtolower($Iregion); // region is always lowercase.
		$requestString = array(); // array of all String of requests we'll make.
		$result = array(); // array of all results of requests we'll malke.
		
		// First request is to select the right user
		$requestString[0] = "SELECT id, user FROM users WHERE ";
		$conditions = array ("region" => $region);
		
		// If the id is provided, go for it
		if (isset($Iid)) {
			$conditions["id"] = $Iid;
			$id = $Iid;
			$requestString[0] .= conditions($conditions); 
			$findSummoner = $pdo->prepare($requestString[0]);
			$findSummoner->bindParam(":id", $Iid);
		// Else, look for the username
		} else {
			$conditions["user"] = $Iname;
			$name = $Iname;
			$requestString[0] .= conditions($conditions);
			$findSummoner = $pdo->prepare($requestString[0]);
			$findSummoner->bindParam(":user", $Iname);
		}
		$findSummoner->bindParam(":region", $region);
		
		//   START Debug
		if (isset($_GET['debug'])) {
			echo "REQUEST: ".$requestString[0];
		} // END Debug
	
		$findSummoner->execute();        // Execute the request
		$foundSummoner = $findSummoner->fetch(); // Get the array
		
		// If there is an user
		if (count($foundSummoner) > 0) {
			//   START Debug
			if (isset($_GET['debug'])) {
				echo "<br>ARRAY:<pre>";
				print_r($foundSummoner);
				echo "</pre>";
			} // END Debug
			$id = $foundSummoner['id'];
			$name = $foundSummoner['user'];
			
			$filtersStr = array(); // sexy String of all filters conditions
			
			// START FILTERS
			
			$filters = array( // Array of activated filters
						'fChampion' => false,
						'fMode' => false
			);
			
			// filter games by Champion
			if (isset($_GET['fChampion']) AND $_GET['fChampion'] != '') {
				$filters['fChampion'] = $_GET['fChampion'];
				$championFilterStr = " AND players.championId = :championId";
				$filtersStr[] = $championFilterStr;
			}
			
			// filter games by Game Mode
			if (isset($_GET['fMode']) AND $_GET['fMode'] != '') {
				$filters['fMode'] = $_GET['fMode'];
				$modeFilterStr = " AND games.subType = :typeStr";
				$filtersStr[] = $modeFilterStr;
			}
			
			$conditions = "
			FROM games 
			LEFT JOIN players ON games.gameId = players.gameId
			LEFT JOIN data ON games.gameId = data.gameId AND players.gameId = data.gameId
			WHERE players.summonerId = :sId AND data.summonerId = :sId".implode($filtersStr);
			
			$statsString = "SELECT count(*) AS nbGames, avg(data.championsKilled) AS k, avg(data.numDeaths) AS d, avg(data.assists) AS a,
			avg(data.minionsKilled) AS minions, avg(data.goldEarned) AS gold, avg(data.timePlayed) AS duration".$conditions;
			
			$wonGamesString = "SELECT count(*) AS nb".$conditions." AND data.win = (1);";
			
			$requestString[1] = "SELECT * ".$conditions." ORDER BY `games`.`createDate` DESC;";

			if (isset($_GET['debug'])) {
				echo $requestString[1];
			}
			// New request : All games of this user
			$summonerGames = $pdo->prepare($requestString[1]);
			$summonerGames->bindParam(":sId", $id);
			// Stats requests
			$stats = $pdo->prepare($statsString);
			$stats->bindParam(":sId", $id);
			$wonGames = $pdo->prepare($wonGamesString);
			$wonGames->bindParam(":sId", $id);
			
			// Check if each filter is activated
			
			// filter games by Champion
			if ($filters['fChampion']) {
				$summonerGames->bindParam(":championId", intval($filters['fChampion']));
				$stats->bindParam(":championId", intval($filters['fChampion']));
				$wonGames->bindParam(":championId", intval($filters['fChampion']));
			}
			// filter games by Mode
			if ($filters['fMode']) {
				$summonerGames->bindParam(":typeStr", $filters['fMode']);
				$stats->bindParam(":typeStr", $filters['fMode']);
				$wonGames->bindParam(":typeStr", $filters['fMode']);
			}
			
			$summonerGames->execute();        // Execute the request
			$stats->execute();        // Execute the request
			$wonGames->execute();        // Execute the request
			//$summonerGames->fetch(); // Get the array
		}
	}
	
	// We want infos about all champions. This request will never change
	$requestString[2] = "SELECT * FROM champions ORDER BY name ASC;";
	$championsRequest = $pdo->prepare($requestString[2]);
	$championsRequest->execute(); // Execute the request
	
	$champsId = array();
	$champsDisplay = array();
	while ($champ = $championsRequest->fetch()) {
		$champsId[$champ['id']] = $champ['id'];
		$champsDisplay[$champ['id']] = $champ['display'];
		$champsName[$champ['id']] = $champ['name'];
	}
?>
<div class="row">
	<div class="span12">
		<div class="well">
			<?php 
			if (isset($rName[$region])) {
				$regionTxt = $rName[$region];} else {$regionTxt = $region; } ?>
			<h2><?php echo htmlentities(utf8_decode($name));?> <a href="http://www.lolking.net/summoner/<? echo $region."/".$id; ?>"><img src="<?php echo PATH;?>img/lolking.png" alt="lolking"></a></h2>
			<?php echo htmlentities($regionTxt);?>
			<br><?php echo htmlentities($id);?>
		</div>
	</div>
</div>

<div class="row">
	<div class="span12">
		<div class="well">
			<legend>Statistics</legend>
			<?php
			$finalStats = $stats->fetch();
			$nbWon = $wonGames->fetch();
			echo $nbWon['nb']." wins / ".$finalStats['nbGames']." games (".round($nbWon['nb']/$finalStats['nbGames']*100, 2)."% win)";
			echo "<br>Average KDA; ".round($finalStats['k'], 1)." / ".round($finalStats['d'], 1)." / ".round($finalStats['a'], 1);
			echo "<br>Rate; ".round(($finalStats['k']+$finalStats['a'])/$finalStats['d'], 2)." : 1";
			?>
		</div>
	</div>
</div>

<div class="row">
	<div class="span12">
		<form class="form-horizontal well" action="index.php" method="get">
			<fieldset>
				<legend>Filter games</legend>
				<input type="hidden" name="page" value="player"/>
				<input type="hidden" name="region" value="<?php echo $Iregion?>"/>
				<input type="hidden" name="name" value="<?php echo $Iname?>"/> 
				<div class="control-group">
					<label class="control-label"><commit_msg>Correcting obvious warnings<commit_after><?php
	
	$champsFolder = PATH."img/champions/";

	if (isset($_GET["name"])) { $Iname = $_GET["name"];}
	if (isset($_GET["id"])) { $Iid = $_GET/*27*/["id"];}
	if (isset($_GET["region"])) { $Iregion = $_GET["region"];}
	
	/* If we have recieved valid arguments */
	if ( (isset($Iname) OR isset($Iid)) AND isset($Iregion)) {
		$region = strtolower($Iregion); // region is always lowercase.
		$requestString = array(); // array of all String of requests we'll make.
		$result = array(); // array of all results of requests we'll malke.
		
		// First request is to select the right user
		$requestString[0] = "SELECT id, user FROM users WHERE ";
		$conditions = array ("region" => $region);
		
		// If the id is provided, go for it
		if (isset($Iid)) {
			$conditions["id"] = $Iid;
			$id = $Iid;
			$requestString[0] .= conditions($conditions); 
			$findSummoner = $pdo->prepare($requestString[0]);
			$findSummoner->bindParam(":id", $Iid);
		// Else, look for the username
		} else {
			$conditions["user"] = $Iname;
			$name = $Iname;
			$requestString[0] .= conditions($conditions);
			$findSummoner = $pdo->prepare($requestString[0]);
			$findSummoner->bindParam(":user", $Iname);
		}
		$findSummoner->bindParam(":region", $region);
		
		//   START Debug
		if (isset($_GET['debug'])) {
			echo "REQUEST: ".$requestString[0];
		} // END Debug
	
		$findSummoner->execute();        // Execute the request
		$foundSummoner = $findSummoner->fetch(); // Get the array
		
		// If there is an user
		if (count($foundSummoner) > 0) {
			//   START Debug
			if (isset($_GET['debug'])) {
				echo "<br>ARRAY:<pre>";
				print_r($foundSummoner);
				echo "</pre>";
			} // END Debug
			$id = $foundSummoner['id'];
			$name = $foundSummoner['user'];
			
			$filtersStr = array(); // sexy String of all filters conditions
			
			// START FILTERS
			
			$filters = array( // Array of activated filters
						'fChampion' => false,
						'fMode' => false
			);
			
			// filter games by Champion
			if (isset($_GET['fChampion']) AND $_GET['fChampion'] != '') {
				$filters['fChampion'] = $_GET['fChampion'];
				$championFilterStr = " AND players.championId = :championId";
				$filtersStr[] = $championFilterStr;
			}
			
			// filter games by Game Mode
			if (isset($_GET['fMode']) AND $_GET['fMode'] != '') {
				$filters['fMode'] = $_GET['fMode'];
				$modeFilterStr = " AND games.subType = :typeStr";
				$filtersStr[] = $modeFilterStr;
			}
			
			$conditions = "
			FROM games 
			LEFT JOIN players ON games.gameId = players.gameId
			LEFT JOIN data ON games.gameId = data.gameId AND players.gameId = data.gameId
			WHERE players.summonerId = :sId AND data.summonerId = :sId".implode($filtersStr);
			
			$statsString = "SELECT count(*) AS nbGames, avg(data.championsKilled) AS k, avg(data.numDeaths) AS d, avg(data.assists) AS a,
			avg(data.minionsKilled) AS minions, avg(data.goldEarned) AS gold, avg(data.timePlayed) AS duration".$conditions;
			
			$wonGamesString = "SELECT count(*) AS nb".$conditions." AND data.win = (1);";
			
			$requestString[1] = "SELECT * ".$conditions." ORDER BY `games`.`createDate` DESC;";

			if (isset($_GET['debug'])) {
				echo $requestString[1];
			}
			// New request : All games of this user
			$summonerGames = $pdo->prepare($requestString[1]);
			$summonerGames->bindParam(":sId", $id);
			// Stats requests
			$stats = $pdo->prepare($statsString);
			$stats->bindParam(":sId", $id);
			$wonGames = $pdo->prepare($wonGamesString);
			$wonGames->bindParam(":sId", $id);
			
			// Check if each filter is activated
			
			// filter games by Champion
			if ($filters['fChampion']) {
				$summonerGames->bindParam(":championId", intval($filters['fChampion']));
				$stats->bindParam(":championId", intval($filters['fChampion']));
				$wonGames->bindParam(":championId", intval($filters['fChampion']));
			}
			// filter games by Mode
			if ($filters['fMode']) {
				$summonerGames->bindParam(":typeStr", $filters['fMode']);
				$stats->bindParam(":typeStr", $filters['fMode']);
				$wonGames->bindParam(":typeStr", $filters['fMode']);
			}
			
			$summonerGames->execute();        // Execute the request
			$stats->execute();        // Execute the request
			$wonGames->execute();        // Execute the request
			//$summonerGames->fetch(); // Get the array
		}
	}
	
	// We want infos about all champions. This request will never change
	$requestString[2] = "SELECT * FROM champions ORDER BY name ASC;";
	$championsRequest = $pdo->prepare($requestString[2]);
	$championsRequest->execute(); // Execute the request
	
	$champsId = array();
	$champsDisplay = array();
	while ($champ = $championsRequest->fetch()) {
		$champsId[$champ['id']] = $champ['id'];
		$champsDisplay[$champ['id']] = $champ['display'];
		$champsName[$champ['id']] = $champ['name'];
	}
?>
<div class="row">
	<div class="span12">
		<div class="well">
			<?php 
			if (isset($rName[$region])) {
				$regionTxt = $rName[$region];} else {$regionTxt = $region; } ?>
			<h2><?php echo htmlentities(utf8_decode($name));?> <a href="http://www.lolking.net/summoner/<? echo $region."/".$id; ?>"><img src="<?php echo PATH;?>img/lolking.png" alt="lolking"></a></h2>
			<?php echo htmlentities($regionTxt);?>
			<br><?php echo htmlentities($id);?>
		</div>
	</div>
</div>

<div class="row">
	<div class="span12">
		<div class="well">
			<legend>Statistics</legend>
			<?php
			$finalStats = $stats->fetch();
			$nbWon = $wonGames->fetch();
			echo $nbWon['nb']." wins / ".$finalStats['nbGames']." games (".round($nbWon['nb']/$finalStats['nbGames']*100, 2)."% win)";
			echo "<br>Average KDA; ".round($finalStats['k'], 1)." / ".round($finalStats['d'], 1)." / ".round($finalStats['a'], 1);
			echo "<br>Rate; ".round(($finalStats['k']+$finalStats['a'])/$finalStats['d'], 2)." : 1";
			?>
		</div>
	</div>
</div>

<div class="row">
	<div class="span12">
		<form class="form-horizontal well" action="index.php" method="get">
			<fieldset>
				<legend>Filter games</legend>
				<input type="hidden" name="page" value="player"/>
				<input type="hidden" name="region" value="<?php echo $region?>"/>
				<input type="hidden" name="name" value="<?php echo $name?>"/>
				<input type="hidden" name="id" value="<?php echo $id?>"/> 
				<div class="control-group">
					<label class="control-label">
{'fraction_non_alphanumeric': 0.20425190533493784, 'fraction_numerical': 0.00529482551143201, 'mean_word_length': 3.822437137330754, 'pattern_counts': {'":': 24, '<': 97, '<?xml version=': 0, '>': 161, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2832142', 'n_tokens_mistral': 4976, 'n_tokens_neox': 4334, 'n_words': 959}