From 8c193673abebac4b6c2835e408d7c8ba750f1ee8 Mon Sep 17 00:00:00 2001 From: Leander Date: Sat, 1 Apr 2017 11:10:06 +0200 Subject: [PATCH] T1704011110 --- Database.php | 6 ++++++ Question.php | 37 +++++++++++++++++++++++++++++++++++++ includes.inc.php | 1 + register.php | 12 ++++++------ start.php | 32 ++++++++++++++++++++------------ 5 files changed, 70 insertions(+), 18 deletions(-) create mode 100644 Question.php diff --git a/Database.php b/Database.php index ba051fd..4734e2f 100644 --- a/Database.php +++ b/Database.php @@ -6,6 +6,9 @@ * Date: 02/03/2017 * Time: 09:59 */ + +require_once('includes.inc.php'); + class Database { private $db; @@ -14,6 +17,7 @@ class Database $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. function query($sql, $params = null) { $result = null; if($params) { @@ -33,6 +37,7 @@ class Database return $query_response; } + // Create response to API request. function create_response($data, $error, $values = null) { $status_message = Database::create_status_code($error, $values); $meta = array('status'=>$error, 'message'=>$status_message); @@ -40,6 +45,7 @@ class Database return $response_element_json; } + // Create status message from status code. function create_status_code($error, $values) { switch ($error) { case 0: diff --git a/Question.php b/Question.php new file mode 100644 index 0000000..3f86ebb --- /dev/null +++ b/Question.php @@ -0,0 +1,37 @@ +query('SELECT * FROM "Questions" ORDER BY "ID" OFFSET $1 LIMIT 1', array($offset)); + $q = $db->query('SELECT "question" FROM "Questions" WHERE "ID" = $1', array($qid)); + $a0 = $db->query('SELECT "answer0" FROM "Questions" WHERE "ID" = $1', array($qid)); + $a1 = $db->query('SELECT "answer1" FROM "Questions" WHERE "ID" = $1', array($qid)); + $a2 = $db->query('SELECT "answer2" FROM "Questions" WHERE "ID" = $1', array($qid)); + $a3 = $db->query('SELECT "answer3" FROM "Questions" WHERE "ID" = $1', array($qid)); + $cid = $db->query('SELECT "Categories_ID" FROM ' + $this->question = $q; + $this->answers = array('a0' => $a0, 'a1' => $a1, 'a2' => $a2, 'a3' => $a3); + shuffle($this->answers); + $this->right_answer_position = array_search('a0', $this->answers); + } + + // Create the question message for API. + function get_question() { + + } + +} \ No newline at end of file diff --git a/includes.inc.php b/includes.inc.php index 5264098..408f29e 100644 --- a/includes.inc.php +++ b/includes.inc.php @@ -11,5 +11,6 @@ require_once('config.inc.php'); require_once('header.inc.php'); require_once('Database.php'); +require_once('Question.php'); ?> \ No newline at end of file diff --git a/register.php b/register.php index 221b70a..3acfd2e 100644 --- a/register.php +++ b/register.php @@ -12,17 +12,17 @@ $error = 0; $name = $_GET['name']; $uid = md5(uniqid(rand(), true)); - $database = new Database(); + $db = new Database(); - $numbers = range(0, 19); - shuffle($numbers); - for($i = 0; $i < 20; $i++){ - print $numbers[$i]; - } + $v = $db->query('SELECT * FROM "Questions" ORDER BY "ID" OFFSET $1 LIMIT 1', array(0)); + print $v; if(preg_match(PATTERN, $name) != 1 or !$name) { $error = 11101; } else { + // Create user. + $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; } else { diff --git a/start.php b/start.php index abc8b97..8862a2d 100644 --- a/start.php +++ b/start.php @@ -13,8 +13,15 @@ $uid = $_GET['uid']; $length = $_GET['length']; $length = (($length === '0') || ($length && gettype(+$length) == 'integer')) ? +$_GET['length'] : 10; - $database = new Database(); - $questions_count = $database->query('SELECT COUNT(*) FROM "Questions"')['data'][0]['count']; + $db = new Database(); + $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){ + $numbers = range($min, $max); + shuffle($numbers); + return array_slice($numbers, 0, $count-1); + } if($length == 0) { $error = 12101; @@ -22,19 +29,20 @@ $error = 12102; $values = array($questions_count); } else { - if($database->query('SELECT EXISTS (SELECT 1 FROM "Games" WHERE "ID" = $1)', array($uid)) == 't') { - $database->query('DELETE FROM "Games" WHERE "ID" = $1', array($uid)); + // Delete existing game for user. + if($db->query('SELECT EXISTS (SELECT 1 FROM "Games" WHERE "ID" = $1)', array($uid)) == 't') { + $db->query('DELETE FROM "Games" WHERE "ID" = $1', array($uid)); } - $database->query('INSERT INTO "Games" ("ID") VALUES ($1)', array($uid)); - for($i = 0; $i < $length; $i++) { - do { - $offset = rand(0, $questions_count-1); - $question = $database->query('SELECT "ID" FROM "Questions" OFFSET $1 LIMIT 1', array($offset)); - $database->query('INSERT INTO "GamesQuestions" ("Games_ID", "Questions_ID") VALUES ($1, $2)', - array($uid, $question)); - } while(pg_last_error != ''); + // Create new game for user. + $db->query('INSERT INTO "Games" ("ID") VALUES ($1)', array($uid)); + // 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)); + $db->query('INSERT INTO "GamesQuestions" ("Games_ID", "Questions_ID") VALUES ($1, $2)', array($uid, $q)); } } + $response = Database::create_response($data, $error, $values); print $response; ?> \ No newline at end of file