cerceis-lib
Version:
Contains list of quality of life functions that is written in TypeScript and es6
54 lines (53 loc) • 1.74 kB
JavaScript
;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
for (var name in all)
__defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
if (from && typeof from === "object" || typeof from === "function") {
for (let key of __getOwnPropNames(from))
if (!__hasOwnProp.call(to, key) && key !== except)
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
}
return to;
};
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
// src/gacha/index.ts
var gacha_exports = {};
__export(gacha_exports, {
Gacha: () => Gacha
});
module.exports = __toCommonJS(gacha_exports);
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;
}
};
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
Gacha
});
//# sourceMappingURL=index.js.map