예제 #1
0
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)
예제 #2
0
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,
    )