T201704041219
This commit is contained in:
84
start.php
84
start.php
@@ -6,51 +6,51 @@
|
||||
* Time: 14:07
|
||||
*/
|
||||
|
||||
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;
|
||||
$db = new Database();
|
||||
$questions_count = $db->query('SELECT COUNT(*) FROM "Questions"')['data'][0]['count'];
|
||||
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;
|
||||
$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);
|
||||
// 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($uid == '') {
|
||||
$error = 12101;
|
||||
} else if($length == 0) {
|
||||
$error = 12102;
|
||||
} else if($length > $questions_count) {
|
||||
$error = 12103;
|
||||
$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($uid == '') {
|
||||
$error = 12101;
|
||||
} else if($length == 0) {
|
||||
$error = 12102;
|
||||
} else if($length > $questions_count) {
|
||||
$error = 12103;
|
||||
$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));
|
||||
}
|
||||
// Create new game for user.
|
||||
$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));
|
||||
}
|
||||
// Create new game for user.
|
||||
$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));
|
||||
}
|
||||
}
|
||||
|
||||
$next_question = Question::get_next_question($db, $uid);
|
||||
$data = array('next' => $next_question);
|
||||
$next_question = Question::get_next_question($db, $uid);
|
||||
$data = array('next' => $next_question);
|
||||
|
||||
$response = Database::create_response($data, $error, $values);
|
||||
print_r($response);
|
||||
$response = Database::create_response($data, $error, $values);
|
||||
print_r($response);
|
||||
?>
|
||||
Reference in New Issue
Block a user