Skip to content

Commit

Permalink
db: Convert Chart.level to str (denotes displayed level)
Browse files Browse the repository at this point in the history
  • Loading branch information
beer-psi committed Aug 12, 2023
1 parent fc1bbf0 commit 7526839
Show file tree
Hide file tree
Showing 8 changed files with 144 additions and 39 deletions.
5 changes: 4 additions & 1 deletion alembic.ini
Original file line number Diff line number Diff line change
Expand Up @@ -69,10 +69,13 @@ sqlalchemy.url = sqlite:///database/database.sqlite3
# detail and examples

# format using "black" - use the console_scripts runner, against the "black" entrypoint
hooks = black
hooks = black isort
black.type = console_scripts
black.entrypoint = black
black.options = -l 79 REVISION_SCRIPT_FILENAME
isort.type = console_scripts
isort.entrypoint = isort
isort.options = REVISION_SCRIPT_FILENAME

# Logging configuration
[loggers]
Expand Down
10 changes: 7 additions & 3 deletions cogs/botutils.py
Original file line number Diff line number Diff line change
Expand Up @@ -146,13 +146,17 @@ async def annotate_song(
return annotated_song
annotated_song.internal_level = chart_data.const

level = chart_data.level
annotated_song.level = str(floor(level)) + ("+" if level * 10 % 10 >= 5 else "")
annotated_song.level = chart_data.level

try:
numeric_level = float(chart_data.level.replace("+", ".5"))
except ValueError:
numeric_level = 0

internal_level = (
annotated_song.internal_level
if annotated_song.internal_level != None
else level
else numeric_level
)

annotated_song.play_rating = calculate_rating(song.score, internal_level)
Expand Down
14 changes: 5 additions & 9 deletions cogs/misc.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
from chunithm_net.entities.enums import Difficulty
from cogs.botutils import UtilsCog
from database.models import Chart, Prefix, Song
from utils import floor_to_ndp, format_level, yt_search_link
from utils import floor_to_ndp, yt_search_link
from utils.calculation.overpower import (
calculate_overpower_base,
calculate_overpower_max,
Expand Down Expand Up @@ -381,8 +381,7 @@ async def find(self, ctx: Context, level: str):
query_level = float(level)
stmt = stmt.where(Chart.const == query_level)
else:
query_level = float(level.replace("+", ".5"))
stmt = stmt.where(Chart.level == query_level)
stmt = stmt.where(Chart.level == level)
except ValueError:
raise commands.BadArgument("Please enter a valid level or chart constant.")

Expand Down Expand Up @@ -428,8 +427,7 @@ async def random(self, ctx: Context, level: str, count: int = 3):
query_level = float(level)
stmt = stmt.where(Chart.const == query_level)
else:
query_level = float(level.replace("+", ".5"))
stmt = stmt.where(Chart.level == query_level)
stmt = stmt.where(Chart.level == level)
except ValueError:
raise commands.BadArgument(
"Please enter a valid level or chart constant."
Expand All @@ -444,7 +442,6 @@ async def random(self, ctx: Context, level: str, count: int = 3):
embeds: list[discord.Embed] = []
for chart in charts:
difficulty = Difficulty.from_short_form(chart.difficulty)
chart_level = format_level(chart.level)

if chart.sdvxin_chart_view is not None:
url = chart.sdvxin_chart_view.url
Expand All @@ -461,7 +458,7 @@ async def random(self, ctx: Context, level: str, count: int = 3):
.add_field(name="Category", value=chart.song.genre)
.add_field(
name=str(difficulty),
value=f"[{chart_level}{f' ({chart.const})' if chart.const is not None else ''}]({url})",
value=f"[{chart.level}{f' ({chart.const})' if chart.const is not None else ''}]({url})",
)
)
await ctx.reply(embeds=embeds, mention_author=False)
Expand Down Expand Up @@ -525,7 +522,6 @@ async def recommend(
assert chart.const is not None

difficulty = Difficulty.from_short_form(chart.difficulty)
chart_level = format_level(chart.level)
rating_diff = max_rating - chart.const

# if-else intentionally used to ensure State-of-the-Art Shitcode compliance
Expand Down Expand Up @@ -575,7 +571,7 @@ async def recommend(
.add_field(name="Category", value=chart.song.genre)
.add_field(
name=str(difficulty),
value=f"[{chart_level}{f' ({chart.const})' if chart.const is not None else ''}]({url})",
value=f"[{chart.level}{f' ({chart.const})' if chart.const is not None else ''}]({url})",
)
.add_field(
name="Target Score",
Expand Down
3 changes: 1 addition & 2 deletions cogs/search.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
from database.models import Alias, Chart, Song
from utils import (
did_you_mean_text,
format_level,
release_to_chunithm_version,
yt_search_link,
)
Expand Down Expand Up @@ -193,7 +192,7 @@ async def info(self, ctx: Context, *, query: str):
if chart.sdvxin_chart_view is not None
else yt_search_link(song.title, chart.difficulty)
)
desc = f"[{chart.difficulty[0]}]({url}) {format_level(chart.level)}"
desc = f"[{chart.difficulty[0]}]({url}) {chart.level}"
if chart.const != 0:
desc += f" ({chart.const:.1f})"
chart_level_desc.append(desc)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
"""convert level into string
Revision ID: 1e5d59ce579e
Revises: 50f211f12e75
Create Date: 2023-08-12 22:17:48.946733
"""
from typing import Sequence, Union

import sqlalchemy as sa
from alembic import op

# revision identifiers, used by Alembic.
revision: str = "1e5d59ce579e"
down_revision: Union[str, None] = "50f211f12e75"
branch_labels: Union[str, Sequence[str], None] = None
depends_on: Union[str, Sequence[str], None] = None


def upgrade() -> None:
with op.batch_alter_table("chunirec_charts", schema=None) as batch_op:
batch_op.alter_column(
"level",
existing_type=sa.REAL(),
existing_nullable=False,
type_=sa.VARCHAR(),
nullable=False,
)

op.execute(
"""
UPDATE chunirec_charts
SET level = replace(replace(level, ".5", "+"), ".0", "")
WHERE difficulty != "WE"
"""
)

op.execute(
"""
UPDATE chunirec_charts
SET level = replace(replace(chunirec_songs.title, rtrim(chunirec_songs.title, replace(chunirec_songs.title, '【', '')), ''), "】", ""),
const = NULL
FROM chunirec_songs
WHERE chunirec_songs.id = chunirec_charts.song_id AND chunirec_charts.difficulty = "WE"
"""
)

op.execute(
"""
UPDATE chunirec_songs
SET title = rtrim(rtrim(title, replace(title, '【', '')), '【')
WHERE chunithm_id >= 8000
"""
)


def downgrade() -> None:
op.execute(
"""
UPDATE chunirec_songs
SET title = chunirec_songs.title || "【" || chunirec_charts.level || "】"
FROM chunirec_charts
WHERE chunirec_songs.id = chunirec_charts.song_id AND chunirec_charts.difficulty = "WE"
"""
)

op.execute(
"""
UPDATE chunirec_charts
SET level = 0.0, const = 0.0
WHERE chunirec_charts.difficulty = "WE"
"""
)

op.execute(
"""
UPDATE chunirec_charts
SET level = CASE
WHEN level like '%+' THEN replace(level, "+", ".5")
ELSE level || ".0"
END
WHERE difficulty != "WE"
"""
)

with op.batch_alter_table("chunirec_charts", schema=None) as batch_op:
batch_op.alter_column(
"level",
existing_type=sa.VARCHAR(),
existing_nullable=False,
type_=sa.REAL(),
nullable=False,
)
2 changes: 1 addition & 1 deletion database/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ class Chart(Base):
)

difficulty: Mapped[str] = mapped_column(nullable=False)
level: Mapped[float] = mapped_column(nullable=False)
level: Mapped[str] = mapped_column(nullable=False)
const: Mapped[Optional[float]] = mapped_column(nullable=True)

maxcombo: Mapped[Optional[int]] = mapped_column(nullable=True)
Expand Down
52 changes: 33 additions & 19 deletions update_db.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,40 +84,42 @@ class ZetarakuChunithmData:
}

MANUAL_MAPPINGS: dict[str, dict[str, str]] = {
"1bc5d471609c4d10": { # folern【狂】
"id": "8166",
"catname": "ORIGINAL",
"image": "0511952ab823d845.jpg",
},
"7a561ab609a0629d": { # Trackless wilderness【狂】
"id": "8227",
"catname": "ORIGINAL",
"image": "168de844aeef254b.jpg",
"title": "Trackless wilderness",
"we_kanji": "狂",
"image": "629be924b3383e08.jpg",
},
"e6605126a95c4c8d": { # Trrricksters!!【狂】
"id": "8228",
"catname": "ORIGINAL",
"image": "1195656064a159f0.jpg",
"title": "Trrricksters!!",
"we_kanji": "狂",
"image": "7615de9e9eced518.jpg",
},
}
for idx, random in enumerate(
# Random WE, A through F
[
"d8b8af2016eec2f0",
"5a0bc7702113a633",
"948e0c4b67f4269d",
"56e583c091b4295c",
"49794fec968b90ba",
"b9df9d9d74b372d9",
("d8b8af2016eec2f0", "97af9ed62e768d73.jpg"),
("5a0bc7702113a633", "fd4a488ed2bc67d8.jpg"),
("948e0c4b67f4269d", "ce911dfdd8624a7c.jpg"),
("56e583c091b4295c", "6a3201f1b63ff9a3.jpg"),
("49794fec968b90ba", "d43ab766613ba19e.jpg"),
("b9df9d9d74b372d9", "4a359278c6108748.jpg"),
]
):
MANUAL_MAPPINGS[random] = {
random_id, random_image = random
MANUAL_MAPPINGS[random_id] = {
"id": str(8244 + idx),
"catname": "VARIETY",
"image": "ca580486c86bd49b.jpg",
"title": "Random",
"we_kanji": f"分{chr(65 + idx)}",
"image": random_image,
}

WORLD_END_REGEX = re.compile(r"【.{1,2}】$", re.MULTILINE)
WORLD_END_REGEX = re.compile(r"【(.{1,2})】$", re.MULTILINE)


def normalize_title(title: str, remove_we_kanji: bool = False) -> str:
Expand Down Expand Up @@ -382,7 +384,8 @@ async def update_db(async_session: async_sessionmaker[AsyncSession]):
dict(
id=song.meta.id,
chunithm_id=chunithm_id,
title=song.meta.title,
# Don't use song.meta.title
title=chunithm_song["title"],
chunithm_catcode=chunithm_catcode,
genre=song.meta.genre,
artist=song.meta.artist,
Expand All @@ -392,7 +395,7 @@ async def update_db(async_session: async_sessionmaker[AsyncSession]):
zetaraku_jacket=zetaraku_jacket,
)
)
for difficulty in ["BAS", "ADV", "EXP", "MAS", "ULT", "WE"]:
for difficulty in ["BAS", "ADV", "EXP", "MAS", "ULT"]:
if (chart := getattr(song.data, difficulty)) is not None:
if 0 < chart.level <= 9.5:
chart.const = chart.level
Expand All @@ -402,12 +405,23 @@ async def update_db(async_session: async_sessionmaker[AsyncSession]):
dict(
song_id=song.meta.id,
difficulty=difficulty,
level=chart.level,
level=str(chart.level).replace(".5", "+").replace(".0", ""),
const=None if chart.is_const_unknown == 1 else chart.const,
maxcombo=chart.maxcombo if chart.maxcombo != 0 else None,
)
)

if (chart := getattr(song.data, "WE")) is not None:
inserted_charts.append(
dict(
song_id = song.meta.id,
difficulty="WE",
level=chunithm_song["we_kanji"],
const=None,
maxcombo=chart.maxcombo if chart.maxcombo != 0 else None,
)
)

async with async_session() as session, session.begin():
insert_statement = insert(Song).values(inserted_songs)
upsert_statement = insert_statement.on_conflict_do_update(
Expand Down
4 changes: 0 additions & 4 deletions utils/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,6 @@ def floor_to_ndp(number: "T", dp: int) -> "T":
return type(number)(round(decimal.Decimal(str(number)), dp)) # type: ignore


def format_level(level: float) -> str:
return str(level).replace(".0", "").replace(".5", "+")


def did_you_mean_text(result: "Song", alias: "Alias | None") -> str:
did_you_mean = f"**{escape_markdown(result.title)}**"
if alias is not None:
Expand Down

0 comments on commit 7526839

Please sign in to comment.