-
Notifications
You must be signed in to change notification settings - Fork 0
/
generate.py
147 lines (127 loc) · 5.11 KB
/
generate.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
import json
import re
def clean_pokemon_name(pokemon):
"""
Clean the Pokémon name by removing any prefixes like "1st:", "2nd:", "3rd:" and extra spaces.
"""
# Remove prefixes like "1st:", "2nd:", "3rd:" and any extra spaces
pokemon = re.sub(r'^\d+(st|nd|rd|th):\s*', '', pokemon).strip()
# Remove any "(Encounter)" or "(Shiny)" tags
pokemon = re.sub(r'\s*\(Encounter\)', '', pokemon).strip()
pokemon = re.sub(r'\s*\(Shiny\)', '', pokemon).strip()
return pokemon.upper()
def parse_pokemon_line(line, slot):
"""
Parse a line of Pokémon data and return the formatted dictionary for the team array.
"""
pokemon_data = []
pokemons = re.split(r',\s*', line)
for pokemon in pokemons:
is_shiny = "(Shiny)" in pokemon
cleaned_name = clean_pokemon_name(pokemon)
pokemon_lower = cleaned_name.lower()
# Determine the form and clean the name accordingly
if "alolan" in pokemon_lower:
form = "ALOLAN"
name = cleaned_name.replace("ALOLAN", "").strip()
elif "galarian" in pokemon_lower:
form = "GALARIAN"
name = cleaned_name.replace("GALARIAN", "").strip()
elif "paldean" in pokemon_lower:
form = "PALDEAN"
name = cleaned_name.replace("PALDEAN", "").strip()
elif "hisuian" in pokemon_lower:
form = "HISUIAN"
name = cleaned_name.replace("HISUIAN", "").strip()
else:
form = "FORM_UNSET"
name = cleaned_name
# Format the Pokémon name with form if applicable
pokemon_form_name = f"{name}_{form}" if form != "FORM_UNSET" else name
pokemon_data.append({
"pokemon": {"name": name},
"form": {"name": pokemon_form_name},
"slot": slot,
})
return pokemon_data
def parse_rewards(line):
"""
Parse a line of Pokémon marked as encounter and return the formatted dictionary for the rewards array.
"""
reward_data = []
pokemons = re.split(r',\s*', line)
for pokemon in pokemons:
is_shiny = "(Shiny)" in pokemon
cleaned_name = clean_pokemon_name(pokemon)
pokemon_lower = cleaned_name.lower()
# Determine the form and clean the name accordingly
if "alolan" in pokemon_lower:
form = "ALOLAN"
name = cleaned_name.replace("ALOLAN", "").strip()
elif "galarian" in pokemon_lower:
form = "GALARIAN"
name = cleaned_name.replace("GALARIAN", "").strip()
elif "paldean" in pokemon_lower:
form = "PALDEAN"
name = cleaned_name.replace("PALDEAN", "").strip()
elif "hisuian" in pokemon_lower:
form = "HISUIAN"
name = cleaned_name.replace("HISUIAN", "").strip()
else:
form = "FORM_UNSET"
name = cleaned_name
# Format the Pokémon name with form if applicable
pokemon_form_name = f"{name}_{form}" if form != "FORM_UNSET" else name
reward_data.append({
"pokemon": {"name": name},
"form": {"name": pokemon_form_name},
"shinies": 1 if is_shiny else 0
})
return reward_data
def parse_character_data(character_name, lines):
"""
Parse the character data based on the type and return the formatted dictionary.
"""
character_name = character_name.strip().upper().replace(" (FEMALE)", " FEMALE").replace(" (MALE)", " MALE")
character_data = {
"character": {
"name": f"CHARACTER_{character_name.replace(' ', '_').replace('(', '').replace(')', '')}"
},
"rewards": [],
"team": []
}
slot = 0
for line in lines:
line = line.strip()
if "(Encounter)" in line:
character_data["rewards"].extend(parse_rewards(line))
# Ensure all Pokémon data including encounterables are added to team
character_data["team"].extend(parse_pokemon_line(line, slot))
slot += 1
return character_data
def main():
with open('data.txt', 'r') as file:
lines = file.readlines()
result = {"characters": []}
current_character_name = ""
current_character_lines = []
for line in lines:
line = line.strip()
if line == "":
# Empty line signals the end of a character's section
if current_character_name and current_character_lines:
result["characters"].append(parse_character_data(current_character_name, current_character_lines))
current_character_name = ""
current_character_lines = []
elif not current_character_name:
# Start of a new character's section
current_character_name = line
else:
current_character_lines.append(line)
# Add the last character
if current_character_name and current_character_lines:
result["characters"].append(parse_character_data(current_character_name, current_character_lines))
with open('data.json', 'w') as outfile:
json.dump(result, outfile, indent=4)
if __name__ == "__main__":
main()