This repository has been archived by the owner on Feb 18, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 230
/
dailyAnswer.js
381 lines (353 loc) · 11.7 KB
/
dailyAnswer.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
importClass(android.database.sqlite.SQLiteDatabase);
/**
* @description: 增加或更新数据库
* @param: sql
* @return: null
*/
function insertOrUpdate(sql) {
var dbName = "tiku.db";
var path = files.path(dbName);
if (!files.exists(path)) {
//files.createWithDirs(path);
toastLog("未找到题库!请将题库放置与js同一目录下");
}
var db = SQLiteDatabase.openOrCreateDatabase(path, null);
db.execSQL(sql);
db.close();
}
/**
* @description: 从数据库中搜索答案
* @param: question 问题
* @return: answer 答案
*/
function getAnswer(question, table_name) {
var dbName = "tiku.db";//题库文件名
var path = files.path(dbName);
var db = SQLiteDatabase.openOrCreateDatabase(path, null);
sql = "SELECT answer FROM " + table_name + " WHERE question LIKE '" + question + "%'"
var cursor = db.rawQuery(sql, null);
if (cursor.moveToFirst()) {
var answer = cursor.getString(0);
cursor.close();
return answer;
}
else {
console.error("题库中未找到答案");
cursor.close();
return '';
}
}
/**
* @description: 延时函数
* @param: seconds-延迟秒数
* @return: null
*/
function delay(seconds) {
sleep(1000 * seconds);//sleep函数参数单位为毫秒所以乘1000
}
/**
* @description: 获取填空题题目数组
* @param: null
* @return: questionArray
*/
function getFitbQuestion() {
var questionCollections = className("EditText").findOnce().parent().parent();
var questionArray = [];
var findBlank = false;
var blankCount = 0;
var blankNumStr = "";
var i = 0;
questionCollections.children().forEach(item => {
if (item.className() != "android.widget.EditText") {
if (item.text() != "") {//题目段
if (findBlank) {
blankNumStr = "|" + blankCount.toString();
questionArray.push(blankNumStr);
findBlank = false;
}
questionArray.push(item.text());
}
else {
findBlank = true;
blankCount = (className("EditText").findOnce(i).parent().childCount() - 1);
i++;
}
}
});
return questionArray;
}
/**
* @description: 获取选择题题目数组
* @param: null
* @return: questionArray
*/
function getChoiceQuestion() {
var questionCollections = className("ListView").findOnce().parent().child(1);
var questionArray = [];
questionArray.push(questionCollections.text());
return questionArray;
}
/**
* @description: 获取提示字符串
* @param: null
* @return: tipsStr
*/
function getTipsStr() {
var tipsStr = "";
while (tipsStr == "") {
if (text("查看提示").exists()) {
var seeTips = text("查看提示").findOnce();
seeTips.click();
delay(1);
click(device.width * 0.5, device.height * 0.41);
delay(1);
click(device.width * 0.5, device.height * 0.35);
} else {
console.error("未找到查看提示");
}
if (text("提示").exists()) {
var tipsLine = text("提示").findOnce().parent();
//获取提示内容
var tipsView = tipsLine.parent().child(1).child(0);
tipsStr = tipsView.text();
//关闭提示
tipsLine.child(1).click();
break;
}
delay(1);
}
return tipsStr;
}
/**
* @description: 从提示中获取填空题答案
* @param: timu, tipsStr
* @return: ansTips
*/
function getAnswerFromTips(timu, tipsStr) {
var ansTips = "";
for (var i = 1; i < timu.length - 1; i++) {
if (timu[i].charAt(0) == "|") {
var blankLen = timu[i].substring(1);
var indexKey = tipsStr.indexOf(timu[i + 1]);
var ansFind = tipsStr.substr(indexKey - blankLen, blankLen);
ansTips += ansFind;
}
}
return ansTips;
}
/**
* @description: 根据提示点击选择题选项
* @param: tipsStr
* @return: clickStr
*/
function clickByTips(tipsStr) {
var clickStr = "";
var isFind = false;
if (className("ListView").exists()) {
var listArray = className("ListView").findOne().children();
listArray.forEach(item => {
var ansStr = item.child(0).child(2).text();
if (tipsStr.indexOf(ansStr) >= 0) {
item.child(0).click();
clickStr += item.child(0).child(1).text().charAt(0);
isFind = true;
}
});
if (!isFind) { //没有找到 点击第一个
listArray[0].child(0).click();
clickStr += listArray[0].child(0).child(1).text().charAt(0);
}
}
return clickStr;
}
/**
* @description: 根据答案点击选择题选项
* @param: answer
* @return: null
*/
function clickByAnswer(answer) {
if (className("ListView").exists()) {
var listArray = className("ListView").findOnce().children();
listArray.forEach(item => {
var listIndexStr = item.child(0).child(1).text().charAt(0);
//单选答案为非ABCD
var listDescStr = item.child(0).child(2).text();
if (answer.indexOf(listIndexStr) >= 0 || answer == listDescStr) {
item.child(0).click();
}
});
}
}
/**
* @description: 检查答案是否正确,并更新数据库
* @param: question, ansTiku, answer
* @return: null
*/
function checkAndUpdate(question, ansTiku, answer) {
if (text("答案解析").exists()) {//答错了
swipe(100, device.height - 100, 100, 100, 500);
var nCout = 0
while (nCout < 5) {
if (textStartsWith("正确答案").exists()) {
var correctAns = textStartsWith("正确答案").findOnce().text().substr(6);
console.info("正确答案是:" + correctAns);
if (ansTiku == "") { //题库为空则插入正确答案
var sql = "INSERT INTO tiku VALUES ('" + question + "','" + correctAns + "','')";
} else { //更新题库答案
var sql = "UPDATE tiku SET answer='" + correctAns + "' WHERE question LIKE '" + question + "'";
}
insertOrUpdate(sql);
console.log("更新题库答案...");
delay(1);
break;
} else {
var clickPos = className("android.webkit.WebView").findOnce().child(2).child(0).child(1).bounds();
click(clickPos.left + device.width * 0.13, clickPos.top + device.height * 0.1);
console.error("未捕获正确答案,尝试修正");
}
nCout++;
}
if (className("Button").exists()) {
className("Button").findOnce().click();
} else {
click(device.width * 0.85, device.height * 0.06);
}
} else { //正确后进入下一题,或者进入再来一局界面
if (ansTiku == "" && answer != "") { //正确进入下一题,且题库答案为空
var sql = "INSERT INTO tiku VALUES ('" + question + "','" + answer + "','')";
insertOrUpdate(sql);
console.log("更新题库答案...");
}
}
}
/**
* @description: 每日答题循环
* @param: null
* @return: null
*/
function dailyQuestionLoop() {
if (textStartsWith("填空题").exists()) {
var questionArray = getFitbQuestion();
}
else if (textStartsWith("多选题").exists() || textStartsWith("单选题").exists()) {
var questionArray = getChoiceQuestion();
}
var blankArray = [];
var question = "";
questionArray.forEach(item => {
if (item != null && item.charAt(0) == "|") { //是空格数
blankArray.push(item.substring(1));
} else { //是题目段
question += item;
}
});
question = question.replace(/\s/g, "");
console.log("题目:" + question);
var ansTiku = getAnswer(question, 'tiku');
if (ansTiku.length == 0) {//tiku表中没有则到tikuNet表中搜索答案
ansTiku = getAnswer(question, 'tikuNet');
}
var answer = ansTiku.replace(/(^\s*)|(\s*$)/g, "");
if (textStartsWith("填空题").exists()) {
if (answer == "") {
var tipsStr = getTipsStr();
answer = getAnswerFromTips(questionArray, tipsStr);
console.info("提示中的答案:" + answer);
setText(0, answer.substr(0, blankArray[0]));
if (blankArray.length > 1) {
for (var i = 1; i < blankArray.length; i++) {
setText(i, answer.substr(blankArray[i - 1], blankArray[i]));
}
}
} else {
console.info("答案:" + answer);
setText(0, answer.substr(0, blankArray[0]));
if (blankArray.length > 1) {
for (var i = 1; i < blankArray.length; i++) {
setText(i, answer.substr(blankArray[i - 1], blankArray[i]));
}
}
}
}
else if (textStartsWith("多选题").exists() || textStartsWith("单选题").exists()) {
if (answer == "") {
var tipsStr = getTipsStr();
answer = clickByTips(tipsStr);
console.info("提示中的答案:" + answer);
} else {
console.info("答案:" + ansTiku);
clickByAnswer(answer);
}
}
delay(0.5);
if (text("确定").exists()) {
text("确定").click();
delay(0.5);
} else if (text("下一题").exists()){
click("下一题");
delay(0.5);
} else if (text("完成").exists()) {
text("完成").click();
delay(0.5);
} else {
console.warn("未找到右上角确定按钮控件,根据坐标点击");
click(device.width * 0.85, device.height * 0.06);//右上角确定按钮,根据自己手机实际修改
}
checkAndUpdate(question, ansTiku, answer);
console.log("------------");
delay(2);
}
/**
* @description: 每日答题
* @param: null
* @return: null
*/
function dailyQuestion() {
let dlNum = 0;//每日答题轮数
while (true) {
delay(1)
if (!(textStartsWith("填空题").exists() || textStartsWith("多选题").exists() || textStartsWith("单选题").exists())) {
toastLog("没有找到题目!请检查是否进入答题界面!");
console.error("没有找到题目!请检查是否进入答题界面!");
console.log("停止");
break;
}
dailyQuestionLoop();
if (text("再练一次").exists()) {
console.log("每周答题结束!")
text("返回").click(); delay(2);
back();
break;
} else if (text("查看解析").exists()) {
console.log("专项答题结束!")
back();delay(0.5);
back();delay(0.5);
break;
} else if (text("再来一组").exists()) {
delay(2);
dlNum++;
if (!text("领取奖励已达今日上限").exists()) {
text("再来一组").click();
console.warn("第" + (dlNum + 1).toString() + "轮答题:");
delay(1);
}
else {
console.log("每日答题结束!")
text("返回").click(); delay(2);
break;
}
}
}
}
function main() {
console.setPosition(0, device.height / 2);
console.show();
delay(1);
dailyQuestion();
console.hide();
}
// 双填空需要两个空字数相等
// 双填空测试四月第二周(正常)
// 复杂填空民法典专项一(已支持)
// main() // 调试完记得注释掉
module.exports = main;