From a56b47a5fa1eb408d975ef181ce4019d06f606e8 Mon Sep 17 00:00:00 2001 From: imcompiling Date: Sun, 22 Dec 2024 15:34:14 -0500 Subject: [PATCH] Revamped utils to handle clan and event points in one. Updated record as needed. --- cogs/commands/record.py | 20 ++++++++------ utils.py | 59 ++++++++++++++++++++++++++--------------- 2 files changed, 50 insertions(+), 29 deletions(-) diff --git a/cogs/commands/record.py b/cogs/commands/record.py index 0b13003..069fb13 100644 --- a/cogs/commands/record.py +++ b/cogs/commands/record.py @@ -5,7 +5,7 @@ from discord import TextInputStyle from nextcord.ext import commands from DakinySettings import settings -from utils import GUILDS, MAX_DISCORD_MESSAGE_LENGTH, record_event_points +from utils import GUILDS, MAX_DISCORD_MESSAGE_LENGTH, PointType, record_points class RecordCog(commands.Cog, name="Record Commands"): @@ -117,16 +117,18 @@ class RecordCog(commands.Cog, name="Record Commands"): await interaction.response.send_message("Please only provide 1 recipient.", ephemeral=True) return elif recipient is not None: # noqa: RET505 - record_event_points( + record_points( [str(recipient.id)], - settings.point_settings.participate_points, + settings.point_settings.clan_points, + PointType.CLAN, is_member=True, ) recipient_str = f"<@{recipient.id}>" elif non_discord_recipient is not None: - record_event_points( + record_points( [non_discord_recipient], - settings.point_settings.participate_points, + settings.point_settings.clan_points, + PointType.CLAN, is_member=False, ) recipient_str = f'"{non_discord_recipient}"' @@ -337,18 +339,20 @@ def process_event(discord_users: list[nextcord.Member], non_discord_users: str, """ if discord_users: discord_users_list: list[str] = [str(user.id) for user in discord_users if user.id is not host_id] - record_event_points( + record_points( discord_users_list, settings.point_settings.participate_points, + PointType.EVENT, is_member=True, ) if non_discord_users: non_discord_users_list: list[str] = non_discord_users.split("\n") - record_event_points( + record_points( non_discord_users_list, settings.point_settings.participate_points, + PointType.EVENT, is_member=False, ) - record_event_points([host_id], settings.point_settings.host_points, is_member=True) + record_points([host_id], settings.point_settings.host_points, PointType.EVENT, is_member=True) diff --git a/utils.py b/utils.py index 66fda0d..fc21c61 100644 --- a/utils.py +++ b/utils.py @@ -15,9 +15,9 @@ from sqlalchemy.orm import Session from DakinySettings import settings from database_setup.sql_data_classes import ( CouncilMembers, - EventPoints, - EventPointsLimit, Messages, + Points, + PointsLimit, Votes, engine, ) @@ -30,6 +30,7 @@ MAX_DISCORD_MESSAGE_LENGTH = 2000 GUILDS = [1000247361639030804, 239758371358638080] VoteType = StrEnum("VoteType", ["COUNCIL", "DISCORDMANAGER", "CLANLEADER"]) +PointType = StrEnum("PointType", ["CLAN", "EVENT"]) def get_datetime(day: str) -> datetime: @@ -290,41 +291,46 @@ def decorate_dm_nomination(user_id: int, speech: str) -> str: return msg -def record_event_points(users: Sequence[str], add_points: int, *, is_member: bool) -> None: +def record_points(users: Sequence[str], add_points: int, point_type: PointType, *, is_member: bool) -> None: """ - Adds event points to all users listed. + Adds points to all users listed. Checks if they don't exist in order to make a new entry, otherwise - adds points to the existing value. Can be used for hosting or participating. + adds points to the existing value. Can be used for hosting or participating in events or for the clan + points. Args: users (Sequence[int]): users that participated and need points. add_points (int): number of points to add + point_type (PointType): which point type to add to is_member (bool): whether the users are a discord member or not """ quarter_name = get_quarter_name() with Session(engine) as session: for user in users: - stmt = select(EventPoints).where(EventPoints.UserId == user).where(EventPoints.QuarterName == quarter_name) + stmt = select(Points).where(Points.UserId == user).where(Points.QuarterName == quarter_name) - points = session.scalar(stmt) - if points is None: - add_points = get_allowed_points(session, settings.point_settings.events_weekly_limit, user, add_points) - new_points = EventPoints( + points_obj = session.scalar(stmt) + add_points = get_allowed_points(session, point_type, user, add_points) + add_clan_points = add_points if point_type == PointType.CLAN else 0 + add_event_points = add_points if point_type == PointType.EVENT else 0 + if points_obj is None: + new_points = Points( QuarterName=quarter_name, UserId=user, DiscordMember=is_member, - Points=add_points, + EventPoints=add_event_points, + ClanPoints=add_clan_points, ) session.add(new_points) else: - add_points = get_allowed_points(session, settings.point_settings.events_weekly_limit, user, add_points) - points.Points += add_points + points_obj.EventPoints += add_event_points + points_obj.ClanPoints += add_clan_points session.commit() -def get_allowed_points(session: Session, points_limit: int, user: str, points: int) -> int: +def get_allowed_points(session: Session, point_type: PointType, user: str, points: int) -> int: """ Gets the maximum number of points a user can be given. @@ -334,7 +340,7 @@ def get_allowed_points(session: Session, points_limit: int, user: str, points: i Args: session (Session): Open connection to database. - points_limit (int): Maximum number of points a user can be given in a week. + point_type (PointType): Which type of points to check the limit for. user (str): user to be given points. Used to check a user's limit. points (int): maximum number of points to give to user. @@ -344,18 +350,29 @@ def get_allowed_points(session: Session, points_limit: int, user: str, points: i # check for limit today = datetime.now(tz=UTC).date() this_week = f"{today.isocalendar().year}-{today.isocalendar().week}" - stmt = select(EventPointsLimit).where(EventPointsLimit.UserId == user).where(EventPointsLimit.Week == this_week) + points_limit = ( + settings.point_settings.events_weekly_limit + if point_type == PointType.EVENT + else settings.point_settings.clan_points_weekly_limit + ) + stmt = select(PointsLimit).where(PointsLimit.UserId == user).where(PointsLimit.Week == this_week) # if under limit, can add up to however many points would make the current limit equal to the # allowed limit. - limit = session.scalar(stmt) - if limit is None: - new_limit = EventPointsLimit(UserId=user, Points=points, Week=this_week) + limit_obj = session.scalar(stmt) + if limit_obj is None: + clan_points = points if point_type == PointType.CLAN else 0 + event_points = points if point_type == PointType.EVENT else 0 + new_limit = PointsLimit(UserId=user, Week=this_week, EventPoints=event_points, ClanPoints=clan_points) add_points = points session.add(new_limit) else: - add_points = points if limit.Points + points <= points_limit else (max(0, points_limit - limit.Points)) - limit.Points += add_points + curr_points = limit_obj.EventPoints if point_type == PointType.EVENT else limit_obj.ClanPoints + add_points = points if curr_points + points <= points_limit else (max(0, points_limit - curr_points)) + if point_type == PointType.EVENT: + limit_obj.EventPoints += add_points + else: + limit_obj.ClanPoints += add_points session.commit()