Skip to content

Instantly share code, notes, and snippets.

@pedrofaria
Created October 20, 2010 14:20
Show Gist options
  • Save pedrofaria/636499 to your computer and use it in GitHub Desktop.
Save pedrofaria/636499 to your computer and use it in GitHub Desktop.
/**
* iCards Object
*/
var iCards = exports.iCards = function () {
var self = this;
this.version = '1.0';
// Valid commands
this._cmds = {
register_game: {
type: 'string'
},
register_player: {
site_id: 'string',
name: 'string',
},
start_game: {},
};
// Games Database
this.games = {};
}
iCards.prototype.parse = function (socket, data) {
try {
var cmd = JSON.parse(data);
this.validate(cmd);
// execute
// send back all data
}
catch (err) {
console.log(err);
socket.write('Unkown command... you suck!\r\n');
}
}
iCards.prototype.validate = function (command) {
// validate basic info
this.validateKey(command, 'c', 'string'); // command
this.validateKey(command, 'g'); // game key / can be null in some commands.
this.validateKey(command, 'd', 'object'); // data
// validate server commands
if (command['c'] in this._cmds) {
for (var c in this._cmds[commmand['c']]) {
this.validateKey(command['d'], c, this._cmds[commmand['c']][c]);
}
return true;
}
// validate game commands
this.validateKey(this.games, command['g'], 'object');
throw "iCards_Ex_Cmd_Unknow";
}
iCards.prototype.validateKey = function (command, key, type) {
if (! key in command || ( type && !typeof command[key] == type)) {
throw "iCards_Ex_Cmd_Bad";
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment