def cancel_pvp_game(): logger = get_logger() if SessionKeys.ACTIVE_GAME_ID not in session: logger.debug("No active game to cancel") return jsonify({'FAILURE': "There is no active game to cancel."}) if not session[SessionKeys.IS_GAME_HOST]: logger.debug("Can't cancel when not host") return jsonify({'FAILURE': "Cannot cancel as non-host."}) db_util = DBUtil(get_db_connection()) active_game_id = session[SessionKeys.ACTIVE_GAME_ID] game_status = db_util.get_game_status(active_game_id) if game_status != GameStatus.WAITING_FOR_OPPONENT: logger.debug( "Game status is not 'Waiting for opponent', so cannot cancel it" ) return jsonify( {'FAILURE': "Game is not in a state to be able to cancel it."}) logger.debug("Cancelling PVP game") db_util.cancel_pvp_game(active_game_id) session.pop(SessionKeys.ACTIVE_GAME_ID, None) session.pop(SessionKeys.IS_GAME_HOST, None) session.pop(SessionKeys.IS_PLAYING_WHITE, None) session.pop(SessionKeys.IS_PLAYERS_TURN, None) return jsonify({'SUCCESS': "Game has been cancelled."})