def login(self): """ Login erquested from within game client """ nm = self.get_post('nm', '!').lower() pw = self.get_post('pw', '!') self.out = '0' self.a_user = None try: a = auth.get_auth() self.a_user = a.get_user_by_password("own:" + nm, pw, remember=True, save_session=True) except auth.InvalidAuthIdError: self.out = '0Invalid E-mail' except auth.InvalidPasswordError: self.out = '0Invalid Password' # user found and password checked out if self.a_user: self.out = '0Server error. Please try again in a few minutes.' self.user_token = UserToken.get(user=self.a_user['user_id'], subject='auth', token=self.a_user['token']) if not self.user_token: self.out = '0Server error 1. Please try again in a few minutes.' self.user = User.get_by_id(self.a_user['user_id']) if not self.user: self.out = '0Server error 2. Please try again in a few minutes.' if self.user and self.user_token: uid = int(self.user.get_id()) # update the token now = int(time.time()) self.user_token.uid = uid self.user_token.plrname = self.user.name self.user_token.location = 'L' # in lobby self.user_token.last_game_refresh = now self.user_token.last_chat_refresh = now self.user_token.put(); self.out = '1' # send the auth cookie in the body too just incase i cant read it from header self.out += 'auth='+self.session_store.serializer.serialize('auth', a.session) # output player name self.out += '^' + self.user.name + '^' # tell client which parts of game the player own #for s in self.user.shop.values(): # self.out += s self.out += ''.join(self.user.shop) + '^' # send settings self.out += '1^' if self.user.email_bmchallenge else '0^' self.respond()
def post(self): if not self.auth(): self.respond('!') # not authenticated return name = self.get_post('u', '') channel_ids = self.get_post('n', '') self.out = '0' #default is error # player wants to join/create a private channel with another player if name: # find the player's uid since I work with UIDs and not names q = User.query(User.name_lower == name.lower()) user = q.get() if not user: self.respond('0'); return # not found uid = int(user.get_id()) if uid == self.uid: self.respond('0'); return # cant PM self # first check if the channel between the two players dont exist, else create it # check for channel(s) where this user and target is in the uids and it is pm_channel q = ChatChannel.query(ChatChannel.pm_channel == True, ChatChannel.uids == uid, ChatChannel.uids == self.uid) channel = q.get() # get first result if channel: # there is a channel, update its created date so it is fresh and then send it to player channel.created = datetime.datetime.now() channel.put() self.out = '1' + str(channel.key.id()) else: # first check if requested user exist target = User.get_by_id(uid) # create the channel if target: channel = ChatChannel(pm_channel = True, uids = [uid, self.uid] ) key = channel.put() if key: self.out = '1' + str(key.id()) # player wants the names for channels elif channel_ids: sids = channel_ids.split(',') cids = [int(s) for s in sids if s] channels = ChatChannel.get_multi_by_id(cids) if cids else None if channels: self.out = '1' for channel in channels: # send the name as is if the channel has one if channel.name: self.out += '|' + str(channel.key.id()) + ',' + channel.name # else try and find the name it should be, prolly a player name for pm channel else: uid = 0 # find the other player's id in the list for idd in channel.uids: if idd != self.uid: uid = idd break if uid > 0: player = User.get_by_id(uid) if player: self.out += '|' + str(channel.key.id()) + ',' + player.name self.respond()