Skip to content

Commit

Permalink
Merge pull request #38 from lukemurphy147/main
Browse files Browse the repository at this point in the history
Cleaned up redundant variables
  • Loading branch information
TomCasavant authored Jun 21, 2024
2 parents 45f30df + 6414ae0 commit b0e1a26
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 25 deletions.
18 changes: 6 additions & 12 deletions bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -21,17 +25,15 @@ 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", {})
self.gameboy_config = self.config.get("gameboy", {})

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):
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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}'.")
Expand Down
16 changes: 3 additions & 13 deletions gb.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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))
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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:
Expand All @@ -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
Expand Down

0 comments on commit b0e1a26

Please sign in to comment.