def answer_lobby_invite(self, request): if not isinstance(request, AnswerLobbyInviteRequest): return RpcError(message="Wrong message type, expecting AnswerLobbyInviteRequest", error_code=1) with db(session): query_type = Lobby.query.filter(Lobby.lobby_id==request.lobbyId).first() if not query_type: return RpcError(message="Lobby does not exists", error_code=2) accepted_count = LobbyMembership.query.filter(LobbyMembership.lobby_id==request.lobbyId).filter(LobbyMembership.status=="member").count() user_lobby = LobbyMembership.query.filter(LobbyMembership.lobby_id==request.lobbyId).filter(LobbyMembership.user_id==request.userId).first() if not user_lobby: return RpcError(message="Not permitted to the lobby", error_code=30) if(request.answer == "accept"): if(query_type.game_type >= accepted_count + 1): session.delete(user_lobby) session.add(LobbyMembership(lobby_id=user_lobby.lobby_id, status="member", user_id=user_lobby.user_id)) session.commit() return AnswerLobbyInviteResponse(answer=True) else: session.delete(user_lobby) session.commit() return RpcError(message="Lobby is full", error_code=31) elif(request.answer == "deny"): try: session.delete(user_lobby) session.commit() return AnswerLobbyInviteResponse(answer=True) except Exception as e: return AnswerLobbyInviteResponse(answer=False) return RpcError(message="Unknown answer should be accept or deny", error_code=3)
def add_friend(self, request): print "in add_Friend" with db(session): try: friend = User.query.filter(User.email == request.friend_email).first() friendship = Friendship(user_id=request.userId, friend_id=friend.id) session.add(friendship) session.commit() return AddFriendResponse(friend_added=True) except Exception as e: session.rollback() return AddFriendResponse(friend_added=False)
def create_lobby(self, request): if not isinstance(request, CreateLobbyRequest): return RpcError(message="Wrong message type, expecting CreateLobbyRequest", error_code=1) lobby = Lobby(game_type = request.gameType, owner_id = request.userId) member = LobbyMembership( status="member", user_id=request.userId ) lobby.lobbymemberships.append(member) session.add(lobby) session.commit() return CreateLobbyResponse(lobbyId=lobby.lobby_id)
def invite_to_lobby(self, request): if not isinstance(request, InviteLobbyRequest): return RpcError(message="Wrong message type, expecting InviteLobbyRequest", error_code=1) if not request.invites: return InviteLobbyResponse(friends_invited=False) with db(session): lobby = Lobby.query.filter(Lobby.lobby_id==request.lobbyId).filter(Lobby.owner_id==request.userId).first() if(lobby==None): return RpcError(message="No permission to manage the lobby", error_code=35) # Clearing out duplicates from invited list users = User.query.filter(User.id.in_(request.invites)).all() users = set(users) uids = map(lambda u:u.id, users) # Check to make sure the lobby owner does not get invited for user in users: if(user.id == request.userId): users.remove(user) break # Check and remove invited users who has previously been invited already_invited = LobbyMembership.query.filter(LobbyMembership.user_id.in_(uids)).filter(LobbyMembership.lobby_id==request.lobbyId).all() for invited in already_invited: for person in users: if(invited.user_id==person.id): users.remove(person) break print users if(users == set([])): return InviteLobbyResponse(friends_invited=False) else: for invited in users: lobby_membership = LobbyMembership(lobby_id = request.lobbyId, status = "invited" , user_id = invited.id) session.add(lobby_membership) session.commit() return InviteLobbyResponse(friends_invited=True)
def create_user(self, request): if not isinstance(request, RegistrationRequest): return RpcError(message = "Wrong message type, expecting RegistrationRequest", error_code=1) # TODO: add more logic before insert the user to db if None in (request.email, request.password): return RpcError(message="Both email and password requires to be set", error_code=10) try: user = User( email = request.email, password = request.password ) session.add(user) session.commit() return RegistrationResponse(userId=user.id) except IntegrityError as e: session.rollback() return RpcError(message="E-mail already used", error_code=11) except Exception as e: session.rollback() return RpcError(message=e.message) # TODO: improve this