-
Notifications
You must be signed in to change notification settings - Fork 2
/
bot.py
331 lines (298 loc) · 11.3 KB
/
bot.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
"""
A bot that interacts with a Mastodon compatible API, plays gameboy games via a Poll
"""
import os
import random
import time
import toml
from mastodon import Mastodon
from requests.exceptions import RequestException
from gb import Gameboy
class Bot:
"""
A Mastodon-API Compatible bot that handles gameboy gameplay through polls
"""
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")
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"))
self.gameboy = Gameboy(rom, True)
def simulate(self):
"""Simulates gameboy actions by pressing random buttons, useful for testing"""
while True:
# print(self.gameboy.is_running())
# self.gameboy.random_button()
buttons = {
"a": self.gameboy.a,
"b": self.gameboy.b,
"start": self.gameboy.start,
"select": self.gameboy.select,
"up": self.gameboy.dpad_up,
"down": self.gameboy.dpad_down,
"right": self.gameboy.dpad_right,
"left": self.gameboy.dpad_left,
"random": self.gameboy.random_button,
"tick": "tick",
}
# self.gameboy.random_button()
print(buttons)
press = input("Button: ")
if press == "tick":
for _ in range(60):
self.gameboy.pyboy.tick()
else:
buttons[press]()
self.gameboy.pyboy.tick()
# time.sleep(1)
def random_button(self):
"""Chooses a random button and presses it on the gameboy"""
buttons = {
"a": self.gameboy.a,
"b": self.gameboy.b,
"start": self.gameboy.start,
"select": self.gameboy.select,
"up": self.gameboy.dpad_up,
"down": self.gameboy.dpad_down,
"right": self.gameboy.dpad_right,
"left": self.gameboy.dpad_left,
}
random_button = random.choice(list(buttons.keys()))
action = buttons[random_button]
action()
return random_button
def login(self):
"""Logs into the mastodon server using config credentials"""
server = self.mastodon_config.get("server")
print(f"Logging into {server}")
return Mastodon(
access_token=self.mastodon_config.get("access_token"), api_base_url=server
)
def post_poll(self, status, options, expires_in=60 * 60, reply_id=None):
"""Posts a poll to Mastodon compatible server"""
poll = self.mastodon.make_poll(
options, expires_in=expires_in, hide_totals=False
)
return self.mastodon.status_post(
status, in_reply_to_id=reply_id, language="en", poll=poll
)
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:
post_id, poll_id = content.split(",")
return post_id, poll_id
except FileNotFoundError:
return None, None
return None
def pin_posts(self, post_id, poll_id):
"""Pin posts to profile"""
self.mastodon.status_pin(poll_id)
self.mastodon.status_pin(post_id)
def unpin_posts(self, post_id, poll_id):
"""Unpin posts from profile"""
self.mastodon.status_unpin(post_id)
self.mastodon.status_unpin(poll_id)
def take_action(self, result):
"""Presses button on gameboy based on poll result"""
buttons = {
"up ⬆️": self.gameboy.dpad_up,
"down ⬇️": self.gameboy.dpad_down,
"right ➡️": self.gameboy.dpad_right,
"left ⬅️": self.gameboy.dpad_left,
"🅰": self.gameboy.a,
"🅱": self.gameboy.b,
"start": self.gameboy.start,
"select": self.gameboy.select,
}
print(buttons)
# Perform the corresponding action
if result.lower() in buttons:
action = buttons[result.lower()]
action()
else:
print(f"No action defined for '{result}'.")
def retry_mastodon_call(self, func, *args, retries=5, interval=10, **kwargs):
"""Continuously retries mastodon call, useful for servers with timeout issues"""
for _ in range(retries):
try:
return func(*args, **kwargs)
except Exception as e:
print(f"Failure to execute {func.__name__}: {e}")
time.sleep(interval)
return False # Failed to execute
def run(self):
"""
Runs the main gameplay, reads mastodon poll result, takes action, generates new posts
"""
self.gameboy.load()
post_id, poll_id = self.read_ids()
top_result = None
if post_id:
try:
self.unpin_posts(post_id, poll_id)
except BaseException:
time.sleep(30)
self.unpin_posts(post_id, poll_id)
poll_status = self.mastodon.status(poll_id)
poll_results = poll_status.poll["options"]
max_result = max(poll_results, key=lambda x: x["votes_count"])
if max_result["votes_count"] == 0:
button = self.random_button()
top_result = f"Random (no votes, chose {button})"
else:
top_result = max_result["title"]
self.take_action(top_result)
frames = self.gameboy.loop_until_stopped()
result = False
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()
alt_text = 'Screenshot of ' + self.gameboy_config.get('title', 'a Game Boy game.')
media = self.retry_mastodon_call(
self.mastodon.media_post,
retries=5,
interval=10,
media_file=image,
description=alt_text
)
media_ids = []
# Probably add a check here if generating a gif is enabled (so we don't
# have to generate one every single hour?)
try:
previous_frames = self.gameboy.get_recent_frames("screenshots",
25,
self.gameboy_config['gif_outline']
)
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 BaseException as e:
print(f"ERROR {e}")
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],
)
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=[
"Up ⬆️",
"Down ⬇️",
"Right ➡️ ",
"Left ⬅️",
"🅰",
"🅱",
"Start",
"Select",
],
expires_in=poll_duration*60,
reply_id=post["id"],
)
self.retry_mastodon_call(
self.pin_posts,
retries=5,
interval=10,
post_id=post["id"],
poll_id=poll["id"],
)
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="#Pokemon #FediPlaysPokemon",
media_ids=[gif["id"]],
in_reply_to_id=poll["id"],
)
self.save_ids(post["id"], poll["id"])
# Save game state
self.gameboy.save()
def test(self):
"""Method used for testing"""
self.gameboy.load()
self.gameboy.get_recent_frames("screenshots", 25)
# self.gameboy.build_gif("gif_images")
while True:
inp = input("Action: ")
buttons = {
"up": self.gameboy.dpad_up,
"down": self.gameboy.dpad_down,
"right": self.gameboy.dpad_right,
"left": self.gameboy.dpad_left,
"a": self.gameboy.a,
"b": self.gameboy.b,
"start": self.gameboy.start,
"select": self.gameboy.select,
}
# Perform the corresponding action
if inp.lower() in buttons:
action = buttons[inp.lower()]
# self.gameboy.tick()
action()
frames = self.gameboy.loop_until_stopped()
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}'.")
self.gameboy.save()
# self.gameboy.build_gif("gif_images")
# self.take_action(inp)
# self.gameboy.tick(300)
if __name__ == "__main__":
bot = Bot()
# bot.test()
bot.run()
# for i in range(2):
# bot.run()
# time.sleep(60)
# bot.simulate()