¶ BalanceSheet:Copyright(c) 2012 Stefano Balietti MIT Licensed Keeps a record of |
(function(exports, node){ |
¶ Global scope |
exports.BalanceSheet = BalanceSheet;
var JSUS = node.JSUS,
NDDB = node.NDDB,
GameDB = node.GameDB,
GameBit = node.GameBit;
PlayerList = node.PlayerList; |
//Inheriting from NDDB |
var log = console.log;
var isValidSheet = function (sheet) {
if (!sheet) {
log('Cannot add a sheet with no name.', 'ERR');
return false;
}
if (this.sheets[sheet]) {
log('A sheet with the same already exists: ' + sheet);
return false;
}
return true;
} |
¶ BalanceSheet constructorCreates a new instance of BalanceSheet |
function BalanceSheet (options) {
|
¶ Public properties |
this.sheets = {};
this.sheet = null;
log = options.log || log; |
¶ BalanceSheet.optionsReference to current configuration |
this.options = options || {}; |
¶ BalanceSheet methods |
|
¶ BalanceSheet.initConfigures the BalanceSheet instance Takes the configuration as an input parameter or
recycles the settings in The configuration object is of the type var options = { returnAt: 'first', // or 'last' triggers: [ myFunc, myFunc2 ], } Params
options
object
Optional. Configuration object
|
BalanceSheet.prototype.init = function (options) {
this.options = options || this.options;
if (this.options.returnAt === BalanceSheet.first || this.options.returnAt === BalanceSheet.last) {
this.returnAt = this.options.returnAt;
}
this.resetTriggers();
}; |
¶ BalanceSheet.addSheetAdds a new sheet and sets it as default Params
sheet
string
The sheet name
options.
object
Optional. Configuration options for the sheet
items.
array
Optional. An initial set of items for the sheet
Returns
boolean
TRUE, if the sheet is added successfully
BalanceSheet.prototype.addSheet = function (sheet, pl, options) { if (!isValidSheet(sheet)) return false; pl = pl || new PlayerList(); this.sheets[sheet] = pl; if (!this.initSheet(sheet, options)) { return false; } this.sheet = sheet; return true; }; |
|
BalanceSheet.prototype.initSheet = function (sheet, options) { if (!isValidSheet(sheet)) return false; this.sheets[sheet].each(function(p){ if (!p.balance) { p.balance = 0; } }); }; |
BalanceSheet.prototype.updateBalance = function (player, amount) {
if (!player || !amount) return;
if (!this.sheet) {
log('No balance sheet selected');
return;
}
if (!this.sheet.player)
return
}
this.sheet.players[player].__balance += amount;
};
BalanceSheet.prototype.report = function (sheet) {
if (!isValidSheet(sheet)) return false;
return this.sheets[sheet].keep(['__balance']);
}; |
¶ BalanceSheet.clearClears the trigger array Requires a boolean parameter to be passed for confirmation Params
clear
boolean
TRUE, to confirm clearing
Returns
boolean
TRUE, if clearing was successful
|
BalanceSheet.prototype.clear = function (clear) {
if (!clear) {
node.log('Do you really want to clear the current BalanceSheet obj? Please use clear(true)', 'WARN');
return false;
}
triggersArray = [];
return clear;
};
|
¶ Sheet |
|
Inheriting from NDDB |
Sheet.prototype = JSUS.clone(NDDB.prototype);
Sheet.prototype.constructor = Sheet;
function Sheet(options, db, parent) {
options = options || {};
if (!options.log) options.log = node.log;
NDDB.call(this, options, db, parent);
this.name;
this.pl;
} |
¶ 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
|
Sheet.prototype.add = function (player, value, state, key) {
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;
};
Sheet.prototype.init = function(options) {
NDDB.prototype.init.call(this, options);
options = options || this.options;
}; |
¶ Closure |
})(
('undefined' !== typeof node) ? node : module.exports
, ('undefined' !== typeof node) ? node : module.parent.exports
);
|