document/脚本/冒泡/modules/mp-info.js
2024-11-17 00:35:38 +08:00

496 lines
16 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

let mpImage = require('./mp-image');
let mpApp = require('./mp-app');
let mp_info = {
ra: null,
apiBaseUrl: "http://192.168.1.12:800/",
/**
* 检测次数间隔时间大约每33毫秒检测一次1秒30次
*/
detectInterval: 1000 / 3,
detectTimer: null,
AutoFightInfo: {
isAutoFight: false,
thread: null,
zhandouCount: 0,
isZhanDou: false,
currentRounds: 0,
fightTimer: null,
/**
* 自动战斗间隔时间500毫秒
*/
autoFightInterval: 500,
/**
*
*/
isFengShou: false,
isLaDao: false,
},
User: {
status: -1, // 未知。1未战斗2战斗中
update: false,
ws: null,
thread: null,
autoSeekFightThread: null
},
Task: {
autoSeekTask: false,
thread: null
},
startTask() {
this.Task.autoSeekTask = true;
this.Task.thread = threads.start(() => this.autoTask());
},
getNeiZhiDaNao(url, mImage) {
let img = this.getCaptureScreen();
var base = images.toBase64(img, format = "jpeg", quality = 50)
img.recycle();
//判断当前位置区域
var postData = { image: base, mImage: "" };
if (mImage != null) {
postData.mImage = mImage;
}
var response = http.postJson(url, postData);
console.log(url, response.body.string(), response.body.json());
if (response == null) {
return null;
}
var data = response.body.json();
return data;
},
autoTask() {
console.log('开启帮派自动任务', this.apiBaseUrl);
var panduanweizhi = this.apiBaseUrl + "api/Task/PanDuanWeiZhi";
var taskList = this.apiBaseUrl + "api/Task/TaskList";
var piPei = this.apiBaseUrl + "api/Task/PiPei";
while (this.Task.autoSeekTask) {
var data = this.getNeiZhiDaNao(panduanweizhi, null);
if (data == null) {
sleep(5000)
break;
}
if (data.action == "click") {
console.log(data.data.x, data.data.y);
click(data.data.x, data.data.y)
//休眠500毫秒
sleep(1000);
}
var data1 = this.getNeiZhiDaNao(taskList);
if (data1 == null) {
sleep(5000)
break;
}
if (data1.action == "ok") {
console.log('模拟按键OK');
KeyCode("KEYCODE_DPAD_CENTER");
sleep(1000);
KeyCode("KEYCODE_DPAD_CENTER");
sleep(500);
KeyCode("KEYCODE_DPAD_CENTER");
sleep(500);
}
//再次点击帮派主管
click(data.data.x, data.data.y)
this.完成帮派任务(data, piPei, taskList);
sleep(2000);
}
},
完成帮派任务(data, piPei, taskList) {
//休眠500毫秒
sleep(1000);
//判断是否在任务列表中
var data1 = this.getNeiZhiDaNao(taskList, null);
if (data1 == null) {
sleep(5000)
return;
}
if (data1.action == "ok") {
//等于帮派列表
console.log('模拟按键OK');
KeyCode("KEYCODE_DPAD_CENTER");
}
//
var 完成任务 = this.getNeiZhiDaNao(piPei, "detect_tigers/帮派/renwuweiwanc.png")
if (完成任务.action != null && 完成任务.action != "") {
KeyCode("KEYCODE_DPAD_CENTER");
console.log('模拟按键OK');
KeyCode("KEYCODE_DPAD_CENTER");
sleep(1000);
KeyCode("KEYCODE_DPAD_CENTER");
sleep(500);
KeyCode("KEYCODE_DPAD_CENTER");
sleep(500);
console.log('任务完成');
return;
}
//任务未完成
KeyCode("KEYCODE_DPAD_CENTER");
sleep(100)
var data1 = this.getNeiZhiDaNao(taskList, null);
if (data1 == null) {
KeyCode("KEYCODE_DPAD_CENTER");
sleep(1000)
}
var 更换帮派任务 = this.getNeiZhiDaNao(piPei, "detect_tigers/帮派/genghuan.png")
if (更换帮派任务.action == "ok") {
//任务未完成
click(更换帮派任务.data.x, 更换帮派任务.data.y)
sleep(500)
var 确定更换帮派任务 = this.getNeiZhiDaNao(piPei, "detect_tigers/帮派/genghuanqueding.png")
if (确定更换帮派任务.action == "ok") {
KeyCode("KEYCODE_DPAD_CENTER");
console.log('模拟按键OK');
KeyCode("KEYCODE_DPAD_CENTER");
sleep(1000);
KeyCode("KEYCODE_DPAD_CENTER");
sleep(500);
}
}
},
stopTask() {
this.Task.autoSeekTask = false;
this.Task.thread.interrupt();
this.Task.thread = null;
},
startUser(userId) {
let ws = new WebSocket('ws://192.168.1.12:800/ws?id=' + userId);
ws
.on(WebSocket.EVENT_OPEN, (res, ws) => {
console.log('WebSocket 已连接');
})
.on(WebSocket.EVENT_MESSAGE, (message, ws) => {
console.log('接收到消息');
})
.on(WebSocket.EVENT_TEXT, (text, ws) => {
console.info('接收到文本消息:');
console.info(`text: ${text}`);
if (text != null && text != '') {
var objx = JSON.parse(text);
if (objx != null) {
if (objx.Action == "click") {
console.log('点击坐标');
Tap(objx.Data.X, objx.Data.Y)
setTimeout(() => {
console.log('点击坐标284, 369');
Tap(284, 369)
console.log('点击坐标173, 714');
Tap(173, 714)
}, 500)
// sleep(50);
}
}
}
})
.on(WebSocket.EVENT_BYTES, (bytes, ws) => {
console.info('接收到字节数组消息:');
console.info(`utf8: ${bytes.utf8()}`);
// console.info(`base64: ${bytes.base64()}`);
// console.info(`md5: ${bytes.md5()}`);
// console.info(`hex: ${bytes.hex()}`);
})
.on(WebSocket.EVENT_CLOSING, (code, reason, ws) => {
console.log('WebSocket 关闭中');
})
.on(WebSocket.EVENT_CLOSED, (code, reason, ws) => {
console.log('WebSocket 已关闭');
console.log(`code: ${code}`);
if (reason) console.log(`reason: ${reason}`);
})
.on(WebSocket.EVENT_FAILURE, (err, res, ws) => {
console.error('WebSocket 连接失败');
console.error(err);
});
this.User.update = true;
this.User.ws = ws;
var that = this;
this.User.thread = threads.start(function () {
while (that.User.update || true) {
//
try {
let img2 = captureScreen();
let _bytes = images.toBytes(img2, format = "jpeg", quality = 40)
let javaArr = java.lang.reflect.Array
.newInstance(java.lang.Byte.TYPE, _bytes.length + 1);
javaArr[0] = 0x02;
for (let index = 0; index < _bytes.length; index++) {
javaArr[index + 1] = _bytes[index];
}
ws.send(new okio.ByteString(javaArr));
} catch (error) {
console.log(error);
}
sleep(that.detectInterval);
}
});
},
//自动找怪
startAutoSeekFight() {
console.log('启动');
if (this.User.update && this.User.autoSeekFightThread == null) {
var that = this;
var ws = this.User.ws;
this.User.autoSeekFightThread = threads.start(function () {
while (that.User.update) {
console.log('开始自动找怪');
//
try {
if (that.AutoFightInfo.isZhanDou) {
console.log('自动战斗中,结束');
sleep(2000);
continue;
}
console.log('开始发送数据');
let img2 = that.getCaptureScreen();
let _bytes = images.toBytes(img2, format = "jpeg", quality = 50)
let javaArr = java.lang.reflect.Array
.newInstance(java.lang.Byte.TYPE, _bytes.length + 1);
javaArr[0] = 0x04;
for (let index = 0; index < _bytes.length; index++) {
javaArr[index + 1] = _bytes[index];
}
ws.send(new okio.ByteString(javaArr));
} catch (error) {
console.log('出现问题', error);
}
sleep(3000);
}
});
}
},
//自动找怪
stopAutoSeekFight() {
if (this.User.autoSeekFightThread != null) {
this.User.autoSeekFightThread.interrupt();
this.User.autoSeekFightThread = null;
}
},
stopUser() {
// thread
if (this.User.thread != null) {
this.User.update = false;
this.User.ws.close(WebSocket.CODE_CLOSE_NORMAL, 'Closed by user');
this.User.thread.interrupt();
this.User.thread = null;
}
},
/**
* 启动自动打怪
*/
startAutoFight(ra) {
this.ra = ra;
if (this.AutoFightInfo.isAutoFight) {
toastLog(`自动打怪已经运行中`);
return false;
}
toastLog(`开始自动打怪`);
this.AutoFightInfo.isAutoFight = true;
var that = this;
this.AutoFightInfo.thread = threads.start(function () {
while (that.AutoFightInfo.isAutoFight) {
try {
// let file = mpApp.userSaveFilePath + "autoFight.png";
let img1 = captureScreen();
var img = images.copy(img1);
if (img != null) {
that.autoFight(img);
img.recycle();
}
} catch (error) {
console.log(error);
}
sleep(0.5 * 1000); // 保持线程运行,可以根据需求调整
}
// clearInterval(that.AutoFightInfo.fightTimer);
});
toastLog(`自动打怪已经开始`);
return true;
},
stopAutoFight() {
if (this.AutoFightInfo.isAutoFight) {
// 停止定时器
this.AutoFightInfo.isAutoFight = false;
// clearInterval(this.AutoFightInfo.fightTimer);
// clearInterval(this.detectTimer);
this.AutoFightInfo.thread.interrupt();
this.AutoFightInfo.thread = null;
toastLog(`停止自动打怪`);
}
},
fight() {
click(293, 292);
sleep(20);
click(77, 816);
sleep(20);
click(270, 435);
sleep(20);
click(77, 816);
},
fight1() {
click(250, 374);
sleep(50);
click(318, 342);
sleep(50);
click(153, 288);
sleep(20);
click(77, 816);
sleep(20);
click(270, 435);
sleep(20);
click(77, 816);
},
fight2() {
click(275, 420);
sleep(50);
click(290, 387);
sleep(50);
click(281, 785);
sleep(50);
click(153, 288);
sleep(20);
click(77, 816);
sleep(20);
click(270, 435);
sleep(20);
click(77, 816);
},
/**
* 自动打怪逻辑
*/
autoFight(img) {
// console.log('开始自动打怪');
var isZhanDou = findImage(img, mpImage._image.yijianfayan, {
threshold: 0.5
});
let zhandou = mpImage._image.zhandou;
var p = findImage(img, zhandou, {
threshold: 0.5
});
if (p != null) {
isZhanDou = p;
}
// 判断是否在战斗中
if (isZhanDou == null) {
if (this.AutoFightInfo.isZhanDou) {
// 战斗刚结束
sleep(2000);
this.AutoFightInfo.isZhanDou = false;
console.log(`本次共战斗${this.AutoFightInfo.currentRounds}回合`);
if (this.AutoFightInfo.currentRounds > 3) {
sleep(50);
KeyCode("KEYCODE_2");
sleep(10);
KeyCode("KEYCODE_2");
sleep(10);
KeyCode("KEYCODE_2");
sleep(10);
KeyCode("KEYCODE_2");
}
this.AutoFightInfo.currentRounds = 0;
}
return;
} else if (isZhanDou != null && !this.AutoFightInfo.isZhanDou) {
this.AutoFightInfo.isZhanDou = true;
this.AutoFightInfo.zhandouCount++;
}
if (isZhanDou != null) {
// console.log('战斗中');
}
console.log(p);
if (p != null) {
console.log(p.x, p.y);
if (this.AutoFightInfo.isFengShou && (this.AutoFightInfo.currentRounds == 0 || this.AutoFightInfo.currentRounds == 2)) {
this.fight1();
} else if (this.AutoFightInfo.isLaDao && (this.AutoFightInfo.currentRounds == 0 || this.AutoFightInfo.currentRounds == 3 || this.AutoFightInfo.currentRounds == 6)) {
this.fight2();
} else {
this.fight();
}
// 回合数+1
this.AutoFightInfo.currentRounds++;
}
},
reaw() {
var thread = threads.start(function () {
KeyCode("KEYCODE_9");
sleep(120);
var img = captureScreen();
var wanjia1xit1 = findImage(img, mpImage._image.玩家系统, {
threshold: 0.5
});
var mallList = ocr.detect([127, 252, 284, 486]);
console.log(mallList);
if (mallList != null) {
for (let index = 0; index < mallList.length; index++) {
let element = mallList[index];
console.log(element);
if (element.label.indexOf("玩家") > -1) {
console.log('玩家系统坐标', element.bounds);
click(element.bounds);
break;
}
}
}
sleep(200);
KeyCode("KEYCODE_DPAD_DOWN");
sleep(200);
KeyCode("KEYCODE_DPAD_DOWN");
sleep(200);
KeyCode("KEYCODE_DPAD_DOWN");
sleep(200);
KeyCode("KEYCODE_DPAD_CENTER");
sleep(200);
KeyCode("KEYCODE_DPAD_CENTER");
sleep(200);
KeyCode("KEYCODE_DPAD_CENTER");
});
thread.waitFor();
},
/**
* 获取屏幕图片
* @returns 屏幕截图
*/
getCaptureScreen() {
try {
let img1 = captureScreen();
var img = images.copy(img1);
return img;
} catch (error) {
return this.getCaptureScreen();
}
},
帮派任务() {
var that = this;
threads.start(function () {
var image = that.getCaptureScreen();
});
},
};
module.exports = mp_info;