Finish updating utils to use new models.

This commit is contained in:
2025-03-03 21:52:30 -05:00
parent 37fb9c1abb
commit cf5629a6dd
+40 -38
View File
@@ -110,19 +110,19 @@ def autofill_users_in_group_category(guild_id: int, group_id: int, category_id:
# If no category id, get all users in group.
if category_id == 0:
subq = select(col(Category.id)).where(Category.group_id == category_group.id)
subq = select(col(Category.id)).where(col(Category.group_id) == category_group.id)
stmt = select(distinct(col(Score.username))).where(col(Score.category_id).in_(subq))
else:
# get all users in category_id
subq = (
select(Category)
.where(Category.group_id == category_group.id)
.where(Category.id == category_id)
.where(col(Category.group_id) == category_group.id)
.where(col(Category.id) == category_id)
.subquery()
)
category = aliased(Category, subq)
stmt = select(distinct(Score.username)).where(Score.category == category.id)
stmt = select(distinct(col(Score.username))).where(col(Score.category_id) == category.id)
users_list: list[str] = list(session.scalars(stmt))
@@ -148,9 +148,9 @@ def autofill_users_in_guild(guild_id: int, user_begin: str) -> "list[str]":
list[str]: All users with submissions in the guild.
"""
with Session(engine) as session:
group_ids = select(col(CategoryGroup.id)).where(CategoryGroup.guild_id == guild_id)
category_ids = select(Category.id).where(col(Category.group_id).in_(group_ids))
stmt = select(distinct(Score.username)).where(Score.category_id.in_(category_ids))
group_ids = select(col(CategoryGroup.id)).where(col(CategoryGroup.guild_id) == guild_id)
category_ids = select(col(Category.id)).where(col(Category.group_id).in_(group_ids))
stmt = select(distinct(col(Score.username))).where(col(Score.category_id).in_(category_ids))
users_list: list[str] = list(session.scalars(stmt))
# if string empty, return all users
@@ -181,21 +181,21 @@ def format_highscore_message(session: Session, group: CategoryGroup) -> str:
"""
ret_string = f"**{group.name}**\n```"
stmt = select(Guild).where(Guild.id == group.guild_id)
stmt = select(Guild).where(col(Guild.id) == group.guild_id)
# Get all categories in the group
subq = (
select(CategoryGroup)
.where(CategoryGroup.guild_id == group.guild_id)
.where(CategoryGroup.name == group.name)
.where(col(CategoryGroup.guild_id) == group.guild_id)
.where(col(CategoryGroup.name) == group.name)
.subquery()
)
cat_group_alias = aliased(CategoryGroup, subq)
stmt = select(Category).where(Category.group_id == cat_group_alias.id)
stmt = select(Category).where(col(Category.group_id) == cat_group_alias.id)
# set up a map for the categories
# also grab the max category size
categories = []
categories: list[Category] = []
category_ids = []
max_cat_size: int = 0
pad_length = group.pad_length
@@ -206,7 +206,7 @@ def format_highscore_message(session: Session, group: CategoryGroup) -> str:
# get all scores in category group, load into data frame.
d_f = pd.read_sql_query(
sql=session.query(Score).where(Score.category_id.in_(category_ids)).statement,
sql=session.query(Score).where(col(Score.category_id).in_(category_ids)).statement,
con=engine,
)
@@ -217,15 +217,15 @@ def format_highscore_message(session: Session, group: CategoryGroup) -> str:
for category in categories:
# category name string
str_list[0] += f"{category.CategoryName}".ljust(pad_length, " ")
str_list[0] += f"{category.name}".ljust(pad_length, " ")
# ensure there is data to print
if category.CategoryId in groups_df.indices:
group_df = groups_df.get_group(category.CategoryId)
if category.id in groups_df.indices:
group_df = groups_df.get_group(category.id)
# process data. Want each group sorted correctly and only top X results
processed_group = (
group_df.sort_values("Score", ascending=category.IsAscending).head(category.Size).reset_index()
group_df.sort_values("Score", ascending=category.is_ascending).head(category.size).reset_index()
)
# add each score to list
@@ -237,19 +237,19 @@ def format_highscore_message(session: Session, group: CategoryGroup) -> str:
str_list[ind + 1] += format_score_string(
username,
score,
group.UsernameLength,
category.IsTimeRecord,
group.username_length,
category.is_time_record,
ind + 1,
pad_length,
)
# if not enough entries, pad out
elif ind + 1 <= category.Size:
elif ind + 1 <= category.size:
str_list[ind + 1] += f"{ind + 1}: ".ljust(pad_length, " ")
else:
str_list[ind + 1] += "".ljust(pad_length, " ")
else:
for ind in range(max_cat_size):
if ind + 1 <= category.Size:
if ind + 1 <= category.size:
str_list[ind + 1] += f"{ind + 1}: ".ljust(pad_length, " ")
else:
str_list[ind + 1] += "".ljust(pad_length, " ")
@@ -304,13 +304,13 @@ def format_score_string(
if is_time_record:
# MM:SS.ms is 7 characters long
# if there are single digit minutes, add a space for formatting.
score = get_time_str(score)
if score.find(":") and len(score.split(":", maxsplit=1)[0]) < 2:
score = " " + score
formatted_score = get_time_str(score)
if formatted_score.find(":") and len(formatted_score.split(":", maxsplit=1)[0]) < 2: # noqa: PLR2004
formatted_score = " " + formatted_score
else:
score = str(int(score)).rjust(7, " ")
formatted_score = str(int(score)).rjust(7, " ")
return f"{rank}: {username} - {score}".ljust(pad_length, " ")
return f"{rank}: {username} - {formatted_score}".ljust(pad_length, " ")
def get_time_str(time_in: float) -> str:
@@ -331,6 +331,7 @@ def get_time_str(time_in: float) -> str:
async def send_highscore_message(session: Session, guild: nextcord.Guild, group: CategoryGroup) -> nextcord.Message:
"""
Function takes care of checking if a message exists for a group.
Creates and sends/edits the message.
Args:
@@ -342,15 +343,15 @@ async def send_highscore_message(session: Session, guild: nextcord.Guild, group:
nextcord.Message: Message object that was posted with group contents.
"""
highscore_string = format_highscore_message(session, group)
channel = guild.get_channel(group.ChannelId)
channel = guild.get_channel(group.channel_id)
if group.MessageId is None:
if group.message_id is None:
# message doesn't exist.
message: nextcord.Message = await channel.send(highscore_string)
group.MessageId = message.id
session.commit()
else:
message: nextcord.Message = await channel.fetch_message(group.MessageId)
message: nextcord.Message = await channel.fetch_message(group.message_id)
await message.edit(content=highscore_string)
return message
@@ -365,37 +366,38 @@ async def submit_score(
) -> None:
"""
Adds passed score to group. Then updates group message by calling send_highscore_message.
Ensures that a highscores channel exists.
Args:
interaction (nextcord.Interaction): Interaction that caused the score submission.
group (CategoryGroup): group to submit score to.
category_id (Category): Category to submit score to.
group_id (int): group id to submit score to.
category_id (int): Category id to submit score to.
username (str): The user who submitted the score.
score (str): Score to submit. Need to convert to correct type.
"""
with Session(engine) as session:
stmt = select(Category.IsTimeRecord).where(Category.CategoryId == category_id)
stmt = select(col(Category.is_time_record)).where(col(Category.id) == category_id)
is_time_record = session.scalar(stmt)
if is_time_record:
minutes = float(score.split(":", maxsplit=1)[0])
seconds = float(score.split(":", maxsplit=1)[1])
score = (minutes * 60) + seconds
score_f = (minutes * 60) + seconds
else:
score = float(score)
score_f = float(score)
stmt = select(Score).where(Score.UserName == username).where(Score.CategoryId == category_id)
stmt = select(Score).where(col(Score.username) == username).where(col(Score.category_id) == category_id)
existing_score = session.scalar(stmt)
if existing_score:
existing_score.Score = score
existing_score.Score = score_f
else:
score_obj = Score(UserName=username, Score=score, CategoryId=category_id)
score_obj = Score(username=username, score=score_f, category_id=category_id)
session.add(score_obj)
session.commit()
stmt = select(CategoryGroup).where(CategoryGroup.GroupId == group_id)
stmt = select(CategoryGroup).where(col(CategoryGroup.id) == group_id)
group = session.scalar(stmt)
await send_highscore_message(session, interaction.guild, group)