diff --git a/bot.py b/bot.py index 6c69995..053cac4 100644 --- a/bot.py +++ b/bot.py @@ -13,6 +13,10 @@ from gb import Gameboy +script_dir = os.path.dirname(os.path.realpath(__file__)) +ids_loc = os.path.join(script_dir, "ids.txt") +gif_dir = os.path.join(script_dir, "gif_images") + class Bot: """ A Mastodon-API Compatible bot that handles gameboy gameplay through polls @@ -21,9 +25,7 @@ class Bot: def __init__(self, config_path="config.toml"): # If config_path is not provided, use the config.toml file in the same # directory as the script - # Get the directory of the current script - self.script_dir = os.path.dirname(os.path.realpath(__file__)) - config_path = os.path.join(self.script_dir, "config.toml") + config_path = os.path.join(script_dir, "config.toml") with open(config_path, "r", encoding="utf-8") as config_file: self.config = toml.load(config_file) self.mastodon_config = self.config.get("mastodon", {}) @@ -31,7 +33,7 @@ def __init__(self, config_path="config.toml"): self.mastodon = self.login() print(self.gameboy_config.get("rom")) - rom = os.path.join(self.script_dir, self.gameboy_config.get("rom")) + rom = os.path.join(script_dir, self.gameboy_config.get("rom")) self.gameboy = Gameboy(rom, True) def simulate(self): @@ -99,18 +101,12 @@ def post_poll(self, status, options, expires_in=60 * 60, reply_id=None): def save_ids(self, post_id, poll_id): """Saves post IDs to a text file""" - # Get the directory of the current script - script_dir = os.path.dirname(os.path.realpath(__file__)) - ids_loc = os.path.join(script_dir, "ids.txt") with open(ids_loc, "w", encoding="utf-8") as file: file.write(f"{post_id},{poll_id}") def read_ids(self): """Reads IDs from the text file""" try: - # Get the directory of the current script - script_dir = os.path.dirname(os.path.realpath(__file__)) - ids_loc = os.path.join(script_dir, "ids.txt") with open(ids_loc, "r", encoding="utf-8") as file: content = file.read() if content: @@ -191,7 +187,6 @@ def run(self): if frames >= 70: result = self.gameboy.build_gif("gif_images", self.gameboy_config['gif_outline']) else: - gif_dir = os.path.join(self.script_dir, "gif_images") self.gameboy.empty_directory(gif_dir) image = self.gameboy.screenshot() @@ -311,7 +306,6 @@ def test(self): if frames > 51: self.gameboy.build_gif("gif_images") else: - gif_dir = os.path.join(self.script_dir, "gif_images") self.gameboy.empty_directory(gif_dir) else: print(f"No action defined for '{inp}'.") diff --git a/gb.py b/gb.py index 4cbf4e0..f04ff0c 100644 --- a/gb.py +++ b/gb.py @@ -13,6 +13,9 @@ from pyboy import PyBoy, WindowEvent +script_dir = os.path.dirname(os.path.realpath(__file__)) +save_loc = os.path.join(script_dir, "save.state") + class Gameboy: """Provides an easy way to interface with pyboy @@ -65,7 +68,6 @@ def compare_frames(self, frame1, frame2): def get_recent_frames(self, directory, num_frames=100, gif_outline='gameboy.png'): """Gets the most recent frames from a provided directory""" - script_dir = os.path.dirname(os.path.realpath(__file__)) screenshot_dir = os.path.join(script_dir, directory) # Probably should replace this with heap (especially since there are so # many image files) @@ -101,8 +103,6 @@ def empty_directory(self, directory): def build_gif(self, image_path, delete=True, fps=120, output_name="action.mp4", gif_outline="gameboy.png"): """Build a gif from a folder of images""" - # Get the directory of the current script - script_dir = os.path.dirname(os.path.realpath(__file__)) gif_dir = os.path.join(script_dir, image_path) image_files = [ i for i in os.listdir(gif_dir) if os.path.isfile(os.path.join(gif_dir, i)) @@ -213,8 +213,6 @@ def select(self) -> None: def screenshot(self, path="screenshots"): """Takes a screenshot of gameboy screen and saves it to the path""" - # Get the directory of the current script - script_dir = os.path.dirname(os.path.realpath(__file__)) screenshot_dir = os.path.join(script_dir, path) # Create screenshots directory if it doesn't exist os.makedirs(screenshot_dir, exist_ok=True) @@ -253,9 +251,6 @@ def random_button(self): def load(self): """Loads the save state""" - # Get the directory of the current script - script_dir = os.path.dirname(os.path.realpath(__file__)) - save_loc = os.path.join(script_dir, "save.state") result = False if os.path.exists(save_loc): with open(save_loc, "rb") as file: @@ -267,16 +262,11 @@ def load(self): def save(self): """Saves current state to a file""" - # Get the directory of the current script - script_dir = os.path.dirname(os.path.realpath(__file__)) - save_loc = os.path.join(script_dir, "save.state") - with open(save_loc, "wb") as file: self.pyboy.save_state(file) def loop_until_stopped(self, threshold=1): """Simulates the gameboy bot""" - script_dir = os.path.dirname(os.path.realpath(__file__)) running = True previous_frame = None current_frame = None