diff --git a/Database.php b/Database.php index e2af49c..b159caa 100644 --- a/Database.php +++ b/Database.php @@ -14,7 +14,11 @@ class Database private $db; function __construct() { - $this->db = pg_connect("host=".DATABASE_URL." dbname=".DATABASE_NAME." user=".DATABASE_USER." password=".DATABASE_PASSWORD); + $this->db = pg_connect( + "host=".DATABASE_URL. + " dbname=".DATABASE_NAME. + " user=".DATABASE_USER. + " password=".DATABASE_PASSWORD); } // Execute $sql unsing array $params as params when needed. @@ -34,7 +38,7 @@ class Database } else { $data = null; } - $query_response=array('data' => $data, 'error' => $error); + $query_response = array('data' => $data, 'error' => $error); return $query_response; } diff --git a/allhighscores.php b/allhighscores.php index c4e905d..59f3b92 100644 --- a/allhighscores.php +++ b/allhighscores.php @@ -9,36 +9,65 @@ require_once('includes.inc.php'); $data = array(); $error = 0; -$cid = isset($_GET['category']) ? $_GET['category'] : -1; -$limit = isset($_GET['limit']) ? $_GET['limit'] : 10; -$offset = isset($_GET['offset']) ? $_GET['offset'] : 0; +$cid = isset($_GET['category']) ? + $_GET['category'] : -1; +$limit = isset($_GET['limit']) ? + $_GET['limit'] : 10; +$offset = isset($_GET['offset']) ? + $_GET['offset'] : 0; $db = new Database(); if($cid == -1) { - $overview = $db->query('SELECT "name", COALESCE("highscore", -1) as "highscore" - FROM "Users" ORDER BY COALESCE("highscore", -1) DESC, "name" ASC - LIMIT $1 OFFSET $2', array($limit, $offset))['data']; + $overview = $db->query( + 'SELECT "name", COALESCE("highscore", -1) as "highscore" + FROM "Users" + ORDER BY COALESCE("highscore", -1) DESC, "name" ASC + LIMIT $1 OFFSET $2', + array($limit, $offset) + )['data']; foreach($overview as $row) { $name = $row['name']; $score = $row['highscore']; - $place = $db->query('SELECT COUNT(*) FROM "Users" WHERE "highscore" > $1', array($score))['data'][0]['count'] + 1; - $data[] = array('place' => $place, 'name' => $name, 'score' => $score); + $place = $db->query( + 'SELECT COUNT(*) + FROM "Users" + WHERE "highscore" > $1', + array($score) + )['data'][0]['count'] + 1; + $data[] = array( + 'place' => $place, + 'name' => $name, + 'score' => $score + ); } } else { - $overview = $db->query('SELECT u."name", COALESCE(h."score", -1) AS "c_score" + $overview = $db->query( + 'SELECT u."name", COALESCE(h."score", -1) AS "c_score" FROM "Users" u LEFT OUTER JOIN - (SELECT "Users_ID", "score" - FROM "Highscores" - WHERE "Categories_ID" = $1) h ON u."ID" = h."Users_ID" + ( + SELECT "Users_ID", "score" + FROM "Highscores" + WHERE "Categories_ID" = $1 + ) h + ON u."ID" = h."Users_ID" ORDER BY "c_score" DESC, u."name" ASC - LIMIT $2 OFFSET $3 - ', array($cid, $limit, $offset))['data']; + LIMIT $2 OFFSET $3', + array($cid, $limit, $offset) + )['data']; foreach($overview as $row) { $name = $row['name']; $score = $row['c_score']; - $place = $db->query('SELECT COUNT(*) FROM "Highscores" WHERE "score" > $1 AND "Categories_ID" = $2', - array($score, $cid))['data'][0]['count'] + 1; - $data[] = array('place' => $place, 'name' => $name, 'score' => $score); + $place = $db->query( + 'SELECT COUNT(*) + FROM "Highscores" + WHERE "score" > $1 AND "Categories_ID" = $2', + array($score, $cid) + )['data'][0]['count'] + 1; + $data[] = array( + 'place' => $place, + 'name' => $name, + 'score' => $score + ); } } diff --git a/answer.php b/answer.php index 0f1560f..0b2c925 100644 --- a/answer.php +++ b/answer.php @@ -9,35 +9,56 @@ require_once('includes.inc.php'); $data = ''; $error = 0; -$uid = isset($_GET['uid']) ? $_GET['uid'] : ''; -$time = isset($_GET['time']) ? $_GET['time'] : ''; -$time = (($time === '0') || ($time && gettype(+$time) == 'integer')) ? +$_GET['time'] : 0; -$chosen_answer = isset($_GET['answer']) ? $_GET['answer'] : ''; -$chosen_answer = (($chosen_answer === '0') || ($chosen_answer && gettype(+$chosen_answer) == 'integer')) ? +$_GET['answer'] : -1; -$token = isset($_GET['token']) ? $_GET['token'] : ''; +$uid = isset($_GET['uid']) ? + $_GET['uid'] : ''; +$time = isset($_GET['time']) ? + $_GET['time'] : ''; +$time = (($time === '0') || ($time && gettype(+$time) == 'integer')) ? + +$_GET['time'] : 0; +$chosen_answer = isset($_GET['answer']) ? + $_GET['answer'] : ''; +$chosen_answer = (($chosen_answer === '0') || ($chosen_answer && gettype(+$chosen_answer) == 'integer')) ? + +$_GET['answer'] : -1; +$token = isset($_GET['token']) ? + $_GET['token'] : ''; $db = new Database(); function calculate_points($time) { $points = 0; if($time > 10) { - if ($time < TIME_FULL) { + 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))); + $points = round(MAX_POINTS + - ( + (POINTS_REDUCTION * sqrt(REDUCTION_STEP * (-8 * TIME_FULL + REDUCTION_STEP + 8 * $time))) + / (2 * REDUCTION_STEP) + ) + ); } - if ($points < MIN_POINTS) { + 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; +$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) { @@ -46,58 +67,144 @@ if($uid == '') { $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') { +} 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']; + $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']; + $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']; + $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)); + $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') { + $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)); + $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') { + 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', + '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']; + $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; - $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)); + $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) + ); } } diff --git a/categories.php b/categories.php index a447e2d..0ff2124 100644 --- a/categories.php +++ b/categories.php @@ -10,7 +10,10 @@ require_once('includes.inc.php'); $db = new Database(); $error = 0; -$categories = $db->query('SELECT "ID", "name" FROM "Categories"')['data']; +$categories = $db->query( + 'SELECT "ID", "name" + FROM "Categories"' +)['data']; $data = array(); foreach($categories as $cat) { $id = $cat['ID']; diff --git a/highscores.php b/highscores.php index a04b806..bc0a47b 100644 --- a/highscores.php +++ b/highscores.php @@ -9,34 +9,72 @@ require_once('includes.inc.php'); $data = ''; $error = 0; -$uid = isset($_GET['uid']) ? $_GET['uid'] : ''; +$uid = isset($_GET['uid']) ? + $_GET['uid'] : ''; $db = new Database(); if($uid == '') { $error = 15101; -} else if($db->query('SELECT EXISTS (SELECT 1 FROM "Users" WHERE "ID" = $1)', array($uid)) - ['data']['0']['exists'] == 'f') { +} else if( + $db->query( + 'SELECT EXISTS ( + SELECT 1 + FROM "Users" + WHERE "ID" = $1 + )', + array($uid) + )['data']['0']['exists'] == 'f' +) { $error = 15102; } else { - $score_all = $db->query('SELECT COALESCE("highscore", -1) as "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_all))['data'][0]['count'] + 1; + $score_all = $db->query( + 'SELECT COALESCE("highscore", -1) as "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_all) + )['data'][0]['count'] + 1; //get categories highscores - $categories = $db->query('SELECT "ID", "name" from "Categories"')['data']; + $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']; - $score = ($score == null) ? -1 : $score; + $score = $db->query( + 'SELECT "score" + FROM "Highscores" + WHERE "Users_ID" = $1 AND "Categories_ID" = $2', + array($uid, $cid) + )['data'][0]['score']; + $score = ($score == null) ? + -1 : $score; $place = $db->query( 'SELECT COUNT (*) FROM "Highscores" WHERE "Categories_ID" = $1 AND "score" > $2', - array($cid, $score))['data'][0]['count'] + 1; - $cat_scores[] = array('id' => $cid, 'name' => $cat, 'score' => $score, 'place' => $place); + array($cid, $score) + )['data'][0]['count'] + 1; + $cat_scores[] = array( + 'id' => $cid, + 'name' => $cat, + 'score' => $score, + 'place' => $place + ); } - $data = array('score' => $score_all, 'place' => $place_all, 'all' => $all, 'categories' => $cat_scores); + $data = array( + 'score' => $score_all, + 'place' => $place_all, + 'all' => $all, + 'categories' => $cat_scores); } $response = Database::create_response($data, $error); diff --git a/login.php b/login.php index cf82b45..06168fd 100644 --- a/login.php +++ b/login.php @@ -9,17 +9,30 @@ require_once('includes.inc.php'); $data = ''; $error = 0; -$uid = isset($_GET['uid']) ? $_GET['uid'] : ''; +$uid = isset($_GET['uid']) ? + $_GET['uid'] : ''; $db = new Database(); if($uid == '') { $error = 16101; } else { - $uid_exists = $db->query('SELECT EXISTS (SELECT 1 FROM "Users" WHERE "ID" = $1)', array($uid))['data'][0]['exists']; + $uid_exists = $db->query( + 'SELECT EXISTS ( + SELECT 1 + FROM "Users" + WHERE "ID" = $1 + )', + array($uid) + )['data'][0]['exists']; if ($uid_exists == 'f') { $error = 16102; } else { - $name = $db->query('SELECT "name" FROM "Users" WHERE "ID" = $1', array($uid))['data'][0]['name']; + $name = $db->query( + 'SELECT "name" + FROM "Users" + WHERE "ID" = $1', + array($uid) + )['data'][0]['name']; $data = array('uid' => $uid, 'name' => $name); } } diff --git a/register.php b/register.php index dcd2786..cdcbb67 100644 --- a/register.php +++ b/register.php @@ -10,7 +10,8 @@ require_once('includes.inc.php'); define('PATTERN', '/^[a-zA-Z0-9 ]{6,20}$/'); $data = ''; $error = 0; -$name = isset($_GET['name']) ? $_GET['name'] : ''; +$name = isset($_GET['name']) ? + $_GET['name'] : ''; $uid = md5(uniqid(rand(), true)); $db = new Database(); @@ -18,7 +19,11 @@ if(preg_match(PATTERN, $name) != 1) { $error = 11101; } else { // Create user. - $db->query('INSERT INTO "Users" ("ID", "name") VALUES ($1, $2)', array($uid, $name)); + $db->query( + 'INSERT INTO "Users" ("ID", "name") + VALUES ($1, $2)', + array($uid, $name) + ); // Create error if user to create already exists. if(preg_match('/violates unique constraint "Users_name_unique"/', pg_last_error()) == 1) { $error = 11102; diff --git a/rename.php b/rename.php index 5ce96c1..daecdd5 100644 --- a/rename.php +++ b/rename.php @@ -18,11 +18,24 @@ if(preg_match(PATTERN, $name) != 1) { $error = 14101; } else if($uid == '') { $error = 14102; -} else if($db->query('SELECT EXISTS (SELECT 1 FROM "Users" WHERE "ID" = $1)', array($uid)) - ['data'][0]['exists'] == 'f') { +} else if( + $db->query( + 'SELECT EXISTS ( + SELECT 1 + FROM "Users" + WHERE "ID" = $1 + )', + array($uid) + )['data'][0]['exists'] == 'f' +) { $error = 14104; } else { - $db->query('UPDATE "Users" SET "name" = $1 WHERE "ID" = $2', array($name, $uid)); + $db->query( + 'UPDATE "Users" + SET "name" = $1 + WHERE "ID" = $2', + array($name, $uid) + ); if(preg_match('/violates unique constraint "Users_name_unique"/', pg_last_error()) == 1) { $error = 14103; } else { diff --git a/start.php b/start.php index 39341d6..0f203f7 100644 --- a/start.php +++ b/start.php @@ -10,11 +10,17 @@ require_once('includes.inc.php'); $data = ''; $error = 0; $values = null; -$uid = isset($_GET['uid']) ? $_GET['uid'] : ''; -$length = isset($_GET['length']) ? $_GET['length'] : ''; -$length = (($length === '0') || ($length && gettype(+$length) == 'integer')) ? +$_GET['length'] : 10; +$uid = isset($_GET['uid']) ? + $_GET['uid'] : ''; +$length = isset($_GET['length']) ? + $_GET['length'] : ''; +$length = (($length === '0') || ($length && gettype(+$length) == 'integer')) ? + +$_GET['length'] : 10; $db = new Database(); -$questions_count = $db->query('SELECT COUNT(*) FROM "Questions"')['data'][0]['count']; +$questions_count = $db->query( + 'SELECT COUNT(*) + FROM "Questions"' +)['data'][0]['count']; // Return $count random, non repeating numbers in range from $min to $max. function random_numbers($min, $max, $count){ @@ -32,19 +38,43 @@ if($uid == '') { $values = array($questions_count); } else { // Delete existing game for user. - if($db->query('SELECT EXISTS (SELECT 1 FROM "Games" WHERE "ID" = $1)', array($uid))['data'][0]['exists'] == 't') { - $db->query('DELETE FROM "Games" WHERE "ID" = $1', array($uid)); + if( + $db->query( + 'SELECT EXISTS ( + SELECT 1 + FROM "Games" + WHERE "ID" = $1 + )', + array($uid) + )['data'][0]['exists'] == 't' + ) { + $db->query( + 'DELETE FROM "Games" + WHERE "ID" = $1', + array($uid) + ); } // Create new game for user. - $db->query('INSERT INTO "Games" ("ID") VALUES ($1)', array($uid)); + $db->query( + 'INSERT INTO "Games" ("ID") + VALUES ($1)', + array($uid)); if(preg_match('/violates foreign key constraint "Games_ID_fkey"/', pg_last_error()) == 1) { $error = 12104; } // Find questions for new game and insert into table. $questions = random_numbers(0, $questions_count-1, $length); foreach($questions as $question) { - $q = $db->query('SELECT "ID" FROM "Questions" OFFSET $1 LIMIT 1', array($question))['data'][0]['ID']; - $db->query('INSERT INTO "GamesQuestions" ("Games_ID", "Questions_ID") VALUES ($1, $2)', array($uid, $q)); + $qid = $db->query( + 'SELECT "ID" + FROM "Questions" + OFFSET $1 LIMIT 1', + array($question) + )['data'][0]['ID']; + $db->query( + 'INSERT INTO "GamesQuestions" ("Games_ID", "Questions_ID") + VALUES ($1, $2)', + array($uid, $qid)); } }