-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
244 lines (208 loc) · 5.9 KB
/
index.js
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
const secrets = require("./local/secrets")
const puppeteer = require("puppeteer")
const OpenAI = require("openai")
const path = require("path")
const fs = require("fs")
const SCREEN_CAP_LIMIT = 10
const SCREEN_DIR = path.join(__dirname, "./screenshots")
const EMULATOR_URL = "http://127.0.0.1:8080/"
const ASSISTANT_ID = secrets.ASSISTANT_ID
const GPT_KEY = secrets.GPT_KEY
// Increase max cycles for long running games.
// 100 is just to get a taste of how it works.
const MAX_CYCLES = 100
// globals:
let cycles = 0
let openai
let browser
let page
main()
async function main() {
try {
await initAnalyzer()
await initEmulator()
await playGame()
} catch (error) {
console.error("Process failed:", error)
} finally {
if (browser) await browser.close()
console.log("Browser closed.")
}
}
async function initAnalyzer() {
openai = new OpenAI({ apiKey: GPT_KEY })
}
async function initEmulator() {
browser = await puppeteer.launch({ headless: false })
page = await browser.newPage()
console.log("loading emulator...")
await page.goto(EMULATOR_URL)
console.log("uploading rom...")
const romUploadSelector = 'input[type="file"]'
await page.waitForSelector(romUploadSelector)
const romPath = path.join(__dirname, "local", "rom.gb")
const fileInput = await page.$(romUploadSelector)
await fileInput.uploadFile(romPath)
console.log(`uploaded "${romPath}"`)
// wait for emulator
// to load the game rom:
await sleep(5000)
// todo: automate rom.state upload.
// manually upload rom.state file.
console.log("load rom.state manually!")
await sleep(5000)
for (let i = 0; i < 5; i++) {
console.log(`GPT will start in ${5 - i}s`)
await sleep(1000)
}
}
async function playGame() {
// enable local screenshot storage:
if (!fs.existsSync(SCREEN_DIR)) {
fs.mkdirSync(SCREEN_DIR)
console.log(`created screenshots directory: ${SCREEN_DIR}`)
}
while (cycles < MAX_CYCLES) {
cycles += 1
console.log(`--------- action #${cycles} ---------`)
// capture screenshot:
await clickControl("Pause")
const screenPath = path.join(SCREEN_DIR, `${Date.now()}.png`)
await page.screenshot({ path: screenPath })
clearScreenshots()
// analyze screenshot:
const result = await analyzeScreenshot()
console.log("result:", JSON.stringify(result, null, 2))
// execute action:
const action = actions[result.action]
if (action) {
// resume game and wait
// for action to complete:
await clickControl("Play")
await sleep(200)
action()
await sleep(3000)
} else {
throw new Error("failed to call action")
}
}
console.log(`reached MAX_CYCLES: ${MAX_CYCLES}`)
}
function lastScreenshot() {
const files = fs.readdirSync(SCREEN_DIR)
const sortedFiles = files.map((file) => ({
time: fs.statSync(path.join(SCREEN_DIR, file)).mtime.getTime(),
file,
}))
sortedFiles.sort((a, b) => b.time - a.time)
return sortedFiles.length > 0
? path.join(SCREEN_DIR, sortedFiles[0].file)
: null
}
async function analyzeScreenshot() {
const filePath = lastScreenshot()
const file = await openai.files.create({
file: fs.createReadStream(filePath),
purpose: "vision",
})
console.log("uploaded file:", file.filename)
await openai.beta.threads.messages.create(secrets.THREAD_ID, {
role: "user",
content: [
{
type: "image_file",
image_file: {
file_id: file.id,
detail: "auto",
},
},
],
})
console.log("analyzing screenshot...")
const run = await openai.beta.threads.runs.createAndPoll(secrets.THREAD_ID, {
assistant_id: ASSISTANT_ID,
})
let result
if (run.status === "completed") {
const messages = await openai.beta.threads.messages.list(run.thread_id, {
order: "desc",
limit: 1,
})
try {
const raw = messages.data[0].content[0].text.value
result = JSON.parse(raw)
} catch (err) {
throw new Error(`failed to parse response: ${err}`)
}
} else {
throw new Error(`analysis failed with status: ${run.status}`)
}
return result
}
function clearScreenshots() {
const files = fs.readdirSync(SCREEN_DIR).map((file) => {
const filePath = path.join(SCREEN_DIR, file)
const stats = fs.statSync(filePath)
return {
filePath,
createdAt: stats.birthtime,
}
})
if (files.length < SCREEN_CAP_LIMIT) {
// If there are fewer than limit,
// nothing needs to be done
return
}
files.sort((a, b) => a.createdAt - b.createdAt)
const deleteCount = Math.ceil(files.length / 2)
for (let i = 0; i < deleteCount; i++) {
fs.unlinkSync(files[i].filePath)
}
console.log(`${deleteCount} files deleted.`)
}
async function sleep(ms) {
return new Promise((res) => {
return setTimeout(res, ms)
})
}
async function press(key) {
await page.keyboard.down(key)
await sleep(200)
await page.keyboard.up(key)
}
async function clickControl(txt) {
const selector = `xpath/.//button[descendant::text()[contains(., '${txt}')]]`
const [btn] = await page.$$(selector)
if (!btn) throw new Error(`Failed to find button: ${txt}`)
await btn.click()
await sleep(200)
// set focus on game:
await clickCanvas()
}
async function clickCanvas() {
const canvasSelector = "canvas"
const canvas = await page.$(canvasSelector)
if (canvas) {
const canvasBox = await canvas.boundingBox()
if (canvasBox) {
await page.mouse.click(
canvasBox.x + canvasBox.width / 2,
canvasBox.y + canvasBox.height / 2
)
} else {
throw new Error("unable to retrieve canvas bounding box")
}
} else {
throw new Error("canvas not found")
}
}
const actions = {
a: () => press("z"),
b: () => press("x"),
up: () => press("ArrowUp"),
down: () => press("ArrowDown"),
left: () => press("ArrowLeft"),
right: () => press("ArrowRight"),
start: () => press("Enter"),
unknown: (msg) => console.log(`Analyzer failed: ${msg}`),
}