Skip to content

Commit

Permalink
Refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
TLNBS2405 committed Dec 14, 2023
1 parent 88822ea commit 8cfbab7
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 18 deletions.
21 changes: 20 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,21 @@
# heihachi
# Heihachi
A discord bot to receive Tekken 8 frame data primary from [wavu.wiki](https://wavu.wiki/t/Wavu:Tekken_8)

## Instruction

Clone this to a linux server that has Python 3.10.0+ and install the dependencies with:
```py
pip install -r requirements.txt
```

You need your own discord bot ([instructions](https://github.com/reactiflux/discord-irc/wiki/Creating-a-discord-bot-&-getting-a-token)) and have the tokens in the `src/resources/config.json`. You can add a feedback channel there also.


The executable is `src/main.py`.

## Commands
```
Slash Command
/fd <character> <move> - get frame data of a move from a character
```
25 changes: 13 additions & 12 deletions src/main.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import json, datetime, logging, os, discord, sched, time
from typing import List

from src.module import character_importer
from src.module import wavu_importer
from src.module import configurator
from src.module import json_movelist_reader
from src.module import embed
Expand Down Expand Up @@ -49,40 +49,41 @@ async def self(interaction: discord.Interaction, character_name: str, move: str)
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)
move_list = json_movelist_reader.get_movelist(character_name)

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) -> List[character.Character]:
def create_json_movelists(character_list_path: str) -> List[character.Character]:
with open(character_list_path) as file:
characters = json.load(file)
all_characters = json.load(file)
character_list = []

for character_meta in characters:
character = character_importer.import_character(character_meta)
for character_meta in all_characters:
character = wavu_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):
def schedule_create_json_movelists(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,))
create_json_movelists(character_list_path)
scheduler.enter(3600, 1, create_json_movelists, (character_list_path, scheduler,))

except Exception as e:
raise Exception("Error when importing character from wavu" + str(e))


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

## Repeat importing move list of all character from wavu.wiki once an hour
scheduler.enter(3600, 1, schedule_create_json_movelists, (character_list_path, scheduler,))
Thread(target=scheduler.run).start()

client.run(discord_token)
Expand Down
2 changes: 1 addition & 1 deletion src/module/json_movelist_reader.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
base_path = os.path.dirname(__file__)


def get_movelist_from_json(character_name: str) -> dict:
def get_movelist(character_name: str) -> dict:
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:
Expand Down
4 changes: 2 additions & 2 deletions src/module/test/test_character_importer.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import unittest, json, os
from src.module import character_importer
from src.module import wavu_importer

dir_path = os.path.dirname(os.path.realpath(__file__))

Expand All @@ -10,5 +10,5 @@ def test_character_importer(self):
azu_json = dir_path + "/azu.json"
with open(azu_json) as azu:
azu_meta = json.load(azu)
azucena = character_importer.import_character(azu_meta)
azucena = wavu_importer.import_character(azu_meta)
self.assertEqual(azucena.name, "Azucena")
4 changes: 2 additions & 2 deletions src/module/test/test_json_movelist_reader.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@ class MyTestCase(unittest.TestCase):

def test_get_movelist_from_json(self):

result = json_movelist_reader.get_movelist_from_json("azucena")
result = json_movelist_reader.get_movelist("azucena")
self.assertEqual(result[0]["id"],"Azucena-1")


def test_get_move(self):

azu_move_list = json_movelist_reader.get_movelist_from_json("azucena")
azu_move_list = json_movelist_reader.get_movelist("azucena")
move = json_movelist_reader.get_move("d/f+1",azu_move_list)

self.assertEqual(move["id"],"Azucena-df+1")
Expand Down
File renamed without changes.

0 comments on commit 8cfbab7

Please sign in to comment.