cerceis-lib
Version:
Contains list of quality of life functions that is written in TypeScript and es6
1 lines • 1.26 kB
Source Map (JSON)
{"version":3,"sources":["../../src/gacha/index.ts"],"sourcesContent":["export class Gacha<T = unknown> {\n private entries: Array<{ item: T; accumulatedWeight: number }> = [];\n private totalWeight = 0;\n\n /**\n * Add an item with a given weight.\n * Higher weight = higher probability of being drawn.\n * @param item Item to add.\n * @param weight Relative weight.\n */\n addEntries(item: T, weight: number): void {\n this.totalWeight += weight;\n this.entries.push({ item, accumulatedWeight: this.totalWeight });\n }\n\n /**\n * Draw a random item, weighted by the entries added.\n * Returns undefined if no entries have been added.\n */\n getRandom(): T | undefined {\n const r = Math.random() * this.totalWeight;\n return this.entries.find((e) => e.accumulatedWeight >= r)?.item;\n }\n}\n"],"mappings":";AAAO,IAAM,QAAN,MAAyB;AAAA,EAAzB;AACH,SAAQ,UAAyD,CAAC;AAClE,SAAQ,cAAc;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAQtB,WAAW,MAAS,QAAsB;AACtC,SAAK,eAAe;AACpB,SAAK,QAAQ,KAAK,EAAE,MAAM,mBAAmB,KAAK,YAAY,CAAC;AAAA,EACnE;AAAA;AAAA;AAAA;AAAA;AAAA,EAMA,YAA2B;AACvB,UAAM,IAAI,KAAK,OAAO,IAAI,KAAK;AAC/B,WAAO,KAAK,QAAQ,KAAK,CAAC,MAAM,EAAE,qBAAqB,CAAC,GAAG;AAAA,EAC/D;AACJ;","names":[]}