From 65259494af5b7c7db81101d4b6ea8efb39031811 Mon Sep 17 00:00:00 2001 From: Chris Strom Date: Thu, 13 May 2010 22:23:59 -0400 Subject: [PATCH] Refactor the comet view into a binary application so that an upstream app can feed it player info --- game.js | 45 +++++++++++++++++++++------------------------ 1 file changed, 21 insertions(+), 24 deletions(-) diff --git a/game.js b/game.js index f77530a..a6bcd12 100644 --- a/game.js +++ b/game.js @@ -23,25 +23,7 @@ with ( require( "fab" ) ) ( /^\/comet_view/ ) ( init_comet ) - - ( - function() { - var out = this; - return function( head ) { - var search = head.url.search.substring(1); - var q = require('querystring').parse(search); - for (var prop in q) { - puts(prop + ": " + q[prop]); - } - puts(q.x); - - - var app = out({ body: {x: q.x || 0, y: q.y || 0} }); - if ( app ) app(); - }; - } - ) - + ( player_from_querystring ) (/^\/(javascript|stylesheets)/) (/^\/([_\w]+)\.(js|css)$/) @@ -76,10 +58,10 @@ function init_comet (app) { var out = this; return app.call( function listener(obj) { - - players.push( out ); - out({ headers: { "content-type": "text/html" }, - body: "\n" }) + if (obj && obj.body) { + players.push(out); + out({ headers: { "content-type": "text/html" }, + body: "\n" }) ({body: "\n"}) ({body: "\n"}) @@ -92,8 +74,23 @@ function init_comet (app) { ({body: "\n"}) ({body: "\n"}) ({body: "\n"}); - + } return listener; }); }; } + +function player_from_querystring() { + var out = this; + return function(head) { + if (head.url.search) { + var search = head.url.search.substring(1); + var q = require('querystring').parse(search); + var app = out({ body: {id: q.id, x: q.x || 0, y: q.y || 0} }); + if ( app ) app(); + } + else { + out(); + } + }; +}