예제 #1
0
    def get(self):
        params = request.args.to_dict()

        session = get_session(params['token'])
        user_id = session['user']

        if user_id:
            user = get_user_by_id(user_id)
            if not user:
                return {"response": "User not found"}, 404

        gi = ObjectId(params["game_instance"])
        gi = get_game_instance(gi)

        letter = "X"
        if gi["user2"] == user["_id"]:
            letter = "O"

        _winner = None
        if gi["winner"] != None:
            _winner = get_user_by_id(gi["winner"])["alias"]

        return {
            "response": {
                "cstate": gi["cstate"],
                "letter": letter,
                "winner": _winner
            }
        }
예제 #2
0
    def put(self):
        params = request.args.to_dict()

        session = get_session(params['token'])
        user_id = session['user']

        if user_id:
            requested_user = get_user_by_id(user_id)
            if not requested_user:
                return {"response": "User not found"}, 404

        payload = request.json

        try:
            userid = payload["userid"]
        except:
            return {"response": "Bad request"}, 400

        requesting_user = get_user_by_id(userid)
        if not requesting_user:
            return {"response": "Requested User not found"}, 404

        user1, user2 = requesting_user["_id"], requested_user["_id"]
        gi = get_pending_request(user1, user2)

        accepted_game_instance(gi["_id"])

        return {"response": single_game_instance(gi)}
예제 #3
0
    def post(self):
        params = request.args.to_dict()

        session = get_session(params['token'])
        user_id = session['user']

        if user_id:
            requesting_user = get_user_by_id(user_id)
            if not requesting_user:
                return {"response": "User not found"}, 404

        payload = request.json

        try:
            email = payload['email']
        except:
            return {"response": "Bad request"}, 400

        requested_user = get_user_by_mail(email)
        if not requested_user:
            return {"response": "Requested User not found"}, 404

        user1, user2 = requesting_user["_id"], requested_user["_id"]

        if get_pending_request(user1, user2):
            return {"response": "Already requested for game instance"}

        gi = create_game_instance(user1, user2)
        return {"response": str(gi)}
예제 #4
0
    def get(self):
        params = request.args.to_dict()

        session = get_session(params['token'])
        user_id = session['user']

        if user_id:
            user = get_user_by_id(user_id)
            if not user:
                return {"response": "User not found"}, 404

        gi_pending = get_game_invitations_to_user(user_id)
        gi_pending = multiple_game_instance(gi_pending)

        gi_inaction = get_inactive_game_instances(user_id)
        gi_inaction = multiple_game_instance(gi_inaction)

        gi_completed = get_completed_game_instances(user_id)
        gi_completed = multiple_game_instance(gi_completed)
        return {
            "response": {
                "pending": add_buttonValue_to_game_instance(gi_pending, user),
                "inaction":
                add_buttonValue_to_game_instance(gi_inaction, user),
                "completed":
                add_buttonValue_to_game_instance(gi_completed, user)
            }
        }
예제 #5
0
    def get(self, user_id=None):
        params = request.args.to_dict()

        session = get_session(params['token'])
        current_user = session['user']

        print "\nCURRENT USER : "******"\n\n"

        if user_id:
            user = get_user_by_id(user_id)

            if not user:
                return {"response": "User not found"}, 404
            return {"response": userview.single(user)}

        users = get_all_user()
        return {"response": userview.multiple(users)}
예제 #6
0
def add_buttonValue_to_game_instance(gi, user):
    for obj in gi:
        opposition_user = obj["user1"]
        if ObjectId(obj["user2"]) != user["_id"]:
            opposition_user = obj["user2"]
        user_alias = get_user_by_id(opposition_user)
        obj["buttonValue"] = user_alias["alias"]
    return gi
예제 #7
0
    def get(self, user_id = None):
        params = request.args.to_dict()
        
        session = get_session(params['token'])
        current_user = session['user']
        
        print "\nCURRENT USER : "******"\n\n"

        if user_id:
            user = get_user_by_id(user_id)

            if not user:
                return {"response" : "User not found"}, 404  
            return {"response" : userview.single(user) }

        users = get_all_user()
        return {"response" : userview.multiple(users) }
예제 #8
0
    def post(self):
        params = request.args.to_dict()

        session = get_session(params['token'])
        user_id = session['user']

        if user_id:
            user = get_user_by_id(user_id)
            if not user:
                return {"response": "User not found"}, 404

        payload = request.json
        print payload
        try:
            gi = ObjectId(payload["game_instance"])
            row = int(payload["row"]) - 1
            col = int(payload["column"]) - 1
        except:
            return {"response": "Bad Reqeust"}, 400

        gi = get_game_instance(gi)
        if not gi:
            return {"response": "Instance for a Game is not found"}, 404

        if gi["next_player"] != user["_id"]:
            return {"response": "Not Valid Next User"}, 400

        if not set([row, col]).issubset(set(range(len(gi["cstate"])))):
            return {"response": "Bad Reqeust"}, 400

        if gi["cstate"][row][col]:
            return {"response": "Cell already occupied"}, 400

        symbol, next_player = "X", gi["user2"]
        if gi["user2"] == user["_id"]:
            symbol, next_player = "O", gi["user1"]

        cstate = gi["cstate"]
        cstate[row][col] = symbol

        cordinates = [(rowIndx, colIndx)
                      for rowIndx, rowVal in enumerate(cstate)
                      for colIndx, colVal in enumerate(rowVal)
                      if colVal == symbol]

        if cordinates in gconst.winning_state:
            gi = make_move(gi, cstate, next_player, gconst.final_status,
                           str(user["_id"]))
            return {"response": single_game_instance(gi)}
        gi = make_move(gi, cstate, next_player, gconst.accepted_status)
        return {"response": single_game_instance(gi)}
예제 #9
0
 def get(self):
     """查询个人账户
     """
     user_info = get_user_by_id(self.db_session, self.user_id)
     deposit_info = get_deposit_by_uid(self.db_session, self.user_id)
     resp_data = {
         'user_info': {
             'user_id': self.user_id,
             'name': user_info.name,
         },
         'deposit_info': {
             'money': deposit_info.money,
             'deposit_type': deposit_info.deposit_type,
         },
     }
     return self.response(resp_data=resp_data)