Exemplo n.º 1
0
 def get_lobby_list(self, request):
     if not isinstance(request, GetLobbyListRequest):
         return RpcError(message="Wrong message type, expecting GetLobbyListRequest", error_code=1)
     with db(session):
         userservice = UserServiceClient("tcp://*:5551")
         # Fetch all lobbies and eager load all members where the user is member
         lobbies = Lobby.query.join(Lobby.lobbymemberships).options(joinedload(Lobby.lobbymemberships)) \
         .filter(LobbyMembership.user_id==request.userId).all()
         
         members_list = []
         for lobby in lobbies:
             user_ids = map(lambda i:i.user_id, lobby.lobbymemberships)
             members_list.extend(user_ids)
                   
         users_response = userservice.get_multiple_users(GetMultipleUsersRequest(userIds=members_list), timeout=5000)
         users_dict = {user.id: user for user in users_response.users}
         
         lobbies_list = []
         for lobby in lobbies:
             memb_list=[]
             for member in lobby.lobbymemberships:
                 memb_list.append(ProtoLobbyMembership(user=users_dict.get(member.user_id),status=member.status))
            
             prot_lobby = ProtoLobby(lobbyId=lobby.lobby_id, owner=users_dict.get(lobby.owner_id), lobbymembers=memb_list, gameType=lobby.game_type)                
             lobbies_list.append(prot_lobby)
             
         return GetLobbyListResponse(lobbies=lobbies_list)
Exemplo n.º 2
0
    def get_friends(self, request):
        with db(session):
            userservice = UserServiceClient("tcp://*:5551")

            # TODO: We may also decide if the user fetching should be done
            # by the user service which means that the friendservice calls the userservice over zmq via the client api
            # as planned earlier, but the responsible for this decide.
            friends = Friendship.query.filter(Friendship.user_id == request.userId).all()
            if not friends:
                # we need to return an empty list when friends is empty
                # to avoid to do an IN query with an empty list.
                return GetFriendsResponse(friends=[])
            users = User.query.filter(User.id.in_(map(lambda f: f.friend_id, friends)))
            # print "USERS", users
            friends = map(lambda u: u.id, users)
            # print "friends", friends
            response = userservice.get_multiple_users(GetMultipleUsersRequest(userIds=friends), timeout=5000)
            if isinstance(response, GetMultipleUsersResponse):
                return GetFriendsResponse(friends=response.users)
            else:
                return RpcError(message="Invalid user id")