def submit_pvp_resignation(): logger = get_logger() if SessionKeys.ACTIVE_GAME_ID not in session: logger.debug( "Auto resign victory submitted, but there is no active game. Returning error JSON" ) return get_error_json( "No active game to submit autoresign victory for") game_id = session[SessionKeys.ACTIVE_GAME_ID] is_host = session[SessionKeys.IS_GAME_HOST] game_manager = get_pvp_game_manager(game_id) game_manager.declare_winner(not is_host, from_resign=True) db_util = DBUtil(get_db_connection()) db_util.end_game(game_id, GameEndMethod.RESIGN, not is_host, False, '') return ('', 204)
def submit_move(): logger = get_logger() logger.debug("RECEIVED REQUEST") if SessionKeys.ACTIVE_GAME_ID not in session: logger.debug("No active game to submit move for") return get_error_json("No active game to submit move for") game_id = session[SessionKeys.ACTIVE_GAME_ID] is_playing_white = session[SessionKeys.IS_PLAYING_WHITE] game_manager = get_pvp_game_manager(game_id) from_square = str(request.form['fromSquare']) to_square = str(request.form['toSquare']) promotion = None if 'promotion' in request.form: promotion = request.form['promotion'] KNIGHT = 2 QUEEN = 5 if not isinstance(promotion, int) or promotion < KNIGHT or promotion > QUEEN: promotion = None try: game_manager.make_move(is_playing_white, from_square, to_square, promotion) session[SessionKeys.IS_PLAYERS_TURN] = False # handle case where the game has ended board = game_manager.get_board() if board.is_game_over(): db_util = DBUtil(get_db_connection()) if board.is_checkmate(): is_host = session[SessionKeys.IS_GAME_HOST] game_manager.declare_winner(is_host, from_checkmate=True) db_util.end_game(game_id, GameEndMethod.CHECKMATE, is_host, False, '') elif board.is_insufficient_material(): game_manager.declare_draw(from_insufficient_material=True) db_util.end_game(game_id, GameEndMethod.DRAW_INSUFFICIENT, False, True, '') elif board.is_stalemate(): game_manager.declare_draw(from_stalemate=True) db_util.end_game(game_id, GameEndMethod.DRAW_STALEMATE, False, True, '') return jsonify({'isMoveLegal': True}) except: return jsonify({'isMoveLegal': False})
def check_in_pvp_game(): logger = get_logger() if SessionKeys.ACTIVE_GAME_ID not in session: logger.debug( "check-in request was called with no active game to check into. Returning error JSON" ) return get_error_json("No active game to check into") json_to_return = dict() JSON_OPPONENT_STATUS_PROP = 'opponentStatus' JSON_GAME_STATUS_PROP = 'gameStatus' JSON_REASON_PROP = 'reasonForGameEnding' JSON_PLAYERS_TURN = 'isPlayersTurn' game_id = session[SessionKeys.ACTIVE_GAME_ID] game_manager = get_pvp_game_manager(game_id) game_manager.get_number_of_moves_made() # testing this logger.debug("GameManager obj: " + str(game_manager)) is_host = session[SessionKeys.IS_GAME_HOST] # handle cases where opponent has been offline for some time time_since_opponent_last_check_in = game_manager.get_time_since_last_check_in_from_opponent( is_host) TIME_FOR_AUTO_RESIGN = 180 if time_since_opponent_last_check_in >= TIME_FOR_AUTO_RESIGN: # end the game by auto-resignation of opponent game_manager.declare_winner(is_host, from_auto_resign=True) db_util = DBUtil(get_db_connection()) db_util.end_game(game_id, GameEndMethod.AUTO_RESIGN, is_host, False, '') TIME_TO_DECLARE_OPPONENT_OFFLINE = 12 if time_since_opponent_last_check_in >= TIME_TO_DECLARE_OPPONENT_OFFLINE: json_to_return[JSON_OPPONENT_STATUS_PROP] = 'OFFLINE' else: json_to_return[JSON_OPPONENT_STATUS_PROP] = 'ONLINE' # handle isPlayersTurn property json_to_return[JSON_PLAYERS_TURN] = is_players_turn(game_id) # get the current game status game_status = game_manager.get_game_status() if game_status == LiveGameStatus.ACTIVE: json_to_return[JSON_GAME_STATUS_PROP] = 'ACTIVE' game_manager.receive_check_in(is_host) return jsonify(json_to_return) else: if game_status == LiveGameStatus.DRAW_INSUFFICIENT: json_to_return[JSON_GAME_STATUS_PROP] = 'DRAW' json_to_return[JSON_REASON_PROP] = 'INSUFFICENT MATERIAL' elif game_status == LiveGameStatus.DRAW_STALEMENT: json_to_return[JSON_GAME_STATUS_PROP] = 'DRAW' json_to_return[JSON_REASON_PROP] = 'STALEMATE' else: if game_manager.did_player_win(is_host): json_to_return[JSON_GAME_STATUS_PROP] = 'WON' else: json_to_return[JSON_GAME_STATUS_PROP] = 'LOST' if game_status == LiveGameStatus.AUTO_RESIGNED: json_to_return[JSON_REASON_PROP] = 'AUTO-RESIGNATION' elif game_status == LiveGameStatus.RESIGNED: json_to_return[JSON_REASON_PROP] = 'RESIGNATION' elif game_status == LiveGameStatus.CHECKMATED: json_to_return[JSON_REASON_PROP] = 'CHECKMATE' return jsonify(json_to_return)