refactor(CrystalflyTrap): 使用JSON替代txt文件 (#572)

This commit is contained in:
ddaodan
2025-04-14 22:10:41 +08:00
committed by GitHub
parent 9bf3b9fb4b
commit 9315626a5e
5 changed files with 48 additions and 25 deletions

View File

@@ -4,7 +4,6 @@
await genshin.returnMainUi();
let enable = settings.enable;
let type = settings.type;
let ore = settings.ore;
if (!enable) {
@@ -13,11 +12,31 @@
if (!ore) {
throw new Error("不是吧,哥们,你配置好了吗?用什么矿石啊?");
}
log.info(`当前动作: ${type}`);
// 读取状态数据,如果文件不存在则创建默认状态
let statusData = { isPlaced: false, collectionTime: 0 };
try {
const statusContent = file.readTextSync("status.json");
if (statusContent) {
statusData = JSON.parse(statusContent);
}
} catch (err) {
log.info("未找到状态文件或文件格式错误,创建默认状态");
await file.WriteTextSync("status.json", JSON.stringify(statusData, null, 2));
}
// 根据当前状态决定操作类型
const isPlaced = statusData.isPlaced;
const now = new Date().getTime();
const operationType = isPlaced ? "收集" : "放置";
log.info(`当前动作: ${operationType}`);
log.info("传送至枫丹冒险家协会锚点");
await genshin.tp(4509, 3631);
await sleep(1000);
if (type == "放置") {
if (!isPlaced) {
// 放置操作
log.info("放置晶蝶诱捕装置");
keyPress("B");
await sleep(1000);
@@ -80,21 +99,28 @@
log.error(`未找到${ore},请检查背包内是否拥有该矿石!`);
throw new Error(`未找到${ore}`);
}
// 更新状态信息
log.info(`当前时间: ${new Date().toLocaleString()}`);
let collectionTime = new Date().getTime() + 7 * 24 * 60 * 60 * 1000;
log.info(`领取时间: ${new Date(collectionTime).toLocaleString()}`);
await file.WriteTextSync("time.txt", collectionTime.toString());
log.info("时间保存成功");
} else if (type == "收集") {
const content = file.readTextSync("time.txt");
// 使用JSON格式保存状态
statusData.isPlaced = true;
statusData.collectionTime = collectionTime;
await file.WriteTextSync("status.json", JSON.stringify(statusData, null, 2));
log.info("状态保存成功");
} else {
// 收集操作
log.info(`读取领取时间成功`);
const readableTime = new Date(parseInt(content)).toLocaleString();
const readableTime = new Date(statusData.collectionTime).toLocaleString();
log.info(`领取时间: ${readableTime}`);
const now = new Date().getTime();
if (now < parseInt(content)) {
if (now < statusData.collectionTime) {
log.error("还没到领取时间");
throw new Error(`还没到领取时间,领取时间: ${readableTime}`);
}
let captureRegion = captureGameRegion();
let resList = captureRegion.findMulti(RecognitionObject.ocr(1210, 510, 210, 50));
for (let i = 0; i < resList.count; i++) {
@@ -106,11 +132,14 @@
}
log.info("收获晶蝶");
keyPress("F");
await file.WriteTextSync("time.txt", "");
// 更新状态
statusData.isPlaced = false;
statusData.collectionTime = 0;
await file.WriteTextSync("status.json", JSON.stringify(statusData, null, 2));
await sleep(3000);
keyPress("VK_ESCAPE");
} else {
throw new Error("不是吧,哥们,你配置好了吗?放置还是收集啊?");
}
} catch (e) {
log.error(`执行脚本时出错:${e}`, e.message);

View File

@@ -1,9 +1,9 @@
{
"manifest_version": 1,
"name": "晶蝶诱捕装置",
"version": "1.0",
"version": "2.0",
"bgi_version": "0.44.3",
"description": "自动放置和收集晶蝶诱捕装置。\n\n----------使用说明----------\n●BetterGI 版本必须大于0.44.3,低于此版本无法使用!\n●请在调度器内添加两次脚本,一个收集,一个放置。\n●请确保背包内有「晶蝶诱捕装置」。\n●晶蝶诱捕装置放在枫丹冒险家协会锚点。\n●请确保背包内所选择的矿石数量大于30个。\n●请勿删除\"time.txt\"文件,里面保存的是收集奖励的时间,删除后将无法通过脚本领取晶核!",
"description": "自动放置和收集晶蝶诱捕装置。\n\n----------使用说明----------\n●BetterGI 版本必须大于0.44.3,低于此版本无法使用!\n●请确保背包内有「晶蝶诱捕装置」。\n●晶蝶诱捕装置放在枫丹冒险家协会锚点。\n●请确保背包内所选择的矿石数量大于30个。\n●请勿修改\"status.json\"文件,里面保存的是收集奖励的时间,否则将无法通过脚本领取晶核!\n\n----------更新说明----------\n●2.0版本改用json储存时间如果你的晶核还没领取请打开脚本所在的文件夹复制\"time.txt\"中的所有内容,粘贴到\"status.json\"中\"collectionTime\": 后面要把原来的0删掉并将false改为true。",
"authors": [
{
"name": "ddaodan",

View File

@@ -4,15 +4,6 @@
"type": "checkbox",
"label": "是否启用\n防止不加调度器运行"
},
{
"name": "type",
"type": "select",
"label": "放置还是收集?",
"options": [
"放置",
"收集"
]
},
{
"name": "ore",
"type": "select",

View File

@@ -0,0 +1,4 @@
{
"isPlaced": false,
"collectionTime": 0
}

View File

@@ -1 +0,0 @@
1744916234293