GameDB

Copyright(c) 2012 Stefano Balietti MIT Licensed

Provides a simple, lightweight NO-SQL database for nodeGame

Entries are stored as GameBit messages.

It automatically creates three indexes.

  1. by player,
  2. by state,
  3. by key.

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 constructor

Creates 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.add

Creates 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

Container of relevant information for the game


A GameBit unit always contains the following properties

  • state GameState
  • player Player
  • key
  • value
  • time

GameBit methods

GameBit constructor

Creates 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.toString

Returns 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 player, state, and key are identical.

If the strict parameter is set, also the value property is used for comparison

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

  • -1: the player id of the second game-bit is larger
  • 1: the player id of the first game-bit is larger
  • 0: the two gamebits belong to the same player
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

  • -1: the key of the first game-bit comes first alphabetically
  • 1: the key of the second game-bit comes first alphabetically
  • 0: the two gamebits have the same key
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

  • -1: the value of the first game-bit comes first alphabetically / numerically
  • 1: the value of the second game-bit comes first alphabetically / numerically
  • 0: the two gamebits have identical value properties
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
);