Add new game states and update

This commit is contained in:
TDSTOS
2025-12-08 21:59:40 +01:00
parent 590318772f
commit fef0072007
10 changed files with 440 additions and 52 deletions

View File

@@ -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
}
}