summaryrefslogtreecommitdiff
path: root/server.js
diff options
context:
space:
mode:
Diffstat (limited to 'server.js')
-rw-r--r--server.js80
1 files changed, 44 insertions, 36 deletions
diff --git a/server.js b/server.js
index 482638b..3953267 100644
--- a/server.js
+++ b/server.js
@@ -61,6 +61,7 @@ app.post('/upload', function(req, res) {
/******************************* WEBSOCKETS
*/
+var WORDS = "abstract cat dog lobster snail running column statue".split(" ");
var State = {
RESET: -1,
WAITING: 0,
@@ -84,6 +85,8 @@ function Channel() {
this.word = "";
this.timer = null;
}
+
+// CHATROOM STUFF
Channel.prototype.join = function(user, socket){
this.playerCount++;
}
@@ -94,6 +97,17 @@ Channel.prototype.pushMessage = function(message){
if (this.messages.length > 20) this.messages.shift();
this.messages.push(message);
}
+
+// GAME STATE STUFF
+Channel.prototype.getState = function(){
+ return {
+ state: this.state,
+ word: this.word,
+ players: this.players,
+ images: this.images,
+ imageCount: this.imageCount,
+ }
+}
Channel.prototype.report = function(){
return {
state: this.state,
@@ -101,28 +115,38 @@ Channel.prototype.report = function(){
players: this.players
}
}
-Channel.prototype.pushImage = function(image){
- this.imageCount++;
- this.images[ image.nick ] = image;
- io.sockets.emit( 'event-image', imageData );
- if (this.imageCount == this.playerCount) this.startVoting();
+Channel.prototype.newWord = function(){
+ return choice(WORDS);
+}
+Channel.prototype.setTimeout = function( callback, delay ){
+ var owner = this;
+ if (this.timeout) clearTimeout( this.timeout );
+ this.timeout = setTimeout( function(){ callback.call( owner ) }, delay );
}
+
+// Someone joined/left, so either start a game or stop it.
Channel.prototype.checkState = function(){
if ( this.hasEnoughPlayers() ) {
- if (this.state <= State.WAITING) {
- this.newRound();
- }
+ if (this.state <= State.WAITING) this.startDrawing();
}
- else {
- if (this.state > State.WAITING) {
- this.state = State.WAITING;
- }
+ else if (this.state > State.WAITING) this.startWaiting();
+}
+Channel.prototype.hasEnoughPlayers = function(){
+ var count = 0;
+ for (var i in this.players) {
+ if (++count > 2) return true;
}
+ return false;
}
-Channel.prototype.newWord = function(){
- return choice(WORDS);
+
+Channel.prototype.pushImage = function(image){
+ this.imageCount++;
+ this.images[ image.nick ] = image;
+ io.sockets.emit( 'event-image', imageData );
+ if (this.imageCount == this.playerCount) this.startVoting();
}
-Channel.prototype.newRound = function(){
+
+Channel.prototype.startDrawing = function(){
this.state = State.DRAWING;
this.word = this.newWord();
this.images = {};
@@ -138,30 +162,14 @@ Channel.prototype.startVoting = function(){
Channel.prototype.startWinning = function(){
this.state = State.WINNING;
io.sockets.emit( 'event-state', this.getState() );
- this.setTimeout( this.startWinning, Delay.WINNING );
-}
-Channel.prototype.setTimeout = function( callback, delay ){
- var owner = this;
- if (this.timeout) clearTimeout( this.timeout );
- this.timeout = setTimeout( function(){ callback.call( owner ) }, delay );
-}
-Channel.prototype.getState = function(){
- return {
- state: this.state,
- word: this.word,
- players: this.players,
- images: this.images,
- imageCount: this.imageCount,
- }
+ this.setTimeout( this.startDrawing, Delay.WINNING );
}
-Channel.prototype.hasEnoughPlayers = function(){
- var count = 0;
- for (var i in this.players) {
- if (++count > 2) return true;
- }
- return false;
+Channel.prototype.startWaiting = function(){
+ this.state = State.WAITING;
+ io.sockets.emit( 'event-state', this.getState() );
}
+
var game = new Channel ();
io.sockets.on( 'connection', function(socket){
socket.emit( 'event-welcome', game.report() );