@bettergi/utils 0.0.1 → 0.0.2
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/dist/action.d.ts +2 -2
- package/dist/action.js +4 -3
- package/dist/ocr.d.ts +18 -0
- package/dist/ocr.js +40 -12
- package/package.json +1 -1
package/dist/action.d.ts
CHANGED
|
@@ -4,7 +4,7 @@
|
|
|
4
4
|
* @param timeout 超时时间(毫秒),默认 5000 毫秒
|
|
5
5
|
* @param interval 等待间隔(毫秒),默认 200 毫秒
|
|
6
6
|
* @param action 每次等待循环中执行的操作(可选)
|
|
7
|
-
* @returns - true
|
|
7
|
+
* @returns - true 在超时前条件已满足
|
|
8
8
|
* - false 在超时后条件仍未满足
|
|
9
9
|
*/
|
|
10
|
-
export declare const waitUntil: (condition: () => boolean, timeout?: number, interval?: number, action?: () => void) => Promise<boolean>;
|
|
10
|
+
export declare const waitUntil: (condition: (context: Record<string, any>) => boolean, timeout?: number, interval?: number, action?: (context: Record<string, any>) => void) => Promise<boolean>;
|
package/dist/action.js
CHANGED
|
@@ -4,15 +4,16 @@
|
|
|
4
4
|
* @param timeout 超时时间(毫秒),默认 5000 毫秒
|
|
5
5
|
* @param interval 等待间隔(毫秒),默认 200 毫秒
|
|
6
6
|
* @param action 每次等待循环中执行的操作(可选)
|
|
7
|
-
* @returns - true
|
|
7
|
+
* @returns - true 在超时前条件已满足
|
|
8
8
|
* - false 在超时后条件仍未满足
|
|
9
9
|
*/
|
|
10
10
|
export const waitUntil = async (condition, timeout, interval, action) => {
|
|
11
|
+
const context = {};
|
|
11
12
|
const deadline = Date.now() + (timeout ?? 5 * 1000);
|
|
12
13
|
while (Date.now() < deadline) {
|
|
13
|
-
if (condition())
|
|
14
|
+
if (condition(context))
|
|
14
15
|
return true;
|
|
15
|
-
action?.();
|
|
16
|
+
action?.(context);
|
|
16
17
|
await sleep(interval ?? 200);
|
|
17
18
|
}
|
|
18
19
|
return false;
|
package/dist/ocr.d.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { type Region } from "@bettergi/types/csharp/BetterGenshinImpact/GameTask/Model/Area/Region";
|
|
2
|
+
type Direction = "north" | "north-east" | "east" | "south-east" | "south" | "south-west" | "west" | "north-west";
|
|
2
3
|
/**
|
|
3
4
|
* 搜索图片
|
|
4
5
|
* @param path 图片路径
|
|
@@ -15,6 +16,13 @@ export declare const findImage: (path: string) => Region | undefined;
|
|
|
15
16
|
* @returns 如果找到匹配的图片区域,则返回该区域,否则返回 undefined
|
|
16
17
|
*/
|
|
17
18
|
export declare const findImageWithinBounds: (path: string, x: number, y: number, w: number, h: number) => Region | undefined;
|
|
19
|
+
/**
|
|
20
|
+
* 在指定方向上搜索图片
|
|
21
|
+
* @param path 图片路径
|
|
22
|
+
* @param direction 搜索方向
|
|
23
|
+
* @returns 如果找到匹配的图片区域,则返回该区域,否则返回 undefined
|
|
24
|
+
*/
|
|
25
|
+
export declare const findImageInDirection: (path: string, direction: Direction) => Region | undefined;
|
|
18
26
|
/**
|
|
19
27
|
* 搜索文本
|
|
20
28
|
* @param text 待搜索文本
|
|
@@ -36,3 +44,13 @@ export declare const findText: (text: string, contains: boolean, ignoreCase: boo
|
|
|
36
44
|
* @returns 如果找到匹配的文本区域,则返回该区域,否则返回 undefined
|
|
37
45
|
*/
|
|
38
46
|
export declare const findTextWithinBounds: (text: string, contains: boolean, ignoreCase: boolean, x: number, y: number, w: number, h: number) => Region | undefined;
|
|
47
|
+
/**
|
|
48
|
+
* 在指定方向上搜索文本
|
|
49
|
+
* @param text 待搜索文本
|
|
50
|
+
* @param contains 是否包含
|
|
51
|
+
* @param ignoreCase 是否忽略大小写
|
|
52
|
+
* @param direction 搜索方向
|
|
53
|
+
* @returns 如果找到匹配的文本区域,则返回该区域,否则返回 undefined
|
|
54
|
+
*/
|
|
55
|
+
export declare const findTextInDirection: (text: string, contains: boolean, ignoreCase: boolean, direction: Direction) => Region | undefined;
|
|
56
|
+
export {};
|
package/dist/ocr.js
CHANGED
|
@@ -1,12 +1,18 @@
|
|
|
1
|
-
const findFirst = (
|
|
2
|
-
const candidates =
|
|
1
|
+
const findFirst = (ir, ro, predicate) => {
|
|
2
|
+
const candidates = ir.findMulti(ro);
|
|
3
3
|
for (let i = 0; i < candidates.count; i++) {
|
|
4
|
-
if (predicate(candidates[i]))
|
|
4
|
+
if (predicate(candidates[i]))
|
|
5
5
|
return candidates[i];
|
|
6
|
-
}
|
|
7
6
|
}
|
|
8
7
|
return undefined;
|
|
9
8
|
};
|
|
9
|
+
const directionToBounds = (direction) => {
|
|
10
|
+
const x = direction.includes("east") ? genshin.width / 2 : 0;
|
|
11
|
+
const y = direction.includes("south") ? genshin.height / 2 : 0;
|
|
12
|
+
const w = direction === "north" || direction === "south" ? genshin.width : genshin.width / 2;
|
|
13
|
+
const h = direction === "west" || direction === "east" ? genshin.height : genshin.height / 2;
|
|
14
|
+
return { x, y, w, h };
|
|
15
|
+
};
|
|
10
16
|
/**
|
|
11
17
|
* 搜索图片
|
|
12
18
|
* @param path 图片路径
|
|
@@ -14,9 +20,9 @@ const findFirst = (im, ro, predicate) => {
|
|
|
14
20
|
*/
|
|
15
21
|
export const findImage = (path) => {
|
|
16
22
|
try {
|
|
17
|
-
const
|
|
23
|
+
const ir = captureGameRegion();
|
|
18
24
|
const ro = RecognitionObject.templateMatch(file.readImageMatSync(path));
|
|
19
|
-
return findFirst(
|
|
25
|
+
return findFirst(ir, ro, region => region.isExist());
|
|
20
26
|
}
|
|
21
27
|
catch (err) {
|
|
22
28
|
err?.message && log.warn(`${err.message}`);
|
|
@@ -33,14 +39,24 @@ export const findImage = (path) => {
|
|
|
33
39
|
*/
|
|
34
40
|
export const findImageWithinBounds = (path, x, y, w, h) => {
|
|
35
41
|
try {
|
|
36
|
-
const
|
|
42
|
+
const ir = captureGameRegion();
|
|
37
43
|
const ro = RecognitionObject.templateMatch(file.readImageMatSync(path), x, y, w, h);
|
|
38
|
-
return findFirst(
|
|
44
|
+
return findFirst(ir, ro, region => region.isExist());
|
|
39
45
|
}
|
|
40
46
|
catch (err) {
|
|
41
47
|
err?.message && log.warn(`${err.message}`);
|
|
42
48
|
}
|
|
43
49
|
};
|
|
50
|
+
/**
|
|
51
|
+
* 在指定方向上搜索图片
|
|
52
|
+
* @param path 图片路径
|
|
53
|
+
* @param direction 搜索方向
|
|
54
|
+
* @returns 如果找到匹配的图片区域,则返回该区域,否则返回 undefined
|
|
55
|
+
*/
|
|
56
|
+
export const findImageInDirection = (path, direction) => {
|
|
57
|
+
const { x, y, w, h } = directionToBounds(direction);
|
|
58
|
+
return findImageWithinBounds(path, x, y, w, h);
|
|
59
|
+
};
|
|
44
60
|
/**
|
|
45
61
|
* 搜索文本
|
|
46
62
|
* @param text 待搜索文本
|
|
@@ -50,9 +66,9 @@ export const findImageWithinBounds = (path, x, y, w, h) => {
|
|
|
50
66
|
*/
|
|
51
67
|
export const findText = (text, contains, ignoreCase) => {
|
|
52
68
|
const searchText = ignoreCase ? text.toLowerCase() : text;
|
|
53
|
-
const
|
|
69
|
+
const ir = captureGameRegion();
|
|
54
70
|
const ro = RecognitionObject.ocrThis;
|
|
55
|
-
return findFirst(
|
|
71
|
+
return findFirst(ir, ro, region => {
|
|
56
72
|
const itemText = ignoreCase ? region.text.toLowerCase() : region.text;
|
|
57
73
|
const isMatch = contains ? itemText.includes(searchText) : itemText === searchText;
|
|
58
74
|
return isMatch && region.isExist();
|
|
@@ -72,11 +88,23 @@ export const findText = (text, contains, ignoreCase) => {
|
|
|
72
88
|
*/
|
|
73
89
|
export const findTextWithinBounds = (text, contains, ignoreCase, x, y, w, h) => {
|
|
74
90
|
const searchText = ignoreCase ? text.toLowerCase() : text;
|
|
75
|
-
const
|
|
91
|
+
const ir = captureGameRegion();
|
|
76
92
|
const ro = RecognitionObject.ocr(x, y, w, h);
|
|
77
|
-
return findFirst(
|
|
93
|
+
return findFirst(ir, ro, region => {
|
|
78
94
|
const itemText = ignoreCase ? region.text.toLowerCase() : region.text;
|
|
79
95
|
const isMatch = contains ? itemText.includes(searchText) : itemText === searchText;
|
|
80
96
|
return isMatch && region.isExist();
|
|
81
97
|
});
|
|
82
98
|
};
|
|
99
|
+
/**
|
|
100
|
+
* 在指定方向上搜索文本
|
|
101
|
+
* @param text 待搜索文本
|
|
102
|
+
* @param contains 是否包含
|
|
103
|
+
* @param ignoreCase 是否忽略大小写
|
|
104
|
+
* @param direction 搜索方向
|
|
105
|
+
* @returns 如果找到匹配的文本区域,则返回该区域,否则返回 undefined
|
|
106
|
+
*/
|
|
107
|
+
export const findTextInDirection = (text, contains, ignoreCase, direction) => {
|
|
108
|
+
const { x, y, w, h } = directionToBounds(direction);
|
|
109
|
+
return findTextWithinBounds(text, contains, ignoreCase, x, y, w, h);
|
|
110
|
+
};
|