-
Notifications
You must be signed in to change notification settings - Fork 27
/
index.js
269 lines (207 loc) · 6.23 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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
const ElTetris = require('./eltetris');
const averageColour = require('average-colour');
const robot = require('robotjs');
const { spawn } = require('child_process');
robot.setXDisplayName(':99');
robot.setKeyboardDelay(0);
const URL = 'https://tetr.io';
// the position and sizes of where the next pieces will be
const NEXT_PIECE_POSITION_X = 479;
const NEXT_PIECE_POSITION_Y = 173;
const NEXT_PIECE_WIDTH = 69;
const NEXT_PIECE_HEIGHT = 42;
const NEXT_PIECE_COUNT = 5;
const JOIN_X = 400;
const JOIN_Y = 430;
const SOLO_X = 300;
const SOLO_Y = 420;
const ZEN_X = 300;
const ZEN_Y = 450;
const START_X = 592;
const START_Y = 324;
// average colours of the blocks
const PIECE_COLOURS = {
T: '#b151a7',
L: '#ba6d3e',
O: '#b9a13c',
J: '#7969c6',
I: '#3db388',
Z: '#bb3e44',
S: '#82ac3b'
};
const PIECE_INDEXES = {
I: 0,
T: 1,
O: 2,
J: 3,
L: 4,
S: 5,
Z: 6
};
// where each orientation will be placed
const ORIENTATION_COLUMNS = {
I: [4, 6],
T: [4, 5, 4, 4],
O: [5],
L: [4, 5, 4, 4],
S: [4, 5],
J: [4, 5, 4, 4],
Z: [4, 5]
};
const nearestColour = require('nearest-color').from(PIECE_COLOURS);
const sleep = (time) => new Promise((resolve) => setTimeout(resolve, time));
function getLuminence(colour) {
const r = Number.parseInt(colour.slice(0, 2), 16);
const g = Number.parseInt(colour.slice(2, 4), 16);
const b = Number.parseInt(colour.slice(4, 6), 16);
return 0.2126 * r + 0.7152 * g + 0.0722 * b;
}
class TetrioBot {
constructor() {
this.currentPiece = undefined;
this.currentColumn = 4; // 5 if O
this.eltetris = new ElTetris(10, 20);
this.moves = 0;
}
async launchChromium() {
spawn('chromium', ['--start-fullscreen', URL]);
//spawn('feh', ['-F', __dirname + '/screenshot-0.png']);
await sleep(5000);
}
// click through the menus to start zen mode
async startGame() {
// join as guest
robot.moveMouseSmooth(JOIN_X, JOIN_Y);
robot.mouseClick();
await sleep(1200);
// click solo
robot.moveMouseSmooth(SOLO_X, SOLO_Y);
robot.mouseClick();
await sleep(1200);
// click zen
robot.moveMouseSmooth(ZEN_X, ZEN_Y);
robot.mouseClick();
await sleep(1200);
// start zen
robot.moveMouse(START_X, START_Y);
robot.mouseClick();
// wait until we make it to the countdown
await sleep(6200);
this.currentPieces = this.getNextPieces();
this.getNextPiece();
// wait until countdown is finished*/
await sleep(1800);
}
// return the piece letters from the right side (in the order they'll drop
// in)
getNextPieces() {
const nextPiecesBitmap = robot.screen.capture(
NEXT_PIECE_POSITION_X,
NEXT_PIECE_POSITION_Y,
NEXT_PIECE_WIDTH,
NEXT_PIECE_HEIGHT * NEXT_PIECE_COUNT
);
const pieces = [];
for (let i = 0; i < NEXT_PIECE_COUNT; i += 1) {
let x = 0;
let y = i * NEXT_PIECE_HEIGHT;
const distance = Math.sqrt(
Math.pow(NEXT_PIECE_WIDTH / 2, 2) +
Math.pow(NEXT_PIECE_HEIGHT / 2, 2)
);
const colours = [];
for (let j = 0; j < Math.floor(distance); j += 1) {
const colour = nextPiecesBitmap.colorAt(x + j, y + j);
if (getLuminence(colour) <= 90) {
continue;
}
colours.push(`#${colour}`);
}
console.log(
nearestColour(averageColour(colours)).name,
averageColour(colours)
);
pieces.push(nearestColour(averageColour(colours)).name);
}
//spawn('import', ['-w', 'root', `./screenshot-${pieces.join(',')}.png`]);
return pieces;
}
moveLeft() {
console.log('left');
robot.keyTap('left');
}
moveRight() {
console.log('right');
robot.keyTap('right');
}
dropPiece() {
console.log('drop');
robot.keyTap('space');
}
changeToOrientation(orientation) {
console.log('change to orientation', orientation);
for (let i = 0; i < orientation; i += 1) {
robot.keyTap('up');
}
this.currentColumn =
ORIENTATION_COLUMNS[this.currentPiece][orientation];
}
moveToColumn(column) {
//console.log('our column', this.currentColumn);
//console.log('goal column', column);
const deltaColumns = column - this.currentColumn + 1;
if (deltaColumns < 0) {
for (let i = 0; i < Math.abs(deltaColumns); i += 1) {
this.moveLeft();
}
} else if (deltaColumns > 0) {
for (let i = 0; i < Math.abs(deltaColumns); i += 1) {
this.moveRight();
}
}
}
// call this after we place a piece
getNextPiece() {
this.currentPiece = this.currentPieces.shift();
if (!this.currentPieces.length) {
this.currentPieces = this.getNextPieces();
this.getNextPiece();
}
this.currentColumn = this.currentPiece === 'O' ? 5 : 4;
}
playMove() {
const {
orientationIndex,
orientation,
column
} = this.eltetris.pickMove(PIECE_INDEXES[this.currentPiece]);
this.eltetris.playMove(this.eltetris.board, orientation, column);
console.log(
'move #',
this.moves,
'played',
this.currentPiece,
'at column',
column
);
ElTetris.drawBoard(this.eltetris.board);
this.changeToOrientation(+orientationIndex);
this.moveToColumn(column);
this.getNextPiece();
this.dropPiece();
//spawn('import', ['-w', 'root', `./screenshot-${this.moves}.png`]);
this.moves += 1;
}
async init() {
await this.launchChromium();
//this.getNextPieces();
await this.startGame();
while (true) {
this.playMove();
}
}
}
(async () => {
const tetrioBot = new TetrioBot();
await tetrioBot.init();
})();