diff --git a/Config.txt b/Config.txt index e16dedd..d68a424 100644 --- a/Config.txt +++ b/Config.txt @@ -14,3 +14,8 @@ 12102 - 0 questions wanted, no game without a question! 12103 - More questions wanted then existing 12104 - User name not existing + += answer.php = +13101 - User ID not specified +13102 - No valid time specified +13103 - No chosen answer specified diff --git a/Database.php b/Database.php index b1bdf1d..2c378b0 100644 --- a/Database.php +++ b/Database.php @@ -60,14 +60,18 @@ class Database return 'User name does not match rules. User name must be 6 to 20 characters and consist of English letters and numbers only.'; case 11102: return 'User name already exists. Please pick a different name.'; - case 12101: - return 'User name not specified. This is an internal error.'; + case 12101 || 13101: + return 'User ID not specified. This is an internal error.'; case 12102: return 'A game without a question is not possible. Please select new game length.'; case 12103: return 'Sorry, we only have '.$values[0].' questions. Please try again.'; case 12104: - return 'The specified user name does not exist.'; + return 'The specified user name does not exist. This is an internal error.'; + case 13102: + return 'No valid time specified. This is an internal error.'; + case 13103: + return 'No chosen answer specified. This is an internal error.'; default: return 'An unknown error occurred.'; } diff --git a/answer.php b/answer.php index a70fb14..d5b7dd8 100644 --- a/answer.php +++ b/answer.php @@ -9,18 +9,35 @@ require_once('includes.inc.php'); $data = ''; $error = 0; - $user = isset($_GET['uid']) ? $_GET['uid'] : ''; + $uid = isset($_GET['uid']) ? $_GET['uid'] : ''; $time = isset($_GET['time']) ? $_GET['time'] : ''; $time = (($time === '0') || ($time && gettype(+$time) == 'integer')) ? +$_GET['time'] : 0; - $answer = isset($_GET['answer']) ? $_GET['answer'] : ''; - $answer = (($answer === '0') || ($answer && gettype(+$answer) == 'integer')) ? +$_GET['answer'] : -1; + $db = new Database(); + $chosen_answer = isset($_GET['answer']) ? $_GET['answer'] : ''; + $chosen_answer = (($chosen_answer === '0') || ($chosen_answer && gettype(+$chosen_answer) == 'integer')) ? +$_GET['answer'] : -1; function calculate_points($time) { - + $points = $time < 2000 ? 100 : 141.1421 - 0.02842916 * $time + 0.000004132312 * pow($time, 2) + - 3.432331e-10 * pow($time, 3) + 1.391508e-14 * pow($time, 4) - 2.166078e-19 * pow($time, 5); + $points = $points < 10 ? 10 : intval($points); + return $points; } - //if user is ''... - //if time is 0... - //if answer is -1... + if($uid == '') { + $error = 13101; + } else if($time == 0) { + $error = 13102; + } else if($chosen_answer == -1) { + $error = 13103; + } else { + $correct_answer = $db->query('SELECT "current_right_answer" FROM "Games" WHERE "ID" = $1', array($uid)) + ['data'][0]['current_right_answer']; + $correct = $correct_answer == $chosen_answer ? true : false; + $score = $correct_answer == $chosen_answer ? calculate_points($time) : 0; + } + + $results = array('correct' => $correct, 'score', $score); + $next_question = Question::get_next_question($db, $uid); + $data = array('results' => $results, 'next' => $next_question); ?> \ No newline at end of file