Permalink
Browse files

Clean up example

  • Loading branch information...
creationix committed Jun 14, 2011
1 parent eadfd0b commit c0b67b07ae5210faa593659e566a4925a69b3d17
Showing with 139 additions and 141 deletions.
  1. +0 −127 app.js
  2. +0 −3 config.js
  3. +11 −5 public/app.js
  4. +1 −4 public/index.html
  5. +0 −1 public/io
  6. +3 −0 public/style.css
  7. +124 −1 server.js
View
127 app.js
@@ -1,127 +0,0 @@
-require.paths.push('./lib', './lib/connect/lib', './lib/world-db/lib', './lib/step/lib');
-
-
-var Connect = require('connect'),
- io = require('socket.io'),
- worldDB = require('world-db'),
- imageClasses = require('./public/tiles'),
- imageClassesInv = {};
-
-Object.keys(imageClasses).forEach(function (i) {
- imageClassesInv[imageClasses[i]] = parseInt(i, 10);
-})
-
-var world = worldDB('/home/node/world.db', 1024, 10000);
-var emitter = new process.EventEmitter();
-
-// HTTP Logic
-module.exports = Connect.createServer(
- Connect.logger({format: ':method :url :status HTTP:http-version :remote-addr (:response-time ms)'}),
- Connect.conditionalGet(),
- Connect.cache(),
- Connect.gzip(),
- Connect.staticProvider(__dirname + "/public")
-);
-
-var socket = io.listen(module.exports);
-
-socket.on('connection', function (client) {
- // Lets us "know" what data the client wants
- var map = {};
-
- setInterval(function () {
- client.send('{}');
- }, 2000);
-
- function setMap(x, y, value) {
- (map[x] || (map[x] = {}))[y] = value;
- }
-
- function getMap(x, y) {
- return (map[x] && map[x][y]);
- }
-
- function trimMap() {
- var x2 = X + W;
- var y2 = Y + H;
- // Trim old columns
- Object.keys(map).forEach(function (ix) {
- if (ix < X || ix >= x2) {
- delete map[ix];
- return;
- }
- // Trim old rows
- var column = map[ix];
- Object.keys(column).forEach(function (iy) {
- if (iy < Y || iy >= y2) {
- delete column[iy];
- }
- });
- });
- }
-
- var X = 0, Y = 0, W = 0, H = 0;
-
- function watch(x,y,z,value) {
- if (x >= X && x < X + W && y >= Y && y < Y + H) {
- console.log("Sending to client");
- var message = {};
- message[x] = {};
- message[x][y] = {};
- message[x][y][z] = imageClassesInv[value];
- client.send(JSON.stringify(message));
- }
- }
- emitter.on("change", watch);
-
- client.on('message', function (json) {
- try {
- var message = JSON.parse(json);
- } catch (err) {
- console.error(err.stack);
- return;
- }
- if (message.v !== undefined) {
- var value = imageClasses[message.v];
- world.set(message.x, message.y, message.z, value);
- emitter.emit('change', message.x, message.y, message.z, value);
- return;
- }
- if (message.w && message.h) {
- X = message.x;
- Y = message.y;
- W = message.w;
- H = message.h;
-
- trimMap();
- var updates = {};
- var count = 0;
- var x2 = X + W, y2 = Y + H;
- for (var x = X; x < x2; x++) {
- for (var y = Y; y < y2; y++) {
- if (getMap(x, y) === undefined) {
- var cell = (updates[x] || (updates[x] = {}))[y] = [];
- setMap(x, y, true);
- for (var z = 0; z < 4; z++) {
- var value = imageClassesInv[world.get(x, y, z)];
- if (value > 0) {
- count++;
- cell[z] = parseInt(value, 10);
- }
- }
- }
- }
- }
-
- if (count) {
- client.send(JSON.stringify(updates));
- console.log("Send %d updates to client %s:%s", count, client.request.socket.remoteAddress, client.request.socket.remotePort);
- }
- }
- });
- client.on('disconnect', function () {
- emitter.removeListener('change', watch);
- });
-});
-
-
View
@@ -1,3 +0,0 @@
-module.exports = {
- port: 1234
-}
View
@@ -167,9 +167,10 @@ function scrollMap() {
if (ox < 0) ox += TILE_WIDTH;
var oy = wy % TILE_HEIGHT;
if (oy < 0) oy += TILE_HEIGHT;
-
- mapFrame.scrollLeft = ox;
- mapFrame.scrollTop = oy + TILE_HEIGHT / 2;
+ oy += TILE_HEIGHT / 2;
+ mapDiv.style.webkitTransform = "translate3d(" + -ox + "px," + -oy + "px,0)";
+ // mapFrame.scrollLeft = ox;
+ // mapFrame.scrollTop = oy;
}
@@ -253,8 +254,13 @@ function onLoad() {
paletteFrame.scrollTop += dy;
}, onClick);
-
- socket = new io.Socket(null);//, {transports: ['xhr-polling']});
+ if (typeof PalmSystem === "undefined") {
+ socket = new io.Socket(null);
+ } else {
+ socket = new io.Socket("10.0.1.6");
+ PalmSystem.stageReady();
+ PalmSystem.enableFullScreenMode(true);
+ }
socket.connect();
socket.on('message', onMessage);
socket.on('connect', function () {
View
@@ -8,10 +8,7 @@
<link rel="stylesheet" href="style.css"/>
<script src="compat.js" type="text/javascript" charset="utf-8"></script>
<script src="tiles.js" type="text/javascript" charset="utf-8"></script>
- <script src="/io/socket.io.js" type="text/javascript" charset="utf-8"></script>
- <script>
- io.setPath('/io/');
- </script>
+ <script src="socket.io/socket.io.js" type="text/javascript" charset="utf-8"></script>
<script src="app.js" type="text/javascript" charset="utf-8"></script>
</head>
<body id="main">
Submodule io deleted from a8efec
View
@@ -17,6 +17,9 @@
overflow: hidden;
background-color: #aaa;
}
+#map {
+ position: absolute;
+}
body {
border: 0;
margin: 0;
View
125 server.js
@@ -1 +1,124 @@
-require('./app').listen(80);
+
+var Http = require('http'),
+ Stack = require('stack'),
+ Creationix = require('creationix'),
+ io = require('socket.io'),
+ worldDB = require('world-db'),
+ imageClasses = require('./public/tiles'),
+ imageClassesInv = {};
+
+Object.keys(imageClasses).forEach(function (i) {
+ imageClassesInv[imageClasses[i]] = parseInt(i, 10);
+})
+
+var world = worldDB('/Users/tim/Desktop/adventure/world.db', 1024, 10000);
+var emitter = new process.EventEmitter();
+
+// HTTP Logic
+module.exports = Http.createServer(Stack(
+ Creationix.log(),
+ Creationix.static("/", __dirname + "/public", "index.html"),
+ Creationix.indexer("/", __dirname + "/public")
+));
+
+module.exports.listen(4000);
+console.log("demo running at port 4000");
+
+var socket = io.listen(module.exports);
+
+socket.on('connection', function (client) {
+ // Lets us "know" what data the client wants
+ var map = {};
+
+ function setMap(x, y, value) {
+ (map[x] || (map[x] = {}))[y] = value;
+ }
+
+ function getMap(x, y) {
+ return (map[x] && map[x][y]);
+ }
+
+ function trimMap() {
+ var x2 = X + W;
+ var y2 = Y + H;
+ // Trim old columns
+ Object.keys(map).forEach(function (ix) {
+ if (ix < X || ix >= x2) {
+ delete map[ix];
+ return;
+ }
+ // Trim old rows
+ var column = map[ix];
+ Object.keys(column).forEach(function (iy) {
+ if (iy < Y || iy >= y2) {
+ delete column[iy];
+ }
+ });
+ });
+ }
+
+ var X = 0, Y = 0, W = 0, H = 0;
+
+ function watch(x,y,z,value) {
+ if (x >= X && x < X + W && y >= Y && y < Y + H) {
+ console.log("Sending to client");
+ var message = {};
+ message[x] = {};
+ message[x][y] = {};
+ message[x][y][z] = imageClassesInv[value];
+ client.send(JSON.stringify(message));
+ }
+ }
+ emitter.on("change", watch);
+
+ client.on('message', function (json) {
+ try {
+ var message = JSON.parse(json);
+ } catch (err) {
+ console.error(err.stack);
+ return;
+ }
+ if (message.v !== undefined) {
+ var value = imageClasses[message.v];
+ world.set(message.x, message.y, message.z, value);
+ emitter.emit('change', message.x, message.y, message.z, value);
+ return;
+ }
+ if (message.w && message.h) {
+ X = message.x;
+ Y = message.y;
+ W = message.w;
+ H = message.h;
+
+ trimMap();
+ var updates = {};
+ var count = 0;
+ var x2 = X + W, y2 = Y + H;
+ for (var x = X; x < x2; x++) {
+ for (var y = Y; y < y2; y++) {
+ if (getMap(x, y) === undefined) {
+ var cell = (updates[x] || (updates[x] = {}))[y] = [];
+ setMap(x, y, true);
+ for (var z = 0; z < 4; z++) {
+ var value = imageClassesInv[world.get(x, y, z)];
+ if (value > 0) {
+ count++;
+ cell[z] = parseInt(value, 10);
+ }
+ }
+ }
+ }
+ }
+
+ if (count) {
+ client.send(JSON.stringify(updates));
+ console.log("Send %d updates to client %s:%s", count, client.request.socket.remoteAddress, client.request.socket.remotePort);
+ }
+ }
+ });
+ client.on('disconnect', function () {
+ emitter.removeListener('change', watch);
+ });
+});
+
+

0 comments on commit c0b67b0

Please sign in to comment.