@bettergi/utils 0.0.8 → 0.0.10
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 +9 -1
- package/dist/flow.d.ts +1 -1
- package/dist/flow.js +4 -4
- package/dist/game.d.ts +0 -1
- package/dist/game.js +6 -7
- package/dist/http.js +2 -2
- package/dist/mouse.js +8 -12
- package/dist/ocr.d.ts +0 -2
- package/dist/ocr.js +2 -4
- package/package.json +3 -3
- package/dist/action.d.ts +0 -10
- package/dist/action.js +0 -20
- package/dist/time.d.ts +0 -0
- package/dist/time.js +0 -0
package/README.md
CHANGED
|
@@ -1,11 +1,19 @@
|
|
|
1
|
-
本项目是一个为[Better Genshin Impact](https://github.com/babalae/better-genshin-impact) 设计的 JavaScript
|
|
1
|
+
本项目是一个为[Better Genshin Impact](https://github.com/babalae/better-genshin-impact) 设计的 JavaScript 开发工具集,旨在帮助开发者简化代码、不用重复造轮子。工具函数支持按需引入,轻量依赖。
|
|
2
2
|
|
|
3
3
|
## 安装
|
|
4
4
|
|
|
5
|
+
### 使用 npm
|
|
6
|
+
|
|
5
7
|
```shell
|
|
6
8
|
npm install @bettergi/utils
|
|
7
9
|
```
|
|
8
10
|
|
|
11
|
+
### 使用 pnpm
|
|
12
|
+
|
|
13
|
+
```shell
|
|
14
|
+
pnpm install @bettergi/utils
|
|
15
|
+
```
|
|
16
|
+
|
|
9
17
|
## 函数清单
|
|
10
18
|
|
|
11
19
|
### 游戏内操作
|
package/dist/flow.d.ts
CHANGED
|
@@ -7,4 +7,4 @@
|
|
|
7
7
|
* @returns - true 在超时前条件已满足
|
|
8
8
|
* - false 在超时后条件仍未满足
|
|
9
9
|
*/
|
|
10
|
-
export declare const waitUntil: (condition: (context: Record<string, any>) => boolean, timeout?: number, interval?: number, action?: (context: Record<string, any>) => void) => Promise<boolean>;
|
|
10
|
+
export declare const waitUntil: (condition: (context: Record<string, any>) => boolean, timeout?: number, interval?: number, action?: (context: Record<string, any>) => Promise<void> | void) => Promise<boolean>;
|
package/dist/flow.js
CHANGED
|
@@ -7,14 +7,14 @@
|
|
|
7
7
|
* @returns - true 在超时前条件已满足
|
|
8
8
|
* - false 在超时后条件仍未满足
|
|
9
9
|
*/
|
|
10
|
-
export const waitUntil = async (condition, timeout, interval, action) => {
|
|
10
|
+
export const waitUntil = async (condition, timeout = 3000, interval = 300, action) => {
|
|
11
11
|
const context = {};
|
|
12
|
-
const deadline = Date.now() +
|
|
12
|
+
const deadline = Date.now() + timeout;
|
|
13
13
|
while (Date.now() < deadline) {
|
|
14
14
|
if (condition(context))
|
|
15
15
|
return true;
|
|
16
|
-
action?.(context);
|
|
17
|
-
await sleep(interval
|
|
16
|
+
await action?.(context);
|
|
17
|
+
await sleep(interval);
|
|
18
18
|
}
|
|
19
19
|
return false;
|
|
20
20
|
};
|
package/dist/game.d.ts
CHANGED
package/dist/game.js
CHANGED
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
import "@bettergi/types/csharp/BetterGenshinImpact/GameTask/Model/Area/Region";
|
|
2
1
|
import { waitUntil } from "./flow";
|
|
3
2
|
import { mouseSlide } from "./mouse";
|
|
4
3
|
import { findTextInDirection, findTextWithinBounds, findTextWithinListView } from "./ocr";
|
|
@@ -20,11 +19,11 @@ export const openPaimonMenu = async () => {
|
|
|
20
19
|
* @param reverse 是否反向搜索(可选)
|
|
21
20
|
* @param config 搜索参数(可选)
|
|
22
21
|
*/
|
|
23
|
-
export const openMenu = async (name, reverse, config) => {
|
|
22
|
+
export const openMenu = async (name, reverse = false, config = {}) => {
|
|
24
23
|
// 1.打开派蒙菜单
|
|
25
24
|
await openPaimonMenu();
|
|
26
25
|
// 2.搜索菜单按钮
|
|
27
|
-
const { x = 50, step = 30, timeout = 3000 } = config
|
|
26
|
+
const { x = 50, step = 30, timeout = 3000 } = config;
|
|
28
27
|
const findTooltip = () => findTextWithinBounds(name, false, true, 0, 0, x + 150, genshin.height);
|
|
29
28
|
let result = undefined;
|
|
30
29
|
const steps = Math.ceil(genshin.height / step);
|
|
@@ -53,11 +52,11 @@ export const openMenu = async (name, reverse, config) => {
|
|
|
53
52
|
* @param name 菜单页面名称
|
|
54
53
|
* @param config 菜单页面视图参数
|
|
55
54
|
*/
|
|
56
|
-
export const openMenuPage = async (name, config) => {
|
|
55
|
+
export const openMenuPage = async (name, config = {}) => {
|
|
57
56
|
// 1.打开派蒙菜单
|
|
58
57
|
await openPaimonMenu();
|
|
59
58
|
// 2.搜索菜单页面
|
|
60
|
-
const { x = 100, y = 330, w = 670, h = 730, lineHeight = 142 } = config
|
|
59
|
+
const { x = 100, y = 330, w = 670, h = 730, lineHeight = 142 } = config;
|
|
61
60
|
const pageButton = await findTextWithinListView(name, false, true, {
|
|
62
61
|
x,
|
|
63
62
|
y,
|
|
@@ -76,11 +75,11 @@ export const openMenuPage = async (name, config) => {
|
|
|
76
75
|
* @param period 时间段
|
|
77
76
|
* @param config 时钟参数
|
|
78
77
|
*/
|
|
79
|
-
export const setTime = async (period, config) => {
|
|
78
|
+
export const setTime = async (period, config = {}) => {
|
|
80
79
|
// 1.打开时间页面
|
|
81
80
|
await openMenu("时间", true);
|
|
82
81
|
// 2.拨动指针
|
|
83
|
-
const { centerX = 1440, centerY = 502, radius = 400, offset = 5 } = config
|
|
82
|
+
const { centerX = 1440, centerY = 502, radius = 400, offset = 5 } = config;
|
|
84
83
|
const index = ["night", "morning", "noon", "evening"].indexOf(period);
|
|
85
84
|
const periodsDirections = [
|
|
86
85
|
() => mouseSlide(centerX, centerY, centerX - offset, centerY + radius),
|
package/dist/http.js
CHANGED
|
@@ -6,8 +6,8 @@
|
|
|
6
6
|
* @param headers 请求头
|
|
7
7
|
* @returns 响应体内容
|
|
8
8
|
*/
|
|
9
|
-
export const requestForBody = async (method, url, body, headers) => {
|
|
10
|
-
const resp = await http.request(method, url, body
|
|
9
|
+
export const requestForBody = async (method, url, body = "null", headers) => {
|
|
10
|
+
const resp = await http.request(method, url, body, headers ? JSON.stringify(headers) : "null");
|
|
11
11
|
if (resp.status_code >= 200 && resp.status_code < 400) {
|
|
12
12
|
return resp.body;
|
|
13
13
|
}
|
package/dist/mouse.js
CHANGED
|
@@ -10,36 +10,32 @@ const _simulateScroll = async (scrollAmountInClicks, times) => {
|
|
|
10
10
|
* @param height 滚动高度
|
|
11
11
|
* @param algorithm 自定义滚动算法函数,接收高度参数并返回滚动次数(默认算法为每18像素滚动一次)
|
|
12
12
|
*/
|
|
13
|
-
export const mouseScrollUp = (height, algorithm) => {
|
|
14
|
-
|
|
15
|
-
const scrollTimes = scrollAlgorithm(height);
|
|
16
|
-
return _simulateScroll(120, scrollTimes);
|
|
13
|
+
export const mouseScrollUp = (height, algorithm = h => Math.floor(h / 18)) => {
|
|
14
|
+
return _simulateScroll(120, algorithm(height));
|
|
17
15
|
};
|
|
18
16
|
/**
|
|
19
17
|
* 鼠标滚轮向下滚动指定高度
|
|
20
18
|
* @param height 滚动高度
|
|
21
19
|
* @param algorithm 自定义滚动算法函数,接收高度参数并返回滚动次数(默认算法为每18像素滚动一次)
|
|
22
20
|
*/
|
|
23
|
-
export const mouseScrollDown = (height, algorithm) => {
|
|
24
|
-
|
|
25
|
-
const scrollTimes = scrollAlgorithm(height);
|
|
26
|
-
return _simulateScroll(-120, scrollTimes);
|
|
21
|
+
export const mouseScrollDown = (height, algorithm = h => Math.floor(h / 18)) => {
|
|
22
|
+
return _simulateScroll(-120, algorithm(height));
|
|
27
23
|
};
|
|
28
24
|
/**
|
|
29
25
|
* 鼠标滚轮向上滚动指定行数
|
|
30
26
|
* @param lines 滚动行数
|
|
31
27
|
* @param lineHeight 行高(默认值为175像素)
|
|
32
28
|
*/
|
|
33
|
-
export const mouseScrollUpLines = (lines, lineHeight) => {
|
|
34
|
-
return mouseScrollUp(lines *
|
|
29
|
+
export const mouseScrollUpLines = (lines, lineHeight = 175) => {
|
|
30
|
+
return mouseScrollUp(lines * lineHeight);
|
|
35
31
|
};
|
|
36
32
|
/**
|
|
37
33
|
* 鼠标滚轮向下滚动指定行数
|
|
38
34
|
* @param lines 滚动行数
|
|
39
35
|
* @param lineHeight 行高(默认值为175像素)
|
|
40
36
|
*/
|
|
41
|
-
export const mouseScrollDownLines = (lines, lineHeight) => {
|
|
42
|
-
return mouseScrollDown(lines *
|
|
37
|
+
export const mouseScrollDownLines = (lines, lineHeight = 175) => {
|
|
38
|
+
return mouseScrollDown(lines * lineHeight);
|
|
43
39
|
};
|
|
44
40
|
/**
|
|
45
41
|
* 鼠标拖拽滑动到指定位置
|
package/dist/ocr.d.ts
CHANGED
|
@@ -1,5 +1,3 @@
|
|
|
1
|
-
import "@bettergi/types/csharp/BetterGenshinImpact/GameTask/Model/Area/ImageRegion";
|
|
2
|
-
import "@bettergi/types/csharp/BetterGenshinImpact/GameTask/Model/Area/Region";
|
|
3
1
|
type Direction = "north" | "north-east" | "east" | "south-east" | "south" | "south-west" | "west" | "north-west";
|
|
4
2
|
/**
|
|
5
3
|
* 在整个画面内搜索图片
|
package/dist/ocr.js
CHANGED
|
@@ -1,5 +1,3 @@
|
|
|
1
|
-
import "@bettergi/types/csharp/BetterGenshinImpact/GameTask/Model/Area/ImageRegion";
|
|
2
|
-
import "@bettergi/types/csharp/BetterGenshinImpact/GameTask/Model/Area/Region";
|
|
3
1
|
import { waitUntil } from "./flow";
|
|
4
2
|
import { mouseScrollDownLines } from "./mouse";
|
|
5
3
|
const findFirst = (ir, ro, predicate) => {
|
|
@@ -121,7 +119,7 @@ export const findTextInDirection = (text, contains, ignoreCase, direction) => {
|
|
|
121
119
|
* @param timeout 搜索超时
|
|
122
120
|
* @returns 如果找到匹配的文本区域,则返回该区域,否则返回 undefined
|
|
123
121
|
*/
|
|
124
|
-
export const findTextWithinListView = async (text, contains, ignoreCase, listView, timeout) => {
|
|
122
|
+
export const findTextWithinListView = async (text, contains, ignoreCase, listView, timeout = 30 * 1000) => {
|
|
125
123
|
const { x, y, w, h, maxListItems, lineHeight, padding = 10 } = listView;
|
|
126
124
|
const find = () => {
|
|
127
125
|
return findTextWithinBounds(text, contains, ignoreCase, x, y, w, h);
|
|
@@ -143,7 +141,7 @@ export const findTextWithinListView = async (text, contains, ignoreCase, listVie
|
|
|
143
141
|
return true;
|
|
144
142
|
}
|
|
145
143
|
};
|
|
146
|
-
const ok = await waitUntil(() => find() != undefined || isBottomTouched(), timeout
|
|
144
|
+
const ok = await waitUntil(() => find() != undefined || isBottomTouched(), timeout, 1000, async () => {
|
|
147
145
|
moveMouseTo(x + w - padding, y + padding);
|
|
148
146
|
await mouseScrollDownLines(maxListItems, lineHeight);
|
|
149
147
|
});
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@bettergi/utils",
|
|
3
|
-
"version": "0.0.
|
|
4
|
-
"description": "
|
|
3
|
+
"version": "0.0.10",
|
|
4
|
+
"description": "开发 BetterGI 脚本常用工具集",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"author": "Bread Grocery<https://github.com/breadgrocery>",
|
|
7
7
|
"license": "MIT",
|
|
@@ -33,7 +33,7 @@
|
|
|
33
33
|
"build": "tsc"
|
|
34
34
|
},
|
|
35
35
|
"devDependencies": {
|
|
36
|
-
"@bettergi/types": "^0.0.
|
|
36
|
+
"@bettergi/types": "^0.0.13",
|
|
37
37
|
"typescript": "5.6.3"
|
|
38
38
|
}
|
|
39
39
|
}
|
package/dist/action.d.ts
DELETED
|
@@ -1,10 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* 等待直到条件满足或超时
|
|
3
|
-
* @param condition 等待的条件判断函数,返回 true 表示条件满足
|
|
4
|
-
* @param timeout 超时时间(毫秒),默认 5000 毫秒
|
|
5
|
-
* @param interval 等待间隔(毫秒),默认 200 毫秒
|
|
6
|
-
* @param action 每次等待循环中执行的操作(可选)
|
|
7
|
-
* @returns - true 在超时前条件已满足
|
|
8
|
-
* - false 在超时后条件仍未满足
|
|
9
|
-
*/
|
|
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
DELETED
|
@@ -1,20 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* 等待直到条件满足或超时
|
|
3
|
-
* @param condition 等待的条件判断函数,返回 true 表示条件满足
|
|
4
|
-
* @param timeout 超时时间(毫秒),默认 5000 毫秒
|
|
5
|
-
* @param interval 等待间隔(毫秒),默认 200 毫秒
|
|
6
|
-
* @param action 每次等待循环中执行的操作(可选)
|
|
7
|
-
* @returns - true 在超时前条件已满足
|
|
8
|
-
* - false 在超时后条件仍未满足
|
|
9
|
-
*/
|
|
10
|
-
export const waitUntil = async (condition, timeout, interval, action) => {
|
|
11
|
-
const context = {};
|
|
12
|
-
const deadline = Date.now() + (timeout ?? 5 * 1000);
|
|
13
|
-
while (Date.now() < deadline) {
|
|
14
|
-
if (condition(context))
|
|
15
|
-
return true;
|
|
16
|
-
action?.(context);
|
|
17
|
-
await sleep(interval ?? 200);
|
|
18
|
-
}
|
|
19
|
-
return false;
|
|
20
|
-
};
|
package/dist/time.d.ts
DELETED
|
File without changes
|
package/dist/time.js
DELETED
|
File without changes
|