-
Notifications
You must be signed in to change notification settings - Fork 0
/
pokeTP_utils.py
194 lines (141 loc) · 6.54 KB
/
pokeTP_utils.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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
import os
import sys
import requests
import json
from tabulate import tabulate
##
DEBUG_MODE = True
def toggleDebugMode (debug_flag):
global DEBUG_MODE
DEBUG_MODE = debug_flag
return
def debugPrint (message, debug = True):
if (debug):
print(message)
return
def queryPokeAPI (url, cache = True, checkCache = True):
# Cached directory
api_directory = url[26:-1] # 26 truncates 'https://pokeapi.co/api/v2/'
if (api_directory.find('?')):
# If a '?' is in the url, it means the query will return a partial response
api_directory = api_directory.replace('?', '/partial').replace('offset=', '_').replace('limit=', '')
cache_fullPath = f'pokeAPICache/{api_directory}.json'
if (checkCache and os.path.exists(cache_fullPath)):
with open(cache_fullPath, 'r') as cached_file:
return json.load(cached_file)
response = requests.get(url)
if response.status_code != 200:
debugPrint(f"Error: {response.status_code}", DEBUG_MODE)
sys.exit()
# Convert response into json
response = response.json()
if not cache:
return response
# Create directory and cache the response
cache_directory = os.path.dirname(cache_fullPath)
if not os.path.exists(cache_directory):
os.makedirs(cache_directory)
with open(cache_fullPath, 'w') as file:
json.dump(response, file, indent=4)
return response
def selectPokemonSpecies (pokemon_query):
pokemon_data_iterator = 0
# Querying pokemon species to list all the possible species with the given name (region forms, etc)
results = queryPokeAPI(f'https://pokeapi.co/api/v2/pokemon-species/{pokemon_query}/')
variant_list = []
for variant in results['varieties']:
variant_list.append(variant['pokemon']['name'])
if (len(variant_list) == 1):
return variant_list[0]
# This pokemon has more than one variant
# A list of the variants should be displayed so the user can manually pick the desired one
print(f"{pokemon_query} has more than one variant.")
list_iterator = 0
for variant in variant_list:
list_iterator += 1
print(f'\t{list_iterator})', variant)
# Input sanity check
while True:
selected_variant = int(input(f"Please select the desired variant [1 - {list_iterator}]: "))
if (selected_variant > 0 and selected_variant <= len(variant_list)):
break
print(f"Error: your input is outside the allowed selection, please input a number in the following interval: 0 - {list_iterator}\n")
debugPrint(f"Selected pokémon: {variant_list[selected_variant-1]}", DEBUG_MODE)
return variant_list[selected_variant-1]
def selectGameName (game_query):
game_area_iterator = 0
games_list = []
# Collecting each game that matches the game_query onto a list
while True:
results = queryPokeAPI(f'https://pokeapi.co/api/v2/version?offset={game_area_iterator}&limit=20/')
# Check if the games on the list match the game_query
for result in results['results']:
if (game_query in result['name']):
games_list.append(result['name'])
# Update the iterator according to the default pokeAPI limit (=20)
game_area_iterator += 20
# Aborts the search when the last batch is reached (there's no next)
if (results['next'] is None):
break
if (not len(games_list)):
debugPrint(f"Error: couldn't find a game that matches the argument: {game_query}\nAborting...", DEBUG_MODE)
sys.exit()
if (len(games_list) == 1):
debugPrint(f"Game selected: {games_list[0]}", DEBUG_MODE)
return games_list[0]
# If the list has more than one element, the search was not conclusive
# A list of the matches should be displayed so the user can manually pick a game
print(f"Ambiguous game name, did you mean?")
list_iterator = 0
for game in games_list:
list_iterator += 1
print(f'\t{list_iterator})', game)
# Input sanity check
while True:
selected_game = int(input(f"Please select the desired game [1 - {list_iterator}]: "))
if (selected_game > 0 and selected_game <= len(games_list)):
break
print(f"Error: your input is outside the allowed selection, please input a number in the following interval: 0 - {list_iterator}\n")
debugPrint(f"Game selected: {games_list[selected_game-1]}", DEBUG_MODE)
return games_list[selected_game-1]
def getLocationAreasFromLocation (location_url):
location_areas_list = []
location_areas_iterator = 1
location_query = queryPokeAPI(location_url)
for location_area in location_query['areas']:
debugPrint(f"\t{location_areas_iterator}) {location_area['name']}", DEBUG_MODE)
location_areas_list.append(location_area['name'])
location_areas_iterator += 1
return location_areas_list
def checkEncountersInLocationArea (game_version, location_area_name):
location_area_query = queryPokeAPI(f'https://pokeapi.co/api/v2/location-area/{location_area_name}/')
for encounter in location_area_query['pokemon_encounters']:
for encounter_version in encounter['version_details']:
if (encounter_version['version']['name'] == game_version):
debugPrint(f'\t\tEncounter detected in {location_area_name}', DEBUG_MODE)
return True
return False
def printTableHead (content, table_width):
print(f'+', f'='*(table_width-2), f'+', sep='')
print(f'|', f'{content:^{table_width-2}}', f'|', sep='')
def getTerminalWidth (discounted_chars = 2):
return os.get_terminal_size().columns - discounted_chars
def printFramedTitle (content, new_lines = 1, title_width_per = 100, centered = False):
# Title with frame cannot exceed 100% of the terminal width
MAX_TITLE_WITDH = 100
if (title_width_per > MAX_TITLE_WITDH):
title_width_per = MAX_TITLE_WITDH
# Default padding
padding = 0
# New empty lines for spacing
for i in range(new_lines):
print()
# Calculate the frame width
frame_width = getTerminalWidth()*title_width_per/100
frame_width = int(frame_width)
# If the title width is smaller than 100%, pad the frame so it looks centered
if (centered == True and title_width_per < MAX_TITLE_WITDH):
padding = int((MAX_TITLE_WITDH-title_width_per)/2)
print(f'{" "}'*padding + f'+', f'='*frame_width, f'+', sep='')
print(f'{" "}'*padding + f'|', f'{content:^{frame_width}}', f'|', sep='')
print(f'{" "}'*padding + f'+', f'='*frame_width, f'+', sep='')