@bettergi/utils 0.1.10 → 0.1.11

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,13 +47,13 @@ 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 = findImageInDirection("assets/关闭.png", "north-east");
55
55
 
56
- // 在指定区域内搜索图片,相似度0.8(默认),找不到返回 undefined
56
+ // 在指定区域内搜索图片,找不到返回 undefined
57
57
  const i3 = findImageWithinBounds("assets/关闭.png", 960, 0, 960, 1080);
58
58
 
59
59
  // 在整个画面内搜索文本(不包含、忽略大小写),找不到返回 undefined
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,4 +1,4 @@
1
- type MouseWaypointsOptions = {
1
+ export type MouseWaypointsOptions = {
2
2
  /** 是否按住鼠标左键拖动 */
3
3
  shouldDrag?: boolean;
4
4
  /** 超时时间(毫秒,默认: 不超时) */
@@ -22,7 +22,7 @@ export declare const mouseMoveAlongWaypoints: (waypoints: {
22
22
  * @param y2 终止垂直方向偏移量(像素)
23
23
  */
24
24
  export declare const mouseDrag: (x1: number, y1: number, x2: number, y2: number) => Promise<boolean>;
25
- type NaturalMouseMoveOptions = {
25
+ export type NaturalMouseMoveOptions = {
26
26
  /** 移动持续时间(毫秒,默认: 800) */
27
27
  duration: number;
28
28
  /** 摆动幅度(默认: 30) */
@@ -71,4 +71,3 @@ export declare const mouseScrollUpLines: (lines: number, lineHeight?: number) =>
71
71
  * @param lineHeight 行高(默认值为175像素)
72
72
  */
73
73
  export declare const mouseScrollDownLines: (lines: number, lineHeight?: number) => Promise<void>;
74
- export {};
package/dist/ocr.d.ts CHANGED
@@ -1,12 +1,16 @@
1
1
  import { RetryOptions } from "./workflow";
2
- type MatchDirection = "north" /** 上半边 */ | "north-east" /** 右上四分之一 */ | "east" /** 右半边 */ | "south-east" /** 右下四分之一 */ | "south" /** 下半边 */ | "south-west" /** 左下四分之一 */ | "west" /** 左半边 */ | "north-west"; /** 左上四分之一 */
2
+ export type ROInstance = InstanceType<typeof RecognitionObject>;
3
+ export type ROConfig = Partial<{
4
+ [K in keyof ROInstance]: ROInstance[K];
5
+ }>;
6
+ export type MatchDirection = "north" /** 上半边 */ | "north-east" /** 右上四分之一 */ | "east" /** 右半边 */ | "south-east" /** 右下四分之一 */ | "south" /** 下半边 */ | "south-west" /** 左下四分之一 */ | "west" /** 左半边 */ | "north-west"; /** 左上四分之一 */
3
7
  /**
4
8
  * 在整个画面内搜索图片
5
9
  * @param path 图片路径
6
- * @param similarity 相似度阈值(默认: 0.8)
10
+ * @param config 识别对象配置
7
11
  * @returns 如果找到匹配的图片区域,则返回该区域
8
12
  */
9
- export declare const findImage: (path: string, similarity?: number) => Region | undefined;
13
+ export declare const findImage: (path: string, config?: ROConfig) => Region | undefined;
10
14
  /**
11
15
  * 在指定区域内搜索图片
12
16
  * @param path 图片路径
@@ -14,19 +18,20 @@ export declare const findImage: (path: string, similarity?: number) => Region |
14
18
  * @param y 垂直方向偏移量(像素)
15
19
  * @param w 宽度
16
20
  * @param h 高度
17
- * @param similarity 相似度阈值(默认: 0.8)
21
+ * @param config 识别对象配置
18
22
  * @returns 如果找到匹配的图片区域,则返回该区域
19
23
  */
20
- export declare const findImageWithinBounds: (path: string, x: number, y: number, w: number, h: number, similarity?: number) => Region | undefined;
24
+ export declare const findImageWithinBounds: (path: string, x: number, y: number, w: number, h: number, config?: ROConfig) => Region | undefined;
21
25
  /**
22
26
  * 在指定方向上搜索图片
23
27
  * @param path 图片路径
24
28
  * @param direction 搜索方向
29
+ * @param config 识别对象配置
25
30
  * @returns 如果找到匹配的图片区域,则返回该区域
26
31
  */
27
- export declare const findImageInDirection: (path: string, direction: MatchDirection, similarity?: number) => Region | undefined;
32
+ export declare const findImageInDirection: (path: string, direction: MatchDirection, config?: ROConfig) => Region | undefined;
28
33
  /** 文本搜索选项 */
29
- type TextMatchOptions = {
34
+ export type TextMatchOptions = {
30
35
  /** 是否忽略大小写(默认: 是) */
31
36
  ignoreCase?: boolean;
32
37
  /** 是否非完全匹配(默认: 否) */
@@ -36,9 +41,10 @@ type TextMatchOptions = {
36
41
  * 在整个画面内搜索文本
37
42
  * @param text 待搜索文本
38
43
  * @param options 搜索选项
44
+ * @param config 识别对象配置
39
45
  * @returns 如果找到匹配的文本区域,则返回该区域
40
46
  */
41
- export declare const findText: (text: string, options?: TextMatchOptions) => Region | undefined;
47
+ export declare const findText: (text: string, options?: TextMatchOptions, config?: ROConfig) => Region | undefined;
42
48
  /**
43
49
  * 在指定区域内搜索文本
44
50
  * @param text 待搜索文本
@@ -47,17 +53,19 @@ export declare const findText: (text: string, options?: TextMatchOptions) => Reg
47
53
  * @param w 宽度
48
54
  * @param h 高度
49
55
  * @param options 搜索选项
56
+ * @param config 识别对象配置
50
57
  * @returns 如果找到匹配的文本区域,则返回该区域
51
58
  */
52
- export declare const findTextWithinBounds: (text: string, x: number, y: number, w: number, h: number, options?: TextMatchOptions) => Region | undefined;
59
+ export declare const findTextWithinBounds: (text: string, x: number, y: number, w: number, h: number, options?: TextMatchOptions, config?: ROConfig) => Region | undefined;
53
60
  /**
54
61
  * 在指定方向上搜索文本
55
62
  * @param text 待搜索文本
56
63
  * @param direction 搜索方向
57
64
  * @param options 搜索选项
65
+ * @param config 识别对象配置
58
66
  * @returns 如果找到匹配的文本区域,则返回该区域
59
67
  */
60
- export declare const findTextInDirection: (text: string, direction: MatchDirection, options?: TextMatchOptions) => Region | undefined;
68
+ export declare const findTextInDirection: (text: string, direction: MatchDirection, options?: TextMatchOptions, config?: ROConfig) => Region | undefined;
61
69
  /** 列表视图参数 */
62
70
  export type ListView = {
63
71
  x: number;
@@ -78,8 +86,8 @@ export type ListView = {
78
86
  * @param text 待搜索文本
79
87
  * @param listView 列表视图参数
80
88
  * @param matchOptions 搜索选项
81
- * @param timeout 搜索超时
89
+ * @param retryOptions 重试选项
90
+ * @param config 识别对象配置
82
91
  * @returns 如果找到匹配的文本区域,则返回该区域,否则返回 undefined
83
92
  */
84
- export declare const findTextWithinListView: (text: string, listView: ListView, matchOptions?: TextMatchOptions, retryOptions?: RetryOptions) => Promise<Region | undefined>;
85
- export {};
93
+ 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) {
@@ -69,24 +69,27 @@ export const findImageWithinBounds = (path, x, y, w, h, similarity = 0.8) => {
69
69
  * 在指定方向上搜索图片
70
70
  * @param path 图片路径
71
71
  * @param direction 搜索方向
72
+ * @param config 识别对象配置
72
73
  * @returns 如果找到匹配的图片区域,则返回该区域
73
74
  */
74
- export const findImageInDirection = (path, direction, similarity = 0.8) => {
75
+ export const findImageInDirection = (path, direction, config = {}) => {
75
76
  const { x, y, w, h } = directionToBounds(direction);
76
- return findImageWithinBounds(path, x, y, w, h, similarity);
77
+ return findImageWithinBounds(path, x, y, w, h, config);
77
78
  };
78
79
  /**
79
80
  * 在整个画面内搜索文本
80
81
  * @param text 待搜索文本
81
82
  * @param options 搜索选项
83
+ * @param config 识别对象配置
82
84
  * @returns 如果找到匹配的文本区域,则返回该区域
83
85
  */
84
- export const findText = (text, options) => {
86
+ export const findText = (text, options, config = {}) => {
85
87
  const { ignoreCase = true, contains = false } = options || {};
86
88
  const searchText = ignoreCase ? text.toLowerCase() : text;
87
89
  const ir = captureGameRegion();
88
90
  try {
89
91
  const ro = RecognitionObject.ocrThis;
92
+ Object.assign(ro, config);
90
93
  return findFirst(ir, ro, region => {
91
94
  const itemText = ignoreCase ? region.text.toLowerCase() : region.text;
92
95
  const isMatch = contains ? itemText.includes(searchText) : itemText === searchText;
@@ -108,14 +111,16 @@ export const findText = (text, options) => {
108
111
  * @param w 宽度
109
112
  * @param h 高度
110
113
  * @param options 搜索选项
114
+ * @param config 识别对象配置
111
115
  * @returns 如果找到匹配的文本区域,则返回该区域
112
116
  */
113
- export const findTextWithinBounds = (text, x, y, w, h, options) => {
117
+ export const findTextWithinBounds = (text, x, y, w, h, options, config = {}) => {
114
118
  const { ignoreCase = true, contains = false } = options || {};
115
119
  const searchText = ignoreCase ? text.toLowerCase() : text;
116
120
  const ir = captureGameRegion();
117
121
  try {
118
122
  const ro = RecognitionObject.ocr(x, y, w, h);
123
+ Object.assign(ro, config);
119
124
  return findFirst(ir, ro, region => {
120
125
  const itemText = ignoreCase ? region.text.toLowerCase() : region.text;
121
126
  const isMatch = contains ? itemText.includes(searchText) : itemText === searchText;
@@ -134,24 +139,26 @@ export const findTextWithinBounds = (text, x, y, w, h, options) => {
134
139
  * @param text 待搜索文本
135
140
  * @param direction 搜索方向
136
141
  * @param options 搜索选项
142
+ * @param config 识别对象配置
137
143
  * @returns 如果找到匹配的文本区域,则返回该区域
138
144
  */
139
- export const findTextInDirection = (text, direction, options) => {
145
+ export const findTextInDirection = (text, direction, options, config = {}) => {
140
146
  const { x, y, w, h } = directionToBounds(direction);
141
- return findTextWithinBounds(text, x, y, w, h, options);
147
+ return findTextWithinBounds(text, x, y, w, h, options, config);
142
148
  };
143
149
  /**
144
150
  * 在列表视图中滚动搜索文本
145
151
  * @param text 待搜索文本
146
152
  * @param listView 列表视图参数
147
153
  * @param matchOptions 搜索选项
148
- * @param timeout 搜索超时
154
+ * @param retryOptions 重试选项
155
+ * @param config 识别对象配置
149
156
  * @returns 如果找到匹配的文本区域,则返回该区域,否则返回 undefined
150
157
  */
151
- export const findTextWithinListView = async (text, listView, matchOptions, retryOptions) => {
158
+ export const findTextWithinListView = async (text, listView, matchOptions, retryOptions, config = {}) => {
152
159
  const { x, y, w, h, lineHeight, scrollLines = 1, paddingX = 10, paddingY = 10 } = listView;
153
160
  const { maxAttempts = 30, retryInterval = 1000 } = retryOptions || {};
154
- const findTargetText = () => findTextWithinBounds(text, x, y, w, h, matchOptions);
161
+ const findTargetText = () => findTextWithinBounds(text, x, y, w, h, matchOptions, config);
155
162
  let lastTextRegion;
156
163
  const isReachedBottom = () => {
157
164
  const textRegion = findFirst(captureGameRegion(), RecognitionObject.ocr(x, y, w, h), region => {
@@ -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.11",
4
4
  "description": "开发 BetterGI 脚本常用工具集",
5
5
  "type": "module",
6
6
  "author": "Bread Grocery<https://github.com/breadgrocery>",