summaryrefslogtreecommitdiff
path: root/server.js
diff options
context:
space:
mode:
Diffstat (limited to 'server.js')
-rw-r--r--server.js32
1 files 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() );
}
@@ -187,6 +206,9 @@ io.sockets.on( 'connection', function(socket){
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;
game.part(socket);