def submit_pvp_game_credentials(): if SessionKeys.ACTIVE_GAME_ID in session: return redirect(URLs.HOME_PAGE) if request.form['gameID'] is None or request.form['password'] is None: return render_template('joinGame.html', prefilled_id='', showUnsuccessfulMessage=True) submitted_game_id = str(request.form['gameID']) submitted_pass = str(request.form['password']) if submitted_game_id == '' or not submitted_game_id.isalnum(): return render_template('joinGame.html', prefilled_id='', showUnsuccessfulMessage=True) if submitted_pass != '' and not submitted_pass.isalnum(): return render_template('joinGame.html', prefilled_id='', showUnsuccessfulMessage=True) db_util = DBUtil(get_db_connection()) game_status = db_util.get_game_status(submitted_game_id) if game_status is None or game_status != GameStatus.WAITING_FOR_OPPONENT: return render_template('joinGame.html', prefilled_id='', showUnsuccessfulMessage=True) actual_pass = db_util.get_pvp_game_pass(submitted_game_id) if actual_pass is None or actual_pass != submitted_pass: return render_template('joinGame.html', prefilled_id='', showUnsuccessfulMessage=True) else: is_playing_white = db_util.set_pvp_game_active( submitted_game_id, session[SessionKeys.USER_ID]) session[SessionKeys.ACTIVE_GAME_ID] = submitted_game_id session[SessionKeys.IS_GAME_HOST] = False session[SessionKeys.IS_PLAYING_WHITE] = is_playing_white return redirect(URLs.HOME_PAGE)
def get_join_pvp_game_page(): if request.args.get('gameID') is not None: game_id = str(request.args.get('gameID')) if game_id != '' and not game_id.isalnum(): return render_template('joinGame.html', prefilled_id='', showUnsuccessfulMessage=True) db_util = DBUtil(get_db_connection()) game_status = db_util.get_game_status(game_id) if game_status is None or game_status != GameStatus.WAITING_FOR_OPPONENT: return render_template('joinGame.html', prefilled_id='', showUnsuccessfulMessage=True) password = db_util.get_pvp_game_pass(game_id) if password is None: return render_template('joinGame.html', prefilled_id='', showUnsuccessfulMessage=True) if password == '': is_playing_white = db_util.set_pvp_game_active( game_id, session[SessionKeys.USER_ID]) session[SessionKeys.ACTIVE_GAME_ID] = game_id session[SessionKeys.IS_GAME_HOST] = False session[SessionKeys.IS_PLAYING_WHITE] = is_playing_white return redirect(URLs.HOME_PAGE) else: return render_template('joinGame.html', prefilledID=game_id, showUnsuccessfulMessage=False) else: return render_template('joinGame.html', prefilled_id='', showUnsuccessfulMessage=False)
def get_game_status(): logger = get_logger() if SessionKeys.ACTIVE_GAME_ID not in session: logger.debug( "getGameStatus was called when there is no active game. Returning error message" ) return get_error_json("Something went wrong.") active_game_id = session[SessionKeys.ACTIVE_GAME_ID] db_util = DBUtil(get_db_connection()) game_status = db_util.get_game_status(active_game_id) POSSIBLE_STATUSES = [ "WAITING_FOR_OPPONENT", "ACTIVE", "COMPLETED", "CANCELLED" ] if game_status < 0 or game_status > len(POSSIBLE_STATUSES): logger.debug( "Game status in DB is " + str(game_status) + " which should not be possible. Returning error message") return get_error_json("Something went wrong.") jsonDict = {'gameStatus': POSSIBLE_STATUSES[game_status]} return jsonify(jsonDict)
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."})
def before_request_callback(): if 'static' in request.path: return if SessionKeys.NICKNAME not in session: if request.path == URLs.SET_NICKNAME: return if request.path in JSON_REQUESTS: get_logger().debug( "There is no nickname in the session, so returning an 'error' JSON" ) return get_error_json( "There is no nickname in the session currently.") get_logger().debug( "There is no nickname in the session, so redirecting to nickname page" ) return redirect_to_nickname_page(request.url, False, False, False) else: pass #update_last_request_column_for_user(get_db_connection(), session[SessionKeys.USER_ID]) if SessionKeys.ACTIVE_GAME_ID in session: db_util = DBUtil(get_db_connection()) game_status = db_util.get_game_status( session[SessionKeys.ACTIVE_GAME_ID]) if (game_status == GameStatus.CANCELLED or game_status == GameStatus.COMPLETED) and ( request.path != URLs.CHECK_IN_PVP_GAME) and ( request.path != URLs.GET_PVP_BOARD_SVG): 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 if request.method == 'GET' and game_status == GameStatus.WAITING_FOR_OPPONENT and request.path != URLs.WAIT_FOR_OPP_PAGE: return redirect(URLs.WAIT_FOR_OPP_PAGE) if request.method == 'GET' and game_status == GameStatus.ACTIVE and request.path != URLs.GO_TO_GAME: return redirect(URLs.GO_TO_GAME)