@bettergi/utils 0.0.10 → 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
@@ -8,3 +8,23 @@
8
8
  * - false 在超时后条件仍未满足
9
9
  */
10
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,7 +11,7 @@
7
11
  * @returns - true 在超时前条件已满足
8
12
  * - false 在超时后条件仍未满足
9
13
  */
10
- export const waitUntil = async (condition, timeout = 3000, interval = 300, action) => {
14
+ export const waitUntil = async (condition, timeout = defaultTimeout, interval = defaultInterval, action) => {
11
15
  const context = {};
12
16
  const deadline = Date.now() + timeout;
13
17
  while (Date.now() < deadline) {
@@ -18,3 +22,31 @@ export const waitUntil = async (condition, timeout = 3000, interval = 300, actio
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
  * 打开游戏菜单(左侧按钮)
@@ -31,17 +28,15 @@ export const openMenu = async (name, reverse = false, 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} 失败`);
@@ -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/mouse.js CHANGED
@@ -46,11 +46,11 @@ export const mouseScrollDownLines = (lines, lineHeight = 175) => {
46
46
  */
47
47
  export const mouseSlide = async (x1, y1, x2, y2) => {
48
48
  moveMouseTo(x1, y1);
49
- await sleep(50);
49
+ await sleep(100);
50
50
  leftButtonDown();
51
- await sleep(50);
51
+ await sleep(100);
52
52
  moveMouseTo(x2, y2);
53
- await sleep(50);
53
+ await sleep(100);
54
54
  leftButtonUp();
55
55
  };
56
56
  /**
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@bettergi/utils",
3
- "version": "0.0.10",
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
  }