¶ GameDBCopyright(c) 2012 Stefano Balietti MIT Licensed ¶ Provides a simple, lightweight NO-SQL database for nodeGameEntries are stored as GameBit messages. It automatically creates three indexes.
Uses GameState.compare to compare the state property of each entry. @see GameBit @see GameState.compare |
(function (exports, node) { |
¶ Global scope |
var JSUS = node.JSUS,
NDDB = node.NDDB;
var GameState = node.GameState; |
Inheriting from NDDB |
GameDB.prototype = new NDDB();
GameDB.prototype.constructor = GameDB; |
Expose constructors |
exports.GameDB = GameDB;
exports.GameBit = GameBit; |
¶ GameDB constructorCreates an instance of GameDB Params
options
object
Optional. A configuration object
db
array
Optional. An initial array of items to import into the database
parent
NDDB
GameDB
Optional. A reference to the parent database
@see NDDB constructor
|
function GameDB (options, db, parent) {
options = options || {};
if (!options.update) options.update = {}; |
Auto build indexes by default |
options.update.indexes = true;
NDDB.call(this, options, db, parent);
this.c('state', GameBit.compareState);
if (!this.player) {
this.h('player', function(gb) {
return gb.player;
});
}
if (!this.state) {
this.h('state', function(gb) {
return GameState.toHash(gb.state, 'S.s.r');
});
}
if (!this.key) {
this.h('key', function(gb) {
return gb.key;
});
}
} |
¶ GameDB methods |
|
¶ GameDB.addCreates a GameBit and adds it to the database Params
key
string
An alphanumeric id for the entry
value
mixed
Optional. The value to store
player
Player
Optional. The player associated to the entry. Defaults, node.player
player
GameState
Optional. The state associated to the entry. Defaults, node.game.state
Returns
boolean
TRUE, if insertion was successful
@see GameBit
|
GameDB.prototype.add = function (key, value, player, state) {
if (!key) return false;
state = state || node.game.state;
player = player || node.player;
this.insert(new GameBit({
player: player,
key: key,
value: value,
state: state
}));
return true;
}; |
¶ GameBit methods |
|
¶ GameBit constructorCreates a new instance of GameBit |
function GameBit (options) {
this.state = options.state;
this.player = options.player;
this.key = options.key;
this.value = options.value;
this.time = (Date) ? Date.now() : null;
}; |
¶ GameBit.toStringReturns a string representation of the instance of GameBit Returns
string
string representation of the instance of GameBit
|
GameBit.prototype.toString = function () {
return this.player + ', ' + GameState.stringify(this.state) + ', ' + this.key + ', ' + this.value;
}; |
¶ GameBit.equals (static)Compares two GameBit objects Returns TRUE if the attributes of If the strict parameter is set, also the Params
gb1
GameBit
The first game-bit to compare
gb2
GameBit
The second game-bit to compare
strict
boolean
Optional. If TRUE, compares also the
value property
Returns
boolean
TRUE, if the two objects are equals
@see GameBit.comparePlayer
@see GameBit.compareState
@see GameBit.compareKey
@see GameBit.compareValue
|
GameBit.equals = function (gb1, gb2, strict) {
if (!gb1 || !gb2) return false;
strict = strict || false;
if (GameBit.comparePlayer(gb1, gb2) !== 0) return false;
if (GameBit.compareState(gb1, gb2) !== 0) return false;
if (GameBit.compareKey(gb1, gb2) !== 0) return false;
if (strict && gb1.value && GameBit.compareValue(gb1, gb2) !== 0) return false;
return true;
}; |
¶ GameBit.comparePlayer (static)Sort two game-bits by player numerical id Returns a numerical id that can assume the following values
Params
gb1
GameBit
The first game-bit to compare
gb2
GameBit
The second game-bit to compare
Returns
number
The result of the comparison
|
GameBit.comparePlayer = function (gb1, gb2) {
if (!gb1 && !gb2) return 0;
if (!gb1) return 1;
if (!gb2) return -1;
if (gb1.player === gb2.player) return 0;
if (gb1.player > gb2.player) return 1;
return -1;
}; |
¶ GameBit.compareState (static)Sort two game-bits by their state property GameState.compare is used for comparison Params
gb1
GameBit
The first game-bit to compare
gb2
GameBit
The second game-bit to compare
Returns
number
The result of the comparison
@see GameState.compare
|
GameBit.compareState = function (gb1, gb2) {
return GameState.compare(gb1.state, gb2.state);
}; |
¶ GameBit.compareKey (static)Sort two game-bits by their key property Returns a numerical id that can assume the following values
Params
gb1
GameBit
The first game-bit to compare
gb2
GameBit
The second game-bit to compare
Returns
number
The result of the comparison
|
GameBit.compareKey = function (gb1, gb2) {
if (!gb1 && !gb2) return 0;
if (!gb1) return 1;
if (!gb2) return -1;
if (gb1.key === gb2.key) return 0;
if (gb1.key < gb2.key) return -1;
return 1;
}; |
¶ GameBit.compareValue (static)Sorts two game-bits by their value property Uses JSUS.equals for equality. If they differs, further comparison is performed, but results will be inaccurate for objects. Returns a numerical id that can assume the following values
Params
gb1
GameBit
The first game-bit to compare
gb2
GameBit
The second game-bit to compare
Returns
number
The result of the comparison
@see JSUS.equals
|
GameBit.compareValue = function (gb1, gb2) {
if (!gb1 && !gb2) return 0;
if (!gb1) return 1;
if (!gb2) return -1;
if (JSUS.equals(gb1.value, gb2.value)) return 0;
if (gb1.value > gb2.value) return 1;
return -1;
}; |
¶ Closure |
})(
'undefined' != typeof node ? node : module.exports
, 'undefined' != typeof node ? node : module.parent.exports
);
|