def get(self): params = request.args.to_dict() session = get_session(params['token']) if not session: return { "response": None, "messege": "UnAuthorized", "status": False }, 401 current_user_id = str(session['user']) if current_user_id: user = get_user_by_id(current_user_id) if not user: return { "response": None, "messege": "user not found", "status": False }, 404 game_id = params["game_id"] moves = get_moves_by_game_id(game_id) return { "response": self.__multiple(moves), "messege": "All Moves", "status": True }, 200
def post(self): params = request.args.to_dict() session = get_session(params['token']) if not session : return { "response" : None, "messege" : "UnAuthorized", "status" : False },401 current_user_id = str(session['user']) user1 = get_user_by_id(current_user_id) if not user1: return { "response" : None, "messege" : "user not found", "status": False }, 404 payload = request.json if not self.__validate_game_instance_creation(payload): return { "response" : None, "messege" : "Bad request", "status" : False },404 player2_email = payload['email'] user2 = get_user_by_mail(player2_email) if not user2: return { "response" : None, "messege" : "user not found by email", "status": False }, 404 player1 ={ "player_id":str(user1["_id"]), "email":user1["email"], "name" :user1["name"], "piece_type": "O" } player2 ={ "player_id":str(user2["_id"]), "email":user2["email"], "name" :user2["name"], "piece_type": "X" } tossNo = random.choice([1,2]) tossWinner = player1 if tossNo == 2: tossWinner = player2 game_instance_id = create_game_instance(player1,player2,tossWinner) game_single = self.__single(get_game_instance_by_id(game_instance_id)) # print(game_single) return { "response" : game_single , "messege" : "game created successfully", "status" : True },200
def patch(self): params = request.args.to_dict() session = get_session(params['token']) if not session : return { "response" : None, "messege" : "UnAuthorized", "status" : False },401 current_user_id = str(session['user']) if current_user_id: user = get_user_by_id(current_user_id) if not user: return { "response" : None, "messege" : "user not found", "status": False }, 404 payload = request.json game_id = payload['game_id'] game = get_game_instance_by_id(game_id) if current_user_id != game["player2"]["player_id"]: return{ "response" : None , "messege" : "You are not allowed to accept challenge on behalf of other", "status" : False },400 accepte_game_instance(game_id) return{ "response" : None , "messege" : "Challenge Accepted Successfully", "status" : True },200
def get(self): params = request.args.to_dict() session = get_session(params['token']) current_user_id = str(session['user']) if current_user_id: user = get_user_by_id(current_user_id) if not user: return { "response" : None, "messege" : "Invalid Token", "status": False }, 404 return { "response" : self.__single(user), "messege" : "Ok", "status": True },200
def post(self): params = request.args.to_dict() session = get_session(params['token']) if not session: return { "response": None, "messege": "UnAuthorized", "status": False }, 401 current_user_id = str(session['user']) if current_user_id: user = get_user_by_id(current_user_id) if not user: return { "response": None, "messege": "user not found", "status": False }, 404 payload = request.json if not self.__validate_move_obj(payload): return { "response": None, "messege": "Bad request", "status": False }, 401 game_id = payload['game_id'] proposed_state = payload['pstate'] game_instance = get_game_instance_by_id(game_id) if not game_instance: return { "response": None, "messege": "Game Instance not found", "status": False }, 404 if game_instance["status"] == FINAL_STATUS: return { "response": game_instance["cstate"], "messege": "Game Over", "status": False }, 400 piece_type = "O" next_player = game_instance["player1"] if game_instance["player1"]["player_id"] == current_user_id: piece_type = game_instance["player1"]["piece_type"] next_player = game_instance["player2"] if game_instance["next_player"]["piece_type"] != game_instance[ "player1"]["piece_type"]: return { "response": game_instance["cstate"], "messege": "Not Your Turn", "status": False }, 401 elif game_instance["player2"]["player_id"] == current_user_id: piece_type = game_instance["player2"]["piece_type"] next_player = game_instance["player1"] if game_instance["next_player"]["piece_type"] != game_instance[ "player2"]["piece_type"]: return { "response": game_instance["cstate"], "messege": "Not Your Turn", "status": False }, 401 else: return { "response": game_instance["cstate"], "messege": "You are Not player in this game", "status": False }, 401 curr_game = TicTacToe() curr_game.assignNewStateValue(game_instance["cstate"]) print(curr_game) ppos_game = TicTacToe() ppos_game.assignNewStateValue(proposed_state) print(ppos_game) success = curr_game.isValidMove(ppos_game, piece_type) if not success: return { "response": game_instance["cstate"], "messege": "Invalid Move", "status": False }, 401 isgameover = ppos_game.isGameOver() winnerPiece = ppos_game.checkWinner() if isgameover: if winnerPiece == game_instance["next_player"]["piece_type"]: make_move(game_id, ppos_game.state, None, FINAL_STATUS, winner=game_instance["next_player"]) elif winnerPiece == next_player["piece_type"]: make_move(game_id, ppos_game.state, None, FINAL_STATUS, winner=next_player) else: make_move(game_id, ppos_game.state, None, FINAL_STATUS, None) else: make_move(game_id, ppos_game.state, next_player, ACCEPTED_STATUS) make_move_entry(game_id, curr_game.state, ppos_game.state, game_instance["next_player"], next_player) game_instance = get_game_instance_by_id(game_id) if game_instance["status"] == FINAL_STATUS: return { "response": self.__singleGame(game_instance), "messege": "You Win", "status": True }, 200 return { "response": self.__singleGame(game_instance), "messege": "You Played successfully", "status": True }, 200