Skip to content

Commit

Permalink
for web
Browse files Browse the repository at this point in the history
  • Loading branch information
retraigo committed Nov 13, 2022
1 parent 67a4680 commit 6e95459
Show file tree
Hide file tree
Showing 2 changed files with 101 additions and 0 deletions.
75 changes: 75 additions & 0 deletions dist/fortuna.js
Original file line number Diff line number Diff line change
@@ -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 };
26 changes: 26 additions & 0 deletions dist/roll.js
Original file line number Diff line number Diff line change
@@ -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 };

0 comments on commit 6e95459

Please sign in to comment.