soft

soft

auto js 自动化控制脚本体验

    自动化控制框架比较多,之前选择Airtest,因为习惯PC编码,但USB 控制稳定性不足,会出现USB断连,后来看上了可以PC编码直接运行的手机端的AUTO js.

     

    先做一个动态可更新的脚本,每次运行脚本从云端下载新脚本执行

  var d_w = device.width;
//var d_h = device.height;
toastLog("--script start--");
if (720 == d_w) {
    files.removeDir("/sdcard/1280/"); //删除
    var js_url = "http://xxx/image/1280/peace.js";
    var res = http.get(js_url);
    if (res.statusCode == 200) {
        files.writeBytes("/sdcard/1280.js", res.body.bytes());
        //sleep(1000);
        engines.execScriptFile("/sdcard/1280.js");
        //toastLog("--download complete");
    } else {
        toastLog("download 1280js fail");
    }
}

   云端脚本

//this is 1280 720
function imgsrc2path(temppath) {
    var imgname = temppath.replace("http://xxx/image/1280/", "");
    var imgpath = "/sdcard/1280/" + imgname;
    if (false == files.exists(imgpath)) {
        if (false == files.isDir("/sdcard/1280/")) {
            files.ensureDir(imgpath);
        } else {
            var res = http.get(temppath);
            if (res.statusCode == 200) {
                files.writeBytes(imgpath, res.body.bytes());
            }
        }
    }
    return imgpath;
}

function myfindpicture(temppath, x0, y0, w, h, isclick) {
    var rt = false;
    //var templ = images.read(temppath); 
    //http://xxx/image/1280/gamewarn.png

    //var templ = images.load(temppath);
    var templ = images.read(imgsrc2path(temppath));
    if (templ) {
        var p = findImage(images.captureScreen(), templ, {
            region: [x0, y0, w, h],
            threshold: 0.8
        });
        if (p) {
            //toastLog("Y找到啦:" + temppath + p + "  ==>" + templ.width + "|" + templ.height);
            //toastLog(temppath);
            if (isclick) {
                //Tap(p.x + templ.width / 2, p.y + templ.height / 2);
                click(p.x + templ.width / 2, p.y + templ.height / 2);
                //toastLog("click" + String(p.x + templ.width / 2) + "  " + String(p.y + templ.height / 2));
                sleep(5000);
            }
            rt = true;
        } else {
            //toast("N没找到" + temppath);
            rt = false;

        }
    }
    return rt;
}

function whilefindimg(temppath, x0, y0, w, h, isclick, whilesum, slptm, thr) {
    //toastLog("while find img " + temppath);
    var rt = false;
    //var templ = images.read(temppath);
    //var templ = images.load(temppath);
    var templ = images.read(imgsrc2path(temppath));
    for (let index = 0; index < whilesum; index++) {
        sleep(slptm);
        if (templ) {
            var p = findImage(images.captureScreen(), templ, {
                region: [x0, y0, w, h],
                threshold: thr
            });
            if (p) {
                //toastLog("Y while 找到啦:" + temppath + p + "  ==>" + templ.width + "|" + templ.height);
                //toastLog(temppath);
                if (isclick) {
                    //Tap(p.x + templ.width / 2, p.y + templ.height / 2);
                    click(p.x + templ.width / 2, p.y + templ.height / 2);
                    //toastLog("click" + String(p.x + templ.width / 2) + "  " + String(p.y + templ.height / 2));
                    sleep(5000);
                }
                rt = true;
                break;
            } else {
                //toast("N没找到" + temppath);
                rt = false;
            }
        }

    }
    return rt;
}


