labag 1.0.4 → 1.0.6

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 ADDED
@@ -0,0 +1,14 @@
1
+ # LaBaG
2
+
3
+ LaBaG 是一款使用 TypeScript 撰寫的拉霸機。
4
+ 多模式、多種組合得分,適合用於遊戲開發與教育用途。
5
+
6
+ [![npm version](https://img.shields.io/npm/v/labag.svg)](https://www.npmjs.com/package/labag)
7
+
8
+ ---
9
+
10
+ ## 安裝方式
11
+
12
+ ```bash
13
+ npm install labag
14
+ ```
@@ -77,7 +77,7 @@ class BaseLaBaG {
77
77
  this.OneData["SuperHHH"] = __1.Modes.SuperHHH.RandNum;
78
78
  this.OneData["GreenWei"] = __1.Modes.GreenWei.RandNum;
79
79
  const RateRange = this.RateRanges[this.NowMode()];
80
- const PCodes = Object.keys(P_1.P.Map);
80
+ const PCodes = Array.from(P_1.P.Map.keys());
81
81
  RandNums.forEach((RandNum, i) => {
82
82
  const code = PCodes.find((_, j) => RandNum <= RateRange[j]);
83
83
  if (code) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "labag",
3
- "version": "1.0.4",
3
+ "version": "1.0.6",
4
4
  "description": "",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",
package/src/index.ts CHANGED
@@ -57,7 +57,7 @@ const Modes: Record<Exclude<ModeNames, "Normal">, Mode> = {
57
57
  Score: 0,
58
58
  RandNum: 0,
59
59
  Random(): void {
60
- this.RandNum = RandInt();
60
+ this.RandNum = RandInt(1, 100);
61
61
  },
62
62
  Judge(Game: LaBaG): void {
63
63
  if (
@@ -107,7 +107,7 @@ const Modes: Record<Exclude<ModeNames, "Normal">, Mode> = {
107
107
  Score: 0, // 咖波累積數
108
108
  RandNum: 0,
109
109
  Random(): void {
110
- this.RandNum = RandInt();
110
+ this.RandNum = RandInt(1, 100);
111
111
  },
112
112
  Judge(Game: LaBaG): void {
113
113
  if (
@@ -0,0 +1,193 @@
1
+ import { Modes } from "src";
2
+ import { AllDataType, LaBaG, OneDataType } from "./LaBaG";
3
+ import { Mode, ModeNames } from "./Mode";
4
+ import { P } from "./P";
5
+ import { RandInt } from "src/utils/RandInt";
6
+
7
+ export class BaseLaBaG implements LaBaG {
8
+ AllData: AllDataType = {};
9
+ OneData: OneDataType = {};
10
+ DataIndex: number = 0;
11
+
12
+ Times: number = 30;
13
+ Played: number = 0;
14
+
15
+ Score: number = 0;
16
+ MarginScore: number = 0;
17
+
18
+ Ps: [P | null, P | null, P | null] = [null, null, null];
19
+
20
+ RateRanges: Record<ModeNames, number[]> = [
21
+ "Normal",
22
+ "SuperHHH",
23
+ "GreenWei",
24
+ "PiKaChu",
25
+ ].reduce((Ranges: Record<ModeNames, number[]>, mode: string) => {
26
+ const res: number[] = [];
27
+ let accRate: number = 0;
28
+ for (const p of P.Map.values()) {
29
+ accRate += p.rates[mode as ModeNames];
30
+ res.push(accRate);
31
+ }
32
+ Ranges[mode as ModeNames] = res;
33
+ return Ranges;
34
+ }, {} as Record<ModeNames, number[]>);
35
+
36
+ ScoreTimes: Record<ModeNames, number> = {
37
+ Normal: 1,
38
+ SuperHHH: 1,
39
+ GreenWei: 3,
40
+ PiKaChu: 1,
41
+ };
42
+ ScoreTime: number = 1;
43
+
44
+ ModeToScreen: boolean = false;
45
+
46
+ constructor() {}
47
+
48
+ GameRunning(): boolean {
49
+ //遊戲進行
50
+ return this.Times > this.Played;
51
+ }
52
+
53
+ NowMode(): ModeNames {
54
+ // 查找當前模式
55
+ const mode = Object.entries(Modes).find(
56
+ ([_, mode]: [string, Mode]) => mode.InMode ?? false
57
+ );
58
+ return mode ? (mode[0] as ModeNames) : "Normal";
59
+ }
60
+
61
+ Reset() {
62
+ // 重置
63
+ this.AllData = {};
64
+ this.DataIndex = 0;
65
+
66
+ this.Played = 0;
67
+ this.Score = 0;
68
+ this.MarginScore = 0;
69
+ this.ScoreTime = 1;
70
+
71
+ this.Ps = [null, null, null];
72
+
73
+ Object.values(Modes).forEach((mode: Mode) => {
74
+ if (mode.InMode !== undefined) mode.InMode = false;
75
+ if (mode.Score !== undefined) mode.Score = 0;
76
+ if (mode.RandNum !== undefined) mode.RandNum = 0;
77
+ if (mode.Times !== undefined) mode.Times = 0;
78
+ });
79
+ }
80
+
81
+ Random(): void {
82
+ const RandNums: [number, number, number] = Array.from({ length: 3 }, () =>
83
+ RandInt(1, 100)
84
+ ) as [number, number, number];
85
+
86
+ RandNums.forEach((RandNum: number, index: number) => {
87
+ this.OneData[`RandNums[${index}]` as keyof OneDataType] = RandNum;
88
+ });
89
+
90
+ Object.values(Modes).forEach((mode: Mode) => {
91
+ mode.Random?.();
92
+ });
93
+ this.OneData["SuperHHH"] = Modes.SuperHHH.RandNum as number;
94
+ this.OneData["GreenWei"] = Modes.GreenWei.RandNum as number;
95
+
96
+ const RateRange: number[] = this.RateRanges[this.NowMode()];
97
+ const PCodes = Array.from(P.Map.keys());
98
+ RandNums.forEach((RandNum: number, i: number) => {
99
+ const code = PCodes.find((_, j: number) => RandNum <= RateRange[j]);
100
+ if (code) {
101
+ this.Ps[i] = P.Map.get(code) ?? null;
102
+ }
103
+ });
104
+
105
+ // 累積咖波累積數
106
+ this.Ps.forEach((p) => {
107
+ if (Modes.GreenWei.Score !== undefined) {
108
+ if (p?.code === "A" && Modes.GreenWei.Score < 20) {
109
+ Modes.GreenWei.Score += 1;
110
+ }
111
+ }
112
+ });
113
+ }
114
+ CalculateScore(): void {
115
+ //計算分數
116
+ const UniqueCount: number = new Set(this.Ps.map((p) => p?.code)).size;
117
+ switch (UniqueCount) {
118
+ case 1: // 三個一樣
119
+ this.MarginScore += this.Ps[0]?.scores?.[0] as number;
120
+ break;
121
+ case 2: // 兩個一樣
122
+ if (this.Ps[0]?.code === this.Ps[1]?.code) {
123
+ this.MarginScore += this.Ps[0]?.scores?.[1] as number;
124
+ this.MarginScore += this.Ps[2]?.scores?.[2] as number;
125
+ this.MarginScore = Math.round(this.MarginScore / 1.4);
126
+ } else if (this.Ps[1]?.code === this.Ps[2]?.code) {
127
+ this.MarginScore += this.Ps[1]?.scores?.[1] as number;
128
+ this.MarginScore += this.Ps[0]?.scores?.[2] as number;
129
+ this.MarginScore = Math.round(this.MarginScore / 1.4);
130
+ } else if (this.Ps[2]?.code === this.Ps[0]?.code) {
131
+ this.MarginScore += this.Ps[2]?.scores?.[1] as number;
132
+ this.MarginScore += this.Ps[1]?.scores?.[2] as number;
133
+ this.MarginScore = Math.round(this.MarginScore / 1.4);
134
+ }
135
+ break;
136
+ case 3: // 三個不一樣
137
+ this.MarginScore += this.Ps[0]?.scores?.[2] as number;
138
+ this.MarginScore += this.Ps[1]?.scores?.[2] as number;
139
+ this.MarginScore += this.Ps[2]?.scores?.[2] as number;
140
+ this.MarginScore = Math.round(this.MarginScore / 3);
141
+ break;
142
+ }
143
+
144
+ // 根據當前模式更新加分倍數
145
+ this.ScoreTime = this.ScoreTimes[this.NowMode()];
146
+ this.MarginScore *= this.ScoreTime;
147
+ }
148
+ Result(): void {
149
+ // 結果
150
+ this.Played += 1;
151
+ this.DataIndex += 1;
152
+ this.Score += this.MarginScore;
153
+ this.AllData[`${this.DataIndex}`] = this.OneData;
154
+ }
155
+ JudgeMode(): void {
156
+ if (!this.GameRunning()) {
157
+ Modes.PiKaChu.Judge?.(this);
158
+ return;
159
+ }
160
+
161
+ const mode: ModeNames = this.NowMode();
162
+ switch (mode) {
163
+ case "Normal":
164
+ case "PiKaChu":
165
+ Modes.SuperHHH.Judge?.(this);
166
+ if (!Modes.SuperHHH.InMode) {
167
+ Modes.GreenWei.Judge?.(this);
168
+ }
169
+ break;
170
+ case "SuperHHH":
171
+ Modes.SuperHHH.Judge?.(this);
172
+ break;
173
+ case "GreenWei":
174
+ Modes.GreenWei.Judge?.(this);
175
+ break;
176
+ }
177
+ }
178
+ Logic(): void {
179
+ // 邏輯流程
180
+ this.Reset();
181
+ while (this.GameRunning()) {
182
+ this.ModeToScreen = false;
183
+ this.OneData = {};
184
+
185
+ this.MarginScore = 0;
186
+
187
+ this.Random();
188
+ this.CalculateScore();
189
+ this.Result();
190
+ this.JudgeMode();
191
+ }
192
+ }
193
+ }
@@ -1,6 +1,6 @@
1
1
  import { P } from "./P";
2
- import { BaseLaBaG } from "./LaBaG";
3
2
  import { Modes } from "..";
3
+ import { BaseLaBaG } from "./BaseLaBaG";
4
4
 
5
5
  export class JsonLaBaG extends BaseLaBaG {
6
6
  jsonData: BaseLaBaG["AllData"];
@@ -21,7 +21,7 @@ export class JsonLaBaG extends BaseLaBaG {
21
21
  }
22
22
 
23
23
  Random(): void {
24
- const currData = this.jsonData[this.dataIndex];
24
+ const currData = this.jsonData[`${this.dataIndex}`];
25
25
  if (!currData) {
26
26
  super.Random();
27
27
  return;
@@ -1,11 +1,15 @@
1
1
  import { P } from "../types/P";
2
- import { Mode, ModeNames } from "../types/Mode";
3
- import { RandInt } from "../utils/RandInt";
4
- import { Modes } from "..";
2
+ import { ModeNames } from "../types/Mode";
3
+
4
+ export type OneDataType = Partial<
5
+ Record<"SuperHHH" | "GreenWei" | `RandNum[${0 | 1 | 2}]`, number>
6
+ >;
7
+
8
+ export type AllDataType = Record<`${number}`, OneDataType>;
5
9
 
6
10
  export interface LaBaG {
7
- AllData: Record<string, Record<string, number>>;
8
- OneData: Record<string, number>;
11
+ AllData: AllDataType;
12
+ OneData: OneDataType;
9
13
  DataIndex: number;
10
14
 
11
15
  Times: number;
@@ -31,191 +35,3 @@ export interface LaBaG {
31
35
  JudgeMode(): void;
32
36
  Logic(): void;
33
37
  }
34
-
35
- export class BaseLaBaG implements LaBaG {
36
- AllData: Record<string, Record<string, number>> = {};
37
- OneData: Record<string, number> = {};
38
- DataIndex: number = 0;
39
-
40
- Times: number = 30;
41
- Played: number = 0;
42
-
43
- Score: number = 0;
44
- MarginScore: number = 0;
45
-
46
- Ps: [P | null, P | null, P | null] = [null, null, null];
47
-
48
- RateRanges: Record<ModeNames, number[]> = [
49
- "Normal",
50
- "SuperHHH",
51
- "GreenWei",
52
- "PiKaChu",
53
- ].reduce((Ranges: Record<ModeNames, number[]>, mode: string) => {
54
- const res: number[] = [];
55
- let accRate: number = 0;
56
- for (const p of P.Map.values()) {
57
- accRate += p.rates[mode as ModeNames];
58
- res.push(accRate);
59
- }
60
- Ranges[mode as ModeNames] = res;
61
- return Ranges;
62
- }, {} as Record<ModeNames, number[]>);
63
-
64
- ScoreTimes: Record<ModeNames, number> = {
65
- Normal: 1,
66
- SuperHHH: 1,
67
- GreenWei: 3,
68
- PiKaChu: 1,
69
- };
70
- ScoreTime: number = 1;
71
-
72
- ModeToScreen: boolean = false;
73
-
74
- constructor() {}
75
-
76
- GameRunning(): boolean {
77
- //遊戲進行
78
- return this.Times > this.Played;
79
- }
80
-
81
- NowMode(): ModeNames {
82
- // 查找當前模式
83
- const mode = Object.entries(Modes).find(
84
- ([_, mode]: [string, Mode]) => mode.InMode ?? false
85
- );
86
- return mode ? (mode[0] as ModeNames) : "Normal";
87
- }
88
-
89
- Reset() {
90
- // 重置
91
- this.AllData = {};
92
- this.DataIndex = 0;
93
-
94
- this.Played = 0;
95
- this.Score = 0;
96
- this.MarginScore = 0;
97
- this.ScoreTime = 1;
98
-
99
- this.Ps = [null, null, null];
100
-
101
- Object.values(Modes).forEach((mode: Mode) => {
102
- if (mode.InMode !== undefined) mode.InMode = false;
103
- if (mode.Score !== undefined) mode.Score = 0;
104
- if (mode.RandNum !== undefined) mode.RandNum = 0;
105
- if (mode.Times !== undefined) mode.Times = 0;
106
- });
107
- }
108
-
109
- Random(): void {
110
- const RandNums: [number, number, number] = Array.from({ length: 3 }, () =>
111
- RandInt()
112
- ) as [number, number, number];
113
-
114
- RandNums.forEach((RandNum: number, index: number) => {
115
- this.OneData[`RandNums[${index}]`] = RandNum;
116
- });
117
-
118
- Object.values(Modes).forEach((mode: Mode) => {
119
- mode.Random?.();
120
- });
121
- this.OneData["SuperHHH"] = Modes.SuperHHH.RandNum as number;
122
- this.OneData["GreenWei"] = Modes.GreenWei.RandNum as number;
123
-
124
- const RateRange: number[] = this.RateRanges[this.NowMode()];
125
- const PCodes = Object.keys(P.Map);
126
- RandNums.forEach((RandNum: number, i: number) => {
127
- const code = PCodes.find((_, j: number) => RandNum <= RateRange[j]);
128
- if (code) {
129
- this.Ps[i] = P.Map.get(code) ?? null;
130
- }
131
- });
132
-
133
- // 累積咖波累積數
134
- this.Ps.forEach((p) => {
135
- if (Modes.GreenWei.Score !== undefined) {
136
- if (p?.code === "A" && Modes.GreenWei.Score < 20) {
137
- Modes.GreenWei.Score += 1;
138
- }
139
- }
140
- });
141
- }
142
- CalculateScore(): void {
143
- //計算分數
144
- const UniqueCount: number = new Set(this.Ps.map((p) => p?.code)).size;
145
- switch (UniqueCount) {
146
- case 1: // 三個一樣
147
- this.MarginScore += this.Ps[0]?.scores?.[0] as number;
148
- break;
149
- case 2: // 兩個一樣
150
- if (this.Ps[0]?.code === this.Ps[1]?.code) {
151
- this.MarginScore += this.Ps[0]?.scores?.[1] as number;
152
- this.MarginScore += this.Ps[2]?.scores?.[2] as number;
153
- this.MarginScore = Math.round(this.MarginScore / 1.4);
154
- } else if (this.Ps[1]?.code === this.Ps[2]?.code) {
155
- this.MarginScore += this.Ps[1]?.scores?.[1] as number;
156
- this.MarginScore += this.Ps[0]?.scores?.[2] as number;
157
- this.MarginScore = Math.round(this.MarginScore / 1.4);
158
- } else if (this.Ps[2]?.code === this.Ps[0]?.code) {
159
- this.MarginScore += this.Ps[2]?.scores?.[1] as number;
160
- this.MarginScore += this.Ps[1]?.scores?.[2] as number;
161
- this.MarginScore = Math.round(this.MarginScore / 1.4);
162
- }
163
- break;
164
- case 3: // 三個不一樣
165
- this.MarginScore += this.Ps[0]?.scores?.[2] as number;
166
- this.MarginScore += this.Ps[1]?.scores?.[2] as number;
167
- this.MarginScore += this.Ps[2]?.scores?.[2] as number;
168
- this.MarginScore = Math.round(this.MarginScore / 3);
169
- break;
170
- }
171
-
172
- // 根據當前模式更新加分倍數
173
- this.ScoreTime = this.ScoreTimes[this.NowMode()];
174
- this.MarginScore *= this.ScoreTime;
175
- }
176
- Result(): void {
177
- // 結果
178
- this.Played += 1;
179
- this.DataIndex += 1;
180
- this.Score += this.MarginScore;
181
- this.AllData[`${this.DataIndex}`] = this.OneData;
182
- }
183
- JudgeMode(): void {
184
- if (!this.GameRunning()) {
185
- Modes.PiKaChu.Judge?.(this);
186
- return;
187
- }
188
-
189
- const mode: ModeNames = this.NowMode();
190
- switch (mode) {
191
- case "Normal":
192
- case "PiKaChu":
193
- Modes.SuperHHH.Judge?.(this);
194
- if (!Modes.SuperHHH.InMode) {
195
- Modes.GreenWei.Judge?.(this);
196
- }
197
- break;
198
- case "SuperHHH":
199
- Modes.SuperHHH.Judge?.(this);
200
- break;
201
- case "GreenWei":
202
- Modes.GreenWei.Judge?.(this);
203
- break;
204
- }
205
- }
206
- Logic(): void {
207
- // 邏輯流程
208
- this.Reset();
209
- while (this.GameRunning()) {
210
- this.ModeToScreen = false;
211
- this.OneData = {};
212
-
213
- this.MarginScore = 0;
214
-
215
- this.Random();
216
- this.CalculateScore();
217
- this.Result();
218
- this.JudgeMode();
219
- }
220
- }
221
- }
@@ -1,5 +1,5 @@
1
1
  import { Modes } from "..";
2
- import { BaseLaBaG } from "./LaBaG";
2
+ import { BaseLaBaG } from "./BaseLaBaG";
3
3
  import { ModeNames } from "./Mode";
4
4
 
5
5
  export class PlayLaBaG extends BaseLaBaG {
@@ -1,5 +1,3 @@
1
- export function RandInt(): number {
2
- const min: number = 1;
3
- const max: number = 100;
1
+ export function RandInt(min: number, max: number): number {
4
2
  return Math.floor(Math.random() * (max - min + 1)) + min;
5
3
  }