diff options
| author | Jules Laplace <jules@okfoc.us> | 2013-03-04 16:14:46 -0500 |
|---|---|---|
| committer | Jules Laplace <jules@okfoc.us> | 2013-03-04 16:14:46 -0500 |
| commit | 915f6f2bf830260301d8117a2233e5d2c7788c34 (patch) | |
| tree | 6f53e80d021a0310c85b95718849046f8896b0a3 /server.js | |
| parent | 8cc16aaa52e0ea7670a9a7e8bdca9deb002012e3 (diff) | |
moving things around
Diffstat (limited to 'server.js')
| -rw-r--r-- | server.js | 80 |
1 files changed, 44 insertions, 36 deletions
@@ -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() ); |
