161 lines
4.0 KiB
Python
161 lines
4.0 KiB
Python
"""
|
|
File contains util functions.
|
|
"""
|
|
|
|
from datetime import datetime
|
|
|
|
from sqlalchemy import select
|
|
from sqlalchemy.orm import Session
|
|
|
|
from config import DakinyConfig
|
|
from database_setup.sql_data_classes import CouncilMembers, Votes, engine
|
|
|
|
NOMINEE_SPLITTER = "========================================\n\n"
|
|
|
|
|
|
def get_datetime(day: str) -> datetime:
|
|
"""
|
|
Takes in a string month and day and returns a datetime object of that Month/Day this year.
|
|
|
|
Args:
|
|
day (str): Month/Day in that format.
|
|
|
|
Returns:
|
|
datetime: Datetime representation of Month/Day.
|
|
"""
|
|
year = datetime.today().year
|
|
month = int(day.split("/")[0])
|
|
day = int(day.split("/")[1])
|
|
|
|
# ensure that year rolls over if current month is December and the next month is less than 12.
|
|
if datetime.today().month == 12 & month < 12:
|
|
year += 1
|
|
|
|
return datetime(year, month, day)
|
|
|
|
|
|
def get_election_cycle_name(config: DakinyConfig) -> str:
|
|
"""
|
|
Builds the unique name of the current election cycle
|
|
|
|
Args:
|
|
config (DakinyConfig): the config used to know the election cycle.
|
|
|
|
Returns:
|
|
str: the name of the election cycle
|
|
"""
|
|
cycle = get_current_cycle(config)
|
|
current_cycle = config.election_cycles[cycle]
|
|
|
|
return (
|
|
f"vote-{get_datetime(current_cycle.election_start).year}-{current_cycle.name}"
|
|
)
|
|
|
|
|
|
def get_dm_election_name(config: DakinyConfig) -> str:
|
|
"""
|
|
creates the name of the discord manager election
|
|
|
|
Args:
|
|
config (DakinyConfig): Config sent to get_election_cycle_name
|
|
|
|
Returns:
|
|
str: the name of the election
|
|
"""
|
|
|
|
return f"{get_election_cycle_name(config)}-dm"
|
|
|
|
|
|
def get_cl_election_name(config: DakinyConfig) -> str:
|
|
"""
|
|
creates the name of the clan leader election
|
|
|
|
Args:
|
|
config (DakinyConfig): Config sent to get_election_cycle_name
|
|
|
|
Returns:
|
|
str: the name of the election
|
|
"""
|
|
|
|
return f"{get_election_cycle_name(config)}-cl"
|
|
|
|
|
|
def get_current_cycle(config: DakinyConfig) -> int:
|
|
"""
|
|
Returns index for active election. An election is considered active between the election start and the term start.
|
|
|
|
Args:
|
|
config (DakinyConfig): Config object to use
|
|
|
|
Returns:
|
|
int: cycle index for active election. -1 for no active cycle
|
|
"""
|
|
|
|
today = datetime.today()
|
|
|
|
for cycle, election in enumerate(config.election_cycles):
|
|
if (
|
|
today.date() >= get_datetime(election.election_start).date()
|
|
and today.date() <= get_datetime(election.term_start).date()
|
|
):
|
|
return cycle
|
|
|
|
return -1
|
|
|
|
|
|
def build_votes_str(config: DakinyConfig) -> str:
|
|
"""
|
|
Creates a string that contains all nominees and their votes to be sent as a discord message.
|
|
|
|
Args:
|
|
config (DakinyConfig): Daking config object
|
|
|
|
Returns:
|
|
str: String that contains all nominees and their votes.
|
|
"""
|
|
|
|
votes: dict[int, int] = {}
|
|
|
|
with Session(engine) as session:
|
|
stmt = select(CouncilMembers).where(
|
|
CouncilMembers.ElectionCycle == get_election_cycle_name(config)
|
|
)
|
|
|
|
for nominee in session.scalars(stmt):
|
|
votes[nominee.MemberId] = 0
|
|
|
|
stmt = select(Votes).where(
|
|
Votes.ElectionCycle == get_election_cycle_name(config)
|
|
)
|
|
|
|
for vote in session.scalars(stmt):
|
|
if vote.VotedForId is not None:
|
|
votes[vote.VotedForId] += 1
|
|
|
|
votes_str: str = "===================="
|
|
for user_id, num_votes in votes.items():
|
|
votes_str += f"\n<@{user_id}>: "
|
|
|
|
for num in range(num_votes):
|
|
votes_str += "☑️"
|
|
|
|
return votes_str
|
|
|
|
|
|
def decorate_nomination(user_id: int, speech: str) -> str:
|
|
"""
|
|
Decorates a nomination speech so that it sits within multiple speeches better.
|
|
|
|
Args:
|
|
user_id (int): ID of user sending the speech
|
|
speech (str): Speech from the user
|
|
|
|
Returns:
|
|
str: Decorated speech
|
|
"""
|
|
msg = f"**Clan Council Nominee: <@{user_id}>**\n"
|
|
msg += NOMINEE_SPLITTER
|
|
msg += speech
|
|
|
|
return msg
|