Revamped utils to handle clan and event points in one. Updated record as needed.
This commit is contained in:
+12
-8
@@ -5,7 +5,7 @@ from discord import TextInputStyle
|
|||||||
from nextcord.ext import commands
|
from nextcord.ext import commands
|
||||||
|
|
||||||
from DakinySettings import settings
|
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"):
|
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)
|
await interaction.response.send_message("Please only provide 1 recipient.", ephemeral=True)
|
||||||
return
|
return
|
||||||
elif recipient is not None: # noqa: RET505
|
elif recipient is not None: # noqa: RET505
|
||||||
record_event_points(
|
record_points(
|
||||||
[str(recipient.id)],
|
[str(recipient.id)],
|
||||||
settings.point_settings.participate_points,
|
settings.point_settings.clan_points,
|
||||||
|
PointType.CLAN,
|
||||||
is_member=True,
|
is_member=True,
|
||||||
)
|
)
|
||||||
recipient_str = f"<@{recipient.id}>"
|
recipient_str = f"<@{recipient.id}>"
|
||||||
elif non_discord_recipient is not None:
|
elif non_discord_recipient is not None:
|
||||||
record_event_points(
|
record_points(
|
||||||
[non_discord_recipient],
|
[non_discord_recipient],
|
||||||
settings.point_settings.participate_points,
|
settings.point_settings.clan_points,
|
||||||
|
PointType.CLAN,
|
||||||
is_member=False,
|
is_member=False,
|
||||||
)
|
)
|
||||||
recipient_str = f'"{non_discord_recipient}"'
|
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:
|
if discord_users:
|
||||||
discord_users_list: list[str] = [str(user.id) for user in discord_users if user.id is not host_id]
|
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,
|
discord_users_list,
|
||||||
settings.point_settings.participate_points,
|
settings.point_settings.participate_points,
|
||||||
|
PointType.EVENT,
|
||||||
is_member=True,
|
is_member=True,
|
||||||
)
|
)
|
||||||
|
|
||||||
if non_discord_users:
|
if non_discord_users:
|
||||||
non_discord_users_list: list[str] = non_discord_users.split("\n")
|
non_discord_users_list: list[str] = non_discord_users.split("\n")
|
||||||
record_event_points(
|
record_points(
|
||||||
non_discord_users_list,
|
non_discord_users_list,
|
||||||
settings.point_settings.participate_points,
|
settings.point_settings.participate_points,
|
||||||
|
PointType.EVENT,
|
||||||
is_member=False,
|
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)
|
||||||
|
|||||||
@@ -15,9 +15,9 @@ from sqlalchemy.orm import Session
|
|||||||
from DakinySettings import settings
|
from DakinySettings import settings
|
||||||
from database_setup.sql_data_classes import (
|
from database_setup.sql_data_classes import (
|
||||||
CouncilMembers,
|
CouncilMembers,
|
||||||
EventPoints,
|
|
||||||
EventPointsLimit,
|
|
||||||
Messages,
|
Messages,
|
||||||
|
Points,
|
||||||
|
PointsLimit,
|
||||||
Votes,
|
Votes,
|
||||||
engine,
|
engine,
|
||||||
)
|
)
|
||||||
@@ -30,6 +30,7 @@ MAX_DISCORD_MESSAGE_LENGTH = 2000
|
|||||||
GUILDS = [1000247361639030804, 239758371358638080]
|
GUILDS = [1000247361639030804, 239758371358638080]
|
||||||
|
|
||||||
VoteType = StrEnum("VoteType", ["COUNCIL", "DISCORDMANAGER", "CLANLEADER"])
|
VoteType = StrEnum("VoteType", ["COUNCIL", "DISCORDMANAGER", "CLANLEADER"])
|
||||||
|
PointType = StrEnum("PointType", ["CLAN", "EVENT"])
|
||||||
|
|
||||||
|
|
||||||
def get_datetime(day: str) -> datetime:
|
def get_datetime(day: str) -> datetime:
|
||||||
@@ -290,41 +291,46 @@ def decorate_dm_nomination(user_id: int, speech: str) -> str:
|
|||||||
return msg
|
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
|
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:
|
Args:
|
||||||
users (Sequence[int]): users that participated and need points.
|
users (Sequence[int]): users that participated and need points.
|
||||||
add_points (int): number of points to add
|
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
|
is_member (bool): whether the users are a discord member or not
|
||||||
"""
|
"""
|
||||||
quarter_name = get_quarter_name()
|
quarter_name = get_quarter_name()
|
||||||
with Session(engine) as session:
|
with Session(engine) as session:
|
||||||
for user in users:
|
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)
|
points_obj = session.scalar(stmt)
|
||||||
if points is None:
|
add_points = get_allowed_points(session, point_type, user, add_points)
|
||||||
add_points = get_allowed_points(session, settings.point_settings.events_weekly_limit, user, add_points)
|
add_clan_points = add_points if point_type == PointType.CLAN else 0
|
||||||
new_points = EventPoints(
|
add_event_points = add_points if point_type == PointType.EVENT else 0
|
||||||
|
if points_obj is None:
|
||||||
|
new_points = Points(
|
||||||
QuarterName=quarter_name,
|
QuarterName=quarter_name,
|
||||||
UserId=user,
|
UserId=user,
|
||||||
DiscordMember=is_member,
|
DiscordMember=is_member,
|
||||||
Points=add_points,
|
EventPoints=add_event_points,
|
||||||
|
ClanPoints=add_clan_points,
|
||||||
)
|
)
|
||||||
session.add(new_points)
|
session.add(new_points)
|
||||||
else:
|
else:
|
||||||
add_points = get_allowed_points(session, settings.point_settings.events_weekly_limit, user, add_points)
|
points_obj.EventPoints += add_event_points
|
||||||
points.Points += add_points
|
points_obj.ClanPoints += add_clan_points
|
||||||
|
|
||||||
session.commit()
|
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.
|
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:
|
Args:
|
||||||
session (Session): Open connection to database.
|
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.
|
user (str): user to be given points. Used to check a user's limit.
|
||||||
points (int): maximum number of points to give to user.
|
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
|
# check for limit
|
||||||
today = datetime.now(tz=UTC).date()
|
today = datetime.now(tz=UTC).date()
|
||||||
this_week = f"{today.isocalendar().year}-{today.isocalendar().week}"
|
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
|
# if under limit, can add up to however many points would make the current limit equal to the
|
||||||
# allowed limit.
|
# allowed limit.
|
||||||
limit = session.scalar(stmt)
|
limit_obj = session.scalar(stmt)
|
||||||
if limit is None:
|
if limit_obj is None:
|
||||||
new_limit = EventPointsLimit(UserId=user, Points=points, Week=this_week)
|
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
|
add_points = points
|
||||||
session.add(new_limit)
|
session.add(new_limit)
|
||||||
else:
|
else:
|
||||||
add_points = points if limit.Points + points <= points_limit else (max(0, points_limit - limit.Points))
|
curr_points = limit_obj.EventPoints if point_type == PointType.EVENT else limit_obj.ClanPoints
|
||||||
limit.Points += add_points
|
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()
|
session.commit()
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user