T1704011110
This commit is contained in:
@@ -6,6 +6,9 @@
|
|||||||
* Date: 02/03/2017
|
* Date: 02/03/2017
|
||||||
* Time: 09:59
|
* Time: 09:59
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
require_once('includes.inc.php');
|
||||||
|
|
||||||
class Database
|
class Database
|
||||||
{
|
{
|
||||||
private $db;
|
private $db;
|
||||||
@@ -14,6 +17,7 @@ class Database
|
|||||||
$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.
|
||||||
function query($sql, $params = null) {
|
function query($sql, $params = null) {
|
||||||
$result = null;
|
$result = null;
|
||||||
if($params) {
|
if($params) {
|
||||||
@@ -33,6 +37,7 @@ class Database
|
|||||||
return $query_response;
|
return $query_response;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Create response to API request.
|
||||||
function create_response($data, $error, $values = null) {
|
function create_response($data, $error, $values = null) {
|
||||||
$status_message = Database::create_status_code($error, $values);
|
$status_message = Database::create_status_code($error, $values);
|
||||||
$meta = array('status'=>$error, 'message'=>$status_message);
|
$meta = array('status'=>$error, 'message'=>$status_message);
|
||||||
@@ -40,6 +45,7 @@ class Database
|
|||||||
return $response_element_json;
|
return $response_element_json;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Create status message from status code.
|
||||||
function create_status_code($error, $values) {
|
function create_status_code($error, $values) {
|
||||||
switch ($error) {
|
switch ($error) {
|
||||||
case 0:
|
case 0:
|
||||||
|
|||||||
37
Question.php
Normal file
37
Question.php
Normal file
@@ -0,0 +1,37 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Created by PhpStorm.
|
||||||
|
* User: leanderschmedemann
|
||||||
|
* Date: 01/04/2017
|
||||||
|
* Time: 10:08
|
||||||
|
*/
|
||||||
|
|
||||||
|
require_once('includes.inc.php');
|
||||||
|
|
||||||
|
class Question
|
||||||
|
{
|
||||||
|
private $question;
|
||||||
|
private $answers;
|
||||||
|
private $right_answer_position;
|
||||||
|
|
||||||
|
function __construct($offset, $db) {
|
||||||
|
$qid = $db->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() {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -11,5 +11,6 @@
|
|||||||
require_once('config.inc.php');
|
require_once('config.inc.php');
|
||||||
require_once('header.inc.php');
|
require_once('header.inc.php');
|
||||||
require_once('Database.php');
|
require_once('Database.php');
|
||||||
|
require_once('Question.php');
|
||||||
|
|
||||||
?>
|
?>
|
||||||
12
register.php
12
register.php
@@ -12,17 +12,17 @@
|
|||||||
$error = 0;
|
$error = 0;
|
||||||
$name = $_GET['name'];
|
$name = $_GET['name'];
|
||||||
$uid = md5(uniqid(rand(), true));
|
$uid = md5(uniqid(rand(), true));
|
||||||
$database = new Database();
|
$db = new Database();
|
||||||
|
|
||||||
$numbers = range(0, 19);
|
$v = $db->query('SELECT * FROM "Questions" ORDER BY "ID" OFFSET $1 LIMIT 1', array(0));
|
||||||
shuffle($numbers);
|
print $v;
|
||||||
for($i = 0; $i < 20; $i++){
|
|
||||||
print $numbers[$i];
|
|
||||||
}
|
|
||||||
|
|
||||||
if(preg_match(PATTERN, $name) != 1 or !$name) {
|
if(preg_match(PATTERN, $name) != 1 or !$name) {
|
||||||
$error = 11101;
|
$error = 11101;
|
||||||
} else {
|
} 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) {
|
if(preg_match('/violates unique constraint "Users_name_unique"/', pg_last_error()) == 1) {
|
||||||
$error = 11102;
|
$error = 11102;
|
||||||
} else {
|
} else {
|
||||||
|
|||||||
32
start.php
32
start.php
@@ -13,8 +13,15 @@
|
|||||||
$uid = $_GET['uid'];
|
$uid = $_GET['uid'];
|
||||||
$length = $_GET['length'];
|
$length = $_GET['length'];
|
||||||
$length = (($length === '0') || ($length && gettype(+$length) == 'integer')) ? +$_GET['length'] : 10;
|
$length = (($length === '0') || ($length && gettype(+$length) == 'integer')) ? +$_GET['length'] : 10;
|
||||||
$database = new Database();
|
$db = new Database();
|
||||||
$questions_count = $database->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){
|
||||||
|
$numbers = range($min, $max);
|
||||||
|
shuffle($numbers);
|
||||||
|
return array_slice($numbers, 0, $count-1);
|
||||||
|
}
|
||||||
|
|
||||||
if($length == 0) {
|
if($length == 0) {
|
||||||
$error = 12101;
|
$error = 12101;
|
||||||
@@ -22,19 +29,20 @@
|
|||||||
$error = 12102;
|
$error = 12102;
|
||||||
$values = array($questions_count);
|
$values = array($questions_count);
|
||||||
} else {
|
} else {
|
||||||
if($database->query('SELECT EXISTS (SELECT 1 FROM "Games" WHERE "ID" = $1)', array($uid)) == 't') {
|
// Delete existing game for user.
|
||||||
$database->query('DELETE FROM "Games" WHERE "ID" = $1', array($uid));
|
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));
|
// Create new game for user.
|
||||||
for($i = 0; $i < $length; $i++) {
|
$db->query('INSERT INTO "Games" ("ID") VALUES ($1)', array($uid));
|
||||||
do {
|
// Find questions for new game and insert into table.
|
||||||
$offset = rand(0, $questions_count-1);
|
$questions = random_numbers(0, $questions_count-1, $length);
|
||||||
$question = $database->query('SELECT "ID" FROM "Questions" OFFSET $1 LIMIT 1', array($offset));
|
foreach($questions as $question) {
|
||||||
$database->query('INSERT INTO "GamesQuestions" ("Games_ID", "Questions_ID") VALUES ($1, $2)',
|
$q = $db->query('SELECT "ID" FROM "Questions" OFFSET $1 LIMIT 1', array($question));
|
||||||
array($uid, $question));
|
$db->query('INSERT INTO "GamesQuestions" ("Games_ID", "Questions_ID") VALUES ($1, $2)', array($uid, $q));
|
||||||
} while(pg_last_error != '');
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
$response = Database::create_response($data, $error, $values);
|
$response = Database::create_response($data, $error, $values);
|
||||||
print $response;
|
print $response;
|
||||||
?>
|
?>
|
||||||
Reference in New Issue
Block a user