From 015246c8a445dfad325cb9df846885d34d6284ff Mon Sep 17 00:00:00 2001 From: Dylan Lloyd Date: Fri, 4 Oct 2013 09:29:22 +0000 Subject: [PATCH] show other folk in the world --- mud.js | 38 ++++++++++++++++++++++++++++---------- mud.php | 10 +++++++--- 2 files changed, 35 insertions(+), 13 deletions(-) diff --git a/mud.js b/mud.js index 2384832..a9dc45b 100644 --- a/mud.js +++ b/mud.js @@ -66,19 +66,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 +93,7 @@ $(document).ready(function(){ var self = this; this.rooms = []; + this.players = []; this.build = function(seed) { this.rows = seed.length; @@ -108,7 +116,9 @@ $(document).ready(function(){ this.join = function(name) { $.getJSON(endpoint, { 'cmd' : 'join', 'name' : name }, function(json) { self.player = new player(json.x, json.y, json.id); + console.log(mud); setInterval(self.poll, 1000); + self.populate(json.poll.players); }); } @@ -214,19 +224,27 @@ $(document).ready(function(){ $.getJSON(endpoint, { 'cmd' : 'start' }, function(json) { self.build(json) - }) - .fail(function(jqxhr, textStatus, error) { + }).fail(function(jqxhr, textStatus, error) { console.log(jqxhr, jqxhr.responseText, textStatus, error); }); + this.populate = function(players) { + for (var i in this.players) + mud.rooms[this.players[i].x][this.players[i].y].clear(); + this.players = players; + for (var i in this.players) + 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) { + $.getJSON(endpoint, { 'cmd' : 'poll' }, function(json) { + for (var i in json.messages) { var msg = messages[i]; if (msg.id == self.player.id) continue; writeToLog(msg.name + ':', msg.type, msg.message); - $("#log").animate({ scrollTop: $('#log')[0].scrollHeight}, 1000); + $("#log").animate({ scrollTop: $('#log')[0].scrollHeight }, 1000); } + self.populate(json.players); }); } } diff --git a/mud.php b/mud.php index 7306bd0..7dae853 100644 --- a/mud.php +++ b/mud.php @@ -65,7 +65,7 @@ class universe { public $rooms = array(); public function __construct($db) { - $rooms = $db->query("SELECT id,x,y,state FROM rooms"); + $rooms = $db->query('SELECT id,x,y,state FROM rooms'); if ($rooms) { foreach ($rooms as $room) { $this->rooms[$room['x']][$room['y']] = @@ -143,7 +143,7 @@ class mud extends model { 'si', $_GET['name'], $this->universe->rooms[$x][$y]['id']); $_SESSION['id'] = $id; //$others = $this->query('SELECT id, room FROM players WHERE id != ?', 'i', $this->player->id); - return array('x' => $x, 'y' => $y, 'id' => $id, 'name' => $name); + return array('x' => $x, 'y' => $y, 'id' => $id, 'name' => $name, 'poll' => $this->poll()); } private function yell($msg) { @@ -173,8 +173,12 @@ class mud extends model { . ' OR (type = "tell" AND destination = ?)' . ' OR (type = "say" AND messages.room = ?))', 'iii', $time, $this->player->id, $this->player->room); + $players = $this->query( + 'SELECT players.id, name, x ,y ' + . 'FROM players JOIN rooms ' + . 'ON players.room = rooms.id'); $_SESSION['last_polled'] = time(); - return $messages; + return array('messages' => $messages, 'players' => $players); } public function response($content) { -- 2.30.2