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
+45 -14
View File
@@ -319,7 +319,7 @@ def decorate_dm_nomination(user_id: int, speech: str) -> str:
def record_event_points( 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 Adds event points to all users listed. Checks if they don't exist in order to make a new entry, otherwise
@@ -332,27 +332,57 @@ def record_event_points(
points (int): number of points to add points (int): number of points to add
""" """
cycle = get_quarter_name(config) quarter_name = get_quarter_name(config)
with Session(engine) as session: with Session(engine) as session:
for user in users: for user in users:
stmt = ( stmt = (
select(ParticipationPoints) select(ParticipationPoints)
.where(ParticipationPoints.UserId == user) .where(ParticipationPoints.UserId == user)
.where(ParticipationPoints.Cycle == cycle) .where(ParticipationPoints.Cycle == quarter_name)
) )
points = session.scalar(stmt) points = session.scalar(stmt)
if points is None: if points is None:
add_points = get_allowed_points(
session, config.params.event_points_weekly_limit, user, add_points
)
new_points = ParticipationPoints( new_points = ParticipationPoints(
Cycle=cycle, Cycle=quarter_name,
UserId=user, UserId=user,
DiscordMember=is_member, DiscordMember=is_member,
Points=points, Points=add_points,
) )
session.add(new_points) session.add(new_points)
else: else:
add_points = get_allowed_points(
session, config.params.event_points_weekly_limit, user, 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 # check for limit
this_week = f"{datetime.today().isocalendar().year}-{datetime.today().isocalendar().week}" this_week = (
f"{datetime.today().isocalendar().year}-{datetime.today().isocalendar().week}"
)
stmt = ( stmt = (
select(EventPointsLimit) select(EventPointsLimit)
.where(EventPointsLimit.UserId == user) .where(EventPointsLimit.UserId == user)
@@ -362,21 +392,22 @@ def record_event_points(
# 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 = 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 = ( add_points = (
points points
if limit.Points + points <= config.params.event_points_weekly_limit if limit.Points + points <= points_limit
else ( else (points_limit - limit.Points if points_limit - limit.Points > 0 else 0)
config.params.event_points_weekly_limit - limit.Points
if config.params.event_points_weekly_limit - limit.Points > 0
else 0
) )
)
limit.Points += add_points limit.Points += add_points
points.Points += add_points
session.commit() session.commit()
return add_points
async def update_clan_messages(guild: nextcord.Guild, channel_updates: Sequence[str]): async def update_clan_messages(guild: nextcord.Guild, channel_updates: Sequence[str]):
""" """