UNPKG

casino-server

Version:

An multi-rule scalable online poker game server powered by redis, node.js and socket.io

249 lines (215 loc) 4.83 kB
exports = module.exports = Client; function Client( socket ) { this.uid = null; this.pin = null; this.profile = {}; this.events = {}; this.room = null; this.cmds = {}; this.setUplink( socket ); } Client.prototype.setUplink = function(socket) { var client = this; client.uplink = socket; if(! socket.gamers) { socket.gamers = {}; socket.gamers_count = 0; socket.rpc_seq = 0; socket.rpc_callbacks = {}; socket.on('notify', function( msg ){ // { uid:x, e:xx, args:xxx } if(socket.log_traffic) console.log('notify', msg); if(! msg) return; if(typeof msg !== 'object') return; var event = msg.e; if(! event) return; var args = msg.args; var target; if(msg.uid) { target = socket.gamers[ msg.uid ]; if(target) target.onNotify( event, args ); } else { var gamers = socket.gamers; for(var uid in gamers) { target = gamers[ uid ]; if(target) target.onNotify( event, args ); } } }); socket.on('rpc_ret', function(reply){ if(socket.log_traffic) console.log('rpc_ret', reply); if(reply && reply.seq) { var seq = reply.seq, err = reply.err, ret = reply.ret; var callback = socket.rpc_callbacks[ seq ]; if(callback) { if(! err) { if(ret && ret.cmds) { client.filterCmds( ret.cmds ); } } var func = callback.func; if(typeof func === 'function') func(err, ret); delete socket.rpc_callbacks[ seq ]; } } }); } client.uid = ++ socket.rpc_seq; socket.gamers[ client.uid ] = client; return this; }; Client.prototype.on = function(event, func) { this.events[ event ] = func; return this; }; Client.prototype.filterCmds = function(cmds) { for(var i in cmds) { if(cmds[i] !== null) this.cmds[i] = 1; } var login = cmds.login; var signup = cmds.signup; var fastsignup = cmds.fastsignup; if(login) { for(i in this.cmds) { cmds[i] = null; } cmds.login = login; if(signup) cmds.signup = signup; if(fastsignup) cmds.fastsignup = fastsignup; } for(i in this.cmds){ if(this.cmds[i] === null) delete this.cmds[i]; } }; Client.prototype.onNotify = function(event, args) { switch(event) { case 'prompt': this.filterCmds(args); break; case 'look': this.room = args; break; case 'enter': this.room.gamers[ args.who.uid ] = args.who; break; case 'leave': args.who = this.room.gamers[ args.uid ]; break; case 'takeseat': this.room.seats[ args.where ] = args.uid; args.who = this.room.gamers[ args.uid ]; break; case 'unseat': this.room.seats[ args.where ] = null; args.who = this.room.gamers[ args.uid ]; break; case 'say': args.who = this.room.gamers[ args.uid ]; break; case 'refresh': var uid = args.uid; if(uid === this.uid) { this.profile = args.profile; } var room = this.room; if(room && (uid in room.gamers)) { room.gamers[ uid ] = args.profile; } } var func; if(typeof (func = this.events[event]) === 'function') { func(args); } else if(typeof (func = this.events['else']) === 'function') { func(args); } switch(event) { case 'bye': this.pin = null; this.profile = {}; this.room = null; this.cmds = {}; break; } }; Client.prototype.removeUplink = function() { var socket = client.uplink; if(socket) { client.socket = null; delete socket.gamers[ this.uid ]; socket.gamers_count --; } return this; }; /* * accepted methods and args: * * signup, {uid, passwd, name, email, phone, uuid} * login, {uid, passwd} * logout, 0 * * games, 0 * rooms, gameid * shout, msg * entergame, gameid * enter, roomid * say, msg * look, 0 * takeseat, seat or '' * unseat, 0 * leave, 0 * * follow, 0 * addchip, n * giveup, 0 * pk, uid * checkcard, 0 * showcard, 0 * */ Client.prototype.rpc = function(method, args, func) { var client = this; var socket = client.uplink; if(typeof func !== 'function') { throw 'need a callback func(err,ret)'; } var callback_func = func; switch(method) { case 'fastsignup': case 'signup': break; case 'login': callback_func = function(err, ret){ if(! err) { if(client.uid !== ret.token.uid) { delete socket.gamers[ client.uid ]; } client.uid = ret.token.uid; client.pin = ret.token.pin; client.profile = ret.profile; socket.gamers[ client.uid ] = client; } func(err, ret); }; break; default: if(! client.pin) { func(400, 'need login first'); return this; } } var callback = { seq: ++ socket.rpc_seq, func: callback_func, t: Date.now() }; socket.rpc_callbacks[ callback.seq ] = callback; var req = { seq: callback.seq, uid: client.uid, pin: client.pin, f: method, args: args }; socket.emit('rpc', req); if(socket.log_traffic) console.log('rpc', req); return this; };