def logoutUser(self, user): """Logout the user and broadcast to all other users""" # Get the state and id channelId = self.channelId jsonState = self.state level = Constants.levelFromExperience(user) del jsonState.state[user.alias] # The key value pair addMessage = {"LOGOUT": {'alias' : user.alias, 'level' : level }} # Login in the user on all of the channels for alias, _ in jsonState.state.iteritems(): # Everyone except the user logging in if user.alias != alias: channel.send_message(channelId + alias, simplejson.dumps(addMessage)) # Save the object self.state.put() # Cache client = memcache.Client() if not client.replace("users", self.state): if not client.set("users", self.state): logging.error("Memcache failed in UserManager")
def loginUser(self, user): """Add a user to the user list""" # Get the state and id channelId = self.channelId level = Constants.levelFromExperience(user) jsonState = self.state jsonState.state[user.alias] = level # The key value pair addMessage = {"LOGIN": {'alias' : user.alias, 'level' : level }} # Login in the user on all of the channels for alias, _ in jsonState.state.iteritems(): # Everyone except the user logging in if user.alias != alias: channel.send_message(channelId + alias, simplejson.dumps(addMessage)) # Send the current state to the addMessage allState = {'LOGIN' : [{'alias': alias, 'level':level} for alias, level in jsonState.state.iteritems()]} message = simplejson.dumps(allState) channel.send_message(channelId + user.alias,message) # Save the object self.state.put() # Cache client = memcache.Client() if not client.replace("users", self.state): if not client.set("users", self.state): logging.error("Memcache failed in UserManager")
def getAllActiveUsers(self): """Return all active users""" return ArrayCollection([{'alias': u.alias, 'level' : Constants.levelFromExperience(u)} for u in User.all().filter('isLoggedIn =', True).fetch(1000)])