mirror of
https://github.com/Melledy/Nebula.git
synced 2025-12-17 06:44:35 +01:00
Implement gacha banners (not newbie)
This commit is contained in:
35
src/main/java/emu/nebula/util/WeightedList.java
Normal file
35
src/main/java/emu/nebula/util/WeightedList.java
Normal file
@@ -0,0 +1,35 @@
|
||||
package emu.nebula.util;
|
||||
|
||||
import java.util.NavigableMap;
|
||||
import java.util.TreeMap;
|
||||
import java.util.concurrent.ThreadLocalRandom;
|
||||
|
||||
public class WeightedList<E> {
|
||||
private final NavigableMap<Double, E> map = new TreeMap<>();
|
||||
private double total = 0;
|
||||
|
||||
public WeightedList() {
|
||||
|
||||
}
|
||||
|
||||
public WeightedList<E> add(double weight, E result) {
|
||||
if (weight <= 0) return this;
|
||||
total += weight;
|
||||
map.put(total, result);
|
||||
return this;
|
||||
}
|
||||
|
||||
public E next() {
|
||||
double value = ThreadLocalRandom.current().nextDouble() * total;
|
||||
return map.higherEntry(value).getValue();
|
||||
}
|
||||
|
||||
public int size() {
|
||||
return map.size();
|
||||
}
|
||||
|
||||
public void clear() {
|
||||
this.map.clear();
|
||||
this.total = 0;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user