function submitGraph() { if (isset($_GET["graph_id"]) && isset($_GET["solution"])) { $graphId = $_GET["graph_id"]; if ($graphId < 1 || $graphId > 9) { echo "invalid graph ID!"; return; } $username = ""; if (isset($_GET["username"])) { $username = $_GET["username"]; } $tc = new GraphTestCase(); $currGraphJson = $tc->jsonArray[$graphId - 1]; $currGraph = json_decode($currGraphJson, true); $solutionArray = json_decode($_GET["solution"], true); // echo var_dump($solutionArray); $totalScore = 0; $numMatch = count($solutionArray); foreach ($solutionArray as $solutionEdge) { $solutionLeft = $solutionEdge[0]; $solutionRight = $solutionEdge[1]; $valid = false; foreach ($currGraph["E"] as $edge) { $left = $edge[0]; $right = $edge[1]; if ($solutionLeft == $left && $solutionRight == $right) { $totalScore = $totalScore + $edge[2]; $valid = true; } } if (!$valid) { echo "edge (" . $solutionLeft . "," . $solutionRight . ") is invalid!"; return; } } // echo $totalScore; if (isset($_SESSION['start']) && isset($_SESSION['graph_id']) && $_SESSION['graph_id'] == $graphId) { /* or use !isset */ $_SESSION['start'] = 0; $_SESSION['graph_id'] = 0; $elapsed = time() - $_SESSION['visittime']; } else { echo "Invalid Session, please reset and try again!"; return; } $highscoreArray = array(); $highscoreArray["graph_id"] = $graphId; $highscoreArray["duration"] = $elapsed; $database = new Database(); $stmt = $database->getTop10Score($graphId); // echo var_dump($stmt); // Only submit if the username is not empty if (strlen($username) > 0) { $database->submitScore($graphId, $username, $numMatch, $totalScore, $elapsed); } // if nobody never played this graph_id yet if (count($stmt) == 0) { $highscoreArray["num_match"] = $numMatch; $highscoreArray["match_score"] = $totalScore; $highscoreArray["best_duration"] = $elapsed; $highscoreArray["new_best"] = 1; } else { // echo $numMatch." -- ".$stmt[0]["num_match"]."<br>"; // echo $totalScore." -- ".$stmt[0]["match_score"]."<br>"; $newHighscore = false; if ($numMatch > $stmt[0]["num_match"]) { $newHighscore = true; } else { if ($numMatch == $stmt[0]["num_match"] && $totalScore > $stmt[0]["match_score"]) { $newHighscore = true; } else { if ($numMatch == $stmt[0]["num_match"] && $totalScore == $stmt[0]["match_score"] && $elapsed < $stmt[0]["duration"]) { $newHighscore = true; } } } if ($newHighscore) { $highscoreArray["num_match"] = $numMatch; $highscoreArray["match_score"] = $totalScore; $highscoreArray["best_duration"] = $elapsed; $highscoreArray["new_best"] = 1; } else { $highscoreArray["num_match"] = $stmt[0]["num_match"]; $highscoreArray["match_score"] = $stmt[0]["match_score"]; $highscoreArray["best_duration"] = $stmt[0]["duration"]; $highscoreArray["new_best"] = 0; } } echo json_encode($highscoreArray); } else { echo "Wrong parameters for solution submission!"; return; } }