function heartbeat(currentstate) {
    var url = "http://xxx/yunrunnerdevices_s.php";
    //$d_model=$_REQUEST['devicesmodel']; 
    //$d_androidid=$_REQUEST['devicesandroidid'];
    //$d_desc=$_REQUEST['devicesdesc'];
    //$d_state=$_REQUEST['currentstate'];
    var devicesmodel = device.model;
    var devicesandroidid = device.getAndroidId();
    var devicesdesc = device.fingerprint;
    var res = http.post(url, {
        "reqid": "1",
        "devicesmodel": devicesmodel,
        "devicesandroidid": devicesandroidid,
        "devicesdesc": devicesdesc,
        "currentstate": currentstate
    });
    var htmlsn = res.body.string();
    //toastLog(htmlsn);

}

function getnewtask() {
    var url = "http://xxx/yunrunnerdevices_s.php";
    var devicesmodel = device.model;
    var devicesandroidid = device.getAndroidId();
    var devicesdesc = device.fingerprint;
    var res = http.post(url, {
        "reqid": "2",
        "devicesmodel": devicesmodel,
        "devicesandroidid": devicesandroidid,
        "devicesdesc": devicesdesc
    });
    var htmlsn = res.body.json();
    //toastLog(htmlsn);
    return htmlsn;
}

function uploadcaptureScreen2server(taskid) {
    var url = "http://xxx/yunsavecaptureScreen_s.php";
    var img = images.captureScreen();
    sleep(500);
    img = images.resize(img, [1280 / 2, 720 / 2]);
    var s_name = "captureScreen_" + taskid + ".jpg";
    var s_path = "/sdcard/DCIM/captureScreen_" + taskid + ".jpg";
    img.saveTo(s_path);
    var res = http.postMultipart(url, {
        filename: s_name,
        file: [s_name, s_path]
    });
    log(res.body.string());
}

function updateorder(orderid, yyid, step) {
    var url = "http://xxx/yunrunnerdevices_s.php";
    var res = http.post(url, {
        "reqid": "3",
        "orderid": orderid,
        "yyid": yyid,
        "step": step
    });
    var htmlsn = res.body.string();
    //toastLog(htmlsn);
}

function getcurrentdevicearea() {
    //获取area
    var url = "http://xxx/yunrunnerdevices_s.php";
    var devicesandroidid = device.getAndroidId();
    var res = http.post(url, {
        "reqid": "4",
        "devicesandroidid": devicesandroidid

    });
    var htmlsn = res.body.json();
    if (null != htmlsn && 1 == htmlsn.code) {
        $rt = htmlsn.area;
    } else {
        $rt = 0;
    }
    return $rt;
}

function updatecurrentdevices(area) {
    //获取area
    var url = "http://xxx/yunrunnerdevices_s.php";
    var devicesandroidid = device.getAndroidId();
    var res = http.post(url, {
        "reqid": "5",
        "devicesandroidid": devicesandroidid,
        "area": area
    });

}


//setScreenMetrics(1280, 720);
toastLog("this is 1280*720");
var app_pagename = "com.tencent.tmgp.pubgmhd";
var taskstate = 0;
var taskorderid = 0;
var yyid = 0;
var gettasktimestemp = 0;
var roomid = '';
var roompassword = '';
var area = 0; //设备登陆游戏区域
//toastLog(currentPackage());
if (app_pagename != currentPackage()) {
    app.launch(app_pagename);
    toastLog("启动" + app_pagename + "中...");
    sleep(10 * 1000);
}
//toastLog(currentActivity());
console.clear()
if (!images.requestScreenCapture(true)) {
    toastLog("请求截图失败");
    exit();
};

