@bettergi/utils 0.1.10 → 0.1.12

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 CHANGED
@@ -47,23 +47,29 @@ await navigateToTab(() => {
47
47
  > 对 RecognitionObject 代码的封装,对于简单的找图、找字操作,不再需要编写复杂的代码。
48
48
 
49
49
  ```ts
50
- // 在整个画面内搜索图片,相似度0.8(默认),找不到返回 undefined
51
- const i1 = findImage("assets/关闭.png");
50
+ // 在整个画面内搜索图片,找不到返回 undefined
51
+ const i1 = findImage("assets/关闭.png", { use3Channels: true }); // 匹配颜色
52
52
 
53
- // 在指定方向上搜索图片,相似度0.9,找不到返回 undefined
54
- const i2 = findImageInDirection("assets/关闭.png", "north-east", 0.9);
53
+ // 在指定区域内搜索图片,找不到返回 undefined
54
+ const i2 = findImageWithinBounds("assets/关闭.png", 960, 0, 960, 1080);
55
55
 
56
- // 在指定区域内搜索图片,相似度0.8(默认),找不到返回 undefined
57
- const i3 = findImageWithinBounds("assets/关闭.png", 960, 0, 960, 1080);
56
+ // 在指定坐标范围内搜索图片,找不到返回 undefined
57
+ const i3 = findImageBetweenCoordinates("assets/关闭.png", 960, 0, 1920, 1080);
58
+
59
+ // 在指定方向上搜索图片,找不到返回 undefined
60
+ const i4 = findImageInDirection("assets/关闭.png", "north-east");
58
61
 
59
62
  // 在整个画面内搜索文本(不包含、忽略大小写),找不到返回 undefined
60
63
  const t1 = findText("购买");
61
64
 
62
- // 在指定方向上搜索文本(包含、忽略大小写),找不到返回 undefined
63
- const t2 = findTextInDirection("师傅", "east", { contains: true, ignoreCase: true });
64
-
65
65
  // 在指定区域内搜索文本(不包含、忽略大小写),找不到返回 undefined
66
- const t3 = findTextWithinBounds("确认", 960, 540, 960, 540);
66
+ const t2 = findTextWithinBounds("确认", 960, 540, 960, 540);
67
+
68
+ // 在指定坐标范围内搜索文本(不包含、忽略大小写),找不到返回 undefined
69
+ const t3 = findTextBetweenCoordinates("确认", 960, 540, 1920, 1080);
70
+
71
+ // 在指定方向上搜索文本(包含、忽略大小写),找不到返回 undefined
72
+ const t4 = findTextInDirection("师傅", "east", { contains: true, ignoreCase: true });
67
73
  ```
68
74
 
69
75
  ### 行为流程
@@ -119,7 +125,7 @@ await mouseScrollUpLines(99);
119
125
  await mouseScrollDownLines(1, 115);
120
126
  ```
121
127
 
122
- ### 数据存储
128
+ ### 状态管理和持久化
123
129
 
124
130
  > 对象数据持久化,通过 Proxy 实现自动存储。从而可以无感知地读取/更新数据,而无需考虑如何持久化。
125
131
 
package/dist/game.d.ts CHANGED
@@ -28,7 +28,7 @@ export declare const openMenu: (name: "\u62CD\u7167" | "\u516C\u544A" | "\u90AE\
28
28
  * @param listView 菜单页面视图参数
29
29
  */
30
30
  export declare const openMenuPage: (name: "\u5546\u57CE" | "\u961F\u4F0D\u914D\u7F6E" | "\u597D\u53CB" | "\u6210\u5C31" | "\u56FE\u9274" | "\u89D2\u8272\u56FE\u9274" | "\u89D2\u8272" | "\u63D0\u5347\u6307\u5357" | "\u80CC\u5305" | "\u4EFB\u52A1" | "\u5730\u56FE" | "\u6D3B\u52A8" | "\u5192\u9669\u4E4B\u8BC1" | "\u7948\u613F" | "\u7EAA\u884C" | "\u591A\u4EBA\u6E38\u620F" | "\u5343\u661F\u5546\u57CE" | "\u4EBA\u6C14\u5947\u57DF" | "\u5947\u57DF\u6536\u85CF" | "\u6211\u7684\u5947\u57DF" | "\u5927\u5385" | "\u88C5\u626E\u642D\u914D" | "\u9882\u613F" | "\u7EAA\u6E38" | (string & {}), listView?: ListView) => Promise<void>;
31
- type ClockOptions = {
31
+ export type ClockOptions = {
32
32
  /** 时钟中心X坐标 */
33
33
  centerX?: number;
34
34
  /** 时钟中心Y坐标 */
@@ -54,7 +54,7 @@ export declare const setTimeTo: (hour: number, minute: number, options?: ClockOp
54
54
  */
55
55
  export declare const setTime: (period: "night" | "morning" | "noon" | "evening", options?: ClockOptions) => Promise<void>;
56
56
  /** tab 翻页配置 */
57
- type TabNavigationOptions = {
57
+ export type TabNavigationOptions = {
58
58
  /** 标签页图标宽度(默认:96) */
59
59
  tabIconWidth?: number;
60
60
  /** 翻页按钮垂直偏移(默认:540) */
@@ -78,4 +78,3 @@ type TabNavigationOptions = {
78
78
  * - false 达到最大重试次数
79
79
  */
80
80
  export declare const navigateToTab: (condition: () => boolean, options?: TabNavigationOptions) => Promise<boolean>;
81
- export {};
package/dist/mouse.d.ts CHANGED
@@ -1,6 +1,8 @@
1
- type MouseWaypointsOptions = {
1
+ export type MouseWaypointsOptions = {
2
2
  /** 是否按住鼠标左键拖动 */
3
3
  shouldDrag?: boolean;
4
+ /** 移动延时(毫秒) */
5
+ delay?: number;
4
6
  /** 超时时间(毫秒,默认: 不超时) */
5
7
  timeout?: number;
6
8
  };
@@ -22,7 +24,7 @@ export declare const mouseMoveAlongWaypoints: (waypoints: {
22
24
  * @param y2 终止垂直方向偏移量(像素)
23
25
  */
24
26
  export declare const mouseDrag: (x1: number, y1: number, x2: number, y2: number) => Promise<boolean>;
25
- type NaturalMouseMoveOptions = {
27
+ export type NaturalMouseMoveOptions = {
26
28
  /** 移动持续时间(毫秒,默认: 800) */
27
29
  duration: number;
28
30
  /** 摆动幅度(默认: 30) */
@@ -71,4 +73,3 @@ export declare const mouseScrollUpLines: (lines: number, lineHeight?: number) =>
71
73
  * @param lineHeight 行高(默认值为175像素)
72
74
  */
73
75
  export declare const mouseScrollDownLines: (lines: number, lineHeight?: number) => Promise<void>;
74
- export {};
package/dist/mouse.js CHANGED
@@ -4,7 +4,7 @@
4
4
  * @param options 鼠标移动选项
5
5
  */
6
6
  export const mouseMoveAlongWaypoints = async (waypoints, options) => {
7
- const { shouldDrag = false, timeout = 0 } = options || {};
7
+ const { shouldDrag = false, delay = 50, timeout = 0 } = options || {};
8
8
  try {
9
9
  const startTime = Date.now();
10
10
  for (let i = 0; i < waypoints.length; i++) {
@@ -13,9 +13,9 @@ export const mouseMoveAlongWaypoints = async (waypoints, options) => {
13
13
  leftButtonDown();
14
14
  moveMouseTo(Math.trunc(waypoints[i].x), Math.trunc(waypoints[i].y));
15
15
  // 等待指定延迟
16
- const delay = Math.trunc(waypoints[i].delay || 50);
17
- if (delay > 0)
18
- await sleep(delay);
16
+ const duration = Math.trunc(waypoints[i].delay || delay);
17
+ if (duration > 0)
18
+ await sleep(duration);
19
19
  // 超时检查
20
20
  if (timeout > 0 && Date.now() - startTime > timeout)
21
21
  return false;
package/dist/ocr.d.ts CHANGED
@@ -1,12 +1,18 @@
1
1
  import { RetryOptions } from "./workflow";
2
- type MatchDirection = "north" /** 上半边 */ | "north-east" /** 右上四分之一 */ | "east" /** 右半边 */ | "south-east" /** 右下四分之一 */ | "south" /** 下半边 */ | "south-west" /** 左下四分之一 */ | "west" /** 左半边 */ | "north-west"; /** 左上四分之一 */
2
+ /** 识别对象实例 */
3
+ export type ROInstance = InstanceType<typeof RecognitionObject>;
4
+ /** 识别对象配置 */
5
+ export type ROConfig = Partial<{
6
+ [K in keyof ROInstance as ROInstance[K] extends Function ? never : K]: ROInstance[K];
7
+ }>;
8
+ export type MatchDirection = "north" /** 上半边 */ | "north-east" /** 右上四分之一 */ | "east" /** 右半边 */ | "south-east" /** 右下四分之一 */ | "south" /** 下半边 */ | "south-west" /** 左下四分之一 */ | "west" /** 左半边 */ | "north-west"; /** 左上四分之一 */
3
9
  /**
4
10
  * 在整个画面内搜索图片
5
11
  * @param path 图片路径
6
- * @param similarity 相似度阈值(默认: 0.8)
12
+ * @param config 识别对象配置
7
13
  * @returns 如果找到匹配的图片区域,则返回该区域
8
14
  */
9
- export declare const findImage: (path: string, similarity?: number) => Region | undefined;
15
+ export declare const findImage: (path: string, config?: ROConfig) => Region | undefined;
10
16
  /**
11
17
  * 在指定区域内搜索图片
12
18
  * @param path 图片路径
@@ -14,19 +20,31 @@ export declare const findImage: (path: string, similarity?: number) => Region |
14
20
  * @param y 垂直方向偏移量(像素)
15
21
  * @param w 宽度
16
22
  * @param h 高度
17
- * @param similarity 相似度阈值(默认: 0.8)
23
+ * @param config 识别对象配置
18
24
  * @returns 如果找到匹配的图片区域,则返回该区域
19
25
  */
20
- export declare const findImageWithinBounds: (path: string, x: number, y: number, w: number, h: number, similarity?: number) => Region | undefined;
26
+ export declare const findImageWithinBounds: (path: string, x: number, y: number, w: number, h: number, config?: ROConfig) => Region | undefined;
27
+ /**
28
+ * 在指定坐标范围内搜索图片
29
+ * @param path 图片路径
30
+ * @param left 左边界偏移量(像素)
31
+ * @param top 上边界偏移量(像素)
32
+ * @param right 右边界偏移量(像素)
33
+ * @param bottom 下边界偏移量(像素)
34
+ * @param config 识别对象配置
35
+ * @returns 如果找到匹配的图片区域,则返回该区域
36
+ */
37
+ export declare const findImageBetweenCoordinates: (path: string, left: number, top: number, right: number, bottom: number, config?: ROConfig) => Region | undefined;
21
38
  /**
22
39
  * 在指定方向上搜索图片
23
40
  * @param path 图片路径
24
41
  * @param direction 搜索方向
42
+ * @param config 识别对象配置
25
43
  * @returns 如果找到匹配的图片区域,则返回该区域
26
44
  */
27
- export declare const findImageInDirection: (path: string, direction: MatchDirection, similarity?: number) => Region | undefined;
45
+ export declare const findImageInDirection: (path: string, direction: MatchDirection, config?: ROConfig) => Region | undefined;
28
46
  /** 文本搜索选项 */
29
- type TextMatchOptions = {
47
+ export type TextMatchOptions = {
30
48
  /** 是否忽略大小写(默认: 是) */
31
49
  ignoreCase?: boolean;
32
50
  /** 是否非完全匹配(默认: 否) */
@@ -36,9 +54,10 @@ type TextMatchOptions = {
36
54
  * 在整个画面内搜索文本
37
55
  * @param text 待搜索文本
38
56
  * @param options 搜索选项
57
+ * @param config 识别对象配置
39
58
  * @returns 如果找到匹配的文本区域,则返回该区域
40
59
  */
41
- export declare const findText: (text: string, options?: TextMatchOptions) => Region | undefined;
60
+ export declare const findText: (text: string, options?: TextMatchOptions, config?: ROConfig) => Region | undefined;
42
61
  /**
43
62
  * 在指定区域内搜索文本
44
63
  * @param text 待搜索文本
@@ -47,17 +66,31 @@ export declare const findText: (text: string, options?: TextMatchOptions) => Reg
47
66
  * @param w 宽度
48
67
  * @param h 高度
49
68
  * @param options 搜索选项
69
+ * @param config 识别对象配置
70
+ * @returns 如果找到匹配的文本区域,则返回该区域
71
+ */
72
+ export declare const findTextWithinBounds: (text: string, x: number, y: number, w: number, h: number, options?: TextMatchOptions, config?: ROConfig) => Region | undefined;
73
+ /**
74
+ * 在指定坐标范围内搜索文本
75
+ * @param text 待搜索文本
76
+ * @param left 左边界偏移量(像素)
77
+ * @param top 上边界偏移量(像素)
78
+ * @param right 右边界偏移量(像素)
79
+ * @param bottom 下边界偏移量(像素)
80
+ * @param options 搜索选项
81
+ * @param config 识别对象配置
50
82
  * @returns 如果找到匹配的文本区域,则返回该区域
51
83
  */
52
- export declare const findTextWithinBounds: (text: string, x: number, y: number, w: number, h: number, options?: TextMatchOptions) => Region | undefined;
84
+ export declare const findTextBetweenCoordinates: (text: string, left: number, top: number, right: number, bottom: number, options?: TextMatchOptions, config?: ROConfig) => Region | undefined;
53
85
  /**
54
86
  * 在指定方向上搜索文本
55
87
  * @param text 待搜索文本
56
88
  * @param direction 搜索方向
57
89
  * @param options 搜索选项
90
+ * @param config 识别对象配置
58
91
  * @returns 如果找到匹配的文本区域,则返回该区域
59
92
  */
60
- export declare const findTextInDirection: (text: string, direction: MatchDirection, options?: TextMatchOptions) => Region | undefined;
93
+ export declare const findTextInDirection: (text: string, direction: MatchDirection, options?: TextMatchOptions, config?: ROConfig) => Region | undefined;
61
94
  /** 列表视图参数 */
62
95
  export type ListView = {
63
96
  x: number;
@@ -78,8 +111,8 @@ export type ListView = {
78
111
  * @param text 待搜索文本
79
112
  * @param listView 列表视图参数
80
113
  * @param matchOptions 搜索选项
81
- * @param timeout 搜索超时
114
+ * @param retryOptions 重试选项
115
+ * @param config 识别对象配置
82
116
  * @returns 如果找到匹配的文本区域,则返回该区域,否则返回 undefined
83
117
  */
84
- export declare const findTextWithinListView: (text: string, listView: ListView, matchOptions?: TextMatchOptions, retryOptions?: RetryOptions) => Promise<Region | undefined>;
85
- export {};
118
+ export declare const findTextWithinListView: (text: string, listView: ListView, matchOptions?: TextMatchOptions, retryOptions?: RetryOptions, config?: ROConfig) => Promise<Region | undefined>;
package/dist/ocr.js CHANGED
@@ -24,14 +24,14 @@ const directionToBounds = (direction) => {
24
24
  /**
25
25
  * 在整个画面内搜索图片
26
26
  * @param path 图片路径
27
- * @param similarity 相似度阈值(默认: 0.8)
27
+ * @param config 识别对象配置
28
28
  * @returns 如果找到匹配的图片区域,则返回该区域
29
29
  */
30
- export const findImage = (path, similarity = 0.8) => {
30
+ export const findImage = (path, config = {}) => {
31
31
  const ir = captureGameRegion();
32
32
  try {
33
33
  const ro = RecognitionObject.templateMatch(file.readImageMatSync(path));
34
- ro.threshold = similarity;
34
+ Object.assign(ro, config);
35
35
  return findFirst(ir, ro, region => region.isExist());
36
36
  }
37
37
  catch (err) {
@@ -48,14 +48,14 @@ export const findImage = (path, similarity = 0.8) => {
48
48
  * @param y 垂直方向偏移量(像素)
49
49
  * @param w 宽度
50
50
  * @param h 高度
51
- * @param similarity 相似度阈值(默认: 0.8)
51
+ * @param config 识别对象配置
52
52
  * @returns 如果找到匹配的图片区域,则返回该区域
53
53
  */
54
- export const findImageWithinBounds = (path, x, y, w, h, similarity = 0.8) => {
54
+ export const findImageWithinBounds = (path, x, y, w, h, config = {}) => {
55
55
  const ir = captureGameRegion();
56
56
  try {
57
57
  const ro = RecognitionObject.templateMatch(file.readImageMatSync(path), x, y, w, h);
58
- ro.threshold = similarity;
58
+ Object.assign(ro, config);
59
59
  return findFirst(ir, ro, region => region.isExist());
60
60
  }
61
61
  catch (err) {
@@ -65,28 +65,44 @@ export const findImageWithinBounds = (path, x, y, w, h, similarity = 0.8) => {
65
65
  ir.dispose();
66
66
  }
67
67
  };
68
+ /**
69
+ * 在指定坐标范围内搜索图片
70
+ * @param path 图片路径
71
+ * @param left 左边界偏移量(像素)
72
+ * @param top 上边界偏移量(像素)
73
+ * @param right 右边界偏移量(像素)
74
+ * @param bottom 下边界偏移量(像素)
75
+ * @param config 识别对象配置
76
+ * @returns 如果找到匹配的图片区域,则返回该区域
77
+ */
78
+ export const findImageBetweenCoordinates = (path, left, top, right, bottom, config = {}) => {
79
+ return findImageWithinBounds(path, left, top, right - left, bottom - top, config);
80
+ };
68
81
  /**
69
82
  * 在指定方向上搜索图片
70
83
  * @param path 图片路径
71
84
  * @param direction 搜索方向
85
+ * @param config 识别对象配置
72
86
  * @returns 如果找到匹配的图片区域,则返回该区域
73
87
  */
74
- export const findImageInDirection = (path, direction, similarity = 0.8) => {
88
+ export const findImageInDirection = (path, direction, config = {}) => {
75
89
  const { x, y, w, h } = directionToBounds(direction);
76
- return findImageWithinBounds(path, x, y, w, h, similarity);
90
+ return findImageWithinBounds(path, x, y, w, h, config);
77
91
  };
78
92
  /**
79
93
  * 在整个画面内搜索文本
80
94
  * @param text 待搜索文本
81
95
  * @param options 搜索选项
96
+ * @param config 识别对象配置
82
97
  * @returns 如果找到匹配的文本区域,则返回该区域
83
98
  */
84
- export const findText = (text, options) => {
99
+ export const findText = (text, options, config = {}) => {
85
100
  const { ignoreCase = true, contains = false } = options || {};
86
101
  const searchText = ignoreCase ? text.toLowerCase() : text;
87
102
  const ir = captureGameRegion();
88
103
  try {
89
104
  const ro = RecognitionObject.ocrThis;
105
+ Object.assign(ro, config);
90
106
  return findFirst(ir, ro, region => {
91
107
  const itemText = ignoreCase ? region.text.toLowerCase() : region.text;
92
108
  const isMatch = contains ? itemText.includes(searchText) : itemText === searchText;
@@ -108,14 +124,16 @@ export const findText = (text, options) => {
108
124
  * @param w 宽度
109
125
  * @param h 高度
110
126
  * @param options 搜索选项
127
+ * @param config 识别对象配置
111
128
  * @returns 如果找到匹配的文本区域,则返回该区域
112
129
  */
113
- export const findTextWithinBounds = (text, x, y, w, h, options) => {
130
+ export const findTextWithinBounds = (text, x, y, w, h, options, config = {}) => {
114
131
  const { ignoreCase = true, contains = false } = options || {};
115
132
  const searchText = ignoreCase ? text.toLowerCase() : text;
116
133
  const ir = captureGameRegion();
117
134
  try {
118
135
  const ro = RecognitionObject.ocr(x, y, w, h);
136
+ Object.assign(ro, config);
119
137
  return findFirst(ir, ro, region => {
120
138
  const itemText = ignoreCase ? region.text.toLowerCase() : region.text;
121
139
  const isMatch = contains ? itemText.includes(searchText) : itemText === searchText;
@@ -129,29 +147,45 @@ export const findTextWithinBounds = (text, x, y, w, h, options) => {
129
147
  ir.dispose();
130
148
  }
131
149
  };
150
+ /**
151
+ * 在指定坐标范围内搜索文本
152
+ * @param text 待搜索文本
153
+ * @param left 左边界偏移量(像素)
154
+ * @param top 上边界偏移量(像素)
155
+ * @param right 右边界偏移量(像素)
156
+ * @param bottom 下边界偏移量(像素)
157
+ * @param options 搜索选项
158
+ * @param config 识别对象配置
159
+ * @returns 如果找到匹配的文本区域,则返回该区域
160
+ */
161
+ export const findTextBetweenCoordinates = (text, left, top, right, bottom, options, config = {}) => {
162
+ return findTextWithinBounds(text, left, top, right - left, bottom - top, options, config);
163
+ };
132
164
  /**
133
165
  * 在指定方向上搜索文本
134
166
  * @param text 待搜索文本
135
167
  * @param direction 搜索方向
136
168
  * @param options 搜索选项
169
+ * @param config 识别对象配置
137
170
  * @returns 如果找到匹配的文本区域,则返回该区域
138
171
  */
139
- export const findTextInDirection = (text, direction, options) => {
172
+ export const findTextInDirection = (text, direction, options, config = {}) => {
140
173
  const { x, y, w, h } = directionToBounds(direction);
141
- return findTextWithinBounds(text, x, y, w, h, options);
174
+ return findTextWithinBounds(text, x, y, w, h, options, config);
142
175
  };
143
176
  /**
144
177
  * 在列表视图中滚动搜索文本
145
178
  * @param text 待搜索文本
146
179
  * @param listView 列表视图参数
147
180
  * @param matchOptions 搜索选项
148
- * @param timeout 搜索超时
181
+ * @param retryOptions 重试选项
182
+ * @param config 识别对象配置
149
183
  * @returns 如果找到匹配的文本区域,则返回该区域,否则返回 undefined
150
184
  */
151
- export const findTextWithinListView = async (text, listView, matchOptions, retryOptions) => {
185
+ export const findTextWithinListView = async (text, listView, matchOptions, retryOptions, config = {}) => {
152
186
  const { x, y, w, h, lineHeight, scrollLines = 1, paddingX = 10, paddingY = 10 } = listView;
153
187
  const { maxAttempts = 30, retryInterval = 1000 } = retryOptions || {};
154
- const findTargetText = () => findTextWithinBounds(text, x, y, w, h, matchOptions);
188
+ const findTargetText = () => findTextWithinBounds(text, x, y, w, h, matchOptions, config);
155
189
  let lastTextRegion;
156
190
  const isReachedBottom = () => {
157
191
  const textRegion = findFirst(captureGameRegion(), RecognitionObject.ocr(x, y, w, h), region => {
@@ -172,6 +206,7 @@ export const findTextWithinListView = async (text, listView, matchOptions, retry
172
206
  };
173
207
  const isTextFoundOrBottomReached = await waitForAction(() => findTargetText() != undefined || isReachedBottom(), async () => {
174
208
  moveMouseTo(x + w - paddingX, y + paddingY); // 移动到滚动条附近
209
+ await sleep(50);
175
210
  await mouseScrollDownLines(scrollLines, lineHeight); // 滚动指定行数
176
211
  }, { maxAttempts, retryInterval });
177
212
  return isTextFoundOrBottomReached ? findTargetText() : undefined;
@@ -1,4 +1,5 @@
1
1
  export type Action = () => Promise<void> | void;
2
+ /** 重试选项 */
2
3
  export type RetryOptions = {
3
4
  /** 重试次数(默认: 5) */
4
5
  maxAttempts?: number;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@bettergi/utils",
3
- "version": "0.1.10",
3
+ "version": "0.1.12",
4
4
  "description": "开发 BetterGI 脚本常用工具集",
5
5
  "type": "module",
6
6
  "author": "Bread Grocery<https://github.com/breadgrocery>",