-
Notifications
You must be signed in to change notification settings - Fork 0
/
lambda_function.py
185 lines (160 loc) · 7.4 KB
/
lambda_function.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
import json
from nacl.signing import VerifyKey
from nacl.exceptions import BadSignatureError
from pyboy import PyBoy
from base64 import b64decode, b64encode
from PIL import Image
from pyboy import WindowEvent
import boto3
import io
import random
import string
import binascii
import os
PUBLIC_KEY = '61a5bac59316241719607f9730308266f868817002d714a5c988d4b24a7c25f1'
PING_PONG = {"type": 1}
RESPONSE_TYPES = {
"PONG": 1,
"ACK_NO_SOURCE": 2,
"MESSAGE_NO_SOURCE": 3,
"MESSAGE_WITH_SOURCE": 4,
"ACK_WITH_SOURCE": 5
}
def verify_signature(event):
raw_body = event.get("body")
auth_sig = event['headers'].get('x-signature-ed25519')
auth_ts = event['headers'].get('x-signature-timestamp')
message = auth_ts.encode() + raw_body.encode()
verify_key = VerifyKey(bytes.fromhex(PUBLIC_KEY))
verify_key.verify(message, bytes.fromhex(auth_sig)) # raises an error if unequal
def get_random_string(length):
result_str = ''.join(random.choice(string.ascii_letters) for i in range(length))
return result_str
def ping_pong(body):
if body.get("type") == 1:
return True
return False
def lambda_handler(event, context):
print(f"event {event}") # debug print
if 'data' in event:
if event['data'] == 'PING':
return {
"statusCode": 200,
"headers": {},
"body": "PONG",
"isBase64Encoded": False
}
# verify the signature
try:
verify_signature(event)
except Exception as e:
raise Exception(f"[UNAUTHORIZED] Invalid request signature: {e}")
# check if message is a ping
body = json.loads(event.get('body'))
if ping_pong(body):
return PING_PONG
dynamodb = boto3.client('dynamodb')
response = dynamodb.get_item(TableName='pokemon_yellow', Key={'guild_id':{'N': body['guild_id']}})
print(f"response {response}")
pyboy = PyBoy('/opt/yellow.gbc', disable_renderer=True)
f = open('/tmp/ram.state', 'wb')
if 'Item' in response:
print('Found save state')
f.write(b64decode(response['Item']['state']['B']))
f.close()
state = open('/tmp/ram.state', "rb")
pyboy.load_state(state)
state.close()
pyboy.set_emulation_speed(0)
if 'data' in body and 'custom_id' in body['data']:
command = body['data']['custom_id']
if command == "UP":
pyboy.send_input(WindowEvent.PRESS_ARROW_UP)
for x in range(14):
pyboy.tick()
pyboy.send_input(WindowEvent.RELEASE_ARROW_UP)
elif command == "DOWN":
pyboy.send_input(WindowEvent.PRESS_ARROW_DOWN)
for x in range(15):
pyboy.tick()
pyboy.send_input(WindowEvent.RELEASE_ARROW_DOWN)
elif command == "LEFT":
pyboy.send_input(WindowEvent.PRESS_ARROW_LEFT)
for x in range(14):
pyboy.tick()
pyboy.send_input(WindowEvent.RELEASE_ARROW_LEFT)
elif command == "RIGHT":
pyboy.send_input(WindowEvent.PRESS_ARROW_RIGHT)
for x in range(15):
pyboy.tick()
pyboy.send_input(WindowEvent.RELEASE_ARROW_RIGHT)
elif command == "A":
pyboy.send_input(WindowEvent.PRESS_BUTTON_A)
for x in range(14):
pyboy.tick()
pyboy.send_input(WindowEvent.RELEASE_BUTTON_A)
elif command == "B":
pyboy.send_input(WindowEvent.PRESS_BUTTON_B)
for x in range(15):
pyboy.tick()
pyboy.send_input(WindowEvent.RELEASE_BUTTON_B)
elif command == "START":
pyboy.send_input(WindowEvent.PRESS_BUTTON_START)
for x in range(14):
pyboy.tick()
pyboy.send_input(WindowEvent.RELEASE_BUTTON_START)
elif command == "SELECT":
pyboy.send_input(WindowEvent.PRESS_BUTTON_SELECT)
for x in range(15):
pyboy.tick()
pyboy.send_input(WindowEvent.RELEASE_BUTTON_SELECT)
for x in range(60*5):
pyboy.tick()
state = io.BytesIO()
state.seek(0)
pyboy.save_state(state)
pyboy.stop(save=False)
dynamodb.put_item(TableName='pokemon_yellow', Item={'guild_id': { 'N': body['guild_id'] }, 'state': { 'B': b64encode(state.getvalue()).decode() }})
s3 = boto3.client('s3')
name = body['guild_id'] + get_random_string(12)
pyboy.screen_image().resize((480,432), resample=Image.NEAREST).save("/tmp/" + name + ".png", format="png")
with open("/tmp/" + name + ".png", mode='rb') as file:
fileContent = file.read()
boundary = binascii.hexlify(os.urandom(16)).decode('ascii')
body = json.dumps({
"type": RESPONSE_TYPES['MESSAGE_WITH_SOURCE'],
"data": {
"tts": False,
"attachments": [ {'id': 0, 'filename': "screenshot.png" } ],
#"embeds": [ { "image": {"url": "attachment://screenshot.png", "width": 480, "height": 432} } ],
"components": [ {"type": 1, "components": [
{"type": 2, "custom_id": "UP", "style": 1, "emoji": { "id": "904722142430654474", "name": ":pokemon_up:", "animated": False}},
{"type": 2, "custom_id": "DOWN", "style": 1, "emoji": { "id": "904724131591897098", "name": ":pokemon_dowm:", "animated": False}},
{"type": 2, "custom_id": "LEFT", "style": 1, "emoji": { "id": "904724142199300116", "name": ":pokemon_left:", "animated": False}},
{"type": 2, "custom_id": "RIGHT", "style": 1, "emoji": { "id": "904724155931459604", "name": ":pokemon_right:", "animated": False}},
{"type": 2, "custom_id": "REFRESH", "style": 1, "emoji": { "id": "904731380037091358", "name": ":pokemon_refresh:", "animated": False}}
] },
{"type": 1, "components": [
{"type": 2, "custom_id": "A", "style": 1, "emoji": { "id": "904731161891332126", "name": ":pokemon_a:", "animated": False}},
{"type": 2, "custom_id": "B", "style": 1, "emoji": { "id": "904731279357014067", "name": ":pokemon_b:", "animated": False}},
{"type": 2, "custom_id": "START", "style": 1, "emoji": { "id": "904741277717909516", "name": ":pokemon_start:", "animated": False}},
{"type": 2, "custom_id": "SELECT", "style": 1, "emoji": { "id": "904741289499693107", "name": ":pokemon_select:", "animated": False}}
] }],
"allowed_mentions": []
}
})
response = "--" + boundary + '\r\n'
response += 'Content-Disposition: form-data; name="payload_json"\nContent-Type: application/json\r\n\r\n'
response += body + '\r\n'
response += "--" + boundary + '\r\n'
response += 'Content-Disposition: form-data; name="files[0]"; filename="screenshot.png\r\nContent-Type: image/png\r\nContent-Transfer-Encoding: base64\r\n\r\n'
response += b64encode(fileContent).decode() + '\r\n'
response += "--" + boundary + '--'
return {
"statusCode": 200,
"headers": {
"Content-Type": "multipart/form-data; boundary=" + boundary
},
"body": response,
"isBase64Encoded": False
}