Advertisement
Strider64

game_play_01.php

Sep 25th, 2016
235
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.32 KB | None | 0 0
  1. <?php
  2.  
  3. require_once 'lib/includes/utilities.inc.php';
  4.  
  5. use website_project\trivia_game\OutputQA;
  6.  
  7. /* Makes it so we don't have to decode the json coming from JQuery */
  8. header('Content-type: application/json');
  9.  
  10.  
  11.  
  12. $game_play = new OutputQA();
  13.  
  14. $q_num = filter_input(INPUT_POST, 'current_question', FILTER_SANITIZE_FULL_SPECIAL_CHARS);
  15.  
  16. if (isset($q_num) && $q_num) {
  17.     $data = $game_play->readQA($q_num);
  18.     if ($data) {
  19.         $output = json_encode($data[0]);
  20.         output($output);
  21.     } else {
  22.         $output = 'eof';
  23.         errorOutput($output, 410);
  24.     }
  25. }
  26.  
  27. $id = filter_input(INPUT_POST, 'id', FILTER_SANITIZE_NUMBER_INT);
  28. $answer = filter_input(INPUT_POST, 'answer', FILTER_SANITIZE_NUMBER_INT);
  29.  
  30. if (isset($id) && isset($answer)) {
  31.  
  32.     $result = $game_play->checkDailyTen($id, $answer);
  33.  
  34.     if ($result) {
  35.         $output = json_encode($result);
  36.         output($output);
  37.     } else {
  38.         $output = json_encode(['eof' => TRUE, 'message' => 'There are no more questions']);
  39.         errorOutput($output, 410);
  40.     }
  41. }
  42.  
  43. /*
  44.  * Set error code then send message to Ajax/JQuery
  45.  */
  46.  
  47. function errorOutput($output, $code = 500) {
  48.     http_response_code($code);
  49.     echo $output;
  50. }
  51.  
  52. /*
  53.  * If everything validates OK then send success message to Ajax/jQuery
  54.  */
  55.  
  56. function output($output) {
  57.     http_response_code(200);
  58.     echo $output;
  59. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement