From c2fd4356225607c45a162370ec229662a1d5c871 Mon Sep 17 00:00:00 2001 From: w Date: Tue, 3 Jun 2025 22:53:10 -0300 Subject: [PATCH 1/7] weighing migration --- migrations/0002_weigh_infer.sql | 1 + 1 file changed, 1 insertion(+) create mode 100644 migrations/0002_weigh_infer.sql 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; From 0c2e3754da7f5e47cb9d094eaabac7af372777c2 Mon Sep 17 00:00:00 2001 From: w Date: Tue, 3 Jun 2025 22:54:25 -0300 Subject: [PATCH 2/7] weigh response --- bot/response.py | 13 +++---------- 1 file changed, 3 insertions(+), 10 deletions(-) diff --git a/bot/response.py b/bot/response.py index 567962d..1911c29 100644 --- a/bot/response.py +++ b/bot/response.py @@ -84,10 +84,10 @@ dumbass.', 'attachment_urls': None } - if len(arguments) != 3: + if len(arguments) != 2: return { 'message': f'{full_user} Please specify the following attributes \ -in order: name, rarity, drop weighting', +in order: name, rarity', 'attachment_urls': None } @@ -97,17 +97,10 @@ in order: name, rarity, drop weighting', be a number between 1 and 5', 'attachment_urls': None } - if not (is_float(arguments[2]) and 0.0 < float(arguments[2]) <= 1.0): - return { - 'message': f'{full_user} Invalid drop weight: \'{arguments[2]}\' \ -must be a decimal value between 0.0 and 1.0', - 'attachment_urls': None - } character_id, file_id = add_character( name=arguments[0], rarity=int(arguments[1]), - weight=float(arguments[2]), image_url=image_url ) return { @@ -121,7 +114,7 @@ def do_help(author: str) -> BotResponse: return { 'message': f'{author} Here\'s what I can do:\n\ - `roll` Pulls a random character.\n\ -- `create ` Creates a character using a given image.\n\ +- `create ` Creates a character using a given image.\n\ - `help` Shows this message.', 'attachment_urls': None } From 4c2b3f589f9e7a6b518e942b5e59952e99a29044 Mon Sep 17 00:00:00 2001 From: w Date: Tue, 3 Jun 2025 22:55:08 -0300 Subject: [PATCH 3/7] db_utils weigh change test --- bot/db_utils.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/bot/db_utils.py b/bot/db_utils.py index 6f8ae28..6159129 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'] } From 9f9c0344618f6bf734505292000c297e3cd9ec4b Mon Sep 17 00:00:00 2001 From: w Date: Tue, 3 Jun 2025 22:55:39 -0300 Subject: [PATCH 4/7] weight config --- bot/config.py | 11 +++++++++++ example_config.ini | 7 +++++++ 2 files changed, 18 insertions(+) diff --git a/bot/config.py b/bot/config.py index 3621103..fb34223 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/example_config.ini b/example_config.ini index d7f1c14..5f897cb 100644 --- a/example_config.ini +++ b/example_config.ini @@ -9,6 +9,13 @@ 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 +Rarity_1 = 0.7 ; common +Rarity_2 = 0.2 ; uncommon +Rarity_3 = 0.08 ; rare +Rarity_4 = 0.015 ; epic +Rarity_5 = 0.005 ; legendary [notification] ; Number of seconds to sleep while awaiting new notifications From cbdbefb5fc10ad3b6c7311bd259db020e8f524c5 Mon Sep 17 00:00:00 2001 From: w Date: Tue, 3 Jun 2025 23:08:15 -0300 Subject: [PATCH 5/7] weight add_character change --- bot/add_character.py | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) 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 From 956d5927cd2265c0ce99cc4663c48125c290d6af Mon Sep 17 00:00:00 2001 From: w Date: Sat, 7 Jun 2025 01:01:06 -0300 Subject: [PATCH 6/7] small config fix --- bot/config.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/bot/config.py b/bot/config.py index fb34223..227f949 100644 --- a/bot/config.py +++ b/bot/config.py @@ -25,8 +25,8 @@ 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_")) + if key.startswith("rarity_"): + rarity = int(key.removeprefix("rarity_")) rarity_weights[rarity] = float(value) return rarity_weights From 8569f28f3c0ff683445205a4e9cae0a7c7eef072 Mon Sep 17 00:00:00 2001 From: w Date: Sat, 7 Jun 2025 01:42:30 -0300 Subject: [PATCH 7/7] example config fix --- example_config.ini | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/example_config.ini b/example_config.ini index 5f897cb..af7e0f2 100644 --- a/example_config.ini +++ b/example_config.ini @@ -11,11 +11,12 @@ DatabaseLocation = ./gacha_game.db RollInterval = 72000 ; Rarity drop weights (1 to 5 stars) ; Format: rarity=weight per line -Rarity_1 = 0.7 ; common -Rarity_2 = 0.2 ; uncommon -Rarity_3 = 0.08 ; rare -Rarity_4 = 0.015 ; epic -Rarity_5 = 0.005 ; legendary +; 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