예제 #1
0
    async def process(self, message, args):
        # args = [(problem ID || -1)?]
        if len(args) == 0:
            active_problems = problems_table.get_active_problems()
            if len(active_problems) == 0:
                return await problems_table.send_no_active_problems(
                    message, False)
            if not discord_funcs.is_a_dm(message):
                out = "Here are the active problems: "
                await discord_funcs.reply_to_message(message, out)
                await problems_table.send_problems(message, active_problems)
            else:
                name = discord_funcs.get_username(message)
                config_vars = config.get_config_vars()
                min_percent = config.get_var_val_from_vars(
                    config.ConfigVars.percent.value.var_name(), config_vars)
                new_problem_ids = []
                for p in active_problems:
                    problem_id = p[constants.ProblemTableStruct.ID.value]
                    attempts = problem_files.get_all_attempts(problem_id)
                    userAttempt = [
                        a for a in attempts
                        if a[constants.ProblemFileStruct.NAME.value] == name
                    ]
                    if len(userAttempt) == 0 or problems_table.attempt_fails(
                            userAttempt[0], min_percent):
                        new_problem_ids.append(problem_id)
                new_active_problems = [
                    p for p in active_problems if
                    p[constants.ProblemTableStruct.ID.value] in new_problem_ids
                ]
                if len(new_active_problems) == 0:
                    return await problems_table.send_no_active_problems(
                        message, True)
                out = "Here are your active problems"
                await discord_funcs.reply_to_message(message, out)
                await problems_table.send_problems(message,
                                                   new_active_problems)
        elif args[0] == constants.ALL_ID:
            all_problems = problems_table.get_all_problems()
            if len(all_problems) == 0:
                out = "There are currently no problems"
                await discord_funcs.reply_to_message(message, out)
                return

            out = "Here are all of the problems: "
            await discord_funcs.reply_to_message(message, out)
            await problems_table.send_problems(message, all_problems)
        else:
            if not problems_table.problem_id_exists(args[0]):
                import error_messages
                return await error_messages.error_problem_does_not_exist(
                    message, args[0])
            problem = problems_table.get_problem_table_problem_by_id(args[0])
            out = "Here is problem {}: ".format(args[0])
            await discord_funcs.reply_to_message(message, out)
            await problems_table.send_problem(message, problem)
예제 #2
0
async def update_attempt_to_problem_file(message, problem_id, percent, big_o,
                                         lang):
    all_attempts = get_all_attempts(problem_id)
    # Removes any previous forfeit attempts
    name = discord_funcs.get_username(message)
    all_attempts = [
        a for a in all_attempts
        if not (a[constants.ProblemFileStruct.NAME.value] == name
                and is_a_forfeit_attempt(a))
    ]
    attempts = [a for a in all_attempts if not is_a_forfeit_attempt(a)]
    global_board = process_leaderboard.get_global_leader_board()
    # Deletes old attempt entries as points are based off ranking
    diff = problems_table.get_difficulty(problem_id)
    old_points = process_leaderboard.calculate_points(attempts, diff)
    for a in attempts:
        global_board = process_leaderboard.remove_attempt_global_leader_board(
            global_board, a[constants.ProblemFileStruct.NAME.value],
            a[constants.ProblemFileStruct.PERCENT.value],
            old_points[a[constants.ProblemFileStruct.NAME.value]])
    # Deletes lasts attempt and then
    # adds new entry into attempts
    # the new attempt could be a forfeit attempt
    attempts = [
        a for a in attempts
        if a[constants.ProblemFileStruct.NAME.value] != name
    ]
    new_attempt = create_new_attempt(name, percent, big_o, lang)
    attempts.append(new_attempt)
    # Adds back all of the attempts into the leaderboard
    new_points = process_leaderboard.calculate_points(attempts, diff)
    for a in attempts:
        if is_a_forfeit_attempt(a):
            continue
        global_board = process_leaderboard.add_attempt_global_leader_board(
            global_board, a[constants.ProblemFileStruct.NAME.value],
            a[constants.ProblemFileStruct.PERCENT.value],
            new_points[a[constants.ProblemFileStruct.NAME.value]])

    process_leaderboard.save_global_board(global_board)
    # all_attempts include the correct forfeit attempts but incorrect actual attempts
    # attempts doesn't include forfeit attempts but correct actual attempts
    attempts = attempts + [a for a in all_attempts if is_a_forfeit_attempt(a)]
    save_problem_file(attempts, problem_id)
    await problems_table.update_active_val(message, attempts, problem_id)
예제 #3
0
async def display_successful_name_update(message, new_name):
    out = "Successfully changed {}'s display name to {}".format(discord_funcs.get_username(message), new_name)
    await discord_funcs.reply_to_message(message, out)
예제 #4
0
async def display_successful_clear_name(message):
    out = "Successfully cleared {}'s display name".format(
        discord_funcs.get_username(message)[:-constants.DISCORD_HASH_LENGTH])
    await discord_funcs.reply_to_message(message, out)
예제 #5
0
def clear_display_name(message):
    names = get_display_names()
    discord_name = discord_funcs.get_username(message)
    new_names = remove_display_name_from_names(discord_name, names)
    save_display_names(new_names)
예제 #6
0
def add_new_display_name(message, display_name):
    names = get_display_names()
    discord_name = discord_funcs.get_username(message)
    removed_old = remove_display_name_from_names(discord_name, names)
    removed_old.append([discord_name, display_name])
    save_display_names(removed_old)
예제 #7
0
async def display_successful_forfeit(message, problem_id):
    out = "Successfully forfeited problem {} for ".format(problem_id)
    out += process_leaderboard.get_display_name(
        process_leaderboard.get_display_names(),
        discord_funcs.get_username(message))
    await discord_funcs.reply_to_message(message, out)