mirror of
https://github.com/LazoCoder/Pokemon-Terminal
synced 2024-11-23 20:33:08 +00:00
You can see if a Pokemon exists in the database now
This commit is contained in:
parent
bdca26f777
commit
c8b3c5d8f1
1 changed files with 38 additions and 12 deletions
|
@ -8,8 +8,8 @@
|
||||||
# reason for this is because 'Extra' images don't have IDs. The tuple contains the name of
|
# reason for this is because 'Extra' images don't have IDs. The tuple contains the name of
|
||||||
# the image and the location of the image.
|
# the image and the location of the image.
|
||||||
|
|
||||||
|
|
||||||
import os
|
import os
|
||||||
|
from sys import argv
|
||||||
|
|
||||||
|
|
||||||
class Pokemon:
|
class Pokemon:
|
||||||
|
@ -43,8 +43,9 @@ class Pokemon:
|
||||||
class Database:
|
class Database:
|
||||||
__pokemon_list = []
|
__pokemon_list = []
|
||||||
__pokemon_dictionary = {}
|
__pokemon_dictionary = {}
|
||||||
__extra_pokemon = [] # The Pokemon from the 'Extra' folder.
|
__extra_pokemon = {} # The Pokemon from the 'Extra' folder.
|
||||||
__directory = "" # The global location of the code.
|
__directory = "" # The global location of the code.
|
||||||
|
__MAX_ID = 493 # Highest possible Pokemon ID.
|
||||||
|
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
self.directory = os.get_exec_path()[0]
|
self.directory = os.get_exec_path()[0]
|
||||||
|
@ -57,16 +58,39 @@ class Database:
|
||||||
string += str(element) + "\n"
|
string += str(element) + "\n"
|
||||||
string += "EXTRA:\n"
|
string += "EXTRA:\n"
|
||||||
for element in self.__extra_pokemon:
|
for element in self.__extra_pokemon:
|
||||||
string += str(element[0]) + " in " + str(element[1] + "\n")
|
string += str(element) + " in " + str(self.__extra_pokemon[element] + "\n")
|
||||||
return string[:-1] # Remove the final new line ("\n").
|
return string[:-1] # Remove the final new line ("\n").
|
||||||
|
|
||||||
|
def pokemon_exists(self, pokemon):
|
||||||
|
# Check for a Pokemon by ID or name.
|
||||||
|
if type(pokemon) is int or str(pokemon).isdigit():
|
||||||
|
return self.id_exists(pokemon)
|
||||||
|
else:
|
||||||
|
return self.name_exists(pokemon)
|
||||||
|
|
||||||
|
def id_exists(self, identifier):
|
||||||
|
# Check for Pokemon by ID.
|
||||||
|
identifier = int(identifier)
|
||||||
|
if identifier < 1 or identifier > self.__MAX_ID:
|
||||||
|
return False
|
||||||
|
else:
|
||||||
|
return True
|
||||||
|
|
||||||
|
def name_exists(self, name):
|
||||||
|
# Check for Pokemon by Name.
|
||||||
|
if name.lower() in self.__pokemon_dictionary:
|
||||||
|
return True
|
||||||
|
if name.lower() in self.__extra_pokemon:
|
||||||
|
return True
|
||||||
|
return False
|
||||||
|
|
||||||
def __load_data(self):
|
def __load_data(self):
|
||||||
# Load all the Pokemon data. This does not include the 'Extra' Pokemon.
|
# Load all the Pokemon data. This does not include the 'Extra' Pokemon.
|
||||||
path = self.directory + "/./Data/pokemon.txt"
|
path = self.directory + "/./Data/pokemon.txt"
|
||||||
data_file = open(path, "r+")
|
data_file = open(path, "r+")
|
||||||
for line in data_file: # Load everything but the Pokemon from the 'Extra' folder.
|
for line in data_file: # Load everything but the Pokemon from the 'Extra' folder.
|
||||||
identifier = line.split(' ')[0] # First part of the line is the id.
|
identifier = line.split(' ')[0] # First part of the line is the id.
|
||||||
name = line[len(identifier)+1:-1] # The rest is the name (minus the new line at the end).
|
name = line[len(identifier)+1:-1].lower() # The rest is the name (minus the new line at the end).
|
||||||
identifier = self.__add_zeroes(identifier) # This statement cannot occur before name has been created.
|
identifier = self.__add_zeroes(identifier) # This statement cannot occur before name has been created.
|
||||||
region = self.__determine_region(identifier)
|
region = self.__determine_region(identifier)
|
||||||
folder = self.__determine_folder(identifier)
|
folder = self.__determine_folder(identifier)
|
||||||
|
@ -78,10 +102,9 @@ class Database:
|
||||||
# Load all the file names of the images in the Extra folder.
|
# Load all the file names of the images in the Extra folder.
|
||||||
for file in os.listdir(self.directory + "/./Images/Extra"):
|
for file in os.listdir(self.directory + "/./Images/Extra"):
|
||||||
if file.endswith(".png"):
|
if file.endswith(".png"):
|
||||||
name = os.path.join("/Images/Extra", file).split('/')[-1][0:-4]
|
name = os.path.join("/Images/Extra", file).split('/')[-1][0:-4].lower()
|
||||||
folder = self.directory + "/./Images/Extra"
|
folder = self.directory + "/./Images/Extra"
|
||||||
extra = (name, folder)
|
self.__extra_pokemon[name] = folder
|
||||||
self.__extra_pokemon.append(extra)
|
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def __add_zeroes(number):
|
def __add_zeroes(number):
|
||||||
|
@ -93,8 +116,7 @@ class Database:
|
||||||
zeroes = "0"
|
zeroes = "0"
|
||||||
return zeroes + str(number)
|
return zeroes + str(number)
|
||||||
|
|
||||||
@staticmethod
|
def __determine_region(self, identifier):
|
||||||
def __determine_region(identifier):
|
|
||||||
# Determine which region a Pokemon is from.
|
# Determine which region a Pokemon is from.
|
||||||
identifier = int(identifier)
|
identifier = int(identifier)
|
||||||
if identifier < 1:
|
if identifier < 1:
|
||||||
|
@ -126,6 +148,10 @@ class Database:
|
||||||
else:
|
else:
|
||||||
raise Exception("Pokemon ID cannot be greater than 493.")
|
raise Exception("Pokemon ID cannot be greater than 493.")
|
||||||
|
|
||||||
|
# Method for debugging.
|
||||||
database = Database()
|
if __name__ == "__main__":
|
||||||
print(database)
|
database = Database()
|
||||||
|
if len(argv) == 1:
|
||||||
|
print(database)
|
||||||
|
else:
|
||||||
|
print(database.pokemon_exists(argv[1]))
|
||||||
|
|
Loading…
Reference in a new issue