10) { if($time < TIME_FULL) { $points = MAX_POINTS; } else { $points = round(MAX_POINTS - ( (POINTS_REDUCTION * sqrt(REDUCTION_STEP * (-8 * TIME_FULL + REDUCTION_STEP + 8 * $time))) / (2 * REDUCTION_STEP) ) ); } if($points < MIN_POINTS) { $points = MIN_POINTS; } } return $points; } $correct_answer = $db->query( 'SELECT "current_right_answer" FROM "Games" WHERE "ID" = $1', array($uid) )['data'][0]['current_right_answer']; $correct_token = $db->query( 'SELECT "current_token" FROM "Users" WHERE "ID" = $1', array($uid) )['data'][0]['current_token']; $correct = $correct_answer == $chosen_answer ? true : false; $score = $correct_answer == $chosen_answer ? calculate_points($time) : 0; if($uid == '') { $error = 13101; } else if($time == 0) { $error = 13102; } else if($chosen_answer == -1) { $error = 13103; } else if($token != $correct_token) { $data = []; } else if( $db->query( 'SELECT EXISTS ( SELECT 1 FROM "Games" WHERE "ID" = $1 )', array($uid) )['data'][0]['exists'] == 'f' ) { $error = 13104; } else { //set time and points $old_q_count = $db->query( 'SELECT "answered_questions" FROM "Games" WHERE "ID" = $1', array($uid) )['data'][0]['answered_questions']; $new_q_count = $old_q_count + 1; $db->query( 'UPDATE "Games" SET "answered_questions" = $1 WHERE "ID" = $2', array($new_q_count, $uid) ); $old_points = $db->query( 'SELECT "current_score" FROM "Games" WHERE "ID" = $1', array($uid) )['data'][0]['current_score']; $new_points = $old_points + $score; $db->query( 'UPDATE "Games" SET "current_score" = $1 WHERE "ID" = $2', array($new_points, $uid) ); $results = array( 'correct' => $correct, 'correctPos' => $correct_answer, 'score' => $score, 'total' => $new_points ); $old_time = $db->query( 'SELECT "total_time" FROM "Games" WHERE "ID" = $1', array($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 category highscore $cid = $db->query( 'SELECT "current_category" FROM "Games" WHERE "ID" = $1', array($uid) )['data'][0]['current_category']; 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_high = $db->query( 'SELECT "score" FROM "Highscores" WHERE "Users_ID" = $1 AND "Categories_ID" = $2', array($uid, $cid) )['data'][0]['score']; $new_high = $old_high == 0 ? $score : round($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 - then set user highscore 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); $data = array('results' => $results, 'next' => $next_question); } else { $end = array('score' => $new_points, 'time' => $new_time); $data = array('results' => $results, 'end' => $end); $old_user_high = $db->query( 'SELECT "highscore" FROM "Users" WHERE "ID" = $1', array($uid) )['data'][0]['highscore']; $answered_questions = $db->query( 'SELECT "answered_questions" FROM "Games" WHERE "ID" = $1', array($uid) )['data'][0]['answered_questions']; $new_points = round($new_points / $answered_questions); $score_multiplyer = (0.005 * $answered_questions < 0.25) ? 0.005 * $answered_questions : 0.25; $new_user_high = $old_user_high == 0 ? $new_points : round( $old_user_high * (1 - $score_multiplyer) + $new_points * $score_multiplyer ); $db->query( 'UPDATE "Users" SET "highscore" = $1, "current_token" = NULL WHERE "ID" = $2', array($new_user_high, $uid) ); $db->query( 'DELETE FROM "Games" WHERE "ID" = $1', array($uid) ); } } $response = Database::create_response($data, $error); print_r($response); ?>