Jump to content

Search the Community

Showing results for tags 'kotlin'.

  • Search By Tags

    Type tags separated by commas.
  • Search By Author

Content Type


Forums

  • Tribot
    • News and Announcements
    • Premium Scripts
    • Help and Guidance
    • Support Tickets
    • Community Scripts
  • Community
    • Discussion
    • Programming
    • Feedback
    • Scripting
  • Market
    • Private Script Shops
    • Gold Market
    • Account Market
    • Services
    • Graphics
    • Vouch Threads

Find results in...

Find results that contain...


Date Created

  • Start

    End


Last Updated

  • Start

    End


Filter by number of...

Joined

  • Start

    End


Group


About Me

Found 2 results

  1. To allow for more randomisation throughout my scripts, I've implemented a how I like to call it, Lottery helper. It's a rather simple re-usable Singleton, which allows for lambda code execution, if a given probability is achieved. For this, I've created the following Singleton: package scripts.utils.antiban import scripts.utils.Logger import kotlin.random.Random /** * Utility class to randomize certain actions on a probability(%) basis. */ object Lottery { var logger: Logger? = null fun initLogger(log: Logger) { this.logger = log } /** * Pass through a probability and lambda to execute, if our case is within the probability threshold */ fun execute(probability: Double, action: () -> Unit) { val won: Boolean = shouldExecute(probability) if (won) { this.logger?.info("[Lottery/Antiban] - Executing randomized action") action() } } /** * Probability refers to the rough percentage % this action will be executed on */ private fun shouldExecute(probability: Double): Boolean { require(probability in 0.0..1.0) { "Probability must be between 0.0 and 1.0" } val diceRoll = Random.nextDouble() return diceRoll < probability } } For example, executing a mini-break every now and then: Lottery.execute(0.16) { MiniBreak.leave() } Or, to better demonstrate, by running a 1.000 iterations: fun main() { val probabilities = listOf(0.01, 0.05, 0.1, 0.25, 0.5, 0.75, 0.9) // Test probabilities from 1% to 90% probabilities.forEach { probability -> var executedCount = 0 val totalRuns = 1000 // Run each test 1000 times for a statistical sample repeat(totalRuns) { Lottery.execute(probability) { executedCount++ // Increment if action is executed } } println("Probability: ${(probability * 100).toInt()}% -> Executed: $executedCount times out of $totalRuns") } } Which resulted in the following: Probability: 1% -> Executed: 9 times out of 1000 Probability: 5% -> Executed: 44 times out of 1000 Probability: 10% -> Executed: 97 times out of 1000 Probability: 25% -> Executed: 227 times out of 1000 Probability: 50% -> Executed: 523 times out of 1000 Probability: 75% -> Executed: 733 times out of 1000 Hope this introduction either helped you out or inspired you to create amazing, randomised code!
  2. Here is a simple Kotlin experience tracking system that lets you register for new skills that have gained XP. Also provides several useful calculations and accounts for XP/hr only with active time on the skill! You can tweak the inactivity timer by changing the `inactivityThreshold` in the ExperienceTracker Usage // Declare once in the root on your main script class YourScript : TribotScript { private val experienceTracker = ExperienceTracker() override fun execute(args: String) { experienceTracker.addNewSkillListener { skill -> Log.info("Trying to track ${skill.name}") } experienceTracker.addActivityStatusListener(skill) { isInactive -> Log.info("Your ${skill.name} is $isInactive") } // Your loop here scriptLoop { experienceTracker.update() } } } Library package scripts import java.time.Instant import org.tribot.script.sdk.Skill class ExperienceTracker { private val skillTrackers = mutableMapOf<Skill, SkillTracker>() private var lastUpdateTime: Instant = Instant.now() private val newSkillListeners = mutableListOf<(Skill) -> Unit>() private val inactivityThreshold: Long = 60 * 3 private val activityStatusListeners = mutableMapOf<Skill, MutableList<(Boolean) -> Unit>>() fun update() { val currentTime = Instant.now() Skill.values().forEach { skill -> val currentXp = skill.getXp() val tracker = skillTrackers.getOrPut(skill) { SkillTracker(skill, currentXp, currentTime) } val hasGainedXp = tracker.update(currentXp, currentTime) // Notify listeners when a new skill is tracked and has gained XP if (hasGainedXp && tracker.isNewlyTracked) { newSkillListeners.forEach { listener -> listener(skill) } tracker.isNewlyTracked = false } // Check for inactivity status change and notify listeners val wasInactive = tracker.wasInactive val isNowInactive = tracker.isInactive(currentTime) if (wasInactive != isNowInactive) { tracker.wasInactive = isNowInactive activityStatusListeners[skill]?.forEach { it(isNowInactive) } } } lastUpdateTime = currentTime } fun getChangedSkills(): List<Skill> { return skillTrackers.values.filter { it.hasChanged() }.map { it.skill } } fun getSkillStats(skill: Skill): SkillStats? { return skillTrackers[skill]?.getStats() } fun getXpToNextLevel(skill: Skill): Int? { return skill.currentXpToNextLevel } fun getTimeToNextLevel(skill: Skill): Double? { val tracker = skillTrackers[skill] ?: return null val stats = tracker.getStats() val xpToNextLevel = skill.currentXpToNextLevel return if (stats.xpPerHour > 0) { val activeTime = stats.activeTimeInSeconds if (activeTime > 0) { (xpToNextLevel / (stats.totalChange.toDouble() / activeTime)) // Calculate time in seconds } else { null } } else { null } } fun isSkillInactive(skill: Skill): Boolean { return skillTrackers[skill]?.isInactive(Instant.now()) ?: false } private inner class SkillTracker(val skill: Skill, initialXp: Int, initialTime: Instant) { private var startXp: Int = initialXp private var lastXp: Int = initialXp private var currentXp: Int = initialXp private var startTime: Instant = initialTime private var startLevel: Int = skill.getCurrentLevel() private var currentLevel: Int = startLevel private var levelsGained: Int = 0 private var lastActiveTime: Instant = initialTime private var xpHistory: MutableList<Pair<Instant, Int>> = mutableListOf() var isNewlyTracked: Boolean = true var wasInactive: Boolean = false fun update(newXp: Int, currentTime: Instant): Boolean { val hasGainedXp = newXp > currentXp if (hasGainedXp) { lastXp = currentXp currentXp = newXp val newLevel = skill.getCurrentLevel() if (newLevel > currentLevel) { levelsGained += newLevel - currentLevel currentLevel = newLevel } wasInactive = isInactive(currentTime) lastActiveTime = currentTime xpHistory.add(currentTime to newXp) // Keep only the last hour of data xpHistory = xpHistory .dropWhile { it.first.isBefore(currentTime.minusSeconds(3600)) } .toMutableList() } return hasGainedXp } fun isInactive(currentTime: Instant): Boolean { return java.time.Duration.between(lastActiveTime, currentTime).seconds > inactivityThreshold } fun hasChanged(): Boolean = currentXp != lastXp fun getStats(): SkillStats { val totalChange = currentXp - startXp val recentChange = currentXp - lastXp val activeTime = calculateActiveTime() val xpPerHour = if (activeTime > 0) { totalChange.toDouble() / activeTime * 3600 } else { 0.0 } return SkillStats( skill = skill, totalChange = totalChange, recentChange = recentChange, xpPerHour = xpPerHour, xpHistory = xpHistory.toList(), isInactive = isInactive(Instant.now()), activeTimeInSeconds = activeTime, levelsGained = levelsGained) } private fun calculateActiveTime(): Long { var activeTime = 0L var lastXpTime = startTime for ((time, _) in xpHistory) { val timeSinceLastXp = java.time.Duration.between(lastXpTime, time).seconds activeTime += minOf(timeSinceLastXp, inactivityThreshold) lastXpTime = time } val timeSinceLastXp = java.time.Duration.between(lastXpTime, Instant.now()).seconds activeTime += minOf(timeSinceLastXp, inactivityThreshold) return activeTime } } data class SkillStats( val skill: Skill, val totalChange: Int, val recentChange: Int, val xpPerHour: Double, val xpHistory: List<Pair<Instant, Int>>, val isInactive: Boolean, val activeTimeInSeconds: Long, val levelsGained: Int ) fun addNewSkillListener(listener: (Skill) -> Unit) { newSkillListeners.add(listener) } fun removeNewSkillListener(listener: (Skill) -> Unit) { newSkillListeners.remove(listener) } fun addActivityStatusListener(skill: Skill, listener: (Boolean) -> Unit) { activityStatusListeners.getOrPut(skill) { mutableListOf() }.add(listener) } fun removeActivityStatusListener(skill: Skill, listener: (Boolean) -> Unit) { activityStatusListeners[skill]?.remove(listener) } }
×
×
  • Create New...