Permalink
Browse files

"changed world object to a closure, moved to separate file. Tested lo…

…cally w/ no issues"
  • Loading branch information...
1 parent a0034a9 commit 82f04f6937612fc3c7b56ca10f0499412b47135f @clintcparker clintcparker committed Mar 1, 2012
Showing with 39 additions and 25 deletions.
  1. +6 −25 main.js
  2. +33 −0 static/js/world.js
View
31 main.js
@@ -1,9 +1,10 @@
-var express = require('express')
- , app = express.createServer()
- , io = require('socket.io').listen(app);
+var express = require('express'),
+ app = express.createServer(),
+ io = require('socket.io').listen(app),
+ world = require("./static/js/world");
app.configure(function(){
- app.use(express.static(__dirname + '/static'));
+ app.use(express["static"](__dirname + '/static'));
//app.use(express.errorHandler({ dumpExceptions: true, showStack: true }));
app.use(express.bodyParser());
});
@@ -14,32 +15,12 @@ app.get('/', function (req, res) {
res.sendfile(__dirname + '/static/simpleClient.html');
});
-var world = {
- grid: []
- , players: []
- , minPlayersToStart: 4
- , addPlayer: function(player) {this.players.push(player);}
- , howManyPlayers: function() {return this.players.length;}
- , timerId: 0
- , update: function() {
- // update ticks
- console.log("update ran");
- io.sockets.volatile.emit('update', {"hoo": "ray"});
- }
- , init: function() {
- if (this.timerId || this.howManyPlayers() < this.minPlayersToStart)
- return;
-
- this.timerId = setInterval(world.update, 1000);
- }
-};
-
io.sockets.on('connection', function (socket) {
// Update our list of players Add me
world.addPlayer({id: socket.id});
world.init();
- socket.broadcast.emit('playerCount', {players: world.howManyPlayers()})
+ socket.broadcast.emit('playerCount', {players: world.howManyPlayers()});
socket.on('move', function(data) {
// Update where this player wants to "move"
data.id = socket.id;
View
@@ -0,0 +1,33 @@
+var world = function () {
+ var grid = [];
+ var players = [];
+ var minPlayersToStart = 4;
+ var addPlayer = function(player) {players.push(player);}
+ var howManyPlayers = function() {return players.length;}
+ var timerId = 0;
+ var update = function() {
+ // update ticks
+ console.log("update ran");
+ io.sockets.volatile.emit('update', {"hoo": "ray"});
+ }
+ var init= function() {
+ if (this.timerId || howManyPlayers() < minPlayersToStart)
+ return;
+
+ this.timerId = setInterval(world.update, 1000);
+ }
+
+ return {
+ addPlayer:addPlayer ,
+ howManyPlayers:howManyPlayers,
+ update:update,
+ init:init
+ }
+}();
+
+if (typeof exports !== 'undefined') {
+ if (typeof module !== 'undefined' && module.exports) {
+ exports = module.exports = world;
+ }
+ exports.testingObj = world;
+}

0 comments on commit 82f04f6

Please sign in to comment.