-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsingle-bot.js
85 lines (73 loc) · 2.35 KB
/
single-bot.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
import BotManager from './BotManager.js';
import ActiveCode from './ActiveCode.js';
import ArenaManager from './ArenaManager.js';
import fetch from 'node-fetch';
import HighlightManager from './HighlightManager.js';
import 'dotenv/config';
async function waitForArenaConnection(arena) {
return new Promise((resolve, reject) => {
arena.once('connected', resolve);
});
}
async function waitForBotConnection(botManager) {
return new Promise((resolve, reject) => {
botManager.once('connect', resolve);
});
}
async function waitForArenaLeave(arena) {
return new Promise((resolve, reject) => {
arena.once('gone', id => {
console.log(`🎉 ${id} has left the arena!`);
resolve();
});
});
}
// From https://stackoverflow.com/a/49959557
const keypress = async () => {
process.stdin.setRawMode(true);
return new Promise(resolve => process.stdin.once('data', () => {
process.stdin.setRawMode(false);
resolve()
}));
}
if(process.argv.length < 3) {
console.log('Provide ID');
process.exit(1);
}
const fileID = process.argv[2];
console.log(`ID: "${fileID}"`);
(async () => {
const codeResponse = await fetch(`https://autosumo.techchrism.me/code/download/${fileID}.js`);
if(!codeResponse.ok) {
console.log(`Status code for ${fileID} is ${codeResponse.status}`);
process.exit(1);
}
const code = await codeResponse.text();
console.group('Running code:');
console.log(code);
console.groupEnd();
console.log('');
console.log('⌛ Waiting for bot and arena connection...');
const botManager = new BotManager(8090);
const arena = new ArenaManager();
const highlightManager = new HighlightManager();
await Promise.all([
waitForArenaConnection(arena),
waitForBotConnection(botManager),
highlightManager.waitForReady(process.env['ROBOT_SERVER_ID'])
]);
const activeCode = new ActiveCode(code, botManager.bots[0].botID, botManager, highlightManager);
console.log('👌 Ready! Press any key to execute');
await keypress();
console.log('➡️ Running!');
await Promise.any([
keypress(),
activeCode.start(),
waitForArenaLeave(arena)
]);
activeCode.killTarget();
console.log('Done!');
setTimeout(() => {
process.exit(0);
}, 1000);
})();