functioning polls
This commit is contained in:
parent
bd6f94a976
commit
d7fdc1e183
@ -26,10 +26,15 @@ def create_app(test_config: None = None) -> Flask:
|
||||
app.config.from_pyfile('redeems.py', silent=True)
|
||||
|
||||
# set up polls if they're enabled
|
||||
if app.config['POLLS']:
|
||||
if app.config['POLLS_ENABLED']:
|
||||
app.config.from_object('tlapbot.defaults.default_polls')
|
||||
app.config.from_pyfile('polls.py', silent=True)
|
||||
|
||||
for poll in app.config['POLLS']:
|
||||
if ' ' in poll:
|
||||
app.logger.warning(f"Poll '{poll}' has spaces in its name.")
|
||||
app.logger.warning("Poll with spaces are impossible to redeem.")
|
||||
|
||||
# Make logging work for gunicorn-ran instances of tlapbot.
|
||||
if app.config['GUNICORN']:
|
||||
gunicorn_logger = logging.getLogger('gunicorn.error')
|
||||
|
||||
@ -92,12 +92,24 @@ def refresh_counters() -> bool:
|
||||
return insert_counters(db)
|
||||
|
||||
|
||||
def populate_poll_options(db: sqlite3.Connection, poll: str) -> bool:
|
||||
for option in current_app.config['POLLS'][poll]['options']:
|
||||
db.execute(
|
||||
"INSERT INTO polls(poll_name, points, option) VALUES(?, ?, ?)",
|
||||
(poll, 0, option)
|
||||
)
|
||||
db.commit()
|
||||
return True
|
||||
|
||||
|
||||
def refresh_polls() -> bool:
|
||||
db = get_db()
|
||||
|
||||
try:
|
||||
db.execute("DELETE FROM polls")
|
||||
db.commit()
|
||||
for poll in current_app.config['POLLS']:
|
||||
populate_poll_options(db, poll)
|
||||
db.commit()
|
||||
except sqlite3.Error as e:
|
||||
print("Error occurred deleting old counters:", e.args[0])
|
||||
return False
|
||||
@ -114,12 +126,7 @@ def reset_poll(poll: str) -> bool:
|
||||
"DELETE FROM polls WHERE poll_name = ?",
|
||||
(poll,)
|
||||
)
|
||||
for option in current_app.config['POLLS'][poll]:
|
||||
db.execute(
|
||||
"INSERT INTO polls(poll_name, points, option) VALUES(?, ?, ?)",
|
||||
(poll, 0, option)
|
||||
)
|
||||
db.commit()
|
||||
populate_poll_options(db, poll)
|
||||
return True
|
||||
except sqlite3.Error as e:
|
||||
current_app.logger.error(f"Error occurred resetting poll: {e.args[0]}")
|
||||
|
||||
@ -8,4 +8,4 @@ LIST_REDEEMS=False
|
||||
ACTIVE_CATEGORIES=[]
|
||||
GUNICORN=False
|
||||
PREFIX='!'
|
||||
POLLS=True
|
||||
POLLS_ENABLED=True
|
||||
@ -25,6 +25,7 @@ def handle_poll_vote(message: str, user_id: str) -> None:
|
||||
if not user_points:
|
||||
send_chat(f"Can't vote in {poll_name} poll, failed to read users' points.")
|
||||
return
|
||||
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.")
|
||||
if (vote_in_poll(db, poll_name, option, poll_points) and
|
||||
|
||||
@ -28,11 +28,11 @@ def vote_in_poll(db: Connection, poll_name: str, option: str, points: int) -> bo
|
||||
if poll_option_exists(db, poll_name, option):
|
||||
try:
|
||||
cursor = db.execute(
|
||||
"UPDATE polls SET progress = progress + ? WHERE poll_name = ? and option = ?",
|
||||
"UPDATE polls SET points = points + ? WHERE poll_name = ? and option = ?",
|
||||
(points, poll_name, option)
|
||||
)
|
||||
db.commit()
|
||||
return True
|
||||
except Error as e:
|
||||
current_app.logger.error(f"Error occurred updating milestone: {e.args[0]}")
|
||||
current_app.logger.error(f"Error occurred updating poll: {e.args[0]}")
|
||||
return False
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user