code restructured
This commit is contained in:
@@ -14,7 +14,11 @@ class Database
|
|||||||
private $db;
|
private $db;
|
||||||
|
|
||||||
function __construct() {
|
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.
|
// Execute $sql unsing array $params as params when needed.
|
||||||
|
|||||||
@@ -9,36 +9,65 @@
|
|||||||
require_once('includes.inc.php');
|
require_once('includes.inc.php');
|
||||||
$data = array();
|
$data = array();
|
||||||
$error = 0;
|
$error = 0;
|
||||||
$cid = isset($_GET['category']) ? $_GET['category'] : -1;
|
$cid = isset($_GET['category']) ?
|
||||||
$limit = isset($_GET['limit']) ? $_GET['limit'] : 10;
|
$_GET['category'] : -1;
|
||||||
$offset = isset($_GET['offset']) ? $_GET['offset'] : 0;
|
$limit = isset($_GET['limit']) ?
|
||||||
|
$_GET['limit'] : 10;
|
||||||
|
$offset = isset($_GET['offset']) ?
|
||||||
|
$_GET['offset'] : 0;
|
||||||
$db = new Database();
|
$db = new Database();
|
||||||
|
|
||||||
if($cid == -1) {
|
if($cid == -1) {
|
||||||
$overview = $db->query('SELECT "name", COALESCE("highscore", -1) as "highscore"
|
$overview = $db->query(
|
||||||
FROM "Users" ORDER BY COALESCE("highscore", -1) DESC, "name" ASC
|
'SELECT "name", COALESCE("highscore", -1) as "highscore"
|
||||||
LIMIT $1 OFFSET $2', array($limit, $offset))['data'];
|
FROM "Users"
|
||||||
|
ORDER BY COALESCE("highscore", -1) DESC, "name" ASC
|
||||||
|
LIMIT $1 OFFSET $2',
|
||||||
|
array($limit, $offset)
|
||||||
|
)['data'];
|
||||||
foreach($overview as $row) {
|
foreach($overview as $row) {
|
||||||
$name = $row['name'];
|
$name = $row['name'];
|
||||||
$score = $row['highscore'];
|
$score = $row['highscore'];
|
||||||
$place = $db->query('SELECT COUNT(*) FROM "Users" WHERE "highscore" > $1', array($score))['data'][0]['count'] + 1;
|
$place = $db->query(
|
||||||
$data[] = array('place' => $place, 'name' => $name, 'score' => $score);
|
'SELECT COUNT(*)
|
||||||
|
FROM "Users"
|
||||||
|
WHERE "highscore" > $1',
|
||||||
|
array($score)
|
||||||
|
)['data'][0]['count'] + 1;
|
||||||
|
$data[] = array(
|
||||||
|
'place' => $place,
|
||||||
|
'name' => $name,
|
||||||
|
'score' => $score
|
||||||
|
);
|
||||||
}
|
}
|
||||||
} else {
|
} 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
|
FROM "Users" u LEFT OUTER JOIN
|
||||||
(SELECT "Users_ID", "score"
|
(
|
||||||
|
SELECT "Users_ID", "score"
|
||||||
FROM "Highscores"
|
FROM "Highscores"
|
||||||
WHERE "Categories_ID" = $1) h ON u."ID" = h."Users_ID"
|
WHERE "Categories_ID" = $1
|
||||||
|
) h
|
||||||
|
ON u."ID" = h."Users_ID"
|
||||||
ORDER BY "c_score" DESC, u."name" ASC
|
ORDER BY "c_score" DESC, u."name" ASC
|
||||||
LIMIT $2 OFFSET $3
|
LIMIT $2 OFFSET $3',
|
||||||
', array($cid, $limit, $offset))['data'];
|
array($cid, $limit, $offset)
|
||||||
|
)['data'];
|
||||||
foreach($overview as $row) {
|
foreach($overview as $row) {
|
||||||
$name = $row['name'];
|
$name = $row['name'];
|
||||||
$score = $row['c_score'];
|
$score = $row['c_score'];
|
||||||
$place = $db->query('SELECT COUNT(*) FROM "Highscores" WHERE "score" > $1 AND "Categories_ID" = $2',
|
$place = $db->query(
|
||||||
array($score, $cid))['data'][0]['count'] + 1;
|
'SELECT COUNT(*)
|
||||||
$data[] = array('place' => $place, 'name' => $name, 'score' => $score);
|
FROM "Highscores"
|
||||||
|
WHERE "score" > $1 AND "Categories_ID" = $2',
|
||||||
|
array($score, $cid)
|
||||||
|
)['data'][0]['count'] + 1;
|
||||||
|
$data[] = array(
|
||||||
|
'place' => $place,
|
||||||
|
'name' => $name,
|
||||||
|
'score' => $score
|
||||||
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
193
answer.php
193
answer.php
@@ -9,12 +9,18 @@
|
|||||||
require_once('includes.inc.php');
|
require_once('includes.inc.php');
|
||||||
$data = '';
|
$data = '';
|
||||||
$error = 0;
|
$error = 0;
|
||||||
$uid = isset($_GET['uid']) ? $_GET['uid'] : '';
|
$uid = isset($_GET['uid']) ?
|
||||||
$time = isset($_GET['time']) ? $_GET['time'] : '';
|
$_GET['uid'] : '';
|
||||||
$time = (($time === '0') || ($time && gettype(+$time) == 'integer')) ? +$_GET['time'] : 0;
|
$time = isset($_GET['time']) ?
|
||||||
$chosen_answer = isset($_GET['answer']) ? $_GET['answer'] : '';
|
$_GET['time'] : '';
|
||||||
$chosen_answer = (($chosen_answer === '0') || ($chosen_answer && gettype(+$chosen_answer) == 'integer')) ? +$_GET['answer'] : -1;
|
$time = (($time === '0') || ($time && gettype(+$time) == 'integer')) ?
|
||||||
$token = isset($_GET['token']) ? $_GET['token'] : '';
|
+$_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();
|
$db = new Database();
|
||||||
|
|
||||||
function calculate_points($time) {
|
function calculate_points($time) {
|
||||||
@@ -23,7 +29,12 @@ function calculate_points($time) {
|
|||||||
if($time < TIME_FULL) {
|
if($time < TIME_FULL) {
|
||||||
$points = MAX_POINTS;
|
$points = MAX_POINTS;
|
||||||
} else {
|
} 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;
|
$points = MIN_POINTS;
|
||||||
@@ -32,12 +43,22 @@ function calculate_points($time) {
|
|||||||
return $points;
|
return $points;
|
||||||
}
|
}
|
||||||
|
|
||||||
$correct_answer = $db->query('SELECT "current_right_answer" FROM "Games" WHERE "ID" = $1', array($uid))
|
$correct_answer = $db->query(
|
||||||
['data'][0]['current_right_answer'];
|
'SELECT "current_right_answer"
|
||||||
$correct_token = $db->query('SELECT "current_token" FROM "Users" WHERE "ID" = $1', array($uid))
|
FROM "Games"
|
||||||
['data'][0]['current_token'];
|
WHERE "ID" = $1',
|
||||||
$correct = $correct_answer == $chosen_answer ? true : false;
|
array($uid)
|
||||||
$score = $correct_answer == $chosen_answer ? calculate_points($time) : 0;
|
)['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 == '') {
|
if($uid == '') {
|
||||||
$error = 13101;
|
$error = 13101;
|
||||||
} else if($time == 0) {
|
} else if($time == 0) {
|
||||||
@@ -46,58 +67,144 @@ if($uid == '') {
|
|||||||
$error = 13103;
|
$error = 13103;
|
||||||
} else if($token != $correct_token) {
|
} else if($token != $correct_token) {
|
||||||
$data = [];
|
$data = [];
|
||||||
} else if($db->query('SELECT EXISTS (SELECT 1 FROM "Games" WHERE "ID" = $1)', array($uid))
|
} else if(
|
||||||
['data'][0]['exists'] == 'f') {
|
$db->query(
|
||||||
|
'SELECT EXISTS (
|
||||||
|
SELECT 1
|
||||||
|
FROM "Games"
|
||||||
|
WHERE "ID" = $1
|
||||||
|
)',
|
||||||
|
array($uid)
|
||||||
|
)['data'][0]['exists'] == 'f'
|
||||||
|
) {
|
||||||
$error = 13104;
|
$error = 13104;
|
||||||
} else {
|
} else {
|
||||||
//set time and points
|
//set time and points
|
||||||
$old_q_count = $db->query('SELECT "answered_questions" FROM "Games" WHERE "ID" = $1',
|
$old_q_count = $db->query(
|
||||||
array($uid))['data'][0]['answered_questions'];
|
'SELECT "answered_questions"
|
||||||
|
FROM "Games"
|
||||||
|
WHERE "ID" = $1',
|
||||||
|
array($uid)
|
||||||
|
)['data'][0]['answered_questions'];
|
||||||
$new_q_count = $old_q_count + 1;
|
$new_q_count = $old_q_count + 1;
|
||||||
$db->query('UPDATE "Games" SET "answered_questions" = $1 WHERE "ID" = $2', array($new_q_count, $uid));
|
$db->query(
|
||||||
$old_points = $db->query('SELECT "current_score" FROM "Games" WHERE "ID" = $1', array($uid))
|
'UPDATE "Games"
|
||||||
['data'][0]['current_score'];
|
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;
|
$new_points = $old_points + $score;
|
||||||
$db->query('UPDATE "Games" SET "current_score" = $1 WHERE "ID" = $2', array($new_points, $uid));
|
$db->query(
|
||||||
$results = array('correct' => $correct, 'correctPos' => $correct_answer, 'score' => $score, 'total' => $new_points);
|
'UPDATE "Games"
|
||||||
$old_time = $db->query('SELECT "total_time" FROM "Games" WHERE "ID" = $1', array($uid))
|
SET "current_score" = $1
|
||||||
['data'][0]['total_time'];
|
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;
|
$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
|
//set category highscore
|
||||||
$cid = $db->query('SELECT "current_category" FROM "Games" WHERE "ID" = $1', array($uid))
|
$cid = $db->query(
|
||||||
['data'][0]['current_category'];
|
'SELECT "current_category"
|
||||||
if($db->query('SELECT EXISTS (SELECT 1 FROM "Highscores" WHERE "Users_ID" = $1 AND "Categories_ID" = $2)',
|
FROM "Games"
|
||||||
array($uid, $cid))['data'][0]['exists'] == 'f') {
|
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));
|
$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',
|
$old_high = $db->query(
|
||||||
array($uid, $cid))['data'][0]['score'];
|
'SELECT "score"
|
||||||
$new_high = $old_high == 0 ? $score : round($old_high * 0.95 + $score * 0.05);
|
FROM "Highscores"
|
||||||
$db->query('UPDATE "Highscores" SET "score" = $1 WHERE "Users_ID" = $2 AND "Categories_ID" = $3',
|
WHERE "Users_ID" = $1 AND "Categories_ID" = $2',
|
||||||
array($new_high, $uid, $cid));
|
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
|
//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))
|
if(
|
||||||
['data'][0]['exists'] == 't') {
|
$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);
|
$next_question = Question::get_next_question($db, $uid);
|
||||||
$data = array('results' => $results, 'next' => $next_question);
|
$data = array('results' => $results, 'next' => $next_question);
|
||||||
} else {
|
} else {
|
||||||
$end = array('score' => $new_points, 'time' => $new_time);
|
$end = array('score' => $new_points, 'time' => $new_time);
|
||||||
$data = array('results' => $results, 'end' => $end);
|
$data = array('results' => $results, 'end' => $end);
|
||||||
$old_user_high = $db->query(
|
$old_user_high = $db->query(
|
||||||
'SELECT "highscore" FROM "Users" WHERE "ID" = $1',
|
'SELECT "highscore"
|
||||||
|
FROM "Users"
|
||||||
|
WHERE "ID" = $1',
|
||||||
array($uid)
|
array($uid)
|
||||||
)['data'][0]['highscore'];
|
)['data'][0]['highscore'];
|
||||||
$answered_questions = $db->query('SELECT "answered_questions" FROM "Games" WHERE "ID" = $1',
|
$answered_questions = $db->query(
|
||||||
array($uid))['data'][0]['answered_questions'];
|
'SELECT "answered_questions"
|
||||||
|
FROM "Games"
|
||||||
|
WHERE "ID" = $1',
|
||||||
|
array($uid)
|
||||||
|
)['data'][0]['answered_questions'];
|
||||||
$new_points = round($new_points / $answered_questions);
|
$new_points = round($new_points / $answered_questions);
|
||||||
$score_multiplyer = 0.005 * $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_user_high = $old_user_high == 0 ?
|
||||||
$new_points * $score_multiplyer);
|
$new_points : round(
|
||||||
$db->query('UPDATE "Users" SET "highscore" = $1, "current_token" = NULL WHERE "ID" = $2', array($new_user_high, $uid));
|
$old_user_high * (1 - $score_multiplyer)
|
||||||
$db->query('DELETE FROM "Games" WHERE "ID" = $1', array($uid));
|
+ $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)
|
||||||
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -10,7 +10,10 @@ require_once('includes.inc.php');
|
|||||||
$db = new Database();
|
$db = new Database();
|
||||||
$error = 0;
|
$error = 0;
|
||||||
|
|
||||||
$categories = $db->query('SELECT "ID", "name" FROM "Categories"')['data'];
|
$categories = $db->query(
|
||||||
|
'SELECT "ID", "name"
|
||||||
|
FROM "Categories"'
|
||||||
|
)['data'];
|
||||||
$data = array();
|
$data = array();
|
||||||
foreach($categories as $cat) {
|
foreach($categories as $cat) {
|
||||||
$id = $cat['ID'];
|
$id = $cat['ID'];
|
||||||
|
|||||||
@@ -9,34 +9,72 @@
|
|||||||
require_once('includes.inc.php');
|
require_once('includes.inc.php');
|
||||||
$data = '';
|
$data = '';
|
||||||
$error = 0;
|
$error = 0;
|
||||||
$uid = isset($_GET['uid']) ? $_GET['uid'] : '';
|
$uid = isset($_GET['uid']) ?
|
||||||
|
$_GET['uid'] : '';
|
||||||
$db = new Database();
|
$db = new Database();
|
||||||
|
|
||||||
if($uid == '') {
|
if($uid == '') {
|
||||||
$error = 15101;
|
$error = 15101;
|
||||||
} else if($db->query('SELECT EXISTS (SELECT 1 FROM "Users" WHERE "ID" = $1)', array($uid))
|
} else if(
|
||||||
['data']['0']['exists'] == 'f') {
|
$db->query(
|
||||||
|
'SELECT EXISTS (
|
||||||
|
SELECT 1
|
||||||
|
FROM "Users"
|
||||||
|
WHERE "ID" = $1
|
||||||
|
)',
|
||||||
|
array($uid)
|
||||||
|
)['data']['0']['exists'] == 'f'
|
||||||
|
) {
|
||||||
$error = 15102;
|
$error = 15102;
|
||||||
} else {
|
} else {
|
||||||
$score_all = $db->query('SELECT COALESCE("highscore", -1) as "highscore"
|
$score_all = $db->query(
|
||||||
FROM "Users" WHERE "ID" = $1', array($uid))['data'][0]['highscore'];
|
'SELECT COALESCE("highscore", -1) as "highscore"
|
||||||
$all = $db->query('SELECT COUNT (*) FROM "Users"')['data'][0]['count'];
|
FROM "Users"
|
||||||
$place_all = $db->query('SELECT COUNT (*) FROM "Users" WHERE "highscore" > $1', array($score_all))['data'][0]['count'] + 1;
|
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
|
//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) {
|
foreach($categories as $category) {
|
||||||
$cid = $category['ID'];
|
$cid = $category['ID'];
|
||||||
$cat = $category['name'];
|
$cat = $category['name'];
|
||||||
$score = $db->query('SELECT "score" FROM "Highscores" WHERE "Users_ID" = $1 AND "Categories_ID" = $2',
|
$score = $db->query(
|
||||||
array($uid, $cid))['data'][0]['score'];
|
'SELECT "score"
|
||||||
$score = ($score == null) ? -1 : $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(
|
$place = $db->query(
|
||||||
'SELECT COUNT (*) FROM "Highscores"
|
'SELECT COUNT (*) FROM "Highscores"
|
||||||
WHERE "Categories_ID" = $1 AND "score" > $2',
|
WHERE "Categories_ID" = $1 AND "score" > $2',
|
||||||
array($cid, $score))['data'][0]['count'] + 1;
|
array($cid, $score)
|
||||||
$cat_scores[] = array('id' => $cid, 'name' => $cat, 'score' => $score, 'place' => $place);
|
)['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);
|
$response = Database::create_response($data, $error);
|
||||||
|
|||||||
19
login.php
19
login.php
@@ -9,17 +9,30 @@
|
|||||||
require_once('includes.inc.php');
|
require_once('includes.inc.php');
|
||||||
$data = '';
|
$data = '';
|
||||||
$error = 0;
|
$error = 0;
|
||||||
$uid = isset($_GET['uid']) ? $_GET['uid'] : '';
|
$uid = isset($_GET['uid']) ?
|
||||||
|
$_GET['uid'] : '';
|
||||||
$db = new Database();
|
$db = new Database();
|
||||||
|
|
||||||
if($uid == '') {
|
if($uid == '') {
|
||||||
$error = 16101;
|
$error = 16101;
|
||||||
} else {
|
} 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') {
|
if ($uid_exists == 'f') {
|
||||||
$error = 16102;
|
$error = 16102;
|
||||||
} else {
|
} 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);
|
$data = array('uid' => $uid, 'name' => $name);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -10,7 +10,8 @@ require_once('includes.inc.php');
|
|||||||
define('PATTERN', '/^[a-zA-Z0-9 ]{6,20}$/');
|
define('PATTERN', '/^[a-zA-Z0-9 ]{6,20}$/');
|
||||||
$data = '';
|
$data = '';
|
||||||
$error = 0;
|
$error = 0;
|
||||||
$name = isset($_GET['name']) ? $_GET['name'] : '';
|
$name = isset($_GET['name']) ?
|
||||||
|
$_GET['name'] : '';
|
||||||
$uid = md5(uniqid(rand(), true));
|
$uid = md5(uniqid(rand(), true));
|
||||||
$db = new Database();
|
$db = new Database();
|
||||||
|
|
||||||
@@ -18,7 +19,11 @@ if(preg_match(PATTERN, $name) != 1) {
|
|||||||
$error = 11101;
|
$error = 11101;
|
||||||
} else {
|
} else {
|
||||||
// Create user.
|
// 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.
|
// 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;
|
||||||
|
|||||||
19
rename.php
19
rename.php
@@ -18,11 +18,24 @@ if(preg_match(PATTERN, $name) != 1) {
|
|||||||
$error = 14101;
|
$error = 14101;
|
||||||
} else if($uid == '') {
|
} else if($uid == '') {
|
||||||
$error = 14102;
|
$error = 14102;
|
||||||
} else if($db->query('SELECT EXISTS (SELECT 1 FROM "Users" WHERE "ID" = $1)', array($uid))
|
} else if(
|
||||||
['data'][0]['exists'] == 'f') {
|
$db->query(
|
||||||
|
'SELECT EXISTS (
|
||||||
|
SELECT 1
|
||||||
|
FROM "Users"
|
||||||
|
WHERE "ID" = $1
|
||||||
|
)',
|
||||||
|
array($uid)
|
||||||
|
)['data'][0]['exists'] == 'f'
|
||||||
|
) {
|
||||||
$error = 14104;
|
$error = 14104;
|
||||||
} else {
|
} 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) {
|
if(preg_match('/violates unique constraint "Users_name_unique"/', pg_last_error()) == 1) {
|
||||||
$error = 14103;
|
$error = 14103;
|
||||||
} else {
|
} else {
|
||||||
|
|||||||
48
start.php
48
start.php
@@ -10,11 +10,17 @@ require_once('includes.inc.php');
|
|||||||
$data = '';
|
$data = '';
|
||||||
$error = 0;
|
$error = 0;
|
||||||
$values = null;
|
$values = null;
|
||||||
$uid = isset($_GET['uid']) ? $_GET['uid'] : '';
|
$uid = isset($_GET['uid']) ?
|
||||||
$length = isset($_GET['length']) ? $_GET['length'] : '';
|
$_GET['uid'] : '';
|
||||||
$length = (($length === '0') || ($length && gettype(+$length) == 'integer')) ? +$_GET['length'] : 10;
|
$length = isset($_GET['length']) ?
|
||||||
|
$_GET['length'] : '';
|
||||||
|
$length = (($length === '0') || ($length && gettype(+$length) == 'integer')) ?
|
||||||
|
+$_GET['length'] : 10;
|
||||||
$db = new Database();
|
$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.
|
// Return $count random, non repeating numbers in range from $min to $max.
|
||||||
function random_numbers($min, $max, $count){
|
function random_numbers($min, $max, $count){
|
||||||
@@ -32,19 +38,43 @@ if($uid == '') {
|
|||||||
$values = array($questions_count);
|
$values = array($questions_count);
|
||||||
} else {
|
} else {
|
||||||
// Delete existing game for user.
|
// Delete existing game for user.
|
||||||
if($db->query('SELECT EXISTS (SELECT 1 FROM "Games" WHERE "ID" = $1)', array($uid))['data'][0]['exists'] == 't') {
|
if(
|
||||||
$db->query('DELETE FROM "Games" WHERE "ID" = $1', array($uid));
|
$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.
|
// 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) {
|
if(preg_match('/violates foreign key constraint "Games_ID_fkey"/', pg_last_error()) == 1) {
|
||||||
$error = 12104;
|
$error = 12104;
|
||||||
}
|
}
|
||||||
// Find questions for new game and insert into table.
|
// Find questions for new game and insert into table.
|
||||||
$questions = random_numbers(0, $questions_count-1, $length);
|
$questions = random_numbers(0, $questions_count-1, $length);
|
||||||
foreach($questions as $question) {
|
foreach($questions as $question) {
|
||||||
$q = $db->query('SELECT "ID" FROM "Questions" OFFSET $1 LIMIT 1', array($question))['data'][0]['ID'];
|
$qid = $db->query(
|
||||||
$db->query('INSERT INTO "GamesQuestions" ("Games_ID", "Questions_ID") VALUES ($1, $2)', array($uid, $q));
|
'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));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user