👨‍💻 dev porting Auxiliary modules Java->kotlin

development

fukurou

the supreme coder
ADMIN
Kotlin:
class DrawRnd(vararg values: String) {
    // draw a random element, then take said element out
    private var strings = ArrayList<String>()
    private val stringsSource = ArrayList<String>()
    private val rand = Random()
    fun addElement(element: String) {
        strings.add(element)
        stringsSource.add(element)
    }

    fun draw(): String {
        if (strings.isEmpty()) {
            return ""
        }
        val x = rand.nextInt(strings.size)
        val element = strings[x]
        strings.removeAt(x)
        return element
    }

    fun getSimpleRNDNum(bound: Int): Int {
        // return 0->bound-1
        return rand.nextInt(bound)
    }

    private val tc = LGTypeConverter()

    init {
        for (i in values.indices) {
            strings.add(values[i])
            stringsSource.add(values[i])
        }
    }

    fun drawAsInt(): Int {
        if (strings.isEmpty()) {
            return 0
        }
        val rand = Random()
        val x = rand.nextInt(strings.size)
        val element = strings[x]
        strings.removeAt(x)
        return tc.convertToInt(element)
    }

    fun reset() {
        val dc = DeepCopier()
        strings = dc.copyList(stringsSource)
    }

    val isEmptied: Boolean
        get() = strings.isEmpty()
}
 

fukurou

the supreme coder
ADMIN
Kotlin:
public class DrawRndDigits {
    // draw a random integer, then take said element out
    private ArrayList<Integer> strings = new ArrayList<>();
    private ArrayList<Integer> stringsSource = new ArrayList<>();
    private Random rand = new Random();
    public DrawRndDigits(int... values) {
        for (int i = 0; i < values.length; i++) {
            strings.add(values[i]);
            stringsSource.add(values[i]);
        }
    }
    public void addElement(int element){
        strings.add(element);
        stringsSource.add(element);
    }
    public int draw(){
        if (strings.isEmpty()) {return -1;}

        int x = rand.nextInt(strings.size());
        int element = strings.get(x);
        strings.remove(x);
        return element;
    }
    public int getSimpleRNDNum(int bound){
        // return 0->bound-1
        return rand.nextInt(bound);
    }
    public void reset(){
        DeepCopier dc = new DeepCopier();
        strings = dc.copyListOfInts(stringsSource);
    }
}
 

fukurou

the supreme coder
ADMIN
Kotlin:
class Responder(vararg replies: String) {
    // simple random response dispenser
    private val responses = ArrayList<String>()
    private val rand = Random()

    init {
        Collections.addAll(responses, *replies)
    }

    val aResponse: String
        get() = if (responses.isEmpty()) {
            ""
        } else responses[rand.nextInt(responses.size)]

    fun responsesContainsStr(str: String): Boolean {
        return responses.contains(str)
    }

    fun strContainsResponse(str: String): Boolean {
        var result = false
        for (tempStr in responses) {
            if (str.contains(tempStr)) {
                result = true
                break
            }
        }
        return result
    }

    fun addResponse(s1: String) {
        responses.add(s1)
    }
}
 

fukurou

the supreme coder
ADMIN
Kotlin:
class AXNeuroSama    // the higher the rate the less likely to decorate outputs
    (private val rate: Int) {
    private val nyaa = Responder(" heart", " heart", " wink", " heart heart heart")
    private val rnd = DrawRnd()
    fun decorate(output: String): String {
        if (output.isEmpty()) {
            return output
        }
        return if (rnd.getSimpleRNDNum(rate) == 0) {
            output + nyaa.aResponse
        } else output
    }
}
 

fukurou

the supreme coder
ADMIN
Kotlin:
class PercentDripper {
    private val dr = DrawRnd()
    private var limis = 35
    fun setLimis(limis: Int) {
        this.limis = limis
    }

    fun drip(): Boolean {
        return dr.getSimpleRNDNum(100) < limis
    }

    fun dripPlus(plus: Int): Boolean {
        return dr.getSimpleRNDNum(100) < limis + plus
    }
}
 

fukurou

the supreme coder
ADMIN
Kotlin:
open class AXNPC(replyStockLim: Int, outputChance: Int) {
    var responder = RefreshQ()
    var dripper = PercentDripper()
    var cmdBreaker = AXCmdBreaker("say")

