Tidy up gacha_response.py

This commit is contained in:
Valkyrie 2025-05-17 18:01:12 +01:00
parent bc0f71d5be
commit 36f0d4a0d1

View file

@ -3,6 +3,7 @@ from db_utils import get_or_create_user, add_pull, get_db_connection
from add_character import add_character from add_character import add_character
def get_character(): def get_character():
''' Gets a random character from the database'''
conn = get_db_connection() conn = get_db_connection()
cur = conn.cursor() cur = conn.cursor()
cur.execute('SELECT * FROM characters') cur.execute('SELECT * FROM characters')
@ -10,21 +11,25 @@ def get_character():
conn.close() conn.close()
if not characters: if not characters:
return None, None return None, None, None, None
weights = [c['weight'] for c in characters] weights = [c['weight'] for c in characters]
chosen = random.choices(characters, weights=weights, k=1)[0] chosen = random.choices(characters, weights=weights, k=1)[0]
return chosen['id'], chosen['name'], chosen['file_id'], chosen['rarity'] return chosen['id'], chosen['name'], chosen['file_id'], chosen['rarity']
# TODO: See issue #3, separate command parsing from game logic.
def gacha_response(command,full_user, arguments,note_obj): def gacha_response(command,full_user, arguments,note_obj):
'''Parses a given command with arguments, processes the game state and
returns a response'''
if command == "roll": if command == "roll":
user_id = get_or_create_user(full_user) user_id = get_or_create_user(full_user)
character_id, character_name, file_id, rarity = get_character() character_id, character_name, file_id, rarity = get_character()
if not character_id: if not character_id:
#TODO: Can't have tuples of a single element
# Return these as a dict or object instead.
return(f"@{full_user} Uwaaa... something went wrong! No characters found. 😿") return(f"@{full_user} Uwaaa... something went wrong! No characters found. 😿")
add_pull(user_id,character_id) add_pull(user_id,character_id)
@ -35,12 +40,13 @@ def gacha_response(command,full_user, arguments,note_obj):
# Example call from bot logic # Example call from bot logic
image_url = note_obj.get("files", [{}])[0].get("url") if note_obj.get("files") else None image_url = note_obj.get("files", [{}])[0].get("url") if note_obj.get("files") else None
if not image_url: if not image_url:
return("You need an image to create a character, dumbass.") return "You need an image to create a character, dumbass."
character_id, file_id = add_character( character_id, file_id = add_character(
name=arguments[0], name=arguments[0],
rarity=int(arguments[1]), rarity=int(arguments[1]),
weight=float(arguments[2]), weight=float(arguments[2]),
image_url=image_url image_url=image_url
) )
return([f"Added {arguments[0]}, ID {character_id}.",[file_id]]) return([f"Added {arguments[0]}, ID {character_id}.",[file_id]])
return None