summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJules Laplace <jules@okfoc.us>2013-03-04 16:14:46 -0500
committerJules Laplace <jules@okfoc.us>2013-03-04 16:14:46 -0500
commit915f6f2bf830260301d8117a2233e5d2c7788c34 (patch)
tree6f53e80d021a0310c85b95718849046f8896b0a3
parent8cc16aaa52e0ea7670a9a7e8bdca9deb002012e3 (diff)
moving things around
-rw-r--r--public/js/game.js8
-rw-r--r--server.js80
2 files changed, 48 insertions, 40 deletions
diff --git a/public/js/game.js b/public/js/game.js
index 1025d82..8aed496 100644
--- a/public/js/game.js
+++ b/public/js/game.js
@@ -4,7 +4,7 @@ var State = {
WAITING: 0,
DRAWING: 1,
VOTING: 2,
- WIN: 3,
+ WINNING: 3,
RESET: -1,
}
@@ -20,8 +20,8 @@ var Game = {
socket.on('event-part', Events.receive.part);
socket.on('event-message', Events.receive.message);
socket.on('event-state', Events.receive.state);
- socket.on('event-vote', Events.receive.vote);
socket.on('event-image', Events.receive.image);
+ socket.on('event-vote', Events.receive.vote);
Auth.init();
Chat.init();
}
@@ -30,7 +30,7 @@ var Game = {
var Events = {
receive: {
-
+
// Player connects to the game
welcome: function(data){
for (var i in data.messages) {
@@ -114,7 +114,7 @@ UI[ State.VOTING ] = {
}
}
-UI[ State.WIN ] = {
+UI[ State.WINNING ] = {
load: function(){
},
unload: function(){
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() );