Example #1
0
async def show_user(request: Request):
    calc_system = get_system(request.session.get("calc_system"))
    if not calc_system:
        calc_system = DEFAULT_CALC_SYSTEM
    calc_system = calc_system()

    uid: int = request.path_params["user_id"]
    mode: str = request.query_params.get("mode")
    if mode not in ["osu", "catch", "taiko", "mania"]:
        mode = None

    user = await User.get_or_none(osuId=uid)
    if not user:
        raise HTTPException(404, "User not found.")

    d = datetime.now() - timedelta(90)
    nominations = await user.get_nomination_activity(d, mode=mode)

    if not nominations:
        ctx = {
            "request": request,
            "user": user,
            "error": True,
            "title": user.username
        }
        return templates.TemplateResponse("pages/user/no_noms.html", ctx)

    for nom in nominations:
        nom.map = await nom.get_map()

    nominations.sort(key=lambda x: abs(x.score[calc_system.name].total_score),
                     reverse=True)
    ctx = {
        "calc_system": calc_system,
        "request": request,
        "user": user,
        "nominations": nominations,
        "title": user.username,
    }
    return templates.TemplateResponse("pages/score/show.html", ctx)
Example #2
0
async def listing(request: Request):
    users = await User.get_users()
    counts = [await u.total_nominations() for u in users]
    eval_counts = [await u.total_nominations(90) for u in users]

    ctx = {
        "request": request,
        "users": users,
        "counts": counts,
        "genres": [g.name.replace("_", " ") for g in Genre],
        "languages": [lang.name for lang in Language],
        "diffs": [diff.name for diff in Difficulty],
        "last_update": max(users, key=lambda x: x.last_updated).last_updated,
        "title": "User Listing",
        "eval_counts": eval_counts,
    }
    return templates.TemplateResponse("pages/user/listing.html", ctx)
Example #3
0
async def leaderboard(request: Request):
    calc_system = get_system(request.session.get("calc_system"))
    if not calc_system:
        calc_system = DEFAULT_CALC_SYSTEM
    calc_system = calc_system()

    selected_mode = request.query_params.get("mode")
    is_valid_mode = selected_mode and selected_mode in [
        "osu",
        "taiko",
        "catch",
        "mania",
    ]
    if is_valid_mode:
        users = (await User.filter(modes__contains=selected_mode
                                   ).all().order_by("username"))
    else:
        users = await User.get_users()

    for u in users:
        u.score = await u.get_score(calc_system)
        u.score_modes = {}
        for mode in u.modes:
            u.score_modes[mode] = await u.get_score(calc_system, mode=mode)

    if is_valid_mode:
        users.sort(key=lambda x: x.score_modes[selected_mode], reverse=True)
    else:
        users.sort(key=lambda x: x.score, reverse=True)

    ctx = {
        "request": request,
        "users": users,
        "last_update": max(users, key=lambda x: x.last_updated).last_updated,
        "title": "Leaderboard",
        "mode": selected_mode,
    }
    return templates.TemplateResponse("pages/score/leaderboard.html", ctx)
Example #4
0
async def homepage(request: Request):
    ctx = {"request": request}
    return templates.TemplateResponse("pages/index.html", ctx)
Example #5
0
async def show_user(request: Request):
    uid: int = request.path_params["user_id"]
    user = await User.get_or_none(osuId=uid)
    if not user:
        raise HTTPException(404, "User not found.")

    day_limit: int = request.query_params.get("days")
    if type(day_limit) == str:
        if day_limit.isnumeric():
            day_limit = int(day_limit)
        else:
            day_limit = None

    datetime_limit = None
    if day_limit and day_limit in (30, 90, 360):
        datetime_limit = datetime.utcnow() - timedelta(day_limit)

    nominations = await user.get_nomination_activity(datetime_limit)

    # No nominations present, what even to show?
    if not nominations:
        ctx = {
            "request": request,
            "user": user,
            "error": True,
            "title": user.username
        }
        return templates.TemplateResponse("pages/user/no_noms.html", ctx)

    invalid_nominations = []
    for nom in nominations:
        nom.map = await nom.get_map()

        # Map is deleted in osu!
        if not nom.map.beatmaps:
            invalid_nominations.append(nom)

    for nom in invalid_nominations:
        nominations.remove(nom)

    graph_labels: Dict[str, List[str]] = {
        "genre": [],
        "language": [],
        "sr-top": [],
        "sr-all": [],
    }
    graph_data: Dict[str, List[int]] = {
        "genre": [],
        "language": [],
        "sr-top": [],
        "sr-all": [],
    }

    # Count for genres
    elem: Genre
    counts_genre = Counter([nom.map.genre for nom in nominations])
    for elem, cnt in counts_genre.items():
        graph_labels["genre"].append(elem.name.replace("_", " "))
        graph_data["genre"].append(cnt)

    # Count for languages
    elem: Language
    counts_lang = Counter([nom.map.language for nom in nominations])
    for elem, cnt in counts_lang.items():
        graph_labels["language"].append(elem.name)
        graph_data["language"].append(cnt)

    # Count for difficulty
    elem: Difficulty
    counts_top = list(
        Counter([nom.map.top_difficulty.difficulty
                 for nom in nominations]).items())
    counts_top.sort(key=lambda x: x[0].value)
    for elem, cnt in counts_top:
        graph_labels["sr-top"].append(elem.name)
        graph_data["sr-top"].append(cnt)

    difficulties = []
    for nom in nominations:
        for map in nom.map.beatmaps:
            difficulties.append(map.difficulty)

    counts_diff = list(Counter(difficulties).items())
    counts_diff.sort(key=lambda x: x[0].value)
    for elem, cnt in counts_diff:
        graph_labels["sr-all"].append(elem.name)
        graph_data["sr-all"].append(cnt)

    line_labels, line_datas = _create_nomination_chartdata(nominations)

    calc_system = get_system(request.session.get("calc_system"))
    if not calc_system:
        calc_system = DEFAULT_CALC_SYSTEM
    calc_system = calc_system()

    user.score = await user.get_score(calc_system)
    user.score_modes = {}
    for mode in user.modes:
        user.score_modes[mode] = await user.get_score(calc_system, mode=mode)

    ctx = {
        "request": request,
        "user": user,
        "nominations": nominations,
        "labels": graph_labels,
        "datas": graph_data,
        "avg_length": format_time(user.avg_length),
        "length_data": _create_length_chartdata(nominations),
        "line_labels": line_labels,
        "line_datas": line_datas,
        "last_update": user.last_updated,
        "title": user.username,
    }
    return templates.TemplateResponse("pages/user/show.html", ctx)