Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Configuration Options #33

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 33 additions & 14 deletions bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ def __init__(self, config_path="config.toml"):
self.config = toml.load(config_file)
self.mastodon_config = self.config.get("mastodon", {})
self.gameboy_config = self.config.get("gameboy", {})
self.debug_config = self.config.get("debug", {})

self.mastodon = self.login()
print(self.gameboy_config.get("rom"))
Expand Down Expand Up @@ -165,7 +166,7 @@ def run(self):
"""
Runs the main gameplay, reads mastodon poll result, takes action, generates new posts
"""
self.gameboy.load()
self.gameboy.load(self.gameboy_config.get("save_state_path", "save.state"))
post_id, poll_id = self.read_ids()
top_result = None

Expand Down Expand Up @@ -228,29 +229,47 @@ def run(self):
retries=5,
interval=10,
status=(
f"Previous Action: {top_result}\n\n"
"#pokemon #gameboy #nintendo #FediPlaysPokemon"
f"Previous Action: {top_result}\n\n" +
self.mastodon_config.get("screenshot_status", "")
),
media_ids=[media_ids],
)

poll_duration = self.mastodon_config.get('poll_duration', 60)

poll = self.retry_mastodon_call(
self.post_poll,
retries=5,
interval=10,
status="Vote on the next action:\n\n#FediPlaysPokemon",
options=[
poll_options = [
"Up ⬆️",
"Down ⬇️",
"Right ➡️ ",
"Left ⬅️",
"🅰",
"🅱",
"Start",
"Select",
]

if self.debug_config.get("poll_options", "full") == "directions":
poll_options = [
"Up ⬆️",
"Down ⬇️",
"Right ➡️ ",
"Left ⬅️",
"Left ⬅️"
]
elif self.debug_config.get("poll_options", "full") == "buttons":
poll_options = [
"🅰",
"🅱",
"Start",
"Select",
],
]

poll = self.retry_mastodon_call(
self.post_poll,
retries=5,
interval=10,
status="Vote on the next action:\n\n" +
self.mastodon_config.get("poll_status", ""),
options=poll_options,
expires_in=poll_duration*60,
reply_id=post["id"],
)
Expand Down Expand Up @@ -283,11 +302,11 @@ def run(self):
self.save_ids(post["id"], poll["id"])

# Save game state
self.gameboy.save()
self.gameboy.save(self.gameboy_config.get("save_state_path", "save.state"))

def test(self):
"""Method used for testing"""
self.gameboy.load()
self.gameboy.load(self.gameboy_config.get("save_state_path", "save.state"))
self.gameboy.get_recent_frames("screenshots", 25)
# self.gameboy.build_gif("gif_images")
while True:
Expand Down Expand Up @@ -315,7 +334,7 @@ def test(self):
self.gameboy.empty_directory(gif_dir)
else:
print(f"No action defined for '{inp}'.")
self.gameboy.save()
self.gameboy.save(self.gameboy_config.get("save_state_path", "save.state"))
# self.gameboy.build_gif("gif_images")
# self.take_action(inp)
# self.gameboy.tick(300)
Expand Down
12 changes: 12 additions & 0 deletions config.toml.example
Original file line number Diff line number Diff line change
@@ -1,9 +1,21 @@
[mastodon]
server = "https://tomkahe.com"
access_token = ""
screenshot_status = "#pokemon #gameboy #nintendo #FediPlaysPokemon"
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I haven't gotten a chance to test this PR yet, but will this let me plug in the previous action at all, like:

screenshot_status = "$PREV_ACTION\n\n#pokemon #gameboy #nintendo #FediPlaysPokemon"

poll_status = "#FediPlaysPokemon"
poll_duration = 60

[gameboy]
rom = "Pokemon - Gold Version.gbc"
title = "Pokémon Gold"
save_state_path = "save.state"
gif_outline="gameboy.png"
# Modes:
# "generic": Default mode, no game-specific messages
# "gs": Adds messages with player info for Pokémon Gold/Silver Versions. May not be compatible with Crystal.
mode = "generic"

[debug]
# Limit what buttons can be pressed. Accepts "full" (default), "directions", or "buttons".
# Mostly used to test on servers with only 4 options allowed per poll.
poll_options = "full"
8 changes: 4 additions & 4 deletions gb.py
Original file line number Diff line number Diff line change
Expand Up @@ -251,11 +251,11 @@ def random_button(self):
)
button()

def load(self):
def load(self, state):
"""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")
save_loc = os.path.join(script_dir, state)
result = False
if os.path.exists(save_loc):
with open(save_loc, "rb") as file:
Expand All @@ -265,11 +265,11 @@ def load(self):
print("Save state does not exist")
return result

def save(self):
def save(self, state):
"""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")
save_loc = os.path.join(script_dir, state)

with open(save_loc, "wb") as file:
self.pyboy.save_state(file)
Expand Down