Skip to content

Commit

Permalink
Fix HTML stuff and add embed for move
Browse files Browse the repository at this point in the history
  • Loading branch information
TLNBS2405 committed Dec 12, 2023
1 parent d89d16c commit accd4bc
Show file tree
Hide file tree
Showing 9 changed files with 85 additions and 4,607 deletions.
2,312 changes: 0 additions & 2,312 deletions src/json_movelist/azucena.json

This file was deleted.

2,158 changes: 0 additions & 2,158 deletions src/json_movelist/bryan.json

This file was deleted.

42 changes: 31 additions & 11 deletions src/main.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
import json, datetime, logging, os, discord, sched, time
from typing import List

from src.module import character_importer
from src.module import configurator
from src.module import json_movelist_reader
from src.module import embed
from src.module import util
from src.module import character


from threading import Thread


Expand All @@ -14,6 +20,7 @@
character_list_path = "resources/character_list.json"
discord_token = config.read_config()['DISCORD_TOKEN']

character_list = []

class heihachi(discord.Client):
def __init__(self, *, intents: discord.Intents):
Expand All @@ -39,20 +46,31 @@ async def on_ready(self):

@tree.command(name="fd", description="Frame data from a character move", guild=discord.Object("645011181739835397"))
async def self(interaction: discord.Interaction, character_name: str, move: str):
character_name = character_name.lower()
character_name = util.correct_character_name(character_name.lower())
character = util.get_character_by_name(character_name, character_list)

move_list = json_movelist_reader.get_movelist_from_json(character_name)
result = json_movelist_reader.get_move(move, move_list)
await interaction.response.send_message(content=result, ephemeral=False)

character_move = json_movelist_reader.get_move(move, move_list)
move_embed = embed.move_embed(character, character_move)
await interaction.response.send_message(embed=move_embed, ephemeral=False)

def create_all_character_json(character_list_path: str, scheduler):
try:
with open(character_list_path) as file:
characters = json.load(file)
def create_all_character_json(character_list_path: str) -> List[character.Character]:
with open(character_list_path) as file:
characters = json.load(file)
character_list = []

for character_meta in characters:
character = character_importer.import_character(character_meta)
character.export_movelist_as_json()
for character_meta in characters:
character = character_importer.import_character(character_meta)
character.export_movelist_as_json()
character_list.append(character)

return character_list


def schedule_create_all_characters(character_list_path: str, scheduler):
try:
create_all_character_json(character_list_path)
scheduler.enter(3600,1,create_all_character_json, (character_list_path,scheduler,))

except Exception as e:
Expand All @@ -61,8 +79,10 @@ def create_all_character_json(character_list_path: str, scheduler):

try:
## Repeat creating character json once an hour
character_list = create_all_character_json(character_list_path)
print("Character jsons are successfully created")
scheduler = sched.scheduler(time.time, time.sleep)
scheduler.enter(3600,1,create_all_character_json, (character_list_path,scheduler,))
scheduler.enter(3600,1,schedule_create_all_characters, (character_list_path,scheduler,))
Thread(target=scheduler.run).start()

client.run(discord_token)
Expand Down
6 changes: 4 additions & 2 deletions src/module/character.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from typing import List
import os, json
from json import JSONEncoder

import re

