forked from e1399579/autojs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path蚂蚁森林.js
669 lines (576 loc) · 21 KB
/
蚂蚁森林.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
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
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
/**
* 使用说明请看 https://github.com/e1399579/autojs/blob/master/README.md
* @author ridersam <[email protected]>
*/
auto(); // 自动打开无障碍服务
var config = files.isFile("config.js") ? require("config.js") : {};
if (typeof config !== "object") {
config = {};
}
var options = Object.assign({
password: "",
pattern_size: 3
}, config); // 用户配置合并
// 所有操作都是竖屏
const WIDTH = Math.min(device.width, device.height);
const HEIGHT = Math.max(device.width, device.height);
const IS_ROOT = files.exists("/sbin/su") || files.exists("/system/xbin/su") || files.exists("/system/bin/su");
setScreenMetrics(WIDTH, HEIGHT);
start(options);
/**
* 开始运行
* @param options
*/
function start(options) {
checkModule();
var Robot = require("Robot.js");
var robot = new Robot(options.max_retry_times);
var antForest = new AntForest(robot, options);
antForest.saveState();
while (!device.isScreenOn()) {
device.wakeUp();
sleep(1000); // 等待屏幕亮起
}
// 连续运行处理
var taskManager = new TaskManager();
taskManager.init();
taskManager.listen();
taskManager.waitFor();
// 先打开APP,节省等待时间
threads.start(function () {
antForest.openApp();
});
if (files.exists("Secure.js")) {
var Secure = require("Secure.js");
var secure = new Secure(robot, options.max_retry_times);
secure.openLock(options.password, options.pattern_size);
// 拉起到前台界面
antForest.openApp();
}
antForest.launch();
antForest.work();
antForest.resumeState();
// 退出
exit();
throw new Error("强制退出");
}
/**
* 检查必要模块
*/
function checkModule() {
if (!files.exists("Robot.js")) {
throw new Error("缺少Robot.js文件,请核对第一条");
}
if (!files.exists("Secure.js") && context.getSystemService(context.KEYGUARD_SERVICE).inKeyguardRestrictedInputMode()) {
throw new Error("缺少Secure.js文件,请核对第一条");
}
}
function TaskManager() {
this.task_no = 0;
this.time_tag = "start_time";
this.wait_time = 15000;
this.init = function () {
engines.myEngine().setTag(this.time_tag, (new Date()).getTime());
var task_list = this.getTaskList();
this.task_no = this.findIndex(engines.myEngine(), task_list);
log(Object.keys(task_list));
};
this.getTaskList = function () {
return engines.all().sort(function(e1, e2) {
return e1.getTag(this.time_tag) - e2.getTag(this.time_tag);
}.bind(this));
};
this.findIndex = function (engine, list) {
var engine_id = engine.id;
var id_list = list.map(function (o) {
return o.id;
});
return id_list.indexOf(engine_id);
};
this.listen = function() {
// 子线程
threads.start(function () {
// 监听音量上键
events.observeKey();
events.onceKeyDown("volume_up", function (event) {
engines.stopAll();
exit();
});
});
};
this.waitFor = function () {
while (1) {
device.wakeUpIfNeeded();
var task_no = this.findIndex(engines.myEngine(), this.getTaskList());
if (task_no > 0) {
log("任务" + this.task_no + "排队中,前面有" + task_no + "个任务");
sleep(this.wait_time);
} else {
log("任务" + this.task_no + "开始运行");
break;
}
}
};
}
/**
* 蚂蚁森林的各个操作
* @param robot
* @param options
* @constructor
*/
function AntForest(robot, options) {
this.robot = robot;
options = options || {};
var settings = {
timeout: 8000, // 超时时间:毫秒
max_retry_times: 10, // 最大失败重试次数
takeImg: "take.png", // 收取好友能量用到的图片
max_swipe_times: 100, // 好友列表最多滑动次数
min_time: "7:14:00", // 检测时段
max_time: "7:15:50",
check_within_time: 5,
help_img: ""
};
this.options = Object.assign(settings, options);
this.package = "com.eg.android.AlipayGphone"; // 支付宝包名
this.state = {};
this.capture = null;
this.bounds = [0, 0, WIDTH, 1100];
this.icon_num = 1;
this.start_time = (new Date()).getTime();
this.detected = 0;
toastLog("即将收取能量,按音量上键停止");
this.saveState = function () {
this.state.isScreenOn = device.isScreenOn();
this.state.currentPackage = currentPackage(); // 当前运行的程序
this.state.isRunning = IS_ROOT ? parseInt(shell("ps | grep 'AlipayGphone' | wc -l", true).result) : 0; // 支付宝是否运行
this.state.version = context.getPackageManager().getPackageInfo(this.package, 0).versionName;
log(this.state);
};
this.resumeState = function () {
if (this.state.currentPackage !== this.package) {
this.back(); // 回到之前运行的程序
sleep(1500);
}
if (!this.state.isRunning) {
this.closeApp();
}
if (!this.state.isScreenOn) {
KeyCode("KEYCODE_POWER");
}
};
this.openApp = function () {
launch(this.package);
};
this.closeApp = function () {
this.robot.kill(this.package);
};
this.launch = function () {
var times = 0;
do {
if (this.doLaunch()) {
return;
} else {
times++;
this.back();
sleep(1500);
this.openApp();
}
} while (times < this.options.max_retry_times);
throw new Error("运行失败");
};
this.doLaunch = function () {
// 可能出现的红包弹框,点击取消
var timeout = this.options.timeout;
threads.start(function () {
var cancelBtn;
if (cancelBtn = id("com.alipay.mobile.accountauthbiz:id/update_cancel_tv").findOne(timeout)) {
cancelBtn.click();
}
if (cancelBtn = id("com.alipay.android.phone.wallet.sharetoken:id/btn1").findOne(timeout)) {
cancelBtn.click();
}
});
log("打开蚂蚁森林");
app.startActivity({
action: "VIEW",
data: "alipays://platformapi/startapp?appId=60000002"
});
// 等待加载
if (this.waitForLoading("种树")) {
log("进入蚂蚁森林成功");
} else {
toastLog("进入蚂蚁森林失败");
return false;
}
return true;
};
this.waitForLoading = function (keyword) {
var timeout = this.options.timeout;
var waitTime = 200;
sleep(2000);
timeout -= 2000;
for (var i = 0; i < timeout; i += waitTime) {
if (desc(keyword).exists()) {
sleep(1000);
return true;
}
sleep(waitTime); // 加载中
}
return false;
};
this.getPower = function () {
var energy= descMatches(/\d+g/).findOnce();
return energy ? parseInt(energy.contentDescription) : null;
};
this.work = function () {
sleep(1000);
this.robot.click(WIDTH / 2, 510);
var timeout = this.options.timeout;
var startPower = this.getPower();
log("当前能量:" + startPower);
// 开始收取
this.take();
this.takeRemain(this.getRemainList(), this.options.min_time, this.options.max_time);
sleep(500);
var power = this.getPower() - startPower;
if (power > 0) toastLog("收取了" + power + "g自己的能量");
var icon_list = [];
var icon = images.read(this.options.takeImg);
if (null === icon) {
throw new Error("缺少图片文件,请仔细查看使用方法的第一条!!!");
}
icon_list = [icon];
var icon2;
if (this.options.help_img && (icon2 = images.read(this.options.help_img))) {
icon_list[1] = icon2;
}
this.icon_num = icon_list.length;
// 截图权限申请
threads.start(function () {
var remember;
var beginBtn;
if (remember = id("com.android.systemui:id/remember").checkable(true).findOne(timeout)) {
remember.click();
}
if (beginBtn = classNameContains("Button").textContains("立即开始").findOne(timeout)) {
beginBtn.click();
}
});
if (!requestScreenCapture(false)) {
throw new Error("请求截图失败");
}
// 跳过当前屏幕
var y = Math.min(HEIGHT, 1720);
this.robot.swipe(WIDTH / 2, y, WIDTH / 2, 0);
sleep(500);
log("开始收取好友能量");
var bottom = 0;
var total_list = this.takeOthers(icon_list, 500, function () {
var rect = desc("种树").findOnce().bounds();
if (rect.bottom === bottom) {
return true;
}
bottom = rect.bottom;
return false;
});
// 统计下次时间
var minuteList = [];
var keyword = "查看更多好友";
if (desc(keyword).exists()) {
log(keyword);
if (this.robot.clickCenter(desc(keyword).findOne(timeout))) {
// 等待更多列表刷新
if (id("com.alipay.mobile.nebula:id/h5_tv_title").text("好友排行榜").findOne(timeout)) {
sleep(1000);
log("进入好友排行榜成功");
// 跳过第一屏
var y = Math.min(HEIGHT, 1720);
this.robot.swipe(WIDTH / 2, y, WIDTH / 2, 0);
sleep(500);
var page, min_minute, add_total_list, swipe_sleep = 500;
for (;;) {
log("往下翻页");
page = 0;
add_total_list = this.takeOthers(icon_list, swipe_sleep, function () {
/*var selector = desc("没有更多了");
if (!selector.exists()) return false;
return selector.findOne().visibleToUser();*/
page++;
return (page > this.options.max_swipe_times)
|| (findColorEquals(this.capture, "#30BF6C", WIDTH - 300, 0, 200, HEIGHT) !== null);
}.bind(this));
this.addTotal(total_list, add_total_list);
minuteList = this.statisticsNextTime();
this.filterMinuteList(minuteList);
if (!this.executeNextTask()) {
log("检测自己的能量尚未开始,不执行反复检测");
break;
}
if (!minuteList.length) {
break;
}
min_minute = minuteList[0];
log("当前最小剩余" + min_minute + "分钟");
if (min_minute > this.options.check_within_time) {
break;
}
swipe_sleep = 300;
log("往上翻页");
page = 0;
add_total_list = this.takeOthers(icon_list, swipe_sleep, function () {
page++;
return (page > this.options.max_swipe_times)
|| (findColorEquals(this.capture, "#EFAE44", 0, 0, 110, HEIGHT / 2) !== null);
}.bind(this), "prev");
this.addTotal(total_list, add_total_list);
}
this.back();
sleep(2000);
this.waitForLoading("TA收取你");
} else {
toastLog("进入好友排行榜失败");
}
} else {
toastLog("进入好友排行榜失败");
}
} else {
minuteList = this.statisticsNextTime();
this.filterMinuteList(minuteList);
}
var endPower = this.getPower();
var added = endPower - startPower;
added = Math.max(0, added);
this.back();
var message = "收取完毕,共" + total_list[0] + "个好友," + added + "g能量";
if (this.icon_num > 1) {
message += ",帮了" + total_list[1] + "个好友收取";
}
toastLog(message);
sleep(1500);
// 统计部分,可以删除
var timeList = this.getTimeList(minuteList);
if (timeList.length) {
log("可收取时间:" + timeList.join(', '));
// 添加tasker任务
if (this.executeNextTask()) {
var date = new Date();
var today = date.toDateString();
var next_time = today + " " + timeList[0];
this.notifyTasker(next_time);
} else {
log("检测自己的能量尚未开始,不发送Tasker任务");
}
}
};
this.addTotal = function (total_list, add_total_list) {
for (var i = 0; i < this.icon_num; i++) {
total_list[i] += add_total_list[i];
}
};
this.statisticsNextTime = function () {
var minuteList = [];
descMatches(/\d+’/).find().forEach(function (o) {
minuteList.push(parseInt(o.contentDescription));
});
return minuteList;
};
this.filterMinuteList = function (minuteList) {
// 排序
minuteList.sort(function (m1, m2) {
return m1 - m2;
});
// 去掉重复的
for (var i = 1, len = minuteList.length; i < len; i++) {
// 相差1分钟以内认为是同一时间
if ((minuteList[i] - minuteList[i - 1]) <= 1) {
minuteList.splice(i--, 1);
len--;
}
}
};
this.getTimeList = function (minuteList) {
var date = new Date();
var timeList = [];
var timestamp = date.getTime() - 20000;
for (var i = 0, len = minuteList.length; i < len; i++) {
var minute = minuteList[i];
var now = timestamp + minute * 60 * 1000;
date.setTime(now);
timeList.push(date.getHours() + ":" + date.getMinutes() + ":" + date.getSeconds());
}
return timeList;
};
this.executeNextTask = function () {
if (this.detected) return true;
var date = new Date();
var today = date.toDateString();
var max_time = today + " " + this.options.max_time;
var max_timestamp = Date.parse(max_time);
return (this.start_time > max_timestamp);
};
this.notifyTasker = function (time) {
app.sendBroadcast({
action: "net.dinglisch.android.tasker.ActionCodes.RUN_SCRIPT",
extras: {
name: "蚂蚁森林",
time: time
}
});
log("已发送Tasker任务:" + time);
};
/**
* 收取能量
*/
this.take = function () {
var filters = className("android.widget.Button").filter(function (o) {
var desc = o.contentDescription;
return (null !== desc.match(/^收集能量|^\s?$/));
}).find();
var num = filters.length;
log("找到" + num + "个能量球");
sleep(100 * num);
this.robot.clickMultiCenter(filters);
this.autoBack();
};
this.autoBack = function () {
// 误点了按钮则返回
sleep(1000);
if (id("com.alipay.mobile.ui:id/title_bar_title").exists() || text("通知").exists()) {
this.back();
sleep(1500);
}
};
/**
* 获取剩余能量球列表
*/
this.getRemainList = function () {
var list = [];
className("android.widget.Button").filter(function (o) {
var desc = o.contentDescription;
return (null !== desc.match(/^收集能量|^\s?$/));
}).find().forEach(function (o) {
var rect = o.bounds();
list.push([rect.centerX(), rect.centerY()]);
}.bind(this));
return list;
};
this.takeRemain = function (list, min_time, max_time) {
var len = list.length;
if (!len) return;
var date = new Date();
var today = date.toDateString();
var min_timestamp = Date.parse(today + " " + min_time);
var max_timestamp = Date.parse(today + " " + max_time);
var now = date.getTime();
if ((min_timestamp <= now) && (now <= max_timestamp)) {
toastLog("开始检测剩余能量");
var millisecond = max_timestamp - now;
var step_time = 0;
var use_time = step_time + 156 * len;
for (var i = 0;i <= millisecond;i += use_time) {
this.robot.clickMulti(list);
sleep(step_time);
}
this.autoBack();
this.detected = 1;
toastLog("检测结束");
}
};
/**
* 收取好友能量
* @param icon_list
* @param isEndFunc
* @param swipe_sleep
* @param scroll
* @returns {Array}
*/
this.takeOthers = function (icon_list, swipe_sleep, isEndFunc, scroll) {
var row = (192 * (HEIGHT / 1920)) | 0;
var total_list = [];
var take_num = icon_list.length;
var x1, y1, x2, y2;
x2 = x1 = WIDTH / 2;
switch (scroll) {
case "next":
default:
y1 = HEIGHT - row;
y2 = row;
break;
case "prev":
y1 = row * 1.5;
y2 = HEIGHT - row;
break;
}
for (var i = 0; i < take_num; i++) {
total_list[i] = 0;
}
while (1) {
for (var i = 0; i < take_num; i++) {
var icon = icon_list[i];
total_list[i] += this.takeFromImage(icon);
}
if (isEndFunc()) {
break;
}
this.robot.swipe(x1, y1, x2, y2);
sleep(swipe_sleep); // 等待滑动动画
}
return total_list;
};
/**
* 找图收取
* @param icon
* @returns {number}
*/
this.takeFromImage = function (icon) {
var point;
var row_height = HEIGHT / 10;
var options = {
region: [WIDTH - row_height, row_height],
threshold: 0.8
};
var total = 0;
var times = 0;
var x = WIDTH / 2;
var offset = icon.getHeight() / 2;
while (times < this.options.max_retry_times) {
this.capture = captureScreen();
if (null === this.capture) {
toastLog("截图失败");
times++;
sleep(200);
continue;
}
point = findImage(this.capture, icon, options);
if (null === point) {
break;
}
var y = point.y + offset;
this.robot.click(x, y);
// 等待好友的森林
var title = "好友森林";
if (this.waitForLoading("浇水")) {
title = id("com.alipay.mobile.nebula:id/h5_tv_title").findOnce();
log("进入" + title.text() + "成功");
total++;
var cover;
if (cover = descMatches(/\d{2}:\d{2}:\d{2}/).findOnce()) {
toastLog("保护罩还剩" + cover.contentDescription + ",忽略");
} else {
// 收取
this.take();
}
} else {
toastLog("进入好友森林失败");
}
// 返回好友列表
this.back();
sleep(3000);
}
return total;
};
this.back = function () {
back();
};
}