@occultus/random-api 0.18.1
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/LICENSE +9 -0
- package/README.md +1 -0
- package/package.json +27 -0
- package/src/api/Random.ts +73 -0
- package/src/api/RandomEvent.ts +71 -0
- package/src/index.ts +4 -0
- package/src/interface/EventListData.ts +14 -0
- package/src/interface/EventListResult.ts +18 -0
- package/tsconfig.json +21 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
The MIT License (MIT)
|
|
2
|
+
|
|
3
|
+
Copyright © 2025 CTN Studios
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
|
6
|
+
|
|
7
|
+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
|
8
|
+
|
|
9
|
+
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.
|
package/README.md
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
# Starock Random
|
package/package.json
ADDED
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@occultus/random-api",
|
|
3
|
+
"version": "0.18.1",
|
|
4
|
+
"description": "Star Tenon random api",
|
|
5
|
+
"main": "src/index.ts",
|
|
6
|
+
"keywords": [
|
|
7
|
+
"Minecraft",
|
|
8
|
+
"Occultus SDK",
|
|
9
|
+
"Script API"
|
|
10
|
+
],
|
|
11
|
+
"contributors": [
|
|
12
|
+
"FangLimao <mucigames@outlook.com>",
|
|
13
|
+
"RawDiamondMC <RawDiamondMC@outlook.com>"
|
|
14
|
+
],
|
|
15
|
+
"license": "MIT",
|
|
16
|
+
"author": "CTN Studios",
|
|
17
|
+
"dependencies": {
|
|
18
|
+
"@occultus/math-api": "0.18.1"
|
|
19
|
+
},
|
|
20
|
+
"peerDependencies": {
|
|
21
|
+
"@minecraft/server": "2.3.0-beta.1.21.110-preview.20",
|
|
22
|
+
"@occultus/core": ">=0.18.2 || <0.19.0"
|
|
23
|
+
},
|
|
24
|
+
"devDependencies": {
|
|
25
|
+
"typedoc": "^0.28.9"
|
|
26
|
+
}
|
|
27
|
+
}
|
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
import { system } from "@minecraft/server";
|
|
2
|
+
|
|
3
|
+
export class Random {
|
|
4
|
+
/**
|
|
5
|
+
* 生成随机整数
|
|
6
|
+
* @param max 最大值,小数部分将被解析为整数
|
|
7
|
+
* @param min 最小值,小数部分将被解析为整数,默认为0
|
|
8
|
+
* @param inclusive 生成的随机数是否包含最小值和最大值,默认为true
|
|
9
|
+
* @return 在最小值和最大值之间的一个随机整数
|
|
10
|
+
* @throws 如果 max < min,则抛出 RangeError
|
|
11
|
+
*/
|
|
12
|
+
static integer(max: number, min = 0, inclusive = true): number {
|
|
13
|
+
max = Math.ceil(max);
|
|
14
|
+
min = Math.ceil(min);
|
|
15
|
+
if (max < min) {
|
|
16
|
+
throw new RangeError(
|
|
17
|
+
`randomInteger() is used incorrectly! Expect: any number higher than ${min}. Current: ${max}`
|
|
18
|
+
);
|
|
19
|
+
}
|
|
20
|
+
if (inclusive) {
|
|
21
|
+
return Math.floor(Math.random() * (max - min + 1)) + min;
|
|
22
|
+
}
|
|
23
|
+
return Math.floor(Math.random() * (max - min)) + min;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* 生成随机小数
|
|
28
|
+
* @param max 最大值,小数部分将被解析为整数
|
|
29
|
+
* @param min 最小值,小数部分将被解析为整数,默认为0
|
|
30
|
+
* @param fixed 保留的小数位数。默认为2
|
|
31
|
+
* @param inclusive 生成的随机数是否包含最小值和最大值,默认为true
|
|
32
|
+
* @return 在最小值和最大值之间的一个随机小数
|
|
33
|
+
* @throws 如果 max < min,则抛出 RangeError
|
|
34
|
+
*/
|
|
35
|
+
static decimal(max: number, min = 0, fixed = 2, inclusive = true): number {
|
|
36
|
+
max = Math.ceil(max);
|
|
37
|
+
min = Math.ceil(min);
|
|
38
|
+
if (max < min) {
|
|
39
|
+
throw new RangeError(
|
|
40
|
+
`randomDecimal() is used incorrectly! Expect: any number higher than ${min}. Current: ${max}`
|
|
41
|
+
);
|
|
42
|
+
}
|
|
43
|
+
let random: number;
|
|
44
|
+
if (inclusive) {
|
|
45
|
+
random = Math.random() * (max - min + 1) + min;
|
|
46
|
+
} else {
|
|
47
|
+
random = Math.random() * (max - min) + min;
|
|
48
|
+
}
|
|
49
|
+
if (fixed) {
|
|
50
|
+
return Number(random.toFixed(fixed));
|
|
51
|
+
}
|
|
52
|
+
return random;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
/**
|
|
56
|
+
* 生成一个 UUID
|
|
57
|
+
* @return 一个字符串形式的 UUID
|
|
58
|
+
*/
|
|
59
|
+
static UUID(): string {
|
|
60
|
+
const currentTimestamp: number = system.currentTick;
|
|
61
|
+
return "xxxxxxxx-xxxx-4xxx-yxxx-zxxxxxxx".replace(
|
|
62
|
+
/[xyz]/g,
|
|
63
|
+
(args: string) => {
|
|
64
|
+
const random: number = (Math.random() * 16) | 0;
|
|
65
|
+
const value: number = args === "x" ? random : (random & 0x3) | 0x8;
|
|
66
|
+
if (args === "z") {
|
|
67
|
+
return value.toString(16) + currentTimestamp.toString(16).slice(-4);
|
|
68
|
+
}
|
|
69
|
+
return value.toString(16);
|
|
70
|
+
}
|
|
71
|
+
);
|
|
72
|
+
}
|
|
73
|
+
}
|
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
import { OccultusSDKError } from "@occultus/core";
|
|
2
|
+
import { Percent } from "@occultus/math-api";
|
|
3
|
+
import { Random } from "./Random";
|
|
4
|
+
import { EventListData } from "../interface/EventListData";
|
|
5
|
+
import { EventListResult } from "../interface/EventListResult";
|
|
6
|
+
|
|
7
|
+
export class RandomEvent {
|
|
8
|
+
/**
|
|
9
|
+
* @param chance 触发该事件的概率,必须为百分数,例如 0.5 表示 50%
|
|
10
|
+
* @param event
|
|
11
|
+
*/
|
|
12
|
+
constructor(
|
|
13
|
+
public chance: number,
|
|
14
|
+
public event: () => void
|
|
15
|
+
) {
|
|
16
|
+
if (!Percent.check(chance)) {
|
|
17
|
+
throw new OccultusSDKError(`Invaild chance!`, "Random Event");
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
/**
|
|
21
|
+
* 调用事件,根据给定的概率决定是否触发事件
|
|
22
|
+
*
|
|
23
|
+
* @return 一个布尔值,表示是否触发了事件
|
|
24
|
+
*/
|
|
25
|
+
call(): boolean {
|
|
26
|
+
const result = Random.decimal(1) <= this.chance;
|
|
27
|
+
if (result) {
|
|
28
|
+
this.event();
|
|
29
|
+
}
|
|
30
|
+
return result;
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
export class EventList {
|
|
35
|
+
/**
|
|
36
|
+
* @param data 该事件列表的数据
|
|
37
|
+
*/
|
|
38
|
+
constructor(public data: EventListData[]) {}
|
|
39
|
+
/**
|
|
40
|
+
* 调用事件,根据给定的权重决定触发何种事件
|
|
41
|
+
*
|
|
42
|
+
* @returns 该函数运行的结果
|
|
43
|
+
*/
|
|
44
|
+
call(): EventListResult {
|
|
45
|
+
const weights: number[] = [];
|
|
46
|
+
this.data.forEach((data) => {
|
|
47
|
+
weights.push(data.weight);
|
|
48
|
+
});
|
|
49
|
+
weights.sort((a, b) => a - b);
|
|
50
|
+
const cumulativeWeights: number[] = [];
|
|
51
|
+
let sum = 0;
|
|
52
|
+
for (const weight of weights) {
|
|
53
|
+
sum += weight;
|
|
54
|
+
cumulativeWeights.push(sum);
|
|
55
|
+
}
|
|
56
|
+
const num = Random.integer(0, sum);
|
|
57
|
+
for (let i = 0; i < cumulativeWeights.length; i++) {
|
|
58
|
+
if (num < cumulativeWeights[i]) {
|
|
59
|
+
if (this.data[i]?.event) {
|
|
60
|
+
this.data[i]?.event?.();
|
|
61
|
+
}
|
|
62
|
+
return {
|
|
63
|
+
weightSum: sum,
|
|
64
|
+
weightRand: num,
|
|
65
|
+
dataIndex: i,
|
|
66
|
+
};
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
throw new OccultusSDKError("Cannot find the event to call!", "Event List");
|
|
70
|
+
}
|
|
71
|
+
}
|
package/src/index.ts
ADDED
package/tsconfig.json
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
{
|
|
2
|
+
"include": ["src/*"],
|
|
3
|
+
"exclude": ["./out"],
|
|
4
|
+
"Modules": {
|
|
5
|
+
"resolvePackageJsonExports": true
|
|
6
|
+
},
|
|
7
|
+
"compilerOptions": {
|
|
8
|
+
"noEmit": true,
|
|
9
|
+
"noEmitOnError": true,
|
|
10
|
+
"target": "es2022",
|
|
11
|
+
"lib": ["es2020", "dom"],
|
|
12
|
+
"strict": true,
|
|
13
|
+
"moduleResolution": "node16",
|
|
14
|
+
"esModuleInterop": true,
|
|
15
|
+
"module": "node16",
|
|
16
|
+
"outDir": ".",
|
|
17
|
+
"removeComments": true,
|
|
18
|
+
"newLine": "lf",
|
|
19
|
+
"resolveJsonModule": true
|
|
20
|
+
}
|
|
21
|
+
}
|