@bettergi/utils 0.0.10 → 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +32 -42
- package/dist/asserts.d.ts +57 -0
- package/dist/asserts.js +91 -0
- package/dist/flow.d.ts +20 -0
- package/dist/flow.js +33 -1
- package/dist/game.d.ts +35 -22
- package/dist/game.js +92 -64
- package/dist/http.js +2 -2
- package/dist/index.d.ts +2 -1
- package/dist/index.js +2 -1
- package/dist/mouse.d.ts +48 -22
- package/dist/mouse.js +78 -38
- package/dist/ocr.d.ts +44 -34
- package/dist/ocr.js +40 -43
- package/dist/store.d.ts +1 -0
- package/dist/store.js +38 -6
- package/dist/workflow.d.ts +52 -0
- package/dist/workflow.js +72 -0
- package/package.json +3 -3
package/README.md
CHANGED
|
@@ -21,8 +21,6 @@ pnpm install @bettergi/utils
|
|
|
21
21
|
> 常见游戏内操作封装,省去手动实现的繁琐。
|
|
22
22
|
|
|
23
23
|
```ts
|
|
24
|
-
import { openMenu, openMenuPage, openPaimonMenu, setTime } from "@bettergi/utils";
|
|
25
|
-
|
|
26
24
|
// 打开派蒙菜单
|
|
27
25
|
await openPaimonMenu();
|
|
28
26
|
|
|
@@ -32,7 +30,10 @@ await openMenu("邮件");
|
|
|
32
30
|
// 打开菜单页面
|
|
33
31
|
await openMenuPage("问卷");
|
|
34
32
|
|
|
35
|
-
//
|
|
33
|
+
// 调整游戏到指定时间
|
|
34
|
+
await setTimeTo(12, 35);
|
|
35
|
+
|
|
36
|
+
// 调整游戏时间段
|
|
36
37
|
await setTime("evening");
|
|
37
38
|
```
|
|
38
39
|
|
|
@@ -41,16 +42,6 @@ await setTime("evening");
|
|
|
41
42
|
> 对 RecognitionObject 代码的封装,对于简单的找图、找字操作,不再需要编写复杂的代码。
|
|
42
43
|
|
|
43
44
|
```ts
|
|
44
|
-
import {
|
|
45
|
-
findImage,
|
|
46
|
-
findImageInDirection,
|
|
47
|
-
findImageWithinBounds,
|
|
48
|
-
findText,
|
|
49
|
-
findTextInDirection,
|
|
50
|
-
findTextWithinBounds
|
|
51
|
-
} from "@bettergi/utils";
|
|
52
|
-
|
|
53
|
-
// 在整个画面内搜索图片,找不到返回 undefined
|
|
54
45
|
const i1 = findImage("assets/关闭.png");
|
|
55
46
|
|
|
56
47
|
// 在指定方向上搜索图片,找不到返回 undefined
|
|
@@ -60,13 +51,13 @@ const i2 = findImageInDirection("assets/关闭.png", "north-east");
|
|
|
60
51
|
const i3 = findImageWithinBounds("assets/关闭.png", 960, 0, 960, 1080);
|
|
61
52
|
|
|
62
53
|
// 在整个画面内搜索文本(不包含、忽略大小写),找不到返回 undefined
|
|
63
|
-
const t1 = findText("购买"
|
|
54
|
+
const t1 = findText("购买");
|
|
64
55
|
|
|
65
56
|
// 在指定方向上搜索文本(包含、忽略大小写),找不到返回 undefined
|
|
66
|
-
const t2 = findTextInDirection("师傅",
|
|
57
|
+
const t2 = findTextInDirection("师傅", "east", { contains: true, ignoreCase: true });
|
|
67
58
|
|
|
68
59
|
// 在指定区域内搜索文本(不包含、忽略大小写),找不到返回 undefined
|
|
69
|
-
const t3 = findTextWithinBounds("确认",
|
|
60
|
+
const t3 = findTextWithinBounds("确认", 960, 540, 960, 540);
|
|
70
61
|
```
|
|
71
62
|
|
|
72
63
|
### 行为流程
|
|
@@ -74,16 +65,28 @@ const t3 = findTextWithinBounds("确认", false, true, 960, 540, 960, 540);
|
|
|
74
65
|
> 对脚本开发过程中常见工作流的抽象,例如:等待 XXX 完成/出现/消失。
|
|
75
66
|
|
|
76
67
|
```ts
|
|
77
|
-
import { findImageInDirection, waitUntil } from "@bettergi/utils";
|
|
78
|
-
|
|
79
68
|
// 等待直到找不到[关闭按钮] 或 5秒后超时,每隔1秒检查一次,期间按 Esc 键
|
|
80
|
-
const done = await
|
|
69
|
+
const done = await waitForAction(
|
|
81
70
|
() => findImageInDirection("assets/关闭.png", "north-east") === undefined,
|
|
82
|
-
|
|
83
|
-
1000
|
|
84
|
-
() => keyPress("ESCAPE")
|
|
71
|
+
() => keyPress("ESCAPE"),
|
|
72
|
+
{ maxAttempts: 5, retryInterval: 1000 }
|
|
85
73
|
);
|
|
86
74
|
if (!done) throw new Error("关闭页面超时");
|
|
75
|
+
|
|
76
|
+
// 断言 "世界等级" 区域即将出现 或 5秒后超时抛出异常,每隔1秒检查一次,期间按 Esc 键
|
|
77
|
+
await assertRegionAppearing(
|
|
78
|
+
() => findTextInDirection("世界等级", "north-west"),
|
|
79
|
+
"打开派蒙菜单超时",
|
|
80
|
+
() => keyPress("ESCAPE"),
|
|
81
|
+
{ maxAttempts: 5, retryInterval: 1000 }
|
|
82
|
+
);
|
|
83
|
+
|
|
84
|
+
// 断言 "购买" 区域不存在 或 5秒后超时抛出异常,每隔1秒检查一次,期间如果存在 "购买" 按钮则点击
|
|
85
|
+
const findButton = () => findTextWithinBounds("购买", 500, 740, 900, 110);
|
|
86
|
+
await assertRegionDisappearing(findButton, "点击购买按钮超时", () => findButton()?.click(), {
|
|
87
|
+
maxAttempts: 5,
|
|
88
|
+
retryInterval: 1000
|
|
89
|
+
});
|
|
87
90
|
```
|
|
88
91
|
|
|
89
92
|
### 鼠标操作
|
|
@@ -91,15 +94,11 @@ if (!done) throw new Error("关闭页面超时");
|
|
|
91
94
|
> 对常见鼠标操作的封装,如鼠标滚动、拖拽等。
|
|
92
95
|
|
|
93
96
|
```ts
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
mouseSlide,
|
|
100
|
-
mouseSlideX,
|
|
101
|
-
mouseSlideY
|
|
102
|
-
} from "@bettergi/utils";
|
|
97
|
+
// 鼠标从 (745, 610) 平滑自然地移动 (1920, 1080)
|
|
98
|
+
await naturalMouseMove(0, 0, 1920, 1080);
|
|
99
|
+
|
|
100
|
+
// 鼠标从 (745, 610) 拖拽到 (1280, 610)
|
|
101
|
+
await mouseDrag(745, 610, 1280, 610);
|
|
103
102
|
|
|
104
103
|
// 鼠标滚轮向上滚动 175 像素
|
|
105
104
|
await mouseScrollUp(175);
|
|
@@ -112,15 +111,6 @@ await mouseScrollUpLines(99);
|
|
|
112
111
|
|
|
113
112
|
// 鼠标滚轮向下滚动 1 行,行高 115(自定义:商店物品行高)
|
|
114
113
|
await mouseScrollDownLines(1, 115);
|
|
115
|
-
|
|
116
|
-
// 鼠标从 (745, 610) 拖拽到 (1280, 610)
|
|
117
|
-
await mouseSlide(745, 610, 1280, 610);
|
|
118
|
-
|
|
119
|
-
// 鼠标从 (745, 610) 向右拖拽 435 像素
|
|
120
|
-
await mouseSlideX(745, 610, 435);
|
|
121
|
-
|
|
122
|
-
// 鼠标从 (1290, 140) 向下拖拽 175 像素
|
|
123
|
-
await mouseSlideY(1290, 140, 175);
|
|
124
114
|
```
|
|
125
115
|
|
|
126
116
|
### 数据存储
|
|
@@ -154,8 +144,8 @@ import { getForBody, postForBody } from "@bettergi/utils";
|
|
|
154
144
|
|
|
155
145
|
// 发送 GET 请求获取响应体内容
|
|
156
146
|
// 提示:需要在 `manifest.json` 文件中配置 `http_allowed_urls`,并在 调度器 -> 修改通用配置 中启用
|
|
157
|
-
const body1 = await getForBody("https://example.com/",
|
|
158
|
-
const body2 = await postForBody("https://example.com/",
|
|
147
|
+
const body1 = await getForBody("https://example.com/", undefined, { "User-Agent": "BetterGI" });
|
|
148
|
+
const body2 = await postForBody("https://example.com/", undefined, { "User-Agent": "BetterGI" });
|
|
159
149
|
log.info(`GET 请求响应体内容${body1}`);
|
|
160
150
|
log.info(`POST 请求响应体内容${body2}`);
|
|
161
151
|
```
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
import { type Action, type RetryOptions } from "./workflow";
|
|
2
|
+
/**
|
|
3
|
+
* 断言某个区域当前存在,否则抛出异常
|
|
4
|
+
* @param regionProvider 返回区域的函数
|
|
5
|
+
* @param message 错误信息
|
|
6
|
+
*/
|
|
7
|
+
export declare const assertRegionPresent: (regionProvider: () => Region | null | undefined, message: string) => Promise<void>;
|
|
8
|
+
/**
|
|
9
|
+
* 断言某个区域当前不存在,否则抛出异常
|
|
10
|
+
* @param regionProvider 返回区域的函数
|
|
11
|
+
* @param message 错误信息
|
|
12
|
+
*/
|
|
13
|
+
export declare const assertRegionAbsent: (regionProvider: () => Region | null | undefined, message: string) => Promise<void>;
|
|
14
|
+
/**
|
|
15
|
+
* 断言整个画面上某个元素当前存在,否则抛出异常
|
|
16
|
+
* @param recognitionObject 识别对象
|
|
17
|
+
* @param message 错误信息
|
|
18
|
+
*/
|
|
19
|
+
export declare const assertElementPresent: (recognitionObject: RecognitionObject, message: string) => Promise<void>;
|
|
20
|
+
/**
|
|
21
|
+
* 断言整个画面上某个元素当前不存在,否则抛出异常
|
|
22
|
+
* @param recognitionObject 识别对象
|
|
23
|
+
* @param message 错误信息
|
|
24
|
+
*/
|
|
25
|
+
export declare const assertElementAbsent: (recognitionObject: RecognitionObject, message: string) => Promise<void>;
|
|
26
|
+
/**
|
|
27
|
+
* 断言某个区域即将出现,否则抛出异常
|
|
28
|
+
* @param regionProvider 返回区域的函数
|
|
29
|
+
* @param message 错误信息
|
|
30
|
+
* @param retryAction 每次重试时执行的操作(可选)
|
|
31
|
+
* @param options 配置选项
|
|
32
|
+
*/
|
|
33
|
+
export declare const assertRegionAppearing: (regionProvider: () => Region | null | undefined, message: string, retryAction?: Action, options?: RetryOptions) => Promise<void>;
|
|
34
|
+
/**
|
|
35
|
+
* 断言某个区域即将消失,否则抛出异常
|
|
36
|
+
* @param regionProvider 返回区域的函数
|
|
37
|
+
* @param message 错误信息
|
|
38
|
+
* @param retryAction 每次重试时执行的操作(可选)
|
|
39
|
+
* @param options 配置选项
|
|
40
|
+
*/
|
|
41
|
+
export declare const assertRegionDisappearing: (regionProvider: () => Region | null | undefined, message: string, retryAction?: Action, options?: RetryOptions) => Promise<void>;
|
|
42
|
+
/**
|
|
43
|
+
* 断言整个画面上某个元素即将出现,否则抛出异常
|
|
44
|
+
* @param recognitionObject 识别对象
|
|
45
|
+
* @param message 错误信息
|
|
46
|
+
* @param retryAction 每次重试时执行的操作(可选)
|
|
47
|
+
* @param options 配置选项
|
|
48
|
+
*/
|
|
49
|
+
export declare const assertElementAppearing: (recognitionObject: RecognitionObject, message: string, retryAction?: Action, options?: RetryOptions) => Promise<void>;
|
|
50
|
+
/**
|
|
51
|
+
* 断言整个画面上某个元素即将消失,否则抛出异常
|
|
52
|
+
* @param recognitionObject 识别对象
|
|
53
|
+
* @param message 错误信息
|
|
54
|
+
* @param retryAction 每次重试时执行的操作(可选)
|
|
55
|
+
* @param options 配置选项
|
|
56
|
+
*/
|
|
57
|
+
export declare const assertElementDisappearing: (recognitionObject: RecognitionObject, message: string, retryAction?: Action, options?: RetryOptions) => Promise<void>;
|
package/dist/asserts.js
ADDED
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
import { waitForElementAppear, waitForElementDisappear, waitForRegionAppear, waitForRegionDisappear } from "./workflow";
|
|
2
|
+
/**
|
|
3
|
+
* 断言某个区域当前存在,否则抛出异常
|
|
4
|
+
* @param regionProvider 返回区域的函数
|
|
5
|
+
* @param message 错误信息
|
|
6
|
+
*/
|
|
7
|
+
export const assertRegionPresent = async (regionProvider, message) => {
|
|
8
|
+
const region = regionProvider();
|
|
9
|
+
if (region == null || !region.isExist()) {
|
|
10
|
+
throw new Error(message);
|
|
11
|
+
}
|
|
12
|
+
};
|
|
13
|
+
/**
|
|
14
|
+
* 断言某个区域当前不存在,否则抛出异常
|
|
15
|
+
* @param regionProvider 返回区域的函数
|
|
16
|
+
* @param message 错误信息
|
|
17
|
+
*/
|
|
18
|
+
export const assertRegionAbsent = async (regionProvider, message) => {
|
|
19
|
+
const region = regionProvider();
|
|
20
|
+
if (region != null && region.isExist()) {
|
|
21
|
+
throw new Error(message);
|
|
22
|
+
}
|
|
23
|
+
};
|
|
24
|
+
/**
|
|
25
|
+
* 断言整个画面上某个元素当前存在,否则抛出异常
|
|
26
|
+
* @param recognitionObject 识别对象
|
|
27
|
+
* @param message 错误信息
|
|
28
|
+
*/
|
|
29
|
+
export const assertElementPresent = async (recognitionObject, message) => {
|
|
30
|
+
return assertRegionPresent(() => captureGameRegion().find(recognitionObject), message);
|
|
31
|
+
};
|
|
32
|
+
/**
|
|
33
|
+
* 断言整个画面上某个元素当前不存在,否则抛出异常
|
|
34
|
+
* @param recognitionObject 识别对象
|
|
35
|
+
* @param message 错误信息
|
|
36
|
+
*/
|
|
37
|
+
export const assertElementAbsent = async (recognitionObject, message) => {
|
|
38
|
+
return assertRegionAbsent(() => captureGameRegion().find(recognitionObject), message);
|
|
39
|
+
};
|
|
40
|
+
/**
|
|
41
|
+
* 断言某个区域即将出现,否则抛出异常
|
|
42
|
+
* @param regionProvider 返回区域的函数
|
|
43
|
+
* @param message 错误信息
|
|
44
|
+
* @param retryAction 每次重试时执行的操作(可选)
|
|
45
|
+
* @param options 配置选项
|
|
46
|
+
*/
|
|
47
|
+
export const assertRegionAppearing = async (regionProvider, message, retryAction, options) => {
|
|
48
|
+
const isAppeared = await waitForRegionAppear(regionProvider, retryAction, options);
|
|
49
|
+
if (!isAppeared) {
|
|
50
|
+
throw new Error(message);
|
|
51
|
+
}
|
|
52
|
+
};
|
|
53
|
+
/**
|
|
54
|
+
* 断言某个区域即将消失,否则抛出异常
|
|
55
|
+
* @param regionProvider 返回区域的函数
|
|
56
|
+
* @param message 错误信息
|
|
57
|
+
* @param retryAction 每次重试时执行的操作(可选)
|
|
58
|
+
* @param options 配置选项
|
|
59
|
+
*/
|
|
60
|
+
export const assertRegionDisappearing = async (regionProvider, message, retryAction, options) => {
|
|
61
|
+
const isDisappeared = await waitForRegionDisappear(regionProvider, retryAction, options);
|
|
62
|
+
if (!isDisappeared) {
|
|
63
|
+
throw new Error(message);
|
|
64
|
+
}
|
|
65
|
+
};
|
|
66
|
+
/**
|
|
67
|
+
* 断言整个画面上某个元素即将出现,否则抛出异常
|
|
68
|
+
* @param recognitionObject 识别对象
|
|
69
|
+
* @param message 错误信息
|
|
70
|
+
* @param retryAction 每次重试时执行的操作(可选)
|
|
71
|
+
* @param options 配置选项
|
|
72
|
+
*/
|
|
73
|
+
export const assertElementAppearing = async (recognitionObject, message, retryAction, options) => {
|
|
74
|
+
const isAppeared = await waitForElementAppear(recognitionObject, retryAction, options);
|
|
75
|
+
if (!isAppeared) {
|
|
76
|
+
throw new Error(message);
|
|
77
|
+
}
|
|
78
|
+
};
|
|
79
|
+
/**
|
|
80
|
+
* 断言整个画面上某个元素即将消失,否则抛出异常
|
|
81
|
+
* @param recognitionObject 识别对象
|
|
82
|
+
* @param message 错误信息
|
|
83
|
+
* @param retryAction 每次重试时执行的操作(可选)
|
|
84
|
+
* @param options 配置选项
|
|
85
|
+
*/
|
|
86
|
+
export const assertElementDisappearing = async (recognitionObject, message, retryAction, options) => {
|
|
87
|
+
const isDisappeared = await waitForElementDisappear(recognitionObject, retryAction, options);
|
|
88
|
+
if (!isDisappeared) {
|
|
89
|
+
throw new Error(message);
|
|
90
|
+
}
|
|
91
|
+
};
|
package/dist/flow.d.ts
CHANGED
|
@@ -8,3 +8,23 @@
|
|
|
8
8
|
* - false 在超时后条件仍未满足
|
|
9
9
|
*/
|
|
10
10
|
export declare const waitUntil: (condition: (context: Record<string, any>) => boolean, timeout?: number, interval?: number, action?: (context: Record<string, any>) => Promise<void> | void) => Promise<boolean>;
|
|
11
|
+
/**
|
|
12
|
+
* 断言区域存在
|
|
13
|
+
* @param 获取区域的函数
|
|
14
|
+
* @param message 错误信息
|
|
15
|
+
* @param timeout 超时时间(毫秒),默认 3000 毫秒
|
|
16
|
+
* @param interval 等待间隔(毫秒),默认 300 毫秒
|
|
17
|
+
* @param action 每次等待循环中执行的操作(可选)
|
|
18
|
+
* @throws 如果区域在超时时间内未找到则抛出错误
|
|
19
|
+
*/
|
|
20
|
+
export declare const assertExists: (regionProvider: () => Region | undefined, message?: string, timeout?: number, interval?: number, action?: (context: Record<string, any>) => Promise<void> | void) => Promise<void>;
|
|
21
|
+
/**
|
|
22
|
+
* 断言区域不存在
|
|
23
|
+
* @param 获取区域的函数
|
|
24
|
+
* @param message 错误信息
|
|
25
|
+
* @param timeout 超时时间(毫秒),默认 3000 毫秒
|
|
26
|
+
* @param interval 等待间隔(毫秒),默认 300 毫秒
|
|
27
|
+
* @param action 每次等待循环中执行的操作(可选)
|
|
28
|
+
* @throws 如果区域在超时时间内仍然存在则抛出错误
|
|
29
|
+
*/
|
|
30
|
+
export declare const assertNotExists: (regionProvider: () => Region | undefined, message?: string, timeout?: number, interval?: number, action?: (context: Record<string, any>) => Promise<void> | void) => Promise<void>;
|
package/dist/flow.js
CHANGED
|
@@ -1,3 +1,7 @@
|
|
|
1
|
+
/** 默认超时时间(毫秒) */
|
|
2
|
+
const defaultTimeout = 3 * 1000;
|
|
3
|
+
/** 默认等待间隔(毫秒) */
|
|
4
|
+
const defaultInterval = 300;
|
|
1
5
|
/**
|
|
2
6
|
* 等待直到条件满足或超时
|
|
3
7
|
* @param condition 等待的条件判断函数,返回 true 表示条件满足
|
|
@@ -7,7 +11,7 @@
|
|
|
7
11
|
* @returns - true 在超时前条件已满足
|
|
8
12
|
* - false 在超时后条件仍未满足
|
|
9
13
|
*/
|
|
10
|
-
export const waitUntil = async (condition, timeout =
|
|
14
|
+
export const waitUntil = async (condition, timeout = defaultTimeout, interval = defaultInterval, action) => {
|
|
11
15
|
const context = {};
|
|
12
16
|
const deadline = Date.now() + timeout;
|
|
13
17
|
while (Date.now() < deadline) {
|
|
@@ -18,3 +22,31 @@ export const waitUntil = async (condition, timeout = 3000, interval = 300, actio
|
|
|
18
22
|
}
|
|
19
23
|
return false;
|
|
20
24
|
};
|
|
25
|
+
/**
|
|
26
|
+
* 断言区域存在
|
|
27
|
+
* @param 获取区域的函数
|
|
28
|
+
* @param message 错误信息
|
|
29
|
+
* @param timeout 超时时间(毫秒),默认 3000 毫秒
|
|
30
|
+
* @param interval 等待间隔(毫秒),默认 300 毫秒
|
|
31
|
+
* @param action 每次等待循环中执行的操作(可选)
|
|
32
|
+
* @throws 如果区域在超时时间内未找到则抛出错误
|
|
33
|
+
*/
|
|
34
|
+
export const assertExists = async (regionProvider, message = "断言区域存在失败", timeout = defaultTimeout, interval = defaultInterval, action) => {
|
|
35
|
+
const ok = await waitUntil(() => regionProvider() !== undefined, timeout, interval, action);
|
|
36
|
+
if (!ok)
|
|
37
|
+
throw new Error(message);
|
|
38
|
+
};
|
|
39
|
+
/**
|
|
40
|
+
* 断言区域不存在
|
|
41
|
+
* @param 获取区域的函数
|
|
42
|
+
* @param message 错误信息
|
|
43
|
+
* @param timeout 超时时间(毫秒),默认 3000 毫秒
|
|
44
|
+
* @param interval 等待间隔(毫秒),默认 300 毫秒
|
|
45
|
+
* @param action 每次等待循环中执行的操作(可选)
|
|
46
|
+
* @throws 如果区域在超时时间内仍然存在则抛出错误
|
|
47
|
+
*/
|
|
48
|
+
export const assertNotExists = async (regionProvider, message = "断言区域不存在失败", timeout = defaultTimeout, interval = defaultInterval, action) => {
|
|
49
|
+
const ok = await waitUntil(() => regionProvider() === undefined, timeout, interval, action);
|
|
50
|
+
if (!ok)
|
|
51
|
+
throw new Error(message);
|
|
52
|
+
};
|
package/dist/game.d.ts
CHANGED
|
@@ -1,3 +1,13 @@
|
|
|
1
|
+
import { ListView } from "./ocr";
|
|
2
|
+
/**
|
|
3
|
+
* 临时设置游戏分辨率和DPI缩放比例,执行指定动作后恢复
|
|
4
|
+
* 适用于使用了鼠标移动的操作,保证在不同的分辨率和DPI下都能正确地复现鼠标操作
|
|
5
|
+
* @param w 游戏宽度
|
|
6
|
+
* @param y 游戏高度
|
|
7
|
+
* @param dpi 系统屏幕的DPI缩放比例
|
|
8
|
+
* @param action 执行动作
|
|
9
|
+
*/
|
|
10
|
+
export declare const withGameMetrics: <T>(w: number, y: number, dpi: number, action: () => Promise<T> | T) => Promise<T>;
|
|
1
11
|
/**
|
|
2
12
|
* 打开派蒙菜单
|
|
3
13
|
*/
|
|
@@ -5,34 +15,37 @@ export declare const openPaimonMenu: () => Promise<void>;
|
|
|
5
15
|
/**
|
|
6
16
|
* 打开游戏菜单(左侧按钮)
|
|
7
17
|
* @param name 菜单名称
|
|
8
|
-
* @param
|
|
9
|
-
* @param
|
|
18
|
+
* @param stepBackwards 是否反向步进(可选)
|
|
19
|
+
* @param options 搜索参数(可选)
|
|
10
20
|
*/
|
|
11
|
-
export declare const openMenu: (name: "\u62CD\u7167" | "\u516C\u544A" | "\u90AE\u4EF6" | "\u65F6\u95F4" | "\u8BBE\u7F6E" | (string & {}),
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
timeout?: number;
|
|
21
|
+
export declare const openMenu: (name: "\u62CD\u7167" | "\u516C\u544A" | "\u90AE\u4EF6" | "\u65F6\u95F4" | "\u8BBE\u7F6E" | (string & {}), stepBackwards?: boolean, options?: {
|
|
22
|
+
navWidth?: number;
|
|
23
|
+
steps?: number;
|
|
15
24
|
}) => Promise<void>;
|
|
16
25
|
/**
|
|
17
26
|
* 打开游戏菜单页面(菜单按钮列表)
|
|
18
27
|
* @param name 菜单页面名称
|
|
19
|
-
* @param
|
|
20
|
-
*/
|
|
21
|
-
export declare const openMenuPage: (name: string, config?: {
|
|
22
|
-
x?: number;
|
|
23
|
-
y?: number;
|
|
24
|
-
w?: number;
|
|
25
|
-
h?: number;
|
|
26
|
-
lineHeight?: number;
|
|
27
|
-
}) => Promise<void>;
|
|
28
|
-
/**
|
|
29
|
-
* 调整游戏时间
|
|
30
|
-
* @param period 时间段
|
|
31
|
-
* @param config 时钟参数
|
|
28
|
+
* @param listView 菜单页面视图参数
|
|
32
29
|
*/
|
|
33
|
-
export declare const
|
|
30
|
+
export declare const openMenuPage: (name: string, listView?: ListView) => Promise<void>;
|
|
31
|
+
type ClockOptions = {
|
|
34
32
|
centerX?: number;
|
|
35
33
|
centerY?: number;
|
|
34
|
+
offsetHours?: number;
|
|
36
35
|
radius?: number;
|
|
37
|
-
|
|
38
|
-
}
|
|
36
|
+
smooth?: number;
|
|
37
|
+
};
|
|
38
|
+
/**
|
|
39
|
+
* 调整游戏时间到指定时分
|
|
40
|
+
* @param hour 小时
|
|
41
|
+
* @param minute 分钟
|
|
42
|
+
* @param options 时钟参数
|
|
43
|
+
*/
|
|
44
|
+
export declare const setTimeTo: (hour: number, minute: number, options?: ClockOptions) => Promise<void>;
|
|
45
|
+
/**
|
|
46
|
+
* 调整游戏时间到指定时间段
|
|
47
|
+
* @param period 时间段
|
|
48
|
+
* @param options 时钟参数
|
|
49
|
+
*/
|
|
50
|
+
export declare const setTime: (period: "night" | "morning" | "noon" | "evening", options?: ClockOptions) => Promise<void>;
|
|
51
|
+
export {};
|
package/dist/game.js
CHANGED
|
@@ -1,6 +1,24 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
1
|
+
import { assertRegionAppearing, assertRegionDisappearing } from "./asserts";
|
|
2
|
+
import { mouseMoveAlongWaypoints } from "./mouse";
|
|
3
3
|
import { findTextInDirection, findTextWithinBounds, findTextWithinListView } from "./ocr";
|
|
4
|
+
/**
|
|
5
|
+
* 临时设置游戏分辨率和DPI缩放比例,执行指定动作后恢复
|
|
6
|
+
* 适用于使用了鼠标移动的操作,保证在不同的分辨率和DPI下都能正确地复现鼠标操作
|
|
7
|
+
* @param w 游戏宽度
|
|
8
|
+
* @param y 游戏高度
|
|
9
|
+
* @param dpi 系统屏幕的DPI缩放比例
|
|
10
|
+
* @param action 执行动作
|
|
11
|
+
*/
|
|
12
|
+
export const withGameMetrics = async (w, y, dpi, action) => {
|
|
13
|
+
const { width, height, screenDpiScale } = genshin;
|
|
14
|
+
try {
|
|
15
|
+
setGameMetrics(w, y, dpi);
|
|
16
|
+
return await action();
|
|
17
|
+
}
|
|
18
|
+
finally {
|
|
19
|
+
setGameMetrics(width, height, screenDpiScale);
|
|
20
|
+
}
|
|
21
|
+
};
|
|
4
22
|
/**
|
|
5
23
|
* 打开派蒙菜单
|
|
6
24
|
*/
|
|
@@ -8,40 +26,37 @@ export const openPaimonMenu = async () => {
|
|
|
8
26
|
// 1.返回主界面
|
|
9
27
|
await genshin.returnMainUi();
|
|
10
28
|
// 2.打开派蒙菜单
|
|
11
|
-
|
|
12
|
-
const ok = await waitUntil(() => findWorldLevel() !== undefined, 5000, 1000, () => keyPress("ESCAPE"));
|
|
13
|
-
if (!ok)
|
|
14
|
-
throw new Error("打开派蒙菜单超时");
|
|
29
|
+
await assertRegionAppearing(() => findTextWithinBounds("世界等级", 300, 230, 440, 100), "打开派蒙菜单超时", () => keyPress("ESCAPE"));
|
|
15
30
|
};
|
|
16
31
|
/**
|
|
17
32
|
* 打开游戏菜单(左侧按钮)
|
|
18
33
|
* @param name 菜单名称
|
|
19
|
-
* @param
|
|
20
|
-
* @param
|
|
34
|
+
* @param stepBackwards 是否反向步进(可选)
|
|
35
|
+
* @param options 搜索参数(可选)
|
|
21
36
|
*/
|
|
22
|
-
export const openMenu = async (name,
|
|
37
|
+
export const openMenu = async (name, stepBackwards = false, options = {}) => {
|
|
23
38
|
// 1.打开派蒙菜单
|
|
24
39
|
await openPaimonMenu();
|
|
25
|
-
// 2
|
|
26
|
-
const {
|
|
27
|
-
const findTooltip = () => findTextWithinBounds(name,
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
40
|
+
// 2.步进搜索菜单按钮
|
|
41
|
+
const { navWidth = 95, steps: step = 30 } = options;
|
|
42
|
+
const findTooltip = () => findTextWithinBounds(name, navWidth, 0, 20 + name.length * 20, genshin.height);
|
|
43
|
+
const tooltip = await withGameMetrics(1920, 1080, 1.5, async () => {
|
|
44
|
+
let result = undefined;
|
|
45
|
+
const steps = Math.ceil(genshin.height / step);
|
|
46
|
+
for (let i = 0; i < steps; i++) {
|
|
47
|
+
const o = i * step;
|
|
48
|
+
const y = stepBackwards ? genshin.height - o : o;
|
|
49
|
+
moveMouseTo(Math.round(navWidth / 2), y);
|
|
50
|
+
await sleep(30); // 等待提示文字出现
|
|
51
|
+
if ((result = findTooltip()) !== undefined)
|
|
52
|
+
return result;
|
|
53
|
+
}
|
|
54
|
+
});
|
|
38
55
|
// 3.点击菜单按钮
|
|
39
|
-
if (
|
|
40
|
-
|
|
41
|
-
click(
|
|
56
|
+
if (tooltip != undefined) {
|
|
57
|
+
await assertRegionDisappearing(findTooltip, `打开菜单 ${name} 超时`, () => {
|
|
58
|
+
click(Math.round(navWidth / 2), tooltip.y);
|
|
42
59
|
});
|
|
43
|
-
if (!ok)
|
|
44
|
-
throw new Error(`打开菜单 ${name} 超时`);
|
|
45
60
|
}
|
|
46
61
|
else {
|
|
47
62
|
throw new Error(`打开菜单 ${name} 失败`);
|
|
@@ -50,54 +65,67 @@ export const openMenu = async (name, reverse = false, config = {}) => {
|
|
|
50
65
|
/**
|
|
51
66
|
* 打开游戏菜单页面(菜单按钮列表)
|
|
52
67
|
* @param name 菜单页面名称
|
|
53
|
-
* @param
|
|
68
|
+
* @param listView 菜单页面视图参数
|
|
54
69
|
*/
|
|
55
|
-
export const openMenuPage = async (name,
|
|
70
|
+
export const openMenuPage = async (name, listView) => {
|
|
56
71
|
// 1.打开派蒙菜单
|
|
57
72
|
await openPaimonMenu();
|
|
58
|
-
// 2
|
|
59
|
-
const
|
|
60
|
-
|
|
61
|
-
x,
|
|
62
|
-
y,
|
|
63
|
-
w,
|
|
64
|
-
h,
|
|
65
|
-
maxListItems: 5,
|
|
66
|
-
lineHeight
|
|
73
|
+
// 2.搜索菜单页面按钮
|
|
74
|
+
const button = await withGameMetrics(1920, 1080, 1.5, async () => {
|
|
75
|
+
const { x = 95, y = 330, w = 670, h = 730, lineHeight = 142 } = listView || {};
|
|
76
|
+
return await findTextWithinListView(name, { x, y, w, h, lineHeight, scrollLines: 2 });
|
|
67
77
|
});
|
|
68
|
-
if (!
|
|
78
|
+
if (!button)
|
|
69
79
|
throw new Error(`搜索菜单页面 ${name} 失败`);
|
|
70
|
-
// 3
|
|
71
|
-
|
|
80
|
+
// 3.点击打开菜单页面
|
|
81
|
+
await assertRegionDisappearing(() => findTextWithinBounds(name, button.x, button.y, button.width, button.height), `打开菜单页面 ${name} 超时`, () => {
|
|
82
|
+
button.click();
|
|
83
|
+
});
|
|
72
84
|
};
|
|
73
85
|
/**
|
|
74
|
-
*
|
|
75
|
-
* @param
|
|
76
|
-
* @param
|
|
86
|
+
* 调整游戏时间到指定时分
|
|
87
|
+
* @param hour 小时
|
|
88
|
+
* @param minute 分钟
|
|
89
|
+
* @param options 时钟参数
|
|
77
90
|
*/
|
|
78
|
-
export const
|
|
91
|
+
export const setTimeTo = async (hour, minute, options) => {
|
|
79
92
|
// 1.打开时间页面
|
|
80
93
|
await openMenu("时间", true);
|
|
81
|
-
// 2
|
|
82
|
-
const { centerX = 1440, centerY = 502, radius =
|
|
83
|
-
const
|
|
84
|
-
const
|
|
85
|
-
|
|
86
|
-
() =>
|
|
87
|
-
|
|
88
|
-
(
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
94
|
+
// 2.计算调整时钟的路径点
|
|
95
|
+
const { centerX = 1440, centerY = 502, offsetHours = 6, radius = 154, smooth = 3 } = options || {};
|
|
96
|
+
const radian = ((((hour + offsetHours) * 60 + minute) % 1440) / 1440) * Math.PI * 2;
|
|
97
|
+
const waypoints = [{ x: centerX, y: centerY }].concat(Array.from({ length: Math.max(smooth, 3) })
|
|
98
|
+
// 计算弧度
|
|
99
|
+
.map((_, i) => radian + (1 + i / (Math.max(smooth, 3) - 1)) * Math.PI)
|
|
100
|
+
// 计算相对圆点坐标
|
|
101
|
+
.map(rad => ({ x: radius * Math.cos(rad), y: radius * Math.sin(rad) }))
|
|
102
|
+
// 计算绝对坐标
|
|
103
|
+
.map(p => ({ x: Math.round(p.x + centerX), y: Math.round(p.y + centerY) })));
|
|
104
|
+
// 3.拨动指针
|
|
105
|
+
await withGameMetrics(1920, 1080, 1.5, async () => {
|
|
106
|
+
await mouseMoveAlongWaypoints(waypoints, { shouldDrag: true });
|
|
107
|
+
// 3.点击确认按钮,等待调整结束
|
|
108
|
+
await assertRegionAppearing(() => findTextInDirection("时间少于", "south-east", { contains: true }), "调整时间超时", () => {
|
|
109
|
+
findTextInDirection("确认", "south-east")?.click();
|
|
110
|
+
}, { maxAttempts: 20, retryInterval: 1000 });
|
|
98
111
|
});
|
|
99
|
-
if (!ok)
|
|
100
|
-
throw new Error("调整时间超时");
|
|
101
112
|
// 4.返回主界面
|
|
102
113
|
await genshin.returnMainUi();
|
|
103
114
|
};
|
|
115
|
+
/**
|
|
116
|
+
* 调整游戏时间到指定时间段
|
|
117
|
+
* @param period 时间段
|
|
118
|
+
* @param options 时钟参数
|
|
119
|
+
*/
|
|
120
|
+
export const setTime = async (period, options) => {
|
|
121
|
+
switch (period) {
|
|
122
|
+
case "night":
|
|
123
|
+
return setTimeTo(0, 0, options);
|
|
124
|
+
case "morning":
|
|
125
|
+
return setTimeTo(6, 0, options);
|
|
126
|
+
case "noon":
|
|
127
|
+
return setTimeTo(12, 0, options);
|
|
128
|
+
case "evening":
|
|
129
|
+
return setTimeTo(18, 0, options);
|
|
130
|
+
}
|
|
131
|
+
};
|
package/dist/http.js
CHANGED
|
@@ -6,8 +6,8 @@
|
|
|
6
6
|
* @param headers 请求头
|
|
7
7
|
* @returns 响应体内容
|
|
8
8
|
*/
|
|
9
|
-
export const requestForBody = async (method, url, body
|
|
10
|
-
const resp = await http.request(method, url, body, headers ? JSON.stringify(headers) : "null");
|
|
9
|
+
export const requestForBody = async (method, url, body, headers) => {
|
|
10
|
+
const resp = await http.request(method, url, body ?? "null", headers ? JSON.stringify(headers) : "null");
|
|
11
11
|
if (resp.status_code >= 200 && resp.status_code < 400) {
|
|
12
12
|
return resp.body;
|
|
13
13
|
}
|
package/dist/index.d.ts
CHANGED
package/dist/index.js
CHANGED