Advertisement
Guest User

Untitled

a guest
Jul 2nd, 2016
146
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.42 KB | None | 0 0
  1. // Variables
  2. var socket = io.connect(),// Creates the connection variable
  3. setUsername = $("#setUsername"), //Form for when a user join
  4. newUser = $("#username"), // Input for where user inputs a name
  5. userSuccess = $(".successMessage"), // Success container for valid username
  6. userError = $(".errorMessage"), // Error container for invalid username
  7. playerOne = null,
  8. playerTwo = null,
  9. answerButton = $(".answerButton");
  10.  
  11. // ===============================================
  12. // GAME START LOGIC
  13. // ===============================================
  14.  
  15. function chatRoom(){
  16. var users = $(".playersOnline"), // Area to append all users names
  17. messageForm = $(".sendMessage"),
  18. messageBox = $(".message"),
  19. chat = $(".chat");
  20.  
  21. // ===============================================
  22. // CHAT AREA
  23. // ===============================================
  24. // Display all the usernames in chat room area
  25. socket.on("usernames", function(data){
  26. var html ="";
  27. for(i=0 ; i < data.length; i++) {
  28. html += data[i] + "<br/>";
  29. }
  30. users.html(html);
  31. });
  32.  
  33. // On sumbit sends message to server to be fufilled
  34. messageForm.click(function(e){
  35. // Prevent sumbit button default
  36. e.preventDefault();
  37. socket.emit("send message", messageBox.val());
  38. messageBox.val('');
  39. });
  40.  
  41. // Appends the new message from the user to the chat box
  42. socket.on("new message", function(data){
  43. console.log(data.user);
  44. chat.append("<b>" + data.user + ": </b>" + data.msg + "<br/>");
  45. });
  46.  
  47. // ===============================================
  48. // PLAYER VS PLAYER APPEND
  49. // ===============================================
  50.  
  51. // Append player names to battle
  52. socket.on("event_new_user", function(data){
  53. playerOne = data.userOne;
  54. playerTwo = data.userTwo;
  55. $(".playerOne").append(playerOne);
  56. $(".playerTwo").append(playerTwo);
  57. });
  58. }
  59.  
  60. // ===============================================
  61. // BUTTON AND ANSWER GENERATOR
  62. // ===============================================
  63.  
  64. function numberGenerator(){
  65. var goodAnswer = 21,
  66. gaPosition = Math.floor(Math.random() * 4),
  67. allAnswers = [],
  68. buttonArea = $("#answerOptions"),
  69. answer, n;
  70.  
  71. for(n = 0; n < 4; n++) {
  72. if(n == gaPosition) {
  73. answer = goodAnswer;
  74. }
  75. else {
  76. do {
  77. answer = Math.floor((Math.random() * 100) + 1);
  78. } while(answer == goodAnswer || allAnswers.indexOf(answer) !== -1);
  79. }
  80.  
  81. allAnswers.push(answer);
  82. input = $('<div class="col-xs-6"><button class="btn btn-primary answerButton">'+ answer +'</button></div>');
  83. input.appendTo(buttonArea);
  84. }
  85.  
  86. console.log(allAnswers);
  87. console.log(gaPosition);
  88.  
  89. $("#answerOptions").on('click', answerButton, function(){
  90.  
  91. if(answerButton == correctAnswer) {
  92. alert("Correct!");
  93. }
  94. });
  95. }
  96.  
  97.  
  98. var d1 = $.get('./pages/game.html' ,function(data){
  99. console.log("Success");
  100. $("#test").html(data);
  101. });
  102.  
  103. // ===============================================
  104. // ON READY
  105. // ===============================================
  106. $(function($) {
  107. socket.on('server ready', function(data)
  108. { console.log('server ready!');
  109. });
  110.  
  111. socket.on("serverAnswer", function(data){
  112. console.log(data.title);
  113. });
  114.  
  115. // Submit function for when a user submits their desired username
  116. setUsername.submit(function(e){
  117. // Prevent sumbit button default
  118. e.preventDefault();
  119. // Gets value of the username. function(data) allows us to reference the app.js
  120. // data which is the array of new users.
  121. socket.emit("new user", newUser.val(), function(data){
  122. // Checks if name is valid else displays an error
  123. if(data) {
  124. socket.on("full_lobby_check", function(data){
  125. if(data.lobbyStatus === false){
  126. // Fades out the login page
  127. userSuccess.removeClass("hide");
  128. setTimeout(function(){
  129. $("#loginPage").fadeOut();
  130. }, 1000);
  131.  
  132.  
  133. // Create a smoother transition between pages
  134. setTimeout(function(){
  135. $('#gamePage').fadeIn();
  136. }, 2000);
  137.  
  138. } else {
  139. // Disables button when lobby is full
  140. $("#userSubmit").addClass("disabled");
  141. }
  142. });
  143. } else {
  144. userError.removeClass("hide");
  145. }
  146. });
  147.  
  148. // Sets input to nothing if username is invalid
  149. newUser.val("");
  150. });
  151.  
  152. $.when(d1).done(function(){
  153. numberGenerator();
  154. chatRoom();
  155. });
  156. });
  157.  
  158.  
  159. // ===============================================
  160. // ANSWER CHECKER
  161. // ===============================================
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement