split redeems.py into a folder

This commit is contained in:
Lili (Tlapka) 2025-03-10 19:36:15 +01:00
parent 56cb94e1a0
commit d0ff884dec
8 changed files with 223 additions and 211 deletions

View File

@ -4,7 +4,7 @@ import click
from flask import current_app, g, Flask
from flask.cli import with_appcontext
from tlapbot.redeems import milestone_complete
from tlapbot.redeems.milestones import milestone_complete
def get_db() -> sqlite3.Connection:

View File

@ -1,207 +0,0 @@
from flask import current_app
from sqlite3 import Error, Connection
from typing import Tuple, Any
from tlapbot.owncast_helpers import use_points
from tlapbot.tlapbot_types import Redeems
def counter_exists(db: Connection, counter_name: str) -> bool | None:
"""Returns None only if error was logged."""
try:
cursor = db.execute(
"SELECT count FROM counters WHERE name = ?",
(counter_name,)
)
counter = cursor.fetchone()
if counter is None:
current_app.logger.warning("Counter not found in database.")
current_app.logger.warning("Maybe you forgot to run the refresh-counters CLI command "
"after you added a new counter to the config?")
return False
return True
except Error as e:
current_app.logger.error(f"Error occurred checking if counter exists: {e.args[0]}")
current_app.logger.error(f"For counter: {counter_name}")
def add_to_counter(db: Connection, counter_name: str) -> bool:
if counter_exists(db, counter_name):
try:
db.execute(
"UPDATE counters SET count = count + 1 WHERE name = ?",
(counter_name,)
)
db.commit()
return True
except Error as e:
current_app.logger.error(f"Error occurred adding to counter: {e.args[0]}")
current_app.logger.error(f"To counter: {counter_name}")
return False
# TODO: test if the new default works
def add_to_redeem_queue(db: Connection, user_id: str, redeem_name: str, note: str="") -> bool:
try:
db.execute(
"INSERT INTO redeem_queue(redeem, redeemer_id, note) VALUES(?, ?, ?)",
(redeem_name, user_id, note)
)
db.commit()
return True
except Error as e:
current_app.logger.error(f"Error occurred adding to redeem queue: {e.args[0]}")
current_app.logger.error(f"To user: {user_id} with redeem: {redeem_name} with note: {note}")
return False
def add_to_milestone(db: Connection, user_id: str, redeem_name: str, points_donated: int) -> bool:
try:
cursor = db.execute(
"SELECT progress, goal FROM milestones WHERE name = ?",
(redeem_name,)
)
row = cursor.fetchone()
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, goal = row
if progress + points_donated > goal:
points_donated = goal - progress
if points_donated < 0:
points_donated = 0
if use_points(db, user_id, points_donated):
cursor = db.execute(
"UPDATE milestones SET progress = ? WHERE name = ?",
(progress + points_donated, redeem_name)
)
db.commit()
return True
except Error as e:
current_app.logger.error(f"Error occurred updating milestone: {e.args[0]}")
return False
def milestone_complete(db: Connection, redeem_name: str) -> bool | None:
"""Returns None only if error was logged."""
try:
cursor = db.execute(
"SELECT progress, goal FROM milestones WHERE name = ?",
(redeem_name,)
)
row = cursor.fetchone()
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?")
else:
progress, goal = row
if progress == goal:
return True
return False
except Error as e:
current_app.logger.error(f"Error occurred checking if milestone is complete: {e.args[0]}")
def all_milestones(db: Connection) -> list[Tuple[str, int, int]] | None:
"""Returns list of all (even inactive) milestones, their progress and their goal.
Returns None only if error was logged."""
try:
cursor = db.execute(
"""SELECT name, progress, goal FROM milestones"""
)
return cursor.fetchall()
except Error as e:
current_app.logger.error(f"Error occurred selecting all milestones: {e.args[0]}")
def all_active_milestones(db: Connection) -> list[Tuple[str, int, int]] | None:
"""Returns list of all active milestones, their progress and their goal.
Returns None only if error was logged."""
milestones = all_milestones(db)
if milestones is not None:
all_active_milestones = []
for name, progress, goal in milestones:
if is_redeem_active(name):
all_active_milestones.append((name, progress, goal))
return all_active_milestones
def all_counters(db: Connection) -> list[Tuple[str, int]] | None:
"""Returns list of all (even inactive) counters and their current value.
Returns None only if error was logged."""
try:
cursor = db.execute(
"""SELECT name, count FROM counters"""
)
return cursor.fetchall()
except Error as e:
current_app.logger.error(f"Error occurred selecting all counters: {e.args[0]}")
def all_active_counters(db: Connection) -> list[Tuple[str, int]] | None:
"""Returns list of all active counters, and their current value.
Returns None if error was logged."""
counters = all_counters(db)
if counters is not None:
all_active_counters = []
for name, count in counters:
if is_redeem_active(name):
all_active_counters.append((name, count))
return all_active_counters
def all_active_redeems() -> Redeems:
"""Returns list of all active redeems."""
redeems = current_app.config['REDEEMS']
all_active_redeems = {}
for redeem_name, redeem_dict in redeems.items():
if redeem_dict.get('category', None):
for category in redeem_dict['category']:
if category in current_app.config['ACTIVE_CATEGORIES']:
all_active_redeems[redeem_name] = redeem_dict
break
else:
all_active_redeems[redeem_name] = redeem_dict
return all_active_redeems
def pretty_redeem_queue(db: Connection) -> list[Tuple[str, str, str, str]] | None:
"""Returns a 'pretty' redeem queue, with name of the redeemer joined instead of ID.
Returns None only if error was logged."""
try:
cursor = db.execute(
"""SELECT redeem_queue.created, redeem_queue.redeem, redeem_queue.note, points.name
FROM redeem_queue
INNER JOIN points
on redeem_queue.redeemer_id = points.id"""
)
return cursor.fetchall()
except Error as e:
current_app.logger.error(f"Error occurred selecting pretty redeem queue: {e.args[0]}")
def whole_redeem_queue(db: Connection) -> list[Any] | None:
"""Returns None if error was logged."""
try:
cursor = db.execute(
"SELECT * from redeem_queue" #TODO: specify columns to fetch
)
return cursor.fetchall()
except Error as e:
current_app.logger.error(f"Error occurred selecting redeem queue: {e.args[0]}")
def is_redeem_active(redeem_name: str) -> bool | None:
"""Checks if redeem is active. Pulls the redeem by name from config.
Returns None if the redeem doesn't exist."""
active_categories = current_app.config['ACTIVE_CATEGORIES']
redeem_dict = current_app.config['REDEEMS'].get(redeem_name, None)
if redeem_dict:
if redeem_dict.get('category', None):
for category in redeem_dict["category"]:
if category in active_categories:
return True
return False
else:
return True
return None # redeem does not exist, unknown active state

View File

View File

@ -0,0 +1,63 @@
from flask import current_app
from sqlite3 import Error, Connection
from typing import Tuple
from tlapbot.redeems.redeems import is_redeem_active
def counter_exists(db: Connection, counter_name: str) -> bool | None:
"""Returns None only if error was logged."""
try:
cursor = db.execute(
"SELECT count FROM counters WHERE name = ?",
(counter_name,)
)
counter = cursor.fetchone()
if counter is None:
current_app.logger.warning("Counter not found in database.")
current_app.logger.warning("Maybe you forgot to run the refresh-counters CLI command "
"after you added a new counter to the config?")
return False
return True
except Error as e:
current_app.logger.error(f"Error occurred checking if counter exists: {e.args[0]}")
current_app.logger.error(f"For counter: {counter_name}")
def add_to_counter(db: Connection, counter_name: str) -> bool:
if counter_exists(db, counter_name):
try:
db.execute(
"UPDATE counters SET count = count + 1 WHERE name = ?",
(counter_name,)
)
db.commit()
return True
except Error as e:
current_app.logger.error(f"Error occurred adding to counter: {e.args[0]}")
current_app.logger.error(f"To counter: {counter_name}")
return False
def all_counters(db: Connection) -> list[Tuple[str, int]] | None:
"""Returns list of all (even inactive) counters and their current value.
Returns None only if error was logged."""
try:
cursor = db.execute(
"""SELECT name, count FROM counters"""
)
return cursor.fetchall()
except Error as e:
current_app.logger.error(f"Error occurred selecting all counters: {e.args[0]}")
def all_active_counters(db: Connection) -> list[Tuple[str, int]] | None:
"""Returns list of all active counters, and their current value.
Returns None if error was logged."""
counters = all_counters(db)
if counters is not None:
all_active_counters = []
for name, count in counters:
if is_redeem_active(name):
all_active_counters.append((name, count))
return all_active_counters

View File

