Skip to content

Commit 917cd1f

Browse files
committed
update
加了下次收取时间日志,方便调整定时任务 bug fixes
1 parent ac73d3d commit 917cd1f

File tree

1 file changed

+87
-48
lines changed

1 file changed

+87
-48
lines changed

蚂蚁森林.js

Lines changed: 87 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -234,7 +234,7 @@ function AntForest(robot) {
234234
this.robot = robot;
235235

236236
this.openApp = function () {
237-
toastLog("即将收取蚂蚁森林能量,按Home键停止");
237+
toastLog("即将收取能量,按Home键停止");
238238

239239
launch(ALIPAY);
240240
//waitForPackage(ALIPAY, 500);
@@ -265,18 +265,8 @@ function AntForest(robot) {
265265

266266
var success = false;
267267
var btn = id("com.alipay.android.phone.openplatform:id/app_text").text("蚂蚁森林");
268-
for (var times = 0; times < MAX_RETRY_TIMES; times++) {
269-
if (btn.exists()) {
270-
log("点击按钮");
271-
success = this.robot.clickCenter(btn.findOne(TIMEOUT));
272-
}
273-
if (success) {
274-
break;
275-
} else {
276-
sleep(20);
277-
}
278-
}
279-
if (!success) {
268+
log("点击按钮");
269+
if (!this.robot.clickCenter(btn.findOne(TIMEOUT))) {
280270
toastLog("点击蚂蚁森林失败");
281271
return false;
282272
}
@@ -338,40 +328,71 @@ function AntForest(robot) {
338328
exit();
339329
}
340330

341-
this.takeOthers(bounds, icon, className("android.webkit.WebView").scrollable(true));
331+
var nextElements = [];
332+
var total = 0;
333+
total += this.takeOthers(bounds, icon, className("android.webkit.WebView").scrollable(true), nextElements);
342334

343335
var more = desc("查看更多好友").className("android.view.View").find();
344336
if (more.length) {
345337
toastLog("查看更多好友");
346-
this.robot.clickCenter(more[0]);
347-
348-
// 等待更多列表刷新
349-
if (id("com.alipay.mobile.nebula:id/h5_tv_title").text("好友排行榜").findOne(TIMEOUT)) {
350-
sleep(2000); // 等待界面渲染
351-
//this.takeOthers(bounds, icon, desc("没有更多了").className("android.view.View"));
352-
this.takeOthers(bounds, icon, className("android.webkit.WebView").scrollable(true));
353-
this.robot.back();
338+
if (this.robot.clickCenter(more[0])) {
339+
// 等待更多列表刷新
340+
if (id("com.alipay.mobile.nebula:id/h5_tv_title").text("好友排行榜").findOne(TIMEOUT)) {
341+
sleep(2000); // 等待界面渲染
342+
//this.takeOthers(bounds, icon, desc("没有更多了").className("android.view.View"), nextElements);
343+
total += this.takeOthers(bounds, icon, className("android.webkit.WebView").scrollable(true), nextElements);
344+
this.robot.back();
345+
} else {
346+
toastLog("进入好友排行榜失败");
347+
}
354348
} else {
355349
toastLog("进入好友排行榜失败");
356350
}
357351
}
358352

359-
toastLog("收取能量完毕");
353+
toastLog("收取能量完毕,共" + total + "个好友");
360354
this.robot.back();
355+
356+
// 统计下次时间
357+
var minuteList = [];
358+
nextElements.forEach(function (o) {
359+
minuteList.push(parseInt(o.contentDescription));
360+
});
361+
nextElements = []; // 释放内存
362+
// 排序
363+
minuteList.sort(function (m1, m2) {
364+
return m1 - m2;
365+
});
366+
// 去掉重复的
367+
for (var i = 1, len = minuteList.length; i < len; i++) {
368+
// 相差1分钟以内认为是同一元素
369+
if ((minuteList[i] - minuteList[i - 1]) <= 1) {
370+
minuteList.splice(i--, 1);
371+
len--;
372+
}
373+
}
374+
375+
var date = new Date();
376+
var timestamp = date.getTime();
377+
var timeList = [];
378+
for (var i = 0, len = minuteList.length; i < len; i++) {
379+
var minute = minuteList[i];
380+
var now = timestamp + 60 * minute * 1000;
381+
date.setTime(now);
382+
timeList.push(date.getHours() + ":" + date.getMinutes());
383+
}
384+
if (timeList.length) {
385+
log("下次收取时间:" + timeList.join(', '));
386+
}
361387
};
362388

363389
/**
364390
* 收取能量
365391
* @param bounds
366392
*/
367393
this.take = function (bounds) {
368-
var filters = [];
369-
370-
var all = descMatches("^(绿色能量|\\d+k?g)$").boundsInside(bounds.left, bounds.top, bounds.right, bounds.bottom).find();
371-
toastLog("找到" + (all.length - 1) + "个能量球");
372-
all.each(function (x) {
373-
filters.push(x);
374-
});
394+
var filters = descMatches(/^(绿|\d+k?g)$/).boundsInside(bounds.left, bounds.top, bounds.right, bounds.bottom).find();
395+
toastLog("找到" + (filters.length - 1) + "个能量球");
375396

376397
// 等待能量球渲染
377398
sleep(1500);
@@ -382,13 +403,11 @@ function AntForest(robot) {
382403
});
383404

384405
// 找到第一个并删除(右上角控件)
385-
if (filters.length > 0) {
386-
filters.splice(0, 1);
387-
}
406+
filters.splice(0, 1);
388407

389408
for (var i = 0, len = filters.length; i < len; i++) {
390409
// 原有的click无效
391-
this.robot.clickCenter(filters[i], 100);
410+
this.robot.clickCenter(filters[i]);
392411
log("点击->" + filters[i]);
393412
sleep(200);
394413
}
@@ -399,8 +418,10 @@ function AntForest(robot) {
399418
* @param bounds
400419
* @param icon
401420
* @param endSelector
421+
* @param nextElements
422+
* @returns {number}
402423
*/
403-
this.takeOthers = function (bounds, icon, endSelector) {
424+
this.takeOthers = function (bounds, icon, endSelector, nextElements) {
404425
var times = 0;
405426
var prevTop = 0;
406427
var top = 0;
@@ -409,13 +430,20 @@ function AntForest(robot) {
409430
var y1 = HEIGHT - row_height;
410431
var x2 = WIDTH / 2;
411432
var y2 = row_height;
433+
var total = 0;
412434
while (times < MAX_RETRY_TIMES) {
413-
this.takeFromImage(bounds, icon);
435+
total += this.takeFromImage(bounds, icon);
436+
descMatches(/\d+/).visibleToUser(true).find().each(function(o) {
437+
nextElements.push(o);
438+
});
414439

415440
this.robot.swipe(x1, y1, x2, y2);
416441
sleep(1500); // 等待滑动动画
417442

418-
this.takeFromImage(bounds, icon);
443+
total += this.takeFromImage(bounds, icon);
444+
descMatches(/\d+/).visibleToUser(true).find().each(function(o) {
445+
nextElements.push(o);
446+
});
419447

420448
// 到底部了
421449
var bottomUi = endSelector.find();
@@ -429,12 +457,15 @@ function AntForest(robot) {
429457
}
430458
}
431459
}
460+
461+
return total;
432462
};
433463

434464
/**
435465
* 找图收取
436466
* @param bounds
437467
* @param icon
468+
* @returns {number}
438469
*/
439470
this.takeFromImage = function (bounds, icon) {
440471
var point, capture;
@@ -443,6 +474,7 @@ function AntForest(robot) {
443474
region: [WIDTH - row_height, row_height],
444475
threshold: 0.9
445476
};
477+
var total = 0;
446478
while(true) {
447479
capture = captureScreen();
448480
if (null === capture) {
@@ -457,8 +489,9 @@ function AntForest(robot) {
457489

458490
// 等待好友的森林(标题不为空)
459491
if (id("com.alipay.mobile.nebula:id/h5_tv_title").textMatches(/.+/).findOne(TIMEOUT)) {
460-
sleep(500); // 等待界面渲染
492+
sleep(1500); // 等待界面渲染
461493
toastLog("进入好友森林成功");
494+
total++;
462495
}
463496

464497
// 收取、返回
@@ -469,6 +502,8 @@ function AntForest(robot) {
469502
id("com.alipay.mobile.nebula:id/h5_tv_title").textMatches(/.+/).findOne(TIMEOUT);
470503
sleep(1500); // 等待界面渲染及加载
471504
}
505+
506+
return total;
472507
}
473508
}
474509

@@ -479,9 +514,9 @@ function AntForest(robot) {
479514
function LollipopRobot() {
480515
this.robot = new RootAutomator();
481516

482-
this.click = function (x, y, duration) {
517+
this.click = function (x, y) {
483518
Tap(x, y);
484-
sleep(duration);
519+
sleep(10);
485520
return true;
486521
};
487522

@@ -499,8 +534,14 @@ function LollipopRobot() {
499534
* @constructor
500535
*/
501536
function NougatRobot() {
502-
this.click = function (x, y, duration) {
503-
return press(x, y, duration);
537+
this.click = function (x, y) {
538+
// 点击可能出现故障,重试
539+
for (var times = 0; times < MAX_RETRY_TIMES; times++) {
540+
if (click(x, y)) {
541+
return true;
542+
}
543+
}
544+
return false;
504545
};
505546

506547
this.swipe = function (x1, y1, x2, y2, duration) {
@@ -516,15 +557,13 @@ function NougatRobot() {
516557
function Robot() {
517558
this.robot = (device.sdkInt < 24) ? new LollipopRobot() : new NougatRobot();
518559

519-
this.click = function (x, y, duration) {
520-
duration = duration || 10;
521-
522-
return this.robot.click(x, y, duration);
560+
this.click = function (x, y) {
561+
return this.robot.click(x, y);
523562
};
524563

525-
this.clickCenter = function (b, duration) {
564+
this.clickCenter = function (b) {
526565
var rect = b.bounds();
527-
return this.click(rect.centerX(), rect.centerY(), duration);
566+
return this.click(rect.centerX(), rect.centerY());
528567
};
529568

530569
this.swipe = function (x1, y1, x2, y2, duration) {

0 commit comments

Comments
 (0)