class Move:
def __init__(self, id: str, name: str, input: str, target: str, damage: str, on_block: str, on_hit: str, on_ch: str,
Expand Down Expand Up @@ -34,7 +34,7 @@ def __init__(self, name: str, portrait: str, move_list: List[Move], move_list_pa

def export_movelist_as_json(self):
self.__create_move_list_file()
with open(self.move_list_path, "w", encoding='utf8') as outfile:
with open(self.move_list_path, "w", encoding='utf-8') as outfile:
json.dump(self.move_list, outfile, sort_keys=True, indent=4, cls=MoveEncoder, ensure_ascii=False)

def __create_move_list_file(self):
Expand All @@ -45,3 +45,5 @@ def __create_move_list_file(self):
class ClassEncoder(JSONEncoder):
def default(self, o):
return o.__dict__


112 changes: 15 additions & 97 deletions src/module/embed.py
Original file line number Diff line number Diff line change
@@ -1,111 +1,29 @@
import discord
from random import randint
from src.module import character

import re

MOVE_NOT_FOUND_TITLE = 'Move not found'


def move_embed(character, move):
def move_embed(character :character, move :dict):
"""Returns the embed message for character and move"""
embed = discord.Embed(title=character['proper_name'],
embed = discord.Embed(title=character.name,
colour=0x00EAFF,
url=character['online_webpage'],
description='**Move: ' + move['Command'] + '**')
embed.set_thumbnail(url=character['portrait'])

block = "Block"
counterhit = "Counter hit"

if 'Throw' in move['Tags']:
block = "On Break"
counterhit = "Break Type"

embed.add_field(name='Property', value=move['Hit level'])
embed.add_field(name='Damage', value=move['Damage'])

result = re.match('^\d', move['Start up frame'])

if result:
embed.add_field(name='Startup', value='i' + move['Start up frame'])
else:
embed.add_field(name='Startup', value=move['Start up frame'])

embed.add_field(name=block, value=move['Block frame'])
embed.add_field(name='Hit', value=move['Hit frame'])
embed.add_field(name=counterhit, value=move['Counter hit frame'])

if 'Recovery' in move:
embed.add_field(name='On whiff', value=move['Recovery'])
if 'Notes' in move and move['Notes'] and not move['Notes'] == "-":
embed.add_field(name='Notes', value=move['Notes'])
if 'Gif' in move and move['Gif'] and not move['Gif'] == "-":
embed.add_field(name='Gif', value=move['Gif'], inline=False)
if 'Tags' in move and move['Tags'] and not move['Tags'] == "-":
embed.add_field(name='Tags', value=move['Tags'], inline=False)

random_value = randint(0, 2)
if random_value == 2:
embed.add_field(name='Dev Note',
value='**IMPORTANT** \n The bot for T7 will shutdown on the 25th Jan 2024',
inline=False)

return embed


def move_list_embed(character, move_list, move_type):
"""Returns the embed message for a list of moves matching to a special move type"""
desc_string = ''
move_list.sort()
for move in move_list:
desc_string += move + '\n'

embed = discord.Embed(title=character['proper_name'] + ' ' + move_type.lower() + ':',
colour=0x00EAFF,
description=desc_string)
return embed


def error_embed(err):
embed = discord.Embed(title='Error',
colour=0xFF4500,
description=err)
return embed


def success_embed(message):
embed = discord.Embed(title='Success',
colour=0x3ddb2c,
description=message)
return embed


def similar_moves_embed(similar_moves, character_name):
for i in range(len(similar_moves)):
similar_moves[i] = f'**{i + 1}**. {similar_moves[i]}'

embed = discord.Embed(title=MOVE_NOT_FOUND_TITLE, colour=0xfcba03,
description='Similar moves from {}\n{}'
.format(character_name, '\n'.join(similar_moves)))
return embed
description='**Move: ' + move['input'] + '**')
embed.set_thumbnail(url=character.portrait)
embed.set_footer(text=move['name'])

embed.add_field(name='Target', value=move['target'])
embed.add_field(name='Damage', value=move['damage'])

def help_embed():
text = "**Slash command** \n" \
"/fd <character> <move>\t\t\t- get frame data of a move from a character \n" \
"/last-updates\t\t\t- get the messages of some latest updates\n" \
"/feedback <message>\t\t\t- send message including sender name to the devs \n\n" \
"**Direct Ping** \n " \
"@discordbot <character> <move>\t\t\t- get frame data of a move from a character \n\n"
embed = discord.Embed(title='Commands', description=text, colour=0x37ba25)
embed.set_author(name='Author: Tib#1303')
embed.add_field(name='Startup', value=move['startup'])

return embed
embed.add_field(name="Block", value=move['on_block'])
embed.add_field(name='Hit', value=move['on_hit'])
embed.add_field(name="CH", value=move['on_ch'])
embed.add_field(name="Notes", value=move['notes'])


def thank_embed():
text = "\n\n" \
"Much thanks and love especially to T7Chicken Team, Ruxx, BKNR, Vesper, Maxwell and Evil. \n\n" \
"This project won't be possible without you guys <3"
embed = discord.Embed(title='Commands', description=text, colour=0x37ba25)
embed.set_author(name='Author: Tib')
return embed
return embed
15 changes: 0 additions & 15 deletions src/module/json_movelist_reader.py
Original file line number Diff line number Diff line change
@@ -1,27 +1,12 @@
from src.resources import const
from src.module import character_importer, character
import os, json

base_path = os.path.dirname(__file__)


def _correct_character_name(alias: str):
# check if input in dictionary or in dictionary values
if alias in const.CHARACTER_ALIAS:
return alias

for key, value in const.CHARACTER_ALIAS.items():
if alias in value:
return key

return None


def get_movelist_from_json(character_name: str) -> dict:
character_name = _correct_character_name(character_name)
os.path.abspath(os.path.join(base_path, "..", "json_movelist", character_name + ".json"))
filepath = os.path.abspath(os.path.join(base_path, "..", "json_movelist", character_name + ".json"))

with open(filepath) as move_file:
move_file_contents = json.loads(move_file.read())
return move_file_contents
Expand Down
21 changes: 21 additions & 0 deletions src/module/util.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
from src.resources import const
from src.module import character


def correct_character_name(alias: str):
# check if input in dictionary or in dictionary values
if alias in const.CHARACTER_ALIAS:
return alias

for key, value in const.CHARACTER_ALIAS.items():
if alias in value:
return key

return None

def get_character_by_name(name :str, character_list :[]) -> character.Character:
for character in character_list:
if character.name == name:
return character


4 changes: 2 additions & 2 deletions src/resources/character_list.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
[
{
"name": "azucena",
"portrait": "x"
"portrait": "https://i.imgur.com/yeyRqH2.jpg"
},
{
"name": "bryan",
"portrait": "x"
"portrait": "https://i.imgur.com/PV9xTeM.jpg"
}
]
22 changes: 12 additions & 10 deletions src/wavu/wavu_reader.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,21 +54,23 @@ def _get_all_parent_values_of(field: str, move_id: str, move_list_json: list) ->
else:
return ""

def _remove_non_ascii(data):
return re.sub(r'[^\x00-\x7F]+', '', data)

def _convert_json_movelist(move_list_json: list) -> List[Move]:
move_list = []
for move in move_list_json:
id = move["title"]["id"]
name = move["title"]["name"]
input = _get_all_parent_values_of("input", move["title"]["parent"], move_list_json) + move["title"]["input"]
target = _get_all_parent_values_of("target", move["title"]["parent"], move_list_json) + move["title"]["target"]
damage = _get_all_parent_values_of("damage", move["title"]["parent"], move_list_json) + move["title"]["damage"]
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"])
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 = move["title"]["block"]
on_hit = _normalize_hit_ch_input(move["title"]["hit"])
on_ch = _normalize_hit_ch_input(move["title"]["ch"])
startup = move["title"]["startup"]
recovery = move["title"]["recv"]
on_block = _remove_non_ascii(move["title"]["block"])
on_hit = _remove_non_ascii(_normalize_hit_ch_input(move["title"]["hit"]))
on_ch = _remove_non_ascii(_normalize_hit_ch_input(move["title"]["ch"]))
startup = _remove_non_ascii(move["title"]["startup"])
recovery = _remove_non_ascii(move["title"]["recv"])

notes = html.unescape(move["title"]["notes"])
notes = BeautifulSoup(notes, features="lxml").get_text()
Expand Down

0 comments on commit accd4bc

Please sign in to comment.