def generate_fake_comp_results(): """ Generates a bunch of fake results for the current competition with realistic-ish results. """ test_users = [ update_or_create_user_for_reddit(name, '') for name in __TEST_USER_NAMES ] for comp_event in get_all_comp_events_for_comp( get_active_competition().id): event_name = comp_event.Event.name if event_name in ('FMC', 'MBLD'): continue thresholds = __AUTO_BLACKLIST_THRESHOLDS.get(event_name) if not thresholds: continue wr_average = thresholds[1] for i, user in enumerate(test_users): results = UserEventResults(comp_event_id=comp_event.id, user_id=user.id, comment='') for solve in [ __build_solve(i, wr_average, event_name, s.id) for s in comp_event.scrambles ]: results.solves.append(solve) process_event_results(results, comp_event, user) save_event_results(results)
def results_list(): """ A route for showing which competitions results can be viewed for. """ comps = get_complete_competitions() comp = get_active_competition() return render_template("results/results_list.html", comps=comps, active=comp)
def index(_=None): """ Main page for the app. Shows cards for every event in the current competition.""" if not current_user.is_authenticated: return redirect(url_for("prompt_login")) # Get the current competition comp = comp_manager.get_active_competition() if not comp: return "There are no competitions created yet. Go make one!" # Get all event results for the logged-in user user_results = get_all_user_results_for_comp_and_user( comp.id, current_user.id) # Get a dict of comp ID to result for all complete events complete_events = { r.CompetitionEvent.id: r.friendly_result() for r in user_results if r.is_complete } # Build a function which determines if a result is incomplete (solves are recorded, but enough yet to be complete) # Then build up a set of comp event IDs for incomplete events is_incomplete_func = __build_is_incomplete_func(set( complete_events.keys())) incomplete_event_ids = set(r.CompetitionEvent.id for r in user_results if is_incomplete_func(r)) # Build a list of competition events in this comp. Initially order them by event ID, but then sort and group them # by WCA events first, non-WCA weekly events next, and then bonus events last. This ordering ensures events are # ordered relative to each other in the same way each comp comp_events = sort_comp_events_by_global_sort_order(comp.events) # Build a set of comp event IDs that are bonus events so we can mark them on the main page bonus_event_names = set(get_all_bonus_events_names()) bonus_events_ids = set(c.id for c in comp_events if c.Event.name in bonus_event_names) # Determine whether to show moving shapes background show_shapes_background = get_setting_for_user( current_user.id, SettingCode.ENABLE_MOVING_SHAPES_BG) show_shapes_background = show_shapes_background == TRUE_STR generate_token() # Phew, finally we can render the page return render_template('index.html', current_competition=comp, comp_events=comp_events, complete_events=complete_events, incomplete_events=incomplete_event_ids, bonus_events_ids=bonus_events_ids, show_shapes_background=show_shapes_background)
def wrap_weekly_competition(): """ A periodic task to schedule sub-tasks related to wrapping up the weekly competitions. """ current_comp = get_active_competition() comp_events_in_comp = get_all_comp_events_for_comp(current_comp.id) set_medals_on_best_event_results(comp_events_in_comp) post_results_thread_task(current_comp.id) generate_new_competition_task() prepare_end_of_competition_info_notifications(current_comp.id) send_gift_code_winner_approval_pm(current_comp.id)
def wrap_weekly_competition(): """ A periodic task to schedule sub-tasks related to wrapping up the weekly competitions. """ current_comp = get_active_competition() comp_events_in_comp = get_all_comp_events_for_comp(current_comp.id) set_medals_on_best_event_results(comp_events_in_comp) post_results_thread_task(current_comp.id, current_comp.title) generate_new_competition_task() clearly_weekly_blacklist() send_weekly_report(current_comp.id) prepare_end_of_competition_info_notifications(current_comp.id)
def increment_new_user_count(comp_id=None): """ Increments new user count. If comp_id is specified, uses that competition's metrics, otherwise uses the metrics for the current week's competition. """ if not comp_id: comp_id = get_active_competition().id metrics = get_weekly_metrics(comp_id) if not metrics.new_users_count: metrics.new_users_count = 1 else: metrics.new_users_count += 1 save_metrics(metrics)
def authorize(): """ Handle the callback from Reddit's OAuth. Create a user if necessary, update their refresh token, and log the user in. """ error = request.args.get('error', None) if error == 'access_denied': return redirect(url_for('denied')) auth_code = request.args.get('code') username, refresh_token = get_username_refresh_token_from_code(auth_code) user = update_or_create_user(username, refresh_token) login_user(user, True) if comp_manager.get_active_competition(): return redirect(url_for('index')) return "Successfully logged in as {}. There are no competitions created yet. Go make one!".format(username)
def get_pb_average_event_results_except_current_comp(user_id, event_id): """ Returns the UserEventResults which were a PB average for the specified user and event. """ current_comp_id = get_active_competition().id return DB.session.\ query(UserEventResults).\ join(User).\ join(CompetitionEvent).\ join(Competition).\ join(Event).\ filter(Event.id == event_id).\ filter(User.id == user_id).\ filter(Competition.id != current_comp_id).\ filter(UserEventResults.was_pb_average).\ filter(UserEventResults.is_blacklisted.isnot(True)).\ filter(UserEventResults.is_complete).\ order_by(UserEventResults.id).\ all()
def results_list(): """ A route for showing which competitions results can be viewed for. """ comps = get_complete_competitions() comp = get_active_competition() past_comps = list( map( lambda past_comp: { 'id': past_comp[0], 'title': past_comp[1], 'startDate': past_comp[3], 'endDate': past_comp[4] }, comps)) current_comp = {'id': comp.id, 'title': comp.title} result = {'currentComp': current_comp, 'pastComps': past_comps} return jsonify(result)
def prompt_login(): """ Prompts the user to login for the best experience. """ comp = comp_manager.get_active_competition() return render_template('prompt_login/prompt_login.html', current_competition=comp)
def denied(): """ For when the user declines Reddit OAuth. """ comp = comp_manager.get_active_competition() return render_template('prompt_login/denied.html', current_competition=comp)
def curr_leaders(): """ Redirects to the current competition's leaderboards. """ comp = get_active_competition() return redirect("leaderboards/{}".format(comp.id))