当前位置: 移动技术网 > IT编程>开发语言>JavaScript > Auto.js自动收取自己和好友蚂蚁森林能量脚本

Auto.js自动收取自己和好友蚂蚁森林能量脚本

2018年08月11日  | 移动技术网IT编程  | 我要评论

小沈阳小品视频,高速开车搞怪,翟男

用autojs写的能量收取脚本软件.说一下达到的效果,把蚂蚁森林按钮要设置在常用应用中.先收取自己主页的能量,然后进入排行榜收集好友的能量.中途可以按下音量下键提前结束脚本运行.改改测测基本上现在运行完美的了,充分衡量了网络延迟和程序运行正常的平衡性.并且支持支持运动能量生成时间段执行,等待自己的运动能量生成收集.我是配合tasker设置的定时执行脚本,由tasker唤醒设备教程链接,调用autojs插件执行任务.总之我觉得要想下载下来直接使用的话可能由于运行环境因素,你可能要调整一下.这个作为参考的价值还是稍微比较好的.

说明:

我的设备是6.0的,因为click及很多函数7.0以上才能用.根据搜索组件,利用uiobject组件的click点击我测试在我这失效,改用了获取到组件位置后,调用tap(x,y).点击、滑动的处理我用的是root的处理方式,没root自行应该改为无障碍方式应该也可以话.目前只支持分辨率为1080,1920.如果分辨率不一样,可能你还有做分辨率兼容性处理.或者等我后续处理.后边会不定期更新新的功能

var myenergetype=["线下支付","行走","共享单车","地铁购票","网络购票","网购火车票","生活缴费","etc缴费","电子发票","绿色办公","咸鱼交易","预约挂号"];
var morningtime="07:10";//自己运动能量生成时间
function tlog(msg) {
  toast(msg);
  console.log(msg)
}
/**
 * 获取权限和设置参数
 */
function preparethings(){
  setscreenmetrics(1080, 1920);
  //请求截图
  if(!requestscreencapture()){
    tlog("请求截图失败");
    exit();
  }
}
/**
 * 设置按键监听 当脚本执行时候按音量减 退出脚本
 */
function registevent() {
  //启用按键监听
  events.observekey();
  //监听音量上键按下
  events.onkeydown("volume_down", function(event){
    tlog("脚本手动退出");
    exit();
  });
}
/**
 * 获取截图
 */
function getcaptureimg(){
  var img0 = capturescreen();
  if(img0==null || typeof(img0)=="undifined"){
    tlog("截图失败,退出脚本");
    exit();
  }else{
    return img0;
  }
}
/**
 * 默认程序出错提示操作
 */
function defaultexception() {
  tlog("程序当前所处状态不合预期,脚本退出");
  exit();
}
/**
 * 等待加载收集能量页面,采用未找到指定组件阻塞的方式,等待页面加载完成
 */
function waitpage(type){
  // 等待进入自己的能量主页
  if(type==0){
    desc("消息").findone();
  }
  // 等待进入他人的能量主页
  else if(type==1){
    desc("浇水").findone();
  }
  //再次容错处理
  sleep(3000);
}
/**
 * 从支付宝主页进入蚂蚁森林我的主页
 */
function entermymainpage(){
  launchapp("支付宝");
  tlog("等待支付宝启动");
  var i=0;
  sleep(1000);
  //五次尝试蚂蚁森林入口
  while (!textendswith("蚂蚁森林").exists() && i<=5){
    sleep(2000);
    i++;
  }
  clickbytext("蚂蚁森林",true,"请把蚂蚁森林入口添加到主页我的应用");
  //等待进入自己的主页
  waitpage(0);
}
/**
 * 进入排行榜
 */
function enterrank(){
  swipe(520,1860,520,100);
  sleep(2500);
  clickbydesc("查看更多好友",0,true,"程序未找到排行榜入口,脚本退出");
  var i=0;
  //等待排行榜主页出现
  sleep(2000);
  while (!textendswith("好友排行榜").exists() && i<=5){
    sleep(2000);
    i++;
  }
  if(i>=5){
    defaultexception();
  }
}
/**
 * 从排行榜获取可收集好有的点击位置
 * @returns {*}
 */
function gethasenergyfriend(type) {
  var img = getcaptureimg();
  var p=null;
  if(type==1){
    //411宽度 #a2cbb4 1032,1820 #30bf6c 1032,1787  -33   #52ca84 1032,1832  12   #ffffff 1032,1835  15
    p = images.findmulticolors(img, "#a2cbb4",[[0, -33, "#30bf6c"], [0,12, "#52ca84"],[0,15, "#ffffff"]], {
      region: [1032, 180, 1, 1700]
    });
  }else if(type==2){
    // 480宽度  基准点1775,832  #30bf6  -2,23 #ffffff  -5,16 #ffffff  0,-12 #30bf6c  0,44  #30bf6c
    p = images.findmulticolors(img, "#30bf6c",[[-2, 23, "#ffffff"], [-5,16, "#ffffff"],[0,-12, "#30bf6c"],[0,44, "#30bf6c"]], {
      region: [1069, 180, 7, 1680]
    });
  }
  if(p!=null){
    return p;
  }else {
    return null;
  }
}
/**
 * 判断是否好有排行榜已经结束
 * @returns {boolean}
 */
