@bettergi/utils 0.0.9 → 0.0.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
@@ -74,7 +74,14 @@ const t3 = findTextWithinBounds("确认", false, true, 960, 540, 960, 540);
74
74
  > 对脚本开发过程中常见工作流的抽象,例如:等待 XXX 完成/出现/消失。
75
75
 
76
76
  ```ts
77
- import { findImageInDirection, waitUntil } from "@bettergi/utils";
77
+ import {
78
+ assertExists,
79
+ assertNotExists,
80
+ findImageInDirection,
81
+ findTextInDirection,
82
+ findTextWithinBounds,
83
+ waitUntil
84
+ } from "@bettergi/utils";
78
85
 
79
86
  // 等待直到找不到[关闭按钮] 或 5秒后超时,每隔1秒检查一次,期间按 Esc 键
80
87
  const done = await waitUntil(
@@ -84,6 +91,19 @@ const done = await waitUntil(
84
91
  () => keyPress("ESCAPE")
85
92
  );
86
93
  if (!done) throw new Error("关闭页面超时");
94
+
95
+ // 断言 "世界等级" 区域存在 或 5秒后超时抛出异常,每隔1秒检查一次,期间按 Esc 键
96
+ await assertExists(
97
+ () => findTextInDirection("世界等级", false, true, "north-west"),
98
+ "打开派蒙菜单超时",
99
+ 5000,
100
+ 1000,
101
+ () => keyPress("ESCAPE")
102
+ );
103
+
104
+ // 断言 "购买" 区域不存在 或 5秒后超时抛出异常,每隔1秒检查一次,期间如果存在 "购买" 按钮则点击
105
+ const findButton = () => findTextWithinBounds("购买", true, true, 500, 740, 900, 110);
106
+ await assertNotExists(findButton, "点击购买按钮超时", 5000, 1000, () => findButton()?.click());
87
107
  ```
88
108
 
89
109
  ### 鼠标操作
package/dist/flow.d.ts CHANGED
@@ -7,4 +7,24 @@
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>;
11
+ /**
12
+ * 断言区域存在
13
+ * @param 获取区域的函数
14
+ * @param message 错误信息
15
+ * @param timeout 超时时间(毫秒),默认 3000 毫秒
16
+ * @param interval 等待间隔(毫秒),默认 300 毫秒
17
+ * @param action 每次等待循环中执行的操作(可选)
18
+ * @throws 如果区域在超时时间内未找到则抛出错误
19
+ */
20
+ export declare const assertExists: (regionProvider: () => Region | undefined, message?: string, timeout?: number, interval?: number, action?: (context: Record<string, any>) => Promise<void> | void) => Promise<void>;
21
+ /**
22
+ * 断言区域不存在
23
+ * @param 获取区域的函数
24
+ * @param message 错误信息
25
+ * @param timeout 超时时间(毫秒),默认 3000 毫秒
26
+ * @param interval 等待间隔(毫秒),默认 300 毫秒
27
+ * @param action 每次等待循环中执行的操作(可选)
28
+ * @throws 如果区域在超时时间内仍然存在则抛出错误
29
+ */
30
+ export declare const assertNotExists: (regionProvider: () => Region | undefined, message?: string, timeout?: number, interval?: number, action?: (context: Record<string, any>) => Promise<void> | void) => Promise<void>;
package/dist/flow.js CHANGED
@@ -1,3 +1,7 @@
1
+ /** 默认超时时间(毫秒) */
2
+ const defaultTimeout = 3 * 1000;
3
+ /** 默认等待间隔(毫秒) */
4
+ const defaultInterval = 300;
1
5
  /**
2
6
  * 等待直到条件满足或超时
3
7
  * @param condition 等待的条件判断函数,返回 true 表示条件满足
@@ -7,14 +11,42 @@
7
11
  * @returns - true 在超时前条件已满足
8
12
  * - false 在超时后条件仍未满足
9
13
  */
