-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
101 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 }; |