function check_level($advance)
{
    global $CONST;
    if (!$_SESSION["advance-level"] || $advance != $_SESSION["prev-salt"] || $_SESSION["prev-salt"] === "") {
        return NULL;
    }
    global $db_connection;
    $query = "SELECT COUNT(*) FROM `Questions-{$_SESSION["username"]}` AS `Q-U` " . "WHERE `Q-U`.`Question Number` LIKE '{$_SESSION["level"]}_'; ";
    $query = mysqli_fetch_array(mysqli_query($db_connection, $query));
    if (intval($query["COUNT(*)"]) < 6) {
        return NULL;
    }
    $query = "SELECT COUNT(*) FROM `Questions-{$_SESSION["username"]}` AS `Q-U` " . "WHERE `Q-U`.`Time Opened` != '-1' AND `Q-U`.`Time Answered` = '-1' " . "AND `Q-U`.`Question Number` LIKE '{$_SESSION["level"]}_'; ";
    $query = mysqli_fetch_array(mysqli_query($db_connection, $query));
    $query_count = intval($query["COUNT(*)"]);
    if ($query_count > 0) {
        push_increase("Unsolved Question Penalty", -$query_count * $CONST["question-penalty"]);
    }
    $_SESSION["question"] = "";
    $_SESSION["advance-level"] = FALSE;
    $_SESSION["level"]++;
    $_SESSION["level-score"] = 0;
    load_constants();
    push_increase("Level Advanced", $CONST["advance-bonus"]);
    sync_scores();
    $query = "UPDATE `ContestantsData` " . "SET `Level` = '{$_SESSION["level"]}' " . "WHERE `Username` = '{$_SESSION["username"]}';";
    mysqli_query($db_connection, $query);
    return "Level Advanced";
}
function check_answer($ans)
{
    $quesFor = $_SESSION["question"];
    $browserOfuser = NULL;
    global $db_connection;
    global $CONST;
    $query = "SELECT `Q`.*,`Q-U`.* FROM `Questions` AS `Q` " . "LEFT JOIN `Questions-{$_SESSION["username"]}` AS `Q-U` ON `Q`.`Question ID` = `Q-U`.`Question ID` " . "LEFT JOIN `QuestionSolves` AS `S` ON `Q`.`Question ID` = `S`.`Question ID` " . "WHERE `Q`.`Question ID` = '{$_SESSION["question"]}'";
    $query = mysqli_fetch_array(mysqli_query($db_connection, $query));
    /////////////////////////
    if ($query["Time Answered"] != "-1") {
        $_SESSION["question"] = "";
        mysqli_close($db_connection);
        header("Location: ./profile.php");
        die;
    }
    ////////////////////////////////
    $query["Attempts"]++;
    $query["Check Answer"] = $query["Answer Regular"];
    if (!filter_var($ans, FILTER_VALIDATE_REGEXP, array("options" => array('regexp' => '/^[a-z0-9]+$/')))) {
        return "Ooops! Wrong Answer! Keep Trying...";
    }
    if ($query["Check Answer"] != $ans) {
        $result = "UPDATE `Questions-{$_SESSION["username"]}` " . "SET `Attempts` = '{$query["Attempts"]}' " . "WHERE `Question ID` = '{$_SESSION["question"]}' ";
        mysqli_query($db_connection, $result);
        return "Ooops! Wrong Answer! Keep Trying...";
    }
    $timeAnsw = intval((time() + 59) / 60);
    $incr = intval($CONST["question-score"]);
    push_increase("Question Answered", $incr);
    if ($_SESSION["advance-level"]) {
        push_increase("Bonus Question", $CONST["bonus-quest"]);
        $incr += $CONST["bonus-quest"];
    }
    sync_scores();
    $result = "UPDATE `Questions-{$_SESSION["username"]}` " . "SET `Time Answered`='{$timeAnsw}', `Obtained Score`='{$incr}', `Attempts`='{$query["Attempts"]}' " . "WHERE `Question ID` = '{$_SESSION["question"]}';";
    mysqli_query($db_connection, $result);
    $query = "SELECT COUNT(*) FROM `Questions-{$_SESSION["username"]}` AS `Q-U` " . "WHERE `Q-U`.`Question Number` LIKE '{$_SESSION["level"]}_'" . "AND `Q-U`.`Time Answered` != '-1' ";
    $query = mysqli_fetch_array(mysqli_query($db_connection, $query));
    if (intval($query["COUNT(*)"]) >= $CONST["advance"]) {
        $_SESSION["advance-level"] = TRUE;
    }
    $query = "SELECT * FROM `QuestionSolves` AS `Q-U` " . "WHERE `Q-U`.`Question ID` = '{$_SESSION["question"]}'";
    $query = mysqli_fetch_array(mysqli_query($db_connection, $query));
    $query["Solves"]++;
    if ($query["First Solve"] == -1) {
        $query["First Solve"] = $timeAnsw;
    }
    $result = "UPDATE `QuestionSolves` " . "SET `Solves` = '{$query["Solves"]}', `First Solve`='{$query["First Solve"]}' " . "WHERE `Question ID` = '{$_SESSION["question"]}';";
    mysqli_query($db_connection, $result);
    $_SESSION["question"] = "";
    mysqli_close($db_connection);
    header("Location: ./profile.php");
    die;
}
function treasure_chest_located($i)
{
    require_once './support/dbcon.php';
    global $db_connection;
    global $_SESSION;
    $value = 1 << $i;
    $_SESSION["tchests"] = $_SESSION["tchests"] | $value;
    $query = "UPDATE `ContestantsData` SET `TChests Unlocked`= '{$_SESSION["tchests"]}' WHERE " . "`Username` = '{$_SESSION["username"]}'";
    mysqli_query($db_connection, $query);
    $score = (int) ($_SESSION["total-score"] / 10);
    push_increase("Treasure Chest UNLOCKED!!!", $score, FALSE);
    sync_scores();
}