milestone upgrade

subtract pts, rename count to progress, send a chat message on success
This commit is contained in:
Lili (Tlapka) 2023-03-15 13:13:14 +01:00
parent 54ea10a43c
commit 22501c80c0
3 changed files with 23 additions and 24 deletions

View File

@ -187,15 +187,14 @@ def add_to_redeem_queue(db, user_id, redeem_name, note=None):
def start_milestone(db, redeem_name): def start_milestone(db, redeem_name):
try: try:
cursor = db.execute( cursor = db.execute(
"SELECT count, goal FROM milestones WHERE name = ?", "SELECT progress, goal FROM milestones WHERE name = ?",
(redeem_name,) (redeem_name,)
) )
milestone = cursor.fetchone() milestone = cursor.fetchone()
current_app.logger.error(f"Milestone: {milestone}") current_app.logger.error(f"Milestone: {milestone}")
if milestone is None: if milestone is None:
current_app.logger.error(f"Adding milestone to db.")
cursor = db.execute( cursor = db.execute(
"INSERT INTO milestones(name, count, goal) VALUES(?, ?, ?)", "INSERT INTO milestones(name, progress, goal) VALUES(?, ?, ?)",
(redeem_name, 0, current_app.config['REDEEMS'][redeem_name]['goal']) (redeem_name, 0, current_app.config['REDEEMS'][redeem_name]['goal'])
) )
db.commit() db.commit()
@ -203,38 +202,38 @@ def start_milestone(db, redeem_name):
current_app.logger.error(f"Error occured adding a milestone: {e.args[0]}") current_app.logger.error(f"Error occured adding a milestone: {e.args[0]}")
def add_to_milestone(db, user_id, redeem_name, points_donated):
def add_to_milestone(db, redeem_name, points):
try: try:
start_milestone(db, redeem_name)
cursor = db.execute( cursor = db.execute(
"SELECT count, goal FROM milestones WHERE name = ?", "SELECT progress, goal FROM milestones WHERE name = ?",
(redeem_name,) (redeem_name,)
) )
row = cursor.fetchone() row = cursor.fetchone()
count = row[0] if row is None:
current_app.logger.warning("Milestone not found in database.")
current_app.logger.warning("Maybe you forgot to run the refresh-milestones CLI command "
"after you added a new milestone to the config?")
return False
progress = row[0]
goal = row[1] goal = row[1]
result = count + points if progress + points_donated > goal:
if result > goal: points_donated = goal - progress
result = goal if use_points(db, user_id, points_donated):
current_app.logger.error(f"Doing db operation to add to milestone.") cursor = db.execute(
cursor = db.execute( "UPDATE milestones SET progress = ? WHERE name = ?",
"UPDATE milestones SET count = ? WHERE name = ?", (progress + points_donated, redeem_name)
(result, redeem_name) )
) db.commit()
db.commit() return True
return True
except Error as e: except Error as e:
current_app.logger.error(f"Error occured updating milestone: {e.args[0]}") current_app.logger.error(f"Error occured updating milestone: {e.args[0]}")
current_app.logger.error(f"Milestone add failed.")
return False return False
def all_milestones(db): def all_milestones(db):
try: try:
cursor = db.execute( cursor = db.execute(
"""SELECT name, count, goal FROM milestones""" """SELECT name, progress, goal FROM milestones"""
) )
return cursor.fetchall() return cursor.fetchall()
except Error as e: except Error as e:

View File

@ -53,7 +53,7 @@ def handle_redeem(message, user_id):
send_chat(f"Cannot redeem {redeem}, amount of points is not an integer.") send_chat(f"Cannot redeem {redeem}, amount of points is not an integer.")
elif int(note) > points: elif int(note) > points:
send_chat(f"Can't redeem {redeem}, you don't have enough points.2") send_chat(f"Can't redeem {redeem}, you don't have enough points.2")
else: elif add_to_milestone(db, user_id, redeem, int(note)):
add_to_milestone(db, redeem, int(note)) send_chat(f"Succesfully donated {int(note)} to {redeem} milestone!")
else: else:
send_chat(f"{redeem} not redeemed, type of redeem not found.") send_chat(f"{redeem} not redeemed, type of redeem not found.")

View File

@ -10,7 +10,7 @@ CREATE TABLE IF NOT EXISTS points (
CREATE TABLE milestones ( CREATE TABLE milestones (
id INTEGER PRIMARY KEY AUTOINCREMENT, id INTEGER PRIMARY KEY AUTOINCREMENT,
name TEXT NOT NULL, name TEXT NOT NULL,
count INTEGER NOT NULL, progress INTEGER NOT NULL,
goal INTEGER NOT NULL goal INTEGER NOT NULL
); );