<?php
include("connectServer.php");
session_start();

if (isset($_GET['done']) && isset($_GET['time']) && isset($_GET['country'])){
	$piecesCountry = explode("_", $_GET['country']);
	$piecesDone = explode("_", $_GET['done']);
	$piecesTime = explode("_", $_GET['time']);
	
	if (count($piecesCountry) != count($piecesDone) || count($piecesCountry) != count($piecesTime)) {
		echo "missing pieces";
	}
	else {
		$_SESSION['score'] = 0;
		$_SESSION['total'] = 0;
		$_SESSION['time'] = 0;
		for ($i = 0; $i < count($piecesCountry); $i++) {
			$req = $bdd->prepare("INSERT INTO gData (COUNTRY, DONE, TIME) VALUES (" . $piecesCountry[$i] . ", " . $piecesDone[$i] . ", " . $piecesTime[$i] . ")");
			$req->execute();
			
			$_SESSION['total'] ++;
			if ($piecesDone[$i] == "1") {
				$_SESSION['score'] ++;
				$_SESSION['time'] += $piecesTime[$i];
			}
		}
		
		if ($_SESSION['time'] != 0) {
			$_SESSION['time'] = "Average response time : " . intval($_SESSION['time'] / $_SESSION['total']) . " s";
		}
		else {
			$_SESSION['time'] = "Looks like you got no right answer ... <br> Maybe next time !";
		}
	}
	
	//echo "<script>window.location.replace('G1.php')</script>";
	
    header('Location: finalG1.php', true, 302);
	exit;
}
else {
	echo "FAIL";
}
?>