Created
July 19, 2011 13:04
-
-
Save rich97/1092227 to your computer and use it in GitHub Desktop.
Game node.js server for socket.io
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // New connection | |
| io.of('/game').sockets.on('connection', function(socket) { | |
| // Login using the token sent to us from the client | |
| socket.on('login', function(token) { | |
| // New player requires a new player object | |
| var player = new Player(token); | |
| // We can only perform other options once the login has completed | |
| player.login(function(player) { | |
| // Remember the player on the socket | |
| socket.set('player', player); | |
| // tell the client to update the view | |
| socket.emit('logged in', player); | |
| // wait for the join command | |
| socket.on('join game', function(level) { | |
| // callback once a game has been joined | |
| games.join(level, socket.get('player'), function(game) { | |
| if (game) { | |
| // Join the room with a unique id | |
| socket.join(game.id); | |
| // Remember the game info in the socket | |
| socket.set('game', game); | |
| // Send the player the other players info | |
| socket.emit('joined game', game.id); | |
| // Broadcast the new player to all members | |
| socket.broadcast.to(game.id) | |
| .emit('new player', socket.get('player')); | |
| // start a countdown timer to start the game | |
| if (game.players.length > 1) { | |
| // counter variable (duh!) | |
| var counter = 0; | |
| // We want to send the countdown in seconds to the client and we start at 30 | |
| var seconds = 30; | |
| // temporary variable for storing how far we have go in the countdown | |
| var remaining; | |
| // set a new interval to go off every second and keep the countdown synced among all players | |
| var interval = setInterval(function() { | |
| // perform the calculation for how many seconds left | |
| remaining = seconds - Math.ceil(counter / 1000); | |
| // broadcast how advanced the countdown is | |
| socket.broadcast.to(game.id) | |
| .emit('countdown', remaining); | |
| if (counter >= 30000) { | |
| // countdown is finished tell the client to change the views. | |
| socket.emit('start game'); | |
| clearInterval(interval); | |
| } | |
| counter += 1000; | |
| }, 1000); | |
| } | |
| } else { | |
| socket.emit('unable to join'); | |
| } | |
| }); | |
| }); | |
| // this code cannot be called by the client until it recives the OK from the server | |
| socket.on('get question', function() { | |
| // get the game | |
| var game = socket.get('game'); | |
| // get a new set of questions if there isn't one already | |
| if (!socket.to(game.id).get('questions')) { | |
| var questionSet = new QuestionSet(game.level); | |
| socket.to(game.id).set('questions', questionSet.get()); | |
| } | |
| // keep track of which question the player is on | |
| if (!socket.get('questionID')) { | |
| socket.set('questionID', 0); | |
| } | |
| // Get a question they haven't answered. | |
| var questionId = socket.get('questionID'); | |
| var question = socket.to(game.id) | |
| .get('questions')[]; | |
| // randomize the answers and remove "correct" key so it can be transmitted to the player | |
| question = questions.prepare(socket.get('questions')); | |
| // send the question back to the client | |
| socket.emit('question', question); | |
| }); | |
| // recive and answer for the question and update the score | |
| socket.on('answer', function(answer) { | |
| // get the game | |
| var game = socket.get('game'); | |
| // get the current question | |
| var questions = socket.to(game.id).get('questions'); | |
| var question = questions[socket.get('questionID')]; | |
| // compare the answer with the original to see if it's correct | |
| if (question.answers[answer].correct === true) { | |
| // add to the score | |
| if (!socket.get('score')) { | |
| socket.set('score', 0); | |
| } | |
| // Typecast the stored score and increment it | |
| var score = Number(socket.get('score'))++; | |
| // Set it as client data | |
| socket.set('score', score); | |
| // get the player | |
| var player = socket.get('player'); | |
| if (score < 20) { | |
| // score limit is 20 | |
| socket.broadcast.to(game.id).emit('score', { | |
| player: player, | |
| score: score | |
| }); | |
| } else { | |
| // someone has won the game update the score once more | |
| socket.broadcast.to(game.id).emit('score', { | |
| player: player, | |
| score: score | |
| }); | |
| // and finish the game | |
| socket.broadcast.to(game.id).emit('win', player); | |
| } | |
| } else { | |
| // get the number of lives remaining | |
| if (!socket.get('lives')) { | |
| socket.set('lives', 3); | |
| } | |
| // decrement number of lives | |
| var lives = Number(socket.get('lives'))--; | |
| // set the lives | |
| socket.set('lives', lives); | |
| // player lost a life | |
| socket.broadcast.to(game.id).emit('lives', { | |
| player: player, | |
| lives: lives | |
| }); | |
| } | |
| }); | |
| }); | |
| }); | |
| socket.on('disconnect', function() { | |
| }); | |
| }); |
Author
Sorry I can't, this is incomplete and from my experience of the last few days it's NOT the way you should go about it. It was a draft so that I could figure out how socket.io works, it should not be used (nor do I think it would work) in a production environment.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
hello, can u show ur player class?