def problems():
    username = current_user.name
    user_stats = generate_user_stats(username, user_pool)

    categorized_problems = categorize_problems(user_stats, parsed_problem_file)
    # return str(categorized_problems)
    return render_template("problems.html", categorized_problems=categorized_problems, logged_in_user=current_user)
def stats():
    username = current_user.name
    user_stats = generate_user_stats(username, user_pool)

    info_to_render = {"user_info": user_stats, "table_info": display_problems_per_user(user_stats, parsed_problem_file)}

    return render_template("stats.html", info_to_render=info_to_render, logged_in_user=current_user)
def buy_solution(problem_name, seller_name):
    current_problem = None
    error = None
    for name, problem in parsed_problem_file:
        if name == problem_name:
            current_problem = lg.Problem.fromString(problem)
            break

    if not current_problem:
        return render_template(
            "error_page.html", logged_in_user=current_user, error="You entered a URL for a non-existent problem."
        )

    username = current_user.name
    user_stats = generate_user_stats(username, user_pool)

    categorized_problems = categorize_problems(user_stats, parsed_problem_file)

    if not problem_name in (name for name, _ in categorized_problems["to_solve"]):
        return render_template(
            "error_page.html",
            logged_in_user=current_user,
            error="You have either solved the problem or you are not allowed to access it yet.",
        )

    all_commodities = rc.list_all_commodities(User.user_database, user_pool)
    found = False
    for prob, user, price_requested in all_commodities:
        if prob == problem_name and user == seller_name:
            found = True
            break
    if not found:
        error = "The seller is not selling the solution to this problem."
        return render_template("error_page.html", logged_in_user=current_user, error=error)

    if get_user_points(username, user_pool) < price_requested:
        error = "You don't have enough points to buy the solution to this problem."
        return render_template("error_page.html", logged_in_user=current_user, error=error)

    rc.pay_user(seller_name, price_requested, user_pool)
    rc.charge_user(current_user.name, price_requested, user_pool)
    result, points = rc.submit_solution(current_problem.answer, current_problem.name, problem_pool)
    if not result:
        error = "Something really bad happened."
        return render_template("error_page.html", logged_in_user=current_user, error=error)
    else:
        rc.update_user_solution(points, current_problem.name, current_user.name, user_pool)
        rc.add_to_purchases(current_user.name, current_problem.name, user_pool)
        log_purchase(current_user.name, seller_name, current_problem.name, price_requested, points)
        message = "You bought the solution of {0} from {1} at the price of {2} points. Check the stats page for the solution".format(
            current_problem.display_name.lower(), get_display_name(seller_name), price_requested
        )
        return render_template("success_page.html", logged_in_user=current_user, message=message)
def list_solutions_for_sale(problem_name):
    current_problem = None
    error = None
    for name, problem in parsed_problem_file:
        if name == problem_name:
            current_problem = lg.Problem.fromString(problem)
            break

    if not current_problem:
        return render_template(
            "error_page.html", logged_in_user=current_user, error="You entered a URL for a non-existent problem."
        )

    username = current_user.name
    user_stats = generate_user_stats(username, user_pool)

    categorized_problems = categorize_problems(user_stats, parsed_problem_file)

    if not problem_name in (name for name, _ in categorized_problems["to_solve"]):
        return render_template(
            "error_page.html",
            logged_in_user=current_user,
            error="You have either already solved the problem, or you are not allowed to solve it.",
        )

    all_commodities = rc.list_all_commodities(User.user_database, user_pool)
    problem_commodities = [
        (prob, user, get_display_name(user), price)
        for (prob, user, price) in all_commodities
        if prob == current_problem.name
    ]
    return render_template(
        "list_problems.html",
        logged_in_user=current_user,
        current_problem=current_problem,
        commodities=problem_commodities,
    )
def change_solution_price(problem_name):
    current_problem = None
    error = None
    for name, problem in parsed_problem_file:
        if name == problem_name:
            current_problem = lg.Problem.fromString(problem)
            break

    if not current_problem:
        error = "You entered a URL for a non-existent problem."
        return render_template("error_page.html", logged_in_user=current_user, error=error)

    username = current_user.name
    user_stats = generate_user_stats(username, user_pool)

    categorized_problems = categorize_problems(user_stats, parsed_problem_file)

    if not problem_name in (name for name, _ in categorized_problems["on_market"]):
        error = "You have not put this problem up on the market yet."
        return render_template("error_page.html", logged_in_user=current_user, error=error)

    if get_user_points(username, user_pool) < lg.MARKET_FEE_CHANGE_PRICE:
        error = "You don't have enough points to change the price of the solution."
        return render_template("error_page.html", logged_in_user=current_user, error=error)

    form = PriceForm(request.form)
    if request.method == "POST":
        price_requested_str = request.form["price"]
        try:
            price_requested = int(price_requested_str)
            if price_requested < 0:
                error = "The price must be a non-negative integer."
                return render_template(
                    "change_price.html",
                    form=form,
                    error=error,
                    logged_in_user=current_user,
                    current_problem=current_problem,
                    market_fee=lg.MARKET_FEE_CHANGE_PRICE,
                )
            result = rc.change_solution_price(
                current_user.name, current_problem.name, price_requested, user_pool, User.user_database
            )
            log_changed_price(current_user.name, current_problem.name, price_requested, lg.MARKET_FEE_CHANGE_PRICE)
            if result:
                return render_template(
                    "success_page.html",
                    logged_in_user=current_user,
                    message="You successfully changed the price of the solution of {0} to {1} points.".format(
                        current_problem.display_name.lower(), price_requested
                    ),
                )
            else:
                return render_template(
                    "error_page.html", logged_in_user=current_user, error="Something wrong happened. Try again."
                )

        except ValueError:
            error = "The price must be a non-negative integer."
            return render_template(
                "change_price.html",
                form=form,
                error=error,
                logged_in_user=current_user,
                current_problem=current_problem,
                market_fee=lg.MARKET_FEE_CHANGE_PRICE,
            )

    return render_template(
        "change_price.html",
        form=form,
        error=error,
        logged_in_user=current_user,
        current_problem=current_problem,
        market_fee=lg.MARKET_FEE_CHANGE_PRICE,
    )
