From 14dd826757826fc61999c31e91760ae92f173f78 Mon Sep 17 00:00:00 2001 From: Jules Laplace Date: Mon, 4 Mar 2013 22:24:34 -0500 Subject: vote round server stuff --- server.js | 32 +++++++++++++++++++++++++++----- 1 file changed, 27 insertions(+), 5 deletions(-) diff --git a/server.js b/server.js index f5170c1..32b5849 100644 --- a/server.js +++ b/server.js @@ -82,6 +82,9 @@ function Channel() { this.playerCount = 0; this.images = {}; this.imageCount = 0; + this.votes = {}; + this.voters = {}; + this.voteCount = 0; this.word = ""; this.timer = null; } @@ -107,8 +110,10 @@ Channel.prototype.getState = function(){ word: this.word, players: this.players, images: this.images, + votes: this.votes, + voteCount: this.voteCount, imageCount: this.imageCount, - } + }; } Channel.prototype.report = function(){ return { @@ -148,11 +153,26 @@ Channel.prototype.pushImage = function(image){ if (this.imageCount == this.playerCount) this.startVoting(); } +Channel.prototype.addVote = function(data, user){ + if (data.nick in this.images && not( user.nick in this.voters )) { + this.voteCount++; + this.voters[ user.nick ] = true; + this.votes[ data.nick ] += 1; + if (this.voteCount == this.playerCount) this.startWinning(); + } +} + +Channel.prototype.reset = function(){ + this.images = {}; + this.imageCount = 0; + this.votes = {}; + this.voters = {}; + this.voteCount = 0; +} Channel.prototype.startDrawing = function(){ + this.reset(); this.state = State.DRAWING; this.word = this.newWord(); - this.images = {}; - this.imageCount = 0; io.sockets.emit( 'event-state', this.getState() ); this.setTimeout( this.startVoting, Delay.DRAWING ); } @@ -167,8 +187,7 @@ Channel.prototype.startWinning = function(){ this.setTimeout( this.startDrawing, Delay.WINNING ); } Channel.prototype.startWaiting = function(){ - this.images = {}; - this.imageCount = 0; + this.reset(); this.state = State.WAITING; io.sockets.emit( 'event-state', this.getState() ); } @@ -186,6 +205,9 @@ io.sockets.on( 'connection', function(socket){ socket.on( 'event-message', function(data){ game.pushMessage(data); socket.broadcast.emit('event-message', data); + } ); + socket.on( 'event-vote', function(data){ + game.addVote(data, socket.user); } ); socket.on( 'disconnect', function(){ var user = socket.user; -- cgit v1.2.3-70-g09d2