From bcfbf1e5b54b5a6e1d33d3ab25a828c0fb6d379d Mon Sep 17 00:00:00 2001 From: gaithern <36639398+gaithern@users.noreply.github.com> Date: Fri, 12 Jan 2024 10:05:17 -0600 Subject: [PATCH] Revert "small refactor" --- worlds/kh1/Client.py | 54 ++++++++++++------------ worlds/kh1/MessageHandler.py | 79 ------------------------------------ 2 files changed, 25 insertions(+), 108 deletions(-) delete mode 100644 worlds/kh1/MessageHandler.py diff --git a/worlds/kh1/Client.py b/worlds/kh1/Client.py index a902f7b8c677..c45e11eea19d 100644 --- a/worlds/kh1/Client.py +++ b/worlds/kh1/Client.py @@ -6,7 +6,6 @@ import shutil import logging import re -from .MessageHandler import KH1_message_type, KH1_UniversalMessage, KH1_MessageHandler import time from calendar import timegm @@ -18,8 +17,6 @@ logger = logging.getLogger("Client") - - if __name__ == "__main__": Utils.init_logging("KH1Client", exception_logger="Client") @@ -33,7 +30,6 @@ def check_stdin() -> None: print("WARNING: Console input is not routed reliably on Windows, use the GUI instead.") class KH1ClientCommandProcessor(ClientCommandProcessor): - def _cmd_deathlink(self): """Toggles Deathlink""" global death_link @@ -54,7 +50,6 @@ def __init__(self, server_address, password): self.send_index: int = 0 self.syncing = False self.awaiting_bridge = False - # self.game_communication_path: files go in this path to pass data between us and the actual game if "localappdata" in os.environ: self.game_communication_path = os.path.expandvars(r"%localappdata%/KH1FM") @@ -67,19 +62,6 @@ def __init__(self, server_address, password): if file.find("obtain") <= -1: os.remove(root+"/"+file) - self.message_handler = KH1_MessageHandler(2, self.game_communication_path) - # - # self.message_handler.receive_message(KH1_message_type.test, [ - # "I'm a test message", - # "with multiple values", - # "isn't that cool?" - # ]); - # - # self.message_handler.receive_message(KH1_message_type.test, [ - # "Thats a second test message", - # "with only two values", - # ]); - async def server_auth(self, password_requested: bool = False): if password_requested and not self.password: await super(KH1Context, self).server_auth(password_requested) @@ -101,7 +83,6 @@ def endpoints(self): return [] async def shutdown(self): - self.message_handler.stop_sending(); await super(KH1Context, self).shutdown() for root, dirs, files in os.walk(self.game_communication_path): for file in files: @@ -116,11 +97,6 @@ def on_package(self, cmd: str, args: dict): filename = f"send{ss}" with open(os.path.join(self.game_communication_path, filename), 'w') as f: f.close() - self.message_handler.receive_message(KH1_message_type.test, [ - "Connected to the Multiworld!" - ]); - - if cmd in {"ReceivedItems"}: start_index = args["index"] @@ -167,13 +143,10 @@ def on_package(self, cmd: str, args: dict): itemCategory = networkItem.flags recieverName = self.player_names[recieverID] filename = "sent" - sanitizedItemName = re.sub('[^A-Za-z0-9 ]+', '',str(itemName))[:15]; - sanitizedRecieverName = re.sub('[^A-Za-z0-9 ]+', '',str(recieverName))[:6] - with open(os.path.join(self.game_communication_path, filename), 'w') as f: f.write( - sanitizedItemName + "\n" - + sanitizedRecieverName + "\n" + re.sub('[^A-Za-z0-9 ]+', '',str(itemName))[:15] + "\n" + + re.sub('[^A-Za-z0-9 ]+', '',str(recieverName))[:6] + "\n" + str(itemCategory) + "\n" + str(locationID)) f.close() @@ -183,6 +156,29 @@ def on_deathlink(self, data: typing.Dict[str, typing.Any]): f.write(str(int(data["time"]))) f.close() +#f.write(self.item_names[NetworkItem(*item).item] + "\n" + self.location_names[NetworkItem(*item).location] + "\n" + self.player_names[NetworkItem(*item).player]) + + + + #last resort we can probably do better + #input: Krujo sent Magic Upgrade to Tim ((TT3) LocationName) + # if cmd in {"PrintJSON"}: + # data = args["data"] + # if data[0]: + # msg = str(data[0]["text"]); + # #player send a location + # # if msg.startswith(self.auth + " sent "): #debug + # with open(os.path.join(self.game_communication_path, "sent"), 'w') as f: + # msg = msg.replace(self.auth + " sent ", "") + # #Magic Upgrade to Tim ((TT3) LocationName) + # splitTo = msg.split(" to ") + # targetPlayer = splitTo[1].split(" ")[0] + # f.close() + + + + + def run_gui(self): """Import kivy UI system and start running it as self.ui_task.""" from kvui import GameManager diff --git a/worlds/kh1/MessageHandler.py b/worlds/kh1/MessageHandler.py deleted file mode 100644 index b2746da09fdc..000000000000 --- a/worlds/kh1/MessageHandler.py +++ /dev/null @@ -1,79 +0,0 @@ -from enum import IntEnum -from datetime import datetime -import threading -import time -import os -import json -import sys -import asyncio -import shutil -import logging -import re - -#type of message that should be sent to the game -class KH1_message_type(IntEnum): - invalid = -1 - test = 0 - recieved_trap = 1 - sent_item = 2 - deathlink = 3 - player_joined = 4 - player_left = 5 - - -#non-persistent message that is sent to the game -class KH1_UniversalMessage(): - def __init__(self, type : KH1_message_type , values : list[str]): - self.type: KH1_message_type = type - self.values: list[str] = values - - def as_message(self): - as_string = str(self.type.value) + "\n" - for value in self.values: - as_string += value + "\n" - return as_string - -class KH1_MessageHandler(): - def __init__(self, intervalSeconds: int, directory: str): - self.interval = intervalSeconds - self.directory = directory - self.message_cache : list[KH1_UniversalMessage] = [] - self.timer = threading.Timer(self.interval, self.send_messages) - self.timer.start() - - def receive_message(self, type: KH1_message_type, values: list[str]): - new_message = KH1_UniversalMessage(type, values) - self.message_cache.append(new_message) - - def send_messages(self): - valid_messages = [] - for message in self.message_cache: - if message.type != KH1_message_type.invalid: - valid_messages.append(message) - - if len(valid_messages) > 0: - print("Sending messages to KH1") - current_time = datetime.now().strftime("%Y%m%d%H%M%S") - all_messages: str = "" - all_messages += current_time + "\n" - - for message in valid_messages: - all_messages += message.as_message() - all_messages += "-\n" - - with open(os.path.join(self.directory, "messages.sem"), 'w') as f: - f.write(all_messages) - f.close() - - os.rename( - os.path.join(self.directory, "messages.sem"), - os.path.join(self.directory, "messages") - ) - - self.message_cache.clear() - self.timer = threading.Timer(self.interval, self.send_messages) - self.timer.start() - - - def stop_sending(self): - self.timer.cancel()