labag 2.5.0 → 2.5.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/dist/recordChecker.js +14 -23
- package/dist/recorder.d.ts +3 -6
- package/dist/recorder.js +8 -10
- package/package.json +1 -1
- package/src/recordChecker.ts +16 -25
- package/src/recorder.ts +11 -13
package/dist/recordChecker.js
CHANGED
|
@@ -21,48 +21,39 @@ class RecordChecker {
|
|
|
21
21
|
* @returns 計算出的分數。
|
|
22
22
|
*/
|
|
23
23
|
calculateScore(record) {
|
|
24
|
-
//
|
|
24
|
+
// 警告:此操作會重置並覆蓋當前傳入的遊戲實例狀態
|
|
25
25
|
this.game.init();
|
|
26
26
|
this.game.times = record.times;
|
|
27
|
-
// 使用 any 類型來存取私有方法
|
|
28
|
-
const gameAny = this.game;
|
|
29
27
|
for (const round of record.rounds) {
|
|
30
28
|
if (!this.game.isRunning()) {
|
|
31
29
|
throw new Error("遊戲次數已達上限,無法繼續遊玩。");
|
|
32
30
|
}
|
|
33
31
|
// 1. 回合開始
|
|
34
|
-
|
|
32
|
+
this.game["roundStart"]();
|
|
35
33
|
// 2. 設定隨機數字與圖案 (模擬 rollSlots)
|
|
36
34
|
const { ranges } = this.game.getCurrentConfig();
|
|
37
|
-
|
|
38
|
-
round.randNums[
|
|
39
|
-
|
|
40
|
-
round.randNums["2"],
|
|
41
|
-
];
|
|
42
|
-
this.game.randNums.forEach((num, index) => {
|
|
35
|
+
for (let i = 0; i < 3; i++) {
|
|
36
|
+
const num = round.randNums?.[i.toString()] ?? 0;
|
|
37
|
+
this.game.randNums[i] = num;
|
|
43
38
|
const match = ranges.find((r) => num <= r.threshold);
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
}
|
|
47
|
-
else {
|
|
48
|
-
this.game.patterns[index] = null;
|
|
49
|
-
}
|
|
50
|
-
});
|
|
39
|
+
this.game.patterns[i] = match ? match.pattern : null;
|
|
40
|
+
}
|
|
51
41
|
// 觸發 rollSlots 事件,讓模式執行其邏輯 (例如 greenwei 產生隨機數)
|
|
52
|
-
|
|
42
|
+
this.game["emit"]("rollSlots");
|
|
53
43
|
// 3. 覆蓋模式的隨機變數 (確保使用紀錄中的數值)
|
|
54
44
|
this.game.modes.forEach((mode) => {
|
|
55
|
-
|
|
56
|
-
|
|
45
|
+
const recordedNum = round.randNums?.[mode.name];
|
|
46
|
+
if (recordedNum !== undefined && mode.variable) {
|
|
47
|
+
mode.variable.randNum = recordedNum;
|
|
57
48
|
}
|
|
58
49
|
});
|
|
59
50
|
// 4. 計算分數
|
|
60
|
-
|
|
51
|
+
this.game["calculateScore"]();
|
|
61
52
|
// 5. 回合結束
|
|
62
|
-
|
|
53
|
+
this.game["roundEnd"]();
|
|
63
54
|
}
|
|
64
55
|
if (!this.game.isRunning()) {
|
|
65
|
-
|
|
56
|
+
this.game["gameOver"]();
|
|
66
57
|
}
|
|
67
58
|
return this.game.score;
|
|
68
59
|
}
|
package/dist/recorder.d.ts
CHANGED
|
@@ -1,11 +1,11 @@
|
|
|
1
1
|
import { LaBaG } from "./labag";
|
|
2
2
|
export type RoundRecord = {
|
|
3
|
-
randNums: Record<string, number
|
|
3
|
+
readonly randNums: Readonly<Record<string, number>>;
|
|
4
4
|
};
|
|
5
5
|
export type GameRecord = {
|
|
6
6
|
times: number;
|
|
7
7
|
score: number;
|
|
8
|
-
rounds: RoundRecord
|
|
8
|
+
readonly rounds: ReadonlyArray<RoundRecord>;
|
|
9
9
|
};
|
|
10
10
|
export type RecorderOptions = {
|
|
11
11
|
debug?: boolean;
|
|
@@ -14,13 +14,10 @@ export declare class Recorder {
|
|
|
14
14
|
#private;
|
|
15
15
|
private game;
|
|
16
16
|
private score;
|
|
17
|
-
private onRoundEndBound;
|
|
18
17
|
private started;
|
|
19
18
|
private debug;
|
|
20
19
|
constructor(gameInstance: LaBaG, options?: RecorderOptions);
|
|
21
|
-
get rounds():
|
|
22
|
-
randNums: Record<string, number>;
|
|
23
|
-
}[];
|
|
20
|
+
get rounds(): ReadonlyArray<RoundRecord>;
|
|
24
21
|
private onRoundEnd;
|
|
25
22
|
init(clearExisting?: boolean): void;
|
|
26
23
|
dispose(): void;
|
package/dist/recorder.js
CHANGED
|
@@ -5,28 +5,26 @@ class Recorder {
|
|
|
5
5
|
game;
|
|
6
6
|
#rounds = [];
|
|
7
7
|
score = 0;
|
|
8
|
-
onRoundEndBound;
|
|
9
8
|
started = false;
|
|
10
9
|
debug = false;
|
|
11
10
|
constructor(gameInstance, options) {
|
|
12
11
|
this.game = gameInstance;
|
|
13
|
-
this.
|
|
14
|
-
this.debug = !!options?.debug;
|
|
12
|
+
this.debug = options?.debug ?? false;
|
|
15
13
|
}
|
|
16
14
|
get rounds() {
|
|
17
|
-
return this.#rounds
|
|
15
|
+
return this.#rounds;
|
|
18
16
|
}
|
|
19
|
-
onRoundEnd(g) {
|
|
17
|
+
onRoundEnd = (g) => {
|
|
20
18
|
const randNums = {};
|
|
21
19
|
g.randNums.forEach((value, key) => {
|
|
22
|
-
if (!isNaN(value)) {
|
|
20
|
+
if (typeof value === "number" && !Number.isNaN(value)) {
|
|
23
21
|
randNums[key] = value;
|
|
24
22
|
}
|
|
25
23
|
});
|
|
26
24
|
// 收集各模式的 randNum(若為數值且有效)
|
|
27
25
|
g.modes.forEach((mode) => {
|
|
28
26
|
const rn = mode?.variable?.randNum;
|
|
29
|
-
if (!isNaN(rn)) {
|
|
27
|
+
if (typeof rn === "number" && !Number.isNaN(rn)) {
|
|
30
28
|
randNums[mode.name] = rn;
|
|
31
29
|
}
|
|
32
30
|
});
|
|
@@ -40,7 +38,7 @@ class Recorder {
|
|
|
40
38
|
});
|
|
41
39
|
this.#rounds.push(round);
|
|
42
40
|
this.score = g.score;
|
|
43
|
-
}
|
|
41
|
+
};
|
|
44
42
|
init(clearExisting = true) {
|
|
45
43
|
if (this.started)
|
|
46
44
|
return;
|
|
@@ -48,14 +46,14 @@ class Recorder {
|
|
|
48
46
|
this.clear();
|
|
49
47
|
}
|
|
50
48
|
if (typeof this.game.addEventListener === "function") {
|
|
51
|
-
this.game.addEventListener("roundEnd", this.
|
|
49
|
+
this.game.addEventListener("roundEnd", this.onRoundEnd);
|
|
52
50
|
this.started = true;
|
|
53
51
|
}
|
|
54
52
|
}
|
|
55
53
|
dispose() {
|
|
56
54
|
if (!this.started)
|
|
57
55
|
return;
|
|
58
|
-
this.game.removeEventListener("roundEnd", this.
|
|
56
|
+
this.game.removeEventListener("roundEnd", this.onRoundEnd);
|
|
59
57
|
this.started = false;
|
|
60
58
|
}
|
|
61
59
|
clear() {
|
package/package.json
CHANGED
package/src/recordChecker.ts
CHANGED
|
@@ -24,58 +24,49 @@ export class RecordChecker {
|
|
|
24
24
|
* @returns 計算出的分數。
|
|
25
25
|
*/
|
|
26
26
|
calculateScore(record: GameRecord): number {
|
|
27
|
-
//
|
|
27
|
+
// 警告:此操作會重置並覆蓋當前傳入的遊戲實例狀態
|
|
28
28
|
this.game.init();
|
|
29
29
|
this.game.times = record.times;
|
|
30
30
|
|
|
31
|
-
// 使用 any 類型來存取私有方法
|
|
32
|
-
const gameAny = this.game as any;
|
|
33
|
-
|
|
34
31
|
for (const round of record.rounds) {
|
|
35
32
|
if (!this.game.isRunning()) {
|
|
36
33
|
throw new Error("遊戲次數已達上限,無法繼續遊玩。");
|
|
37
34
|
}
|
|
38
35
|
|
|
39
36
|
// 1. 回合開始
|
|
40
|
-
|
|
37
|
+
this.game["roundStart"]();
|
|
41
38
|
|
|
42
39
|
// 2. 設定隨機數字與圖案 (模擬 rollSlots)
|
|
43
40
|
const { ranges } = this.game.getCurrentConfig();
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
round.randNums[
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
];
|
|
50
|
-
|
|
51
|
-
this.game.randNums.forEach((num, index) => {
|
|
41
|
+
|
|
42
|
+
for (let i = 0; i < 3; i++) {
|
|
43
|
+
const num = round.randNums?.[i.toString()] ?? 0;
|
|
44
|
+
this.game.randNums[i] = num;
|
|
45
|
+
|
|
52
46
|
const match = ranges.find((r) => num <= r.threshold);
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
} else {
|
|
56
|
-
this.game.patterns[index] = null;
|
|
57
|
-
}
|
|
58
|
-
});
|
|
47
|
+
this.game.patterns[i] = match ? match.pattern : null;
|
|
48
|
+
}
|
|
59
49
|
|
|
60
50
|
// 觸發 rollSlots 事件,讓模式執行其邏輯 (例如 greenwei 產生隨機數)
|
|
61
|
-
|
|
51
|
+
this.game["emit"]("rollSlots");
|
|
62
52
|
|
|
63
53
|
// 3. 覆蓋模式的隨機變數 (確保使用紀錄中的數值)
|
|
64
54
|
this.game.modes.forEach((mode) => {
|
|
65
|
-
|
|
66
|
-
|
|
55
|
+
const recordedNum = round.randNums?.[mode.name];
|
|
56
|
+
if (recordedNum !== undefined && mode.variable) {
|
|
57
|
+
mode.variable.randNum = recordedNum;
|
|
67
58
|
}
|
|
68
59
|
});
|
|
69
60
|
|
|
70
61
|
// 4. 計算分數
|
|
71
|
-
|
|
62
|
+
this.game["calculateScore"]();
|
|
72
63
|
|
|
73
64
|
// 5. 回合結束
|
|
74
|
-
|
|
65
|
+
this.game["roundEnd"]();
|
|
75
66
|
}
|
|
76
67
|
|
|
77
68
|
if (!this.game.isRunning()) {
|
|
78
|
-
|
|
69
|
+
this.game["gameOver"]();
|
|
79
70
|
}
|
|
80
71
|
|
|
81
72
|
return this.game.score;
|
package/src/recorder.ts
CHANGED
|
@@ -1,13 +1,13 @@
|
|
|
1
1
|
import { LaBaG } from "./labag";
|
|
2
2
|
|
|
3
3
|
export type RoundRecord = {
|
|
4
|
-
randNums: Record<string, number
|
|
4
|
+
readonly randNums: Readonly<Record<string, number>>;
|
|
5
5
|
};
|
|
6
6
|
|
|
7
7
|
export type GameRecord = {
|
|
8
8
|
times: number;
|
|
9
9
|
score: number;
|
|
10
|
-
rounds: RoundRecord
|
|
10
|
+
readonly rounds: ReadonlyArray<RoundRecord>;
|
|
11
11
|
};
|
|
12
12
|
|
|
13
13
|
export type RecorderOptions = {
|
|
@@ -17,25 +17,23 @@ export class Recorder {
|
|
|
17
17
|
private game: LaBaG;
|
|
18
18
|
#rounds: RoundRecord[] = [];
|
|
19
19
|
private score: number = 0;
|
|
20
|
-
private onRoundEndBound: (e: LaBaG) => void;
|
|
21
20
|
private started = false;
|
|
22
21
|
private debug = false;
|
|
23
22
|
|
|
24
23
|
constructor(gameInstance: LaBaG, options?: RecorderOptions) {
|
|
25
24
|
this.game = gameInstance;
|
|
26
|
-
this.
|
|
27
|
-
this.debug = !!options?.debug;
|
|
25
|
+
this.debug = options?.debug ?? false;
|
|
28
26
|
}
|
|
29
27
|
|
|
30
|
-
get rounds() {
|
|
31
|
-
return this.#rounds
|
|
28
|
+
get rounds(): ReadonlyArray<RoundRecord> {
|
|
29
|
+
return this.#rounds;
|
|
32
30
|
}
|
|
33
31
|
|
|
34
|
-
private onRoundEnd(g: LaBaG) {
|
|
32
|
+
private onRoundEnd = (g: LaBaG) => {
|
|
35
33
|
const randNums: Record<string, number> = {};
|
|
36
34
|
|
|
37
35
|
g.randNums.forEach((value, key) => {
|
|
38
|
-
if (!isNaN(value)) {
|
|
36
|
+
if (typeof value === "number" && !Number.isNaN(value)) {
|
|
39
37
|
randNums[key] = value;
|
|
40
38
|
}
|
|
41
39
|
});
|
|
@@ -43,7 +41,7 @@ export class Recorder {
|
|
|
43
41
|
// 收集各模式的 randNum(若為數值且有效)
|
|
44
42
|
g.modes.forEach((mode) => {
|
|
45
43
|
const rn = mode?.variable?.randNum;
|
|
46
|
-
if (!isNaN(rn)) {
|
|
44
|
+
if (typeof rn === "number" && !Number.isNaN(rn)) {
|
|
47
45
|
randNums[mode.name] = rn;
|
|
48
46
|
}
|
|
49
47
|
});
|
|
@@ -60,7 +58,7 @@ export class Recorder {
|
|
|
60
58
|
|
|
61
59
|
this.#rounds.push(round);
|
|
62
60
|
this.score = g.score;
|
|
63
|
-
}
|
|
61
|
+
};
|
|
64
62
|
|
|
65
63
|
init(clearExisting = true) {
|
|
66
64
|
if (this.started) return;
|
|
@@ -68,14 +66,14 @@ export class Recorder {
|
|
|
68
66
|
this.clear();
|
|
69
67
|
}
|
|
70
68
|
if (typeof this.game.addEventListener === "function") {
|
|
71
|
-
this.game.addEventListener("roundEnd", this.
|
|
69
|
+
this.game.addEventListener("roundEnd", this.onRoundEnd);
|
|
72
70
|
this.started = true;
|
|
73
71
|
}
|
|
74
72
|
}
|
|
75
73
|
|
|
76
74
|
dispose() {
|
|
77
75
|
if (!this.started) return;
|
|
78
|
-
this.game.removeEventListener("roundEnd", this.
|
|
76
|
+
this.game.removeEventListener("roundEnd", this.onRoundEnd);
|
|
79
77
|
this.started = false;
|
|
80
78
|
}
|
|
81
79
|
clear() {
|