Move limit object management into separate function. Need to also get allowed points when creating a new quarter's event point tracker.
build_develop / build (push) Successful in 1m27s

This commit is contained in:
2024-05-19 22:27:47 -04:00
parent 34da664af0
commit cbfd6e20bc
+57 -26
View File
@@ -319,7 +319,7 @@ def decorate_dm_nomination(user_id: int, speech: str) -> str:
def record_event_points(
config: DakinyConfig, users: Sequence[str], is_member: bool, points: int
config: DakinyConfig, users: Sequence[str], is_member: bool, add_points: int
):
"""
Adds event points to all users listed. Checks if they don't exist in order to make a new entry, otherwise
@@ -332,52 +332,83 @@ def record_event_points(
points (int): number of points to add
"""
cycle = get_quarter_name(config)
quarter_name = get_quarter_name(config)
with Session(engine) as session:
for user in users:
stmt = (
select(ParticipationPoints)
.where(ParticipationPoints.UserId == user)
.where(ParticipationPoints.Cycle == cycle)
.where(ParticipationPoints.Cycle == quarter_name)
)
points = session.scalar(stmt)
if points is None:
add_points = get_allowed_points(
session, config.params.event_points_weekly_limit, user, add_points
)
new_points = ParticipationPoints(
Cycle=cycle,
Cycle=quarter_name,
UserId=user,
DiscordMember=is_member,
Points=points,
Points=add_points,
)
session.add(new_points)
else:
# check for limit
this_week = f"{datetime.today().isocalendar().year}-{datetime.today().isocalendar().week}"
stmt = (
select(EventPointsLimit)
.where(EventPointsLimit.UserId == user)
.where(EventPointsLimit.Week == this_week)
add_points = get_allowed_points(
session, config.params.event_points_weekly_limit, user, add_points
)
# if under limit, can add up to however many points would make the current limit equal to the
# allowed limit.
limit = session.scalar(stmt)
add_points = (
points
if limit.Points + points <= config.params.event_points_weekly_limit
else (
config.params.event_points_weekly_limit - limit.Points
if config.params.event_points_weekly_limit - limit.Points > 0
else 0
)
)
limit.Points += add_points
points.Points += add_points
session.commit()
def get_allowed_points(
session: Session, points_limit: int, user: str, points: int
) -> int:
"""
Gets the maximum number of points a user can be given. Searches how many points they have been given
this week and subtracts that from the weekly limit. If under, returns up to "points" number of points (will not
go over points_limit) otherwise returns 0. Updates the limit object or creates a new one if one isn't found.
Args:
session (Session): Open connection to database.
points_limit (int): Maximum number of points a user can be given in a week.
user (str): user to be given points. Used to check a user's limit.
points (int): maximum number of points to give to user.
Returns:
int: how many points are allowed to be given to the user.
"""
# check for limit
this_week = (
f"{datetime.today().isocalendar().year}-{datetime.today().isocalendar().week}"
)
stmt = (
select(EventPointsLimit)
.where(EventPointsLimit.UserId == user)
.where(EventPointsLimit.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)
add_points = points
session.add(new_limit)
else:
add_points = (
points
if limit.Points + points <= points_limit
else (points_limit - limit.Points if points_limit - limit.Points > 0 else 0)
)
limit.Points += add_points
session.commit()
return add_points
async def update_clan_messages(guild: nextcord.Guild, channel_updates: Sequence[str]):
"""
Updates all clan messages to contents in MESSAGES_DIR.