    init {
        responder.limit = replyStockLim
        if (outputChance in 1..100) {
            dripper.setLimis(outputChance)
        }
    }

    fun respond(): String {
        return if (dripper.drip()) {
            responder.rNDElement
        } else ""
    }

    fun respondPlus(plus: Int): String {
        // increase rate of output
        return if (dripper.dripPlus(plus)) {
            responder.rNDElement
        } else ""
    }

    fun learn(ear: String?): Boolean {
        // say hello there : hello there is learned
        val temp = cmdBreaker.extractCmdParam(ear!!)
        if (temp.isEmpty()) {
            return false
        }
        responder.add(temp)
        return true
    }

    fun strRespond(ear: String): String {
        // respond if ear contains a learned input
        if (ear.isEmpty()) {
            return ""
        }
        return if (dripper.drip() && responder.strContainsResponse(ear)) {
            responder.rNDElement
        } else ""
    }

    fun forceRespond(): String {
        return responder.rNDElement
    }

    fun setConjuration(conjuration: String) {
        cmdBreaker.conjuration = conjuration
    }
}
 

fukurou

the supreme coder
ADMIN
Kotlin:
public class AXNPC2 extends AXNPC{
    public AnnoyedQue annoyedQue = new AnnoyedQue(5);
    public AXNPC2(int replyStockLim, int outputChance) {
        super(replyStockLim, outputChance);
    }
    public void strLearn(String ear){
        // learns inputs containing strings that are repeatedly used by others
        annoyedQue.learn(ear);
        if (annoyedQue.isAnnoyed(ear)){
            this.responder.add(ear);
        }
    }
}
 

fukurou

the supreme coder
ADMIN
Kotlin:
class AXPassword {
    // code # to open the gate
    // while gate is open, code can be changed with: code new_number
    var isOpen = false
        private set
    private var maxAttempts = 3

    // return remaining login attempts
    var loginAttempts = maxAttempts
        private set
    private val regexUtil = RegexUtil()

    // event feature
    // get the code during weekly/monthly event after it has been randomized
    var codeEvent = 0
        private set

    fun codeUpdate(ear: String): Boolean {
        // while the gate is toggled on, the password code can be changed
        if (!isOpen) {
            return false
        }
        if (ear.contains("code")) {
            val temp: String = regexUtil.extractRegex(enumRegexGrimoire.Integer, ear)
            if (!temp.isEmpty()) {
                codeEvent = temp.toInt()
                return true
            }
        }
        return false
    }

    fun openGate(ear: String) {
        if (ear.contains("code") && loginAttempts > 0) {
            val noCode: String = regexUtil.extractRegex(enumRegexGrimoire.Integer, ear)
            if (noCode.isEmpty()) {
                return
            }
            val tempCode = noCode.toInt()
            if (tempCode == codeEvent) {
                loginAttempts = maxAttempts
                isOpen = true
            } else {
                loginAttempts--
            }
        }
    }

    fun resetAttempts() {
        // should happen once a day or hour to prevent hacking
        loginAttempts = maxAttempts
    }

    fun closeGate() {
        isOpen = false
    }

    fun closeGate(ear: String) {
        if (ear.contains("close")) {
            isOpen = false
        }
    }

    fun setMaxAttempts(maxAttempts: Int) {
        this.maxAttempts = maxAttempts
    }

    fun getCode(): Int {
        return if (isOpen) {
            codeEvent
        } else -1
    }

    fun randomizeCode(lim: Int, minimumLim: Int) {
        // event feature
        codeEvent = DrawRnd().getSimpleRNDNum(lim) + minimumLim
    }
}
 

fukurou

the supreme coder
ADMIN
Kotlin:
class Prompt {
    var regexUtil = RegexUtil()
    var kv = AXKeyValuePair()
    var prompt = ""
    private var regex = ""

    init {
        kv.key = "default"
    }

    fun process(in1: String): Boolean {
        kv.value = regexUtil.extractRegex(regex, in1)
        return kv.value.isEmpty() // is prompt still active?
    }

    fun setRegex(regex: String) {
        this.regex = regex
    }
}
 
Last edited:

fukurou

the supreme coder
ADMIN
Kotlin:
class AXPrompt {
    /*
    * example use:
    *
    *   // prompt1
        Prompt prompt1 = new Prompt();
        prompt1.kv.setKey("fruit");
        prompt1.setPrompt("do you prefer an apple, banana or grapes?");
        prompt1.setRegex("apple|banana|grapes");
        // prompt 2
        Prompt prompt2 = new Prompt();
        prompt2.kv.setKey("age");
        prompt2.setPrompt("how old are you??");
        prompt2.setRegex("\\d+(\\.\\d+)?");
        Scanner scanner = new Scanner(System.in);
        String in2 = "";
        AXPrompt axPrompt = new AXPrompt();
        axPrompt.addPrompt(prompt1);
        axPrompt.addPrompt(prompt2);
        axPrompt.activate();
        do{
            System.out.println(axPrompt.getPrompt());
            in2 = scanner.nextLine();
            axPrompt.process(in2);
            // extract keyvaluepair:
            AXKeyValuePair temp = axPrompt.getKv();
            // extract data: field, value
            if (!(temp == null)){
                System.out.println(temp.getValue());
            }
        }
        while (axPrompt.getActive());
    * */
    var active = false
    var index = 0
    var prompts = ArrayList<Prompt>()
    private var kv: AXKeyValuePair? = null
    fun addPrompt(p1: Prompt) {
        prompts.add(p1)
    }

    val prompt: String
        get() = if (prompts.isEmpty()) {
            ""
        } else prompts[index].prompt

    fun process(in1: String?) {
        if (prompts.isEmpty() || !active) {
            return
        }
        val b1 = prompts[index].process(in1!!)
        if (!b1) {
            kv = prompts[index].kv
            index++
        }
        if (index == prompts.size) {
            active = false
        }
    }

    fun getKv(): AXKeyValuePair? {
        if (kv == null) {
            return null
        }
        val temp = AXKeyValuePair()
        temp.key = kv!!.key
        temp.value = kv!!.value
        kv = null
        return temp
    }

    fun activate() {
        // reset
        active = true
        index = 0
    }

    fun deactivate() {
        // reset
        active = false
        index = 0
    }
}
 
Last edited:

fukurou

the supreme coder
ADMIN
Kotlin:
class AXShoutOut {
    private var isActive = false
    var handshake = Responder()
    fun activate() {
        // make engage-able
        isActive = true
    }

    fun engage(ear: String): Boolean {
        if (ear.isEmpty()) {
            return false
        }
        if (isActive) {
            if (handshake.strContainsResponse(ear)) {
                isActive = false
                return true // shout out was replied!
            }
        }
        // unrelated reply to shout out, shout out context is outdated
        isActive = false
        return false
    }
}
 

fukurou

the supreme coder
ADMIN
Kotlin:
class Strategy( // bank of all strategies. out of this pool active strategies are pulled
    var allStrategies: DrawRnd
) {
    private val activeStrategy // active strategic options
            : UniqueItemSizeLimitedPriorityQueue = UniqueItemSizeLimitedPriorityQueue()

    init {
        // create the strategy Object with a bank of options
    }

    fun evolveStrategies(strategiesLimit: Int) {
        // add N strategic options to the active strategies bank, from the total strategy bank
        activeStrategy.limit = strategiesLimit
        var temp = allStrategies.draw()
        for (i in 0 until strategiesLimit) {
            if (temp.isEmpty()) {
                break
            }
            activeStrategy.add(temp)
            temp = allStrategies.draw()
        }
        allStrategies.reset()
    }

    val strategy: String
        get() = activeStrategy.rNDElement
}
 

fukurou

the supreme coder
ADMIN
Kotlin:
class AXStrategy    // limit of active strategies (pulled from all available strategies)
    ( /* this auxiliary module is used to output strategies based on context
        can be used for battles, and games
        upon pain/lose use the evolve methode to update to different new active strategies
        check for battle state end externaly (opponent state/hits on rival counter)
    a dictionary of strategies*/
      private val lim: Int
) {
private val strategies = Hashtable<String, Strategy>()
fun addStrategy(context: String, techniques: DrawRnd?) {
// add strategies per context
        val temp = Strategy(techniques!!)
temp.evolveStrategies(lim)
strategies[context] = temp
    }

fun evolve() {
// replace active strategies
        val e = strategies.keys()
var key: String
while (e.hasMoreElements()) {
            key = e.nextElement()
strategies[key]!!.evolveStrategies(lim)
        }
    }

fun process(context: String): String {
// process input, return action based on game context now
        return if (strategies.containsKey(context)) {
strategies[context]!!.strategy
        } else ""
    }
}
 
