Created
October 20, 2010 14:20
-
-
Save pedrofaria/636499 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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