¶ GameMsgCopyright(c) 2012 Stefano Balietti MIT Licensed
|
(function (exports, node) { |
¶ Global scope |
var GameState = node.GameState,
JSUS = node.JSUS;
exports.GameMsg = GameMsg; |
¶ GameMSg.clone (static)Returns a perfect copy of a game-message Params
gameMsg
GameMsg
The message to clone
Returns
GameMsg
The cloned messaged
@see JSUS.clone
|
GameMsg.clone = function (gameMsg) {
return new GameMsg(gameMsg);
}; |
¶ GameMsg constructorCreates an instance of GameMsg |
function GameMsg (gm) {
gm = gm || {};
|
¶ Private properties |
|
var id = gm.id || Math.floor(Math.random()*1000000);
if (node.support.defineProperty) {
Object.defineProperty(this, 'id', {
value: id,
enumerable: true
});
}
else {
this.id = id;
} |
|
var session = gm.session;
if (node.support.defineProperty) {
Object.defineProperty(this, 'session', {
value: session,
enumerable: true
});
}
else {
this.session = session;
} |
|
¶ Public properties |
|
¶ GameMsg.stateThe game-state in which the message was generated @see GameState |
this.state = gm.state; |
¶ GameMsg.actionThe action of the message @see node.action |
this.action = gm.action;
|
¶ GameMsg.targetThe target of the message @see node.target |
this.target = gm.target;
|
¶ GameMsg.fromThe id of the sender of the message @see Player.id |
this.from = gm.from; |
¶ GameMsg.toThe id of the receiver of the message @see Player.id @see node.player.id |
this.to = gm.to; |
¶ GameMsg.textAn optional text adding a description for the message |
this.text = gm.text;
|
¶ GameMsg.dataAn optional payload field for the message |
this.data = gm.data;
|
¶ GameMsg.priorityA priority index associated to the message |
this.priority = gm.priority;
|
¶ GameMsg.reliableExperimental. Disabled for the moment If set, requires ackwnoledgment of delivery |
this.reliable = gm.reliable; |
¶ GameMsg.createdA timestamp of the date of creation |
this.created = JSUS.getDate();
|
¶ GameMsg.forwardIf TRUE, the message is a forward. E.g. between nodeGame servers |
this.forward = 0;
}; |
¶ GameMsg.stringifyCalls JSON.stringify on the message Returns
string
The stringified game-message
@see GameMsg.toString
|
GameMsg.prototype.stringify = function () {
return JSON.stringify(this);
}; |
¶ GameMsg.toStringCreates a human readable string representation of the message Returns
string
The string representation of the message
@see GameMsg.stringify
|
GameMsg.prototype.toString = function () {
var SPT = ",\t";
var SPTend = "\n";
var DLM = "\"";
var gs = new GameState(this.state);
var line = this.created + SPT;
line += this.id + SPT;
line += this.session + SPT;
line += this.action + SPT;
line += this.target + SPT;
line += this.from + SPT;
line += this.to + SPT;
line += DLM + this.text + DLM + SPT;
line += DLM + this.data + DLM + SPT; // maybe to remove
line += this.reliable + SPT;
line += this.priority + SPTend;
return line;
}; |
¶ GameMSg.toSMSCreates a compact visualization of the most important properties Returns
string
A compact string representing the message
|
GameMsg.prototype.toSMS = function () {
var parseDate = /\w+/; // Select the second word;
var results = parseDate.exec(this.created);
var line = '[' + this.from + ']->[' + this.to + ']\t';
line += '|' + this.action + '.' + this.target + '|'+ '\t';
line += ' ' + this.text + ' ';
return line;
}; |
¶ GameMsg.toInEventHashes the action and target properties of an incoming message Returns
string
The hash string
@see GameMsg.toEvent
|
GameMsg.prototype.toInEvent = function() {
return 'in.' + this.toEvent();
}; |
¶ GameMsg.toOutEventHashes the action and target properties of an outgoing message Returns
string
The hash string
@see GameMsg.toEvent
|
GameMsg.prototype.toOutEvent = function() {
return 'out.' + this.toEvent();
}; |
¶ GameMsg.toEventHashes the action and target properties of the message Returns
string
The hash string
|
GameMsg.prototype.toEvent = function () {
return this.action + '.' + this.target;
}; |
¶ Closure |
})(
'undefined' != typeof node ? node : module.exports
, 'undefined' != typeof node ? node : module.parent.exports
);
|