From 6e95459329e76f9fc4770117ae9cefef3daf8f43 Mon Sep 17 00:00:00 2001 From: Neko Of The Abyss Date: Sun, 13 Nov 2022 17:48:05 +0530 Subject: [PATCH] for web --- dist/fortuna.js | 75 +++++++++++++++++++++++++++++++++++++++++++++++++ dist/roll.js | 26 +++++++++++++++++ 2 files changed, 101 insertions(+) create mode 100644 dist/fortuna.js create mode 100644 dist/roll.js diff --git a/dist/fortuna.js b/dist/fortuna.js new file mode 100644 index 0000000..96cf35a --- /dev/null +++ b/dist/fortuna.js @@ -0,0 +1,75 @@ +// deno-fmt-ignore-file +// deno-lint-ignore-file +// This code was bundled using `deno bundle` and it's not recommended to edit it manually + +class GachaMachine { + #items; + #totalChance; + constructor(items){ + this.#items = new Array(items.length); + this.#totalChance = 0; + this.#configItems(items); + } + set items(items) { + this.#items = new Array(items.length); + this.#totalChance = 0; + this.#configItems(items); + } + #configItems(items) { + let i = 0; + let cumulativeChance = 0; + while(i < items.length){ + cumulativeChance += items[i].chance; + this.#items[i] = { + result: items[i].result, + cumulativeChance: cumulativeChance + }; + i += 1; + } + this.#totalChance = cumulativeChance; + } + get(count, distinct = false) { + if (distinct && count > this.#items.length) { + throw new RangeError(`count must be less than number of items in pool.`); + } + const totalChance = this.#totalChance; + const result = new Array(count); + let i = 0; + if (distinct) { + const data = this.#items.slice(0); + while(i < count){ + const res = rollWithBinarySearch(data, totalChance); + result[i] = data[res].result; + data.splice(res, 1); + i += 1; + } + } else { + const data1 = this.#items; + while(i < count){ + result[i] = data1[rollWithBinarySearch(data1, totalChance)].result; + i += 1; + } + } + return result; + } +} +function rollWithBinarySearch(items, totalChance) { + if (!totalChance) totalChance = items[items.length - 1].cumulativeChance; + if (items.length === 1) return 0; + const rng = Math.random() * totalChance; + let lower = 0; + let max = items.length - 1; + let mid = Math.trunc((max + lower) / 2); + while(mid != 0 && lower <= max){ + if (items[mid].cumulativeChance > rng && items[mid - 1].cumulativeChance < rng || items[mid].cumulativeChance == rng) return mid; + if (items[mid].cumulativeChance < rng) { + lower = mid + 1; + mid = Math.trunc((max + lower) / 2); + } else { + max = mid - 1; + mid = Math.trunc((max + lower) / 2); + } + } + return mid; +} +export { GachaMachine as GachaMachine }; diff --git a/dist/roll.js b/dist/roll.js new file mode 100644 index 0000000..c2a7bbd --- /dev/null +++ b/dist/roll.js @@ -0,0 +1,26 @@ +// deno-fmt-ignore-file +// deno-lint-ignore-file +// This code was bundled using `deno bundle` and it's not recommended to edit it manually + +function roll(choices, totalChance = 0) { + let total = totalChance; + let i = 0; + if (totalChance === 0) { + while(i < choices.length){ + total += choices[i].chance; + i += 1; + } + } + const result = Math.random() * total; + let going = 0.0; + i = 0; + while(i < choices.length){ + going += choices[i].chance; + if (result < going) { + return choices[i].result; + } + i += 1; + } + return choices[Math.floor(Math.random() * choices.length)].result; +} +export { roll as roll };