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++;
}
}
var self = this;
this.rooms = [];
+ this.players = [];
this.build = function(seed) {
this.rows = seed.length;
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);
});
}
$.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);
});
}
}
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']] =
'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) {
. ' 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) {