From d7cd2f3b1b7cacf97bb7ae7ca9ca8642c50e2421 Mon Sep 17 00:00:00 2001 From: lnguy Date: Wed, 20 Dec 2023 11:08:14 +0100 Subject: [PATCH] Refactor --- src/main.py | 26 ++++++++++----------- src/module/embed.py | 23 ++++++++++--------- src/module/json_movelist_reader.py | 9 +++++--- src/wavu/test/test_wavu_reader.py | 12 +++++----- src/wavu/wavu_importer.py | 2 +- src/wavu/wavu_reader.py | 37 +++++++++++++++++------------- 6 files changed, 59 insertions(+), 50 deletions(-) diff --git a/src/main.py b/src/main.py index 4c43ae4..991848a 100644 --- a/src/main.py +++ b/src/main.py @@ -8,7 +8,6 @@ from src.module import util from src.module import character - from threading import Thread sys.path.insert(0, (os.path.dirname(os.path.dirname(__file__)))) @@ -24,6 +23,7 @@ character_list = [] + class heihachi(discord.Client): def __init__(self, *, intents: discord.Intents): super().__init__(intents=intents) @@ -50,24 +50,24 @@ async def on_ready(self): async def self(interaction: discord.Interaction, character_name: str, move: str): original_character_name = character_name original_move = move - character_name = util.correct_character_name(character_name.lower()) + character_name = util.correct_character_name(original_character_name.lower()) if character_name: character = util.get_character_by_name(character_name, character_list) move_list = json_movelist_reader.get_movelist(character_name) - move_type = util.get_move_type(move) + move_type = util.get_move_type(original_move) if move_type: - moves = json_movelist_reader.get_by_move_type(move_type,move_list) - moves_embed = embed.move_list_embed(character,moves,move_type) + moves = json_movelist_reader.get_by_move_type(move_type, move_list) + moves_embed = embed.move_list_embed(character, moves, move_type) await interaction.response.send_message(embed=moves_embed, ephemeral=False) else: - character_move = json_movelist_reader.get_move(move, move_list) + character_move = json_movelist_reader.get_move(original_move, move_list) if character_move: move_embed = embed.move_embed(character, character_move) await interaction.response.send_message(embed=move_embed, ephemeral=False) else: - similar_moves = json_movelist_reader.get_similar_moves(original_move,move_list) - similar_moves_embed = embed.similar_moves_embed(similar_moves,character_name) + similar_moves = json_movelist_reader.get_similar_moves(original_move, move_list) + similar_moves_embed = embed.similar_moves_embed(similar_moves, character_name) await interaction.response.send_message(embed=similar_moves_embed, ephemeral=False) else: error_embed = embed.error_embed(f'Character {original_character_name} does not exist.') @@ -77,14 +77,14 @@ async def self(interaction: discord.Interaction, character_name: str, move: str) def create_json_movelists(character_list_path: str) -> List[character.Character]: with open(character_list_path) as file: all_characters = json.load(file) - character_list = [] + cha_list = [] for character_meta in all_characters: - character = wavu_importer.import_character(character_meta) - character.export_movelist_as_json() - character_list.append(character) + cha = wavu_importer.import_character(character_meta) + cha.export_movelist_as_json() + cha_list.append(cha) - return character_list + return cha_list def schedule_create_json_movelists(character_list_path: str, scheduler): diff --git a/src/module/embed.py b/src/module/embed.py index 80630bd..c95c1dc 100644 --- a/src/module/embed.py +++ b/src/module/embed.py @@ -5,19 +5,20 @@ import re MOVE_NOT_FOUND_TITLE = 'Move not found' -SUCCESS_COLOR = discord.Colour.from_rgb(50,168,82) -WARNING_COLOR = discord.Colour.from_rgb(253,218,13) -ERROR_COLOR = discord.Colour.from_rgb(220,20,60) +SUCCESS_COLOR = discord.Colour.from_rgb(50, 168, 82) +WARNING_COLOR = discord.Colour.from_rgb(253, 218, 13) +ERROR_COLOR = discord.Colour.from_rgb(220, 20, 60) -def _upper_first_letter(input :str) -> str: + +def _upper_first_letter(input: str) -> str: if input: result_string = input[0].capitalize() + input[1:] return result_string else: return input -def similar_moves_embed(similar_moves, character_name): +def similar_moves_embed(similar_moves, character_name): command_list = [] for i in range(len(similar_moves)): command_list.append(f'**{i + 1}**. {similar_moves[i]["input"]}') @@ -27,6 +28,7 @@ def similar_moves_embed(similar_moves, character_name): .format(character_name, '\n'.join(command_list))) return embed + def move_list_embed(character, moves, move_type): """Returns the embed message for a list of moves matching to a special move type""" desc_string = '' @@ -39,15 +41,15 @@ def move_list_embed(character, moves, move_type): description=desc_string) return embed + def error_embed(err): embed = discord.Embed(title='Error', colour=ERROR_COLOR, description=err) return embed -def move_embed(character :character, move :dict): - +def move_embed(character: character, move: dict): """Returns the embed message for character and move""" embed = discord.Embed(title='**' + move['input'] + '**', colour=SUCCESS_COLOR, @@ -56,8 +58,8 @@ def move_embed(character :character, move :dict): ) embed.set_thumbnail(url=character.portrait[0]) - embed.set_footer(text="Wavu.wiki",icon_url="https://i.imgur.com/xfdEUee.png") - embed.set_author(name= _upper_first_letter(character.name), url=character.wavu_page) + embed.set_footer(text="Wavu.wiki", icon_url="https://i.imgur.com/xfdEUee.png") + embed.set_author(name=_upper_first_letter(character.name), url=character.wavu_page) embed.add_field(name='Target', value=move['target']) embed.add_field(name='Damage', value=move['damage']) @@ -70,5 +72,4 @@ def move_embed(character :character, move :dict): if move['notes']: embed.add_field(name="Notes", value=move['notes']) - - return embed \ No newline at end of file + return embed diff --git a/src/module/json_movelist_reader.py b/src/module/json_movelist_reader.py index 3a06861..cbd4840 100644 --- a/src/module/json_movelist_reader.py +++ b/src/module/json_movelist_reader.py @@ -1,4 +1,3 @@ - from difflib import SequenceMatcher from heapq import nlargest as _nlargest @@ -32,6 +31,7 @@ def _simplify_input(input: str) -> str: input = input.lower().replace('wr', 'fff') return input + def _is_command_in_alias(command: str, item: dict) -> bool: if 'alias' in item: aliases = item['alias'] @@ -40,6 +40,7 @@ def _is_command_in_alias(command: str, item: dict) -> bool: return True return False + def get_move(input: str, character_movelist: dict): result = [entry for entry in character_movelist if _simplify_input(entry["input"]) == _simplify_input(input)] if result: @@ -52,11 +53,13 @@ def get_move(input: str, character_movelist: dict): return result[0] return {} -def _correct_move_type(move_type :str) -> str: + +def _correct_move_type(move_type: str) -> str: for k in const.MOVE_TYPES.keys(): if move_type in const.MOVE_TYPES[k]: return k + def get_by_move_type(move_type: str, move_list: dict) -> list: """Gets a list of moves that match move_type from local_json returns a list of move Commands if finds match(es), else empty list""" @@ -104,8 +107,8 @@ def _get_close_matches_indexes(word, possibilities, n=3, cutoff=0.6): # Strip scores for the best n matches return [x for score, x in result] -def get_similar_moves(input :str, move_list: dict) -> list[str]: +def get_similar_moves(input: str, move_list: dict) -> list[str]: command_list = [] for entry in move_list: command_list.append(entry["input"]) diff --git a/src/wavu/test/test_wavu_reader.py b/src/wavu/test/test_wavu_reader.py index 64a39b4..f0f5e45 100644 --- a/src/wavu/test/test_wavu_reader.py +++ b/src/wavu/test/test_wavu_reader.py @@ -4,12 +4,12 @@ class MyTestCase(unittest.TestCase): def test_get_character_movelist(self): - character_movelist = wavu_reader.get_character_movelist("Azucena") + character_movelist = wavu_reader.get_wavu_character_movelist("Azucena") self.assertEqual(character_movelist[0].input,"1") def test_create_alias(self): - character_movelist = wavu_reader.get_character_movelist("asuka") + character_movelist = wavu_reader.get_wavu_character_movelist("asuka") move = wavu_reader.get_move("Asuka-Destabilizer.1",character_movelist) self.assertEqual(move.alias,["f+2+4,1"]) @@ -17,18 +17,18 @@ def test_create_alias(self): self.assertEqual(move.alias,["f+2+4"]) - character_movelist = wavu_reader.get_character_movelist("jun") + character_movelist = wavu_reader.get_wavu_character_movelist("jun") move = wavu_reader.get_move("Jun-1,2,u_d",character_movelist) self.assertEqual(move.alias,["1,2,d"]) self.assertEqual(move.input,"1,2,u") def test_get_move(self): - character_movelist = wavu_reader.get_character_movelist("Azucena") + character_movelist = wavu_reader.get_wavu_character_movelist("Azucena") move = wavu_reader.get_move("Azucena-df+1,4",character_movelist) self.assertEqual(move.id,"Azucena-df+1,4") def test_complete_parent_input(self): - character_movelist = wavu_reader.get_character_movelist("Azucena") + character_movelist = wavu_reader.get_wavu_character_movelist("Azucena") move = wavu_reader.get_move("Azucena-df+1,4,1",character_movelist) move2 = wavu_reader.get_move("Azucena-f+4,4~3",character_movelist) move3 = wavu_reader.get_move("Azucena-LIB.1,2",character_movelist) @@ -42,7 +42,7 @@ def test_complete_parent_input(self): self.assertEqual(move5.input,"df+1,4,1~2") self.assertEqual(move6.on_ch,"+27a") - character_movelist = wavu_reader.get_character_movelist("Bryan") + character_movelist = wavu_reader.get_wavu_character_movelist("Bryan") move7 = wavu_reader.get_move("Bryan-4,3,f+4",character_movelist) self.assertEqual(move7.on_ch,"+31a (+21)") diff --git a/src/wavu/wavu_importer.py b/src/wavu/wavu_importer.py index f68713e..0329a39 100644 --- a/src/wavu/wavu_importer.py +++ b/src/wavu/wavu_importer.py @@ -12,7 +12,7 @@ def import_character(character_meta: dict) -> character.Character: portrait = character_meta["portrait"] wavu_page = character_meta["wavu_page"] - move_list = wavu_reader.get_character_movelist(name) + move_list = wavu_reader.get_wavu_character_movelist(name) move_list_path = os.path.abspath(os.path.join(base_path, "..", "json_movelist", name + ".json")) cha = character.Character(name, portrait, move_list, move_list_path,wavu_page) return cha diff --git a/src/wavu/wavu_reader.py b/src/wavu/wavu_reader.py index bd1293d..3a22ec9 100644 --- a/src/wavu/wavu_reader.py +++ b/src/wavu/wavu_reader.py @@ -9,14 +9,16 @@ wavuwiki = MediaWiki(url=const.WAVU_API_URL) session = requests.Session() -def _upper_first_letter(input :str) -> str: + +def _upper_first_letter(input: str) -> str: if input: result_string = input[0].capitalize() + input[1:] return result_string else: return input -def get_character_movelist(character_name: str) -> List[Move]: + +def get_wavu_character_movelist(character_name: str) -> List[Move]: params = { "action": "cargoquery", "tables": "Move", @@ -52,36 +54,37 @@ def _get_all_parent_values_of(field: str, move_id: str, move_list_json: list) -> original_move = move["title"]["parent"] if "_" in original_move: original_move = original_move.split("_")[0] - complete_input += _get_all_parent_values_of(field,original_move, move_list_json) + complete_input += _get_all_parent_values_of(field, original_move, move_list_json) return complete_input + move["title"][field] else: return "" + def _remove_non_ascii(data): return re.sub(r'[^\x00-\x7F]+', '', data) -#last entry is always the input -def _create_alias(input :str) -> List[str]: - +# last entry is always the input +def _create_alias(input: str) -> List[str]: parts = input.split("_") input = parts[0] aliases = parts[1:] result = [] for entry in aliases: num_characters = len(entry) - x = len(input)-num_characters - if x<0: - x=0 - original_input= input[0:x] + x = len(input) - num_characters + if x < 0: + x = 0 + original_input = input[0:x] alias = original_input + entry if len(alias) > len(input): - input=input+entry[len(input):] + input = input + entry[len(input):] result.append(alias) result.append(input) return result + def _convert_json_movelist(move_list_json: list) -> List[Move]: move_list = [] for move in move_list_json: @@ -89,14 +92,17 @@ def _convert_json_movelist(move_list_json: list) -> List[Move]: id = _remove_non_ascii(move["title"]["id"]) name = _remove_non_ascii(move["title"]["name"]) - input = _remove_non_ascii(_get_all_parent_values_of("input", move["title"]["parent"], move_list_json) + move["title"]["input"]) + input = _remove_non_ascii( + _get_all_parent_values_of("input", move["title"]["parent"], move_list_json) + move["title"]["input"]) if "_" in input: result = _create_alias(input) input = result[-1] - alias = result[0:(len(result)-1)] + alias = result[0:(len(result) - 1)] - target = _remove_non_ascii(_get_all_parent_values_of("target", move["title"]["parent"], move_list_json) + move["title"]["target"]) - damage = _remove_non_ascii(_get_all_parent_values_of("damage", move["title"]["parent"], move_list_json) + move["title"]["damage"]) + target = _remove_non_ascii( + _get_all_parent_values_of("target", move["title"]["parent"], move_list_json) + move["title"]["target"]) + damage = _remove_non_ascii( + _get_all_parent_values_of("damage", move["title"]["parent"], move_list_json) + move["title"]["damage"]) on_block = _remove_non_ascii(move["title"]["block"]) on_hit = _remove_non_ascii(_normalize_hit_ch_input(move["title"]["hit"])) @@ -107,7 +113,6 @@ def _convert_json_movelist(move_list_json: list) -> List[Move]: notes = html.unescape(move["title"]["notes"]) notes = BeautifulSoup(notes, features="lxml").get_text() - move = Move(id, name, input, target, damage, on_block, on_hit, on_ch, startup, recovery, notes, "", alias) move_list.append(move) return move_list