function isrankend() {
  if(descendswith("没有更多了").exists()){
    var b=descendswith("没有更多了").findone();
    var bs=b.bounds();
    if(bs.centery()<1920){
      return true;
    }
  }
  return false;
}
/**
 * 在排行榜页面,循环查找可收集好友
 * @returns {boolean}
 */
function enterothers(){
  tlog("开始检查排行榜");
  var i=1;
  var epoint=gethasenergyfriend(1);
  //确保当前操作是在排行榜界面
  while(epoint==null && textendswith("好友排行榜").exists()){
    //滑动排行榜 root方式的的点击调用.如无root权限,7.0及其以上可采用无障碍模式的相关函数
    swipe(520,1800,520,300,1000);
    sleep(3000);
    epoint=gethasenergyfriend(1);
    i++;
    //检测是否排行榜结束了
    if(isrankend()){
      return false;
    }
    //如果连续32次都未检测到可收集好友,无论如何停止查找(由于程序控制了在排行榜界面,且判断了结束标记,基本已经不存在这种情况了)
    else if(i>32){
      tlog("程序可能出错,连续"+i+"次未检测到可收集好友");
      exit();
    }
  }
  if(epoint!=null){
    //点击位置相对找图后的修正
    tap(epoint.x,epoint.y+20);
    waitpage(1);
    clickbydesc("可收取",80);
    //进去收集完后,递归调用enterothers
    back();
    sleep(2000);
    var j=0;
    //等待返回好有排行榜
    if(!textendswith("好友排行榜").exists() && j<=5){
      sleep(2000);
      j++;
    }
    if(j>=5){
      defaultexception();
    }
    enterothers();
  }else{
    defaultexception();
  }
}
/**
 * 根据描述值 点击
 * @param energytype
 * @param nofindexit
 */
function clickbydesc(energytype,paddingy,nofindexit,exceptionmsg){
  if(descendswith(energytype).exists()){
    descendswith(energytype).find().foreach(function(pos){
      var posb=pos.bounds();
      tap(posb.centerx(),posb.centery()-paddingy);
      sleep(2000);
    });
  }else{
    if(nofindexit!=null && nofindexit){
      if(exceptionmsg !=null){
        tlog(exceptionmsg);
        exit();
      }else{
        defaultexception();
      }
    }
  }
}
/**
 * 根据text值 点击
 * @param energytype
 * @param nofindexit
 */
function clickbytext(energytype,nofindexit,exceptionmsg){
  if(textendswith(energytype).exists()){
    textendswith(energytype).find().foreach(function(pos){
      var posb=pos.bounds();
      tap(posb.centerx(),posb.centery()-60);
    });
  }else{
    if(nofindexit!=null && nofindexit){
      if(exceptionmsg !=null){
        tlog(exceptionmsg);
        exit();
      }else{
        defaultexception();
      }
    }
  }
}
/**
 * 遍历能量类型,收集自己的能量
 */
function collectionmyenergy(){
  var energyregex=generatecollectiontype();
  var checkinmorning=false;
  //如果是早上7点10分左右的话.等待主页能量出现 每隔一秒检测一次
  while(ismorningtime() && descendswith("行走").exists()){
    if (!checkinmorning){
      tlog("等待运动能量生成中...");
      checkinmorning=true;

    }
    descendswith("行走").find().foreach(function(pos){
      var posb=pos.bounds();
      tap(posb.centerx(),posb.centery()-80);
      sleep(1500);
    });
  }
  if(checkinmorning){
    tlog("运动能量收集完成");
  }
  if(descmatches(energyregex).exists()){
    if(!checkinmorning){
      tlog("防止小树的提示遮挡,等待中");
      sleep(7000);
    }
    descmatches(energyregex).find().foreach(function(pos){
      var posb=pos.bounds();
      tap(posb.centerx(),posb.centery()-80);
      sleep(2000);
    });
  }
}
/**
 * 结束后返回主页面
 */
function whencomplete() {
  tlog("结束");
  back();
  sleep(1500);
  back();
  exit();
}
/**
 * 根据能量类型数组生成我的能量类型正则查找字符串
 * @returns {string}
 */
function generatecollectiontype() {
  var regex="/";
  myenergetype.foreach(function (t,num) {
    if(num==0){
      regex+="(\\s*"+t+"$)";
    }else{
      regex+="|(\\s*"+t+"$)";
    }
  });
  regex+="/";
  return regex;
}
function ismorningtime() {
  var now =new date();
  var hour=now.gethours();
  var minu=now.getminutes();
  var targettime=morningtime.split(":");
  if(number(targettime[0])==hour && math.abs(number(targettime[1])-minu)<=2){
    return true;
  }else{
    return false;
  }
}
//程序主入口
function mainentrence(){
  //前置操作
  preparethings();
  //注册音量下按下退出脚本监听
  registevent();
  //从主页进入蚂蚁森林主页
  entermymainpage();
  //收集自己的能量
  collectionmyenergy();
  //进入排行榜
  enterrank();
  //在排行榜检测是否有好有的能量可以收集
  enterothers();
  //结束后返回主页面
  whencomplete();
}
mainentrence();

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持移动技术网。

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网