cerceis-lib
Version:
Contains list of quality of life functions that is written in TypeScript and es6
29 lines • 736 B
JavaScript
// src/gacha/index.ts
var Gacha = class {
constructor() {
this.entries = [];
this.totalWeight = 0;
}
/**
* Add an item with a given weight.
* Higher weight = higher probability of being drawn.
* @param item Item to add.
* @param weight Relative weight.
*/
addEntries(item, weight) {
this.totalWeight += weight;
this.entries.push({ item, accumulatedWeight: this.totalWeight });
}
/**
* Draw a random item, weighted by the entries added.
* Returns undefined if no entries have been added.
*/
getRandom() {
const r = Math.random() * this.totalWeight;
return this.entries.find((e) => e.accumulatedWeight >= r)?.item;
}
};
export {
Gacha
};
//# sourceMappingURL=index.mjs.map