milestone stub + helpers name change

lets hope milestones work
This commit is contained in:
Lili (Tlapka) 2023-03-14 14:15:18 +01:00
parent 90615ce7c6
commit 07c701de71
3 changed files with 45 additions and 4 deletions

View File

@ -137,7 +137,7 @@ def change_display_name(db, user_id, new_name):
current_app.logger.error(f"To user id: {user_id}, with display name: {new_name}") current_app.logger.error(f"To user id: {user_id}, with display name: {new_name}")
def check_counter_exists(db, counter_name): def counter_exists(db, counter_name):
try: try:
cursor = db.execute( cursor = db.execute(
"SELECT count FROM counters WHERE name = ?", "SELECT count FROM counters WHERE name = ?",
@ -156,7 +156,7 @@ def check_counter_exists(db, counter_name):
def add_to_counter(db, counter_name): def add_to_counter(db, counter_name):
if check_counter_exists(db, counter_name): if counter_exists(db, counter_name):
try: try:
cursor = db.execute( cursor = db.execute(
"UPDATE counters SET count = count + 1 WHERE name = ?", "UPDATE counters SET count = count + 1 WHERE name = ?",
@ -184,6 +184,30 @@ def add_to_redeem_queue(db, user_id, redeem_name, note=None):
return False return False
def add_to_milestone(db, redeem_name, points):
try:
cursor = db.execute(
"SELECT count, goal FROM milestones WHERE name = ?",
(redeem_name,)
)
row = cursor.fetchone()
if row is not None:
count = row[0]
goal = row[1]
result = count + points
if result > goal:
result = goal
cursor = db.execute(
"UPDATE milestones SET count = ? WHERE name = ?",
(result, redeem_name)
)
db.commit()
return True
except (Error, Exception) as e:
current_app.logger.error(f"Error occured updating milestone: {e.args[0]}")
return False
def all_counters(db): def all_counters(db):
try: try:
cursor = db.execute( cursor = db.execute(

View File

@ -1,7 +1,8 @@
from flask import current_app from flask import current_app
from tlapbot.db import get_db from tlapbot.db import get_db
from tlapbot.owncast_helpers import (use_points, add_to_redeem_queue, from tlapbot.owncast_helpers import (use_points, add_to_redeem_queue,
add_to_counter, read_users_points, send_chat, remove_emoji) add_to_counter, read_users_points, send_chat, remove_emoji,
add_to_milestone)
def handle_redeem(message, user_id): def handle_redeem(message, user_id):
@ -45,5 +46,14 @@ def handle_redeem(message, user_id):
send_chat(f"{redeem} redeemed for {price} points.") send_chat(f"{redeem} redeemed for {price} points.")
else: else:
send_chat(f"Redeeming {redeem} failed.") send_chat(f"Redeeming {redeem} failed.")
elif redeem_type == "milestone":
if not note:
send_chat(f"Cannot redeem {redeem}, no amount of points specified.")
elif not note.isdigit():
send_chat(f"Cannot redeem {redeem}, amount of points is not an integer.")
elif int(note) < points:
send_chat(f"Can't redeem {redeem}, you don't have enough points.")
else:
add_to_milestone(db, redeem, int(note))
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

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