forked from GH6324/LazyStudy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dati.js
253 lines (232 loc) · 6.9 KB
/
dati.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
//先引用
importClass(android.database.sqlite.SQLiteDatabase);
importClass(android.media.MediaPlayer);
function searchNet(question) {
var ansNet = [];
//toastLog("开始网络搜题");
recents();
sleep(1500);
if (textContains("强国挑战").exists()) {
toastLog("正在切换小程序");
click("强国挑战");
} else {
toastLog("请先点击 切到搜题");
return ansNet;
}
sleep(500);
textContains("搜索").findOnce().parent().child(0).child(0).child(1).child(0).child(0).click();
sleep(800);
setText(question);
sleep(500);
//className("android.widget.EditText").findOnce().setText(question);
textContains("搜索").findOnce().click();
sleep(1000);
if (textContains("答案:").exists()) {
//toastLog(textContains("答案:").findOnce().text().substring(3));
textContains("答案:").find().forEach(item => {
ansNet.push(item.text().substring(3));
});
}
//sleep(300);
recents();
sleep(300);
click("学习强国");
// if (textContains("学习强国").exists()) {
// textContains("学习强国").findOnce().parent().click();
// }
//sleep(500);
return ansNet;
}
function drawfloaty(x, y) {
//floaty.closeAll();
var window = floaty.window(
<frame gravity="center">
<text id="text" text="✔" textColor="red" />
</frame>
);
window.setPosition(x, y - 50);
return window;
//sleep(2000);
//window.close();
}
function beep() {
var player = new MediaPlayer();
var path = files.cwd() + "/beep.mp3";
player.setDataSource(path);
player.setVolume(50, 50);
player.prepare();
player.start();
setTimeout(() => {
player.stop();
player.release();
}, 5000);
}
function searchTiku(keyw) {
//数据文件名
var dbName = "tiku.db";
//文件路径
var path = files.path(dbName);
//确保文件存在
if (!files.exists(path)) {
files.createWithDirs(path);
}
//创建或打开数据库
var db = SQLiteDatabase.openOrCreateDatabase(path, null);
query = "SELECT answer,wrongAnswer FROM tiku WHERE question LIKE '" + keyw + "%'"
//query="select * from tiku"
//db.execSQL(query);
var cursor = db.rawQuery(query, null);
cursor.moveToFirst();
//var toaststr = "共有" + cursor.getCount() + "行记录,答案是 :";
//找到记录
if (cursor.getCount()) {
//toast("找到答案");
//toaststr = toaststr + cursor.getString(0);
var ansTiku = cursor.getString(0);
cursor.close();
return ansTiku;
} else {
toastLog("题库中未找到: " + keyw);
cursor.close();
return "";
}
}
function insertOrUpdate(sqlstr) {
//数据文件名
var dbName = "tiku.db";
//文件路径
var path = files.path(dbName);
//确保文件存在
if (!files.exists(path)) {
files.createWithDirs(path);
}
//创建或打开数据库
var db = SQLiteDatabase.openOrCreateDatabase(path, null);
db.execSQL(sqlstr);
toastLog(sqlstr);
db.close();
}
function tiaoZhan() {
let failDo = false;
//提取题目
if (className("android.widget.ListView").exists()) {
var _timu = className("android.widget.ListView").findOnce().parent().child(0).desc();
} else {
//back();
toastLog("提取题目失败");
failDo = true;
beep();
return;
}
var chutiIndex = _timu.lastIndexOf("出题单位");
if (chutiIndex != -1) {
_timu = _timu.substring(0, chutiIndex - 2);
}
var timuOld = _timu;
_timu = _timu.replace(/\s/g, "");
//提取答案
var ansTimu = [];
if (className("android.widget.ListView").exists()) {
className("android.widget.ListView").findOne().children().forEach(child => {
var answer_q = child.child(0).child(1).desc();
ansTimu.push(answer_q);
});
} else {
//back();
toastLog("答案获取失败");
failDo = true;
beep();
return;
}
var ansTiku = searchTiku(_timu);
sleep(300);
var answer = "";
var ansFind = "";
//toastLog(findAnsRet);
//如果题库中有
if (ansTiku != "") {
//toast("ansTiku="+ansTiku);
answer = ansTiku;
} else {
//toast("进入");
//从题目中提取检索关键词
var reg = /[\u4e00-\u9fa5a-zA-Z\d]{4,}/;
var regTimu = reg.exec(timuOld);
toastLog("search:" + regTimu);
var ansNet = searchNet(regTimu); //一个数组
sleep(500);
//遍历题中的答案
toastLog("网络答案: " + ansNet);
for (let item of ansTimu) {
toastLog(item);
var indexFind = ansNet.indexOf(item);
if (indexFind != -1) {
ansFind = item;
break;
}
}
toastLog("匹配结果: " + ansFind);
if (ansFind != "") {
answer = ansFind;
} else {
//网络也没找到,那么随机咯
let randomIndex = random(0, ansTimu.length - 1);
answer = ansTimu[randomIndex];
beep();
//sleep(10*1000);
//return;
}
}
//开始点击
if (className("android.view.View").desc(answer).exists()) {
//RadioButton位置
var b = className("android.view.View").desc(answer).findOnce().parent().child(0).bounds();
var tipsWindow = drawfloaty(b.centerX(), b.centerY());
sleep(300);
//点击RadioButton
className("android.view.View").desc(answer).findOnce().parent().child(0).click();
sleep(300);
//floaty.closeAll();
tipsWindow.close();
} else {
//back();
toastLog("点击答案失败");
failDo = true;
beep();
//return;
}
sleep(1000);
//写库
if (!className("android.view.View").descContains("本次答对").exists()) {//如果答对
if (ansTiku == "") {
var sqlstr = "INSERT INTO tiku VALUES ('" + _timu + "','" + answer + "','')";
insertOrUpdate(sqlstr);
}
} else {
if (ansTiku != "" && !failDo) {
//删掉这条
toastLog("删除答案: " + ansTiku);
var sqlstr = "DELETE FROM tiku WHERE question LIKE '" + _timu + "'";
insertOrUpdate(sqlstr);
}
}
//sleep(1000);
}
function begin() {
while (true) {
//floaty.closeAll();
if (className("android.view.View").descContains("本次答对").exists()) {
beep();
//break;
//back();
}
if (className("android.view.View").desc("挑战答题").exists()) {
className("android.view.View").desc("挑战答题").click();
sleep(3000);
}
tiaoZhan();
sleep(1000);
//i++;
}
}
module.exports = begin;