@bettergi/utils 0.0.2 → 0.0.3

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
@@ -1,21 +1,81 @@
1
- Utils for [Better Genshin Impact](https://github.com/babalae/better-genshin-impact) JavaScript development.
1
+ 本项目是一个为[Better Genshin Impact](https://github.com/babalae/better-genshin-impact) 设计的 JavaScript 开发工具函数,旨在帮助开发者简化代码。
2
2
 
3
- ## Installation
3
+ ## 安装
4
4
 
5
5
  ```shell
6
6
  npm install @bettergi/utils
7
7
  ```
8
8
 
9
- ## Example
9
+ ## 函数清单
10
+
11
+ ### 图文识别
12
+
13
+ > 对 RecognitionObject 代码的封装,对于简单的 OCR 操作,无需写复杂的代码。
10
14
 
11
15
  ```ts
12
- import { findImage } from "@bettergi/utils";
16
+ import {
17
+ findImage,
18
+ findImageInDirection,
19
+ findImageWithinBounds,
20
+ findText,
21
+ findTextInDirection,
22
+ findTextWithinBounds
23
+ } from "@bettergi/utils";
24
+
25
+ // 在整个画面内搜索图片,找不到返回 undefined
26
+ const f1 = findImage("assets/关闭.png");
27
+
28
+ // 在指定方向上搜索图片,找不到返回 undefined
29
+ const f2 = findImageInDirection("assets/关闭.png", "north-east");
30
+
31
+ // 在指定区域内搜索图片,找不到返回 undefined
32
+ const f3 = findImageWithinBounds("assets/关闭.png", 960, 0, 960, 1080);
33
+
34
+ // 在整个画面内搜索文本(不包含、忽略大小写),找不到返回 undefined
35
+ const t1 = findText("购买", false, true);
36
+
37
+ // 在指定方向上搜索文本(包含、忽略大小写),找不到返回 undefined
38
+ const t2 = findTextInDirection("师傅", true, true, "east");
39
+
40
+ // 在指定区域内搜索文本(不包含、忽略大小写),找不到返回 undefined
41
+ const t3 = findTextWithinBounds("确认", false, true, 960, 540, 960, 540);
42
+ ```
43
+
44
+ ### 行为
13
45
 
14
- const mailIcon = findImage("./assets/mail.png");
46
+ > 对脚本工作流中常见行为的抽象,例如:等待 XXX 完成/出现/消失。
47
+
48
+ ```ts
49
+ import { findImageInDirection, waitUntil } from "@bettergi/utils";
15
50
 
16
- mailIcon?.click();
51
+ // 等待直到找不到[关闭按钮] 或 5秒后超时,每隔1秒检查一次,期间按 Esc 键
52
+ const done = await waitUntil(
53
+ () => findImageInDirection("assets/关闭.png", "north-east") !== undefined,
54
+ 5000,
55
+ 1000,
56
+ () => keyPress("ESCAPE")
57
+ );
58
+ if (!done) throw new Error("关闭页面");
17
59
  ```
18
60
 
19
- ## Related Tools
61
+ ### 存储
20
62
 
21
- [@bettergi/create-script](https://www.npmjs.com/package/@bettergi/create-script)
63
+ > 对象数据持久化,通过代理实现自动存储。可以无感知地读取/更新数据,而无需考虑如何持久化。
64
+
65
+ ```ts
66
+ import { useStore } from "@bettergi/utils";
67
+
68
+ // 创建/读取存储对象,保存到存储文件 store/state.json 中
69
+ // 通过Proxy来实现:对存储对象的操作会同步保存到存储文件
70
+ const state = useStore<{ lastUsedTime?: number; count: number }>("state");
71
+ if (state?.lastUsedTime) {
72
+ log.info(`欢迎回来!上次使用时间:${state.lastUsedTime},计数器已累计至:${state.count}`);
73
+ }
74
+ try {
75
+ for (let i = 0; i < Math.floor(Math.random() * 100); i++) {
76
+ state.count = (state.count || 0) + 1; // 同步保存到文件
77
+ }
78
+ } finally {
79
+ state.lastUsedTime = Date.now(); // 同步保存到文件
80
+ }
81
+ ```
package/dist/index.d.ts CHANGED
@@ -1,3 +1,4 @@
1
1
  export * from "./action";
2
2
  export * from "./mouse";
3
3
  export * from "./ocr";
4
+ export * from "./store";
package/dist/index.js CHANGED
@@ -1,3 +1,4 @@
1
1
  export * from "./action";
2
2
  export * from "./mouse";
3
3
  export * from "./ocr";
4
+ export * from "./store";
package/dist/ocr.d.ts CHANGED
@@ -1,7 +1,7 @@
1
1
  import { type Region } from "@bettergi/types/csharp/BetterGenshinImpact/GameTask/Model/Area/Region";
2
2
  type Direction = "north" | "north-east" | "east" | "south-east" | "south" | "south-west" | "west" | "north-west";
3
3
  /**
4
- * 搜索图片
4
+ * 在整个画面内搜索图片
5
5
  * @param path 图片路径
6
6
  * @returns 如果找到匹配的图片区域,则返回该区域,否则返回 undefined
7
7
  */
@@ -9,8 +9,8 @@ export declare const findImage: (path: string) => Region | undefined;
9
9
  /**
10
10
  * 在指定区域内搜索图片
11
11
  * @param path 图片路径
12
- * @param x - 水平移动偏移量(像素)
13
- * @param y - 垂直移动偏移量(像素)
12
+ * @param x - 水平方向偏移量(像素)
13
+ * @param y - 垂直方向偏移量(像素)
14
14
  * @param w 宽度
15
15
  * @param h 高度
16
16
  * @returns 如果找到匹配的图片区域,则返回该区域,否则返回 undefined
@@ -24,7 +24,7 @@ export declare const findImageWithinBounds: (path: string, x: number, y: number,
24
24
  */
25
25
  export declare const findImageInDirection: (path: string, direction: Direction) => Region | undefined;
26
26
  /**
27
- * 搜索文本
27
+ * 在整个画面内搜索文本
28
28
  * @param text 待搜索文本
29
29
  * @param contains 是否包含
30
30
  * @param ignoreCase 是否忽略大小写
@@ -36,9 +36,9 @@ export declare const findText: (text: string, contains: boolean, ignoreCase: boo
36
36
  * @param text 待搜索文本
37
37
  * @param contains 是否包含
38
38
  * @param ignoreCase 是否忽略大小写
39
- * @param x 水平移动偏移量(像素)
40
- * @param x - 水平移动偏移量(像素)
41
- * @param y - 垂直移动偏移量(像素)
39
+ * @param x 水平方向偏移量(像素)
40
+ * @param x - 水平方向偏移量(像素)
41
+ * @param y - 垂直方向偏移量(像素)
42
42
  * @param w 宽度
43
43
  * @param h 高度
44
44
  * @returns 如果找到匹配的文本区域,则返回该区域,否则返回 undefined
package/dist/ocr.js CHANGED
@@ -14,7 +14,7 @@ const directionToBounds = (direction) => {
14
14
  return { x, y, w, h };
15
15
  };
16
16
  /**
17
- * 搜索图片
17
+ * 在整个画面内搜索图片
18
18
  * @param path 图片路径
19
19
  * @returns 如果找到匹配的图片区域,则返回该区域,否则返回 undefined
20
20
  */
@@ -31,8 +31,8 @@ export const findImage = (path) => {
31
31
  /**
32
32
  * 在指定区域内搜索图片
33
33
  * @param path 图片路径
34
- * @param x - 水平移动偏移量(像素)
35
- * @param y - 垂直移动偏移量(像素)
34
+ * @param x - 水平方向偏移量(像素)
35
+ * @param y - 垂直方向偏移量(像素)
36
36
  * @param w 宽度
37
37
  * @param h 高度
38
38
  * @returns 如果找到匹配的图片区域,则返回该区域,否则返回 undefined
@@ -58,7 +58,7 @@ export const findImageInDirection = (path, direction) => {
58
58
  return findImageWithinBounds(path, x, y, w, h);
59
59
  };
60
60
  /**
61
- * 搜索文本
61
+ * 在整个画面内搜索文本
62
62
  * @param text 待搜索文本
63
63
  * @param contains 是否包含
64
64
  * @param ignoreCase 是否忽略大小写
@@ -79,9 +79,9 @@ export const findText = (text, contains, ignoreCase) => {
79
79
  * @param text 待搜索文本
80
80
  * @param contains 是否包含
81
81
  * @param ignoreCase 是否忽略大小写
82
- * @param x 水平移动偏移量(像素)
83
- * @param x - 水平移动偏移量(像素)
84
- * @param y - 垂直移动偏移量(像素)
82
+ * @param x 水平方向偏移量(像素)
83
+ * @param x - 水平方向偏移量(像素)
84
+ * @param y - 垂直方向偏移量(像素)
85
85
  * @param w 宽度
86
86
  * @param h 高度
87
87
  * @returns 如果找到匹配的文本区域,则返回该区域,否则返回 undefined
@@ -0,0 +1,6 @@
1
+ /**
2
+ * 创建一个持久化存储对象,用于管理应用状态数据
3
+ * 该函数会创建一个代理对象,对该对象的所有属性的修改都会自动同步到相应的JSON文件(脚本的 `store` 目录下)中。
4
+ * @param name 存储对象的名称,将作为文件名(不包扩展名)
5
+ */
6
+ export declare const useStore: <T extends Record<string, any>>(name: string) => T;
package/dist/store.js ADDED
@@ -0,0 +1,20 @@
1
+ /**
2
+ * 创建一个持久化存储对象,用于管理应用状态数据
3
+ * 该函数会创建一个代理对象,对该对象的所有属性的修改都会自动同步到相应的JSON文件(脚本的 `store` 目录下)中。
4
+ * @param name 存储对象的名称,将作为文件名(不包扩展名)
5
+ */
6
+ export const useStore = (name) => {
7
+ const path = `store/${name}.json`;
8
+ const obj = (() => {
9
+ try {
10
+ const text = file.readTextSync(path);
11
+ return JSON.parse(text);
12
+ }
13
+ catch {
14
+ return {};
15
+ }
16
+ })();
17
+ return new Proxy(obj, {
18
+ set: (target, key, value) => Reflect.set(target, key, value) && file.writeTextSync(path, JSON.stringify(target, null, 2))
19
+ });
20
+ };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@bettergi/utils",
3
- "version": "0.0.2",
3
+ "version": "0.0.3",
4
4
  "description": "Utils for BetterGI JavaScript Development",
5
5
  "type": "module",
6
6
  "author": "Bread Grocery<https://github.com/breadgrocery>",
@@ -1 +0,0 @@
1
- export declare const verticalScroll: (scrollAmountInClicks: number) => void;
package/dist/polyfill.js DELETED
@@ -1,10 +0,0 @@
1
- import { _simulateScroll } from "./mouse";
2
- export const verticalScroll = (scrollAmountInClicks) => {
3
- // @ts-ignore
4
- if (globalThis.verticalScroll) {
5
- globalThis.verticalScroll(scrollAmountInClicks);
6
- }
7
- else {
8
- _simulateScroll(scrollAmountInClicks, 1);
9
- }
10
- };