From 9869c1c28ae48ba0276c03b0cad7e4923aef5c78 Mon Sep 17 00:00:00 2001 From: Leander Date: Wed, 5 Apr 2017 11:23:59 +0200 Subject: [PATCH] T201704051123 --- Question.php | 3 ++- answer.php | 19 +++++++++++++++++++ highscores.php | 5 +++++ 3 files changed, 26 insertions(+), 1 deletion(-) diff --git a/Question.php b/Question.php index 01788e1..62693e1 100644 --- a/Question.php +++ b/Question.php @@ -32,7 +32,8 @@ class Question $ca_position = array_search($a0, $this->answers); $this->category_id = $cid; $this->category = $c; - $db->query('UPDATE "Games" SET "current_right_answer" = $1 WHERE "ID" = $2', array($ca_position, $uid)); + $db->query('UPDATE "Games" SET "current_right_answer" = $1, "current_category" = $2 WHERE "ID" = $3', + array($ca_position, $cid, $uid)); } public static function get_next_question($db, $uid) { diff --git a/answer.php b/answer.php index 734eda2..4fcf134 100644 --- a/answer.php +++ b/answer.php @@ -37,6 +37,7 @@ if($uid == '') { ['data'][0]['exists'] == 'f') { $error = 13104; } else { + //set time and points $old_points = $db->query('SELECT "current_score" FROM "Games" WHERE "ID" = $1', array($uid)) ['data'][0]['current_score']; $new_points = $old_points + $score; @@ -46,6 +47,24 @@ if($uid == '') { ['data'][0]['total_time']; $new_time = $old_time + $time; $db->query('UPDATE "Games" SET "total_time" = $1 WHERE "ID" = $2', array($new_time, $uid)); + + //set high score + if($db->query('SELECT EXISTS (SELECT 1 FROM "Highscores" WHERE "Users_ID" = $1 AND "Categories_ID" = $2)', + array($uid, $cid))['data'][0]['exists'] == 'f') { + $db->query('INSERT INTO "Highscores" ("Users_ID", "Categories_ID"), VALUES ($1, $2)', array($uid, $cid)); + } + $old_hi_data = $db->query('SELECT "score", "answered_questions" FROM "Highscores" WHERE "Users_ID" = $1 + AND "Categories_ID" = $2', array($uid, $cid))['data'][0]; + $old_hi_score_pq = $old_hi_data['score']; + $old_hi_questions = $old_hi_data['answered_questions']; + $old_hi_score_total = $old_hi_score_pq * $old_hi_questions; + $new_hi_score_total = $old_hi_score_total + $score; + $new_hi_questions = $old_hi_questions++; + $new_hi_score_pq = intval($new_hi_score_total / $new_hi_questions); + $db->query('UPDATE "Highscores" SET "score" = $1, "answered_questions" = $2', + array($new_hi_score_pq, $new_hi_questions)); + + //create next question if existing, end object otherwise if($db->query('SELECT EXISTS (SELECT 1 FROM "GamesQuestions" WHERE "Games_ID" = $1)',array($uid)) ['data'][0]['exists'] == 't') { $next_question = Question::get_next_question($db, $uid); diff --git a/highscores.php b/highscores.php index 09c5834..735d2a9 100644 --- a/highscores.php +++ b/highscores.php @@ -6,6 +6,11 @@ * Time: 10:18 */ +require_once('includes.inc.php'); +$data = ''; +$error = 0; +$uid = isset($_GET['uid']) ? $_GET['uid'] : ''; + ?> \ No newline at end of file