@ -0,0 +1,79 @@
from flask import current_app
from sqlite3 import Error, Connection
from typing import Tuple, Any
from tlapbot.owncast_helpers import use_points
from tlapbot.redeems import is_redeem_active
def add_to_milestone(db: Connection, user_id: str, redeem_name: str, points_donated: int) -> bool:
try:
cursor = db.execute(
"SELECT progress, goal FROM milestones WHERE name = ?",
(redeem_name,)
)
row = cursor.fetchone()
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, goal = row
if progress + points_donated > goal:
points_donated = goal - progress
if points_donated < 0:
points_donated = 0
if use_points(db, user_id, points_donated):
cursor = db.execute(
"UPDATE milestones SET progress = ? WHERE name = ?",
(progress + points_donated, redeem_name)
)
db.commit()
return True
except Error as e:
current_app.logger.error(f"Error occurred updating milestone: {e.args[0]}")
return False
def milestone_complete(db: Connection, redeem_name: str) -> bool | None:
"""Returns None only if error was logged."""
try:
cursor = db.execute(
"SELECT progress, goal FROM milestones WHERE name = ?",
(redeem_name,)
)
row = cursor.fetchone()
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?")
else:
progress, goal = row
if progress == goal:
return True
return False
except Error as e:
current_app.logger.error(f"Error occurred checking if milestone is complete: {e.args[0]}")
def all_milestones(db: Connection) -> list[Tuple[str, int, int]] | None:
"""Returns list of all (even inactive) milestones, their progress and their goal.
Returns None only if error was logged."""
try:
cursor = db.execute(
"""SELECT name, progress, goal FROM milestones"""
)
return cursor.fetchall()
except Error as e:
current_app.logger.error(f"Error occurred selecting all milestones: {e.args[0]}")
def all_active_milestones(db: Connection) -> list[Tuple[str, int, int]] | None:
"""Returns list of all active milestones, their progress and their goal.
Returns None only if error was logged."""
milestones = all_milestones(db)
if milestones is not None:
all_active_milestones = []
for name, progress, goal in milestones:
if is_redeem_active(name):
all_active_milestones.append((name, progress, goal))
return all_active_milestones

View File

@ -0,0 +1,74 @@
from flask import current_app
from sqlite3 import Error, Connection
from typing import Tuple, Any
from tlapbot.tlapbot_types import Redeems
# TODO: test if the new default works
def add_to_redeem_queue(db: Connection, user_id: str, redeem_name: str, note: str="") -> bool:
try:
db.execute(
"INSERT INTO redeem_queue(redeem, redeemer_id, note) VALUES(?, ?, ?)",
(redeem_name, user_id, note)
)
db.commit()
return True
except Error as e:
current_app.logger.error(f"Error occurred adding to redeem queue: {e.args[0]}")
current_app.logger.error(f"To user: {user_id} with redeem: {redeem_name} with note: {note}")
return False
def all_active_redeems() -> Redeems:
"""Returns list of all active redeems."""
redeems = current_app.config['REDEEMS']
all_active_redeems = {}
for redeem_name, redeem_dict in redeems.items():
if redeem_dict.get('category', None):
for category in redeem_dict['category']:
if category in current_app.config['ACTIVE_CATEGORIES']:
all_active_redeems[redeem_name] = redeem_dict
break
else:
all_active_redeems[redeem_name] = redeem_dict
return all_active_redeems
def pretty_redeem_queue(db: Connection) -> list[Tuple[str, str, str, str]] | None:
"""Returns a 'pretty' redeem queue, with name of the redeemer joined instead of ID.
Returns None only if error was logged."""
try:
cursor = db.execute(
"""SELECT redeem_queue.created, redeem_queue.redeem, redeem_queue.note, points.name
FROM redeem_queue
INNER JOIN points
on redeem_queue.redeemer_id = points.id"""
)
return cursor.fetchall()
except Error as e:
current_app.logger.error(f"Error occurred selecting pretty redeem queue: {e.args[0]}")
def whole_redeem_queue(db: Connection) -> list[Any] | None:
"""Returns None if error was logged."""
try:
cursor = db.execute(
"SELECT * from redeem_queue" #TODO: specify columns to fetch
)
return cursor.fetchall()
except Error as e:
current_app.logger.error(f"Error occurred selecting redeem queue: {e.args[0]}")
def is_redeem_active(redeem_name: str) -> bool | None:
"""Checks if redeem is active. Pulls the redeem by name from config.
Returns None if the redeem doesn't exist."""
active_categories = current_app.config['ACTIVE_CATEGORIES']
redeem_dict = current_app.config['REDEEMS'].get(redeem_name, None)
if redeem_dict:
if redeem_dict.get('category', None):
for category in redeem_dict["category"]:
if category in active_categories:
return True
return False
else:
return True
return None # redeem does not exist, unknown active state

View File

@ -1,8 +1,9 @@
from flask import current_app
from tlapbot.db import get_db
from tlapbot.owncast_requests import send_chat
from tlapbot.redeems import (add_to_redeem_queue, add_to_counter, add_to_milestone,
milestone_complete, is_redeem_active)
from tlapbot.redeems.redeems import add_to_redeem_queue, is_redeem_active
from tlapbot.redeems.counters import add_to_counter
from tlapbot.redeems.milestones import add_to_milestone, milestone_complete
from tlapbot.owncast_helpers import use_points, read_users_points

View File

@ -1,6 +1,8 @@
from flask import render_template, Blueprint, request, current_app
from tlapbot.db import get_db
from tlapbot.redeems import all_active_counters, all_active_milestones, all_active_redeems, pretty_redeem_queue
from tlapbot.redeems.redeems import all_active_redeems, pretty_redeem_queue
from tlapbot.redeems.counters import all_active_counters
from tlapbot.redeems.milestones import all_active_milestones
from tlapbot.owncast_helpers import read_all_users_with_username
from datetime import timezone