-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmain.js
223 lines (210 loc) · 8.54 KB
/
main.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
var robot = require("robotjs");
var Ngocr = require("ng-ocr");
var puppeteer = require("puppeteer");
var tracker = require("./colortracker"); // not tracking.js but based on it
var sharp = require("sharp");
var imageSizeOf = require("image-size");
var b;
var p;
var gameColors = {
"0,178,225": "Blue Tank",
"241,78,84": "Red Tank",
"0,225,110": "Green Tank",
"191,127,245": "Purple Tank",
"255,232,105": "Square & Arena Closer",
"252,118,119": "Triangle",
"118,141,252": "Pentagon",
"137,255,104": "Square, Green",
"249,115,224": "Guardian",
"192,192,192": "Fallen Bosses",
"255,232,106": "Summoner"
};
var g_v = {
size: robot.getScreenSize(),
centerx: Math.round(robot.getScreenSize().width / 2),
centery: Math.round(robot.getScreenSize().height / 2),
spinradius: robot.getScreenSize().height / 6
};
var g_f = {
getMouseLocForSpin: function (degree) {
function toRadians(angle) {
return angle * (Math.PI / 180);
}
var x = g_v.centerx + g_v.spinradius * Math.cos(toRadians(degree));
var y = g_v.centery + g_v.spinradius * Math.sin(toRadians(degree));
return [x, y];
}
};
function start() {
console.log("Args provided: ", process.argv.join(" "));
// of course requires a up-to-date node.js for 'await'
puppeteer.launch({
headless: false,
args: ["--start-maximized", "--disable-infobars"],
appMode: true,
timeout: 0
}).then(function (browser) {
console.log("Chrome opened.");
b = browser;
browser.newPage().then(function (page) {
p = page;
page.on("console", function (msg) {
for (let i = 0; i < msg.args().length; ++i) {
console.log("Page console: " + i + " : " + msg.args()[i]);
}
});
page.goto("http://diep.io/").then(function () {
console.log("Diep.io loaded.");
page.waitForSelector("#canvas").then(async function () {
console.log("Canvas ready");
var buttonx;
var buttony;
var canvaswidth = await page.evaluate(function () {
return document.getElementById("canvas").getBoundingClientRect().width;
});
var canvasheight = await page.evaluate(function () {
return document.getElementById("canvas").getBoundingClientRect().height;
});
var centerX = Math.round(canvaswidth / 2);
var baseY = 38;
var xIncrement = 66;
var secondRowYIncrease = 23;
switch (process.argv[2]) {
// tricky code to get the location of the button to
// change the game mode because it isn't a dom element.
// relies heavily on the fact that the page layout
// hopefully wont change.
case "FFA":
buttonx = centerX - Math.round(1.5 * xIncrement);
buttony = baseY;
break;
case "Survival":
buttonx = centerX - Math.round(0.5 * xIncrement);
buttony = baseY;
break;
case "2 Teams":
buttonx = centerX + Math.round(0.5 * xIncrement);
buttony = baseY;
break;
case "4 Teams":
buttonx = centerX + Math.round(1.5 * xIncrement);
buttony = baseY;
break;
case "Domination":
buttonx = centerX - Math.round(1.5 * xIncrement);
buttony = baseY + secondRowYIncrease;
break;
case "Tag":
buttonx = centerX - Math.round(0.5 * xIncrement);
buttony = baseY + secondRowYIncrease;
break;
case "Maze":
buttonx = centerX + Math.round(0.5 * xIncrement);
buttony = baseY + secondRowYIncrease;
break;
case "Sandbox":
buttonx = centerX + Math.round(1.5 * xIncrement);
buttony = baseY + secondRowYIncrease;
break;
default:
// 4 Teams
buttonx = centerX + Math.round(1.5 * xIncrement);
buttony = baseY;
break;
}
// console.log("Button location: " + buttonx + "x, " + buttony + "y.");
await page.mouse.click(buttonx, buttony, {
button: "left"
});
await page.waitForSelector("#textInput");
var nametouse;
if (!process.argv[3] || process.argv[3].length > 15) {
nametouse = "TheGreatRambler";
} else {
nametouse = process.argv[3];
}
await page.waitFor(3000);
await page.type("#textInput", nametouse);
await page.keyboard.press("Enter");
await waitUntilDiepReady();
startPlaying();
});
}).catch(function (err) {
console.error("Website timed out: ", err);
});
});
});
}
function waitUntilDiepReady() {
return new Promise(function (resolve, reject) {
p.waitForFunction(function () {
return document.getElementById("textInputContainer").style.display === "none";
}).then(function () {
resolve();
});
});
}
function startPlaying() {
console.log("Started");
var repeatid = setInterval(function() {
p.evaluate(function() {
return document.getElementById("a").style.display === "block";
}).then(function(isdead) {
if (isdead) {
// you died!
clearInterval(repeatid);
console.log("Bot died");
p.keyboard.press("Enter").then(function () {
p.waitFor(1000).then(function() {
p.keyboard.press("Enter").then(function () {
waitUntilDiepReady().then(function () {
startPlaying();
});
});
});
});
}
})
}, 200);
Object.keys(gameColors).forEach(function(color) {
tracker.registerColor(color, function(r, g, b, a) {
var colorvalues = color.split(",");
if (r === Number(colorvalues[0]) && g === Number(colorvalues[1]) && b === Number(colorvalues[2])) {
return true;
} else {
return false;
}
});
});
var objecttracker = new tracker(Object.keys(gameColors));
// used PixelPicker
setInterval(function () {
console.log("Frame reading started");
console.time("screenshot");
p.screenshot({
//fullPage: true,
type: "png"
}).then(function (imagebuffer) {
console.timeEnd("screenshot");
var resizefactor = 4;
var imagesize = imageSizeOf(imagebuffer);
var newWidth = Math.round(imagesize.width / resizefactor);
var newHeight = Math.round(imagesize.height / resizefactor);
console.time("resize");
sharp(imagebuffer).resize(newWidth, newHeight).raw().toBuffer().then(function(pixels) {
console.timeEnd("resize");
console.time("tracking");
objecttracker.track(pixels, newWidth, newHeight).then(function(recs) {
console.timeEnd("tracking");
if (recs.length !== 0) {
recs.forEach(function(rec) {
var gameobject = gameColors[rec.color];
console.log(gameobject);
});
}
});
});
});
}, 2000);
};
start();