Skip to content
This repository has been archived by the owner on Jul 6, 2024. It is now read-only.

INTRN-241: refactor to use Unset #115

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions app/api/osu/web.py
Original file line number Diff line number Diff line change
Expand Up @@ -248,11 +248,11 @@ async def get_scores_handler(
if isinstance(beatmap, ServiceError):
if beatmap is ServiceError.BEATMAPS_NOT_FOUND:
beatmap = await beatmaps.fetch_one(file_name=beatmap_filename)
if beatmap is not ServiceError.BEATMAPS_NOT_FOUND:
return f"{BeatmapWebRankedStatus.UPDATE_AVAILABLE}|false".encode()
if beatmap is ServiceError.BEATMAPS_NOT_FOUND:
logger.debug("Beatmap not found", beatmap_md5=beatmap_md5)
return f"{BeatmapWebRankedStatus.NOT_SUBMITTED}|false".encode()

logger.debug("Beatmap not found", beatmap_md5=beatmap_md5)
return f"{BeatmapWebRankedStatus.NOT_SUBMITTED}|false".encode()
return f"{BeatmapWebRankedStatus.UPDATE_AVAILABLE}|false".encode()

logger.error(
"Failed to fetch beatmap",
Expand Down
13 changes: 13 additions & 0 deletions app/game_modes.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,19 @@ class GameMode:
# AP_MANIA = 11 # doesn't exist


def to_string(game_mode: int) -> str:
if game_mode == GameMode.VN_OSU:
return "osu!"
elif game_mode == GameMode.VN_TAIKO:
return "taiko"
elif game_mode == GameMode.VN_CATCH:
return "fruits"
elif game_mode == GameMode.VN_MANIA:
return "mania"
else:
raise ValueError(f"Invalid game mode {game_mode}")


def for_client(server_game_mode: int) -> int:
game_mode = server_game_mode
if game_mode == GameMode.AP_OSU:
Expand Down
3 changes: 2 additions & 1 deletion app/repositories/accounts.py
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,8 @@ async def partial_update(

query = f"""\
UPDATE accounts
SET {",".join(f"{k} = :{k}" for k in update_fields)}
SET {", ".join(f"{k} = :{k}" for k in update_fields)},
updated_at = NOW()
WHERE account_id = :account_id
RETURNING {READ_PARAMS}
"""
Expand Down
180 changes: 106 additions & 74 deletions app/repositories/beatmaps.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
from typing import TypedDict

from app import clients
from app.typing import UNSET
from app.typing import Unset

READ_PARAMS = """\
beatmap_id,
Expand Down Expand Up @@ -61,6 +63,36 @@ class Beatmap(TypedDict):
updated_at: datetime


class BeatmapUpdateFields(TypedDict, total=False):
ranked_status: int | Unset
beatmap_md5: str | Unset
artist: str | Unset
title: str | Unset
version: str | Unset
creator: str | Unset
filename: str | Unset
total_length: int | Unset
max_combo: int | Unset
ranked_status_manually_changed: bool | Unset
plays: int | Unset
passes: int | Unset
mode: int | Unset
bpm: float | Unset
cs: float | Unset
ar: float | Unset
od: float | Unset
hp: float | Unset
star_rating: float | Unset
bancho_ranked_status: int | Unset
bancho_updated_at: datetime | Unset
username: str
email_address: str
privileges: int
password: str
country: str
silence_end: datetime | None


async def create(
beatmap_id: int,
beatmap_set_id: int,
Expand Down Expand Up @@ -180,79 +212,79 @@ async def fetch_one(

async def partial_update(
beatmap_id: int,
ranked_status: int | None = None,
beatmap_md5: str | None = None,
artist: str | None = None,
title: str | None = None,
version: str | None = None,
creator: str | None = None,
filename: str | None = None,
total_length: int | None = None,
max_combo: int | None = None,
ranked_status_manually_changed: bool | None = None,
plays: int | None = None,
passes: int | None = None,
mode: int | None = None,
bpm: float | None = None,
cs: float | None = None,
ar: float | None = None,
od: float | None = None,
hp: float | None = None,
star_rating: float | None = None,
bancho_ranked_status: int | None = None,
bancho_updated_at: datetime | None = None,
ranked_status: int | Unset = UNSET,
beatmap_md5: str | Unset = UNSET,
artist: str | Unset = UNSET,
title: str | Unset = UNSET,
version: str | Unset = UNSET,
creator: str | Unset = UNSET,
filename: str | Unset = UNSET,
total_length: int | Unset = UNSET,
max_combo: int | Unset = UNSET,
ranked_status_manually_changed: bool | Unset = UNSET,
plays: int | Unset = UNSET,
passes: int | Unset = UNSET,
mode: int | Unset = UNSET,
bpm: float | Unset = UNSET,
cs: float | Unset = UNSET,
ar: float | Unset = UNSET,
od: float | Unset = UNSET,
hp: float | Unset = UNSET,
star_rating: float | Unset = UNSET,
bancho_ranked_status: int | Unset = UNSET,
bancho_updated_at: datetime | Unset = UNSET,
) -> Beatmap | None:
beatmap = await clients.database.fetch_one(
query=f"""\
UPDATE beatmaps
SET ranked_status = COALESCE(:ranked_status, ranked_status),
beatmap_md5 = COALESCE(:beatmap_md5, beatmap_md5),
artist = COALESCE(:artist, artist),
title = COALESCE(:title, title),
version = COALESCE(:version, version),
creator = COALESCE(:creator, creator),
filename = COALESCE(:filename, filename),
total_length = COALESCE(:total_length, total_length),
max_combo = COALESCE(:max_combo, max_combo),
ranked_status_manually_changed = COALESCE(:ranked_status_manually_changed, ranked_status_manually_changed),
plays = COALESCE(:plays, plays),
passes = COALESCE(:passes, passes),
mode = COALESCE(:mode, mode),
bpm = COALESCE(:bpm, bpm),
cs = COALESCE(:cs, cs),
ar = COALESCE(:ar, ar),
od = COALESCE(:od, od),
hp = COALESCE(:hp, hp),
star_rating = COALESCE(:star_rating, star_rating),
bancho_ranked_status = COALESCE(:bancho_ranked_status, bancho_ranked_status),
bancho_updated_at = COALESCE(:bancho_updated_at, bancho_updated_at),
updated_at = NOW()
WHERE beatmap_id = :beatmap_id
RETURNING {READ_PARAMS}
""",
values={
"beatmap_id": beatmap_id,
"ranked_status": ranked_status,
"beatmap_md5": beatmap_md5,
"artist": artist,
"title": title,
"version": version,
"creator": creator,
"filename": filename,
"total_length": total_length,
"max_combo": max_combo,
"ranked_status_manually_changed": ranked_status_manually_changed,
"plays": plays,
"passes": passes,
"mode": mode,
"bpm": bpm,
"cs": cs,
"ar": ar,
"od": od,
"hp": hp,
"star_rating": star_rating,
"bancho_ranked_status": bancho_ranked_status,
"bancho_updated_at": bancho_updated_at,
},
)
update_fields: BeatmapUpdateFields = {}
if not isinstance(ranked_status, Unset):
update_fields["ranked_status"] = ranked_status
if not isinstance(beatmap_md5, Unset):
update_fields["beatmap_md5"] = beatmap_md5
if not isinstance(artist, Unset):
update_fields["artist"] = artist
if not isinstance(title, Unset):
update_fields["title"] = title
if not isinstance(version, Unset):
update_fields["version"] = version
if not isinstance(creator, Unset):
update_fields["creator"] = creator
if not isinstance(filename, Unset):
update_fields["filename"] = filename
if not isinstance(total_length, Unset):
update_fields["total_length"] = total_length
if not isinstance(max_combo, Unset):
update_fields["max_combo"] = max_combo
if not isinstance(ranked_status_manually_changed, Unset):
update_fields["ranked_status_manually_changed"] = ranked_status_manually_changed
if not isinstance(plays, Unset):
update_fields["plays"] = plays
if not isinstance(passes, Unset):
update_fields["passes"] = passes
if not isinstance(mode, Unset):
update_fields["mode"] = mode
if not isinstance(bpm, Unset):
update_fields["bpm"] = bpm
if not isinstance(cs, Unset):
update_fields["cs"] = cs
if not isinstance(ar, Unset):
update_fields["ar"] = ar
if not isinstance(od, Unset):
update_fields["od"] = od
if not isinstance(hp, Unset):
update_fields["hp"] = hp
if not isinstance(star_rating, Unset):
update_fields["star_rating"] = star_rating
if not isinstance(bancho_ranked_status, Unset):
update_fields["bancho_ranked_status"] = bancho_ranked_status
if not isinstance(bancho_updated_at, Unset):
update_fields["bancho_updated_at"] = bancho_updated_at

query = f"""\
UPDATE beatmaps
SET {", ".join(f"{key} = :{key}" for key in update_fields)},
updated_at = NOW()
WHERE beatmap_id = :beatmap_id
RETURNING {READ_PARAMS}
"""
values = {"beatmap_id": beatmap_id} | update_fields
beatmap = await clients.database.fetch_one(query, values)
return cast(Beatmap, beatmap) if beatmap is not None else None
46 changes: 27 additions & 19 deletions app/repositories/clan_invites.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
from uuid import UUID

from app import clients
from app.typing import UNSET
from app.typing import Unset


class ClanInvite(TypedDict):
Expand All @@ -17,6 +19,12 @@ class ClanInvite(TypedDict):
updated_at: datetime


class BeatmapUpdateFields(TypedDict, total=False):
clan_id: int | Unset
uses: int | Unset
expires_at: datetime | Unset


READ_PARAMS = """\
clan_invite_id,
clan_id,
Expand Down Expand Up @@ -93,25 +101,25 @@ async def fetch_one(clan_invite_id: UUID) -> ClanInvite | None:

async def partial_update(
clan_invite_id: UUID,
clan_id: int | None = None,
uses: int | None = None,
expires_at: datetime | None = None,
clan_id: int | Unset = UNSET,
uses: int | Unset = UNSET,
expires_at: datetime | Unset = UNSET,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
expires_at: datetime | Unset = UNSET,
expires_at: datetime | None | Unset = UNSET,

) -> ClanInvite | None:
clan_invite = await clients.database.fetch_one(
query=f"""\
UPDATE clan_invites
SET clan_id = COALESCE(:clan_id, clan_id),
uses = COALESCE(:uses, uses),
expires_at = COALESCE(:expires_at, expires_at)
WHERE clan_invite_id = :clan_invite_id
RETURNING {READ_PARAMS}
""",
values={
"clan_invite_id": clan_invite_id,
"clan_id": clan_id,
"uses": uses,
"expires_at": expires_at,
},
)
update_fields: BeatmapUpdateFields = {}
if not isinstance(clan_id, Unset):
update_fields["clan_id"] = clan_id
if not isinstance(uses, Unset):
update_fields["uses"] = uses
if not isinstance(expires_at, Unset):
update_fields["expires_at"] = expires_at

query = f"""\
UPDATE clan_invites
SET {", ".join(f"{key} = :{key}" for key in update_fields)},
updated_at = NOW()
WHERE clan_invite_id = :clan_invite_id
RETURNING {READ_PARAMS}
"""
values = {"clan_invite_id": clan_invite_id} | update_fields
clan_invite = await clients.database.fetch_one(query, values)
return cast(ClanInvite, clan_invite) if not None else None
Loading