From 82f7b40fa6f10c0a6b198eaa6b483a7e112996ef Mon Sep 17 00:00:00 2001 From: ftnfurina Date: Mon, 16 Jun 2025 00:54:16 +0800 Subject: [PATCH] =?UTF-8?q?JS=20=E8=84=9A=E6=9C=AC:=20=E5=8A=A0=E5=85=A5?= =?UTF-8?q?=E5=A5=BD=E5=8F=8B=E7=9A=84=E4=B8=96=E7=95=8C=20(#1108)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- repo/js/JoinFriendsWorld/README.md | 6 ++ repo/js/JoinFriendsWorld/index.js | 93 ++++++++++++++++++++++++++ repo/js/JoinFriendsWorld/manifest.json | 14 ++++ repo/js/JoinFriendsWorld/settings.json | 8 +++ 4 files changed, 121 insertions(+) create mode 100644 repo/js/JoinFriendsWorld/README.md create mode 100644 repo/js/JoinFriendsWorld/index.js create mode 100644 repo/js/JoinFriendsWorld/manifest.json create mode 100644 repo/js/JoinFriendsWorld/settings.json diff --git a/repo/js/JoinFriendsWorld/README.md b/repo/js/JoinFriendsWorld/README.md new file mode 100644 index 00000000..9a0d411f --- /dev/null +++ b/repo/js/JoinFriendsWorld/README.md @@ -0,0 +1,6 @@ +# 加入好友的世界 + +## 使用说明 + +1. 修改自定义配置,将好友的 UID 添加到配置中 +2. 执行脚本即可 diff --git a/repo/js/JoinFriendsWorld/index.js b/repo/js/JoinFriendsWorld/index.js new file mode 100644 index 00000000..244c4521 --- /dev/null +++ b/repo/js/JoinFriendsWorld/index.js @@ -0,0 +1,93 @@ +// 等待好友确认超时时间 25s +const WAIT_FRIEND_CONFIRM_TIMEOUT = 25 * 1000; +/** + * 创建自适应 16:9 缩放的 OCR 对象 + * @param x X 坐标 + * @param y Y 坐标 + * @param w 宽度 + * @param h 高度 + */ +function createAutoZoomOcr(x, y, w, h) { + const ratio = genshin.scaleTo1080PRatio; + return RecognitionObject.ocr(x * ratio, y * ratio, w * ratio, h * ratio); +} +/** + * 是否在多人游戏中 + */ +function inMultiplayerGame() { + const gameRegion = captureGameRegion(); + const playerCountRegin = gameRegion.find(createAutoZoomOcr(340, 18, 53, 53)); + const playerCountText = playerCountRegin.text.trim().toLocaleLowerCase(); + return playerCountText.includes('p'); +} +(async () => { + try { + const uid = settings.uid?.trim(); + if (!uid) { + throw new Error('UID 不能为空'); + } + if (inMultiplayerGame()) { + throw new Error('正在多人游戏中,无法加入好友世界'); + } + await genshin.returnMainUi(); + log.info(`尝试加入好友世界(UID: ${uid})`); + // 打开好友列表 + keyPress('VK_O'); + await sleep(1000); + // 打开添加好友 + click(960, 50); + await sleep(1000); + // 点击输入框,输入 UID,点击搜索 + click(960, 150); + inputText(uid); + click(1680, 150); + await sleep(500); + const gameRegion = captureGameRegion(); + // 尝试找到用户卡片的冒险等阶 + const levelRegin = gameRegion.find(createAutoZoomOcr(425, 445, 106, 37)); + const levelText = levelRegin.text.trim(); + if (!levelText.includes('冒险等阶')) { + // 判断是否搜索的用户是否是自己 + const yourselfRegin = gameRegion.find(createAutoZoomOcr(660, 495, 601, 88)); + if (yourselfRegin.text.includes('其他玩家')) { + throw new Error('不能使用自己的UID'); + } + throw new Error('UID不存在'); + } + const joinOrAddRegin = gameRegion.find(createAutoZoomOcr(1160, 800, 200, 54)); + const joinOrAddText = joinOrAddRegin.text.trim(); + if (joinOrAddText === '') { + throw new Error("你的好友不在线"); + } + else if (joinOrAddText === '申请加入') { + log.info(`已经发起加入申请,等待好友同意`); + joinOrAddRegin.click(); + } + else { + throw new Error('TA不是你的好友'); + } + const startTime = new Date().getTime(); + const timeout = WAIT_FRIEND_CONFIRM_TIMEOUT + startTime; + while (true) { + // 等待好友回复 + const gameRegion = captureGameRegion(); + const requestRegin = gameRegion.find(createAutoZoomOcr(725, 195, 465, 45)); + const requestText = requestRegin.text.trim(); + if (requestText.endsWith('拒绝了多人游戏申请')) { + throw new Error('好友拒绝了多人游戏'); + } + if (inMultiplayerGame()) { + log.info('成功加入好友世界'); + break; + } + if (new Date().getTime() > timeout) { + throw new Error('请求超时'); + } + await sleep(50); + } + } + catch (error) { + log.error(error.message); + await genshin.returnMainUi(); + } +})(); diff --git a/repo/js/JoinFriendsWorld/manifest.json b/repo/js/JoinFriendsWorld/manifest.json new file mode 100644 index 00000000..02a1e63e --- /dev/null +++ b/repo/js/JoinFriendsWorld/manifest.json @@ -0,0 +1,14 @@ +{ + "manifest_version": 1, + "name": "加入好友的世界", + "description": "快让我访问!", + "version": "0.0.1", + "main": "index.js", + "settings_ui": "settings.json", + "authors": [ + { + "name": "ftnfurina", + "link": "https://github.com/ftnfurina" + } + ] +} \ No newline at end of file diff --git a/repo/js/JoinFriendsWorld/settings.json b/repo/js/JoinFriendsWorld/settings.json new file mode 100644 index 00000000..b738f217 --- /dev/null +++ b/repo/js/JoinFriendsWorld/settings.json @@ -0,0 +1,8 @@ +[ + { + "name": "uid", + "type": "input-text", + "label": "好友UID", + "default": "" + } +]