Skip to content

Commit

Permalink
Long-delayed local updates
Browse files Browse the repository at this point in the history
  • Loading branch information
TomCasavant committed May 2, 2024
1 parent 2c97f2d commit 1961b80
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 11 deletions.
23 changes: 16 additions & 7 deletions bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -154,27 +154,34 @@ def run(self):

frames = self.gameboy.loop_until_stopped()
result = False
if frames > 71:
if frames >= 70:
result = self.gameboy.build_gif("gif_images")
else:
gif_dir = os.path.join(self.script_dir, "gif_images")
self.gameboy.empty_directory(gif_dir)

image = self.gameboy.screenshot()
media = self.retry_mastodon_call(self.mastodon.media_post, retries=5, interval=10, media_file=image, description='Screenshot of Pokemon Gold')
media_ids = []
try: # Probably add a check here if generating a gif is enabled (so we don't have to generate one every single hour?)
previous_frames = self.gameboy.get_recent_frames("screenshots", 25)
previous_media = self.retry_mastodon_call(self.mastodon.media_post, retries=5, interval=10, media_file=previous_frames, description="Video of the previous 45 frames")
media_ids = [media['id'], previous_media['id']]
except:
media_ids = [media['id']]
#try:
# media = self.mastodon.media_post(image, description='Screenshot of pokemon gold')
#except:
# time.sleep(45)
# media = self.mastodon.media_post(image, description='Screenshot of pokemon gold')
#time.sleep(50)
post = self.retry_mastodon_call(self.mastodon.status_post, retries=5, interval=10, status=f"Previous Action: {top_result}\n\n#pokemon #gameboy #nintendo", media_ids=[media['id']])
post = self.retry_mastodon_call(self.mastodon.status_post, retries=5, interval=10, status=f"Previous Action: {top_result}\n\n#pokemon #gameboy #nintendo #FediPlaysPokemon", media_ids=[media_ids])
#try:
# post = self.mastodon.status_post(f"Previous Action: {top_result}\n\n#pokemon #gameboy #nintendo", media_ids=[media['id']])
#except:
# time.sleep(30)
# post = self.mastodon.status_post(f"Previous Action: {top_result}\n\n#pokemon #gamebody #nintendo", media_ids=[media['id']])
poll = self.retry_mastodon_call(self.post_poll, retries=5, interval=10, status="Vote on the next action:", options=["Up ⬆️", "Down ⬇️", "Right ➡️ ", "Left ⬅️", "🅰", "🅱", "Start", "Select"], reply_id=post['id'] )
poll = self.retry_mastodon_call(self.post_poll, retries=5, interval=10, status="Vote on the next action:\n\n#FediPlaysPokemon", options=["Up ⬆️", "Down ⬇️", "Right ➡️ ", "Left ⬅️", "🅰", "🅱", "Start", "Select"], reply_id=post['id'] )

#ry:
# poll = self.post_poll("Vote on the next action:", ["Up ⬆️", "Down ⬇️", "Right ➡️ ", "Left ⬅️", "🅰", "🅱", "Start", "Select"], reply_id=post['id'])
Expand All @@ -190,9 +197,10 @@ def run(self):
# self.pin_posts(post['id'], poll['id'])

#result = self.gameboy.build_gif("gif_images")
result = False
if result:
gif = self.retry_mastodon_call(self.mastodon.media_post, retries=5, interval=10, media_file=result, description='Video of pokemon gold movement')
self.retry_mastodon_call(self.mastodon.status_post, retries=10, interval=10, status="Action Clip", media_ids=[gif['id']], in_reply_to_id=poll['id'])
self.retry_mastodon_call(self.mastodon.status_post, retries=10, interval=10, status="#Pokemon #FediPlaysPokemon", media_ids=[gif['id']], in_reply_to_id=poll['id'])

self.save_ids(post['id'], poll['id'])

Expand All @@ -201,8 +209,9 @@ def run(self):

def test(self):
self.gameboy.load()
self.gameboy.get_recent_frames('screenshots', 25)
#self.gameboy.build_gif("gif_images")
while True:
'''while True:
inp = input("Action: ")
buttons = {
"up": self.gameboy.dpad_up,
Expand All @@ -220,7 +229,7 @@ def test(self):
#self.gameboy.tick()
action()
frames = self.gameboy.loop_until_stopped()
if frames > 71:
if frames > 51:
self.gameboy.build_gif("gif_images")
else:
gif_dir = os.path.join(self.script_dir, "gif_images")
Expand All @@ -230,7 +239,7 @@ def test(self):
self.gameboy.save()
#self.gameboy.build_gif("gif_images")
#self.take_action(inp)
#self.gameboy.tick(300)
#self.gameboy.tick(300)'''

if __name__ == '__main__':
bot = Bot()
Expand Down
26 changes: 22 additions & 4 deletions gb.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,12 +47,29 @@ def compare_frames(self, frame1, frame2):
print(f"Pixels: {changed_pixels} {percent}%")
return percent

def get_recent_frames(self, directory, num_frames=100):
script_dir = os.path.dirname(os.path.realpath(__file__))
screenshot_dir = os.path.join(script_dir, directory)
image_files = [os.path.join(screenshot_dir, i) for i in os.listdir(screenshot_dir)] # Probably should replace this with heap (especially since there are so many image files)
image_files.sort(key=os.path.getmtime)
latest = image_files[-(num_frames):]

count = 0
for image in latest:
print(image)
count+=1
shutil.copy(image, os.path.join(script_dir, 'tmp', f"{count}.png"))

self.build_gif(os.path.join(script_dir, 'tmp'), fps=5, output_name="test.mp4")
self.empty_directory(os.path.join(script_dir, 'tmp'))
return os.path.join(script_dir, 'test.mp4')

def empty_directory(self, directory):
image_files = [i for i in os.listdir(directory) if os.path.isfile(os.path.join(directory, i))]
for img in image_files:
os.remove(os.path.join(directory, img))

def build_gif(self, image_path, delete=True, fps=120):
def build_gif(self, image_path, delete=True, fps=120, output_name="action.mp4"):
script_dir = os.path.dirname(os.path.realpath(__file__)) # Get the directory of the current script
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 All @@ -64,7 +81,8 @@ def build_gif(self, image_path, delete=True, fps=120):
#diffs = []
for file in image_files:
#diffs.append(self.compare_frames(image1, Image.open(os.path.join(gif_dir, file)).convert('L')))
gameboy_outline = Image.open('gameboy.png').convert("RGB")

gameboy_outline = Image.open(os.path.join(script_dir, 'gameboy.png')).convert("RGB")
img = Image.open(os.path.join(gif_dir, file)).convert("RGB")
img = img.resize((181, 163))
combined = gameboy_outline.copy()
Expand All @@ -86,7 +104,7 @@ def build_gif(self, image_path, delete=True, fps=120):
#print(f"Duration {duration}")
#frames = [images[0]]*350 + images
frames = images
save_path = os.path.join(script_dir, "action.mp4")
save_path = os.path.join(script_dir, output_name)
clip = ImageSequenceClip(frames, fps=fps)
clip.write_videofile(save_path, codec='libx264')
if delete:
Expand Down Expand Up @@ -215,7 +233,7 @@ def loop_until_stopped(self, threshold=1):
no_movement += 1
else:
no_movement = 0
if no_movement > 5:
if no_movement > 3:
running = False
if count > 1000: # Shouldn't have lasted this long, something has gone wrong
print("Error")
Expand Down

0 comments on commit 1961b80

Please sign in to comment.