Move conf outside of webroot; fix func invocation
[mudd.git] / mud.js
diff --git a/mud.js b/mud.js
index 2384832..b8e3074 100644 (file)
--- a/mud.js
+++ b/mud.js
@@ -8,33 +8,39 @@ $(document).ready(function(){
         this.x = x;
         this.y = y;
         this.id = id;
+        self = this;
 
         mud.rooms[this.x][this.y].join();
 
         this.move = function(direction) {
             var rooms = mud.rooms;
+
+            function move(direction, oldroom, newroom) {
+                $.get(endpoint, {
+                        'cmd' : 'move',
+                        'direction' : direction
+                    }, function(json) {
+                        oldroom.clear();
+                        newroom.join();
+                        if (json && json.description)
+                            writeToLog('', 'env', json.description);
+                    }
+                );
+            }
             switch (direction) {
                 case 'left':
                 case 'west':
                     if (rooms[this.x-1] && rooms[this.x-1][this.y]
                             && !rooms[this.x-1][this.y].state) {
-//                        $.get(endpoint, {
-//                                'cmd' : 'move',
-//                                'name' : 'majuscule',
-//                                'direction' : 'west'
-//                            }, function() {
-                            rooms[this.x][this.y].clear();
-                            rooms[this.x-1][this.y].join();
-                            this.x--;
-//                        });
+                        move('west', rooms[self.x][self.y], rooms[self.x-1][self.y])
+                        this.x--;
                     }
                     break;
                 case 'up':
                 case 'north':
                     if (rooms[this.x][this.y-1]
                             && !rooms[this.x][this.y-1].state) {
-                        rooms[this.x][this.y].clear();
-                        rooms[this.x][this.y-1].join();
+                        move('north', rooms[this.x][this.y], rooms[this.x][this.y-1]);
                         this.y--;
                     }
                     break;
@@ -42,8 +48,7 @@ $(document).ready(function(){
                 case 'east':
                     if (rooms[this.x+1] && rooms[this.x+1][this.y]
                         && !rooms[this.x+1][this.y].state) {
-                        rooms[this.x][this.y].clear();
-                        rooms[this.x+1][this.y].join();
+                        move('east', rooms[this.x][this.y], rooms[this.x+1][this.y]);
                         this.x++;
                     }
                     break;
@@ -51,8 +56,7 @@ $(document).ready(function(){
                 case 'south':
                     if (rooms[this.x][this.y+1]
                         && !rooms[this.x][this.y+1].state) {
-                        rooms[this.x][this.y].clear();
-                        rooms[this.x][this.y+1].join();
+                        move('south', rooms[this.x][this.y], rooms[this.x][this.y+1]);
                         this.y++;
                     }
                     break;
@@ -66,19 +70,26 @@ $(document).ready(function(){
         this.width = width;
         this.height = height;
         this.state = 0;
+        this.population = 0;
         this.fill = function() {
             c.fillStyle = "rgb(0,0,0)";
             c.fillRect(this.x, this.y, this.height, this.width);
             this.state = 1;
         }
-        this.clear = function() {
-            c.fillStyle = "rgb(255,255,255)";
-            c.fillRect(this.x, this.y, this.height, this.width);
+        this.clear = function(other, player) {
             this.state = 0;
+            this.population--;
+            if (!this.population) {
+                c.fillStyle = "rgb(255,255,255)";
+                c.fillRect(this.x, this.y, this.height, this.width);
+            }
         }
-        this.join = function() {
-            c.fillStyle = "rgb(0,255,0)";
+        this.join = function(other, player) {
+            c.fillStyle = !other || (player && mud && mud.player
+                                    && mud.player.x == player.x && mud.player.y == player.y)
+                            ? "rgb(0,255,0)" : "rgb(0,0,255)";
             c.fillRect(this.x, this.y, this.height, this.width);
+            this.population++;
         }
     }
 
@@ -86,6 +97,7 @@ $(document).ready(function(){
 
         var self = this;
         this.rooms = [];
+        this.players = [];
 
         this.build = function(seed) {
             this.rows = seed.length;
@@ -109,47 +121,47 @@ $(document).ready(function(){
             $.getJSON(endpoint, { 'cmd' : 'join', 'name' : name }, function(json) {
                 self.player = new player(json.x, json.y, json.id);
                 setInterval(self.poll, 1000);
+                self.populate(json.poll.players);
             });
         }
 
         var commands = {
             tell : function(msg) {
-                       console.log(msg);
                 var parts = msg.match(/^(\w+)\s(.*)/);
                 if (!parts[1]) return;
                 var dest = parts[1];
                 msg = parts[2];
-//                $.ajax({
-//                    url: endpoint,
-//                    data: { 'cmd' : 'tell', 'dest' : dest, 'msg' : msg },
-//                    success: function() {
-                        writeToLog('You told ' + dest + ': ', 'tell', msg);
-//                    },
-//                });
+                $.ajax({
+                    url: endpoint,
+                    data: { 'cmd' : 'tell', 'dest' : dest, 'msg' : msg },
+                    success: function() {
+                      self.writeToLog('You told ' + dest + ': ', 'tell', msg);
+                    },
+                });
             },
             yell : function(msg) {
                 $.ajax({
                     url: endpoint,
                     data: { 'cmd' : 'yell', 'msg' : msg },
                     success: function() {
-                        writeToLog('You yelled: ', 'yell', msg);
+                        self.writeToLog('You yelled: ', 'yell', msg);
                     },
                 });
             },
             say : function(msg) {
-//                $.ajax({
-//                    url: endpoint,
-//                    data: { 'cmd' : 'yell', 'msg' : msg },
-//                    success: function() {
-                        writeToLog('You said: ', 'say', msg);
-//                    },
-//                });
+                $.ajax({
+                    url: endpoint,
+                    data: { 'cmd' : 'say', 'msg' : msg },
+                    success: function() {
+                        self.writeToLog('You said: ', 'say', msg);
+                    },
+                });
             },
             move : function(direction) {
-                this.player.move(direction);
+                self.player.move(direction);
             },
         }
-        function writeToLog(action, style, msg) {
+        this.writeToLog = function(action, style, msg) {
             $('#log').append(
                 $('<div>').addClass('logline').append(
                     $('<span>').addClass(style).text(action),
@@ -214,19 +226,27 @@ $(document).ready(function(){
 
         $.getJSON(endpoint, { 'cmd' : 'start' }, function(json) {
             self.build(json)
-        })
-        .fail(function(jqxhr, textStatus, error) {
-            console.log(jqxhr, jqxhr.responseText, textStatus, error);
         });
 
+        this.populate = function(players) {
+            for (var i in this.players)
+                if (this.players[i].id != this.player.id)
+                    mud.rooms[this.players[i].x][this.players[i].y].clear();
+            this.players = players;
+            for (var i in this.players)
+                if (this.players[i].id != this.player.id)
+                    mud.rooms[this.players[i].x][this.players[i].y].join(1, this.players[i]);
+        }
+
         this.poll = function() {
-            $.getJSON(endpoint, { 'cmd' : 'poll' }, function(messages) {
-                for (var i in messages) {
-                    var msg = messages[i];
+            $.getJSON(endpoint, { 'cmd' : 'poll' }, function(json) {
+                for (var i in json.messages) {
+                    var msg = json.messages[i];
                     if (msg.id == self.player.id) continue;
-                    writeToLog(msg.name + ':', msg.type, msg.message);
-                    $("#log").animate({ scrollTop: $('#log')[0].scrollHeight}, 1000);
+                    self.writeToLog(msg.name + ':', msg.type, msg.message);
+                    $("#log").animate({ scrollTop: $('#log')[0].scrollHeight }, 1000);
                 }
+                self.populate(json.players);
             });
         }
     }