def getUserFriendsWithUserAndPage(self, user, page): """ 获取用户好友 """ retFriends = [] friends = user.getAllFriends() friendCount = len(friends) if friends and friendCount > 0: nStart = 0 nEnd = 0 #计算页码是否在范围内 if USERS_PAGES_SIZE * page < friendCount: #计算结束页码 if USERS_PAGES_SIZE * (page + 1) > friendCount: nEnd = friendCount - USERS_PAGES_SIZE * page else: nEnd = USERS_PAGES_SIZE * page #在页码范围内的好友 friends = friends[nStart * USERS_PAGES_SIZE:nEnd] for friend in friends: online = False if friend.connection: online = True retUser = SendToClientPackageUser( friend.DBUser.uid, friend.DBUser.username, friend.DBUser.sex, friend.DBUser.description, online) retFriends.append(retUser) return retFriends
def handleDeleteFriend(self , connection , package): """ 删除好友 """ user = self.onlineUsers.getUserByConnection(connection) retPackage = SendToClientPackage('delfriend') #自己的id if user.DBUser.uid == int(package.uid) and user.DBUser.uid != int(package.fid): retPackage.status = 1 #从数据库中删除 self.dbEngine.deleteFriendshipByUserAndFriendId( package.uid, package.fid ) user.connection.send_message( json.dumps( retPackage , cls=ComplexEncoder ) ) #给在线好友发送通知,删除 online_friend = self.onlineUsers.getUserExistByUserid( package.fid ) if online_friend: sendObj = SendToClientPackageUser(user.DBUser.uid, user.DBUser.username, user.DBUser.sex, user.DBUser.description) retPackage.obj = sendObj online_friend.connection.send_message( json.dumps( retPackage , cls=ComplexEncoder ) ) #从维护的好友列表中删除 user.deleteFriend(online_friend) else: retPackage.errcode = PACKAGE_ERRCODE_USERID user.connection.send_message( json.dumps( retPackage , cls= ComplexEncoder ) )
def handleUserLogin(self, connection , package): """ 用户登录处理 """ retPackage = SendToClientPackage('login') #step 1,检查参数合法性 if self.findBadInput(package.username) or self.findBadInput(package.password): retPackage.errcode = PACKAGE_ERRCODE_INPUTWRONG else: #step 2. 查询数据库 db_user = self.dbEngine.isUserExist(package.username, package.password) if db_user: #step 1. 枚举在线好友,如果在线,退掉 online_user = self.onlineUsers.getUserExistByUsername(package.username) if online_user: #step 1.发送异地登录消息 another = SendToClientPackage('anotherlogin') another.status = 1 online_user.connection.send_message( json.dumps( another , cls= ComplexEncoder ) ) #step 2.关闭联接 online_user.connection.close() #从新加入到在线用户 user = UserObject(connection, db_user) self.onlineUsers.addNewOnlineUser(user) retPackage.status = 1 retPackage.obj = SendToClientPackageUser( user.DBUser.uid, user.DBUser.username, user.DBUser.sex, user.DBUser.description) #加载好友列表 self.getUserFriendsWithDBAndOnLineUsers( user ) #检查离线消息,是否有人希望添加我为好友 self.getAllAddFriendRequestFromDBAndSendToClient( user ) #是否有人给我发离线消息 self.getOfflineChatMessageAndSendWithUser( user ) #广播好友列表,通知本人上线 self.broadcastOnlineStatusToAllFriend( user , 1 ) #修改在线列表,本人上线 self.setUserOnlineInOnlineUsersFriends( user ) else: #用户不存在,提醒注册 retPackage.errcode = 10010 connection.send_message( json.dumps(retPackage, cls=ComplexEncoder) )
def getNotFriendsWithCodeAndDateAndPage(self , user , traincode, date , page): """ 根据列车或者航班+日期获取的好友 """ friends = self.dbEngine.getNotfriendsWithCodeAndDate(traincode, date) ret_friends = [] nCount = 0 if friends: for friend in friends: #不是自己 if friend.userid != user.DBUser.uid: #计算页码 if (nCount / 20 == page): db_friend = self.dbEngine.getUserInfoWithUserId(friend.userid) ret_friend = SendToClientPackageUser(db_friend.uid, db_friend.username, db_friend.sex, db_friend.description) ret_friends.append(ret_friend) nCount += 1 return ret_friends