@occultus/random-api 0.20.0 → 0.21.0
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 +12 -6
- package/package.json +5 -5
- package/src/api/EventList.ts +3 -3
- package/src/api/RandomEvent.ts +2 -3
- package/src/index.ts +8 -5
- package/tsconfig.json +1 -1
package/README.md
CHANGED
|
@@ -3,6 +3,7 @@
|
|
|
3
3
|
该 API 提供了生成随机事件与随机数、随机 UUID 的功能。
|
|
4
4
|
|
|
5
5
|
## EventList
|
|
6
|
+
|
|
6
7
|
`EventList` 是一个用于管理加权事件列表的类,它允许根据权重随机触发事件,并返回触发结果的相关信息:
|
|
7
8
|
|
|
8
9
|
```typescript
|
|
@@ -18,10 +19,11 @@ const eventList = new EventList(eventData);
|
|
|
18
19
|
|
|
19
20
|
// 调用事件
|
|
20
21
|
const result = eventList.call();
|
|
21
|
-
console.log(
|
|
22
|
+
console.log(
|
|
23
|
+
`触发事件索引: ${result.dataIndex}, 权重总和: ${result.weightSum}, 随机权重值: ${result.weightRand}`
|
|
24
|
+
);
|
|
22
25
|
```
|
|
23
26
|
|
|
24
|
-
|
|
25
27
|
### `constructor(public data: EventListData[])`
|
|
26
28
|
|
|
27
29
|
创建一个新的 EventList 实例
|
|
@@ -30,6 +32,7 @@ console.log(`触发事件索引: ${result.dataIndex}, 权重总和: ${result.wei
|
|
|
30
32
|
- `data`: `EventListData[]` - 事件列表数据,包含事件及其对应的权重
|
|
31
33
|
|
|
32
34
|
### `call(): EventListResult`
|
|
35
|
+
|
|
33
36
|
- **描述**: 调用事件,根据给定的权重决定触发何种事件
|
|
34
37
|
- **返回值**: `EventListResult` - 包含事件触发结果的对象
|
|
35
38
|
- `weightSum`: `number` - 所有事件权重的总和
|
|
@@ -38,6 +41,7 @@ console.log(`触发事件索引: ${result.dataIndex}, 权重总和: ${result.wei
|
|
|
38
41
|
- **异常**: 如果找不到要调用的事件,会抛出 `OccultusSDKError` 异常
|
|
39
42
|
|
|
40
43
|
## Random
|
|
44
|
+
|
|
41
45
|
`Random` 是一个提供随机数和 UUID 生成功能的工具类,包含生成随机整数、随机小数和 UUID 的静态方法:
|
|
42
46
|
|
|
43
47
|
```typescript
|
|
@@ -59,6 +63,7 @@ console.log(uuid); // 可能输出: "a1b2c3d4-e5f6-4g7h-i8j9-k0l1m2n3o4p5"
|
|
|
59
63
|
```
|
|
60
64
|
|
|
61
65
|
### `integer(max: number, min = 0, inclusive = true): number`
|
|
66
|
+
|
|
62
67
|
生成指定范围内的随机整数
|
|
63
68
|
|
|
64
69
|
- **参数**:
|
|
@@ -69,6 +74,7 @@ console.log(uuid); // 可能输出: "a1b2c3d4-e5f6-4g7h-i8j9-k0l1m2n3o4p5"
|
|
|
69
74
|
- **异常**: 如果 `max < min`,则抛出 `RangeError`
|
|
70
75
|
|
|
71
76
|
### `decimal(max: number, min = 0, fixed = 2, inclusive = true): number`
|
|
77
|
+
|
|
72
78
|
生成指定范围内的随机小数
|
|
73
79
|
|
|
74
80
|
- **参数**:
|
|
@@ -80,16 +86,15 @@ console.log(uuid); // 可能输出: "a1b2c3d4-e5f6-4g7h-i8j9-k0l1m2n3o4p5"
|
|
|
80
86
|
- **异常**: 如果 `max < min`,则抛出 `RangeError`
|
|
81
87
|
|
|
82
88
|
### `UUID(): string`
|
|
89
|
+
|
|
83
90
|
生成一个 UUID
|
|
84
91
|
|
|
85
92
|
- **返回值**: `string` - 一个字符串形式的 UUID
|
|
86
93
|
|
|
87
|
-
|
|
88
94
|
## RandomEvent
|
|
89
95
|
|
|
90
96
|
`RandomEvent`是一个用于处理概率性事件触发的类,它允许根据设定的概率决定是否执行特定的事件函数:
|
|
91
97
|
|
|
92
|
-
|
|
93
98
|
```typescript
|
|
94
99
|
// 创建一个有 30% 概率触发的事件
|
|
95
100
|
const randomEvent = new RandomEvent(0.3, () => {
|
|
@@ -102,6 +107,7 @@ console.log(`事件是否被触发: ${triggered}`);
|
|
|
102
107
|
```
|
|
103
108
|
|
|
104
109
|
### `constructor(public chance: number, public event: () => void)`
|
|
110
|
+
|
|
105
111
|
创建一个新的 RandomEvent 实例
|
|
106
112
|
|
|
107
113
|
- **参数**:
|
|
@@ -109,8 +115,8 @@ console.log(`事件是否被触发: ${triggered}`);
|
|
|
109
115
|
- `event`: `() => void` - 当事件被触发时执行的回调函数
|
|
110
116
|
- **异常**: 如果提供的概率值无效,会抛出 `OccultusSDKError` 异常
|
|
111
117
|
|
|
112
|
-
|
|
113
118
|
### `call(): boolean`
|
|
119
|
+
|
|
114
120
|
调用事件,根据给定的概率决定是否触发事件
|
|
115
121
|
|
|
116
122
|
- **返回值**: `boolean` - 表示是否触发了事件的布尔值
|
|
@@ -142,4 +148,4 @@ Permission is hereby granted, free of charge, to any person obtaining a copy of
|
|
|
142
148
|
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
|
143
149
|
|
|
144
150
|
THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
145
|
-
```
|
|
151
|
+
```
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@occultus/random-api",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.21.0",
|
|
4
4
|
"repository": {
|
|
5
5
|
"type": "git",
|
|
6
6
|
"url": "https://codeberg.org/TeamOccultus/StarTenonAPI"
|
|
@@ -24,14 +24,14 @@
|
|
|
24
24
|
"license": "MIT",
|
|
25
25
|
"author": "CTN Studios",
|
|
26
26
|
"dependencies": {
|
|
27
|
-
"@occultus/math-api": "0.
|
|
27
|
+
"@occultus/math-api": "0.23.0"
|
|
28
28
|
},
|
|
29
29
|
"peerDependencies": {
|
|
30
|
-
"@minecraft/server": "
|
|
31
|
-
"@occultus/core": "
|
|
30
|
+
"@minecraft/server": "^2.4.0",
|
|
31
|
+
"@occultus/core": "^1.1.0"
|
|
32
32
|
},
|
|
33
33
|
"devDependencies": {
|
|
34
|
-
"typedoc": "^0.28.
|
|
34
|
+
"typedoc": "^0.28.16"
|
|
35
35
|
},
|
|
36
36
|
"scripts": {
|
|
37
37
|
"test": "tsc"
|
package/src/api/EventList.ts
CHANGED
|
@@ -5,7 +5,7 @@ import { Random } from "./Random";
|
|
|
5
5
|
|
|
6
6
|
/**
|
|
7
7
|
* 一个用于管理加权事件列表的类,它允许根据权重触发随机事件,并返回触发结果的相关信息
|
|
8
|
-
*
|
|
8
|
+
*
|
|
9
9
|
* @example
|
|
10
10
|
* // 创建事件列表数据
|
|
11
11
|
* const eventData: EventListData[] = [
|
|
@@ -28,7 +28,7 @@ export class EventList {
|
|
|
28
28
|
constructor(public data: EventListData[]) {}
|
|
29
29
|
/**
|
|
30
30
|
* 调用事件,根据给定的权重决定触发何种事件
|
|
31
|
-
*
|
|
31
|
+
*
|
|
32
32
|
* **注意:如果所有事件权重都为 0,会导致无法触发任何事件**
|
|
33
33
|
*
|
|
34
34
|
* @returns 该函数运行的结果
|
|
@@ -55,7 +55,7 @@ export class EventList {
|
|
|
55
55
|
return {
|
|
56
56
|
weightSum: sum,
|
|
57
57
|
weightRand: num,
|
|
58
|
-
dataIndex: i
|
|
58
|
+
dataIndex: i
|
|
59
59
|
};
|
|
60
60
|
}
|
|
61
61
|
}
|
package/src/api/RandomEvent.ts
CHANGED
|
@@ -9,11 +9,11 @@ import { Random } from "./Random";
|
|
|
9
9
|
* const randomEvent = new RandomEvent(0.3, () => {
|
|
10
10
|
* console.log("事件被触发了!");
|
|
11
11
|
* });
|
|
12
|
-
*
|
|
12
|
+
*
|
|
13
13
|
* // 尝试触发事件
|
|
14
14
|
* const triggered = randomEvent.call();
|
|
15
15
|
* console.log(`事件是否被触发: ${triggered}`);
|
|
16
|
-
*
|
|
16
|
+
*
|
|
17
17
|
*/
|
|
18
18
|
export class RandomEvent {
|
|
19
19
|
/**
|
|
@@ -41,4 +41,3 @@ export class RandomEvent {
|
|
|
41
41
|
return result;
|
|
42
42
|
}
|
|
43
43
|
}
|
|
44
|
-
|
package/src/index.ts
CHANGED
|
@@ -1,5 +1,8 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
export * from "./
|
|
5
|
-
export * from "./
|
|
1
|
+
/**
|
|
2
|
+
* @module @occultus/random-api
|
|
3
|
+
*/
|
|
4
|
+
export * from "./api/Random";
|
|
5
|
+
export * from "./api/RandomEvent";
|
|
6
|
+
export * from "./api/EventList";
|
|
7
|
+
export * from "./interface/EventListData";
|
|
8
|
+
export * from "./interface/EventListResult";
|