| 
									
										
										
										
											2013-04-22 16:51:32 +00:00
										 |  |  | var RDB = require('../RedisDB.js'); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | (function(User) { | 
					
						
							|  |  |  | 	var current_uid; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	User.create = function(username, password) { | 
					
						
							|  |  |  | 		if (current_uid) { | 
					
						
							|  |  |  | 			global.socket.emit('user.create', {'status': 0, 'message': 'Only anonymous users can register a new account.'}); | 
					
						
							|  |  |  | 			return; | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		if (username == null || password == null) { | 
					
						
							|  |  |  | 			global.socket.emit('user.create', {'status': 0, 'message': 'Missing fields'}); | 
					
						
							|  |  |  | 			return; | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		User.exists(username, function(exists) { | 
					
						
							|  |  |  | 			if (exists) { | 
					
						
							|  |  |  | 				return; | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			RDB.incr('global:next_user_id', function(uid) { | 
					
						
							|  |  |  | 				RDB.set('username:' + username + ':uid', uid); | 
					
						
							|  |  |  | 				RDB.set('uid:' + uid + ':username', username); | 
					
						
							|  |  |  | 				RDB.set('uid:' + uid + ':password', password); | 
					
						
							|  |  |  | 				 | 
					
						
							|  |  |  | 				RDB.incr('user:count', function(count) { | 
					
						
							| 
									
										
										
										
											2013-04-22 17:09:40 +00:00
										 |  |  | 					global.io.sockets.emit('user.count', {count: count}); | 
					
						
							| 
									
										
										
										
											2013-04-22 16:51:32 +00:00
										 |  |  | 				}); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 				RDB.lpush('user:users', username); | 
					
						
							| 
									
										
										
										
											2013-04-22 17:09:40 +00:00
										 |  |  | 				global.io.sockets.emit('user.latest', {username: username}); | 
					
						
							| 
									
										
										
										
											2013-04-22 16:51:32 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 				global.socket.emit('user.create', {'status': 1}); | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 	}; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	User.exists = function(username, callback) { | 
					
						
							|  |  |  | 		User.get_uid_by_username(username, function(exists) { | 
					
						
							|  |  |  | 			exists = !!exists; | 
					
						
							|  |  |  | 			global.socket.emit('user.exists', {exists: exists}) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			if (callback) { | 
					
						
							|  |  |  | 				callback(exists); | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 	}; | 
					
						
							|  |  |  | 	User.count = function() { | 
					
						
							|  |  |  | 		RDB.get('user:count', function(count) { | 
					
						
							|  |  |  | 			global.socket.emit('user.count', {count: (count === null) ? 0 : count}); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 	}; | 
					
						
							|  |  |  | 	User.latest = function() { | 
					
						
							|  |  |  | 		RDB.lrange('user:users', 0, 0, function(username) { | 
					
						
							|  |  |  | 			global.socket.emit('user.latest', {username: username}); | 
					
						
							|  |  |  | 		});	 | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	User.get_uid_by_username = function(username, callback) { | 
					
						
							|  |  |  | 		RDB.get('username:' + username + ':uid', callback); | 
					
						
							|  |  |  | 	}; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | }(exports)); |