-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathindex.html
More file actions
542 lines (479 loc) · 18.2 KB
/
index.html
File metadata and controls
542 lines (479 loc) · 18.2 KB
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
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>跳一跳半自动</title>
<style>
* {
font-size: 14px;
}
body {
padding-left: 20px;
}
.btn {
border: none;
background: #128ff3;
color: #fff;
padding: 4px 12px;
font-size: 14px;
border-radius: 4px;
outline: none;
cursor: pointer;
}
.left {
width: 500px;
margin-right: 40px;
}
.left, .right {
float: left
}
#screenshot {
display: block;
}
#screenshot-wrap {
position: relative;
display: inline-block;
}
#screenshot-wrap canvas {
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
}
#error-tips, #operation {
color: #ff0000
}
#coefficient-range-wrap {
margin-top: 30px;
}
#coefficient-range-title {
/* margin-bottom: 40px; */
font-weight: bold
}
#coefficient-range-group {
display: inline-block;
width: 400px;
position: relative;
margin: 40px 0 0 22px ;
}
#coefficient-range-group.disabled{
opacity: .7
}
#coefficient-range{
width: 100%;
}
#coefficient-range[disabled]{
cursor: not-allowed
}
#coefficient-range-tips {
position: absolute;
left: 0;
top: -25px;
background: #128ff3;
color: #fff;
padding: 2px 7px;
font-size: 12px;
border-radius: 4px;
transform: translateX(-50%);
transition: left 200ms ease-out
}
#coefficient-range-tips:after {
content: '';
display: block;
width: 6px;
height: 6px;
position: absolute;
background: #128ff3;
left: 50%;
transform: rotate(45deg);
bottom: -2.5px;
margin-left: -2.5px;
}
#select-point-demo {
position: relative;
color: #128ff3;
cursor: pointer;
}
#select-point-demo img{
position: absolute;
left: 120%;
opacity: 0;
transition: all 200ms ease;
visibility: hidden;
}
#select-point-demo:hover img {
left: 102%;
opacity: 1;
visibility: visible;
}
</style>
</head>
<body>
<p id="loading">loading...</p>
<p id="device-size"></p>
<p id="error-tips"></p>
<div class="left">
<div id="step1" style="display: none;">
<div id="coefficient-range-wrap">
<p id="coefficient-range-title"></p>
<div id="switch-group">
<label>
手动调整系数(越大跳的越远,小则近):
<input type="checkbox" id="switch"/>
</label>
</div>
<div id="coefficient-range-group">
<input id="coefficient-range" type="range" min="0" max="3" step="0.001" value="0"/>
<span id="coefficient-range-tips">0.000</span>
</div>
</div>
<div id="step1-control">
<p>
1. 先打开微信,进入跳一跳小游戏,点击开始游戏
</p>
<p>
2. 在右边截图中依次点击选取(单击)棋子小人的底座中心点,和下一个积木的顶面中心点(单击),并点击试跳按钮进行试跳,看时间系数是否正确<br>
<span id="select-point-demo">
怎么选?
<img src="./screenshot/demo.gif" alt="">
</span>
</p>
<button id="jump-test-btn" class="btn">试跳</button>
<p>
3. 如果能正确跳到积木中心,说明系数正确,可开始刷分,如果没跳正中心,需要手动调整系数重新选择起点和终点试跳,确认系数正确后,点击开始刷分进入下一步
</p>
<button id="step1-next-step" class="btn">开始刷分</button>
</div>
</div>
<div id="step2" style="display:none;">
<p>> 在右边截图中依次点击选取棋子小人的底座中心点和下一个积木的顶面中心点,选取完后会将会自动跳一步,下一步继续选即可</p>
<p>> 跳一步等屏幕截图刷新后再跳哦~</p>
<p>> 若选第一个点手残了,请按鼠标右键或者点击 -> <button class="btn" id="screenshot-reset-btn">重新选</button></p>
<p>> 一步两步,一步两步,一步一步似爪牙,似魔鬼的步伐...ㄟ(▔,▔)ㄏ</p>
</div>
</div>
<!--<p><button class="btn" id="screenshot-btn">刷新截屏</button></p>-->
<div class="right" style="display:none;">
<div id="screenshot-wrap">
<p id="screenshot-loading">获取截图中...</p>
<img id="screenshot" src="" alt="">
<canvas id="line-canvas"></canvas>
<canvas id="point-canvas"></canvas>
</div>
<br>
</div>
<script src="./js/zepto.min.js"></script>
<script src="./js/coefficient-config.js"></script>
<script>
let SCREEN_SCALE = 1 / 3 // 屏幕缩放比例
// 入口
setTimeout(() => {
getWebSocket()
.then(initPage)
.then(initStepOne)
.catch(handlerError)
}, 0)
let common = (() => {
return {
hideLoading() {
$('#loading').hide()
},
pullScreenshot(wneedLoop) {
return ws.send({type: 'screenshot', data: {needLoop: needLoop}})
},
makePromiseFn(fn) {
let promise
return function() {
return promise || (promise = new Promise((resolve, reject) => {
fn(resolve, reject)
}))
}
},
calcDistance(x1, y1, x2, y2) {
return Math.sqrt(Math.pow(x1 - x2, 2) + Math.pow(y1 - y2, 2))
},
clearCanvas(ctx) {
ctx.clearRect(0, 0, Number.MAX_SAFE_INTEGER, Number.MAX_SAFE_INTEGER)
},
drawCircle(ctx, x, y, radius, style) {
ctx.save()
ctx.fillStyle = style
ctx.beginPath()
ctx.arc(x, y, radius, 0, Math.PI * 2)
ctx.fill()
ctx.restore()
},
drawLine(ctx, x1, y1, x2, y2, lineWidth, style) {
ctx.save()
ctx.lineWidth = lineWidth || 1
ctx.strokeStyle = style
ctx.beginPath()
ctx.moveTo(x1, y1)
ctx.lineTo(x2, y2)
ctx.closePath()
ctx.stroke()
ctx.restore()
}
}
})()
let getWebSocket = common.makePromiseFn((resolve, reject) => {
let ws = new WebSocket('ws://127.0.0.1:8899')
let event = $(ws)
ws.on = event.on.bind(event)
ws.one = event.one.bind(event)
ws.trigger = event.trigger.bind(event)
ws.off = event.off.bind(event)
ws.on('open', () => {
let ws_send = ws.send
ws.send = function(param) {
return new Promise((resolve, reject) => {
ws.one(param.type, (e, data) => {
resolve(data)
})
ws.one('deviceError', (e, error) => {
reject(error)
})
ws_send.call(ws, JSON.stringify(param))
})
}
resolve(ws)
})
ws.on('message', (event) => {
let message = JSON.parse(event.data)
console.log('onmessage:', message)
ws.trigger(message.type, [message.data])
})
ws.on('error', () => {
reject('websocket 连接失败,请确认服务端是否开启')
})
})
// 实时监听错误回调
function handlerError(error) {
common.hideLoading()
if (/device not found|no devices/.test(error)) {
$('#error-tips').html('请先用usb连接Android手机,并开启USB调试')
} else {
$('#error-tips').html(`错误:${error || 'websocket 连接失败,请确认服务端是否开启'}`)
}
}
function initPage(ws) {
// 设备没连接时promise不会被resolve或reject
return ws.send({type: 'size'}).then((size) => {
common.hideLoading()
$('#device-size').html(`屏幕尺寸:${size.width} x ${size.height}`)
return size
})
}
function initStepOne(size) {
$('#step1').show()
$('.right').show()
let coefficient = coefficientConfig[`${size.width}x${size.height}`]
let $title = $('#coefficient-range-title')
let $switch = $('#switch')
let $jumpTestBtn = $('#jump-test-btn')
let $nextStep = $('#step1-next-step')
if (coefficient) {
$title.html(`已根据你的机型设置最佳长按时间系数如下,无需手动调整`)
$switch.prop('checked', false)
rangeComp.disabled()
} else {
coefficient = coefficientConfig['default']
$title.html(`暂无法匹配你的机型最佳长按时间系数,已设置默认系数如下,可根据试跳结果手动调整`)
$switch.prop('checked', true)
}
// 设置系数初始值
rangeComp.setRange(coefficient)
// 初始化截图模块
screenshotComp.init(size)
$switch.on('change', function() {
if (this.checked) {
rangeComp.enable()
} else {
rangeComp.disabled()
}
})
$jumpTestBtn.on('click', function() {
screenshotComp.longTap().catch((errMsg) => {
alert(errMsg)
})
})
$nextStep.on('click', function() {
$('#step1-control').hide()
initStepTwo()
})
return coefficient
}
function initStepTwo() {
$('#step2').show()
screenshotComp.setAutoJump(true)
$('#screenshot-reset-btn').on('click', () => {
screenshotComp.resetStatus()
})
}
// 截图操作
let screenshotComp = (() => {
let $screenshot = $('#screenshot')
let $pointCanvas = $('#point-canvas')
let $lineCanvas = $('#line-canvas')
let pointCtx = $pointCanvas[0].getContext('2d')
let lineCtx = $lineCanvas[0].getContext('2d')
let points = [] // 截图上单击选中的点
let autoJump = false
let triggerTap = false
let pointRadius = 4
let drawCount = 0
let resetStatus = () => {
common.clearCanvas(pointCtx)
common.clearCanvas(lineCtx)
drawCount = 0
points = []
}
let initEvent = () => {
let onClick = (e) => {
let x = e.offsetX, y = e.offsetY
// 截图上需要触发单击事件,跟其他操作互斥
if (triggerTap) {
return instance.tap(x, y)
}
if (drawCount < 2) {
common.drawCircle(pointCtx, x, y, pointRadius, '#128ff3')
points.push({x, y})
if (autoJump && drawCount == 1) {
// longTap函数会根据points的两点距离计算实际按压时长
instance.longTap()
}
drawCount++
} else if(!autoJump) {
// reset
resetStatus()
return onClick(e)
}
}
$pointCanvas.on('click', onClick)
.on('mousemove', e => {
if (drawCount == 1) {
let x2 = e.offsetX, y2 = e.offsetY;
common.clearCanvas(lineCtx)
common.drawLine(lineCtx, points[0].x, points[0].y, x2, y2, 2, '#128ff3')
common.drawCircle(lineCtx, x2, y2, pointRadius, '#128ff3')
}
}).on('mouseup', e => {
// 鼠标右键重置
if (e.button == 2) {
resetStatus()
}
})
}
// 禁用鼠标右键菜单
let handlerContextMenu = () => {
let disabled = e => e.preventDefault()
$pointCanvas.on('mouseenter', e => {
$(document).on('contextmenu', disabled)
}).on('mouseleave', e => {
$(document).off('contextmenu', disabled)
})
}
let instance = {
init(size) {
$screenshot.width(size.width * SCREEN_SCALE)
$pointCanvas.prop('width',size.width * SCREEN_SCALE)
.prop('height', size.height * SCREEN_SCALE)
$lineCanvas.prop('width',size.width * SCREEN_SCALE)
.prop('height', size.height * SCREEN_SCALE)
this.pullScreenshot()
},
pullScreenshot() {
getWebSocket().then(ws => {
ws.one('screenshot', (e, path) => {
$('#screenshot-loading').hide()
initEvent()
handlerContextMenu()
}).on('screenshot', (e, path) => {
$screenshot.attr('src', path)
})
ws.send({type: 'screenshot', data: {needLoop: true}})
})
},
tap(x, y) {
getWebSocket().then( ws => {
ws.send({type: 'tap', data: {x, y}}).then(() => {
})
})
},
longTap() {
if (points.length < 2) {
return Promise.reject('先在截图选取两个中心点哦~')
}
return getWebSocket().then( ws => {
let x1 = points[0].x / SCREEN_SCALE
let y1 = points[0].y / SCREEN_SCALE
let x2 = points[1].x / SCREEN_SCALE
let y2 = points[1].y / SCREEN_SCALE
let distance = common.calcDistance(x1, y1, x2, y2)
let pressTime = Math.round(distance * rangeComp.getRange()) // 长按时长
let x = parseInt(Math.random() * 200)
let y = parseInt(500 + Math.random() * 500)
return ws.send({
type: 'longtap',
data: {time: pressTime, x, y}}
).then(() => {
resetStatus()
})
})
},
setAutoJump(bool) {
autoJump = bool
},
setTriggerTap(bool) {
triggerTap = bool
},
resetStatus() {
resetStatus()
}
}
return instance
})()
// 拖动条
let rangeComp = (() => {
// 时间系数设置
let $rangeGroup = $('#coefficient-range-group')
let $range = $('#coefficient-range')
let $rangeTips = $('#coefficient-range-tips')
let onChange = function() {}
$range.on('input', function() {
let value = (+this.value).toFixed(3)
let left = value / this.max * $range.width()
$rangeTips.html(value)
.css('left', left)
onChange(value)
})
return {
setRange(value) {
$range.val(value).trigger('input')
return this
},
getRange() {
return +$range.val()
},
disabled() {
$rangeGroup.addClass('disabled')
$range.prop('disabled', true)
return this
},
enable() {
$rangeGroup.removeClass('disabled')
$range.prop('disabled', false)
return this
},
onChange(callback) {
onChange = callback
return this
}
}
})()
</script>
</body>
</html>