From a1896416fc948781bd8c86a33ec845b76ee21dce Mon Sep 17 00:00:00 2001 From: imcompiling Date: Sat, 16 Dec 2023 23:14:23 -0500 Subject: [PATCH] Make highcouncil vote string builder. Break out repeated code into helper function. --- utils.py | 76 ++++++++++++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 71 insertions(+), 5 deletions(-) diff --git a/utils.py b/utils.py index 1e3cc84..f6f3851 100644 --- a/utils.py +++ b/utils.py @@ -133,15 +133,81 @@ def build_votes_str(config: DakinyConfig) -> str: 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 += "☑️" + votes_str += user_vote_str(votes) return votes_str +def build_highcouncil_votes_str(config: DakinyConfig) -> str: + """ + Creates a string that contains all high council 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. + """ + + cl_votes: dict[int, int] = {} + dm_votes: dict[int, int] = {} + + with Session(engine) as session: + stmt = select(CouncilMembers).where( + CouncilMembers.ElectionCycle == get_cl_election_name(config) + ) + + for nominee in session.scalars(stmt): + cl_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: + cl_votes[vote.VotedForId] += 1 + + stmt = select(CouncilMembers).where( + CouncilMembers.ElectionCycle == get_dm_election_name(config) + ) + + for nominee in session.scalars(stmt): + dm_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: + dm_votes[vote.VotedForId] += 1 + + # build the votes string + votes_str: str = "Clan Leader Votes\n====================" + votes_str += user_vote_str(cl_votes) + + votes_str += "\n\nDiscord Manager Votes\n====================" + votes_str += user_vote_str(dm_votes) + + return votes_str + + +def user_vote_str(votes: dict[int, int]) -> str: + """ + Creates the string representing how many votes each user has. + + Args: + votes (dict[int, int]): Dict of all users and how many votes they have. + + Returns: + str: The string showing names and number of votes. + """ + + for user_id, num_votes in votes.items(): + return f"\n<@{user_id}>: {'☑️' * num_votes}" + + def decorate_nomination(user_id: int, speech: str) -> str: """ Decorates a nomination speech so that it sits within multiple speeches better.