Comparing version 0.0.2 to 0.0.3
@@ -7,17 +7,6 @@ /** | ||
* @license MIT License - See file 'LICENSE' in this project. | ||
* @version 0.0.2 | ||
* @version 0.0.3 | ||
*/ | ||
'use strict'; | ||
/** | ||
* TODO Items: | ||
* | ||
* [x] Win Condition | ||
* [x] Death Condition | ||
* [x] Separation into require'd library | ||
* [ ] Error checking and recovery | ||
*/ | ||
var mongo = require('mongodb').MongoClient; | ||
var natural = require('natural'); | ||
@@ -37,26 +26,24 @@ var i18n = new (require('i18n-2'))({ locales: ['en']}); | ||
* @constructor | ||
* @param {string} connectionString - The MongoDB connection string that we'll | ||
* be connecting to. | ||
*/ | ||
exports = module.exports = advtxt.Server = function(connectionString) { | ||
exports = module.exports = advtxt.Server = function() { | ||
var self = this; | ||
self.mongodb = null; | ||
console.info('Connecting to MongoDB database…'); | ||
// now connect to mongo | ||
mongo.connect(connectionString, function(err, db) { | ||
if (err) throw err; | ||
self.db = null; | ||
self.initialized = false; | ||
} | ||
if (db) { | ||
console.info('Connected.'); | ||
self.mongodb = db; | ||
} | ||
else { | ||
console.error('Connected, but failed to get a database…'); | ||
process.exit(1); | ||
} | ||
}); | ||
} | ||
/** | ||
* Initializes AdvTxt | ||
* | ||
* @since 0.0.3 | ||
* @param {advtxtdb} db - AdvTxt DB adapter. | ||
*/ | ||
advtxt.Server.prototype.initialize = function(db) { | ||
var self = this; | ||
self.db = db; | ||
self.initialized = true; | ||
}; | ||
/** | ||
@@ -136,36 +123,4 @@ * Moves the player's coordinates to another room, and then passes that off to | ||
/** | ||
* Updates something in the database, given the info to do so. | ||
* | ||
* @since 0.0.2 | ||
* @param {string} name - The name of the collection we're updating | ||
* @param {object} selector - The MongoDB selector for what you want to update | ||
* @param {object} data - The data that you want to update, which gets mashed | ||
* into the $set variable of Mongo's update statement. | ||
* @callback {updateDatabaseCallback} next - The callback to be executed after | ||
* the update. | ||
*/ | ||
advtxt.Server.prototype.updateDatabase = function(name, selector, data, next) { | ||
var self = this; | ||
var collection = self.mongodb.collection(name); | ||
collection.update(selector, {$set: data}, {w:1}, function(err, success) { | ||
if (err) throw err; | ||
if (typeof next !== 'undefined' && next) { | ||
next(success); | ||
} | ||
}); | ||
} | ||
/** | ||
* This callback is run after a database entry is updated. | ||
* | ||
* @since 0.0.2 | ||
* @callback updateDatabaseCallback | ||
* @param {number} result - How many records we updated | ||
*/ | ||
/** | ||
* Updates a player's position in the database. | ||
@@ -183,3 +138,3 @@ * | ||
self.updateDatabase('player', selector, data, function(success){ | ||
self.db.update('player', selector, data, function(err, success){ | ||
// if we were successful, lets update their location and send a response | ||
@@ -209,3 +164,3 @@ if (success) { | ||
self.updateDatabase('player', selector, data, function(success) { | ||
self.db.update('player', selector, data, function(err, success) { | ||
// the player is saved, do nothing unless there's an error | ||
@@ -244,3 +199,3 @@ if (!success) { | ||
self.updateDatabase('player', selector, data, function(success) { | ||
self.db.update('player', selector, data, function(err, success) { | ||
if (!success) { | ||
@@ -364,4 +319,3 @@ console.error("Couldn't save player: " + command.player.username); | ||
var roomCollection = self.mongodb.collection('room'); | ||
roomCollection.findOne({x: command.player.x, y: command.player.y, map: command.player.map}, function (err, room) { | ||
self.db.findOne('room', {x: command.player.x, y: command.player.y, map: command.player.map}, function (err, room) { | ||
if (err) throw err; | ||
@@ -407,4 +361,3 @@ | ||
var playerCollection = self.mongodb.collection('player'); | ||
playerCollection.findOne({username: command.player, map: "default"}, function(err, player) { | ||
self.db.findOne('player', {username: command.player, map: "default"}, function(err, player) { | ||
if (err) throw err; | ||
@@ -417,3 +370,3 @@ | ||
else { | ||
playerCollection.insert({ | ||
self.db.insertOne('player', { | ||
username: command.player, | ||
@@ -429,3 +382,3 @@ map: "default", | ||
command.player = player[0]; | ||
command.player = player; | ||
self.getCurrentLocation(command, true); | ||
@@ -432,0 +385,0 @@ }); |
{ | ||
"name": "advtxt", | ||
"version": "0.0.2", | ||
"version": "0.0.3", | ||
"private": false, | ||
@@ -27,3 +27,2 @@ "main": "index", | ||
"dependencies": { | ||
"mongodb": "^1.3.23", | ||
"underscore": "^1.6.0", | ||
@@ -30,0 +29,0 @@ "natural": "^0.1.26", |
AdvTxt | ||
====== | ||
A text adventure engine that uses MongoDB as its backing store. | ||
A text adventure engine. | ||
@@ -16,7 +16,22 @@ Installation | ||
When you set up AdvTxt, you just need to give it a MongoDB connection string. | ||
When you set up AdvTxt, you need to give it a backing store. What follows is an example using the [advtxt-db-mongo][advtxtmongo] module, and the [advtxt-readline](http://github.com/fardog/advtxt-readline) module for I/O. | ||
``` | ||
var yourMongoConnectionUrl = 'mongodb://localhost/advtxt'; | ||
var advtxt = new (require('advtxt'))(yourMongoConnectionUrl); | ||
var config = { | ||
adapter: 'mongodb', | ||
mongodb: { | ||
uri: 'mongodb://localhost/advtxt-test' | ||
} | ||
}; | ||
var advDbMongo = new (require('advtxt-db-mongo'))(); | ||
var advtxt = new (require('advtxt'))(); | ||
var advtxtreadline = require('advtxt-readline'); | ||
advDbMongo.initialize(config, function(err, dbAdapter) { | ||
if (err) throw err; | ||
advtxt.initialize(dbAdapter); | ||
var AdvTxt = new advtxtreadline(advtxt); | ||
}); | ||
``` | ||
@@ -39,2 +54,12 @@ | ||
History | ||
------- | ||
- **v0.0.3** | ||
- Moves MongoDB interface to its own [lightweight adapter][advtxtmongo] | ||
[advtxtmongo]: http://github.com/fardog/advtxt-db-mongo | ||
The MIT License (MIT) | ||
@@ -41,0 +66,0 @@ --------------------- |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
3
86
16302
370
- Removedmongodb@^1.3.23
- Removedabort-controller@3.0.0(transitive)
- Removedbase64-js@1.5.1(transitive)
- Removedbson@0.2.22(transitive)
- Removedbuffer@6.0.3(transitive)
- Removedevent-target-shim@5.0.1(transitive)
- Removedevents@3.3.0(transitive)
- Removedieee754@1.2.1(transitive)
- Removedkerberos@0.0.11(transitive)
- Removedmongodb@1.4.40(transitive)
- Removednan@1.8.4(transitive)
- Removedprocess@0.11.10(transitive)
- Removedreadable-stream@4.5.2(transitive)
- Removedsafe-buffer@5.2.1(transitive)
- Removedstring_decoder@1.3.0(transitive)