Add a limit to the amount of event points one can earn weekly.
This commit is contained in:
@@ -5,7 +5,7 @@ File contains util functions.
|
||||
import os
|
||||
from datetime import datetime
|
||||
from enum import StrEnum
|
||||
from typing import Iterable
|
||||
from typing import Sequence
|
||||
|
||||
import nextcord
|
||||
from sqlalchemy import select, true, update
|
||||
@@ -14,6 +14,7 @@ from sqlalchemy.orm import Session
|
||||
from config import DakinyConfig
|
||||
from database_setup.sql_data_classes import (
|
||||
CouncilMembers,
|
||||
EventPointsLimit,
|
||||
Messages,
|
||||
ParticipationPoints,
|
||||
Votes,
|
||||
@@ -317,19 +318,21 @@ def decorate_dm_nomination(user_id: int, speech: str) -> str:
|
||||
return msg
|
||||
|
||||
|
||||
def record_event_participation(config: DakinyConfig, users: list[str], is_member: bool):
|
||||
def record_event_points(
|
||||
config: DakinyConfig, users: Sequence[str], is_member: bool, points: int
|
||||
):
|
||||
"""
|
||||
Adds participation 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.
|
||||
Adds event 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.
|
||||
|
||||
Args:
|
||||
config (DakinyConfig): Dakiny config for getting points
|
||||
users (list[int]): users that participated and need points.
|
||||
users (Sequence[int]): users that participated and need points.
|
||||
is_member (bool): whether the users are a discord member or not
|
||||
points (int): number of points to add
|
||||
"""
|
||||
|
||||
cycle = get_quarter_name(config)
|
||||
participation_point_value = config.params.event_participate_points
|
||||
with Session(engine) as session:
|
||||
for user in users:
|
||||
stmt = (
|
||||
@@ -344,49 +347,38 @@ def record_event_participation(config: DakinyConfig, users: list[str], is_member
|
||||
Cycle=cycle,
|
||||
UserId=user,
|
||||
DiscordMember=is_member,
|
||||
Points=participation_point_value,
|
||||
Points=points,
|
||||
)
|
||||
session.add(new_points)
|
||||
else:
|
||||
points.Points += participation_point_value
|
||||
# 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)
|
||||
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 record_event_host(config: DakinyConfig, user: int):
|
||||
"""
|
||||
Adds host points to the given user. Checks if they don't exist in order to make a new entry, otherwise adds points
|
||||
to the existing value.
|
||||
|
||||
Args:
|
||||
config (DakinyConfig): Dakiny config for getting points
|
||||
user (int): Id of user that hosted.
|
||||
"""
|
||||
cycle = get_quarter_name(config)
|
||||
host_point_value = config.params.event_host_points
|
||||
with Session(engine) as session:
|
||||
stmt = (
|
||||
select(ParticipationPoints)
|
||||
.where(ParticipationPoints.UserId == str(user))
|
||||
.where(ParticipationPoints.Cycle == cycle)
|
||||
)
|
||||
|
||||
points = session.scalar(stmt)
|
||||
if points is None:
|
||||
new_points = ParticipationPoints(
|
||||
Cycle=cycle,
|
||||
UserId=str(user),
|
||||
DiscordMember=True,
|
||||
Points=host_point_value,
|
||||
)
|
||||
session.add(new_points)
|
||||
else:
|
||||
points.Points += host_point_value
|
||||
|
||||
session.commit()
|
||||
|
||||
|
||||
async def update_clan_messages(guild: nextcord.Guild, channel_updates: Iterable[str]):
|
||||
async def update_clan_messages(guild: nextcord.Guild, channel_updates: Sequence[str]):
|
||||
"""
|
||||
Updates all clan messages to contents in MESSAGES_DIR.
|
||||
Does not to any git work. Is expected to be called after git is pulled.
|
||||
@@ -398,6 +390,7 @@ async def update_clan_messages(guild: nextcord.Guild, channel_updates: Iterable[
|
||||
|
||||
Args:
|
||||
guild (nextcord.Guild): Guild object that all channels and messages belong to.
|
||||
channel_updates: (Sequence[str]): a sequence of the channels to update.
|
||||
"""
|
||||
|
||||
dirs = [
|
||||
|
||||
Reference in New Issue
Block a user