Skip to content

Instantly share code, notes, and snippets.

@its-jackson
Created September 11, 2023 19:36
Show Gist options
  • Save its-jackson/cc00fea38cdc5d4b308697b3c5a9bacd to your computer and use it in GitHub Desktop.
Save its-jackson/cc00fea38cdc5d4b308697b3c5a9bacd to your computer and use it in GitHub Desktop.
package scripts.kotlin.api.framework.routine.api.binary.tree
import org.tribot.script.sdk.Options
import org.tribot.script.sdk.antiban.Antiban
import scripts.kotlin.api.framework.routine.RoutineStatus
import scripts.kotlin.api.framework.routine.api.Logger
import kotlin.reflect.KMutableProperty0
interface Node {
fun execute(): RoutineStatus
fun depth(): Int
fun nodeCount(): Int
fun leafCount(): Int
fun log(indent: Int = 0, logger: Logger)
}
interface Tree : Node
class BinaryTree(private val rootNode: Node) : Tree {
override fun execute() = rootNode.execute()
override fun depth() = rootNode.depth()
override fun nodeCount() = rootNode.nodeCount()
override fun leafCount() = rootNode.leafCount()
override fun log(indent: Int, logger: Logger) {
logger.debug("Binary Tree Structure:")
rootNode.log(logger = logger)
}
}
class Forest {
private val logger: Logger = Logger("Forest Manager")
private val trees: MutableList<BinaryTree> = mutableListOf()
fun addTree(tree: BinaryTree): Boolean {
return trees.add(tree)
}
fun removeTree(tree: BinaryTree): Boolean {
return trees.remove(tree)
}
fun executeAllTrees(): RoutineStatus {
for (tree in trees) {
val status = tree.execute()
if (status !== RoutineStatus.SUCCESS) {
return status
}
}
return RoutineStatus.SUCCESS
}
fun logAllTrees() {
trees.forEach {
logger.debug("Binary tree depth: ${it.depth()}")
logger.debug("Binary tree node count: ${it.nodeCount()}")
logger.debug("Binary tree leaf count: ${it.leafCount()}")
it.log(0, logger)
}
}
}
abstract class DecisionNode(
private val trueNode: Node,
private val falseNode: Node?,
) : Node {
abstract fun conditionCheck(): Boolean
override fun execute(): RoutineStatus {
return if (conditionCheck()) {
trueNode.execute()
}
else {
falseNode?.execute() ?: RoutineStatus.RETRY
}
}
override fun depth(): Int {
val trueDepth = trueNode.depth()
val falseDepth = falseNode?.depth() ?: 0
return 1 + maxOf(trueDepth, falseDepth)
}
override fun nodeCount(): Int = 1 + trueNode.nodeCount() + (falseNode?.nodeCount() ?: 0)
override fun leafCount(): Int {
if (trueNode is PerformNode && (falseNode == null || falseNode is PerformNode)) {
return 1
}
return trueNode.leafCount() + (falseNode?.leafCount() ?: 0)
}
override fun log(indent: Int, logger: Logger) {
logger.debug(" ".repeat(indent) + this::class.simpleName)
trueNode.log(indent + 2, logger)
falseNode?.log(indent + 2, logger)
}
}
abstract class PerformNode : Node {
abstract override fun execute(): RoutineStatus
override fun depth(): Int = 1
override fun nodeCount(): Int = 1
override fun leafCount(): Int = 1
override fun log(indent: Int, logger: Logger) = logger.debug(" ".repeat(indent) + this::class.simpleName)
}
abstract class EffectPerformNode<T>(val callback: (T) -> Unit) : PerformNode()
class FieldReference<T : Any?>(private val property1: KMutableProperty0<T>) {
var value: T
get() = property1.get()
set(value) {
property1.set(value)
}
}
class SetToNullPerformNode<T : Any?>(private val ref: FieldReference<T?>) : PerformNode() {
override fun execute(): RoutineStatus {
ref.value = null
return RoutineStatus.SUCCESS
}
}
class IsObjectNotNullDecisionNode<T>(
private val objProvider: () -> T?,
trueNode: Node,
falseNode: Node?
) : DecisionNode(trueNode, falseNode) {
override fun conditionCheck() = objProvider.invoke() !== null
}
class ShouldTurnOnRunDecisionNode(
trueNode: Node = TurnOnRunPerformNode(),
falseNode: Node
) : DecisionNode(trueNode, falseNode) {
override fun conditionCheck() = Antiban.shouldTurnOnRun() && !Options.isRunEnabled()
}
class TurnOnRunPerformNode : PerformNode() {
override fun execute(): RoutineStatus {
if (Options.setRunEnabled(true)) {
return RoutineStatus.SUCCESS
}
return RoutineStatus.FAILURE
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment