forked from lolisaikou/LazyStudy
-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathchallengeAnswer.js
More file actions
206 lines (190 loc) · 6.26 KB
/
challengeAnswer.js
File metadata and controls
206 lines (190 loc) · 6.26 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
importClass(android.database.sqlite.SQLiteDatabase);
var lCount = 1;//挑战答题轮数
var qCount = 5;//挑战答题每轮答题数
/**
* @description: 延时函数
* @param: seconds-延迟秒数
* @return: null
*/
function delay(seconds) {
sleep(1000 * seconds);//sleep函数参数单位为毫秒所以乘1000
}
/**
* @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.log(table_name + "题库中未找到答案");
cursor.close();
return '';
}
}
function indexFromChar(str) {
return str.charCodeAt(0) - "A".charCodeAt(0);
}
/**
* @description: 每次答题循环
* @param: conNum 连续答对的次数
* @return: null
*/
function challengeQuestionLoop(conNum) {
if (conNum >= qCount)//答题次数足够退出,每轮5次
{
let listArray = className("ListView").findOnce().children();//题目选项列表
let i = random(0, listArray.length - 1);
console.log("今天答题次数已够,随机点击一个答案");
listArray[i].child(0).click();//随意点击一个答案
console.log("------------");
return;
}
if (className("ListView").exists()) {
var question = className("ListView").findOnce().parent().child(0).text();
console.log((conNum + 1).toString() + ".题目:" + question);
}
else {
console.error("提取题目失败!");
let listArray = className("ListView").findOnce().children();//题目选项列表
let i = random(0, listArray.length - 1);
console.log("随机点击一个");
listArray[i].child(0).click();//随意点击一个答案
return;
}
var chutiIndex = question.lastIndexOf("出题单位");
if (chutiIndex != -1) {
question = question.substring(0, chutiIndex - 2);
}
question = question.replace(/\s/g, "%");
var options = [];//选项列表
if (className("ListView").exists()) {
className("ListView").findOne().children().forEach(child => {
var answer_q = child.child(0).child(1).text();
options.push(answer_q);
});
} else {
console.error("答案获取失败!");
return;
}
var optionStr = options.join("_");
question = question + "%" + optionStr;
var answer = getAnswer(question, 'tiku');
if (answer.length == 0) {//tiku表中没有则到tikuNet表中搜索答案
answer = getAnswer(question, 'tikuNet');
}
console.info("答案:" + answer);
if (/^[a-zA-Z]{1}$/.test(answer)) {//如果为ABCD形式
var indexAnsTiku = indexFromChar(answer.toUpperCase());
answer = options[indexAnsTiku];
toastLog("answer from char=" + answer);
}
let hasClicked = false;
let listArray = className("ListView").findOnce().children();//题目选项列表
if (answer == "")//如果没找到答案
{
let i = random(0, listArray.length - 1);
console.error("没有找到答案,随机点击一个");
delay(1);
listArray[i].child(0).click();//随意点击一个答案
hasClicked = true;
console.log("------------");
}
else//如果找到了答案
{
listArray.forEach(item => {
var listDescStr = item.child(0).child(1).text();
if (listDescStr == answer) {
//点击
item.child(0).click();
hasClicked = true;
log("-----------------------------------");
}
});
}
if (!hasClicked)//如果没有点击成功
{
console.error("未能成功点击,随机点击一个");
delay(1);
let i = random(0, listArray.length - 1);
listArray[i].child(0).click();//随意点击一个答案
console.log("------------");
}
}
/**
* @description: 挑战答题
* @param: null
* @return: null
*/
function challengeQuestion() {
text("我的").click();
while (!textContains("我要答题").exists());
delay(1);
click("我要答题");
while (!text("答题竞赛").exists());
delay(1);
var ob= text("答题竞赛").findOne().parent();
var index = ob.childCount() - 1;
ob.child(index).click();
console.info("开始挑战答题")
delay(4);
let conNum = 0; //连续答对的次数
let lNum = 1; //轮数
while (true) {
delay(1);
challengeQuestionLoop(conNum);
delay(1);
if (text("v5IOXn6lQWYTJeqX2eHuNcrPesmSud2JdogYyGnRNxujMT8RS7y43zxY4coWepspQkvw" +
"RDTJtCTsZ5JW+8sGvTRDzFnDeO+BcOEpP0Rte6f+HwcGxeN2dglWfgH8P0C7HkCMJOAAAAAElFTkSuQmCC").exists()) //遇到❌号,则答错了,不再通过结束本局字样判断
{
delay(1);
if (lNum >= lCount && conNum >= qCount) {
console.log("挑战答题结束!返回主页!");
/* 回退4次返回主页 */
back();
delay(1);
back();
delay(1);
back();
delay(1);
back();
delay(1);
break;
} else {
console.log("等5秒开始下一轮...")
delay(1);
click("结束本局");
delay(1);
text("再来一局").waitFor();
click("再来一局");
delay(5);
if (conNum >= qCount) {
lNum++;
}
conNum = 0;
}
console.warn("第" + lNum.toString() + "轮开始...")
} else //答对了
{
conNum++;
}
}
}
function main() {
console.setPosition(0, device.height / 2);//部分华为手机console有bug请注释本行
console.show();
delay(1);
challengeQuestion();
console.hide()
}
//main();
module.exports = main;