var net = require('net');
var fs = require('fs');
var Connection = require('./connection');
var MongoClient = require('mongodb').MongoClient;
var async = require('async');

var NickMOO = function(config) {
  this.config = config;
  this.messages = {}; // Static messages that are displayed to the user

  this.server = null; // No TCP server active atm
  this.db = null; // DB not connected yet
  this.connections = [];
}

NickMOO.NAME    = 'NickMOO';
NickMOO.VERSION = '0.0.1';

// Start listening for connectionks
NickMOO.prototype.init = function() {
  this.log(NickMOO.NAME + ' version ' + NickMOO.VERSION + ' starting...');

  // Use async to make sure the DB is connected and initialized first before
  // the TCP server starts accepting
  async.series([
    // Load messages
    function(callback) {
      this.log('loading messages...');
      fs.readdir('messages', function(err, files) {
        if(err) throw err;
        async.each(files, function(file, callback) {
          this.log('  ' + file);
          fs.readFile('messages/' + file, function(err, data) {
            if(err) { this.log('  failed to read ' + file); return callback(); }
            this.messages[file] = data;
            callback();
          }.bind(this));
        }.bind(this), callback);
      }.bind(this));
    }.bind(this),

    // Connect to database
    function(callback) {
      // Attempt to connect to the database
      this.log('connecting to database...');
      MongoClient.connect(this.config.dbUri, function(err, db) {
        if(err) throw err;
        this.db = db;
        this.log('DB connection successful');
        callback();
      }.bind(this));
    }.bind(this),

    // Set up socket and listen
    function(callback) {
      this.server = net.createServer(function(sock) {
        this.log('accepted connection from ' + sock.address().address + ':' + sock.address().port);

        var connection = new Connection(this, sock);
        this.connections.push(connection); // Add this connection to the connection array
        connection.init(); // Initialize the connection
      }.bind(this));

      this.server.listen(this.config.port, function() {
        this.log('server listening on ' + this.config.port);
      }.bind(this));

      callback();
    }.bind(this)
  ]);
}

// Safely stop the server
NickMOO.prototype.deinit = function() {

}

// Logging function
NickMOO.prototype.log = function(str) {
  console.log('[' + (new Date).toString().split(' ').splice(0, 5).join(' ') + '] ' + str);
}

module.exports = NickMOO;

@@ -388,16 +388,41 @@
 ted yet%0A
+  this.dbObjects = null;%0A
   this.c
@@ -1693,24 +1693,382 @@
 ind(this),%0A%0A
+    // Connect to the object collection%0A    function(callback) %7B%0A      this.log('connecting to objects collection...');%0A      this.db.collection('objects', function(err, collection) %7B%0A        if(err) throw err;%0A        this.dbObjects = collection;%0A        this.log('objects collection connection successful');%0A        callback();%0A      %7D)%0A    %7D.bind(this),%0A%0A
     // Set u

connect to objects collection