def __getWeaknesses(name): """ @param name is the name of the pokemon to get weaknesses for @returns a list of types that the pokemon is weak against """ # halfQuery is completed a little later in the code halfQuery = """ FROM tbl_weakness WHERE (type1, type2) IN (SELECT type1, type2 FROM tbl_pokemon WHERE name ILIKE %(pokemon_name)s); """ ListOfTypesQuery = """ SELECT SUBSTRING(column_name, 9) FROM information_schema.columns WHERE table_schema='public' AND table_name='tbl_weakness' AND column_name NOT ILIKE 'type%'; """ weaknesses = [] connection = DB_conn.getConn() with connection.cursor( cursor_factory=psycopg2.extras.DictCursor) as cursor: cursor.execute(ListOfTypesQuery) types = [x[0] for x in cursor.fetchall()] # NOTE variable types is from a static query, so it is safe. Just wanted to ensure the order and flexibility query = "SELECT " + ", ".join(["against_" + x for x in types]) + halfQuery cursor.execute(query, {"pokemon_name": name}) row = cursor.fetchone() for i in range(len(types)): if row[i] > 1: weaknesses.append(types[i]) return weaknesses
def wilson_feature1(): conn = DB_conn.getConn() moves_completer = WordCompleter(all_moves, ignore_case=True) print( "Given a move, this will return all Pokemon that are weak to this move. It will not take into account abilities that may negate damage." ) option = prompt("Select a move>", completer=moves_completer) move_query_weak(conn, option)
def arron_feature3(move=False): """ @param move determines which route the feature will take If move == False Finds the pokemon using ILIKE, in case you don't know how to spell the pokemon name. Returns a list of pokemon names that match the given input else: Finds the move name using ILIKE, in case you don't know how to spell the move. Returns a list of move names that match the given input """ thing = "pokemon" if not move else "move" print( "Not sure what {0} you are thinking of? Enter part of the name that you remember to try matching it with a {0} name." .format(thing)) user_input = None while (True): user_input = prompt("Enter part of a {0} name>".format(thing)) user_input = user_input.strip() if user_input == "": user_input = None print("Please enter at least 1 character!", flush=True) continue else: if not move: query = """ SELECT name FROM tbl_pokemon WHERE name ILIKE %(name)s ORDER BY name ASC """ else: query = """ SELECT name FROM tbl_allmoves WHERE name ILIKE %(name)s ORDER BY name ASC """ conn = DB_conn.getConn() with conn.cursor() as cursor: cursor.execute(query, {"name": "%%{}%%".format(user_input)}) rows = cursor.fetchall() if len(rows) == 0: print("Could not find a {0} name matching {1}".format( thing, user_input), flush=True) else: msg = "Found " + str(len(rows)) + " matching name" msg += "s!" if len(rows) > 1 else "!" print(msg, flush=True) print("-" * len(msg), flush=True) for row in rows: print(row[0], flush=True) break
def victor_feature2(): print( "Given a move, find all the pokemon that can learn it ordered by stat." ) while True: # This while-loop sanitizes input for type move_completer = WordCompleter(all_moves, ignore_case=True) move = prompt('Please a move>', completer=move_completer) if move == "exit": return if move not in all_moves: print("move is not valid, please try again") continue break query = """ SELECT tbl_pokemon.name, SUM(tbl_pokemon.hp + tbl_pokemon.speed + tbl_pokemon.attack + tbl_pokemon.sp_attack + tbl_pokemon.defense + tbl_pokemon.sp_defense) AS total FROM tbl_allmoves INNER JOIN tbl_pokemon_moves ON tbl_allmoves.name = tbl_pokemon_moves.move_name INNER JOIN tbl_pokemon ON tbl_pokemon.pokedex_number = tbl_pokemon_moves.pokemon_id WHERE tbl_allmoves.name = %s GROUP BY tbl_pokemon.name ORDER BY total DESC; """ connection = DB_conn.getConn() cursor = connection.cursor() with cursor as cursor: cursor.execute(query, (move, )) res = cursor.fetchall() print("Here are your results:") print("---------------------------") print("| Pokemon | Total |") print("---------------------------") for entry in res: print("|{:<12} | {:^10}|".format(entry[0], entry[1])) if len(res) == 0: print("| No Results |") print("---------------------------")
def arron_feature1(): """ Given a pokemon, get a list of pokemon moves (in general) that could be super effective against it? (pokemon.csv with AllMoves) -- subquery """ print( "Please provide a pokemon to retrieve a list of moves that are super affective against it" ) while (True): pokemonName = prompt("Enter Pokemon Name>", completer=PokemonComplete) pokemonName = pokemonName.strip() if pokemonName.lower() == "exit" or pokemonName.lower() == "quit": return elif pokemonName.lower() not in (x.lower() for x in all_pokemon): if pokemonName != "": print( "{} is not a legitimate pokemon name!".format(pokemonName)) else: print("Enter a valid Pokemon name!", flush=True) continue # Get type weaknesses of the valid pokemon Weaknesses = __getWeaknesses(pokemonName) # NOTE variable Weaknesses comes from a static query, so it should be safe query = """ SELECT DISTINCT name, effect, type, pp FROM tbl_allmoves WHERE category <> 'Status' AND type ILIKE '""" + "\' OR type ILIKE \'".join(Weaknesses) + "'" + \ "ORDER BY type, name;" __printTypes(pokemonName) print( "Here are a table of moves that can deal more damage than normal against {}" .format(pokemonName), flush=True) # Get the moves that are of those type weaknesses connection = DB_conn.getConn() with connection.cursor( cursor_factory=psycopg2.extras.DictCursor) as cursor: cursor.execute(query) moves = cursor.fetchall() __printMoveTable(moves) return
def __printTypes(name): """ Prints what the pokemon is and its types @param name is the name of the pokemon to get the dual types of """ query = """ SELECT type1, type2 FROM tbl_pokemon WHERE name ILIKE %s """ conn = DB_conn.getConn() with conn.cursor(cursor_factory=psycopg2.extras.DictCursor) as cursor: cursor.execute(query, (name, )) record = cursor.fetchone() type1 = record["type1"] type2 = record["type2"] if type2 == '' or type1 == type2: print("{} is a {} type pokemon".format(name, type1), flush=True) else: print("{} is a {} {} type pokemon".format(name, type1, type2), flush=True)
def __getPokemons(move): pokemons = [] query = """ SELECT DISTINCT name FROM tbl_pokemon WHERE pokedex_number IN ( SELECT DISTINCT pokemon_id FROM tbl_pokemon_moves WHERE move_name ILIKE %s ) """ conn = DB_conn.getConn() with conn.cursor() as cursor: cursor.execute(query, (move, )) pokemon_names = cursor.fetchall() for i in range(len(pokemon_names)): pokemons.append(pokemon_names[i][0]) return pokemons
def arron_feature2(): """ Given a pokemon and an opponent pokemon, get a list of super effective moves the pokemon can learn against the opponent pokemon based on the types """ pokemonOne = None pokemonTwo = None print( "Given pokemon one and pokemon two, find moves that pokemon one can learn to use to deal a lot of damage to pokemon two", flush=True) while (True): if pokemonOne is None: pokemonOne = prompt("Enter name of Pokemon One>", completer=PokemonComplete) pokemonOne = pokemonOne.strip() if pokemonOne.lower() == "exit" or pokemonOne.lower() == "quit": return elif pokemonOne.lower() not in (x.lower() for x in all_pokemon): if pokemonOne != "": print("{} is not a legitimate pokemon name!".format( pokemonOne)) else: print("Enter a valid Pokemon name!", flush=True) pokemonOne = None continue elif pokemonTwo is None: pokemonTwo = prompt("Enter name of Pokemon Two>", completer=PokemonComplete) if pokemonTwo.lower() == "exit" or pokemonTwo.lower() == "quit": return elif pokemonTwo.lower() not in (x.lower() for x in all_pokemon): if pokemonTwo != "": print("{} is not a legitimate pokemon name!".format( pokemonTwo)) else: print("Enter a valid Pokemon name!", flush=True) pokemonTwo = None continue else: # Start query execution weaknesses = __getWeaknesses(pokemonTwo) # NOTE variable weaknesses should be safe since it comes from a static query query = """ SELECT DISTINCT name, effect, type, pp FROM tbl_allmoves JOIN (SELECT pokedex_number, move_name FROM tbl_pokemon JOIN tbl_pokemon_moves ON tbl_pokemon.pokedex_number=tbl_pokemon_moves.pokemon_id WHERE name ILIKE %(pokemonOne)s) AS move_map ON tbl_allmoves.name=move_map.move_name WHERE category <> 'Status' AND type ILIKE '""" + "\' OR type ILIKE \'".join(weaknesses) + "'" + \ "ORDER BY type, name;" __printTypes(pokemonOne) __printTypes(pokemonTwo) print( "Here are a table of moves that {} can use to deal more damage than normal against {}" .format(pokemonOne, pokemonTwo), flush=True) conn = DB_conn.getConn() with conn.cursor( cursor_factory=psycopg2.extras.DictCursor) as cursor: cursor.execute(query, {"pokemonOne": pokemonOne}) moves = cursor.fetchall() __printMoveTable(moves) return
def victor_feature1(): print( "Given the type(s) and stat(s), the analyzer will rank Pokemon ordered by the sum of the given stats" ) while True: # This while-loop sanitizes input for type type_completer = WordCompleter(all_types, ignore_case=True) input_types = prompt('Please enter type(s), seperated by space>', completer=type_completer) if input_types == "exit": return input_types_arr = input_types.split() num_types = len(input_types_arr) if num_types < 1 or num_types > 2: print("Wrong number of type, please try again") continue if not all(poke_type in all_types for poke_type in input_types_arr): print("One of the type is not valid, please try again") continue break while True: # This while-loop sanitizes input for stats stat_completer = WordCompleter(all_stats, ignore_case=True) input_stats = prompt('Please enter stats you would like to analyze>', completer=stat_completer) if input_stats == "exit": return input_stats_arr = input_stats.split() num_stats = len(input_stats_arr) if num_stats < 1 or num_stats > 6: print("Number of stats is incorrect. The range is 1-6") continue if not all(stat in all_stats for stat in input_stats_arr): print("Some stats you entered is invalid, please try again") continue break query = """ SELECT tbl_pokemon.name, SUM({}) AS total FROM tbl_pokemon WHERE tbl_pokemon.type1 = %s """.format(input_stats.replace(' ', ' + ')) if num_types == 2: query += """ AND tbl_pokemon.type2 = %s """ query += """ GROUP BY tbl_pokemon.name ORDER BY total DESC """ connection = DB_conn.getConn() cursor = connection.cursor() with cursor as cursor: if num_types == 1: cursor.execute(query, (input_types_arr[0], )) else: cursor.execute(query, ( input_types_arr[0], input_types_arr[1], )) res = cursor.fetchall() print("Here are your results:") print("---------------------------") print("| Pokemon | Total |") print("---------------------------") for entry in res: print("|{:<12} | {:^10}|".format(entry[0], entry[1])) if len(res) == 0: print("| No Results |") print("---------------------------")
""" Useful suggestion hints we might want to use """ from database import DB_conn conn = DB_conn.getConn() # all_pokemon is a list of all pokemon query = "SELECT name FROM tbl_pokemon" all_pokemon = [] cursor = conn.cursor() with cursor as cursor: cursor.execute(query) res = cursor.fetchall() for r in res: all_pokemon.append(r[0]) # all_moves is a list of all moves query = "SELECT Name FROM tbl_allmoves" all_moves = [] cursor = conn.cursor() with cursor as cursor: cursor.execute(query) res = cursor.fetchall() for r in res: all_moves.append(r[0]) all_types = [ "normal", "water", "electric",