Advertisement
Strider64

game_play_01.js

Sep 25th, 2016
239
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
jQuery 6.37 KB | None | 0 0
  1. $(function () {
  2.     var totalQuestions = 10,
  3.             currentQuestion = 1,
  4.             score = 0,
  5.             $totalPts = $('.totalPoints'),
  6.             timer = null,
  7.             $timerAPI = $('.timer'),
  8.             duration = 30,
  9.             id = null,
  10.             correct = 0,
  11.             score = 0,
  12.             points = 100,
  13.             bright_green = '#66FF00',
  14.             $checkAns = $('.clicked'),
  15.             $nextBtn = $('.nextBtn');
  16.  
  17.     function displayScore(points) {
  18.         var displayScore = '',
  19.                 maxZeros = 5,
  20.                 x = 0;
  21.         if (points >= 0) {
  22.             $totalPts.css('color', bright_green);
  23.         } else {
  24.             $totalPts.css('color', 'red');
  25.         }
  26.         displayScore = Math.abs(points);
  27.         displayScore = displayScore.toString();
  28.         while ((displayScore.length - maxZeros) !== 0) {
  29.             displayScore = '0' + displayScore;
  30.         }
  31.         $totalPts.text(displayScore);
  32.     } // End of Display Score Function:
  33.  
  34.     displayScore(score);
  35.  
  36.     /*
  37.      * The Timer Function
  38.      */
  39.     function myTimer(sec) {
  40.         var displaySec = null; // This variable is used for HTML display:
  41.         if (timer) {
  42.             clearInterval(timer); // Stop Timer:
  43.         }
  44.         /*
  45.          * The actual timer function, setup and execution.
  46.          */
  47.         timer = setInterval(function () {
  48.             /*
  49.              * Display leading zero if less than 2 digits.
  50.              */
  51.             if (sec < 10) {
  52.                 displaySec = "0" + sec;
  53.             } else {
  54.                 displaySec = sec;
  55.             }
  56.             /*
  57.              * Display if timer is still running.
  58.              */
  59.             if (sec !== -1) {
  60.                 $timerAPI.text(displaySec);
  61.             }
  62.             if (sec === -1) {
  63.                 $checkAns.off('click', check_answer);
  64.                 var params = {id: id, answer: 5};
  65.                 var myData = jQuery.param(params);
  66.                 check_answer_ajax(myData);
  67.                 clearInterval(timer);
  68.                 $timerAPI.css('color', 'red');
  69.                 score = score - 50;
  70.             }
  71.             sec--;
  72.         }, 1000); // End of SetInterval Function:
  73.     } // end of Timer Function:
  74.  
  75.     $timerAPI.css('color', 'green');
  76.     $timerAPI.text(duration);
  77.     myTimer(duration);
  78.     $nextBtn.hide();
  79.  
  80.     function reset_display(e) {
  81.         e.preventDefault();
  82.         $timerAPI.css('color', 'green');
  83.         $timerAPI.text(duration);
  84.         $nextBtn.off('click', reset_display);
  85.         $nextBtn.hide();
  86.         $checkAns.css('background-color', '#3F4E70');
  87.         $checkAns.on('click', check_answer);
  88.         load_question(currentQuestion);
  89.  
  90.     }
  91.  
  92.     function check_answer(e) {
  93.         e.preventDefault();
  94.         //console.log("e.target", e.target);
  95.         var answer = $(e.target).data('answer');
  96.         var params = {id: id, answer: answer};
  97.         var myData = jQuery.param(params); // Set parameters to correct Ajax format:
  98.         check_answer_ajax(myData);
  99.     } // End of check_answer function:
  100.  
  101.     function check_answer_ajax(myData) {
  102.         $.ajax({
  103.             type: 'post',
  104.             url: 'game_play_01.php',
  105.             data: myData,
  106.             success: function (result) {
  107.                 //console.log(result);
  108.  
  109.  
  110.                 if (result.correct) {
  111.                     correct += 1;
  112.                     score = score + points;
  113.                     $('.answer' + result.right_answer).css("background-color", "green");
  114.                 } else if (result.user_answer === 5) {
  115.                     $('.answer' + result.right_answer).css("background-color", "green");
  116.                 } else if (result.user_answer <= 4) {
  117.                     score = score - (points / 4);
  118.                     $('.answer' + result.right_answer).css("background-color", "green");
  119.                     $('.answer' + result.user_answer).css("background-color", "red");
  120.                 }
  121.                 displayScore(score);
  122.                 clearInterval(timer);
  123.                 currentQuestion += 1;
  124.                 if (currentQuestion <= totalQuestions) {
  125.                     $checkAns.off('click', check_answer);
  126.                     $nextBtn.slideDown(500);
  127.                     $nextBtn.on('click', reset_display);
  128.                 } else {
  129.                     $checkAns.off('click', check_answer);
  130.                     $nextBtn.hide();
  131.                     $nextBtn.off('click', reset_display);
  132.                 }
  133.  
  134.             },
  135.             error: function (request, status, error) {
  136.  
  137.                 //console.log(request, status, error);
  138.                 //console.log('check_answer', request, request.responseText);
  139.  
  140.  
  141.             }
  142.         }); // End of ajax function:
  143.     }
  144.  
  145.     function load_question(currentQuestion) {
  146.         var params = {current_question: currentQuestion}; // Set parameters:
  147.         var myData = jQuery.param(params); // Set parameters to correct Ajax format:
  148.         $.ajax({
  149.             type: 'post',
  150.             url: 'game_play_01.php',
  151.             data: myData,
  152.             success: function (data) {
  153.                 myTimer(duration);
  154.                 //console.log(data);
  155.                 id = data.id;
  156.                 $('h3.displayQuest').text(data.question);
  157.                 //$('h3.displayQuest').text(data.q_num + ". " + data.question);
  158.                 $('a.answer1').text(data.answer1);
  159.                 $('a.answer2').text(data.answer2);
  160.                 $('a.answer3').text(data.answer3);
  161.                 $('a.answer4').text(data.answer4);
  162.  
  163.             },
  164.             error: function (request, status, error) {
  165.                 /*
  166.                  * If there is no data in the trivia database then give an error and reload the page.
  167.                  * This usually means that a new day and a new set of question was being "loaded" while
  168.                  * the player started the game.
  169.                  */
  170.                 console.log('load_question', request, request.responseText);
  171.                 if (request.request.responseText === 'eof') {
  172.                     clearInterval(timer);
  173.                     $checkAns.off('click', check_answer);
  174.                     $nextBtn.hide();
  175.                     $nextBtn.off('click', reset_display);
  176.                 }
  177.  
  178.             }
  179.         }); // End of ajax function:
  180.     } // End of retrieve_question function:
  181.  
  182.     load_question(currentQuestion);
  183.     $checkAns.on('click', check_answer);
  184.     $checkAns.click(function (event) {
  185.         event.preventDefault();
  186.  
  187.     });
  188. });  // End of Document Ready:
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement