@bettergi/utils 0.1.22 → 0.1.24

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
@@ -51,7 +51,7 @@ await navigateToTab(() => {
51
51
  const i1 = findImage("assets/关闭.png", { use3Channels: true }); // 匹配颜色
52
52
 
53
53
  // 在指定区域内搜索图片,找不到返回 undefined
54
- const i2 = findImageWithinBounds("assets/关闭.png", 960, 0, 960, 1080);
54
+ const i2 = findImageWithinBounds("assets/关闭.png", 960, 0, 960, 1080, { threshold: 0.75 }); // 阈值0.75
55
55
 
56
56
  // 在指定坐标范围内搜索图片,找不到返回 undefined
57
57
  const i3 = findImageBetweenCoordinates("assets/关闭.png", 960, 0, 1920, 1080);
@@ -157,7 +157,7 @@ await mouseScrollDownLines(1, 115);
157
157
 
158
158
  ### 状态管理和持久化
159
159
 
160
- > 基于深度 Proxy 实现的对象数据持久化,能够在数据被修改时自动同步至文件。使开发其能够像操作普通对象一样进行数据读写,而无需关心底层的持久化细节。
160
+ > 基于深度 Proxy 实现的对象数据持久化,能够在数据被修改时自动同步至文件。使开发者能够像操作普通对象一样进行数据读写,而无需关心底层的持久化细节。
161
161
 
162
162
  ```ts
163
163
  // 创建/读取存储对象,保存到存储文件 store/my-data.json 中
package/dist/game.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- import { ListView } from "./ocr";
1
+ import { type ListView } from "./ocr";
2
2
  /**
3
3
  * 临时设置游戏分辨率和DPI缩放比例,执行指定动作后恢复
4
4
  * 适用于使用了鼠标移动的操作,保证在不同的分辨率和DPI下都能正确地复现鼠标操作
package/dist/game.js CHANGED
@@ -1,6 +1,6 @@
1
1
  import { assertRegionAppearing, assertRegionDisappearing } from "./asserts";
2
2
  import { mouseMoveAlongWaypoints } from "./mouse";
3
- import { findTextInDirection, findTextWithinBounds, findTextWithinListView } from "./ocr";
3
+ import { findTextWithinBounds, findTextWithinListView } from "./ocr";
4
4
  /**
5
5
  * 临时设置游戏分辨率和DPI缩放比例,执行指定动作后恢复
6
6
  * 适用于使用了鼠标移动的操作,保证在不同的分辨率和DPI下都能正确地复现鼠标操作
@@ -10,13 +10,13 @@ import { findTextInDirection, findTextWithinBounds, findTextWithinListView } fro
10
10
  * @param action 执行动作
11
11
  */
12
12
  export const withGameMetrics = async (w, h, dpi, action) => {
13
- const [_w, _h, _dpi] = globalThis["getGameMetrics"] ? getGameMetrics() : [];
13
+ const [_w, _h, _dpi] = getGameMetrics();
14
14
  try {
15
15
  setGameMetrics(w, h, dpi);
16
16
  return await action();
17
17
  }
18
18
  finally {
19
- globalThis["getGameMetrics"] && setGameMetrics(_w, _h, _dpi);
19
+ setGameMetrics(_w, _h, _dpi);
20
20
  }
21
21
  };
22
22
  /**
@@ -108,8 +108,8 @@ export const setTimeTo = async (hour, minute, options) => {
108
108
  // 拨动指针
109
109
  await mouseMoveAlongWaypoints(waypoints, { shouldDrag: true });
110
110
  // 点击确认按钮,等待调整结束
111
- await assertRegionAppearing(() => findTextInDirection("时间少于", "south-east", { contains: true }), "调整时间超时", () => {
112
- findTextInDirection("确认", "south-east")?.click();
111
+ await assertRegionAppearing(() => findTextWithinBounds("时间少于", 960, 540, 960, 540, { contains: true }), "调整时间超时", () => {
112
+ findTextWithinBounds("确认", 960, 540, 960, 540)?.click();
113
113
  }, { maxAttempts: 20, retryInterval: 1000 });
114
114
  });
115
115
  // 4.返回主界面
package/dist/ocr.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- import { RetryOptions } from "./workflow";
1
+ import { type RetryOptions } from "./workflow";
2
2
  /** 识别对象实例 */
3
3
  export type ROInstance = InstanceType<typeof RecognitionObject>;
4
4
  /** 识别对象配置 */
@@ -11,7 +11,7 @@ export type MatchDirection = "north" /** 上半边 */ | "north-east" /** 右上
11
11
  * 在整个画面内搜索图片
12
12
  * @param image 图片路径 或 图片Mat
13
13
  * @param config 识别对象配置
14
- * @returns 如果找到匹配的图片区域,则返回该区域
14
+ * @returns 如果找到匹配的图片区域,则返回该区域,否则返回 undefined
15
15
  */
16
16
  export declare const findImage: (image: string | ImageMat, config?: ROConfig) => Region | undefined;
17
17
  /**
@@ -22,7 +22,7 @@ export declare const findImage: (image: string | ImageMat, config?: ROConfig) =>
22
22
  * @param w 宽度
23
23
  * @param h 高度
24
24
  * @param config 识别对象配置
25
- * @returns 如果找到匹配的图片区域,则返回该区域
25
+ * @returns 如果找到匹配的图片区域,则返回该区域,否则返回 undefined
26
26
  */
27
27
  export declare const findImageWithinBounds: (image: string | ImageMat, x: number, y: number, w: number, h: number, config?: ROConfig) => Region | undefined;
28
28
  /**
@@ -33,7 +33,7 @@ export declare const findImageWithinBounds: (image: string | ImageMat, x: number
33
33
  * @param right 右边界偏移量(像素)
34
34
  * @param bottom 下边界偏移量(像素)
35
35
  * @param config 识别对象配置
36
- * @returns 如果找到匹配的图片区域,则返回该区域
36
+ * @returns 如果找到匹配的图片区域,则返回该区域,否则返回 undefined
37
37
  */
38
38
  export declare const findImageBetweenCoordinates: (image: string | ImageMat, left: number, top: number, right: number, bottom: number, config?: ROConfig) => Region | undefined;
39
39
  /**
@@ -41,7 +41,7 @@ export declare const findImageBetweenCoordinates: (image: string | ImageMat, lef
41
41
  * @param image 图片路径 或 图片Mat
42
42
  * @param direction 搜索方向
43
43
  * @param config 识别对象配置
44
- * @returns 如果找到匹配的图片区域,则返回该区域
44
+ * @returns 如果找到匹配的图片区域,则返回该区域,否则返回 undefined
45
45
  */
46
46
  export declare const findImageInDirection: (image: string | ImageMat, direction: MatchDirection, config?: ROConfig) => Region | undefined;
47
47
  /** 文本匹配选项 */
@@ -56,7 +56,7 @@ export type TextMatchOptions = {
56
56
  * @param text 待搜索文本
57
57
  * @param options 搜索选项
58
58
  * @param config 识别对象配置
59
- * @returns 如果找到匹配的文本区域,则返回该区域
59
+ * @returns 如果找到匹配的文本区域,则返回该区域,否则返回 undefined
60
60
  */
61
61
  export declare const findText: (text: string, options?: TextMatchOptions, config?: ROConfig) => Region | undefined;
62
62
  /**
@@ -68,7 +68,7 @@ export declare const findText: (text: string, options?: TextMatchOptions, config
68
68
  * @param h 高度
69
69
  * @param options 搜索选项
70
70
  * @param config 识别对象配置
71
- * @returns 如果找到匹配的文本区域,则返回该区域
71
+ * @returns 如果找到匹配的文本区域,则返回该区域,否则返回 undefined
72
72
  */
73
73
  export declare const findTextWithinBounds: (text: string, x: number, y: number, w: number, h: number, options?: TextMatchOptions, config?: ROConfig) => Region | undefined;
74
74
  /**
@@ -80,7 +80,7 @@ export declare const findTextWithinBounds: (text: string, x: number, y: number,
80
80
  * @param bottom 下边界偏移量(像素)
81
81
  * @param options 搜索选项
82
82
  * @param config 识别对象配置
83
- * @returns 如果找到匹配的文本区域,则返回该区域
83
+ * @returns 如果找到匹配的文本区域,则返回该区域,否则返回 undefined
84
84
  */
85
85
  export declare const findTextBetweenCoordinates: (text: string, left: number, top: number, right: number, bottom: number, options?: TextMatchOptions, config?: ROConfig) => Region | undefined;
86
86
  /**
@@ -89,7 +89,7 @@ export declare const findTextBetweenCoordinates: (text: string, left: number, to
89
89
  * @param direction 搜索方向
90
90
  * @param options 搜索选项
91
91
  * @param config 识别对象配置
92
- * @returns 如果找到匹配的文本区域,则返回该区域
92
+ * @returns 如果找到匹配的文本区域,则返回该区域,否则返回 undefined
93
93
  */
94
94
  export declare const findTextInDirection: (text: string, direction: MatchDirection, options?: TextMatchOptions, config?: ROConfig) => Region | undefined;
95
95
  /** 列表视图参数 */
package/dist/ocr.js CHANGED
@@ -17,7 +17,7 @@ const directionToBounds = (direction) => {
17
17
  * 在整个画面内搜索图片
18
18
  * @param image 图片路径 或 图片Mat
19
19
  * @param config 识别对象配置
20
- * @returns 如果找到匹配的图片区域,则返回该区域
20
+ * @returns 如果找到匹配的图片区域,则返回该区域,否则返回 undefined
21
21
  */
22
22
  export const findImage = (image, config = {}) => {
23
23
  const ir = captureGameRegion();
@@ -45,7 +45,7 @@ export const findImage = (image, config = {}) => {
45
45
  * @param w 宽度
46
46
  * @param h 高度
47
47
  * @param config 识别对象配置
48
- * @returns 如果找到匹配的图片区域,则返回该区域
48
+ * @returns 如果找到匹配的图片区域,则返回该区域,否则返回 undefined
49
49
  */
50
50
  export const findImageWithinBounds = (image, x, y, w, h, config = {}) => {
51
51
  const ir = captureGameRegion();
@@ -73,7 +73,7 @@ export const findImageWithinBounds = (image, x, y, w, h, config = {}) => {
73
73
  * @param right 右边界偏移量(像素)
74
74
  * @param bottom 下边界偏移量(像素)
75
75
  * @param config 识别对象配置
76
- * @returns 如果找到匹配的图片区域,则返回该区域
76
+ * @returns 如果找到匹配的图片区域,则返回该区域,否则返回 undefined
77
77
  */
78
78
  export const findImageBetweenCoordinates = (image, left, top, right, bottom, config = {}) => {
79
79
  return findImageWithinBounds(image, left, top, right - left, bottom - top, config);
@@ -83,7 +83,7 @@ export const findImageBetweenCoordinates = (image, left, top, right, bottom, con
83
83
  * @param image 图片路径 或 图片Mat
84
84
  * @param direction 搜索方向
85
85
  * @param config 识别对象配置
86
- * @returns 如果找到匹配的图片区域,则返回该区域
86
+ * @returns 如果找到匹配的图片区域,则返回该区域,否则返回 undefined
87
87
  */
88
88
  export const findImageInDirection = (image, direction, config = {}) => {
89
89
  const { x, y, w, h } = directionToBounds(direction);
@@ -122,7 +122,7 @@ const textMatch = (text, searchText, options) => {
122
122
  * @param text 待搜索文本
123
123
  * @param options 搜索选项
124
124
  * @param config 识别对象配置
125
- * @returns 如果找到匹配的文本区域,则返回该区域
125
+ * @returns 如果找到匹配的文本区域,则返回该区域,否则返回 undefined
126
126
  */
127
127
  export const findText = (text, options, config = {}) => {
128
128
  const ir = captureGameRegion();
@@ -151,7 +151,7 @@ export const findText = (text, options, config = {}) => {
151
151
  * @param h 高度
152
152
  * @param options 搜索选项
153
153
  * @param config 识别对象配置
154
- * @returns 如果找到匹配的文本区域,则返回该区域
154
+ * @returns 如果找到匹配的文本区域,则返回该区域,否则返回 undefined
155
155
  */
156
156
  export const findTextWithinBounds = (text, x, y, w, h, options, config = {}) => {
157
157
  const ir = captureGameRegion();
@@ -180,7 +180,7 @@ export const findTextWithinBounds = (text, x, y, w, h, options, config = {}) =>
180
180
  * @param bottom 下边界偏移量(像素)
181
181
  * @param options 搜索选项
182
182
  * @param config 识别对象配置
183
- * @returns 如果找到匹配的文本区域,则返回该区域
183
+ * @returns 如果找到匹配的文本区域,则返回该区域,否则返回 undefined
184
184
  */
185
185
  export const findTextBetweenCoordinates = (text, left, top, right, bottom, options, config = {}) => {
186
186
  return findTextWithinBounds(text, left, top, right - left, bottom - top, options, config);
@@ -191,7 +191,7 @@ export const findTextBetweenCoordinates = (text, left, top, right, bottom, optio
191
191
  * @param direction 搜索方向
192
192
  * @param options 搜索选项
193
193
  * @param config 识别对象配置
194
- * @returns 如果找到匹配的文本区域,则返回该区域
194
+ * @returns 如果找到匹配的文本区域,则返回该区域,否则返回 undefined
195
195
  */
196
196
  export const findTextInDirection = (text, direction, options, config = {}) => {
197
197
  const { x, y, w, h } = directionToBounds(direction);
@@ -240,7 +240,14 @@ export const findWithinListView = async (condition, listView, retryOptions, samp
240
240
  await sleep(50);
241
241
  await mouseScrollDownLines(scrollLines, lineHeight); // 滚动指定行数
242
242
  }, { maxAttempts, retryInterval });
243
- return isFoundOrReachedBottom ? condition(captureListViewRegion()) : undefined;
243
+ if (isFoundOrReachedBottom) {
244
+ const targetRegion = condition(captureListViewRegion());
245
+ if (targetRegion) {
246
+ const { item1, item2 } = targetRegion.convertPositionToGameCaptureRegion(0, 0);
247
+ Object.assign(targetRegion, { x: item1, y: item2 });
248
+ return targetRegion;
249
+ }
250
+ }
244
251
  };
245
252
  /**
246
253
  * 在列表视图中滚动搜索文本
package/dist/store.js CHANGED
@@ -60,7 +60,7 @@ export const useStore = (name) => {
60
60
  * @param defaults 默认值数据对象
61
61
  */
62
62
  export const useStoreWithDefaults = (name, defaults) => {
63
- const store = useStore(name);
64
- Object.assign(store, deepMerge(defaults, store));
65
- return store;
63
+ const newStore = useStore(name);
64
+ Object.assign(newStore, deepMerge(defaults, newStore));
65
+ return newStore;
66
66
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@bettergi/utils",
3
- "version": "0.1.22",
3
+ "version": "0.1.24",
4
4
  "description": "开发 BetterGI 脚本常用工具集",
5
5
  "type": "module",
6
6
  "author": "Bread Grocery<https://github.com/breadgrocery>",