diff --git a/tlapbot/poll_handler.py b/tlapbot/poll_handler.py index d91e04e..2d83bc8 100644 --- a/tlapbot/poll_handler.py +++ b/tlapbot/poll_handler.py @@ -28,6 +28,7 @@ def handle_poll_vote(message: str, user_id: str) -> None: poll_points = int(poll_points) if user_points < poll_points: send_chat(f"Can't vote in {poll_name} poll, you're voting with more points than you have.") + return if (vote_in_poll(db, poll_name, option, poll_points) and use_points(db, user_id, poll_points)): - send_chat(f"{poll_points} donated to {option} in poll {poll_name}") \ No newline at end of file + send_chat(f"{poll_points} points donated to {option} in poll {poll_name}") \ No newline at end of file diff --git a/tlapbot/redeems/polls.py b/tlapbot/redeems/polls.py index 7af29d4..328958a 100644 --- a/tlapbot/redeems/polls.py +++ b/tlapbot/redeems/polls.py @@ -2,6 +2,43 @@ from flask import current_app from sqlite3 import Error, Connection +def get_poll_votes(db: Connection, poll_name: str) -> dict[str, int] | None: + """Get all poll votes and point values for a given `poll_name`""" + try: + options = {} + cursor = db.execute( + "SELECT option, points from polls WHERE poll_name = ?", + (poll_name,) + ) + for row in cursor: + options[row[0]] = row[1] + return options + except Error as e: + current_app.logger.error(f"Error occurred getting poll votes: {e.args[0]}") + current_app.logger.error(f"For poll {poll_name}") + + +def all_active_polls(db: Connection) -> dict[str, dict[str, int]] | None: + """Returns a dict where the keys are poll names, values is a dict of options and points. + Returns None if polls aren't enabled.""" + if not current_app.config['POLLS_ENABLED']: + return None + polls = current_app.config['POLLS'] + poll_votes = {} + for poll_name in polls.keys(): + poll_votes[poll_name] = get_poll_votes(db, poll_name) + return poll_votes + + +def max_poll_votes(poll_dict: dict[str, int]) -> int: + """Returns the maximum number of poll votes/points. Input is the output of `get_poll_votes`""" + highest_points = 0 + for _, points in poll_dict.items(): + if points > highest_points: + highest_points = points + return highest_points + + def poll_option_exists(db: Connection, poll_name: str, option: str) -> bool | None: """Returns None only if error was logged.""" try: diff --git a/tlapbot/templates/dashboard.html b/tlapbot/templates/dashboard.html index 5e7ef2e..b342e0a 100644 --- a/tlapbot/templates/dashboard.html +++ b/tlapbot/templates/dashboard.html @@ -23,7 +23,7 @@
| Active polls | +Points | +||
|---|---|---|---|
| {{ poll_name }} | +{{ option }} | +{{ poll_dict[option] }} | ++ |