Merge pull request '26-rarity-vs-weight' (#46) from 26-rarity-vs-weighing into dev

Reviewed-on: #46
Reviewed-by: nai <nai@waifuism.life>
This commit is contained in:
waifu 2025-06-06 22:12:12 -07:00
commit 70c8fd7a0c
6 changed files with 29 additions and 11 deletions

View file

@ -3,12 +3,12 @@ from misskey.exceptions import MisskeyAPIException
from client import client_connection from client import client_connection
from db_utils import insert_character from db_utils import insert_character
from custom_types import Character from custom_types import Character
from config import RARITY_TO_WEIGHT
def add_character( def add_character(
name: str, name: str,
rarity: int, rarity: int,
weight: float,
image_url: str) -> tuple[int, str]: image_url: str) -> tuple[int, str]:
''' '''
Adds a character to the database, uploading the image from a public URL to Adds a character to the database, uploading the image from a public URL to
@ -17,7 +17,6 @@ def add_character(
Args: Args:
name (str): Character name. name (str): Character name.
rarity (int): Character rarity (e.g., 1-5). 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 image_url (str): Public URL of the image from the post (e.g., from
note['files'][i]['url']). note['files'][i]['url']).
@ -36,8 +35,8 @@ def add_character(
raise ValueError('Character name cannot be empty.') raise ValueError('Character name cannot be empty.')
if rarity < 1: if rarity < 1:
raise ValueError('Rarity must be a positive integer.') raise ValueError('Rarity must be a positive integer.')
if weight <= 0: if rarity not in RARITY_TO_WEIGHT.keys():
raise ValueError('Weight must be a positive number.') raise ValueError(f'Invalid rarity: {rarity}')
if not image_url: if not image_url:
raise ValueError('Image URL must be provided.') raise ValueError('Image URL must be provided.')
@ -59,7 +58,7 @@ def add_character(
character_id = insert_character( character_id = insert_character(
stripped_name, stripped_name,
rarity, rarity,
float(weight), RARITY_TO_WEIGHT[rarity],
file_id file_id
) )
return character_id, file_id return character_id, file_id

View file

@ -21,6 +21,15 @@ def get_config_file() -> str:
raise ConfigError(f'Could not find {config_path}') raise ConfigError(f'Could not find {config_path}')
return 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 = configparser.ConfigParser()
config.read(get_config_file()) config.read(get_config_file())
@ -43,3 +52,5 @@ NOTIFICATION_POLL_INTERVAL = int(config['notification']['PollInterval'])
NOTIFICATION_BATCH_SIZE = int(config['notification']['BatchSize']) NOTIFICATION_BATCH_SIZE = int(config['notification']['BatchSize'])
GACHA_ROLL_INTERVAL = int(config['gacha']['RollInterval']) GACHA_ROLL_INTERVAL = int(config['gacha']['RollInterval'])
RARITY_TO_WEIGHT = get_rarity_to_weight(config['gacha'])

View file

@ -26,14 +26,14 @@ def get_random_character() -> Character | None:
if not characters: if not characters:
return None 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] chosen = choices(characters, weights=weights, k=1)[0]
return { return {
'id': chosen['id'], 'id': chosen['id'],
'name': chosen['name'], 'name': chosen['name'],
'rarity': chosen['rarity'], 'rarity': chosen['rarity'],
'weight': chosen['weight'], 'weight': config.RARITY_TO_WEIGHT[chosen['rarity']],
'image_url': chosen['file_id'] 'image_url': chosen['file_id']
} }

View file

@ -103,10 +103,10 @@ dumbass.',
'attachment_urls': None 'attachment_urls': None
} }
if len(arguments) != 3: if len(arguments) != 2:
return { return {
'message': f'{author} Please specify the following attributes \ 'message': f'{author} Please specify the following attributes \
in order: name, rarity, drop weighting', in order: name, rarity',
'attachment_urls': None 'attachment_urls': None
} }
@ -126,7 +126,6 @@ must be a decimal value between 0.0 and 1.0',
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]),
image_url=image_url image_url=image_url
) )
return { return {
@ -140,7 +139,7 @@ def do_help(author: str) -> BotResponse:
return { return {
'message':f'{author} Here\'s what I can do:\n \ 'message':f'{author} Here\'s what I can do:\n \
- `roll` Pulls a random character.\ - `roll` Pulls a random character.\
- `create <name> <rarity> <weight>` Creates a character using a given image.\ - `create <name> <rarity>` Creates a character using a given image.\
- `signup` Registers your account.\ - `signup` Registers your account.\
- `delete_account` Deletes your account.\ - `delete_account` Deletes your account.\
- `help` Shows this message', - `help` Shows this message',

View file

@ -9,6 +9,14 @@ DatabaseLocation = ./gacha_game.db
[gacha] [gacha]
; Number of seconds players have to wait between rolls ; Number of seconds players have to wait between rolls
RollInterval = 72000 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] [notification]
; Number of seconds to sleep while awaiting new notifications ; Number of seconds to sleep while awaiting new notifications

View file

@ -0,0 +1 @@
ALTER TABLE characters DROP COLUMN weight;