/**
 *  editor：白爵帝丶
 *  createDate：2020-10-21
 *  description：淘宝养猫活动
 *  version: 0.9
**/
var version = '0.9';
var taskList = ['去搜索', '去浏览', '去完成', '去施肥', '去逛逛'];

// 手机高
var height = device.height; 
// 手机宽度
var width = device.width;
// 设置手机屏幕
setScreenMetrics(width, height);

// 浏览速度速度
var speed = 1;

dialogs.alert("淘宝养猫脚本即将启动，请为软件开启无障碍功能与悬浮窗\n作者:白爵帝丶\n版本号：v" + version);
menu: while (true) {
    var choose = dialogs.select(
        "请根据你的手机性能(卡不卡)以及网速选择执行速度", 
        "快速执行", // 0
        "正常执行", // 1
        "缓慢执行", // 2
        "很慢执行" // 3
    );
    switch (choose) {
        case -1:
            toast("请选择");
            continue menu;
        case 0:
            toast("即将快速执行脚本");
            speed = 0.75;
            break menu;
        case 1:
            toast("即将正常执行脚本");
            speed = 1;
            break menu;
        case 2:
            toast("即将缓慢执行脚本");
            speed = 1.5;
            break menu;
        case 3:
            toast("即将很慢执行脚本");
            speed = 2;
            break menu;
        default:
            break;
    }
}

// 显示悬浮框
console.show();
log("开启无障碍和悬浮窗权限后将继续执行");
auto.waitFor();

sleep(1000 * speed);
log("正在打开淘宝");
launch("com.taobao.taobao");
sleeping(1500, 2500);
desc("搜索").findOne().click();
sleeping(1500, 2500);
setText("超级星秀猫");
sleeping(1500, 2500);
text("搜索").findOne().click();
log("等待进入活动页面");
sleeping(4500, 6500);
text("赚喵币").waitFor();
log("成功进入页面");

sleeping(1500, 2500);
scollSrceen(1);
text("赚喵币").findOne().click();
sleeping(1500, 2500);

// 执行任务
doTask();

// 当任务执行完，返回上一层搜索框，重新进去活动页点击猫咪
back();
text("搜索").findOne().click();
log("等待进入活动页面");
sleeping(4500, 6500);
text("赚喵币").waitFor();
log("成功进入页面，我要开始摸猫咪了！100次！");
touchCat();

// 退出脚本
exit();

// 执行任务
function doTask() {
    var i = 0;
    var j = 0;
    var k = 0;
    
    if(text("去浏览").exists()) {
        sleeping(1000, 2500);
        i = 0;
        while(textContains("去浏览").exists) {
            i++;
            k++;
            text("去浏览").findOne().click();
            sleeping(1000, 2500);
            scollSrceen(4);
            log("已完成第" + i + "次浏览任务");
            back();
            sleeping(1000, 2500);
        }
        log("总完成" + k + "次任务");
    } 
    
    if(text("去搜索").exists()) {
        sleeping(1000, 2500);
        i = 0;
        while(textContains("去搜索").exists) {
            i++;
            k++;
            text("去搜索").findOne().click();
            sleeping(1000, 2500);
            scollSrceen(4);
            log("已完成第" + i + "次搜索任务");
            back();
            sleeping(1000, 2500);
        }
        log("总完成" + k + "次任务");
    } 
    
    if(text("去逛逛").exists()) {
        sleeping(1000, 2500);
        i = 0;
        while(textContains("去逛逛").exists) {
            i++;
            k++;
            text("去逛逛").findOne().click();
            sleeping(1000, 2500);
            scollSrceen(4);
            log("已完成第" + i + "次逛逛任务");
            back();
            sleeping(1000, 2500);
        }
        log("总完成" + k + "次任务");
    } 

    if(textContains("邀请好友一起").exists()) {
        sleeping(1000, 2500);
        j = 1;
        while(text("去完成").findOnce(j)) {
            i++;
            k++;
            text("去完成").findOnce(j).click();
            sleeping(1000, 2500);
            scollSrceen(4);
            log("已完成第" + i + "次去完成任务");
            back();
            sleeping(1000, 2500);
        }
        log("总完成" + k + "次任务");
    } else {
        j = 0;
        while(text("去完成").findOnce(j)) {
            i++;
            k++;
            text("去完成").findOnce(j).click();
            sleeping(1000, 2500);
            scollSrceen(4);
            log("已完成第" + i + "次去完成任务");
            back();
            sleeping(1000, 2500);
        }
        log("总完成" + k + "次任务");
    }
    log("任务已全完成，接下来我来帮你摸100次猫咪，若未完成任务请重新运行脚本。");
}

// 点击猫咪获取金币
function touchCat() {
    var x = parseInt(random(400, 600));
    var y = parseInt(random(900, 1400));
    for(var i = 0; i < 100 ; i++) {
        sleeping(150,800);
        click(x, y);
    }
}

// 滚动，scollTimes滚动次数
function scollSrceen(scollTimes) {
    for(var i = 0; i < scollTimes; i++) {
        var sleepTime = parseInt(random(1000*(i+1), 1800*(i+1)));
        sleep(sleepTime * speed);
        var startX = parseInt(random(300, 550));
        var startY = parseInt(random(1700, 1950));
        var endX = parseInt(random(700, 900));
        var endY = parseInt(random(400, 500));
        var scorllTime = parseInt(random(850, 2200));
        // 滑动
        sml_move(startX, startY, endX, endY, scorllTime);
    }
}

// 线程睡眠
function sleeping(min, max) {
    sleep(parseInt(random(min, max)) * speed);
}

function bezier_curves(cp, t) {
    cx = 3.0 * (cp[1].x - cp[0].x); 
    bx = 3.0 * (cp[2].x - cp[1].x) - cx; 
    ax = cp[3].x - cp[0].x - cx - bx; 
    cy = 3.0 * (cp[1].y - cp[0].y); 
    by = 3.0 * (cp[2].y - cp[1].y) - cy; 
    ay = cp[3].y - cp[0].y - cy - by; 
    
    tSquared = t * t; 
    tCubed = tSquared * t; 
    result = {
        "x": 0,
        "y": 0
    };
    result.x = (ax * tCubed) + (bx * tSquared) + (cx * t) + cp[0].x; 
    result.y = (ay * tCubed) + (by * tSquared) + (cy * t) + cp[0].y; 
    return result; 
};

//仿真随机带曲线滑动  
//qx, qy, zx, zy, time 代表起点x,起点y,终点x,终点y,过程耗时单位毫秒
function sml_move(qx, qy, zx, zy, time) {
    var xxy = [time];
    var point = [];
    var dx0 = {
        "x": qx,
        "y": qy
    };
    var dx1 = {
        "x": random(qx - 100, qx + 100),
        "y": random(qy , qy + 50)
    };
    var dx2 = {
        "x": random(zx - 100, zx + 100),
        "y": random(zy , zy + 50),
    };
    var dx3 = {
        "x": zx,
        "y": zy
    };
    for (var i = 0; i < 4; i++) {
        eval("point.push(dx" + i + ")");
    };
    for (let i = 0; i < 1; i += 0.08) {
        xxyy = [parseInt(bezier_curves(point, i).x), parseInt(bezier_curves(point, i).y)];
        xxy.push(xxyy);
    }
    gesture.apply(null, xxy);
};