diff --git a/answer.php b/answer.php index a2282ac..236afd5 100644 --- a/answer.php +++ b/answer.php @@ -33,17 +33,29 @@ $error = 13102; } else if($chosen_answer == -1) { $error = 13103; - } else if($db->query('SELECT EXISTS (SELECT 1 FROM "Games" WHERE "ID" = $1)', array($uid))['data'][0]['exists'] == 'f') { + } else if($db->query('SELECT EXISTS (SELECT 1 FROM "Games" WHERE "ID" = $1)', array($uid)) + ['data'][0]['exists'] == 'f') { $error = 13104; - print 'no game'; } else { - // points in table + $old_points = $db->query('SELECT "current_score" FROM "Games" WHERE "ID" = $1', array($uid)) + ['data'][0]['current_score']; + $new_points = $old_points + $score; + $db->query('UPDATE "Games" SET "current_score" = $1 WHERE "ID" = $2', array($new_points, $uid)); + $results = array('correct' => $correct, 'score' => $score); + $old_time = $db->query('SELECT "total_time" FROM "Games" WHERE "ID" = $1', array($uid)) + ['data'][0]['total_time']; + $new_time = $old_time + $time; + $db->query('UPDATE "Games" SET "total_time" = $1 WHERE "ID" = $2', array($new_time, $uid)); + if($db->query('SELECT EXISTS (SELECT 1 FROM "GamesQuestions" WHERE "Games_ID" = $1)',array($uid)) + ['data'][0]['exist'] == 't') { + $next_question = Question::get_next_question($db, $uid); + $data = array('results' => $results, 'next' => $next_question); + } else { + $end = array('score' => $new_points, 'time' => $new_time); + $data = array('results' => $results, 'end' => $end); + } } - $results = array('correct' => $correct, 'score' => $score); - $next_question = Question::get_next_question($db, $uid); - $data = array('results' => $results, 'next' => $next_question); - $response = Database::create_response($data, $error); print_r($response);