-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
497 lines (476 loc) · 12.6 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
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
const path = require('path')
const fs = require('fs')
const getPixels = require('get-pixels')
const gm = require('gm')
const adb = require('node-adb')
const _ = require('lodash')
const isDebug = process.env.DEBUG - 0
const deviceID = 'e3c7a0ac'
let imageName = 'temp.png'
const testPath = './test/'
const faildPath = './failed/'
const debugPath = './debug/'
const scoreUpHeight = 300
let isFindEnd = false
// 可能要调节 start
const initSecondDot = {
x: 789,
y: 850
}
const swipePos = {
x1: 320,
y1: 410,
x2: 330,
y2: 420
}
const pressCoefficient = 1.468
// 可能要调节 end
function inRange(r, g, b) {
if (
(r >= 50 && r <= 60) &&
(g >= 52 && g <= 62) &&
(b >= 95 && b <= 105)
//
// (r >= 53 && r <= 58) &&
// (g >= 58 && g <= 60) &&
// (b >= 98 && b <= 102)
) {
return true
}
return false
}
function checkRGB(r, g, b, x, y, pixels) {
const num = 18
if (inRange(r, g, b)) {
for (const len = x + num; x < len; x++) {
const nextR = pixels.get(x, y, 0)
const nextG = pixels.get(x, y, 1)
const nextB = pixels.get(x, y, 2)
// if (x >= 328 && x<= 328 + num && y === 1129) {
// console.log('r,g,b', nextR, nextG, nextB, ' x, y', x, y, 'inRange', inRange(nextR, nextG, nextB))
// }
if (x !== len - 1) {
// 前面17个不在范围内就false
if (!inRange(nextR, nextG, nextB)) {
return false
}
} else {
// 第18个不在范围内就true
if (!inRange(nextR, nextG, nextB)) {
return true
}
}
}
return false
}
return false
}
function getCurPos(cb, name) {
let res = path.resolve(imageName)
if (name) {
res = path.join(__dirname, name)
imageName = name
}
getPixels(res, function(err, pixels) {
if (err) {
return
}
const width = pixels.shape[0]
const height = pixels.shape[1]
let isFind = false, arr = [], y = 0
// 从距底部180开始
for (let j = height - 180; j >= 0; j--) {
arr = []
// 左上角是原点
for (let i = 0; i < width; i++) {
const r = pixels.get(i, j, 0)
const g = pixels.get(i, j, 1)
const b = pixels.get(i, j, 2)
if (inRange(r, g, b)) {
arr.push(i)
y = j
}
// if (checkRGB(r, g, b, i, j, pixels)) {
// console.log('找到起始点:', i + 8, j + 1 - 20)
// isFind = true
// cb && cb(pixels, {
// x: i + 8,
// y: j + 1 - 20
// })
// return
// }
}
// 12 22
if (arr.length >= 10 && arr.length <= 30) {
isFind = true
const minX = Math.min.apply(Math, arr)
const maxX = Math.max.apply(Math, arr)
cb && cb(pixels, {
x: (minX + maxX) / 2,
y: y + 1 - 20
})
console.log('找到起始点:', (minX + maxX) / 2, y + 1 - 20)
break
}
}
if (!isFind) {
console.log('没有找到起始点')
copyFile(imageName, faildPath)
cb && cb(null)
}
})
}
function findCenter(pixels, first, cb) {
console.log('使用块的上顶点和左顶点找中间点')
const width = pixels.shape[0]
const xArr = [], xArr2 = [], obj = {}, shadowObj = {}, rgb = [], diff = 5, shadowDiff = 5
let centerTop = 0
for (let j = scoreUpHeight; j <= first.y; j++) {
for (let i = 0; i <= width; i++) {
const r = pixels.get(i, j, 0)
const g = pixels.get(i, j, 1)
const b = pixels.get(i, j, 2)
if (i === 0) {
obj.r = r
obj.g = g
obj.b = b
}
// 中点的x值不会在第一个点x附近
if (i >= first.x - 30 && i <= first.x + 30) {
continue
}
// 在差值外就push
if (
r < obj.r - diff || r > obj.r + diff ||
g < obj.g - diff || g > obj.g + diff ||
b < obj.b - diff || b > obj.b + diff
) {
xArr.push(i)
centerTop = j
rgb.push({r, g, b})
}
}
if (xArr.length > 0) {
break
}
}
const centerX = _.sum(xArr) / xArr.length
// console.log('xArr', xArr)
// console.log(rgb, obj)
console.log('顶点坐标:', centerX, centerTop)
let maxJ = 0, i = 0, firstX = 0
if (centerX >= first.x) { // 右边
// 暂且最大块的宽度是徐记士多520
firstX = Math.floor(Math.max(centerX - 520 / 2, first.x + 30))
maxJ = Math.min((centerX - firstX) * Math.sqrt(3) / 3 + centerTop, first.y)
console.log('初始i, 初始j, 最大i, 最大j:', firstX, centerTop, centerX, maxJ)
} else {
maxJ = Math.min(centerX * Math.sqrt(3) / 3 + centerTop, first.y)
console.log('初始i, 初始j, 最大i, 最大j:', i, centerTop, centerX, maxJ)
}
drawRect({x: firstX | i, y: centerTop}, {x: centerX, y: maxJ}, function() {
for (let j = centerTop; j <= maxJ; j++) {
let isFirst = false
let maxX = centerX - Math.sqrt(3) / 3 * (j - centerTop)
// if (j === 913) {
// console.log(maxX)
// }
if (centerX >= first.x) { // 右边
i = firstX
} else {
i = 0
}
for (; i <= maxX; i++) {
const r = pixels.get(i, j, 0)
const g = pixels.get(i, j, 1)
const b = pixels.get(i, j, 2)
if (!isFirst) {
obj.r = r
obj.g = g
obj.b = b
isFirst = true
shadowObj.r = Math.floor(r - r * 0.3)
shadowObj.g = Math.floor(g - g * 0.3)
shadowObj.b = Math.floor(b - b * 0.3)
}
// 跟影子色相近的色就continue
if (
r >= (shadowObj.r - shadowDiff) && r <= (shadowObj.r + shadowDiff) &&
g >= (shadowObj.g - shadowDiff) && g <= (shadowObj.g + shadowDiff) &&
b >= (shadowObj.b - shadowDiff) && b <= (shadowObj.b + shadowDiff)
) {
continue
}
// 在差值外就push
if (
(r < obj.r - diff || r > obj.r + diff) &&
(g < obj.g - diff || g > obj.g + diff) &&
(b < obj.b - diff || b > obj.b + diff)
) {
xArr2.push({
x: i,
y: j,
r_g_b: `${r}_${g}_${b}`
})
break
}
}
}
const groupRGB = _.groupBy(xArr2, function(o) {
return o.r_g_b
})
const groupX = _.groupBy(xArr2, function(o) {
return o.x + o.r_g_b
})
const arr = []
for (const i in groupRGB) {
arr.push(groupRGB[i])
}
const arrX = []
for (const i in groupX) {
arrX.push(groupX[i])
}
const sortedArr = _.sortBy(arr, function(o) {
return o.length
})
// fix #16
const sortedArrX = _.sortBy(arrX, function(o) {
return o.length
})
const lastItem = sortedArr[sortedArr.length - 1]
const lastItem2 = sortedArrX[sortedArrX.length - 1]
const centerLeftArr = lastItem.map(item => item.x)
const centerLeftArr2 = lastItem2.map(item => item.x)
let centerLeft = Math.min.apply(Math, centerLeftArr)
if (lastItem2.length >= 10) {
centerLeft = centerLeftArr2[0]
}
if (isDebug === 1) {
console.log(lastItem, lastItem2)
console.log('centerLeft:', centerLeft)
}
// 值是tan 30°, math.sqrt(3) / 3
const centerY = centerTop + Math.sqrt(3) / 3 * Math.abs(centerX - centerLeft)
isFindEnd = true
console.log('找到结束点:', centerX, centerY)
cb && cb({
x: centerX,
y: centerY
})
})
}
function getNextPos(pixels, cb, isFirst, first) {
if (!isFirst) {
const width = pixels.shape[0]
// const height = pixels.shape[1]
const circleW = 38, circleH = 22
let xArr = [], arr = []
for (let i = 0; i <= width; i++) {
for (let j = scoreUpHeight; j <= first.y; j++) {
const r = pixels.get(i, j, 0)
const g = pixels.get(i, j, 1)
const b = pixels.get(i, j, 2)
if (r === 245 && g === 245 && b === 245) {
arr.push({
x: i,
y: j
})
}
}
}
const groupX = _.groupBy(arr, function(item) {
return item.x
})
const groupY = _.groupBy(arr, function(item) {
return item.y
})
for (const i in groupY) {
if (groupY[i].length >= 37 && groupY[i].length <= 39) {
xArr = xArr.concat(groupY[i].map(item => item.x))
}
}
if (!xArr.length) {
findCenter(pixels, first, cb)
return
}
const minX = Math.min.apply(Math, xArr)
const maxX = Math.max.apply(Math, xArr)
const x = (minX + maxX) / 2
const yObjArr = groupX[Math.floor(x)] || groupX[Math.ceil(x)]
const yArr = yObjArr.map(item => item.y)
const minY = Math.min.apply(Math, yArr)
const maxY = Math.max.apply(Math, yArr)
const y = (minY + maxY) / 2
if (maxX - minX > circleW || maxY - minY > circleH) {
console.log('宽:', maxX - minX, '高:', maxY - minY)
console.log('找错结束点:', x, y)
copyFile(imageName, faildPath)
findCenter(pixels, first, cb)
} else {
if (x && y) {
isFindEnd = true
console.log('找到结束点:', x, y)
cb && cb({
x: x,
y: y
})
} else {
findCenter(pixels, first, cb)
}
}
} else {
if (!isFindEnd) {
console.log('用初始节点处理!')
cb && cb(initSecondDot)
} else {
copyFile(imageName, faildPath)
}
}
}
function calc(first, second) {
return Math.sqrt((first.x - second.x) * (first.x - second.x) + (first.y - second.y) * (first.y - second.y))
}
function drawRect(first, second, cb) {
// first 左上角的点
// second 右下角的点
gm(path.join(__dirname, imageName))
.fill()
.stroke('black', 1)
// .drawRectangle(first.x, first.y, second.x, second.y)
.drawPolygon([first.x, second.y], [first.x, first.y] ,[second.x, first.y])
.write(path.join(__dirname, debugPath + Date.now() + '_rect.png'), function (err) {
if (!err) {
cb && cb()
}
})
}
function drawLine(first, second, cb) {
gm(path.join(__dirname, imageName))
.stroke('red', 3)
.drawLine(first.x, first.y, second.x, second.y)
.write(path.join(__dirname, debugPath + Date.now() + '_line.png'), function (err) {
if (!err) {
cb && cb()
}
})
}
function jump(first, second, isRight) {
drawLine(first, second, async function() {
const distance = calc(first, second)
console.log('距离:', distance)
let pressTime = distance * (isRight ? pressCoefficient : 1.475)
pressTime = parseInt(pressTime)
pressTime = Math.max(pressTime, 240)
console.log('按的时间:', pressTime)
const destName = Date.now() + '_' + distance + '_' + pressTime
console.log('文件名:', destName)
console.log('\n')
if (isDebug) {
if (isDebug === 2) {
await sleep(1000)
const name = path.parse(imageName).name - 0
test(name + 1)
}
return
}
const randomNum = 56
adbExcute(['shell', 'input swipe', _.random(swipePos.x1 - randomNum , swipePos.x1 + randomNum), _.random(swipePos.y1 - randomNum , swipePos.y1 + randomNum), _.random(swipePos.x2 - randomNum , swipePos.x2 + randomNum), _.random(swipePos.y2 - randomNum , swipePos.y2 + randomNum), pressTime], async function() {
await sleep(_.random(3500, 5000))
main()
})
})
}
function adbExcute(shell, cb) {
adb({
deviceID: deviceID,
shell: shell
}, function(result) {
if (result === undefined || result && result.indexOf('pulled') !== -1) {
cb && cb(result)
}
})
}
function sleep(millis) {
return new Promise(resolve => setTimeout(resolve, millis))
}
/**
* [copyFile description]
* @param {[type]} fileName [拷贝的目标文件]
* @param {[type]} url [拷贝的目标文件夹]
* @param {[type]} destName [拷贝的目标文件名]
* @return {[type]} [description]
*/
function copyFile(fileName, url, destName) {
if (isDebug === 2) {
return
}
var sourceFile = path.join(__dirname, fileName)
var destPath = path.join(__dirname, url, destName || (Date.now() + fileName))
var readStream = fs.createReadStream(sourceFile)
var writeStream = fs.createWriteStream(destPath)
readStream.pipe(writeStream)
}
function main() {
adbExcute(['shell', 'screencap -p', '/sdcard/' + imageName], function() {
adbExcute(['pull', '/sdcard/' + imageName, '.'], function() {
copyFile(imageName, debugPath, Date.now() + imageName)
getCurPos(function(pixels, first) {
if (!first) {
return
}
getNextPos(pixels, function(second1) {
if (!second1) {
getNextPos(pixels, function(second2) {
jump(first, second2, second2.x > first.x)
}, true, first)
} else {
jump(first, second1, second1.x > first.x)
}
}, false, first)
})
})
})
}
function debug() {
getCurPos(function(pixels, first) {
if (!first) {
return
}
getNextPos(pixels, function(second1) {
if (!second1) {
getNextPos(pixels, function(second2) {
jump(first, second2, second2.x > first.x)
}, true, first)
} else {
jump(first, second1, second1.x > first.x)
}
}, false, first)
})
}
function test(i) {
getCurPos(function(pixels, first) {
if (!first) {
return
}
getNextPos(pixels, function(second1) {
if (!second1) {
getNextPos(pixels, function(second2) {
jump(first, second2, second2.x > first.x)
}, true, first)
} else {
jump(first, second1, second1.x > first.x)
}
}, false, first)
}, testPath + i + '.png')
}
switch(isDebug) {
case 0:
main()
break
case 1:
debug()
break
case 2:
test(101)
break
}