Skip to content

Commit

Permalink
Refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
TLNBS2405 committed Dec 20, 2023
1 parent e3e47a2 commit d7cd2f3
Show file tree
Hide file tree
Showing 6 changed files with 59 additions and 50 deletions.
26 changes: 13 additions & 13 deletions src/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -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__))))
Expand All @@ -24,6 +23,7 @@

character_list = []


class heihachi(discord.Client):
def __init__(self, *, intents: discord.Intents):
super().__init__(intents=intents)
Expand All @@ -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.')
Expand All @@ -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):
Expand Down
23 changes: 12 additions & 11 deletions src/module/embed.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"]}')
Expand All @@ -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 = ''
Expand All @@ -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,
Expand All @@ -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'])
Expand All @@ -70,5 +72,4 @@ def move_embed(character :character, move :dict):
if move['notes']:
embed.add_field(name="Notes", value=move['notes'])


return embed
return embed
9 changes: 6 additions & 3 deletions src/module/json_movelist_reader.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

from difflib import SequenceMatcher
from heapq import nlargest as _nlargest

Expand Down Expand Up @@ -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']
Expand All @@ -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:
Expand All @@ -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"""
Expand Down Expand Up @@ -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"])
Expand Down
12 changes: 6 additions & 6 deletions src/wavu/test/test_wavu_reader.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,31 +4,31 @@
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"])

move = wavu_reader.get_move("Asuka-f+1+3",character_movelist)
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)
Expand All @@ -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)")

2 changes: 1 addition & 1 deletion src/wavu/wavu_importer.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
37 changes: 21 additions & 16 deletions src/wavu/wavu_reader.py
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -52,51 +54,55 @@ 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:
alias = []
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"]))
Expand All @@ -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
Expand Down

0 comments on commit d7cd2f3

Please sign in to comment.