@allemandi/gacha-engine 0.2.3 → 0.3.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 +159 -62
- package/dist/gacha-engine.d.ts +3 -18
- package/dist/index.cjs +1 -1
- package/dist/index.cjs.map +1 -1
- package/dist/index.module.js +1 -1
- package/dist/index.module.js.map +1 -1
- package/dist/index.umd.js +1 -1
- package/dist/index.umd.js.map +1 -1
- package/dist/types.d.ts +13 -17
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -3,12 +3,15 @@
|
|
|
3
3
|
[](https://www.npmjs.com/package/@allemandi/gacha-engine)
|
|
4
4
|
[](https://github.com/allemandi/gacha-engine/blob/main/LICENSE)
|
|
5
5
|
|
|
6
|
-
> **Practical, type-safe toolkit for simulating and understanding gacha rates and rate-ups.**
|
|
7
|
-
>
|
|
8
|
-
> Works in Node.js
|
|
6
|
+
> **Practical, type-safe toolkit for simulating and understanding gacha rates and rate-ups.**
|
|
7
|
+
> Supports `"weighted"` and `"flatRate"` modes for different gacha strategies.
|
|
8
|
+
> Works in Node.js and browsers – supports ESM, CommonJS, and UMD.
|
|
9
|
+
|
|
10
|
+
---
|
|
9
11
|
|
|
10
12
|
<!-- omit from toc -->
|
|
11
13
|
## 🔖 Table of Contents
|
|
14
|
+
|
|
12
15
|
- [✨ Features](#-features)
|
|
13
16
|
- [🛠️ Installation](#️-installation)
|
|
14
17
|
- [🚀 Quick Usage Examples](#-quick-usage-examples)
|
|
@@ -17,13 +20,17 @@
|
|
|
17
20
|
- [🔗 Related Projects](#-related-projects)
|
|
18
21
|
- [🤝 Contributing](#-contributing)
|
|
19
22
|
|
|
23
|
+
---
|
|
20
24
|
|
|
21
25
|
## ✨ Features
|
|
22
|
-
|
|
23
|
-
-
|
|
24
|
-
-
|
|
25
|
-
-
|
|
26
|
-
-
|
|
26
|
+
|
|
27
|
+
- 🎲 **Roll simulation** – Perform gacha rolls with weighted or flat-rate logic
|
|
28
|
+
- 🔍 **Probability analysis** – Drop rates, cumulative probabilities, target probabilities
|
|
29
|
+
- 📐 **Multi-rarity support** – Flexible rarity-based or item-based probability distributions
|
|
30
|
+
- ⚡ **Performance optimized** – Efficient with cached calculations
|
|
31
|
+
- 🛡️ **Type-safe** – Written in TypeScript with strict configuration validation
|
|
32
|
+
|
|
33
|
+
---
|
|
27
34
|
|
|
28
35
|
## 🛠️ Installation
|
|
29
36
|
```bash
|
|
@@ -37,7 +44,7 @@ npm install @allemandi/gacha-engine
|
|
|
37
44
|
|
|
38
45
|
## 🚀 Quick Usage Examples
|
|
39
46
|
|
|
40
|
-
**ESM**
|
|
47
|
+
**ESM (Weighted Mode)**
|
|
41
48
|
```js
|
|
42
49
|
import { GachaEngine } from '@allemandi/gacha-engine';
|
|
43
50
|
|
|
@@ -47,17 +54,15 @@ const pools = [
|
|
|
47
54
|
items: [
|
|
48
55
|
{ name: 'Super Hobo', weight: 0.8, rateUp: true },
|
|
49
56
|
{ name: 'Broke King', weight: 0.4 },
|
|
50
|
-
{ name: 'Cardboard Hero', weight: 0.4 }
|
|
51
|
-
{ name: 'Newspaper Warmer', weight: 0.4 }
|
|
57
|
+
{ name: 'Cardboard Hero', weight: 0.4 }
|
|
52
58
|
]
|
|
53
59
|
},
|
|
54
60
|
{
|
|
55
|
-
rarity: 'SR',
|
|
61
|
+
rarity: 'SR',
|
|
56
62
|
items: [
|
|
57
63
|
{ name: 'Cold Salaryman', weight: 1.5, rateUp: true },
|
|
58
64
|
{ name: 'Numb Artist', weight: 1.8 },
|
|
59
|
-
{ name: 'Crying Cook', weight: 1.8 }
|
|
60
|
-
{ name: 'Lonely Cat', weight: 1.8 }
|
|
65
|
+
{ name: 'Crying Cook', weight: 1.8 }
|
|
61
66
|
]
|
|
62
67
|
},
|
|
63
68
|
{
|
|
@@ -70,80 +75,138 @@ const pools = [
|
|
|
70
75
|
];
|
|
71
76
|
|
|
72
77
|
const rarityRates = {
|
|
73
|
-
SSR: 0.01,
|
|
74
|
-
SR: 0.05,
|
|
75
|
-
R: 0.94
|
|
78
|
+
SSR: 0.01,
|
|
79
|
+
SR: 0.05,
|
|
80
|
+
R: 0.94
|
|
76
81
|
};
|
|
77
82
|
|
|
78
|
-
const engine = new GachaEngine({ pools, rarityRates });
|
|
83
|
+
const engine = new GachaEngine({ mode: 'weighted', pools, rarityRates });
|
|
84
|
+
|
|
85
|
+
console.log('10 rolls:', engine.roll(10).join(', '));
|
|
79
86
|
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
87
|
+
const rate = engine.getItemDropRate('Super Hobo');
|
|
88
|
+
console.log('Drop rate for Super Hobo:', (rate * 100).toFixed(3) + '%');
|
|
89
|
+
// ~0.4% → (0.8 / 1.6) * 0.01 = 0.005 → 0.5%
|
|
83
90
|
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
// Output: ~0.4% (0.8/2.0 * 0.01)
|
|
91
|
+
const cumulative = engine.getCumulativeProbabilityForItem('Super Hobo', 300);
|
|
92
|
+
console.log('Probability in 300 rolls:', (cumulative * 100).toFixed(1) + '%');
|
|
93
|
+
// ~77.7%
|
|
88
94
|
|
|
89
|
-
|
|
90
|
-
|
|
95
|
+
console.log('Rolls for 50% chance:', engine.getRollsForTargetProbability('Super Hobo', 0.5));
|
|
96
|
+
// ~138
|
|
91
97
|
|
|
92
|
-
|
|
93
|
-
|
|
98
|
+
console.log('Rate-up items:', engine.getRateUpItems().join(', '));
|
|
99
|
+
// Super Hobo, Cold Salaryman
|
|
94
100
|
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
// Output: "Super Hobo, Cold Salaryman"
|
|
101
|
+
console.log('All items:', engine.getAllItems().join(', '));
|
|
102
|
+
// Super Hobo, Broke King, Cardboard Hero, Cold Salaryman, Numb Artist, Crying Cook, Regular Joe, Normal Person
|
|
98
103
|
```
|
|
99
104
|
|
|
100
|
-
**CommonJS**
|
|
105
|
+
**CommonJS (Flat Mode)**
|
|
101
106
|
```js
|
|
102
107
|
const { GachaEngine } = require('@allemandi/gacha-engine');
|
|
103
108
|
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
109
|
+
const pools = [
|
|
110
|
+
{
|
|
111
|
+
rarity: 'SSR',
|
|
112
|
+
items: [
|
|
113
|
+
{ name: 'God-Tier Rat', flatRate: 0.003, rateUp: true },
|
|
114
|
+
{ name: 'Dumpster King', flatRate: 0.002 }
|
|
115
|
+
]
|
|
116
|
+
},
|
|
117
|
+
{
|
|
118
|
+
rarity: 'SR',
|
|
119
|
+
items: [
|
|
120
|
+
{ name: 'Sleepy Chef', flatRate: 0.015 }
|
|
121
|
+
]
|
|
122
|
+
},
|
|
123
|
+
{
|
|
124
|
+
rarity: 'R',
|
|
125
|
+
items: [
|
|
126
|
+
{ name: 'Unknown Student', flatRate: 0.1 }
|
|
127
|
+
]
|
|
128
|
+
}
|
|
129
|
+
];
|
|
130
|
+
|
|
131
|
+
const engine = new GachaEngine({ mode: 'flat', pools });
|
|
132
|
+
|
|
133
|
+
console.log('Roll x5:', engine.roll(5).join(', '));
|
|
134
|
+
|
|
135
|
+
const dropRate = engine.getItemDropRate('God-Tier Rat');
|
|
136
|
+
console.log('Drop rate for God-Tier Rat:', (dropRate * 100).toFixed(3) + '%');
|
|
137
|
+
// 0.3%
|
|
138
|
+
|
|
139
|
+
const cumulative = engine.getCumulativeProbabilityForItem('God-Tier Rat', 500);
|
|
140
|
+
console.log('Chance after 500 rolls:', (cumulative * 100).toFixed(1) + '%');
|
|
141
|
+
// ~78.5%
|
|
142
|
+
|
|
143
|
+
const rollsFor50 = engine.getRollsForTargetProbability('God-Tier Rat', 0.5);
|
|
144
|
+
console.log('Rolls for 50% chance:', rollsFor50);
|
|
145
|
+
// ~231
|
|
146
|
+
|
|
147
|
+
console.log('Rate-up items:', engine.getRateUpItems().join(', '));
|
|
148
|
+
// God-Tier Rat
|
|
149
|
+
|
|
150
|
+
console.log('All items:', engine.getAllItems().join(', '));
|
|
151
|
+
// God-Tier Rat, Dumpster King, Sleepy Chef, Unknown Student
|
|
107
152
|
```
|
|
108
153
|
|
|
109
|
-
**UMD (Browser)**
|
|
154
|
+
**UMD (Browser, Weighted Mode)**
|
|
110
155
|
```html
|
|
111
156
|
<script src="https://unpkg.com/@allemandi/gacha-engine"></script>
|
|
112
157
|
<script>
|
|
113
|
-
// Access the GachaEngine class
|
|
114
158
|
const { GachaEngine } = window.AllemandiGachaEngine;
|
|
115
|
-
|
|
159
|
+
|
|
116
160
|
const engine = new GachaEngine({
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
161
|
+
mode: 'weighted',
|
|
162
|
+
rarityRates: {
|
|
163
|
+
SSR: 0.02,
|
|
164
|
+
SR: 0.08,
|
|
165
|
+
R: 0.90
|
|
121
166
|
},
|
|
122
167
|
pools: [
|
|
123
168
|
{
|
|
124
169
|
rarity: 'SSR',
|
|
125
170
|
items: [
|
|
126
|
-
{ name: '
|
|
127
|
-
{ name: '
|
|
171
|
+
{ name: 'Trash Wizard', weight: 1.0 },
|
|
172
|
+
{ name: 'Park Master', weight: 1.0, rateUp: true }
|
|
128
173
|
]
|
|
129
174
|
},
|
|
130
175
|
{
|
|
131
176
|
rarity: 'SR',
|
|
132
177
|
items: [
|
|
133
|
-
{ name: 'Street Sweeper', weight:
|
|
178
|
+
{ name: 'Street Sweeper', weight: 2.0 },
|
|
179
|
+
{ name: 'Bench Philosopher', weight: 1.0 }
|
|
134
180
|
]
|
|
135
181
|
},
|
|
136
182
|
{
|
|
137
183
|
rarity: 'R',
|
|
138
184
|
items: [
|
|
139
|
-
{ name: '
|
|
185
|
+
{ name: 'Bus Stop Ghost', weight: 5.0 }
|
|
140
186
|
]
|
|
141
187
|
}
|
|
142
188
|
]
|
|
143
189
|
});
|
|
144
190
|
|
|
145
|
-
|
|
146
|
-
|
|
191
|
+
const rate = engine.getItemDropRate('Park Master');
|
|
192
|
+
const rolls = engine.getRollsForTargetProbability('Park Master', 0.75);
|
|
193
|
+
const cumulative = engine.getCumulativeProbabilityForItem('Park Master', 200);
|
|
194
|
+
|
|
195
|
+
console.log('1x Roll:', engine.roll());
|
|
196
|
+
console.log('Drop rate for Park Master:', (rate * 100).toFixed(2) + '%');
|
|
197
|
+
// 1.0 / 2.0 * 0.02 = 0.01 → 1.00%
|
|
198
|
+
|
|
199
|
+
console.log('Cumulative 200 rolls:', (cumulative * 100).toFixed(1) + '%');
|
|
200
|
+
// ~86.6%
|
|
201
|
+
|
|
202
|
+
console.log('Rolls for 75% chance:', rolls);
|
|
203
|
+
// ~139
|
|
204
|
+
|
|
205
|
+
console.log('Rate-up items:', engine.getRateUpItems().join(', '));
|
|
206
|
+
// Park Master
|
|
207
|
+
|
|
208
|
+
console.log('All items:', engine.getAllItems().join(', '));
|
|
209
|
+
// Trash Wizard, Park Master, Street Sweeper, Bench Philosopher, Bus Stop Ghost
|
|
147
210
|
</script>
|
|
148
211
|
```
|
|
149
212
|
|
|
@@ -154,14 +217,38 @@ console.log('Single roll:', engine.roll());
|
|
|
154
217
|
|
|
155
218
|
Creates a new GachaEngine instance with validation.
|
|
156
219
|
|
|
157
|
-
**Config
|
|
158
|
-
|
|
159
|
-
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
220
|
+
**Config Options:**
|
|
221
|
+
|
|
222
|
+
- Weighted Mode
|
|
223
|
+
```ts
|
|
224
|
+
{
|
|
225
|
+
mode: 'weighted'; // (default)
|
|
226
|
+
rarityRates: Record<string, number>; // Required: must sum to 1.0
|
|
227
|
+
pools: Array<{
|
|
228
|
+
rarity: string; // Must match a key in `rarityRates`
|
|
229
|
+
items: Array<{
|
|
230
|
+
name: string;
|
|
231
|
+
weight: number;
|
|
232
|
+
rateUp?: boolean;
|
|
233
|
+
}>
|
|
234
|
+
}>
|
|
235
|
+
}
|
|
236
|
+
```
|
|
237
|
+
- Flat Rate Mode
|
|
238
|
+
|
|
239
|
+
```ts
|
|
240
|
+
{
|
|
241
|
+
mode: 'flatRate';
|
|
242
|
+
pools: Array<{
|
|
243
|
+
rarity: string; // Used only for categorization
|
|
244
|
+
items: Array<{
|
|
245
|
+
name: string;
|
|
246
|
+
weight: number; // Interpreted as direct probability (must sum to 1.0 across all items)
|
|
247
|
+
rateUp?: boolean;
|
|
248
|
+
}>
|
|
249
|
+
}>
|
|
250
|
+
}
|
|
251
|
+
```
|
|
165
252
|
|
|
166
253
|
### Methods
|
|
167
254
|
|
|
@@ -173,26 +260,36 @@ Creates a new GachaEngine instance with validation.
|
|
|
173
260
|
|
|
174
261
|
#### Analysis
|
|
175
262
|
`getItemDropRate(name: string): number`
|
|
176
|
-
- Returns the effective drop rate
|
|
177
|
-
-
|
|
263
|
+
- Returns the effective drop rate for a specific item
|
|
264
|
+
- In weighted mode:
|
|
265
|
+
- Computed as `dropRate = (item.weight / totalPoolWeight) × rarityBaseRate`
|
|
266
|
+
- In flat rate mode:
|
|
267
|
+
- `Returns the item's defined probability.
|
|
268
|
+
- Throws if the item does not exist.
|
|
178
269
|
|
|
179
270
|
`getRarityProbability(rarity: string): number`
|
|
180
|
-
- Returns the base probability for a rarity tier
|
|
271
|
+
- Returns the base probability for a given rarity tier
|
|
272
|
+
- Only in "weighted" mode.
|
|
273
|
+
- Throws in flatRate mode.
|
|
181
274
|
|
|
182
275
|
`getCumulativeProbabilityForItem(name: string, rolls: number): number`
|
|
183
276
|
- Calculates probability of getting the item at least once in N rolls
|
|
184
277
|
- Uses formula: `1 - (1 - dropRate)^rolls`
|
|
185
278
|
|
|
186
279
|
`getRollsForTargetProbability(name: string, targetProbability: number): number`
|
|
187
|
-
-
|
|
280
|
+
- Calculates the minimum number of rolls needed to reach a specific probability of pulling a given item.
|
|
188
281
|
- Returns `Infinity` if item has zero drop rate
|
|
282
|
+
- Returns 1 if target probability ≥ 1.0
|
|
189
283
|
|
|
190
284
|
#### Utility
|
|
191
285
|
`getRateUpItems(): string[]`
|
|
192
286
|
- Returns names of all items marked with `rateUp: true`
|
|
193
287
|
|
|
194
288
|
`getAllItemDropRates(): Array<{name: string, dropRate: number, rarity: string}>`
|
|
195
|
-
- Returns
|
|
289
|
+
- Returns a list of all items with:
|
|
290
|
+
- name: Item name
|
|
291
|
+
- dropRate: Calculated drop probability
|
|
292
|
+
- rarity: Associated rarity (or "flatRate" in flat mode)
|
|
196
293
|
|
|
197
294
|
## 🧪 Tests
|
|
198
295
|
|
package/dist/gacha-engine.d.ts
CHANGED
|
@@ -2,25 +2,17 @@ import { GachaEngineConfig } from './types';
|
|
|
2
2
|
export declare class GachaEngine {
|
|
3
3
|
private static readonly SCALE;
|
|
4
4
|
private static readonly MAX_SAFE_SCALE;
|
|
5
|
+
private mode;
|
|
5
6
|
private pools;
|
|
6
7
|
private rarityRatesScaled;
|
|
8
|
+
private flatRateMap;
|
|
7
9
|
private dropRateCacheScaled;
|
|
8
|
-
constructor(
|
|
9
|
-
/**
|
|
10
|
-
* Convert floating point rates to scaled integers
|
|
11
|
-
*/
|
|
10
|
+
constructor(config: GachaEngineConfig);
|
|
12
11
|
private scaleRarityRates;
|
|
13
|
-
/**
|
|
14
|
-
* Convert probability to scaled integer
|
|
15
|
-
*/
|
|
16
12
|
private toScaled;
|
|
17
|
-
/**
|
|
18
|
-
* Convert scaled integer back to probability
|
|
19
|
-
*/
|
|
20
13
|
private fromScaled;
|
|
21
14
|
private validateConfig;
|
|
22
15
|
getItemDropRate(name: string): number;
|
|
23
|
-
getRarityProbability(rarity: string): number;
|
|
24
16
|
getCumulativeProbabilityForItem(name: string, rolls: number): number;
|
|
25
17
|
getRollsForTargetProbability(name: string, targetProbability: number): number;
|
|
26
18
|
getRateUpItems(): string[];
|
|
@@ -29,16 +21,9 @@ export declare class GachaEngine {
|
|
|
29
21
|
dropRate: number;
|
|
30
22
|
rarity: string;
|
|
31
23
|
}[];
|
|
32
|
-
/**
|
|
33
|
-
* Get scaled drop rate for internal calculations
|
|
34
|
-
*/
|
|
35
|
-
private getItemDropRateScaled;
|
|
36
24
|
roll(count?: number): string[];
|
|
37
25
|
private selectRarity;
|
|
38
26
|
private selectItemFromPool;
|
|
39
|
-
/**
|
|
40
|
-
* Debug method to inspect scaled values
|
|
41
|
-
*/
|
|
42
27
|
getDebugInfo(): {
|
|
43
28
|
scale: number;
|
|
44
29
|
rarityRatesScaled: Record<string, number>;
|
package/dist/index.cjs
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
function t(t,
|
|
1
|
+
function t(t,e){(null==e||e>t.length)&&(e=t.length);for(var r=0,a=Array(e);r<e;r++)a[r]=t[r];return a}function e(e,r){var a="undefined"!=typeof Symbol&&e[Symbol.iterator]||e["@@iterator"];if(a)return(a=a.call(e)).next.bind(a);if(Array.isArray(e)||(a=function(e,r){if(e){if("string"==typeof e)return t(e,r);var a={}.toString.call(e).slice(8,-1);return"Object"===a&&e.constructor&&(a=e.constructor.name),"Map"===a||"Set"===a?Array.from(e):"Arguments"===a||/^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(a)?t(e,r):void 0}}(e))||r&&e&&"number"==typeof e.length){a&&(e=a);var n=0;return function(){return n>=e.length?{done:!0}:{done:!1,value:e[n++]}}}throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}function r(){return r=Object.assign?Object.assign.bind():function(t){for(var e=1;e<arguments.length;e++){var r=arguments[e];for(var a in r)({}).hasOwnProperty.call(r,a)&&(t[a]=r[a])}return t},r.apply(null,arguments)}var a,n=/*#__PURE__*/function(){function t(t){if(this.mode=void 0,this.pools=[],this.rarityRatesScaled={},this.flatRateMap=new Map,this.dropRateCacheScaled=new Map,this.mode=t.mode,"weighted"===t.mode){var r=t;this.pools=r.pools,this.rarityRatesScaled=this.scaleRarityRates(r.rarityRates),this.validateConfig(r.rarityRates)}else{if("flatRate"!==t.mode)throw new Error("Unknown gacha mode: "+this.mode);for(var a,n=e(t.pools);!(a=n()).done;)for(var i,o=e(a.value.items);!(i=o()).done;){var s=i.value;if(s.weight<0)throw new Error('FlatRate item "'+s.name+'" must have non-negative weight');this.flatRateMap.set(s.name,s.weight)}var u=Array.from(this.flatRateMap.values()).reduce(function(t,e){return t+e},0);if(Math.abs(u-1)>1e-6)throw new Error("FlatRate item rates must sum to 1.0, but got "+u)}}var a=t.prototype;return a.scaleRarityRates=function(t){for(var e={},r=0,a=Object.entries(t);r<a.length;r++){var n=a[r],i=n[0],o=n[1];if(o<0||o>1)throw new Error('Rarity rate for "'+i+'" must be between 0 and 1, got '+o);e[i]=this.toScaled(o)}return e},a.toScaled=function(e){if(e>t.MAX_SAFE_SCALE/t.SCALE)throw new Error("Probability "+e+" too large for safe integer arithmetic");return Math.round(e*t.SCALE)},a.fromScaled=function(e){return e/t.SCALE},a.validateConfig=function(t){var r=new Set(Object.keys(this.rarityRatesScaled)),a=new Set(this.pools.map(function(t){return t.rarity})),n=Array.from(a).filter(function(t){return!r.has(t)});if(n.length>0)throw new Error("Missing rarity rates for: "+n.join(", "));var i=Object.values(t).reduce(function(t,e){return t+e},0);if(Math.abs(i-1)>1e-10)throw new Error("Rarity rates must sum to 1.0, got "+i);for(var o,s=e(this.pools);!(o=s()).done;){var u=o.value;if(0===u.items.length)throw new Error('Rarity "'+u.rarity+'" has no items');if(u.items.reduce(function(t,e){return t+e.weight},0)<=0)throw new Error('Rarity "'+u.rarity+'" has zero total weight');for(var l,f=e(u.items);!(l=f()).done;){var h=l.value;if(h.weight<0)throw new Error('Item "'+h.name+'" weight must be non-negative, got '+h.weight)}if(!u.items.some(function(t){return t.weight>0}))throw new Error('Rarity "'+u.rarity+'" must have at least one item with positive weight')}},a.getItemDropRate=function(r){if("flatRate"===this.mode)return this.flatRateMap.get(r)||0;if(this.dropRateCacheScaled.has(r))return this.fromScaled(this.dropRateCacheScaled.get(r));for(var a,n=e(this.pools);!(a=n()).done;){var i=a.value,o=i.items.find(function(t){return t.name===r});if(o){if(0===o.weight)return this.dropRateCacheScaled.set(r,0),0;var s=i.items.reduce(function(t,e){return t+e.weight},0),u=this.rarityRatesScaled[i.rarity],l=this.toScaled(o.weight),f=this.toScaled(s),h=Math.round(l*u/t.SCALE),c=Math.round(h*t.SCALE/f);return this.dropRateCacheScaled.set(r,c),this.fromScaled(c)}}throw new Error('Item "'+r+'" not found')},a.getCumulativeProbabilityForItem=function(t,e){var r=this.getItemDropRate(t);return 0===r?0:r>=1?1:1-Math.pow(1-r,e)},a.getRollsForTargetProbability=function(t,e){if(e<=0)return 0;if(e>=1)return 1;var r=this.getItemDropRate(t);return r<=0?Infinity:Math.ceil(Math.log(1-e)/Math.log(1-r))},a.getRateUpItems=function(){return this.pools.flatMap(function(t){return t.items.filter(function(t){return t.rateUp}).map(function(t){return t.name})})},a.getAllItemDropRates=function(){var t=this;return"flatRate"===this.mode?Array.from(this.flatRateMap.entries()).map(function(t){return{name:t[0],dropRate:t[1],rarity:"flatRate"}}):this.pools.flatMap(function(e){return e.items.map(function(r){return{name:r.name,dropRate:t.getItemDropRate(r.name),rarity:e.rarity}})})},a.roll=function(t){var r=this;void 0===t&&(t=1);for(var a=[],n=function(){if("flatRate"===r.mode)for(var t,n=Math.random(),i=0,o=e(r.flatRateMap.entries());!(t=o()).done;){var s=t.value;if(n<(i+=s[1])){a.push(s[0]);break}}else{var u=r.selectRarity(),l=r.pools.find(function(t){return t.rarity===u}),f=r.selectItemFromPool(l);a.push(f.name)}},i=0;i<t;i++)n();return a},a.selectRarity=function(){for(var e=Math.floor(Math.random()*t.SCALE),r=0,a=0,n=Object.entries(this.rarityRatesScaled);a<n.length;a++){var i=n[a];if(e<(r+=i[1]))return i[0]}return Object.keys(this.rarityRatesScaled)[0]},a.selectItemFromPool=function(t){for(var a,n=this,i=t.items.filter(function(t){return t.weight>0}),o=i.map(function(t){return r({},t,{scaledWeight:n.toScaled(t.weight)})}),s=o.reduce(function(t,e){return t+e.scaledWeight},0),u=Math.floor(Math.random()*s),l=0,f=e(o);!(a=f()).done;){var h=a.value;if(u<(l+=h.scaledWeight))return{name:h.name,weight:h.weight}}return i[0]},a.getDebugInfo=function(){for(var e={},a=0,n=Object.entries(this.rarityRatesScaled);a<n.length;a++){var i=n[a];e[i[0]]=this.fromScaled(i[1])}return{scale:t.SCALE,rarityRatesScaled:r({},this.rarityRatesScaled),rarityRatesFloat:e}},t}();a=n,n.SCALE=1e6,n.MAX_SAFE_SCALE=Math.floor(Number.MAX_SAFE_INTEGER/a.SCALE),exports.GachaEngine=n;
|
|
2
2
|
//# sourceMappingURL=index.cjs.map
|
package/dist/index.cjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.cjs","sources":["../src/gacha-engine.ts"],"sourcesContent":["import { RarityInput, GachaEngineConfig } from './types';\n\nexport class GachaEngine {\n // Scale factor for fixed-point arithmetic (1,000,000 = 6 decimal places)\n private static readonly SCALE = 1000000;\n private static readonly MAX_SAFE_SCALE = Math.floor(Number.MAX_SAFE_INTEGER / this.SCALE);\n\n private pools: RarityInput[];\n private rarityRatesScaled: Record<string, number>; // Scaled to integers\n private dropRateCacheScaled = new Map<string, number>(); // Cache scaled rates\n\n constructor({ rarityRates, pools }: GachaEngineConfig) {\n this.pools = pools;\n this.rarityRatesScaled = this.scaleRarityRates(rarityRates);\n this.validateConfig(rarityRates);\n }\n\n /**\n * Convert floating point rates to scaled integers\n */\n private scaleRarityRates(rarityRates: Record<string, number>): Record<string, number> {\n const scaled: Record<string, number> = {};\n for (const [rarity, rate] of Object.entries(rarityRates)) {\n if (rate < 0 || rate > 1) {\n throw new Error(`Rarity rate for \"${rarity}\" must be between 0 and 1, got ${rate}`);\n }\n scaled[rarity] = this.toScaled(rate);\n }\n return scaled;\n }\n\n /**\n * Convert probability to scaled integer\n */\n private toScaled(probability: number): number {\n if (probability > GachaEngine.MAX_SAFE_SCALE / GachaEngine.SCALE) {\n throw new Error(`Probability ${probability} too large for safe integer arithmetic`);\n }\n return Math.round(probability * GachaEngine.SCALE);\n }\n\n /**\n * Convert scaled integer back to probability\n */\n private fromScaled(scaledInt: number): number {\n return scaledInt / GachaEngine.SCALE;\n }\n\n private validateConfig(originalRates: Record<string, number>): void {\n const configuredRarities = new Set(Object.keys(this.rarityRatesScaled));\n const usedRarities = new Set(this.pools.map(p => p.rarity));\n const missingArray = Array.from(usedRarities).filter(r => !configuredRarities.has(r));\n \n if (missingArray.length > 0) {\n throw new Error(`Missing rarity rates for: ${missingArray.join(', ')}`);\n }\n\n // Validate that rates sum to exactly 1.0 (within floating point precision)\n const totalRate = Object.values(originalRates).reduce((sum, rate) => sum + rate, 0);\n const totalScaled = Object.values(this.rarityRatesScaled).reduce((sum, rate) => sum + rate, 0);\n \n if (Math.abs(totalRate - 1.0) > 1e-10) {\n throw new Error(`Rarity rates must sum to 1.0, got ${totalRate}`);\n }\n\n // Ensure scaled rates sum to SCALE (accounting for rounding)\n if (Math.abs(totalScaled - GachaEngine.SCALE) > Object.keys(this.rarityRatesScaled).length) {\n console.warn(`Scaled rates sum to ${totalScaled}, expected ${GachaEngine.SCALE}. This is likely due to rounding.`);\n }\n\n for (const pool of this.pools) {\n if (pool.items.length === 0) {\n throw new Error(`Rarity \"${pool.rarity}\" has no items`);\n }\n \n const totalWeight = pool.items.reduce((sum, i) => sum + i.weight, 0);\n if (totalWeight <= 0) {\n throw new Error(`Rarity \"${pool.rarity}\" has zero total weight`);\n }\n\n // Validate that all weights are non-negative\n for (const item of pool.items) {\n if (item.weight < 0) {\n throw new Error(`Item \"${item.name}\" weight must be non-negative, got ${item.weight}`);\n }\n }\n\n // Ensure at least one item has positive weight\n const hasPositiveWeight = pool.items.some(item => item.weight > 0);\n if (!hasPositiveWeight) {\n throw new Error(`Rarity \"${pool.rarity}\" must have at least one item with positive weight`);\n }\n }\n }\n\n getItemDropRate(name: string): number {\n if (this.dropRateCacheScaled.has(name)) {\n return this.fromScaled(this.dropRateCacheScaled.get(name)!);\n }\n\n for (const pool of this.pools) {\n const item = pool.items.find(i => i.name === name);\n if (item) {\n // Handle zero weight items (never drop)\n if (item.weight === 0) {\n this.dropRateCacheScaled.set(name, 0);\n return 0;\n }\n\n const totalPoolWeight = pool.items.reduce((sum, i) => sum + i.weight, 0);\n const baseRarityRateScaled = this.rarityRatesScaled[pool.rarity];\n \n // Convert weights to scaled integers for perfect precision\n const itemWeightScaled = this.toScaled(item.weight);\n const totalWeightScaled = this.toScaled(totalPoolWeight);\n \n // Scaled arithmetic: (itemWeight * baseRate) / totalWeight\n const numeratorScaled = Math.round((itemWeightScaled * baseRarityRateScaled) / GachaEngine.SCALE);\n const rateScaled = Math.round((numeratorScaled * GachaEngine.SCALE) / totalWeightScaled);\n \n this.dropRateCacheScaled.set(name, rateScaled);\n return this.fromScaled(rateScaled);\n }\n }\n throw new Error(`Item \"${name}\" not found`);\n }\n\n getRarityProbability(rarity: string): number {\n if (!this.rarityRatesScaled[rarity]) {\n throw new Error(`Rarity \"${rarity}\" not found`);\n }\n return this.fromScaled(this.rarityRatesScaled[rarity]);\n }\n\n getCumulativeProbabilityForItem(name: string, rolls: number): number {\n const rateScaled = this.getItemDropRateScaled(name);\n \n if (rateScaled === 0) return 0;\n if (rateScaled >= GachaEngine.SCALE) return 1;\n \n // Calculate (1 - rate)^rolls using scaled arithmetic\n const failRateScaled = GachaEngine.SCALE - rateScaled;\n const failRate = this.fromScaled(failRateScaled);\n \n // For large rolls, we need to be careful with precision\n const cumulativeFailProbability = Math.pow(failRate, rolls);\n const cumulativeProbability = 1 - cumulativeFailProbability;\n \n return Math.min(1, Math.max(0, cumulativeProbability));\n }\n\n getRollsForTargetProbability(name: string, targetProbability: number): number {\n if (targetProbability <= 0) return 0;\n if (targetProbability >= 1) return 1;\n \n const rate = this.getItemDropRate(name);\n if (rate <= 0) return Infinity;\n if (rate >= 1) return 1;\n \n return Math.ceil(Math.log(1 - targetProbability) / Math.log(1 - rate));\n }\n\n getRateUpItems(): string[] {\n return this.pools.flatMap(p =>\n p.items.filter(i => i.rateUp).map(i => i.name)\n );\n }\n\n getAllItemDropRates(): { name: string; dropRate: number; rarity: string }[] {\n return this.pools.flatMap(p =>\n p.items.map(i => ({\n name: i.name,\n dropRate: this.getItemDropRate(i.name),\n rarity: p.rarity\n }))\n );\n }\n\n /**\n * Get scaled drop rate for internal calculations\n */\n private getItemDropRateScaled(name: string): number {\n if (this.dropRateCacheScaled.has(name)) {\n return this.dropRateCacheScaled.get(name)!;\n }\n \n // Trigger calculation and caching\n this.getItemDropRate(name);\n return this.dropRateCacheScaled.get(name)!;\n }\n\n roll(count: number = 1): string[] {\n const results: string[] = [];\n for (let i = 0; i < count; i++) {\n const rarity = this.selectRarity();\n const pool = this.pools.find(p => p.rarity === rarity)!;\n const item = this.selectItemFromPool(pool);\n results.push(item.name);\n }\n return results;\n }\n\n private selectRarity(): string {\n const rand = Math.floor(Math.random() * GachaEngine.SCALE);\n let cumulativeScaled = 0;\n \n for (const [rarity, rateScaled] of Object.entries(this.rarityRatesScaled)) {\n cumulativeScaled += rateScaled;\n if (rand < cumulativeScaled) return rarity;\n }\n \n // Fallback (should never happen with proper validation)\n return Object.keys(this.rarityRatesScaled)[0];\n }\n\n private selectItemFromPool(pool: RarityInput): { name: string; weight: number } {\n // Filter out zero-weight items (they can never be selected)\n const selectableItems = pool.items.filter(item => item.weight > 0);\n \n if (selectableItems.length === 0) {\n throw new Error(`No selectable items in pool for rarity \"${pool.rarity}\"`);\n }\n\n // Convert all weights to scaled integers for perfect precision\n const scaledItems = selectableItems.map(item => ({\n ...item,\n scaledWeight: this.toScaled(item.weight)\n }));\n \n const totalScaledWeight = scaledItems.reduce((sum, item) => sum + item.scaledWeight, 0);\n const rand = Math.floor(Math.random() * totalScaledWeight);\n let cumulative = 0;\n \n for (const item of scaledItems) {\n cumulative += item.scaledWeight;\n if (rand < cumulative) {\n return { name: item.name, weight: item.weight };\n }\n }\n \n // Fallback (should never happen)\n return selectableItems[0];\n }\n\n /**\n * Debug method to inspect scaled values\n */\n getDebugInfo(): {\n scale: number;\n rarityRatesScaled: Record<string, number>;\n rarityRatesFloat: Record<string, number>;\n } {\n const rarityRatesFloat: Record<string, number> = {};\n for (const [rarity, scaledRate] of Object.entries(this.rarityRatesScaled)) {\n rarityRatesFloat[rarity] = this.fromScaled(scaledRate);\n }\n \n return {\n scale: GachaEngine.SCALE,\n rarityRatesScaled: { ...this.rarityRatesScaled },\n rarityRatesFloat\n };\n }\n}"],"names":["GachaEngine","_ref","rarityRates","pools","rarityRatesScaled","dropRateCacheScaled","Map","this","scaleRarityRates","validateConfig","_proto","prototype","scaled","_i","_Object$entries","Object","entries","length","_Object$entries$_i","rarity","rate","Error","toScaled","probability","MAX_SAFE_SCALE","SCALE","Math","round","fromScaled","scaledInt","originalRates","configuredRarities","Set","keys","usedRarities","map","p","missingArray","Array","from","filter","r","has","join","totalRate","values","reduce","sum","totalScaled","abs","console","warn","_iterator","_step","_createForOfIteratorHelperLoose","done","pool","value","items","i","weight","_step2","_iterator2","item","name","hasPositiveWeight","some","getItemDropRate","get","_step3","_iterator3","find","set","totalPoolWeight","baseRarityRateScaled","itemWeightScaled","totalWeightScaled","numeratorScaled","rateScaled","getRarityProbability","getCumulativeProbabilityForItem","rolls","getItemDropRateScaled","failRate","cumulativeFailProbability","pow","min","max","getRollsForTargetProbability","targetProbability","Infinity","ceil","log","getRateUpItems","flatMap","rateUp","getAllItemDropRates","_this","dropRate","roll","count","_this2","results","_loop","selectRarity","selectItemFromPool","push","rand","floor","random","cumulativeScaled","_i2","_Object$entries2","_Object$entries2$_i","_this3","selectableItems","_step4","scaledItems","_extends","scaledWeight","totalScaledWeight","cumulative","_iterator4","getDebugInfo","rarityRatesFloat","_i3","_Object$entries3","_Object$entries3$_i","scale","Number","MAX_SAFE_INTEGER","_GachaEngine"],"mappings":"kgCAEaA,eAST,WAAA,SAAAA,EAAAC,GAAc,IAAAC,EAAWD,EAAXC,YAAaC,EAAKF,EAALE,MAJnBA,KAAAA,WACAC,EAAAA,KAAAA,8BACAC,oBAAsB,IAAIC,IAG9BC,KAAKJ,MAAQA,EACbI,K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AKkF,MAAMqB,OAAOC,iBAAmBC,EAAK1G"}
|
|
1
|
+
{"version":3,"file":"index.cjs","sources":["../src/gacha-engine.ts"],"sourcesContent":["import {\n RarityInput,\n GachaEngineConfig,\n WeightedGachaEngineConfig,\n FlatRateGachaEngineConfig,\n} from './types';\n\nexport class GachaEngine {\n private static readonly SCALE = 1_000_000;\n private static readonly MAX_SAFE_SCALE = Math.floor(Number.MAX_SAFE_INTEGER / GachaEngine.SCALE);\n\n private mode: 'weighted' | 'flatRate';\n private pools: RarityInput[] = [];\n private rarityRatesScaled: Record<string, number> = {};\n private flatRateMap: Map<string, number> = new Map();\n private dropRateCacheScaled = new Map<string, number>();\n\n constructor(config: GachaEngineConfig) {\n this.mode = config.mode;\n\n if (config.mode === 'weighted') {\n const weightedConfig = config as WeightedGachaEngineConfig;\n this.pools = weightedConfig.pools;\n this.rarityRatesScaled = this.scaleRarityRates(weightedConfig.rarityRates);\n this.validateConfig(weightedConfig.rarityRates);\n } else if (config.mode === 'flatRate') {\n const flatConfig = config as FlatRateGachaEngineConfig;\n for (const pool of flatConfig.pools) {\n for (const item of pool.items) {\n if (item.weight < 0) {\n throw new Error(`FlatRate item \"${item.name}\" must have non-negative weight`);\n }\n this.flatRateMap.set(item.name, item.weight); // Here, interpreted as direct probability\n }\n }\n const total = Array.from(this.flatRateMap.values()).reduce((sum, v) => sum + v, 0);\n if (Math.abs(total - 1.0) > 1e-6) {\n throw new Error(`FlatRate item rates must sum to 1.0, but got ${total}`);\n }\n } else {\n throw new Error(`Unknown gacha mode: ${this.mode}`);\n }\n }\n\n private scaleRarityRates(rarityRates: Record<string, number>): Record<string, number> {\n const scaled: Record<string, number> = {};\n for (const [rarity, rate] of Object.entries(rarityRates)) {\n if (rate < 0 || rate > 1) {\n throw new Error(`Rarity rate for \"${rarity}\" must be between 0 and 1, got ${rate}`);\n }\n scaled[rarity] = this.toScaled(rate);\n }\n return scaled;\n }\n\n private toScaled(probability: number): number {\n if (probability > GachaEngine.MAX_SAFE_SCALE / GachaEngine.SCALE) {\n throw new Error(`Probability ${probability} too large for safe integer arithmetic`);\n }\n return Math.round(probability * GachaEngine.SCALE);\n }\n\n private fromScaled(scaledInt: number): number {\n return scaledInt / GachaEngine.SCALE;\n }\n\n private validateConfig(originalRates: Record<string, number>): void {\n const configuredRarities = new Set(Object.keys(this.rarityRatesScaled));\n const usedRarities = new Set(this.pools.map(p => p.rarity));\n const missing = Array.from(usedRarities).filter(r => !configuredRarities.has(r));\n\n if (missing.length > 0) {\n throw new Error(`Missing rarity rates for: ${missing.join(', ')}`);\n }\n\n const totalRate = Object.values(originalRates).reduce((sum, rate) => sum + rate, 0);\n if (Math.abs(totalRate - 1.0) > 1e-10) {\n throw new Error(`Rarity rates must sum to 1.0, got ${totalRate}`);\n }\n\n for (const pool of this.pools) {\n if (pool.items.length === 0) {\n throw new Error(`Rarity \"${pool.rarity}\" has no items`);\n }\n\n const totalWeight = pool.items.reduce((sum, i) => sum + i.weight, 0);\n if (totalWeight <= 0) {\n throw new Error(`Rarity \"${pool.rarity}\" has zero total weight`);\n }\n\n for (const item of pool.items) {\n if (item.weight < 0) {\n throw new Error(`Item \"${item.name}\" weight must be non-negative, got ${item.weight}`);\n }\n }\n\n if (!pool.items.some(i => i.weight > 0)) {\n throw new Error(`Rarity \"${pool.rarity}\" must have at least one item with positive weight`);\n }\n }\n }\n\n getItemDropRate(name: string): number {\n if (this.mode === 'flatRate') {\n return this.flatRateMap.get(name) || 0;\n }\n\n if (this.dropRateCacheScaled.has(name)) {\n return this.fromScaled(this.dropRateCacheScaled.get(name)!);\n }\n\n for (const pool of this.pools) {\n const item = pool.items.find(i => i.name === name);\n if (item) {\n if (item.weight === 0) {\n this.dropRateCacheScaled.set(name, 0);\n return 0;\n }\n\n const totalPoolWeight = pool.items.reduce((sum, i) => sum + i.weight, 0);\n const baseRarityRateScaled = this.rarityRatesScaled[pool.rarity];\n const itemWeightScaled = this.toScaled(item.weight);\n const totalWeightScaled = this.toScaled(totalPoolWeight);\n const numeratorScaled = Math.round((itemWeightScaled * baseRarityRateScaled) / GachaEngine.SCALE);\n const rateScaled = Math.round((numeratorScaled * GachaEngine.SCALE) / totalWeightScaled);\n\n this.dropRateCacheScaled.set(name, rateScaled);\n return this.fromScaled(rateScaled);\n }\n }\n\n throw new Error(`Item \"${name}\" not found`);\n }\n\n getCumulativeProbabilityForItem(name: string, rolls: number): number {\n const rate = this.getItemDropRate(name);\n if (rate === 0) return 0;\n if (rate >= 1) return 1;\n\n const cumulativeFailProbability = Math.pow(1 - rate, rolls);\n return 1 - cumulativeFailProbability;\n }\n\n getRollsForTargetProbability(name: string, targetProbability: number): number {\n if (targetProbability <= 0) return 0;\n if (targetProbability >= 1) return 1;\n\n const rate = this.getItemDropRate(name);\n if (rate <= 0) return Infinity;\n return Math.ceil(Math.log(1 - targetProbability) / Math.log(1 - rate));\n }\n\n getRateUpItems(): string[] {\n return this.pools.flatMap(p => p.items.filter(i => i.rateUp).map(i => i.name));\n }\n\n getAllItemDropRates(): { name: string; dropRate: number; rarity: string }[] {\n if (this.mode === 'flatRate') {\n return Array.from(this.flatRateMap.entries()).map(([name, dropRate]) => ({\n name,\n dropRate,\n rarity: 'flatRate',\n }));\n }\n\n return this.pools.flatMap(p =>\n p.items.map(i => ({\n name: i.name,\n dropRate: this.getItemDropRate(i.name),\n rarity: p.rarity,\n }))\n );\n }\n\n roll(count: number = 1): string[] {\n const results: string[] = [];\n for (let i = 0; i < count; i++) {\n if (this.mode === 'flatRate') {\n const rand = Math.random();\n let cumulative = 0;\n for (const [name, rate] of this.flatRateMap.entries()) {\n cumulative += rate;\n if (rand < cumulative) {\n results.push(name);\n break;\n }\n }\n } else {\n const rarity = this.selectRarity();\n const pool = this.pools.find(p => p.rarity === rarity)!;\n const item = this.selectItemFromPool(pool);\n results.push(item.name);\n }\n }\n return results;\n }\n\n private selectRarity(): string {\n const rand = Math.floor(Math.random() * GachaEngine.SCALE);\n let cumulative = 0;\n\n for (const [rarity, scaledRate] of Object.entries(this.rarityRatesScaled)) {\n cumulative += scaledRate;\n if (rand < cumulative) return rarity;\n }\n\n return Object.keys(this.rarityRatesScaled)[0];\n }\n\n private selectItemFromPool(pool: RarityInput): { name: string; weight: number } {\n const items = pool.items.filter(i => i.weight > 0);\n const scaledItems = items.map(i => ({\n ...i,\n scaledWeight: this.toScaled(i.weight),\n }));\n\n const totalScaledWeight = scaledItems.reduce((sum, i) => sum + i.scaledWeight, 0);\n const rand = Math.floor(Math.random() * totalScaledWeight);\n let cumulative = 0;\n\n for (const item of scaledItems) {\n cumulative += item.scaledWeight;\n if (rand < cumulative) {\n return { name: item.name, weight: item.weight };\n }\n }\n\n return items[0]; // Fallback\n }\n\n getDebugInfo(): {\n scale: number;\n rarityRatesScaled: Record<string, number>;\n rarityRatesFloat: Record<string, number>;\n } {\n const rarityRatesFloat: Record<string, number> = {};\n for (const [rarity, scaledRate] of Object.entries(this.rarityRatesScaled)) {\n rarityRatesFloat[rarity] = this.fromScaled(scaledRate);\n }\n\n return {\n scale: GachaEngine.SCALE,\n rarityRatesScaled: { ...this.rarityRatesScaled },\n rarityRatesFloat,\n };\n }\n}\n"],"names":["GachaEngine","config","this","mode","pools","rarityRatesScaled","flatRateMap","Map","dropRateCacheScaled","weightedConfig","scaleRarityRates","rarityRates","validateConfig","Error","_step","_iterator","_createForOfIteratorHelperLoose","done","_step2","_iterator2","value","items","item","weight","name","set","total","Array","from","values","reduce","sum","v","Math","abs","_proto","prototype","scaled","_i","_Object$entries","Object","entries","length","_Object$entries$_i","rarity","rate","toScaled","probability","MAX_SAFE_SCALE","SCALE","round","fromScaled","scaledInt","originalRates","configuredRarities","Set","keys","usedRarities","map","p","missing","filter","r","has","join","totalRate","_iterator3","_step3","pool","i","_step4","_iterator4","some","getItemDropRate","get","_iterator5","_step5","find","totalPoolWeight","baseRarityRateScaled","itemWeightScaled","totalWeightScaled","numeratorScaled","rateScaled","getCumulativeProbabilityForItem","rolls","pow","getRollsForTargetProbability","targetProbability","Infinity","ceil","log","getRateUpItems","flatMap","rateUp","getAllItemDropRates","_this","_ref","dropRate","roll","count","_this2","results","_loop","_step6","rand","random","cumulative","_iterator6","_step6$value","push","selectRarity","selectItemFromPool","floor","_i2","_Object$entries2","_Object$entries2$_i","scaledRate","_step7","_this3","scaledItems","_extends","scaledWeight","totalScaledWeight","_iterator7","getDebugInfo","rarityRatesFloat","_i3","_Object$entries3","_Object$entries3$_i","scale","Number","MAX_SAFE_INTEGER"],"mappings":"kgCAOaA,eAAW,WAUtB,SAAAA,EAAYC,GAGV,GAHmCC,KAN7BC,UAAI,EAAAD,KACJE,MAAuB,GACvBC,KAAAA,kBAA4C,CAAA,EAAEH,KAC9CI,YAAmC,IAAIC,IACvCC,KAAAA,oBAAsB,IAAID,IAGhCL,KAAKC,KAAOF,EAAOE,KAEC,aAAhBF,EAAOE,KAAqB,CAC9B,IAAMM,EAAiBR,EACvBC,KAAKE,MAAQK,EAAeL,MAC5BF,KAAKG,kBAAoBH,KAAKQ,iBAAiBD,EAAeE,aAC9DT,KAAKU,eAAeH,EAAeE,YACrC,SAA2B,aAAhBV,EAAOE,KAehB,MAAM,IAAIU,6BAA6BX,KAAKC,MAb5C,IADA,IACmCW,EAAnCC,EAAAC,EADmBf,EACWG,SAAKU,EAAAC,KAAAE,MACjC,IADmC,IACNC,EAA7BC,EAAAH,EADaF,EAAAM,MACWC,SAAKH,EAAAC,KAAAF,MAAE,CAApB,IAAAK,EAAIJ,EAAAE,MACb,GAAIE,EAAKC,OAAS,EAChB,MAAU,IAAAV,MAAK,kBAAmBS,EAAKE,KAAI,mCAE7CtB,KAAKI,YAAYmB,IAAIH,EAAKE,KAAMF,EAAKC,OACvC,CAEF,IAAMG,EAAQC,MAAMC,KAAK1B,KAAKI,YAAYuB,UAAUC,OAAO,SAACC,EAAKC,GAAC,OAAKD,EAAMC,CAAC,EAAE,GAChF,GAAIC,KAAKC,IAAIR,EAAQ,GAAO,KAC1B,UAAUb,MAAsDa,gDAAAA,EAIpE,C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ArH,KACpCmB,EAAQ+C,EAAK/C,MAAMwC,OAAO,SAAAQ,UAAKA,EAAE9C,OAAS,CAAC,GAC3CiG,EAAcnG,EAAMqC,IAAI,SAAAW,GAACoD,OAAAA,EAC1BpD,CAAAA,EAAAA,EACHqD,CAAAA,aAAcH,EAAKzE,SAASuB,EAAE9C,SAC9B,GAEIoG,EAAoBH,EAAY1F,OAAO,SAACC,EAAKsC,GAAC,OAAKtC,EAAMsC,EAAEqD,YAAY,EAAE,GACzEjB,EAAOxE,KAAKgF,MAAMhF,KAAKyE,SAAWiB,GACpChB,EAAa,EAEjBiB,EAAA5G,EAAmBwG,KAAWF,EAAAM,KAAA3G,MAAE,CAArB,IAAAK,EAAIgG,EAAAlG,MAEb,GAAIqF,GADJE,GAAcrF,EAAKoG,cAEjB,MAAO,CAAElG,KAAMF,EAAKE,KAAMD,OAAQD,EAAKC,OAE3C,CAEA,OAAOF,EAAM,EACf,EAACc,EAED0F,aAAA,WAME,IADA,IAAMC,EAA2C,CAAE,EACnDC,EAAA,EAAAC,EAAmCxF,OAAOC,QAAQvC,KAAKG,mBAAkB0H,EAAAC,EAAAtF,OAAAqF,IAAE,CAAtE,IAAAE,EAAAD,EAAAD,GACHD,EADgBG,EAAA,IACW/H,KAAKiD,WADJ8E,EAAA,GAE9B,CAEA,MAAO,CACLC,MAAOlI,EAAYiD,MACnB5C,kBAAiBoH,EAAO,CAAA,EAAAvH,KAAKG,mBAC7ByH,iBAAAA,EAEJ,EAAC9H,CAAA,CA9OqB,KAAXA,EAAAA,EACaiD,MAAQ,IADrBjD,EAEagD,eAAiBf,KAAKgF,MAAMkB,OAAOC,iBAAmBpI,EAAYiD"}
|
package/dist/index.module.js
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
function t(t,
|
|
1
|
+
function t(t,e){(null==e||e>t.length)&&(e=t.length);for(var r=0,a=Array(e);r<e;r++)a[r]=t[r];return a}function e(e,r){var a="undefined"!=typeof Symbol&&e[Symbol.iterator]||e["@@iterator"];if(a)return(a=a.call(e)).next.bind(a);if(Array.isArray(e)||(a=function(e,r){if(e){if("string"==typeof e)return t(e,r);var a={}.toString.call(e).slice(8,-1);return"Object"===a&&e.constructor&&(a=e.constructor.name),"Map"===a||"Set"===a?Array.from(e):"Arguments"===a||/^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(a)?t(e,r):void 0}}(e))||r&&e&&"number"==typeof e.length){a&&(e=a);var n=0;return function(){return n>=e.length?{done:!0}:{done:!1,value:e[n++]}}}throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}function r(){return r=Object.assign?Object.assign.bind():function(t){for(var e=1;e<arguments.length;e++){var r=arguments[e];for(var a in r)({}).hasOwnProperty.call(r,a)&&(t[a]=r[a])}return t},r.apply(null,arguments)}var a,n=/*#__PURE__*/function(){function t(t){if(this.mode=void 0,this.pools=[],this.rarityRatesScaled={},this.flatRateMap=new Map,this.dropRateCacheScaled=new Map,this.mode=t.mode,"weighted"===t.mode){var r=t;this.pools=r.pools,this.rarityRatesScaled=this.scaleRarityRates(r.rarityRates),this.validateConfig(r.rarityRates)}else{if("flatRate"!==t.mode)throw new Error("Unknown gacha mode: "+this.mode);for(var a,n=e(t.pools);!(a=n()).done;)for(var i,o=e(a.value.items);!(i=o()).done;){var s=i.value;if(s.weight<0)throw new Error('FlatRate item "'+s.name+'" must have non-negative weight');this.flatRateMap.set(s.name,s.weight)}var u=Array.from(this.flatRateMap.values()).reduce(function(t,e){return t+e},0);if(Math.abs(u-1)>1e-6)throw new Error("FlatRate item rates must sum to 1.0, but got "+u)}}var a=t.prototype;return a.scaleRarityRates=function(t){for(var e={},r=0,a=Object.entries(t);r<a.length;r++){var n=a[r],i=n[0],o=n[1];if(o<0||o>1)throw new Error('Rarity rate for "'+i+'" must be between 0 and 1, got '+o);e[i]=this.toScaled(o)}return e},a.toScaled=function(e){if(e>t.MAX_SAFE_SCALE/t.SCALE)throw new Error("Probability "+e+" too large for safe integer arithmetic");return Math.round(e*t.SCALE)},a.fromScaled=function(e){return e/t.SCALE},a.validateConfig=function(t){var r=new Set(Object.keys(this.rarityRatesScaled)),a=new Set(this.pools.map(function(t){return t.rarity})),n=Array.from(a).filter(function(t){return!r.has(t)});if(n.length>0)throw new Error("Missing rarity rates for: "+n.join(", "));var i=Object.values(t).reduce(function(t,e){return t+e},0);if(Math.abs(i-1)>1e-10)throw new Error("Rarity rates must sum to 1.0, got "+i);for(var o,s=e(this.pools);!(o=s()).done;){var u=o.value;if(0===u.items.length)throw new Error('Rarity "'+u.rarity+'" has no items');if(u.items.reduce(function(t,e){return t+e.weight},0)<=0)throw new Error('Rarity "'+u.rarity+'" has zero total weight');for(var l,f=e(u.items);!(l=f()).done;){var h=l.value;if(h.weight<0)throw new Error('Item "'+h.name+'" weight must be non-negative, got '+h.weight)}if(!u.items.some(function(t){return t.weight>0}))throw new Error('Rarity "'+u.rarity+'" must have at least one item with positive weight')}},a.getItemDropRate=function(r){if("flatRate"===this.mode)return this.flatRateMap.get(r)||0;if(this.dropRateCacheScaled.has(r))return this.fromScaled(this.dropRateCacheScaled.get(r));for(var a,n=e(this.pools);!(a=n()).done;){var i=a.value,o=i.items.find(function(t){return t.name===r});if(o){if(0===o.weight)return this.dropRateCacheScaled.set(r,0),0;var s=i.items.reduce(function(t,e){return t+e.weight},0),u=this.rarityRatesScaled[i.rarity],l=this.toScaled(o.weight),f=this.toScaled(s),h=Math.round(l*u/t.SCALE),c=Math.round(h*t.SCALE/f);return this.dropRateCacheScaled.set(r,c),this.fromScaled(c)}}throw new Error('Item "'+r+'" not found')},a.getCumulativeProbabilityForItem=function(t,e){var r=this.getItemDropRate(t);return 0===r?0:r>=1?1:1-Math.pow(1-r,e)},a.getRollsForTargetProbability=function(t,e){if(e<=0)return 0;if(e>=1)return 1;var r=this.getItemDropRate(t);return r<=0?Infinity:Math.ceil(Math.log(1-e)/Math.log(1-r))},a.getRateUpItems=function(){return this.pools.flatMap(function(t){return t.items.filter(function(t){return t.rateUp}).map(function(t){return t.name})})},a.getAllItemDropRates=function(){var t=this;return"flatRate"===this.mode?Array.from(this.flatRateMap.entries()).map(function(t){return{name:t[0],dropRate:t[1],rarity:"flatRate"}}):this.pools.flatMap(function(e){return e.items.map(function(r){return{name:r.name,dropRate:t.getItemDropRate(r.name),rarity:e.rarity}})})},a.roll=function(t){var r=this;void 0===t&&(t=1);for(var a=[],n=function(){if("flatRate"===r.mode)for(var t,n=Math.random(),i=0,o=e(r.flatRateMap.entries());!(t=o()).done;){var s=t.value;if(n<(i+=s[1])){a.push(s[0]);break}}else{var u=r.selectRarity(),l=r.pools.find(function(t){return t.rarity===u}),f=r.selectItemFromPool(l);a.push(f.name)}},i=0;i<t;i++)n();return a},a.selectRarity=function(){for(var e=Math.floor(Math.random()*t.SCALE),r=0,a=0,n=Object.entries(this.rarityRatesScaled);a<n.length;a++){var i=n[a];if(e<(r+=i[1]))return i[0]}return Object.keys(this.rarityRatesScaled)[0]},a.selectItemFromPool=function(t){for(var a,n=this,i=t.items.filter(function(t){return t.weight>0}),o=i.map(function(t){return r({},t,{scaledWeight:n.toScaled(t.weight)})}),s=o.reduce(function(t,e){return t+e.scaledWeight},0),u=Math.floor(Math.random()*s),l=0,f=e(o);!(a=f()).done;){var h=a.value;if(u<(l+=h.scaledWeight))return{name:h.name,weight:h.weight}}return i[0]},a.getDebugInfo=function(){for(var e={},a=0,n=Object.entries(this.rarityRatesScaled);a<n.length;a++){var i=n[a];e[i[0]]=this.fromScaled(i[1])}return{scale:t.SCALE,rarityRatesScaled:r({},this.rarityRatesScaled),rarityRatesFloat:e}},t}();a=n,n.SCALE=1e6,n.MAX_SAFE_SCALE=Math.floor(Number.MAX_SAFE_INTEGER/a.SCALE);export{n as GachaEngine};
|
|
2
2
|
//# sourceMappingURL=index.module.js.map
|
package/dist/index.module.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.module.js","sources":["../src/gacha-engine.ts"],"sourcesContent":["import { RarityInput, GachaEngineConfig } from './types';\n\nexport class GachaEngine {\n // Scale factor for fixed-point arithmetic (1,000,000 = 6 decimal places)\n private static readonly SCALE = 1000000;\n private static readonly MAX_SAFE_SCALE = Math.floor(Number.MAX_SAFE_INTEGER / this.SCALE);\n\n private pools: RarityInput[];\n private rarityRatesScaled: Record<string, number>; // Scaled to integers\n private dropRateCacheScaled = new Map<string, number>(); // Cache scaled rates\n\n constructor({ rarityRates, pools }: GachaEngineConfig) {\n this.pools = pools;\n this.rarityRatesScaled = this.scaleRarityRates(rarityRates);\n this.validateConfig(rarityRates);\n }\n\n /**\n * Convert floating point rates to scaled integers\n */\n private scaleRarityRates(rarityRates: Record<string, number>): Record<string, number> {\n const scaled: Record<string, number> = {};\n for (const [rarity, rate] of Object.entries(rarityRates)) {\n if (rate < 0 || rate > 1) {\n throw new Error(`Rarity rate for \"${rarity}\" must be between 0 and 1, got ${rate}`);\n }\n scaled[rarity] = this.toScaled(rate);\n }\n return scaled;\n }\n\n /**\n * Convert probability to scaled integer\n */\n private toScaled(probability: number): number {\n if (probability > GachaEngine.MAX_SAFE_SCALE / GachaEngine.SCALE) {\n throw new Error(`Probability ${probability} too large for safe integer arithmetic`);\n }\n return Math.round(probability * GachaEngine.SCALE);\n }\n\n /**\n * Convert scaled integer back to probability\n */\n private fromScaled(scaledInt: number): number {\n return scaledInt / GachaEngine.SCALE;\n }\n\n private validateConfig(originalRates: Record<string, number>): void {\n const configuredRarities = new Set(Object.keys(this.rarityRatesScaled));\n const usedRarities = new Set(this.pools.map(p => p.rarity));\n const missingArray = Array.from(usedRarities).filter(r => !configuredRarities.has(r));\n \n if (missingArray.length > 0) {\n throw new Error(`Missing rarity rates for: ${missingArray.join(', ')}`);\n }\n\n // Validate that rates sum to exactly 1.0 (within floating point precision)\n const totalRate = Object.values(originalRates).reduce((sum, rate) => sum + rate, 0);\n const totalScaled = Object.values(this.rarityRatesScaled).reduce((sum, rate) => sum + rate, 0);\n \n if (Math.abs(totalRate - 1.0) > 1e-10) {\n throw new Error(`Rarity rates must sum to 1.0, got ${totalRate}`);\n }\n\n // Ensure scaled rates sum to SCALE (accounting for rounding)\n if (Math.abs(totalScaled - GachaEngine.SCALE) > Object.keys(this.rarityRatesScaled).length) {\n console.warn(`Scaled rates sum to ${totalScaled}, expected ${GachaEngine.SCALE}. This is likely due to rounding.`);\n }\n\n for (const pool of this.pools) {\n if (pool.items.length === 0) {\n throw new Error(`Rarity \"${pool.rarity}\" has no items`);\n }\n \n const totalWeight = pool.items.reduce((sum, i) => sum + i.weight, 0);\n if (totalWeight <= 0) {\n throw new Error(`Rarity \"${pool.rarity}\" has zero total weight`);\n }\n\n // Validate that all weights are non-negative\n for (const item of pool.items) {\n if (item.weight < 0) {\n throw new Error(`Item \"${item.name}\" weight must be non-negative, got ${item.weight}`);\n }\n }\n\n // Ensure at least one item has positive weight\n const hasPositiveWeight = pool.items.some(item => item.weight > 0);\n if (!hasPositiveWeight) {\n throw new Error(`Rarity \"${pool.rarity}\" must have at least one item with positive weight`);\n }\n }\n }\n\n getItemDropRate(name: string): number {\n if (this.dropRateCacheScaled.has(name)) {\n return this.fromScaled(this.dropRateCacheScaled.get(name)!);\n }\n\n for (const pool of this.pools) {\n const item = pool.items.find(i => i.name === name);\n if (item) {\n // Handle zero weight items (never drop)\n if (item.weight === 0) {\n this.dropRateCacheScaled.set(name, 0);\n return 0;\n }\n\n const totalPoolWeight = pool.items.reduce((sum, i) => sum + i.weight, 0);\n const baseRarityRateScaled = this.rarityRatesScaled[pool.rarity];\n \n // Convert weights to scaled integers for perfect precision\n const itemWeightScaled = this.toScaled(item.weight);\n const totalWeightScaled = this.toScaled(totalPoolWeight);\n \n // Scaled arithmetic: (itemWeight * baseRate) / totalWeight\n const numeratorScaled = Math.round((itemWeightScaled * baseRarityRateScaled) / GachaEngine.SCALE);\n const rateScaled = Math.round((numeratorScaled * GachaEngine.SCALE) / totalWeightScaled);\n \n this.dropRateCacheScaled.set(name, rateScaled);\n return this.fromScaled(rateScaled);\n }\n }\n throw new Error(`Item \"${name}\" not found`);\n }\n\n getRarityProbability(rarity: string): number {\n if (!this.rarityRatesScaled[rarity]) {\n throw new Error(`Rarity \"${rarity}\" not found`);\n }\n return this.fromScaled(this.rarityRatesScaled[rarity]);\n }\n\n getCumulativeProbabilityForItem(name: string, rolls: number): number {\n const rateScaled = this.getItemDropRateScaled(name);\n \n if (rateScaled === 0) return 0;\n if (rateScaled >= GachaEngine.SCALE) return 1;\n \n // Calculate (1 - rate)^rolls using scaled arithmetic\n const failRateScaled = GachaEngine.SCALE - rateScaled;\n const failRate = this.fromScaled(failRateScaled);\n \n // For large rolls, we need to be careful with precision\n const cumulativeFailProbability = Math.pow(failRate, rolls);\n const cumulativeProbability = 1 - cumulativeFailProbability;\n \n return Math.min(1, Math.max(0, cumulativeProbability));\n }\n\n getRollsForTargetProbability(name: string, targetProbability: number): number {\n if (targetProbability <= 0) return 0;\n if (targetProbability >= 1) return 1;\n \n const rate = this.getItemDropRate(name);\n if (rate <= 0) return Infinity;\n if (rate >= 1) return 1;\n \n return Math.ceil(Math.log(1 - targetProbability) / Math.log(1 - rate));\n }\n\n getRateUpItems(): string[] {\n return this.pools.flatMap(p =>\n p.items.filter(i => i.rateUp).map(i => i.name)\n );\n }\n\n getAllItemDropRates(): { name: string; dropRate: number; rarity: string }[] {\n return this.pools.flatMap(p =>\n p.items.map(i => ({\n name: i.name,\n dropRate: this.getItemDropRate(i.name),\n rarity: p.rarity\n }))\n );\n }\n\n /**\n * Get scaled drop rate for internal calculations\n */\n private getItemDropRateScaled(name: string): number {\n if (this.dropRateCacheScaled.has(name)) {\n return this.dropRateCacheScaled.get(name)!;\n }\n \n // Trigger calculation and caching\n this.getItemDropRate(name);\n return this.dropRateCacheScaled.get(name)!;\n }\n\n roll(count: number = 1): string[] {\n const results: string[] = [];\n for (let i = 0; i < count; i++) {\n const rarity = this.selectRarity();\n const pool = this.pools.find(p => p.rarity === rarity)!;\n const item = this.selectItemFromPool(pool);\n results.push(item.name);\n }\n return results;\n }\n\n private selectRarity(): string {\n const rand = Math.floor(Math.random() * GachaEngine.SCALE);\n let cumulativeScaled = 0;\n \n for (const [rarity, rateScaled] of Object.entries(this.rarityRatesScaled)) {\n cumulativeScaled += rateScaled;\n if (rand < cumulativeScaled) return rarity;\n }\n \n // Fallback (should never happen with proper validation)\n return Object.keys(this.rarityRatesScaled)[0];\n }\n\n private selectItemFromPool(pool: RarityInput): { name: string; weight: number } {\n // Filter out zero-weight items (they can never be selected)\n const selectableItems = pool.items.filter(item => item.weight > 0);\n \n if (selectableItems.length === 0) {\n throw new Error(`No selectable items in pool for rarity \"${pool.rarity}\"`);\n }\n\n // Convert all weights to scaled integers for perfect precision\n const scaledItems = selectableItems.map(item => ({\n ...item,\n scaledWeight: this.toScaled(item.weight)\n }));\n \n const totalScaledWeight = scaledItems.reduce((sum, item) => sum + item.scaledWeight, 0);\n const rand = Math.floor(Math.random() * totalScaledWeight);\n let cumulative = 0;\n \n for (const item of scaledItems) {\n cumulative += item.scaledWeight;\n if (rand < cumulative) {\n return { name: item.name, weight: item.weight };\n }\n }\n \n // Fallback (should never happen)\n return selectableItems[0];\n }\n\n /**\n * Debug method to inspect scaled values\n */\n getDebugInfo(): {\n scale: number;\n rarityRatesScaled: Record<string, number>;\n rarityRatesFloat: Record<string, number>;\n } {\n const rarityRatesFloat: Record<string, number> = {};\n for (const [rarity, scaledRate] of Object.entries(this.rarityRatesScaled)) {\n rarityRatesFloat[rarity] = this.fromScaled(scaledRate);\n }\n \n return {\n scale: GachaEngine.SCALE,\n rarityRatesScaled: { ...this.rarityRatesScaled },\n rarityRatesFloat\n };\n }\n}"],"names":["GachaEngine","_ref","rarityRates","pools","rarityRatesScaled","dropRateCacheScaled","Map","this","scaleRarityRates","validateConfig","_proto","prototype","scaled","_i","_Object$entries","Object","entries","length","_Object$entries$_i","rarity","rate","Error","toScaled","probability","MAX_SAFE_SCALE","SCALE","Math","round","fromScaled","scaledInt","originalRates","configuredRarities","Set","keys","usedRarities","map","p","missingArray","Array","from","filter","r","has","join","totalRate","values","reduce","sum","totalScaled","abs","console","warn","_iterator","_step","_createForOfIteratorHelperLoose","done","pool","value","items","i","weight","_step2","_iterator2","item","name","hasPositiveWeight","some","getItemDropRate","get","_step3","_iterator3","find","set","totalPoolWeight","baseRarityRateScaled","itemWeightScaled","totalWeightScaled","numeratorScaled","rateScaled","getRarityProbability","getCumulativeProbabilityForItem","rolls","getItemDropRateScaled","failRate","cumulativeFailProbability","pow","min","max","getRollsForTargetProbability","targetProbability","Infinity","ceil","log","getRateUpItems","flatMap","rateUp","getAllItemDropRates","_this","dropRate","roll","count","_this2","results","_loop","selectRarity","selectItemFromPool","push","rand","floor","random","cumulativeScaled","_i2","_Object$entries2","_Object$entries2$_i","_this3","selectableItems","_step4","scaledItems","_extends","scaledWeight","totalScaledWeight","cumulative","_iterator4","getDebugInfo","rarityRatesFloat","_i3","_Object$entries3","_Object$entries3$_i","scale","Number","MAX_SAFE_INTEGER","_GachaEngine"],"mappings":"kgCAEaA,eAST,WAAA,SAAAA,EAAAC,GAAc,IAAAC,EAAWD,EAAXC,YAAaC,EAAKF,EAALE,MAJnBA,KAAAA,WACAC,EAAAA,KAAAA,8BACAC,oBAAsB,IAAIC,IAG9BC,KAAKJ,MAAQA,EACbI,KAAKH,kBAAoBG,KAAKC,iBAAiBN,GAC/CK,KAAKE,eAAeP,EACxB,CAAC,IAAAQ,EAAAV,EAAAW,UAuPA,OAvPAD,EAKOF,iBAAA,SAAiBN,GAErB,IADA,IAAMU,EAAiC,CAAE,EACzCC,IAAAC,EAA6BC,OAAOC,QAAQd,GAAYW,EAAAC,EAAAG,OAAAJ,IAAE,CAArD,IAAAK,EAAAJ,EAAAD,GAAOM,EAAMD,EAAA,GAAEE,EAAIF,EACpB,GAAA,GAAIE,EAAO,GAAKA,EAAO,EACnB,MAAU,IAAAC,MAAK,oBAAqBF,EAAM,kCAAkCC,GAEhFR,EAAOO,GA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CvD,CAAAA,EAAAA,GACHwD,aAAcL,EAAK5F,SAASyC,EAAKH,SAAO,GAGtC4D,EAAoBH,EAAYvE,OAAO,SAACC,EAAKgB,GAAI,OAAKhB,EAAMgB,EAAKwD,YAAY,EAAE,GAC/EZ,EAAOjF,KAAKkF,MAAMlF,KAAKmF,SAAWW,GACpCC,EAAa,EAEjBC,EAAApE,EAAmB+D,KAAWD,EAAAM,KAAAnE,MAAE,KAArBQ,EAAIqD,EAAA3D,MAEX,GAAIkD,GADJc,GAAc1D,EAAKwD,cAEf,MAAO,CAAEvD,KAAMD,EAAKC,KAAMJ,OAAQG,EAAKH,OAE/C,CAGA,OAAOuD,EAAgB,EAC3B,EAACzG,EAKDiH,aAAA,WAMI,IADA,IAAMC,EAA2C,CAAE,EACnDC,EAAA,EAAAC,EAAmC/G,OAAOC,QAAQT,KAAKH,mBAAkByH,EAAAC,EAAA7G,OAAA4G,IAAE,CAAtE,IAAAE,EAAAD,EAAAD,GACDD,EADcG,EAAA,IACaxH,KAAKqB,WADNmG,EAC1BH,GACJ,CAEA,MAAO,CACHI,MAAOhI,EAAYyB,MACnBrB,kBAAiBkH,KAAO/G,KAAKH,mBAC7BwH,iBAAAA,EAER,EAAC5H,CAAA,CA3PD,KATSA,EAAAA,EAEeyB,MAAQ,IAFvBzB,EAGewB,eAAiBE,KAAKkF,MAAMqB,OAAOC,iBAAmBC,EAAK1G"}
|
|
1
|
+
{"version":3,"file":"index.module.js","sources":["../src/gacha-engine.ts"],"sourcesContent":["import {\n RarityInput,\n GachaEngineConfig,\n WeightedGachaEngineConfig,\n FlatRateGachaEngineConfig,\n} from './types';\n\nexport class GachaEngine {\n private static readonly SCALE = 1_000_000;\n private static readonly MAX_SAFE_SCALE = Math.floor(Number.MAX_SAFE_INTEGER / GachaEngine.SCALE);\n\n private mode: 'weighted' | 'flatRate';\n private pools: RarityInput[] = [];\n private rarityRatesScaled: Record<string, number> = {};\n private flatRateMap: Map<string, number> = new Map();\n private dropRateCacheScaled = new Map<string, number>();\n\n constructor(config: GachaEngineConfig) {\n this.mode = config.mode;\n\n if (config.mode === 'weighted') {\n const weightedConfig = config as WeightedGachaEngineConfig;\n this.pools = weightedConfig.pools;\n this.rarityRatesScaled = this.scaleRarityRates(weightedConfig.rarityRates);\n this.validateConfig(weightedConfig.rarityRates);\n } else if (config.mode === 'flatRate') {\n const flatConfig = config as FlatRateGachaEngineConfig;\n for (const pool of flatConfig.pools) {\n for (const item of pool.items) {\n if (item.weight < 0) {\n throw new Error(`FlatRate item \"${item.name}\" must have non-negative weight`);\n }\n this.flatRateMap.set(item.name, item.weight); // Here, interpreted as direct probability\n }\n }\n const total = Array.from(this.flatRateMap.values()).reduce((sum, v) => sum + v, 0);\n if (Math.abs(total - 1.0) > 1e-6) {\n throw new Error(`FlatRate item rates must sum to 1.0, but got ${total}`);\n }\n } else {\n throw new Error(`Unknown gacha mode: ${this.mode}`);\n }\n }\n\n private scaleRarityRates(rarityRates: Record<string, number>): Record<string, number> {\n const scaled: Record<string, number> = {};\n for (const [rarity, rate] of Object.entries(rarityRates)) {\n if (rate < 0 || rate > 1) {\n throw new Error(`Rarity rate for \"${rarity}\" must be between 0 and 1, got ${rate}`);\n }\n scaled[rarity] = this.toScaled(rate);\n }\n return scaled;\n }\n\n private toScaled(probability: number): number {\n if (probability > GachaEngine.MAX_SAFE_SCALE / GachaEngine.SCALE) {\n throw new Error(`Probability ${probability} too large for safe integer arithmetic`);\n }\n return Math.round(probability * GachaEngine.SCALE);\n }\n\n private fromScaled(scaledInt: number): number {\n return scaledInt / GachaEngine.SCALE;\n }\n\n private validateConfig(originalRates: Record<string, number>): void {\n const configuredRarities = new Set(Object.keys(this.rarityRatesScaled));\n const usedRarities = new Set(this.pools.map(p => p.rarity));\n const missing = Array.from(usedRarities).filter(r => !configuredRarities.has(r));\n\n if (missing.length > 0) {\n throw new Error(`Missing rarity rates for: ${missing.join(', ')}`);\n }\n\n const totalRate = Object.values(originalRates).reduce((sum, rate) => sum + rate, 0);\n if (Math.abs(totalRate - 1.0) > 1e-10) {\n throw new Error(`Rarity rates must sum to 1.0, got ${totalRate}`);\n }\n\n for (const pool of this.pools) {\n if (pool.items.length === 0) {\n throw new Error(`Rarity \"${pool.rarity}\" has no items`);\n }\n\n const totalWeight = pool.items.reduce((sum, i) => sum + i.weight, 0);\n if (totalWeight <= 0) {\n throw new Error(`Rarity \"${pool.rarity}\" has zero total weight`);\n }\n\n for (const item of pool.items) {\n if (item.weight < 0) {\n throw new Error(`Item \"${item.name}\" weight must be non-negative, got ${item.weight}`);\n }\n }\n\n if (!pool.items.some(i => i.weight > 0)) {\n throw new Error(`Rarity \"${pool.rarity}\" must have at least one item with positive weight`);\n }\n }\n }\n\n getItemDropRate(name: string): number {\n if (this.mode === 'flatRate') {\n return this.flatRateMap.get(name) || 0;\n }\n\n if (this.dropRateCacheScaled.has(name)) {\n return this.fromScaled(this.dropRateCacheScaled.get(name)!);\n }\n\n for (const pool of this.pools) {\n const item = pool.items.find(i => i.name === name);\n if (item) {\n if (item.weight === 0) {\n this.dropRateCacheScaled.set(name, 0);\n return 0;\n }\n\n const totalPoolWeight = pool.items.reduce((sum, i) => sum + i.weight, 0);\n const baseRarityRateScaled = this.rarityRatesScaled[pool.rarity];\n const itemWeightScaled = this.toScaled(item.weight);\n const totalWeightScaled = this.toScaled(totalPoolWeight);\n const numeratorScaled = Math.round((itemWeightScaled * baseRarityRateScaled) / GachaEngine.SCALE);\n const rateScaled = Math.round((numeratorScaled * GachaEngine.SCALE) / totalWeightScaled);\n\n this.dropRateCacheScaled.set(name, rateScaled);\n return this.fromScaled(rateScaled);\n }\n }\n\n throw new Error(`Item \"${name}\" not found`);\n }\n\n getCumulativeProbabilityForItem(name: string, rolls: number): number {\n const rate = this.getItemDropRate(name);\n if (rate === 0) return 0;\n if (rate >= 1) return 1;\n\n const cumulativeFailProbability = Math.pow(1 - rate, rolls);\n return 1 - cumulativeFailProbability;\n }\n\n getRollsForTargetProbability(name: string, targetProbability: number): number {\n if (targetProbability <= 0) return 0;\n if (targetProbability >= 1) return 1;\n\n const rate = this.getItemDropRate(name);\n if (rate <= 0) return Infinity;\n return Math.ceil(Math.log(1 - targetProbability) / Math.log(1 - rate));\n }\n\n getRateUpItems(): string[] {\n return this.pools.flatMap(p => p.items.filter(i => i.rateUp).map(i => i.name));\n }\n\n getAllItemDropRates(): { name: string; dropRate: number; rarity: string }[] {\n if (this.mode === 'flatRate') {\n return Array.from(this.flatRateMap.entries()).map(([name, dropRate]) => ({\n name,\n dropRate,\n rarity: 'flatRate',\n }));\n }\n\n return this.pools.flatMap(p =>\n p.items.map(i => ({\n name: i.name,\n dropRate: this.getItemDropRate(i.name),\n rarity: p.rarity,\n }))\n );\n }\n\n roll(count: number = 1): string[] {\n const results: string[] = [];\n for (let i = 0; i < count; i++) {\n if (this.mode === 'flatRate') {\n const rand = Math.random();\n let cumulative = 0;\n for (const [name, rate] of this.flatRateMap.entries()) {\n cumulative += rate;\n if (rand < cumulative) {\n results.push(name);\n break;\n }\n }\n } else {\n const rarity = this.selectRarity();\n const pool = this.pools.find(p => p.rarity === rarity)!;\n const item = this.selectItemFromPool(pool);\n results.push(item.name);\n }\n }\n return results;\n }\n\n private selectRarity(): string {\n const rand = Math.floor(Math.random() * GachaEngine.SCALE);\n let cumulative = 0;\n\n for (const [rarity, scaledRate] of Object.entries(this.rarityRatesScaled)) {\n cumulative += scaledRate;\n if (rand < cumulative) return rarity;\n }\n\n return Object.keys(this.rarityRatesScaled)[0];\n }\n\n private selectItemFromPool(pool: RarityInput): { name: string; weight: number } {\n const items = pool.items.filter(i => i.weight > 0);\n const scaledItems = items.map(i => ({\n ...i,\n scaledWeight: this.toScaled(i.weight),\n }));\n\n const totalScaledWeight = scaledItems.reduce((sum, i) => sum + i.scaledWeight, 0);\n const rand = Math.floor(Math.random() * totalScaledWeight);\n let cumulative = 0;\n\n for (const item of scaledItems) {\n cumulative += item.scaledWeight;\n if (rand < cumulative) {\n return { name: item.name, weight: item.weight };\n }\n }\n\n return items[0]; // Fallback\n }\n\n getDebugInfo(): {\n scale: number;\n rarityRatesScaled: Record<string, number>;\n rarityRatesFloat: Record<string, number>;\n } {\n const rarityRatesFloat: Record<string, number> = {};\n for (const [rarity, scaledRate] of Object.entries(this.rarityRatesScaled)) {\n rarityRatesFloat[rarity] = this.fromScaled(scaledRate);\n }\n\n return {\n scale: GachaEngine.SCALE,\n rarityRatesScaled: { ...this.rarityRatesScaled },\n rarityRatesFloat,\n };\n }\n}\n"],"names":["GachaEngine","config","this","mode","pools","rarityRatesScaled","flatRateMap","Map","dropRateCacheScaled","weightedConfig","scaleRarityRates","rarityRates","validateConfig","Error","_step","_iterator","_createForOfIteratorHelperLoose","done","_step2","_iterator2","value","items","item","weight","name","set","total","Array","from","values","reduce","sum","v","Math","abs","_proto","prototype","scaled","_i","_Object$entries","Object","entries","length","_Object$entries$_i","rarity","rate","toScaled","probability","MAX_SAFE_SCALE","SCALE","round","fromScaled","scaledInt","originalRates","configuredRarities","Set","keys","usedRarities","map","p","missing","filter","r","has","join","totalRate","_iterator3","_step3","pool","i","_step4","_iterator4","some","getItemDropRate","get","_iterator5","_step5","find","totalPoolWeight","baseRarityRateScaled","itemWeightScaled","totalWeightScaled","numeratorScaled","rateScaled","getCumulativeProbabilityForItem","rolls","pow","getRollsForTargetProbability","targetProbability","Infinity","ceil","log","getRateUpItems","flatMap","rateUp","getAllItemDropRates","_this","_ref","dropRate","roll","count","_this2","results","_loop","_step6","rand","random","cumulative","_iterator6","_step6$value","push","selectRarity","selectItemFromPool","floor","_i2","_Object$entries2","_Object$entries2$_i","scaledRate","_step7","_this3","scaledItems","_extends","scaledWeight","totalScaledWeight","_iterator7","getDebugInfo","rarityRatesFloat","_i3","_Object$entries3","_Object$entries3$_i","scale","Number","MAX_SAFE_INTEGER"],"mappings":"kgCAOaA,eAAW,WAUtB,SAAAA,EAAYC,GAGV,GAHmCC,KAN7BC,UAAI,EAAAD,KACJE,MAAuB,GACvBC,KAAAA,kBAA4C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M,KAAA3G,MAAE,CAArB,IAAAK,EAAIgG,EAAAlG,MAEb,GAAIqF,GADJE,GAAcrF,EAAKoG,cAEjB,MAAO,CAAElG,KAAMF,EAAKE,KAAMD,OAAQD,EAAKC,OAE3C,CAEA,OAAOF,EAAM,EACf,EAACc,EAED0F,aAAA,WAME,IADA,IAAMC,EAA2C,CAAE,EACnDC,EAAA,EAAAC,EAAmCxF,OAAOC,QAAQvC,KAAKG,mBAAkB0H,EAAAC,EAAAtF,OAAAqF,IAAE,CAAtE,IAAAE,EAAAD,EAAAD,GACHD,EADgBG,EAAA,IACW/H,KAAKiD,WADJ8E,EAAA,GAE9B,CAEA,MAAO,CACLC,MAAOlI,EAAYiD,MACnB5C,kBAAiBoH,EAAO,CAAA,EAAAvH,KAAKG,mBAC7ByH,iBAAAA,EAEJ,EAAC9H,CAAA,CA9OqB,KAAXA,EAAAA,EACaiD,MAAQ,IADrBjD,EAEagD,eAAiBf,KAAKgF,MAAMkB,OAAOC,iBAAmBpI,EAAYiD"}
|
package/dist/index.umd.js
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
!function(t,e){"object"==typeof exports&&"undefined"!=typeof module?e(exports):"function"==typeof define&&define.amd?define(["exports"],e):e((t||self).AllemandiGachaEngine={})}(this,function(t){function e(t,e){(null==e||e>t.length)&&(e=t.length);for(var r=0,a=Array(e);r<e;r++)a[r]=t[r];return a}function r(t,r){var a="undefined"!=typeof Symbol&&t[Symbol.iterator]||t["@@iterator"];if(a)return(a=a.call(t)).next.bind(a);if(Array.isArray(t)||(a=function(t,r){if(t){if("string"==typeof t)return e(t,r);var a={}.toString.call(t).slice(8,-1);return"Object"===a&&t.constructor&&(a=t.constructor.name),"Map"===a||"Set"===a?Array.from(t):"Arguments"===a||/^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(a)?e(t,r):void 0}}(t))||r&&t&&"number"==typeof t.length){a&&(t=a);var n=0;return function(){return n>=t.length?{done:!0}:{done:!1,value:t[n++]}}}throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}function a(){return a=Object.assign?Object.assign.bind():function(t){for(var e=1;e<arguments.length;e++){var r=arguments[e];for(var a in r)({}).hasOwnProperty.call(r,a)&&(t[a]=r[a])}return t},a.apply(null,arguments)}var n,i=/*#__PURE__*/function(){function t(t){
|
|
1
|
+
!function(t,e){"object"==typeof exports&&"undefined"!=typeof module?e(exports):"function"==typeof define&&define.amd?define(["exports"],e):e((t||self).AllemandiGachaEngine={})}(this,function(t){function e(t,e){(null==e||e>t.length)&&(e=t.length);for(var r=0,a=Array(e);r<e;r++)a[r]=t[r];return a}function r(t,r){var a="undefined"!=typeof Symbol&&t[Symbol.iterator]||t["@@iterator"];if(a)return(a=a.call(t)).next.bind(a);if(Array.isArray(t)||(a=function(t,r){if(t){if("string"==typeof t)return e(t,r);var a={}.toString.call(t).slice(8,-1);return"Object"===a&&t.constructor&&(a=t.constructor.name),"Map"===a||"Set"===a?Array.from(t):"Arguments"===a||/^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(a)?e(t,r):void 0}}(t))||r&&t&&"number"==typeof t.length){a&&(t=a);var n=0;return function(){return n>=t.length?{done:!0}:{done:!1,value:t[n++]}}}throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}function a(){return a=Object.assign?Object.assign.bind():function(t){for(var e=1;e<arguments.length;e++){var r=arguments[e];for(var a in r)({}).hasOwnProperty.call(r,a)&&(t[a]=r[a])}return t},a.apply(null,arguments)}var n,i=/*#__PURE__*/function(){function t(t){if(this.mode=void 0,this.pools=[],this.rarityRatesScaled={},this.flatRateMap=new Map,this.dropRateCacheScaled=new Map,this.mode=t.mode,"weighted"===t.mode){var e=t;this.pools=e.pools,this.rarityRatesScaled=this.scaleRarityRates(e.rarityRates),this.validateConfig(e.rarityRates)}else{if("flatRate"!==t.mode)throw new Error("Unknown gacha mode: "+this.mode);for(var a,n=r(t.pools);!(a=n()).done;)for(var i,o=r(a.value.items);!(i=o()).done;){var s=i.value;if(s.weight<0)throw new Error('FlatRate item "'+s.name+'" must have non-negative weight');this.flatRateMap.set(s.name,s.weight)}var f=Array.from(this.flatRateMap.values()).reduce(function(t,e){return t+e},0);if(Math.abs(f-1)>1e-6)throw new Error("FlatRate item rates must sum to 1.0, but got "+f)}}var e=t.prototype;return e.scaleRarityRates=function(t){for(var e={},r=0,a=Object.entries(t);r<a.length;r++){var n=a[r],i=n[0],o=n[1];if(o<0||o>1)throw new Error('Rarity rate for "'+i+'" must be between 0 and 1, got '+o);e[i]=this.toScaled(o)}return e},e.toScaled=function(e){if(e>t.MAX_SAFE_SCALE/t.SCALE)throw new Error("Probability "+e+" too large for safe integer arithmetic");return Math.round(e*t.SCALE)},e.fromScaled=function(e){return e/t.SCALE},e.validateConfig=function(t){var e=new Set(Object.keys(this.rarityRatesScaled)),a=new Set(this.pools.map(function(t){return t.rarity})),n=Array.from(a).filter(function(t){return!e.has(t)});if(n.length>0)throw new Error("Missing rarity rates for: "+n.join(", "));var i=Object.values(t).reduce(function(t,e){return t+e},0);if(Math.abs(i-1)>1e-10)throw new Error("Rarity rates must sum to 1.0, got "+i);for(var o,s=r(this.pools);!(o=s()).done;){var f=o.value;if(0===f.items.length)throw new Error('Rarity "'+f.rarity+'" has no items');if(f.items.reduce(function(t,e){return t+e.weight},0)<=0)throw new Error('Rarity "'+f.rarity+'" has zero total weight');for(var l,u=r(f.items);!(l=u()).done;){var h=l.value;if(h.weight<0)throw new Error('Item "'+h.name+'" weight must be non-negative, got '+h.weight)}if(!f.items.some(function(t){return t.weight>0}))throw new Error('Rarity "'+f.rarity+'" must have at least one item with positive weight')}},e.getItemDropRate=function(e){if("flatRate"===this.mode)return this.flatRateMap.get(e)||0;if(this.dropRateCacheScaled.has(e))return this.fromScaled(this.dropRateCacheScaled.get(e));for(var a,n=r(this.pools);!(a=n()).done;){var i=a.value,o=i.items.find(function(t){return t.name===e});if(o){if(0===o.weight)return this.dropRateCacheScaled.set(e,0),0;var s=i.items.reduce(function(t,e){return t+e.weight},0),f=this.rarityRatesScaled[i.rarity],l=this.toScaled(o.weight),u=this.toScaled(s),h=Math.round(l*f/t.SCALE),c=Math.round(h*t.SCALE/u);return this.dropRateCacheScaled.set(e,c),this.fromScaled(c)}}throw new Error('Item "'+e+'" not found')},e.getCumulativeProbabilityForItem=function(t,e){var r=this.getItemDropRate(t);return 0===r?0:r>=1?1:1-Math.pow(1-r,e)},e.getRollsForTargetProbability=function(t,e){if(e<=0)return 0;if(e>=1)return 1;var r=this.getItemDropRate(t);return r<=0?Infinity:Math.ceil(Math.log(1-e)/Math.log(1-r))},e.getRateUpItems=function(){return this.pools.flatMap(function(t){return t.items.filter(function(t){return t.rateUp}).map(function(t){return t.name})})},e.getAllItemDropRates=function(){var t=this;return"flatRate"===this.mode?Array.from(this.flatRateMap.entries()).map(function(t){return{name:t[0],dropRate:t[1],rarity:"flatRate"}}):this.pools.flatMap(function(e){return e.items.map(function(r){return{name:r.name,dropRate:t.getItemDropRate(r.name),rarity:e.rarity}})})},e.roll=function(t){var e=this;void 0===t&&(t=1);for(var a=[],n=function(){if("flatRate"===e.mode)for(var t,n=Math.random(),i=0,o=r(e.flatRateMap.entries());!(t=o()).done;){var s=t.value;if(n<(i+=s[1])){a.push(s[0]);break}}else{var f=e.selectRarity(),l=e.pools.find(function(t){return t.rarity===f}),u=e.selectItemFromPool(l);a.push(u.name)}},i=0;i<t;i++)n();return a},e.selectRarity=function(){for(var e=Math.floor(Math.random()*t.SCALE),r=0,a=0,n=Object.entries(this.rarityRatesScaled);a<n.length;a++){var i=n[a];if(e<(r+=i[1]))return i[0]}return Object.keys(this.rarityRatesScaled)[0]},e.selectItemFromPool=function(t){for(var e,n=this,i=t.items.filter(function(t){return t.weight>0}),o=i.map(function(t){return a({},t,{scaledWeight:n.toScaled(t.weight)})}),s=o.reduce(function(t,e){return t+e.scaledWeight},0),f=Math.floor(Math.random()*s),l=0,u=r(o);!(e=u()).done;){var h=e.value;if(f<(l+=h.scaledWeight))return{name:h.name,weight:h.weight}}return i[0]},e.getDebugInfo=function(){for(var e={},r=0,n=Object.entries(this.rarityRatesScaled);r<n.length;r++){var i=n[r];e[i[0]]=this.fromScaled(i[1])}return{scale:t.SCALE,rarityRatesScaled:a({},this.rarityRatesScaled),rarityRatesFloat:e}},t}();n=i,i.SCALE=1e6,i.MAX_SAFE_SCALE=Math.floor(Number.MAX_SAFE_INTEGER/n.SCALE),t.GachaEngine=i});
|
|
2
2
|
//# sourceMappingURL=index.umd.js.map
|
package/dist/index.umd.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.umd.js","sources":["../src/gacha-engine.ts"],"sourcesContent":["import { RarityInput, GachaEngineConfig } from './types';\n\nexport class GachaEngine {\n // Scale factor for fixed-point arithmetic (1,000,000 = 6 decimal places)\n private static readonly SCALE = 1000000;\n private static readonly MAX_SAFE_SCALE = Math.floor(Number.MAX_SAFE_INTEGER / this.SCALE);\n\n private pools: RarityInput[];\n private rarityRatesScaled: Record<string, number>; // Scaled to integers\n private dropRateCacheScaled = new Map<string, number>(); // Cache scaled rates\n\n constructor({ rarityRates, pools }: GachaEngineConfig) {\n this.pools = pools;\n this.rarityRatesScaled = this.scaleRarityRates(rarityRates);\n this.validateConfig(rarityRates);\n }\n\n /**\n * Convert floating point rates to scaled integers\n */\n private scaleRarityRates(rarityRates: Record<string, number>): Record<string, number> {\n const scaled: Record<string, number> = {};\n for (const [rarity, rate] of Object.entries(rarityRates)) {\n if (rate < 0 || rate > 1) {\n throw new Error(`Rarity rate for \"${rarity}\" must be between 0 and 1, got ${rate}`);\n }\n scaled[rarity] = this.toScaled(rate);\n }\n return scaled;\n }\n\n /**\n * Convert probability to scaled integer\n */\n private toScaled(probability: number): number {\n if (probability > GachaEngine.MAX_SAFE_SCALE / GachaEngine.SCALE) {\n throw new Error(`Probability ${probability} too large for safe integer arithmetic`);\n }\n return Math.round(probability * GachaEngine.SCALE);\n }\n\n /**\n * Convert scaled integer back to probability\n */\n private fromScaled(scaledInt: number): number {\n return scaledInt / GachaEngine.SCALE;\n }\n\n private validateConfig(originalRates: Record<string, number>): void {\n const configuredRarities = new Set(Object.keys(this.rarityRatesScaled));\n const usedRarities = new Set(this.pools.map(p => p.rarity));\n const missingArray = Array.from(usedRarities).filter(r => !configuredRarities.has(r));\n \n if (missingArray.length > 0) {\n throw new Error(`Missing rarity rates for: ${missingArray.join(', ')}`);\n }\n\n // Validate that rates sum to exactly 1.0 (within floating point precision)\n const totalRate = Object.values(originalRates).reduce((sum, rate) => sum + rate, 0);\n const totalScaled = Object.values(this.rarityRatesScaled).reduce((sum, rate) => sum + rate, 0);\n \n if (Math.abs(totalRate - 1.0) > 1e-10) {\n throw new Error(`Rarity rates must sum to 1.0, got ${totalRate}`);\n }\n\n // Ensure scaled rates sum to SCALE (accounting for rounding)\n if (Math.abs(totalScaled - GachaEngine.SCALE) > Object.keys(this.rarityRatesScaled).length) {\n console.warn(`Scaled rates sum to ${totalScaled}, expected ${GachaEngine.SCALE}. This is likely due to rounding.`);\n }\n\n for (const pool of this.pools) {\n if (pool.items.length === 0) {\n throw new Error(`Rarity \"${pool.rarity}\" has no items`);\n }\n \n const totalWeight = pool.items.reduce((sum, i) => sum + i.weight, 0);\n if (totalWeight <= 0) {\n throw new Error(`Rarity \"${pool.rarity}\" has zero total weight`);\n }\n\n // Validate that all weights are non-negative\n for (const item of pool.items) {\n if (item.weight < 0) {\n throw new Error(`Item \"${item.name}\" weight must be non-negative, got ${item.weight}`);\n }\n }\n\n // Ensure at least one item has positive weight\n const hasPositiveWeight = pool.items.some(item => item.weight > 0);\n if (!hasPositiveWeight) {\n throw new Error(`Rarity \"${pool.rarity}\" must have at least one item with positive weight`);\n }\n }\n }\n\n getItemDropRate(name: string): number {\n if (this.dropRateCacheScaled.has(name)) {\n return this.fromScaled(this.dropRateCacheScaled.get(name)!);\n }\n\n for (const pool of this.pools) {\n const item = pool.items.find(i => i.name === name);\n if (item) {\n // Handle zero weight items (never drop)\n if (item.weight === 0) {\n this.dropRateCacheScaled.set(name, 0);\n return 0;\n }\n\n const totalPoolWeight = pool.items.reduce((sum, i) => sum + i.weight, 0);\n const baseRarityRateScaled = this.rarityRatesScaled[pool.rarity];\n \n // Convert weights to scaled integers for perfect precision\n const itemWeightScaled = this.toScaled(item.weight);\n const totalWeightScaled = this.toScaled(totalPoolWeight);\n \n // Scaled arithmetic: (itemWeight * baseRate) / totalWeight\n const numeratorScaled = Math.round((itemWeightScaled * baseRarityRateScaled) / GachaEngine.SCALE);\n const rateScaled = Math.round((numeratorScaled * GachaEngine.SCALE) / totalWeightScaled);\n \n this.dropRateCacheScaled.set(name, rateScaled);\n return this.fromScaled(rateScaled);\n }\n }\n throw new Error(`Item \"${name}\" not found`);\n }\n\n getRarityProbability(rarity: string): number {\n if (!this.rarityRatesScaled[rarity]) {\n throw new Error(`Rarity \"${rarity}\" not found`);\n }\n return this.fromScaled(this.rarityRatesScaled[rarity]);\n }\n\n getCumulativeProbabilityForItem(name: string, rolls: number): number {\n const rateScaled = this.getItemDropRateScaled(name);\n \n if (rateScaled === 0) return 0;\n if (rateScaled >= GachaEngine.SCALE) return 1;\n \n // Calculate (1 - rate)^rolls using scaled arithmetic\n const failRateScaled = GachaEngine.SCALE - rateScaled;\n const failRate = this.fromScaled(failRateScaled);\n \n // For large rolls, we need to be careful with precision\n const cumulativeFailProbability = Math.pow(failRate, rolls);\n const cumulativeProbability = 1 - cumulativeFailProbability;\n \n return Math.min(1, Math.max(0, cumulativeProbability));\n }\n\n getRollsForTargetProbability(name: string, targetProbability: number): number {\n if (targetProbability <= 0) return 0;\n if (targetProbability >= 1) return 1;\n \n const rate = this.getItemDropRate(name);\n if (rate <= 0) return Infinity;\n if (rate >= 1) return 1;\n \n return Math.ceil(Math.log(1 - targetProbability) / Math.log(1 - rate));\n }\n\n getRateUpItems(): string[] {\n return this.pools.flatMap(p =>\n p.items.filter(i => i.rateUp).map(i => i.name)\n );\n }\n\n getAllItemDropRates(): { name: string; dropRate: number; rarity: string }[] {\n return this.pools.flatMap(p =>\n p.items.map(i => ({\n name: i.name,\n dropRate: this.getItemDropRate(i.name),\n rarity: p.rarity\n }))\n );\n }\n\n /**\n * Get scaled drop rate for internal calculations\n */\n private getItemDropRateScaled(name: string): number {\n if (this.dropRateCacheScaled.has(name)) {\n return this.dropRateCacheScaled.get(name)!;\n }\n \n // Trigger calculation and caching\n this.getItemDropRate(name);\n return this.dropRateCacheScaled.get(name)!;\n }\n\n roll(count: number = 1): string[] {\n const results: string[] = [];\n for (let i = 0; i < count; i++) {\n const rarity = this.selectRarity();\n const pool = this.pools.find(p => p.rarity === rarity)!;\n const item = this.selectItemFromPool(pool);\n results.push(item.name);\n }\n return results;\n }\n\n private selectRarity(): string {\n const rand = Math.floor(Math.random() * GachaEngine.SCALE);\n let cumulativeScaled = 0;\n \n for (const [rarity, rateScaled] of Object.entries(this.rarityRatesScaled)) {\n cumulativeScaled += rateScaled;\n if (rand < cumulativeScaled) return rarity;\n }\n \n // Fallback (should never happen with proper validation)\n return Object.keys(this.rarityRatesScaled)[0];\n }\n\n private selectItemFromPool(pool: RarityInput): { name: string; weight: number } {\n // Filter out zero-weight items (they can never be selected)\n const selectableItems = pool.items.filter(item => item.weight > 0);\n \n if (selectableItems.length === 0) {\n throw new Error(`No selectable items in pool for rarity \"${pool.rarity}\"`);\n }\n\n // Convert all weights to scaled integers for perfect precision\n const scaledItems = selectableItems.map(item => ({\n ...item,\n scaledWeight: this.toScaled(item.weight)\n }));\n \n const totalScaledWeight = scaledItems.reduce((sum, item) => sum + item.scaledWeight, 0);\n const rand = Math.floor(Math.random() * totalScaledWeight);\n let cumulative = 0;\n \n for (const item of scaledItems) {\n cumulative += item.scaledWeight;\n if (rand < cumulative) {\n return { name: item.name, weight: item.weight };\n }\n }\n \n // Fallback (should never happen)\n return selectableItems[0];\n }\n\n /**\n * Debug method to inspect scaled values\n */\n getDebugInfo(): {\n scale: number;\n rarityRatesScaled: Record<string, number>;\n rarityRatesFloat: Record<string, number>;\n } {\n const rarityRatesFloat: Record<string, number> = {};\n for (const [rarity, scaledRate] of Object.entries(this.rarityRatesScaled)) {\n rarityRatesFloat[rarity] = this.fromScaled(scaledRate);\n }\n \n return {\n scale: GachaEngine.SCALE,\n rarityRatesScaled: { ...this.rarityRatesScaled },\n rarityRatesFloat\n };\n }\n}"],"names":["GachaEngine","_ref","rarityRates","pools","rarityRatesScaled","dropRateCacheScaled","Map","this","scaleRarityRates","validateConfig","_proto","prototype","scaled","_i","_Object$entries","Object","entries","length","_Object$entries$_i","rarity","rate","Error","toScaled","probability","MAX_SAFE_SCALE","SCALE","Math","round","fromScaled","scaledInt","originalRates","configuredRarities","Set","keys","usedRarities","map","p","missingArray","Array","from","filter","r","has","join","totalRate","values","reduce","sum","totalScaled","abs","console","warn","_iterator","_step","_createForOfIteratorHelperLoose","done","pool","value","items","i","weight","_step2","_iterator2","item","name","hasPositiveWeight","some","getItemDropRate","get","_step3","_iterator3","find","set","totalPoolWeight","baseRarityRateScaled","itemWeightScaled","totalWeightScaled","numeratorScaled","rateScaled","getRarityProbability","getCumulativeProbabilityForItem","rolls","getItemDropRateScaled","failRate","cumulativeFailProbability","pow","min","max","getRollsForTargetProbability","targetProbability","Infinity","ceil","log","getRateUpItems","flatMap","rateUp","getAllItemDropRates","_this","dropRate","roll","count","_this2","results","_loop","selectRarity","selectItemFromPool","push","rand","floor","random","cumulativeScaled","_i2","_Object$entries2","_Object$entries2$_i","_this3","selectableItems","_step4","scaledItems","_extends","scaledWeight","totalScaledWeight","cumulative","_iterator4","getDebugInfo","rarityRatesFloat","_i3","_Object$entries3","_Object$entries3$_i","scale","Number","MAX_SAFE_INTEGER","_GachaEngine"],"mappings":"gvCAEaA,eAST,WAAA,SAAAA,EAAAC,GAAc,IAAAC,EAAWD,EAAXC,YAAaC,EAAKF,EAALE,MAJnBA,KAAAA,WACAC,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AAA,CA3PD,KATSA,EAAAA,EAEeyB,MAAQ,IAFvBzB,EAGewB,eAAiBE,KAAKkF,MAAMqB,OAAOC,iBAAmBC,EAAK1G"}
|
|
1
|
+
{"version":3,"file":"index.umd.js","sources":["../src/gacha-engine.ts"],"sourcesContent":["import {\n RarityInput,\n GachaEngineConfig,\n WeightedGachaEngineConfig,\n FlatRateGachaEngineConfig,\n} from './types';\n\nexport class GachaEngine {\n private static readonly SCALE = 1_000_000;\n private static readonly MAX_SAFE_SCALE = Math.floor(Number.MAX_SAFE_INTEGER / GachaEngine.SCALE);\n\n private mode: 'weighted' | 'flatRate';\n private pools: RarityInput[] = [];\n private rarityRatesScaled: Record<string, number> = {};\n private flatRateMap: Map<string, number> = new Map();\n private dropRateCacheScaled = new Map<string, number>();\n\n constructor(config: GachaEngineConfig) {\n this.mode = config.mode;\n\n if (config.mode === 'weighted') {\n const weightedConfig = config as WeightedGachaEngineConfig;\n this.pools = weightedConfig.pools;\n this.rarityRatesScaled = this.scaleRarityRates(weightedConfig.rarityRates);\n this.validateConfig(weightedConfig.rarityRates);\n } else if (config.mode === 'flatRate') {\n const flatConfig = config as FlatRateGachaEngineConfig;\n for (const pool of flatConfig.pools) {\n for (const item of pool.items) {\n if (item.weight < 0) {\n throw new Error(`FlatRate item \"${item.name}\" must have non-negative weight`);\n }\n this.flatRateMap.set(item.name, item.weight); // Here, interpreted as direct probability\n }\n }\n const total = Array.from(this.flatRateMap.values()).reduce((sum, v) => sum + v, 0);\n if (Math.abs(total - 1.0) > 1e-6) {\n throw new Error(`FlatRate item rates must sum to 1.0, but got ${total}`);\n }\n } else {\n throw new Error(`Unknown gacha mode: ${this.mode}`);\n }\n }\n\n private scaleRarityRates(rarityRates: Record<string, number>): Record<string, number> {\n const scaled: Record<string, number> = {};\n for (const [rarity, rate] of Object.entries(rarityRates)) {\n if (rate < 0 || rate > 1) {\n throw new Error(`Rarity rate for \"${rarity}\" must be between 0 and 1, got ${rate}`);\n }\n scaled[rarity] = this.toScaled(rate);\n }\n return scaled;\n }\n\n private toScaled(probability: number): number {\n if (probability > GachaEngine.MAX_SAFE_SCALE / GachaEngine.SCALE) {\n throw new Error(`Probability ${probability} too large for safe integer arithmetic`);\n }\n return Math.round(probability * GachaEngine.SCALE);\n }\n\n private fromScaled(scaledInt: number): number {\n return scaledInt / GachaEngine.SCALE;\n }\n\n private validateConfig(originalRates: Record<string, number>): void {\n const configuredRarities = new Set(Object.keys(this.rarityRatesScaled));\n const usedRarities = new Set(this.pools.map(p => p.rarity));\n const missing = Array.from(usedRarities).filter(r => !configuredRarities.has(r));\n\n if (missing.length > 0) {\n throw new Error(`Missing rarity rates for: ${missing.join(', ')}`);\n }\n\n const totalRate = Object.values(originalRates).reduce((sum, rate) => sum + rate, 0);\n if (Math.abs(totalRate - 1.0) > 1e-10) {\n throw new Error(`Rarity rates must sum to 1.0, got ${totalRate}`);\n }\n\n for (const pool of this.pools) {\n if (pool.items.length === 0) {\n throw new Error(`Rarity \"${pool.rarity}\" has no items`);\n }\n\n const totalWeight = pool.items.reduce((sum, i) => sum + i.weight, 0);\n if (totalWeight <= 0) {\n throw new Error(`Rarity \"${pool.rarity}\" has zero total weight`);\n }\n\n for (const item of pool.items) {\n if (item.weight < 0) {\n throw new Error(`Item \"${item.name}\" weight must be non-negative, got ${item.weight}`);\n }\n }\n\n if (!pool.items.some(i => i.weight > 0)) {\n throw new Error(`Rarity \"${pool.rarity}\" must have at least one item with positive weight`);\n }\n }\n }\n\n getItemDropRate(name: string): number {\n if (this.mode === 'flatRate') {\n return this.flatRateMap.get(name) || 0;\n }\n\n if (this.dropRateCacheScaled.has(name)) {\n return this.fromScaled(this.dropRateCacheScaled.get(name)!);\n }\n\n for (const pool of this.pools) {\n const item = pool.items.find(i => i.name === name);\n if (item) {\n if (item.weight === 0) {\n this.dropRateCacheScaled.set(name, 0);\n return 0;\n }\n\n const totalPoolWeight = pool.items.reduce((sum, i) => sum + i.weight, 0);\n const baseRarityRateScaled = this.rarityRatesScaled[pool.rarity];\n const itemWeightScaled = this.toScaled(item.weight);\n const totalWeightScaled = this.toScaled(totalPoolWeight);\n const numeratorScaled = Math.round((itemWeightScaled * baseRarityRateScaled) / GachaEngine.SCALE);\n const rateScaled = Math.round((numeratorScaled * GachaEngine.SCALE) / totalWeightScaled);\n\n this.dropRateCacheScaled.set(name, rateScaled);\n return this.fromScaled(rateScaled);\n }\n }\n\n throw new Error(`Item \"${name}\" not found`);\n }\n\n getCumulativeProbabilityForItem(name: string, rolls: number): number {\n const rate = this.getItemDropRate(name);\n if (rate === 0) return 0;\n if (rate >= 1) return 1;\n\n const cumulativeFailProbability = Math.pow(1 - rate, rolls);\n return 1 - cumulativeFailProbability;\n }\n\n getRollsForTargetProbability(name: string, targetProbability: number): number {\n if (targetProbability <= 0) return 0;\n if (targetProbability >= 1) return 1;\n\n const rate = this.getItemDropRate(name);\n if (rate <= 0) return Infinity;\n return Math.ceil(Math.log(1 - targetProbability) / Math.log(1 - rate));\n }\n\n getRateUpItems(): string[] {\n return this.pools.flatMap(p => p.items.filter(i => i.rateUp).map(i => i.name));\n }\n\n getAllItemDropRates(): { name: string; dropRate: number; rarity: string }[] {\n if (this.mode === 'flatRate') {\n return Array.from(this.flatRateMap.entries()).map(([name, dropRate]) => ({\n name,\n dropRate,\n rarity: 'flatRate',\n }));\n }\n\n return this.pools.flatMap(p =>\n p.items.map(i => ({\n name: i.name,\n dropRate: this.getItemDropRate(i.name),\n rarity: p.rarity,\n }))\n );\n }\n\n roll(count: number = 1): string[] {\n const results: string[] = [];\n for (let i = 0; i < count; i++) {\n if (this.mode === 'flatRate') {\n const rand = Math.random();\n let cumulative = 0;\n for (const [name, rate] of this.flatRateMap.entries()) {\n cumulative += rate;\n if (rand < cumulative) {\n results.push(name);\n break;\n }\n }\n } else {\n const rarity = this.selectRarity();\n const pool = this.pools.find(p => p.rarity === rarity)!;\n const item = this.selectItemFromPool(pool);\n results.push(item.name);\n }\n }\n return results;\n }\n\n private selectRarity(): string {\n const rand = Math.floor(Math.random() * GachaEngine.SCALE);\n let cumulative = 0;\n\n for (const [rarity, scaledRate] of Object.entries(this.rarityRatesScaled)) {\n cumulative += scaledRate;\n if (rand < cumulative) return rarity;\n }\n\n return Object.keys(this.rarityRatesScaled)[0];\n }\n\n private selectItemFromPool(pool: RarityInput): { name: string; weight: number } {\n const items = pool.items.filter(i => i.weight > 0);\n const scaledItems = items.map(i => ({\n ...i,\n scaledWeight: this.toScaled(i.weight),\n }));\n\n const totalScaledWeight = scaledItems.reduce((sum, i) => sum + i.scaledWeight, 0);\n const rand = Math.floor(Math.random() * totalScaledWeight);\n let cumulative = 0;\n\n for (const item of scaledItems) {\n cumulative += item.scaledWeight;\n if (rand < cumulative) {\n return { name: item.name, weight: item.weight };\n }\n }\n\n return items[0]; // Fallback\n }\n\n getDebugInfo(): {\n scale: number;\n rarityRatesScaled: Record<string, number>;\n rarityRatesFloat: Record<string, number>;\n } {\n const rarityRatesFloat: Record<string, number> = {};\n for (const [rarity, scaledRate] of Object.entries(this.rarityRatesScaled)) {\n rarityRatesFloat[rarity] = this.fromScaled(scaledRate);\n }\n\n return {\n scale: GachaEngine.SCALE,\n rarityRatesScaled: { ...this.rarityRatesScaled },\n rarityRatesFloat,\n };\n }\n}\n"],"names":["GachaEngine","config","this","mode","pools","rarityRatesScaled","flatRateMap","Map","dropRateCacheScaled","weightedConfig","scaleRarityRates","rarityRates","validateConfig","Error","_step","_iterator","_createForOfIteratorHelperLoose","done","_step2","_iterator2","value","items","item","weight","name","set","total","Array","from","values","reduce","sum","v","Math","abs","_proto","prototype","scaled","_i","_Object$entries","Object","entries","length","_Object$entries$_i","rarity","rate","toScaled","probability","MAX_SAFE_SCALE","SCALE","round","fromScaled","scaledInt","originalRates","configuredRarities","Set","keys","usedRarities","map","p","missing","filter","r","has","join","totalRate","_iterator3","_step3","pool","i","_step4","_iterator4","some","getItemDropRate","get","_iterator5","_step5","find","totalPoolWeight","baseRarityRateScaled","itemWeightScaled","totalWeightScaled","numeratorScaled","rateScaled","getCumulativeProbabilityForItem","rolls","pow","getRollsForTargetProbability","targetProbability","Infinity","ceil","log","getRateUpItems","flatMap","rateUp","getAllItemDropRates","_this","_ref","dropRate","roll","count","_this2","results","_loop","_step6","rand","random","cumulative","_iterator6","_step6$value","push","selectRarity","selectItemFromPool","floor","_i2","_Object$entries2","_Object$entries2$_i","scaledRate","_step7","_this3","scaledItems","_extends","scaledWeight","totalScaledWeight","_iterator7","getDebugInfo","rarityRatesFloat","_i3","_Object$entries3","_Object$entries3$_i","scale","Number","MAX_SAFE_INTEGER"],"mappings":"gvCAOaA,eAAW,WAUtB,SAAAA,EAAYC,GAGV,GAHmCC,KAN7BC,UAAI,EAAAD,KACJE,MAAuB,GACvBC,KAAAA,kBAA4C,CAAA,EAAEH,KAC9CI,YAAmC,IAAIC,IACvCC,KAAAA,oBAAsB,IAAID,IAGhCL,KAAKC,KAAOF,EAAOE,KAEC,aAAhBF,EAAOE,KAAqB,CAC9B,IAAMM,EAAiBR,EACvBC,KAAKE,MAAQK,EAAeL,MAC5BF,KAAKG,kBAAoBH,KAAKQ,iBAAiBD,EAAeE,aAC9DT,KAAKU,eAAeH,EAAeE,YACrC,SAA2B,aAAhBV,EAAOE,KAehB,MAAM,IAAIU,6BAA6BX,KAAKC,MAb5C,IADA,IACmCW,EAAnCC,EAAAC,EADmBf,EACWG,SAAKU,EAAAC,KAAAE,MACjC,IADmC,IACNC,EAA7BC,EAAAH,EADaF,EAAAM,MACWC,SAAKH,EAAAC,KAAAF,MAAE,CAApB,IAAAK,EAAIJ,EAAAE,MACb,GAAIE,EAAKC,OAAS,EAChB,MAAU,IAAAV,MAAK,kBAAmBS,EAAKE,KAAI,mCAE7CtB,KAAKI,YAAYmB,IAAIH,EAAKE,KAAMF,EAAKC,OACvC,CAEF,IAAMG,EAAQC,MAAMC,KAAK1B,KAAKI,YAAYuB,UAAUC,OAAO,SAACC,EAAKC,GAAC,OAAKD,EAAMC,CAAC,EAAE,GAChF,GAAI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
}
|
|
5
1
|
export interface GachaItem {
|
|
6
2
|
name: string;
|
|
7
|
-
/**
|
|
8
|
-
* Weight determines relative probability within the rarity tier.
|
|
9
|
-
* Can be fractional (e.g., 0.5, 1.2) - will be converted to scaled integers internally.
|
|
10
|
-
* Use weight: 0 for items that should never drop (useful for placeholders or disabled items).
|
|
11
|
-
* Higher weights = higher probability within the rarity tier.
|
|
12
|
-
*/
|
|
13
3
|
weight: number;
|
|
14
4
|
rateUp?: boolean;
|
|
15
5
|
}
|
|
16
|
-
export interface
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
6
|
+
export interface RarityInput {
|
|
7
|
+
rarity: string;
|
|
8
|
+
items: GachaItem[];
|
|
9
|
+
}
|
|
10
|
+
/** Weighted mode requires explicit rarityRates */
|
|
11
|
+
export interface WeightedGachaEngineConfig {
|
|
12
|
+
mode: 'weighted';
|
|
22
13
|
rarityRates: Record<string, number>;
|
|
23
|
-
/** Array of rarity pools containing items */
|
|
24
14
|
pools: RarityInput[];
|
|
25
15
|
}
|
|
16
|
+
/** Flat rate mode does NOT use rarityRates */
|
|
17
|
+
export interface FlatRateGachaEngineConfig {
|
|
18
|
+
mode: 'flatRate';
|
|
19
|
+
pools: RarityInput[];
|
|
20
|
+
}
|
|
21
|
+
export type GachaEngineConfig = WeightedGachaEngineConfig | FlatRateGachaEngineConfig;
|
package/package.json
CHANGED