From b6a1bdce96dfd4a8e26b5cdcdacc84bb9f5fd8d6 Mon Sep 17 00:00:00 2001 From: Leander Date: Thu, 6 Apr 2017 13:01:23 +0200 Subject: [PATCH] Fertig --- answer.php | 5 ++++- highscores.php | 31 ++++++++++++++++++++----------- 2 files changed, 24 insertions(+), 12 deletions(-) diff --git a/answer.php b/answer.php index 29c3d44..4a5ca0b 100644 --- a/answer.php +++ b/answer.php @@ -69,7 +69,10 @@ 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)); + $old_user_high = $db->query( + 'SELECT "highscore" FROM "Users" WHERE "ID" = $1', + array($uid) + )['data'][0]['highscore']; $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)); } diff --git a/highscores.php b/highscores.php index ddde3fd..df9974a 100644 --- a/highscores.php +++ b/highscores.php @@ -12,23 +12,32 @@ $error = 0; $uid = isset($_GET['uid']) ? $_GET['uid'] : ''; $db = new Database(); -$categories[] = array('id' => 1, 'name' => "BWL", 'score' => 321, 'place' => 123); -$categories[] = array('id' => 2, 'name' => "Mathematik", 'score' => 1454, 'place' => 1); -$info = array('score' => 3253, 'place' => 122, 'all' => 321, 'categories' => $categories); - -$response = Database::create_response($info, $error); -print_r($response); - if($uid == '') { $error = 15101; } else if($db->query('SELECT EXISTS (SELECT 1 FROM "Users" WHERE "ID" = $1)', array($uid)) ['data']['0']['exists'] == 'f') { $error = 15102; } else { - $score = $db->query('SELECT "highscore" FROM "Users" WHERE "ID" = $1', array($uid))['data'][0]['highscore']; - $all = $db->query('SELECT COUNT (*) FROM "Users"')['data']['0']['count']; - echo 'Score: '.$score; - print 'All: '.$all; + $score_all = $db->query('SELECT "highscore" FROM "Users" WHERE "ID" = $1', array($uid))['data'][0]['highscore']; + $all = $db->query('SELECT COUNT (*) FROM "Users"')['data'][0]['count']; + $place_all = $db->query('SELECT COUNT (*) FROM "Users" WHERE "highscore" > $1', array($score))['data'][0]['count'] + 1; + //get categories highscores + $categories = $db->query('SELECT "ID", "name" from "Categories"')['data']; + foreach($categories as $category) { + $cid = $category['ID']; + $cat = $category['name']; + $score = $db->query('SELECT "score" FROM "Highscores" WHERE "Users_ID" = $1 AND "Categories_ID" = $2', + array($uid, $cid)['data'][0]['score']); + $place = $db->query( + 'SELECT COUNT (*) FROM "Highscores" + WHERE "Users_ID" = $1 AND "Categories_ID" = $2 AND "score" > $3', + array($uid, $cid, $score)); + $cat_scores[] = array('id' => $cid, 'name' => $cat, 'score' => $score, 'place' => $place); + } + $data = array('score' => $score_all, 'place' => $place_all, 'all' => $all, 'categories' => $categories); } +$response = Database::create_response($data, $error); +print_r($response); + ?> \ No newline at end of file