Add new game states and update
This commit is contained in:
@@ -0,0 +1,73 @@
|
||||
package club.mcscrims.speedhg.util
|
||||
|
||||
import java.util.*
|
||||
|
||||
class RandomCollection<E> {
|
||||
|
||||
private val map = TreeMap<Double, List<E>>()
|
||||
private val names = hashMapOf<List<E>, String>()
|
||||
|
||||
private val random = Random()
|
||||
private var total = 0.0
|
||||
|
||||
fun add(
|
||||
weight: Double,
|
||||
result: E
|
||||
) {
|
||||
if ( weight <= 0 ) return
|
||||
total += weight
|
||||
map[ total ] = listOf( result )
|
||||
}
|
||||
|
||||
fun add(
|
||||
name: String,
|
||||
weight: Double,
|
||||
result: E
|
||||
) {
|
||||
if ( weight <= 0 ) return
|
||||
total += weight
|
||||
map[ total ] = listOf( result )
|
||||
names[listOf( result )] = name
|
||||
}
|
||||
|
||||
fun add(
|
||||
weight: Double,
|
||||
result: List<E>
|
||||
) {
|
||||
if ( weight <= 0 ) return
|
||||
total += weight
|
||||
map[ total ] = result
|
||||
}
|
||||
|
||||
fun add(
|
||||
name: String,
|
||||
weight: Double,
|
||||
result: List<E>
|
||||
) {
|
||||
if ( weight <= 0 ) return
|
||||
total += weight
|
||||
map[ total ] = result
|
||||
names[ result ] = name
|
||||
}
|
||||
|
||||
fun getName(
|
||||
key: E
|
||||
): String
|
||||
{
|
||||
return names.getOrDefault(listOf( key ), "" )
|
||||
}
|
||||
|
||||
fun getName(
|
||||
key: List<E>
|
||||
): String
|
||||
{
|
||||
return names.getOrDefault( key, "" )
|
||||
}
|
||||
|
||||
fun getRandom(): List<E>
|
||||
{
|
||||
val value = random.nextDouble() * total
|
||||
return map.higherEntry( value ).value
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user