@bettergi/utils 0.1.9 → 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
@@ -48,7 +48,7 @@ await navigateToTab(() => {
48
48
 
49
49
  ```ts
50
50
  // 在整个画面内搜索图片,找不到返回 undefined
51
- const i1 = findImage("assets/关闭.png");
51
+ const i1 = findImage("assets/关闭.png", { use3Channels: true }); // 匹配颜色
52
52
 
53
53
  // 在指定方向上搜索图片,找不到返回 undefined
54
54
  const i2 = findImageInDirection("assets/关闭.png", "north-east");
@@ -97,7 +97,7 @@ await assertRegionDisappearing(findButton, "点击购买按钮超时", () => fin
97
97
 
98
98
  ### 鼠标操作
99
99
 
100
- > 对常见鼠标操作的封装,如鼠标滚动、拖拽等。
100
+ > 对常见鼠标操作的封装,如鼠标的平滑移动、鼠标滚轮滚动、鼠标拖拽等。
101
101
 
102
102
  ```ts
103
103
  // 鼠标从 (745, 610) 平滑自然地移动 (1920, 1080)
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,11 +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 图片路径
10
+ * @param config 识别对象配置
6
11
  * @returns 如果找到匹配的图片区域,则返回该区域
7
12
  */
8
- export declare const findImage: (path: string) => Region | undefined;
13
+ export declare const findImage: (path: string, config?: ROConfig) => Region | undefined;
9
14
  /**
10
15
  * 在指定区域内搜索图片
11
16
  * @param path 图片路径
@@ -13,18 +18,20 @@ export declare const findImage: (path: string) => Region | undefined;
13
18
  * @param y 垂直方向偏移量(像素)
14
19
  * @param w 宽度
15
20
  * @param h 高度
21
+ * @param config 识别对象配置
16
22
  * @returns 如果找到匹配的图片区域,则返回该区域
17
23
  */
18
- export declare const findImageWithinBounds: (path: string, x: number, y: number, w: number, h: number) => Region | undefined;
24
+ export declare const findImageWithinBounds: (path: string, x: number, y: number, w: number, h: number, config?: ROConfig) => Region | undefined;
19
25
  /**
20
26
  * 在指定方向上搜索图片
21
27
  * @param path 图片路径
22
28
  * @param direction 搜索方向
29
+ * @param config 识别对象配置
23
30
  * @returns 如果找到匹配的图片区域,则返回该区域
24
31
  */
25
- export declare const findImageInDirection: (path: string, direction: MatchDirection) => Region | undefined;
32
+ export declare const findImageInDirection: (path: string, direction: MatchDirection, config?: ROConfig) => Region | undefined;
26
33
  /** 文本搜索选项 */
27
- type TextMatchOptions = {
34
+ export type TextMatchOptions = {
28
35
  /** 是否忽略大小写(默认: 是) */
29
36
  ignoreCase?: boolean;
30
37
  /** 是否非完全匹配(默认: 否) */
@@ -34,9 +41,10 @@ type TextMatchOptions = {
34
41
  * 在整个画面内搜索文本
35
42
  * @param text 待搜索文本
36
43
  * @param options 搜索选项
44
+ * @param config 识别对象配置
37
45
  * @returns 如果找到匹配的文本区域,则返回该区域
38
46
  */
39
- export declare const findText: (text: string, options?: TextMatchOptions) => Region | undefined;
47
+ export declare const findText: (text: string, options?: TextMatchOptions, config?: ROConfig) => Region | undefined;
40
48
  /**
41
49
  * 在指定区域内搜索文本
42
50
  * @param text 待搜索文本
@@ -45,17 +53,19 @@ export declare const findText: (text: string, options?: TextMatchOptions) => Reg
45
53
  * @param w 宽度
46
54
  * @param h 高度
47
55
  * @param options 搜索选项
56
+ * @param config 识别对象配置
48
57
  * @returns 如果找到匹配的文本区域,则返回该区域
49
58
  */
50
- 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;
51
60
  /**
52
61
  * 在指定方向上搜索文本
53
62
  * @param text 待搜索文本
54
63
  * @param direction 搜索方向
55
64
  * @param options 搜索选项
65
+ * @param config 识别对象配置
56
66
  * @returns 如果找到匹配的文本区域,则返回该区域
57
67
  */
58
- 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;
59
69
  /** 列表视图参数 */
60
70
  export type ListView = {
61
71
  x: number;
@@ -76,8 +86,8 @@ export type ListView = {
76
86
  * @param text 待搜索文本
77
87
  * @param listView 列表视图参数
78
88
  * @param matchOptions 搜索选项
79
- * @param timeout 搜索超时
89
+ * @param retryOptions 重试选项
90
+ * @param config 识别对象配置
80
91
  * @returns 如果找到匹配的文本区域,则返回该区域,否则返回 undefined
81
92
  */
82
- export declare const findTextWithinListView: (text: string, listView: ListView, matchOptions?: TextMatchOptions, retryOptions?: RetryOptions) => Promise<Region | undefined>;
83
- 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,12 +24,14 @@ const directionToBounds = (direction) => {
24
24
  /**
25
25
  * 在整个画面内搜索图片
26
26
  * @param path 图片路径
27
+ * @param config 识别对象配置
27
28
  * @returns 如果找到匹配的图片区域,则返回该区域
28
29
  */
29
- export const findImage = (path) => {
30
+ export const findImage = (path, config = {}) => {
30
31
  const ir = captureGameRegion();
31
32
  try {
32
33
  const ro = RecognitionObject.templateMatch(file.readImageMatSync(path));
34
+ Object.assign(ro, config);
33
35
  return findFirst(ir, ro, region => region.isExist());
34
36
  }
35
37
  catch (err) {
@@ -46,12 +48,14 @@ export const findImage = (path) => {
46
48
  * @param y 垂直方向偏移量(像素)
47
49
  * @param w 宽度
48
50
  * @param h 高度
51
+ * @param config 识别对象配置
49
52
  * @returns 如果找到匹配的图片区域,则返回该区域
50
53
  */
51
- export const findImageWithinBounds = (path, x, y, w, h) => {
54
+ export const findImageWithinBounds = (path, x, y, w, h, config = {}) => {
52
55
  const ir = captureGameRegion();
53
56
  try {
54
57
  const ro = RecognitionObject.templateMatch(file.readImageMatSync(path), x, y, w, h);
58
+ Object.assign(ro, config);
55
59
  return findFirst(ir, ro, region => region.isExist());
56
60
  }
57
61
  catch (err) {
@@ -65,24 +69,27 @@ export const findImageWithinBounds = (path, x, y, w, h) => {
65
69
  * 在指定方向上搜索图片
66
70
  * @param path 图片路径
67
71
  * @param direction 搜索方向
72
+ * @param config 识别对象配置
68
73
  * @returns 如果找到匹配的图片区域,则返回该区域
69
74
  */
70
- export const findImageInDirection = (path, direction) => {
75
+ export const findImageInDirection = (path, direction, config = {}) => {
71
76
  const { x, y, w, h } = directionToBounds(direction);
72
- return findImageWithinBounds(path, x, y, w, h);
77
+ return findImageWithinBounds(path, x, y, w, h, config);
73
78
  };
74
79
  /**
75
80
  * 在整个画面内搜索文本
76
81
  * @param text 待搜索文本
77
82
  * @param options 搜索选项
83
+ * @param config 识别对象配置
78
84
  * @returns 如果找到匹配的文本区域,则返回该区域
79
85
  */
80
- export const findText = (text, options) => {
86
+ export const findText = (text, options, config = {}) => {
81
87
  const { ignoreCase = true, contains = false } = options || {};
82
88
  const searchText = ignoreCase ? text.toLowerCase() : text;
83
89
  const ir = captureGameRegion();
84
90
  try {
85
91
  const ro = RecognitionObject.ocrThis;
92
+ Object.assign(ro, config);
86
93
  return findFirst(ir, ro, region => {
87
94
  const itemText = ignoreCase ? region.text.toLowerCase() : region.text;
88
95
  const isMatch = contains ? itemText.includes(searchText) : itemText === searchText;
@@ -104,14 +111,16 @@ export const findText = (text, options) => {
104
111
  * @param w 宽度
105
112
  * @param h 高度
106
113
  * @param options 搜索选项
114
+ * @param config 识别对象配置
107
115
  * @returns 如果找到匹配的文本区域,则返回该区域
108
116
  */
109
- export const findTextWithinBounds = (text, x, y, w, h, options) => {
117
+ export const findTextWithinBounds = (text, x, y, w, h, options, config = {}) => {
110
118
  const { ignoreCase = true, contains = false } = options || {};
111
119
  const searchText = ignoreCase ? text.toLowerCase() : text;
112
120
  const ir = captureGameRegion();
113
121
  try {
114
122
  const ro = RecognitionObject.ocr(x, y, w, h);
123
+ Object.assign(ro, config);
115
124
  return findFirst(ir, ro, region => {
116
125
  const itemText = ignoreCase ? region.text.toLowerCase() : region.text;
117
126
  const isMatch = contains ? itemText.includes(searchText) : itemText === searchText;
@@ -130,24 +139,26 @@ export const findTextWithinBounds = (text, x, y, w, h, options) => {
130
139
  * @param text 待搜索文本
131
140
  * @param direction 搜索方向
132
141
  * @param options 搜索选项
142
+ * @param config 识别对象配置
133
143
  * @returns 如果找到匹配的文本区域,则返回该区域
134
144
  */
135
- export const findTextInDirection = (text, direction, options) => {
145
+ export const findTextInDirection = (text, direction, options, config = {}) => {
136
146
  const { x, y, w, h } = directionToBounds(direction);
137
- return findTextWithinBounds(text, x, y, w, h, options);
147
+ return findTextWithinBounds(text, x, y, w, h, options, config);
138
148
  };
139
149
  /**
140
150
  * 在列表视图中滚动搜索文本
141
151
  * @param text 待搜索文本
142
152
  * @param listView 列表视图参数
143
153
  * @param matchOptions 搜索选项
144
- * @param timeout 搜索超时
154
+ * @param retryOptions 重试选项
155
+ * @param config 识别对象配置
145
156
  * @returns 如果找到匹配的文本区域,则返回该区域,否则返回 undefined
146
157
  */
147
- export const findTextWithinListView = async (text, listView, matchOptions, retryOptions) => {
158
+ export const findTextWithinListView = async (text, listView, matchOptions, retryOptions, config = {}) => {
148
159
  const { x, y, w, h, lineHeight, scrollLines = 1, paddingX = 10, paddingY = 10 } = listView;
149
160
  const { maxAttempts = 30, retryInterval = 1000 } = retryOptions || {};
150
- const findTargetText = () => findTextWithinBounds(text, x, y, w, h, matchOptions);
161
+ const findTargetText = () => findTextWithinBounds(text, x, y, w, h, matchOptions, config);
151
162
  let lastTextRegion;
152
163
  const isReachedBottom = () => {
153
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.9",
3
+ "version": "0.1.11",
4
4
  "description": "开发 BetterGI 脚本常用工具集",
5
5
  "type": "module",
6
6
  "author": "Bread Grocery<https://github.com/breadgrocery>",