<?
include("top.php");

function stats($game, $player) {
    include("connectServer.php");
    
    if ($player == 0){
        $playerReq = '';
    }
    else {
        $playerReq = ' AND player = ' . $player;
    }
    
    $req = $bdd->prepare('SELECT COUNT(*) AS C FROM gData  WHERE game = ' . $game . $playerReq);
	$req->execute();
	$data = $req->fetch();
    
    if ($player == 0){
        $sadMessage = "<br> No data for this game as no one played it yet =(";
    }
    else {
        $sadMessage = "<br> No data for this game as you have not played it yet =(";
    }
    
    $message = "";
    
    if ($data['C'] == 0) {
        return $sadMessage;
    }
    
    $count = $data['C'];
    
    $message = $message . "<br> Number of times played : " . $data['C'];
    
    $req = $bdd->prepare('SELECT AVG(time) AS A FROM gData WHERE game = ' . $game . $playerReq);
    $req->execute();
    $data = $req->fetch();
    
    if (empty($data['A'])) {
        return $sadMessage;
    }
    $message = $message . "<br> Average time : " . intval($data['A']) . "s";
    
    $req = $bdd->prepare('SELECT COUNT(*) AS C FROM gData  WHERE game = ' . $game . ' AND done = 1' . $playerReq);
	$req->execute();
	$data = $req->fetch();
    
    if ($data['C'] == 0) {
        return $sadMessage;
    }
    $message = $message . "<br> Success rate : " . intval($data['C'] * 100 / $count) . "%";
    
    $req = $bdd->prepare('SELECT country AS C FROM gData WHERE game = ' . $game . $playerReq . ' GROUP BY country ORDER BY AVG(time) DESC LIMIT 1');
	$req->execute();
	$data = $req->fetch();
    $hardest = $data['C'];
    $hardestMessage = "";
    
    $req = $bdd->prepare('SELECT country AS C FROM gData WHERE game = ' . $game . $playerReq . ' GROUP BY country ORDER BY AVG(time) LIMIT 1');
	$req->execute();
	$data = $req->fetch();
    $easiest = $data['C'];
    $easiestMessage = "";
    
    $count = 0;
    $file = fopen("data.data", "r") or die("Unable to open file!");
    while(!feof($file)) {
        
        $count ++;

        $str = fgets($file);
        $str = trim(preg_replace('/\s\s+/', ' ', $str));
        $pieces = explode(" ", $str);
        
        $x = floatval(str_replace(",", ".", $pieces[0]));
        $y = floatval(str_replace(",", ".", $pieces[1]));
        $shortName = $pieces[2];
        
        if ($count-1 == $hardest) {
            $hardestMessage = "<br> Hardest country : " . str_replace("_", " ", $pieces[3]);
        }
        if ($count-1 == $easiest) {
            $easiestMessage = "<br> Easiest country : " . str_replace("_", " ", $pieces[3]);
        }
    }
    
    $message = $message . $easiestMessage . $hardestMessage;
    
    return $message;
}
?>

<body>
<div id="interface">
	<br>
	<br>
	<br>
	<br>
	<p class="bigText"> Stats for GeoQuiz </p>
    <br>   
    <p class="uText">Missing country :</p>
    <br>
    <p> Global stats :<? echo stats(1, 0); ?></p>
    <br>
    <? if ($_SESSION['id'] != 0) { ?> <p> <? echo $_SESSION['name']; ?>'s stats :<? echo stats(1, $_SESSION['id']); ?></p> <br><? } ?>
    <p class="uText">Country shape :</p>
    <br>
    <p> Global stats :<? echo stats(2, 0); ?></p>
    <br>
    <? if ($_SESSION['id'] != 0) { ?> <p> <? echo $_SESSION['name']; ?>'s stats :<? echo stats(2, $_SESSION['id']); ?></p> <br><? } ?>
    <p class="uText">Missing country on reversed Earth :</p>
    <br>
    <p> Global stats :<? echo stats(3, 0); ?></p>
    <br>
    <? if ($_SESSION['id'] != 0) { ?> <p> <? echo $_SESSION['name']; ?>'s stats :<? echo stats(3, $_SESSION['id']); ?></p> <br><? } ?>
    <p class="uText">Rotated country shape :</p>
    <br>
    <p> Global stats :<? echo stats(4, 0); ?></p>
    <br>
    <? if ($_SESSION['id'] != 0) { ?> <p> <? echo $_SESSION['name']; ?>'s stats :<? echo stats(4, $_SESSION['id']); ?></p> <br><? } ?>
    <br>
	<button class="niceButton menuButton" onclick="location.href='http://feudal-ambitions.com/GeoQuiz/'">Go back to main menu</button>
</div>
</body>
</html>