summaryrefslogtreecommitdiff
path: root/server.js
diff options
context:
space:
mode:
authorJules Laplace <jules@okfoc.us>2013-03-04 15:48:20 -0500
committerJules Laplace <jules@okfoc.us>2013-03-04 15:48:20 -0500
commit632a7220c2afdc99ae5942b919b42621a9c5df47 (patch)
tree0cb7c5d1e6b701ed31f5932de155b347967c237e /server.js
parent4dfa8d9dc602e11bf335d289ae7717f408403ca0 (diff)
bit of game logic
Diffstat (limited to 'server.js')
-rw-r--r--server.js107
1 files changed, 97 insertions, 10 deletions
diff --git a/server.js b/server.js
index 6e1d5f5..e731c34 100644
--- a/server.js
+++ b/server.js
@@ -31,6 +31,10 @@ app.get('/latest', function(req, res){
app.post('/upload', function(req, res) {
var nick = req.body.nick;
+
+ if (nick in game.images) return;
+ if (game.state != State.DRAWING) return;
+
var image = new Buffer(req.body.image, 'base64');
var filename = "/test/" + nick + "-" + Date.now() + "-" + "test" + ".png";
@@ -43,10 +47,12 @@ app.post('/upload', function(req, res) {
if (200 == s3res.statusCode) {
console.log('saved to %s', s3req.url);
- io.sockets.emit( 'event-image', {
+ var imageData = {
'nick': nick,
'url': s3req.url
- });
+ }
+
+ game.pushImage( imageData );
}
});
@@ -56,24 +62,35 @@ app.post('/upload', function(req, res) {
*/
var State = {
+ RESET: -1,
WAITING: 0,
DRAWING: 1,
VOTING: 2,
- WIN: 3,
- RESET: -1,
+ WINNING: 3,
};
+var Delay = {
+ DRAWING: 60,
+ VOTING: 30,
+ WINNING: 30,
+}
function Channel() {
this.state = State.WAITING;
this.messages = [];
+ this.message_id = 0;
+ this.players = {};
+ this.playerCount = 0;
this.images = {};
this.imageCount = 0;
- this.players = {};
- this.message_id = 0;
+ this.word = "";
+ this.timer = null;
}
-Channel.prototype.join = function(user){
- // socket.id
+Channel.prototype.join = function(user, socket.id){
+ this.playerCount++;
}
-Channel.prototype.push = function(message){
+Channel.prototype.part = function(){
+ this.playerCount = Math.max( 0, this.playerCount - 1 );
+}
+Channel.prototype.pushMessage = function(message){
if (this.messages.length > 20) this.messages.shift();
this.messages.push(message);
}
@@ -84,22 +101,92 @@ 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.checkState = function(){
+ if ( this.hasEnoughPlayers() ) {
+ if (this.state <= State.WAITING) {
+ this.newRound();
+ }
+ }
+ else {
+ if (this.state > State.WAITING) {
+ this.state = State.WAITING;
+ }
+ }
+}
+Channel.prototype.newWord = function(){
+ return choice(WORDS);
+}
+Channel.prototype.newRound = function(){
+ 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 );
+}
+Channel.prototype.startVoting = function(){
+ this.state = State.VOTING;
+ io.sockets.emit( 'event-state', this.getState() );
+ this.setTimeout( this.startWinning, Delay.VOTING );
+}
+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,
+ }
+}
+Channel.prototype.hasEnoughPlayers = function(){
+ var count = 0;
+ for (var i in this.players) {
+ if (++count > 2) return true;
+ }
+ return false;
+}
var game = new Channel ();
io.sockets.on( 'connection', function(socket){
socket.emit( 'event-welcome', game.report() );
socket.on( 'event-join', function(data){
+ game.join(data);
+ game.checkState();
socket.broadcast.emit('event-join', data);
} );
socket.on( 'event-message', function(data){
- game.push(data);
+ game.pushMessage(data);
socket.broadcast.emit('event-message', data);
} );
socket.on( 'disconnect', function(data){
+ game.part(data);
+ game.checkState();
socket.broadcast.emit('event-part', data);
} );
});
+/******************************* ETC
+*/
+function choice (l) { return l[ rand(l.length) ] }
+function rand (n) { return Math.floor( Math.random() * n ) }
+
+
/******************************* DONE
*/