T1704011110
This commit is contained in:
@@ -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:
|
||||
|
||||
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('header.inc.php');
|
||||
require_once('Database.php');
|
||||
require_once('Question.php');
|
||||
|
||||
?>
|
||||
12
register.php
12
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 {
|
||||
|
||||
32
start.php
32
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;
|
||||
?>
|
||||
Reference in New Issue
Block a user