This repository was archived by the owner on Nov 1, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathmain.js
More file actions
143 lines (131 loc) · 3.91 KB
/
main.js
File metadata and controls
143 lines (131 loc) · 3.91 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
console.log('脚本启动')
auto()
sleep(1000)
console.log('请求截图')
if (!requestScreenCapture(true)) {
toast('请求截图失败')
console.error('请求截图失败')
exit()
}
toastLog('请求截图成功')
const SDCARD = files.getSdcardPath()
const BASE = ''
// const BASE = SDCARD + '/Pictures/'
requiresApi(24)
if (device.width / device.height > 1) {
toast('请将模拟器调为竖屏模式,推荐分辨率720*1280')
console.error('请将模拟器调为竖屏模式(手机模式),推荐分辨率720*1280,当前', device.width, '*', device.height)
exit()
}
function capture() {
let cap = captureScreen()
images.save(cap, SDCARD + '/Pictures/cap.png')
return cap
}
function findAndClick(config) {
if (typeof config === 'string' || Array.isArray(config)) {
config = { match: config }
}
if (typeof config !== 'object') {
toast('配置文件错误')
console.error('配置文件错误')
exit()
}
console.log('步骤:', config)
if (!config.hasOwnProperty('match')) {
toast('配置文件错误:未能找到步骤的匹配项')
console.error('配置文件错误:未能找到步骤的匹配项', match)
exit()
}
let match = config.match
match = typeof match == 'string' ? [ match ] : match
if (!Array.isArray(match)) {
toast('配置文件错误:步骤匹配项无法被解析')
console.error('配置文件错误:步骤匹配项无法被解析:', match)
exit()
}
// standardlizing
let delay = config.hasOwnProperty('delay') ? config.delay : 1000
for (let i in match) {
if (typeof match[i] === 'string') {
match[i] = { name: match[i] }
}
if (typeof match[i] !== 'object') {
toast('配置文件错误:步骤匹配项的第i=', i, '项无法被解析')
console.error('配置文件错误:步骤匹配项的第i=', i, '项无法被解析:', match[i])
exit()
}
if (!match[i].hasOwnProperty('then')) {
match[i].then = 1
}
if (!match[i].hasOwnProperty('delay')) {
match[i].delay = delay
}
}
let imgs = {}
for (var i in match) {
let fileName = match[i].name
imgs[i] = images.read(BASE + fileName + '.png')
if (imgs[i] === null) {
toast(fileName + '无法被读取')
console.error(fileName, '无法被读取')
exit()
}
}
console.log('寻找', match, ',点击延时', delay)
while (true) {
let cap = capture()
let i, p, img
for (i in imgs) {
img = imgs[i]
p = findImage(cap, img)
if (p) {
break
}
}
if (p) {
let { x, y } = p
x += Math.round(Math.random() * img.getWidth())
y += Math.round(Math.random() * img.getHeight())
toastLog('找到(' + match[i].name + ')了,等待' + (Math.round(delay / 100) / 10) + '秒点击(' + x + ',' + y + ')')
sleep(delay)
click(x, y)
return match[i].then
}
toast('等待画面中寻找:' + match.map(item => item.name).join(','))
console.verbose(match, '点击事件尚未找到')
sleep(3000)
}
}
function runConfig(config) {
let pointer = 0
toastLog('脚本开始,请手动打开第一个画面“' + config[0] + '”')
while (true) {
pointer += findAndClick(config[pointer])
if (pointer >= config.length || pointer < 0) {
pointer = 0
}
sleep(3000)
}
}
try {
let config
if (!files.exists(BASE + 'config.json')) {
toast('没有找到配置文件' + BASE + 'config.json')
console.error('没有找到配置文件' + BASE + 'config.json')
exit()
} else {
config = JSON.parse(files.read(BASE + 'config.json'))
toastLog('加载配置文件:' + BASE + 'config.json')
}
if (config.length === 0) {
toast('配置文件有误,没有记录任何脚本流程')
console.error('配置文件有误,没有记录任何脚本流程')
exit()
}
runConfig(config)
} catch (e) {
toast('程序即将关闭,相关信息见日志')
console.error(e)
exit()
}