botodon/src/utils/Selector.ts

24 lines
544 B
TypeScript
Raw Normal View History

2019-06-14 15:10:20 +00:00
export default class Selector<T> {
private last: T
private index: number[]
constructor(private list: T[], private same: boolean, weight: (el: T) => number) {
this.index = list.map((s, idx) => Array(weight(s)).fill(idx)).reduce((a, b) => a.concat(b))
}
next() {
if (this.same) {
if (this.last === undefined) {
this.last = this.select()
}
return this.last
}
return this.select()
}
private select() {
return this.list[this.index[Math.floor(Math.random() * this.index.length)]]
}
}