diff --git a/bot/add_character.py b/bot/add_character.py index e8d078b..18b0f98 100644 --- a/bot/add_character.py +++ b/bot/add_character.py @@ -3,12 +3,12 @@ from misskey.exceptions import MisskeyAPIException from client import client_connection from db_utils import insert_character from custom_types import Character +from config import RARITY_TO_WEIGHT def add_character( name: str, rarity: int, - weight: float, image_url: str) -> tuple[int, str]: ''' Adds a character to the database, uploading the image from a public URL to @@ -17,7 +17,6 @@ def add_character( Args: name (str): Character name. rarity (int): Character rarity (e.g., 1-5). - weight (float): Pull weight (e.g., 0.02). image_url (str): Public URL of the image from the post (e.g., from note['files'][i]['url']). @@ -36,8 +35,8 @@ def add_character( raise ValueError('Character name cannot be empty.') if rarity < 1: raise ValueError('Rarity must be a positive integer.') - if weight <= 0: - raise ValueError('Weight must be a positive number.') + if rarity not in RARITY_TO_WEIGHT.keys(): + raise ValueError(f'Invalid rarity: {rarity}') if not image_url: raise ValueError('Image URL must be provided.') @@ -59,7 +58,7 @@ def add_character( character_id = insert_character( stripped_name, rarity, - float(weight), + RARITY_TO_WEIGHT[rarity], file_id ) return character_id, file_id diff --git a/bot/config.py b/bot/config.py index 3621103..227f949 100644 --- a/bot/config.py +++ b/bot/config.py @@ -21,6 +21,15 @@ def get_config_file() -> str: raise ConfigError(f'Could not find {config_path}') return config_path +def get_rarity_to_weight(config_section): + """Parses Rarity_X keys from config and returns a {rarity: weight} dict.""" + rarity_weights = {} + for key, value in config_section.items(): + if key.startswith("rarity_"): + rarity = int(key.removeprefix("rarity_")) + rarity_weights[rarity] = float(value) + return rarity_weights + config = configparser.ConfigParser() config.read(get_config_file()) @@ -43,3 +52,5 @@ NOTIFICATION_POLL_INTERVAL = int(config['notification']['PollInterval']) NOTIFICATION_BATCH_SIZE = int(config['notification']['BatchSize']) GACHA_ROLL_INTERVAL = int(config['gacha']['RollInterval']) + +RARITY_TO_WEIGHT = get_rarity_to_weight(config['gacha']) \ No newline at end of file diff --git a/bot/db_utils.py b/bot/db_utils.py index bf4b25f..68409be 100644 --- a/bot/db_utils.py +++ b/bot/db_utils.py @@ -26,14 +26,14 @@ def get_random_character() -> Character | None: if not characters: return None - weights = [c['weight'] for c in characters] + weights = [config.RARITY_TO_WEIGHT[c['rarity']] for c in characters] chosen = choices(characters, weights=weights, k=1)[0] return { 'id': chosen['id'], 'name': chosen['name'], 'rarity': chosen['rarity'], - 'weight': chosen['weight'], + 'weight': config.RARITY_TO_WEIGHT[chosen['rarity']], 'image_url': chosen['file_id'] } diff --git a/bot/response.py b/bot/response.py index bd56f20..3fde3ed 100644 --- a/bot/response.py +++ b/bot/response.py @@ -103,10 +103,10 @@ dumbass.', 'attachment_urls': None } - if len(arguments) != 3: + if len(arguments) != 2: return { 'message': f'{author} Please specify the following attributes \ -in order: name, rarity, drop weighting', +in order: name, rarity', 'attachment_urls': None } @@ -126,7 +126,6 @@ must be a decimal value between 0.0 and 1.0', character_id, file_id = add_character( name=arguments[0], rarity=int(arguments[1]), - weight=float(arguments[2]), image_url=image_url ) return { @@ -140,7 +139,7 @@ def do_help(author: str) -> BotResponse: return { 'message':f'{author} Here\'s what I can do:\n \ - `roll` Pulls a random character.\ - - `create ` Creates a character using a given image.\ + - `create ` Creates a character using a given image.\ - `signup` Registers your account.\ - `delete_account` Deletes your account.\ - `help` Shows this message', diff --git a/example_config.ini b/example_config.ini index d7f1c14..af7e0f2 100644 --- a/example_config.ini +++ b/example_config.ini @@ -9,6 +9,14 @@ DatabaseLocation = ./gacha_game.db [gacha] ; Number of seconds players have to wait between rolls RollInterval = 72000 +; Rarity drop weights (1 to 5 stars) +; Format: rarity=weight per line +; In order: common, uncommon, rare, epic and legendary (Example values below) +Rarity_1 = 0.7 +Rarity_2 = 0.2 +Rarity_3 = 0.08 +Rarity_4 = 0.015 +Rarity_5 = 0.005 [notification] ; Number of seconds to sleep while awaiting new notifications diff --git a/migrations/0002_weigh_infer.sql b/migrations/0002_weigh_infer.sql new file mode 100644 index 0000000..28a1002 --- /dev/null +++ b/migrations/0002_weigh_infer.sql @@ -0,0 +1 @@ +ALTER TABLE characters DROP COLUMN weight;