def put_solution_on_market(problem_name):
    current_problem = None
    error = None
    for name, problem in parsed_problem_file:
        if name == problem_name:
            current_problem = lg.Problem.fromString(problem)
            break

    if not current_problem:
        error = "You entered a URL for a non-existent problem."
        return render_template("error_page.html", logged_in_user=current_user, error=error)

    username = current_user.name
    user_stats = generate_user_stats(username, user_pool)

    categorized_problems = categorize_problems(user_stats, parsed_problem_file)

    if not problem_name in (name for name, _ in categorized_problems["to_sell"]):
        error = "You have either already put up the solution for sale, or you have not solved the problem yet."
        return render_template("error_page.html", logged_in_user=current_user, error=error)

    if get_user_points(username, user_pool) < lg.MARKET_FEE_NEW_ITEM:
        error = "You don't have enough points to put up the solution on market."
        return render_template("error_page.html", logged_in_user=current_user, error=error)

    form = PriceForm(request.form)
    if request.method == "POST":
        price_requested_str = request.form["price"]
        try:
            price_requested = int(price_requested_str)
            if price_requested < 0:
                error = "The price must be a non-negative integer."
                return render_template(
                    "sale_page.html",
                    form=form,
                    error=error,
                    logged_in_user=current_user,
                    current_problem=current_problem,
                    market_fee=lg.MARKET_FEE_NEW_ITEM,
                )
            rc.sell_solution(current_user.name, current_problem.name, price_requested, user_pool)
            log_on_market(current_user.name, current_problem.name, price_requested, lg.MARKET_FEE_NEW_ITEM)
            return render_template(
                "success_page.html",
                logged_in_user=current_user,
                message="You successfully put up the solution of {0} for the price of {1} points.".format(
                    current_problem.display_name.lower(), price_requested
                ),
            )

        except ValueError:
            error = "The price must be a non-negative integer."
            return render_template(
                "sale_page.html",
                form=form,
                error=error,
                logged_in_user=current_user,
                current_problem=current_problem,
                market_fee=lg.MARKET_FEE_NEW_ITEM,
            )

    return render_template(
        "sale_page.html",
        form=form,
        error=error,
        logged_in_user=current_user,
        current_problem=current_problem,
        market_fee=lg.MARKET_FEE_NEW_ITEM,
    )
def post_solution(prob_name):
    current_problem = None
    error = None
    for name, problem in parsed_problem_file:
        if name == prob_name:
            current_problem = lg.Problem.fromString(problem)
            break

    if not current_problem:
        return render_template(
            "error_page.html", logged_in_user=current_user, error="You entered a URL for a non-existent problem."
        )

    username = current_user.name
    user_stats = generate_user_stats(username, user_pool)

    categorized_problems = categorize_problems(user_stats, parsed_problem_file)

    if not prob_name in (name for name, _ in categorized_problems["to_solve"]):
        return render_template(
            "error_page.html",
            logged_in_user=current_user,
            error="You have either solved the problem or you are not allowed to solve it.",
        )

    form = AnswerForm(request.form)
    if request.method == "POST":
        submitted_answer = request.form["answer"]
        if submitted_answer != current_problem.answer:
            if not submitted_answer:
                error = "Enter an answer"
            else:
                error = "Wrong answer. Try again."
            return render_template(
                "submit_answer.html",
                form=form,
                error=error,
                logged_in_user=current_user,
                current_problem=current_problem,
            )
        else:
            result, points = rc.submit_solution(submitted_answer, current_problem.name, problem_pool)
            if result:
                status = rc.update_user_solution(points, current_problem.name, current_user.name, user_pool)
                if not status:
                    return render_template(
                        "error_page.html",
                        logged_in_user=current_user,
                        error="Something went wrong. Try submitting your answer again.",
                    )
                else:
                    log_submission(current_user.name, current_problem.name, points)
                    return render_template(
                        "success_page.html",
                        logged_in_user=current_user,
                        message="Your answer was correct. You gained {0} points.".format(points),
                    )

    return render_template(
        "submit_answer.html", form=form, error=error, logged_in_user=current_user, current_problem=current_problem
    )