<?php
	$hostname = 'localhost';
	$username = 'narKampen';
	$pass = 'IfRLzj2HJBXA9eei';
	$database = 'narKampen';
	
	$conn = new mysqli($hostname, $username, $pass, $database);
	if (!$conn) {
		die("Connection Failed. ". mysqli_connect_error());
	}
	mysqli_set_charset($conn,'utf8');
	
	$gameId = $conn->real_escape_string($_POST['gameId']);
	$callFunction = $conn->real_escape_string($_POST['f']);
	
	if ($callFunction === "GetGameInfo") {
		$sql = "SELECT username, status FROM gamePlayers INNER JOIN users ON users.id = playerId WHERE gameId = $gameId";
		$result = $conn->query($sql);


		if ($result->num_rows > 0) {
			$returnArray = array();
			$i = 0;
			while ($data = $result->fetch_assoc()) {
				foreach ($data as $key => $value) {
					$returnArray[$i][$key] = $value;
				}
				$i++;
			}
			echo json_encode($returnArray);
			
		} else {
			echo "No games found for user";
		}
	} else if ($callFunction === "PlayerQuestions") {
		$userName = $conn->real_escape_string($_POST['userName']);
		$sql = "SELECT questions.id as id, question, answer, categoryId as category, r,g,b,a FROM questions 
					INNER JOIN questionToCategory on questions.id = questionToCategory.questionId 
					INNER JOIN category ON category.id = questionToCategory.categoryId 
					WHERE questions.id IN 
						(SELECT questionId FROM usersLockedQuestions 
							WHERE 
						gameId = $gameId AND playerName = '$userName') ORDER BY answer ASC";
		$result = $conn->query($sql);
		$returnArray = array();
		$i = 0;
		while ($data = $result->fetch_assoc()) {
			foreach ($data as $key => $value) {
				$returnArray[$i][$key] = $value;
			}
			$i++;
		}
		echo json_encode($returnArray);
	}
	$conn->close();
	
?>