Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Variables
- var socket = io.connect(),// Creates the connection variable
- setUsername = $("#setUsername"), //Form for when a user join
- newUser = $("#username"), // Input for where user inputs a name
- userSuccess = $(".successMessage"), // Success container for valid username
- userError = $(".errorMessage"), // Error container for invalid username
- playerOne = null,
- playerTwo = null,
- answerButton = $(".answerButton");
- // ===============================================
- // GAME START LOGIC
- // ===============================================
- function chatRoom(){
- var users = $(".playersOnline"), // Area to append all users names
- messageForm = $(".sendMessage"),
- messageBox = $(".message"),
- chat = $(".chat");
- // ===============================================
- // CHAT AREA
- // ===============================================
- // Display all the usernames in chat room area
- socket.on("usernames", function(data){
- var html ="";
- for(i=0 ; i < data.length; i++) {
- html += data[i] + "<br/>";
- }
- users.html(html);
- });
- // On sumbit sends message to server to be fufilled
- messageForm.click(function(e){
- // Prevent sumbit button default
- e.preventDefault();
- socket.emit("send message", messageBox.val());
- messageBox.val('');
- });
- // Appends the new message from the user to the chat box
- socket.on("new message", function(data){
- console.log(data.user);
- chat.append("<b>" + data.user + ": </b>" + data.msg + "<br/>");
- });
- // ===============================================
- // PLAYER VS PLAYER APPEND
- // ===============================================
- // Append player names to battle
- socket.on("event_new_user", function(data){
- playerOne = data.userOne;
- playerTwo = data.userTwo;
- $(".playerOne").append(playerOne);
- $(".playerTwo").append(playerTwo);
- });
- }
- // ===============================================
- // BUTTON AND ANSWER GENERATOR
- // ===============================================
- function numberGenerator(){
- var goodAnswer = 21,
- gaPosition = Math.floor(Math.random() * 4),
- allAnswers = [],
- buttonArea = $("#answerOptions"),
- answer, n;
- for(n = 0; n < 4; n++) {
- if(n == gaPosition) {
- answer = goodAnswer;
- }
- else {
- do {
- answer = Math.floor((Math.random() * 100) + 1);
- } while(answer == goodAnswer || allAnswers.indexOf(answer) !== -1);
- }
- allAnswers.push(answer);
- input = $('<div class="col-xs-6"><button class="btn btn-primary answerButton">'+ answer +'</button></div>');
- input.appendTo(buttonArea);
- }
- console.log(allAnswers);
- console.log(gaPosition);
- $("#answerOptions").on('click', answerButton, function(){
- if(answerButton == correctAnswer) {
- alert("Correct!");
- }
- });
- }
- var d1 = $.get('./pages/game.html' ,function(data){
- console.log("Success");
- $("#test").html(data);
- });
- // ===============================================
- // ON READY
- // ===============================================
- $(function($) {
- socket.on('server ready', function(data)
- { console.log('server ready!');
- });
- socket.on("serverAnswer", function(data){
- console.log(data.title);
- });
- // Submit function for when a user submits their desired username
- setUsername.submit(function(e){
- // Prevent sumbit button default
- e.preventDefault();
- // Gets value of the username. function(data) allows us to reference the app.js
- // data which is the array of new users.
- socket.emit("new user", newUser.val(), function(data){
- // Checks if name is valid else displays an error
- if(data) {
- socket.on("full_lobby_check", function(data){
- if(data.lobbyStatus === false){
- // Fades out the login page
- userSuccess.removeClass("hide");
- setTimeout(function(){
- $("#loginPage").fadeOut();
- }, 1000);
- // Create a smoother transition between pages
- setTimeout(function(){
- $('#gamePage').fadeIn();
- }, 2000);
- } else {
- // Disables button when lobby is full
- $("#userSubmit").addClass("disabled");
- }
- });
- } else {
- userError.removeClass("hide");
- }
- });
- // Sets input to nothing if username is invalid
- newUser.val("");
- });
- $.when(d1).done(function(){
- numberGenerator();
- chatRoom();
- });
- });
- // ===============================================
- // ANSWER CHECKER
- // ===============================================
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement