All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.skillw.asahi.internal.namespacing.prefix.lang.math.Random.kt Maven / Gradle / Ivy

The newest version!
package com.skillw.asahi.internal.namespacing.prefix.lang.math

import com.skillw.asahi.api.annotation.AsahiPrefix
import com.skillw.asahi.api.member.quest.Quester
import com.skillw.asahi.api.prefixParser
import com.skillw.asahi.api.quest
import taboolib.common5.RandomList

/**
 * @className Random
 *
 * @author Glom
 * @date 2023/1/14 0:31 Copyright 2024 Glom.
 */

@AsahiPrefix(["random"], "lang")
private fun random() = prefixParser {
    val x = quest()
    expect("to")
    val y = quest()
    result { com.skillw.pouvoir.util.random(x.get(), y.get()) }
}

@AsahiPrefix(["randomInt"], "lang")
private fun randomInt() = prefixParser {
    val x = quest()
    expect("to")
    val y = quest()
    result { com.skillw.pouvoir.util.randomInt(x.get(), y.get()) }
}

@AsahiPrefix(["randomObj"], "lang")
private fun randomObj() = prefixParser {
    val list = quest>()
    result { list.get().random() }
}

@AsahiPrefix(["weight"], "lang")
private fun weight() = prefixParser {
    expect("[")
    val list = ArrayList, Quester>>()
    do {
        val weight = quest()
        expect("to", "=", ":")
        val value = quest()
        list += weight to value
        expect(",")
    } while (!expect("]"))
    val builder = peek() == "build"
    if (builder) next()
    result {
        val randomList = RandomList()
        list.map { it.second.get() to it.first.get() }.forEach { (value, weight) ->
            randomList.add(value, weight)
        }
        return@result if (!builder) randomList.random()!! else randomList
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy