-
Notifications
You must be signed in to change notification settings - Fork 1
/
botRunner.js
103 lines (102 loc) · 3.43 KB
/
botRunner.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
const BotCode = `
const Bot = (() => {
let lastMessage = ''
let resolve = () => { }
self.onmessage = (m) => {
if (m.data.type != 'keyPass') {
lastMessage = m.data
resolve()
}
}
async function runCommand(command) {
self.postMessage(command)
await new Promise(r => resolve = r)
return lastMessage
}
return {
async setSelfMode(mode) {
return await runCommand(['set_self_mode', mode])
},
async setOtherMode(dir, mode) {
return await runCommand(['set_other_mode', dir, mode])
},
async moveSelf(dir) {
return await runCommand(['move_self', dir])
},
async setSelfMem(mem) {
return await runCommand(['set_self_mem', mem])
},
async setOtherMem(dir, mem) {
return await runCommand(['set_other_mem', dir, mem])
},
async getSelfInfo() {
return await runCommand(['get_self_info'])
},
async look(dir) {
return await runCommand(['look', dir])
},
async harvest(dir, slot) {
return await runCommand(['harvest', dir, slot])
},
async moveItems(fromSlot, toSlot, maxCount = Infinity) {
return await runCommand(['move_items', fromSlot, toSlot, maxCount])
},
async takeItems(dir, fromSlot, toSlot, maxCount = Infinity) {
return await runCommand(['take_items', dir, fromSlot, toSlot, maxCount])
},
async giveItems(dir, fromSlot, toSlot, maxCount = Infinity) {
return await runCommand(['give_items', dir, fromSlot, toSlot, maxCount])
},
async movePlayerControl(target) {
return await runCommand(['move_player_control', target])
},
async inUnderPlayerControl() {
return await runCommand(['is_under_player_control'])
},
async log(text, color) {
return await runCommand(['log',text,color])
},
async setSelfProgram(path) {
return await runCommand(['set_self_program', path])
},
async setOtherProgram(dir, path) {
return await runCommand(['set_other_program', dir, path])
},
async burnCoal(fromSlot, maxBurn=Infinity) {
return await runCommand(['burn_coal', fromSlot, maxBurn])
},
async giveEnergy(dir, maxEnergy) {
return await runCommand(['give_energy', dir, maxEnergy])
},
async craft(recipeName, toSlot) {
return await runCommand(['craft', recipeName, toSlot])
}
}
})()
`
self.addEventListener('message', async (m) => {
const message = m.data
if (message.type == 'keyPass') {
await new Promise(async (resolve, reject) => {
try {
await (new Function(`
return (async () => {
${BotCode};
${message.code}
})()
`))()
resolve()
} catch (err) {
console.error('Error running code:', message.code, err)
reject(err)
}
})
.then(() => {
self.postMessage([message.key])
})
.catch((err) => {
console.error('Error running code:', message.code, err)
self.postMessage([message.key])
})
}
})