10
- export const waitUntil = async (condition, timeout, interval, action) => {
14
+ export const waitUntil = async (condition, timeout = defaultTimeout, interval = defaultInterval, action) => {
11
15
  const context = {};
12
- const deadline = Date.now() + (timeout ?? 3 * 1000);
16
+ const deadline = Date.now() + timeout;
13
17
  while (Date.now() < deadline) {
14
18
  if (condition(context))
15
19
  return true;
16
- action?.(context);
17
- await sleep(interval ?? 300);
20
+ await action?.(context);
21
+ await sleep(interval);
18
22
  }
19
23
  return false;
20
24
  };
25
+ /**
26
+ * 断言区域存在
27
+ * @param 获取区域的函数
28
+ * @param message 错误信息
29
+ * @param timeout 超时时间(毫秒),默认 3000 毫秒
30
+ * @param interval 等待间隔(毫秒),默认 300 毫秒
31
+ * @param action 每次等待循环中执行的操作(可选)
32
+ * @throws 如果区域在超时时间内未找到则抛出错误
33
+ */
34
+ export const assertExists = async (regionProvider, message = "断言区域存在失败", timeout = defaultTimeout, interval = defaultInterval, action) => {
35
+ const ok = await waitUntil(() => regionProvider() !== undefined, timeout, interval, action);
36
+ if (!ok)
37
+ throw new Error(message);
38
+ };
39
+ /**
40
+ * 断言区域不存在
41
+ * @param 获取区域的函数
42
+ * @param message 错误信息
43
+ * @param timeout 超时时间(毫秒),默认 3000 毫秒
44
+ * @param interval 等待间隔(毫秒),默认 300 毫秒
45
+ * @param action 每次等待循环中执行的操作(可选)
46
+ * @throws 如果区域在超时时间内仍然存在则抛出错误
47
+ */
48
+ export const assertNotExists = async (regionProvider, message = "断言区域不存在失败", timeout = defaultTimeout, interval = defaultInterval, action) => {
49
+ const ok = await waitUntil(() => regionProvider() === undefined, timeout, interval, action);
50
+ if (!ok)
51
+ throw new Error(message);
52
+ };
package/dist/game.js CHANGED
@@ -1,4 +1,4 @@
1
- import { waitUntil } from "./flow";
1
+ import { assertExists, assertNotExists } from "./flow";
2
2
  import { mouseSlide } from "./mouse";
3
3
  import { findTextInDirection, findTextWithinBounds, findTextWithinListView } from "./ocr";
4
4
  /**
@@ -8,10 +8,7 @@ export const openPaimonMenu = async () => {
8
8
  // 1.返回主界面
9
9
  await genshin.returnMainUi();
10
10
  // 2.打开派蒙菜单
11
- const findWorldLevel = () => findTextInDirection("世界等级", false, true, "north-west");
12
- const ok = await waitUntil(() => findWorldLevel() !== undefined, 5000, 1000, () => keyPress("ESCAPE"));
13
- if (!ok)
14
- throw new Error("打开派蒙菜单超时");
11
+ await assertExists(() => findTextInDirection("世界等级", false, true, "north-west"), "打开派蒙菜单超时", 5000, 1000, () => keyPress("ESCAPE"));
15
12
  };
16
13
  /**
17
14
  * 打开游戏菜单(左侧按钮)
@@ -19,11 +16,11 @@ export const openPaimonMenu = async () => {
19
16
  * @param reverse 是否反向搜索(可选)
20
17
  * @param config 搜索参数(可选)
21
18
  */
22
- export const openMenu = async (name, reverse, config) => {
19
+ export const openMenu = async (name, reverse = false, config = {}) => {
23
20
  // 1.打开派蒙菜单
24
21
  await openPaimonMenu();
25
22
  // 2.搜索菜单按钮
26
- const { x = 50, step = 30, timeout = 3000 } = config || {};
23
+ const { x = 50, step = 30, timeout = 3000 } = config;
27
24
  const findTooltip = () => findTextWithinBounds(name, false, true, 0, 0, x + 150, genshin.height);
28
25
  let result = undefined;
29
26
  const steps = Math.ceil(genshin.height / step);
@@ -31,17 +28,15 @@ export const openMenu = async (name, reverse, config) => {
31
28
  const o = i * step;
32
29
  const y = reverse ? genshin.height - o : o;
33
30
  moveMouseTo(x, y);
34
- await sleep(30);
31
+ await sleep(30); // 等待提示文字出现
35
32
  if ((result = findTooltip()) !== undefined)
36
33
  break;
37
34
  }
38
35
  // 3.点击菜单按钮
39
36
  if (result != undefined) {
40
- const ok = await waitUntil(() => findTooltip() === undefined, timeout, 1000, () => {
37
+ await assertNotExists(findTooltip, `打开菜单 ${name} 超时`, timeout, 1000, () => {
41
38
  click(x, result.y);
42
39
  });
43
- if (!ok)
44
- throw new Error(`打开菜单 ${name} 超时`);
45
40
  }
46
41
  else {
47
42
  throw new Error(`打开菜单 ${name} 失败`);
@@ -52,11 +47,11 @@ export const openMenu = async (name, reverse, config) => {
52
47
  * @param name 菜单页面名称
53
48
  * @param config 菜单页面视图参数
54
49
  */
55
- export const openMenuPage = async (name, config) => {
50
+ export const openMenuPage = async (name, config = {}) => {
56
51
  // 1.打开派蒙菜单
57
52
  await openPaimonMenu();
58
53
  // 2.搜索菜单页面
59
- const { x = 100, y = 330, w = 670, h = 730, lineHeight = 142 } = config || {};
54
+ const { x = 100, y = 330, w = 670, h = 730, lineHeight = 142 } = config;
60
55
  const pageButton = await findTextWithinListView(name, false, true, {
61
56
  x,
62
57
  y,
@@ -75,11 +70,11 @@ export const openMenuPage = async (name, config) => {
75
70
  * @param period 时间段
76
71
  * @param config 时钟参数
77
72
  */
78
- export const setTime = async (period, config) => {
73
+ export const setTime = async (period, config = {}) => {
79
74
  // 1.打开时间页面
80
75
  await openMenu("时间", true);
81
76
  // 2.拨动指针
82
- const { centerX = 1440, centerY = 502, radius = 400, offset = 5 } = config || {};
77
+ const { centerX = 1440, centerY = 502, radius = 400, offset = 5 } = config;
83
78
  const index = ["night", "morning", "noon", "evening"].indexOf(period);
84
79
  const periodsDirections = [
85
80
  () => mouseSlide(centerX, centerY, centerX - offset, centerY + radius),
@@ -91,13 +86,9 @@ export const setTime = async (period, config) => {
91
86
  for (const job of jobs)
92
87
  await job();
93
88
  // 3.点击确认按钮,等待调整结束
94
- const findTooShort = () => findTextInDirection("时间少于", true, true, "south-east");
95
- const findConfirmButton = () => findTextInDirection("确认", false, true, "south-east");
96
- const ok = await waitUntil(() => findTooShort() !== undefined, 20 * 1000, 1000, () => {
97
- findConfirmButton()?.click();
89
+ await assertExists(() => findTextInDirection("时间少于", true, true, "south-east"), "调整时间超时", 20 * 1000, 1000, () => {
90
+ findTextInDirection("确认", false, true, "south-east")?.click();
98
91
  });
99
- if (!ok)
100
- throw new Error("调整时间超时");
101
92
  // 4.返回主界面
102
93
  await genshin.returnMainUi();
103
94
  };
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 ?? "null", headers ? JSON.stringify(headers) : "null");
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
- const scrollAlgorithm = algorithm ?? (h => Math.floor(h / 18));
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
- const scrollAlgorithm = algorithm ?? (h => Math.floor(h / 18));
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 * (lineHeight ?? 175));
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 * (lineHeight ?? 175));
37
+ export const mouseScrollDownLines = (lines, lineHeight = 175) => {
38
+ return mouseScrollDown(lines * lineHeight);
43
39
  };
44
40
  /**
45
41
  * 鼠标拖拽滑动到指定位置
@@ -50,11 +46,11 @@ export const mouseScrollDownLines = (lines, lineHeight) => {
50
46
  */
51
47
  export const mouseSlide = async (x1, y1, x2, y2) => {
52
48
  moveMouseTo(x1, y1);
53
- await sleep(50);
49
+ await sleep(100);
54
50
  leftButtonDown();
55
- await sleep(50);
51
+ await sleep(100);
56
52
  moveMouseTo(x2, y2);
57
- await sleep(50);
53
+ await sleep(100);
58
54
  leftButtonUp();
59
55
  };
60
56
  /**
package/dist/ocr.js CHANGED
@@ -119,7 +119,7 @@ export const findTextInDirection = (text, contains, ignoreCase, direction) => {
119
119
  * @param timeout 搜索超时
120
120
  * @returns 如果找到匹配的文本区域,则返回该区域,否则返回 undefined
121
121
  */
122
- export const findTextWithinListView = async (text, contains, ignoreCase, listView, timeout) => {
122
+ export const findTextWithinListView = async (text, contains, ignoreCase, listView, timeout = 30 * 1000) => {
123
123
  const { x, y, w, h, maxListItems, lineHeight, padding = 10 } = listView;
124
124
  const find = () => {
125
125
  return findTextWithinBounds(text, contains, ignoreCase, x, y, w, h);
@@ -141,7 +141,7 @@ export const findTextWithinListView = async (text, contains, ignoreCase, listVie
141
141
  return true;
142
142
  }
143
143
  };
144
- const ok = await waitUntil(() => find() != undefined || isBottomTouched(), timeout ?? 30 * 1000, 1000, async () => {
144
+ const ok = await waitUntil(() => find() != undefined || isBottomTouched(), timeout, 1000, async () => {
145
145
  moveMouseTo(x + w - padding, y + padding);
146
146
  await mouseScrollDownLines(maxListItems, lineHeight);
147
147
  });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@bettergi/utils",
3
- "version": "0.0.9",
3
+ "version": "0.0.11",
4
4
  "description": "开发 BetterGI 脚本常用工具集",
5
5
  "type": "module",
6
6
  "author": "Bread Grocery<https://github.com/breadgrocery>",
@@ -33,7 +33,7 @@
33
33
  "build": "tsc"
34
34
  },
35
35
  "devDependencies": {
36
- "@bettergi/types": "^0.0.13",
37
- "typescript": "5.6.3"
36
+ "@bettergi/types": "^0.0.14",
37
+ "typescript": "^5.9.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