Spaces:
Running
Running
from time import time | |
import random | |
import json | |
def load_lists(list_names, base_dir="lists"): | |
lists = {} | |
for name in list_names: | |
with open(f"{base_dir}/{name}.json", "r") as f: | |
lists[name] = json.load(f) | |
return lists | |
def rand_hp(): | |
# Weights from https://bulbapedia.bulbagarden.net/wiki/HP_(TCG) | |
hp_range = list(range(30, 340 + 1, 10)) | |
weights = [156, 542, 1264, 1727, 1477, 1232, 1008, 640, 436, 515, 469, 279, 188, | |
131, 132, 132, 56, 66, 97, 74, 23, 24, 25, 7, 15, 6, 0, 12, 18, 35, 18, 3] | |
return random.choices(hp_range, weights)[0] | |
def rand_type(types=['Grass', 'Fire', 'Water', 'Lightning', 'Fighting', | |
'Psychic', 'Colorless', 'Darkness', 'Metal', 'Dragon', 'Fairy'], can_be_none=False): | |
if can_be_none: | |
return random.choices([random.choices(types)[0], None])[0] | |
else: | |
return random.choices(types)[0] | |
def rand_name(energy_type=rand_type()): | |
lists = load_lists([energy_type], 'lists/names') | |
return random.choices(lists[energy_type])[0] | |
def rand_species(species): | |
random_species = random.choices(species)[0] | |
return f'{random_species.capitalize()}' | |
def rand_length(): | |
# Weights from https://bulbapedia.bulbagarden.net/wiki/List_of_Pok%C3%A9mon_by_height | |
feet_ranges = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '10', '11', '12', '13', '14', '16', | |
'17', '18', '19', '20', '21', '22', '23', '24', '26', '28', '30', '32', '34', '35', '47', '65', '328'] | |
weights = [30, 220, 230, 176, 130, 109, 63, 27, 17, 17, 5, 5, 6, | |
4, 3, 2, 2, 2, 1, 2, 3, 1, 2, 1, 1, 1, 2, 1, 1, 2, 1, 1, 1] | |
return { | |
"feet": random.choices(feet_ranges, weights)[0], | |
"inches": random.randrange(0, 11 + 1, 1) | |
} | |
def rand_weight(): | |
# Weights from https://bulbapedia.bulbagarden.net/wiki/List_of_Pok%C3%A9mon_by_weight | |
weight_ranges = [{"start": 1, "end": 22}, {"start": 22, "end": 44}, {"start": 44, "end": 55}, {"start": 55, "end": 110}, {"start": 110, "end": 132}, {"start": 132, "end": 218}, {"start": 218, "end": 220}, {"start": 221, "end": 226}, { | |
"start": 226, "end": 331}, {"start": 331, "end": 441}, {"start": 441, "end": 451}, {"start": 452, "end": 661}, {"start": 661, "end": 677}, {"start": 677, "end": 793}, {"start": 794, "end": 903}, {"start": 903, "end": 2204}] | |
# 'weights' as in statistical weightings | |
weights = [271, 145, 53, 204, 57, 122, 1, 11, 57, 28, 7, 34, 4, 17, 5, 31] | |
start, end = random.choices(weight_ranges, weights)[0].values() | |
random_weight = random.randrange(start, end + 1, 1) | |
return f'{random_weight} lbs.' | |
def rand_attack(attacks, name, energy_type=None, colorless_only_allowed=False): | |
random_attack = random.choices(attacks)[0] | |
# There are no attacks in Pokémon TCG that have Dragon energy costs | |
# so this would loop indefinitely if looking for one | |
if energy_type is not None and energy_type != 'Dragon' and not colorless_only_allowed: | |
while energy_type not in random_attack["cost"]: | |
random_attack = random.choices(attacks)[0] | |
elif energy_type is not None and energy_type != 'Dragon' and colorless_only_allowed: | |
while energy_type not in random_attack["cost"] and 'Colorless' not in random_attack["cost"]: | |
random_attack = random.choices(attacks)[0] | |
random_attack['text'] = random_attack['text'].replace('<name>', name) | |
return random_attack | |
def rand_attacks(attacks, name, energy_type=None, n=2): | |
attack1 = rand_attack(attacks, name, energy_type) | |
if n > 1: | |
attack2 = rand_attack(attacks, name, energy_type, True) | |
while attack1['text'] == attack2['text']: | |
attack2 = rand_attack(attacks, name, energy_type, True) | |
return [attack1, attack2] | |
else: | |
return [attack1] | |
def rand_retreat(): | |
return random.randrange(0, 4, 1) | |
def rand_description(descriptions): | |
return random.choices(descriptions)[0] | |
def rand_rarity(): | |
return random.choices(['●', '◆', '★'], [10, 5, 1])[0] | |
def rand_details(lists=load_lists(['attacks', 'descriptions', 'species'])): | |
energy_type = rand_type() | |
name = rand_name(energy_type) | |
return { | |
"name": name, | |
"hp": rand_hp(), | |
"energy_type": energy_type, | |
"species": rand_species(lists["species"]), | |
"length": rand_length(), | |
"weight": rand_weight(), | |
"attacks": rand_attacks(lists["attacks"], name, energy_type=energy_type), | |
"weakness": rand_type(can_be_none=True), | |
"resistance": rand_type(can_be_none=True), | |
"retreat": rand_retreat(), | |
"description": rand_description(lists["descriptions"]), | |
"rarity": rand_rarity(), | |
} | |