Last edited:

fukurou

the supreme coder
ADMIN
Kotlin:
class AXStringSplit {
    // may be used to prepare data before saving or after loading
    // the advantage is less data fields. for example: {skills: s1_s2_s3}
    private var spChar = "_"
    fun setSpChar(spChar: String) {
        this.spChar = spChar
    }

    fun split(s1: String): Array<String> {
        return s1.split(spChar.toRegex()).dropLastWhile { it.isEmpty() }.toTypedArray()
    }

    fun stringBuilder(sArr: Array<String?>): String {
        val sb = StringBuilder()
        for (i in 0 until sArr.size - 1) {
            sb.append(sArr[i])
            sb.append(spChar)
        }
        sb.append(sArr[sArr.size - 1])
        return sb.toString()
    }
}
 

fukurou

the supreme coder
ADMIN
Kotlin:
class AXStrOrDefault {
    fun getOrDefault(str1: String, default1: String): String {
        return str1.ifEmpty { default1 }
    }
}
 

fukurou

the supreme coder
ADMIN
Kotlin:
class TrgTime {
    var t = "null"
    var regexUtil = RegexUtil()
    var pl: PlayGround = PlayGround()
    private var alarm = true
    fun setTime(v1: String) {
        t = regexUtil.extractRegex(enumRegexGrimoire.SimpleTimeStamp, v1)
    }

    fun alarm(): Boolean {
        val now: String = pl.currentTimeStamp
        if (alarm) {
            if (now == t) {
                alarm = false
                return true
            }
        }
        if (now != t) {
            alarm = true
        }
        return false
    }
}
 

fukurou

the supreme coder
ADMIN
Kotlin:
class Cron(
    startTime: String, // triggers true, limit times, after initial time, and every minutes interval
    // counter resets at initial time, assuming trigger method was run
    private var minutes // minute interval between triggerings
    : Int, limit: Int
) : TrGEV3() {
    private val pl = PlayGround()
    private val trgTime: TrgTime
    private var timeStamp = ""
    private var initialTimeStamp = ""
    private var limit: Int
    var counter = 0
        private set

    init {
        timeStamp = startTime
        initialTimeStamp = startTime
        trgTime = TrgTime()
        trgTime.setTime(startTime)
        this.limit = limit
        if (limit < 0) {
            this.limit = 1
        }
    }

    fun getLimit(): Int {
        return limit
    }

    fun setLimit(limit: Int) {
        if (limit > -1) {
            this.limit = limit
        }
    }

    fun setMinutes(minutes: Int) {
        if (minutes > -1) {
            this.minutes = minutes
        }
    }

    override fun trigger(): Boolean {
        // delete counter = 0 if you don't want the trigger to work the next day
        if (counter == limit) {
            trgTime.setTime(initialTimeStamp)
            counter = 0
            return false
        }
        if (trgTime.alarm()) {
            timeStamp = pl.getFutureInXMin(minutes)
            trgTime.setTime(timeStamp)
            counter++
            return true
        }
        return false
    }

    fun triggerWithoutRenewal(): Boolean {
        // delete counter = 0 if you don't want the trigger to work the next day
        if (counter == limit) {
            trgTime.setTime(initialTimeStamp)
            return false
        }
        if (trgTime.alarm()) {
            timeStamp = pl.getFutureInXMin(minutes)
            trgTime.setTime(timeStamp)
            counter++
            return true
        }
        return false
    }

    override fun reset() {
        // manual trigger reset
        counter = 0
    }

    fun setStartTime(t1: String) {
        initialTimeStamp = t1
        timeStamp = t1
        trgTime.setTime(t1)
        counter = 0
    }

    fun turnOff() {
        counter = limit
    }
}
 
Top