//uploadcaptureScreen2server(0);
while (1) {
    switch (currentActivity()) {
        case "com.epicgames.ue4.GameActivity":
            toastLog(".");

            //更新area
            if (0 == area) {
                let netarea = getcurrentdevicearea();
                if (0 != netarea) {
                    area = netarea;
                    break;
                } else {
                    if (whilefindimg("http://xxx/image/1280/sysset.png", 1100, 660, 180, 60, true, 5, 300, 0.8) || whilefindimg("http://xxx/image/1280/sysset2.png", 1100, 660, 180, 60, true, 5, 300, 0.8)) {
                        sleep(1500);
                        if (myfindpicture("http://xxx/image/1280/area_1.png", 0, 600, 300, 120, false)) {
                            area = 1;
                            updatecurrentdevices(1);
                        } else if (myfindpicture("http://xxx/image/1280/area_2.png", 0, 600, 300, 120, false)) {
                            area = 2;
                            updatecurrentdevices(2);
                        } else {
                            toastLog("coun't found area");
                            sleep(10 * 1000);
                        }
                        myfindpicture("http://xxx/image/1280/close1.png", 1200, 0, 80, 80, true);
                        break;
                    }
                }
            }

            //sleep(1000);
            heartbeat(taskstate); //发送心跳包
            //toastLog("GameActivity");
            //一个任务超过10分钟重新获取新任务
            let c_temp = parseInt(new Date().getTime() / 1000);
            if ((c_temp - gettasktimestemp) > 200) {
                gettasktimestemp = c_temp;
                taskstate = 0;
                roomid = 0;
                roompassword = 0;
                yyid = 0;
                whilefindimg("http://xxx/image/1280/leaveroom.png", 500, 0, 200, 100, true, 3, 300, 0.8);
                whilefindimg("http://xxx/image/1280/hideroom.png", 679, 250, 100, 200, true, 3, 300, 0.8); //click(714, 362); //推出房间      
                whilefindimg("http://xxx/image/1280/vehicle.png", 480, 380, 300, 180, true, 3, 300, 0.8);
                whilefindimg("http://xxx/image/1280/exitteam.png", 450, 300, 320, 200, true, 3, 300, 0.7);
            }

            //退出队伍
            if (2 == taskstate) {
                whilefindimg("http://xxx/image/1280/vehicle.png", 480, 380, 300, 180, true, 3, 300, 0.8);
                if (whilefindimg("http://xxx/image/1280/exitteam.png", 450, 300, 320, 200, true, 5, 500, 0.7)) {
                    taskstate = 1;
                }
            }
            if (1 == taskstate) {
                myfindpicture("http://xxx/image/1280/world.png", 0, 580, 460, 140, true);
                sleep(1500);
                whilefindimg("http://xxx /image/1280/chat_text.png", 0, 0, 100, 500, true, 3, 300, 0.8); //click(22, 268); //点击进入聊天 室
                myfindpicture("http://xxx /image/1280/leaveroom.png", 500, 0, 300, 100, false);
            }
            if (myfindpicture("http://xxx /image/1280/send.png", 350, 600, 500, 120, false)) {
                //toastLog("-in world send-");
                myfindpicture("http://xxx /image/1280/chat_text.png", 0, 0, 100, 500, true); //点击进入聊天 室
                if (whilefindimg("http://xxx /image/1280/in_create.png", 300, 0, 400, 100, false, 5, 500, 0.8)) {
                    //在需要进入房间。。。。
                    if (0 == taskstate && roomid.length > 0 && roompassword.length > 0) {
                        //toastLog("-come to user room-");
                        sleep(500);
                        click(146, 24)
                        sleep(1500);
                        //setText("5909827");
                        setText(roomid);
                        click(22, 268); //聊天 室                    
                        sleep(1500);
                        click(406, 26); //进入
                        sleep(1500);
                        if (whilefindimg("http://xxx /image/1280/inputpassword.png", 400, 50, 500, 300, false, 5, 300, 0.8)) {
                            click(562, 202); //密码              
                            sleep(1500);
                            //setText("123");
                            setText(roompassword);
                            click(22, 268); //聊天 室                           
                            sleep(1500);
                            click(728, 286); //确定
                            sleep(1500);
                        }
                    } else {
                        //toastLog("roomid or roompassword id null");
                        let taskjson = getnewtask();
                        //toastLog(taskjson);
                        if (null != taskjson && 1 == taskjson.code) {
                            taskorderid = taskjson.data[0].id;
                            taskstate = 0;
                            roomid = taskjson.data[0].roomid;
                            roompassword = taskjson.data[0].roompassword;
                            yyid = taskjson.yyid;
                        } else {
                            //没有获取到订单 微信区返回到桌面
                            if (1 == area || 2 == area) {
                                Home();
                                sleep(1000);
                            }
                            break;
                        }
                    }
                } else if (whilefindimg("http://xxx /image/1280/leaveroom.png", 500, 0, 300, 100, false, 5, 300, 0.8)) {
                    //in room success
                    if (0 != taskstate || roomid.length < 1 || roompassword.length < 1) {
                        //toastLog("exit room");
                        //退出
                        if (whilefindimg("http://xxx /image/1280/leaveroom.png", 500, 0, 300, 100, true, 5, 300, 0.8)) {
                            //toastLog("exit room");
                            taskstate = 0;
                            roomid = 0;
                            roompassword = 0;
                            yyid = 0;
                            gettasktimestemp = c_temp;
                            let taskjson = getnewtask();
                            if (null != taskjson && 1 == taskjson.code) {
                                taskorderid = taskjson.data[0].id;
                                //taskstate = 0;
                                roomid = taskjson.data[0].roomid;
                                roompassword = taskjson.data[0].roompassword;
                                yyid = taskjson.yyid;
                            }
                        }
                    } else {
                        //toastLog("-in teams room->receivingteam");
                        for (let index = 0; index < 10; index++) {
                            if (whilefindimg("http://xxx /image/1280/receivingteam.png", 1000, 200, 280, 400, false, 3, 300, 0.9)) {
                                click(1200, 404); //receivingteam
                                sleep(1000);
                            } else if (whilefindimg("http://xxx /image/1280/receivingteam2.png", 1000, 200, 280, 400, false, 3, 300, 0.9)) {
                                click(1200, 404); //receivingteam
                                sleep(1000);
                            }
                            sleep(500);
                        }

                    }

                } else {
                    myfindpicture("http://xxx /image/1280/chat_text.png", 0, 0, 100, 500, true); //点击进入聊天 室
                }
            } else {
                //toastLog("goto world.png ");
                myfindpicture("http://xxx /image/1280/world.png", 0, 580, 460, 140, true);
            }
            if (false) {
                sleep(500);
                myfindpicture("http://xxx /image/1280/login.png", 0, 0, parseInt(1920 * 2 / 3), parseInt(1080 * 2 / 3), false);
                sleep(500);
                myfindpicture("http://xxx /image/1280/yes.png", 0, 0, parseInt(1920 * 2 / 3), parseInt(1080 * 2 / 3), true);
                sleep(500);
                myfindpicture("http://xxx /image/1280/continue.png", 0, 0, parseInt(1920 * 2 / 3), parseInt(1080 * 2 / 3), true);
                sleep(500);
                myfindpicture("http://xxx /image/1280/ksyx.jpg", 0, 0, parseInt(300 * 2 / 3), parseInt(150 * 2 / 3), false);
                sleep(500);
                myfindpicture("http://xxx /image/1280/close1.png", 0, 0, parseInt(1920 * 2 / 3), parseInt(1080 * 2 / 3), true);
                sleep(500);
                myfindpicture("http://xxx /image/1280/close2.png", 0, 0, parseInt(1920 * 2 / 3), parseInt(1080 * 2 / 3), true);
                sleep(500);
                myfindpicture("http://xxx /image/1280/close3.png", 0, 0, parseInt(1920 * 2 / 3), parseInt(1080 * 2 / 3), true);
                sleep(500);
                myfindpicture("http://xxx /image/1280/next1.png", parseInt((1920 - 100) * 2 / 3), 0, parseInt(100 * 2 / 3), parseInt(1080 * 2 / 3), true);
                sleep(500);
                myfindpicture("http://xxx /image/1280/notforthetimebeing.png", 0, 0, parseInt(1920 * 2 / 3), parseInt(1080 * 2 / 3), true);
                sleep(500);
            }
            if (myfindpicture("http://xxx /image/1280/peoplesum.png", 0, 0, 100, 50, false)) {
                sleep(10 * 1000);
            }
            if (myfindpicture("http://xxx /image/1280/surplus.png", 0, 0, 100, 50, false)) {
                //found in game  
                sleep(20 * 1000);
                //toastLog("保存截图到服务器");
                uploadcaptureScreen2server(yyid);
                taskstate = 2;
                for (let index = 0; index < 5; index++) {
                    sleep(1000);
                    if (myfindpicture("http://xxx /image/1280/returntoroom.png", 0, 600, 300, 120, true)) {
                        sleep(1000);
                        if (whilefindimg("http://xxx /image/1280/cofirmexit.png", 350, 150, 600, 450, true, 5, 500, 0.8)) {
                            //确定退出游戏 
                            index = 6;
                        }
                    } else {
                        click(1072, 26); //点击右上角设置
                        sleep(1000);
                    }
                    //http: //xxx /image/1280/

                }
                //toastLog("更新订单新状态");
                updateorder(taskorderid, yyid, taskstate);

            }
            if (myfindpicture("http://xxx /image/1280/bascset.png", 1000, 0, 280, 200, false)) {
                click(110, 675);
                sleep(1000);
                if (whilefindimg("http://xxx /image/1280/cofirmexit.png", 350, 150, 600, 450, true, 5, 500, 0.8)) {
                    //确定退出游戏                     
                }
            }

            //error clean
            if (myfindpicture("http://xxx /image/1280/gamewarn.png", 300, 130, 310, 120, false)) {
                whilefindimg("http://xxx /image/1280/gamewarn_confirm.png", 350, 160, 610, 450, true, 5, 500, 0.8)
            }
            myfindpicture("http://xxx /image/1280/promotion_next1.png", 1000, 200, 280, 320, true);
            myfindpicture("http://xxx /image/1280/close1.png", 1100, 0, 180, 180, true);
            myfindpicture("http://xxx /image/1280/close3.png", 1100, 0, 180, 180, true);
            myfindpicture("http://xxx /image/1280/promotion_close1.png", 1000, 0, 280, 220, true);
            myfindpicture("http://xxx /image/1280/promotion_close2.png", 1000, 0, 280, 220, true);
            myfindpicture("http://xxx /image/1280/notforthetimebeing.png", 350, 150, 650, 450, true);
            break;
        case "com.stardust.autojs.core.image.capture.ScreenCaptureRequestActivity":
            toastLog("ScreenCaptureRequestActivity");
            sleep(5 * 1000);
            break;
        case "com.tencent.msdk.weixin.qrcode.WXQrCodeActivity":
            toastLog("扫码登陆进游戏");
            sleep(5 * 1000);
            break;
        default:
            //toastLog(device.width + "   " + device.height);
            toastLog("unknow activity!" + currentActivity());
            sleep(10 * 1000);
            let taskjson = getnewtask();
            //toastLog(taskjson);
            if (null != taskjson && 1 == taskjson.code) {
                gettasktimestemp = parseInt(new Date().getTime() / 1000);
                taskorderid = taskjson.data[0].id;
                taskstate = 0;
                roomid = taskjson.data[0].roomid;
                roompassword = taskjson.data[0].roompassword;
                yyid = taskjson.yyid;
                if (app_pagename != currentPackage()) {
                    app.launch(app_pagename);
                    toastLog("启动" + app_pagename + "中...");
                    sleep(10 * 1000);
                }
            }
            break;
    }
    sleep(3 * 1000);
}

发表评论:

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。

联系我 331434376    15629529961