forked from waifu/kemoverse
		
	Merge pull request '26-rarity-vs-weight' (#46) from 26-rarity-vs-weighing into dev
Reviewed-on: waifu/kemoverse#46 Reviewed-by: nai <nai@waifuism.life>
This commit is contained in:
		
						commit
						70c8fd7a0c
					
				
					 6 changed files with 29 additions and 11 deletions
				
			
		|  | @ -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 | ||||
|  |  | |||
|  | @ -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']) | ||||
|  | @ -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'] | ||||
|     } | ||||
| 
 | ||||
|  |  | |||
|  | @ -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 <name> <rarity> <weight>` Creates a character using a given image.\ | ||||
|             - `create <name> <rarity>` Creates a character using a given image.\ | ||||
|             - `signup` Registers your account.\ | ||||
|             - `delete_account` Deletes your account.\ | ||||
|             - `help` Shows this message', | ||||
|  |  | |||
|  | @ -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 | ||||
|  |  | |||
							
								
								
									
										1
									
								
								migrations/0002_weigh_infer.sql
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										1
									
								
								migrations/0002_weigh_infer.sql
									
										
									
									
									
										Normal file
									
								
							|  | @ -0,0 +1 @@ | |||
| ALTER TABLE characters DROP COLUMN weight; | ||||
		Loading…
	
	Add table
		
		Reference in a new issue