From 9509d6fcc96e2af643f07bf94c97228bd4349904 Mon Sep 17 00:00:00 2001 From: Leander Date: Thu, 6 Apr 2017 10:15:05 +0200 Subject: [PATCH] Highscore calcultation implemented --- answer.php | 20 +++++++++----------- 1 file changed, 9 insertions(+), 11 deletions(-) diff --git a/answer.php b/answer.php index 57bee66..29c3d44 100644 --- a/answer.php +++ b/answer.php @@ -18,7 +18,7 @@ $chosen_answer = (($chosen_answer === '0') || ($chosen_answer && gettype(+$chose function calculate_points($time) { $points = $time < 2000 ? 100 : 205 - 1/2 * (205 + sqrt(-3975 + 2 * $time)); - $points = $points < 10 ? 100 : intval($points * 10); + $points = $points < 1 ? 10 : intval($points * 10); return $points; } @@ -54,16 +54,11 @@ if($uid == '') { 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 + 1; - $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)); + $old_high = $db->query('SELECT "score" FROM "Highscores" WHERE "Users_ID" = $1 AND "Categories_ID" = $2', + array($uid, $cid))['data'][0]['score']; + $new_high = intval($old_high * 0.95 + $score * 0.05); + $db->query('UPDATE "Highscores" SET "score" = $1 WHERE "Users_ID" = $2 AND "Categories_ID" = $3', + array($new_high, $uid, $cid)); //create next question if existing, end object otherwise if($db->query('SELECT EXISTS (SELECT 1 FROM "GamesQuestions" WHERE "Games_ID" = $1)',array($uid)) @@ -74,6 +69,9 @@ if($uid == '') { $end = array('score' => $new_points, 'time' => $new_time); $data = array('results' => $results, 'end' => $end); $db->query('DELETE FROM "Games" WHERE "ID" = $1', array($uid)); + $old_user_high = $db->query('SELECT "highscore" FROM "Users" WHERE "ID" = $1', array($uid)); + $new_user_high = $old_user_high == 0 ? $new_points : intval($old_user_high * 0.95 + $new_points * 0.05); + $db->query('UPDATE "Users" SET "highscore" = $1 WHERE "ID" = $1', array($uid)); } }