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)
def main(): uc = UserServiceClient("tcp://*:5551") rr = uc.create_user(RegistrationRequest(email="*****@*****.**", password="******"), timeout=5000) print rr.__class__.__name__ lr = LoginRequest() lr.email = "*****@*****.**" lr.password = "******" response = uc.authenticate(lr, timeout=5000) # timeout on 5 seconds print response.__class__.__name__ lr2 = LoginRequest() lr2.email = "*****@*****.**" lr2.password = "******" response2 = uc.authenticate(lr2, timeout=5000) print response2.__class__.__name__
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")
def get_lobby_state(self, request): if not isinstance(request, GetLobbyStateRequest): return RpcError(message="Wrong message type, expecting GetLobbyStateRequest", error_code=1) with db(session): userservice = UserServiceClient("tcp://*:5551") lobbyQuery = Lobby.query.filter(Lobby.lobby_id==request.lobbyId).first() if not lobbyQuery: return RpcError(message="Lobby does not exists", error_code=2) lobbyMembershipQuery = LobbyMembership.query.filter(LobbyMembership.lobby_id==request.lobbyId).all() lobbyOwner = userservice.get_user(GetUserRequest(userId=lobbyQuery.owner_id), timeout=5000) lobbymemb_list = [] for member in lobbyMembershipQuery: userInLobby = userservice.get_user(GetUserRequest(userId=member.user_id), timeout=5000) lobbym = ProtoLobbyMembership(user=userInLobby.user, status=member.status) lobbymemb_list.append(lobbym) print "the user", userInLobby.user.email print "status", member.status lobby_return = ProtoLobby(lobbyId=request.lobbyId, owner=lobbyOwner.user, lobbymembers=lobbymemb_list, gameType=lobbyQuery.game_type) # Create lobby return GetLobbyStateResponse(lobby=lobby_return)