Highscore calcultation implemented

This commit is contained in:
Leander
2017-04-06 10:15:05 +02:00
parent 82d97b81b5
commit 9509d6fcc9

View File

@@ -18,7 +18,7 @@ $chosen_answer = (($chosen_answer === '0') || ($chosen_answer && gettype(+$chose
function calculate_points($time) { function calculate_points($time) {
$points = $time < 2000 ? 100 : 205 - 1/2 * (205 + sqrt(-3975 + 2 * $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; return $points;
} }
@@ -54,16 +54,11 @@ if($uid == '') {
array($uid, $cid))['data'][0]['exists'] == 'f') { array($uid, $cid))['data'][0]['exists'] == 'f') {
$db->query('INSERT INTO "Highscores" ("Users_ID", "Categories_ID") VALUES ($1, $2)', array($uid, $cid)); $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 $old_high = $db->query('SELECT "score" FROM "Highscores" WHERE "Users_ID" = $1 AND "Categories_ID" = $2',
AND "Categories_ID" = $2', array($uid, $cid))['data'][0]; array($uid, $cid))['data'][0]['score'];
$old_hi_score_pq = $old_hi_data['score']; $new_high = intval($old_high * 0.95 + $score * 0.05);
$old_hi_questions = $old_hi_data['answered_questions']; $db->query('UPDATE "Highscores" SET "score" = $1 WHERE "Users_ID" = $2 AND "Categories_ID" = $3',
$old_hi_score_total = $old_hi_score_pq * $old_hi_questions; array($new_high, $uid, $cid));
$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));
//create next question if existing, end object otherwise //create next question if existing, end object otherwise
if($db->query('SELECT EXISTS (SELECT 1 FROM "GamesQuestions" WHERE "Games_ID" = $1)',array($uid)) 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); $end = array('score' => $new_points, 'time' => $new_time);
$data = array('results' => $results, 'end' => $end); $data = array('results' => $results, 'end' => $end);
$db->query('DELETE FROM "Games" WHERE "ID" = $1', array($uid)); $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));
} }
} }