better separation of concerns

This commit is contained in:
dtookey 2023-08-06 20:09:22 -04:00
parent 2b4c5abc94
commit fb7acdd401
3 changed files with 215 additions and 228 deletions

View File

@ -51,4 +51,29 @@ object HelperFunctions {
}
}
/**
* Gets the current mouse pointer location and returns it as a val declaration string.
*
* This method uses the [getPointerLocationAfter] method to get the current
* mouse pointer location after a small delay.
*
* It then formats this location into a string declaring a val with the provided
* variable name, like:
*
* ```
* val myPoint = Point(123, 456)
* ```
*
* The delay before getting the pointer location helps ensure the mouse has
* settled after any prior movements.
*
* @param varName The name to use for the variable in the declaration string.
* @return A string declaring a val with the pointer location.
*/
fun getPointerLocationAsValDeclarationString(orchestrator: Orchestrator, varName: String): String {
val info = orchestrator.getPointerLocationAfterDelay(5)
return "val $varName = Point(${info.x}, ${info.y})"
}
}

View File

@ -1,4 +1,5 @@
import java.awt.Point
import java.awt.event.InputEvent
/**
* Interface for orchestrating automated interactions with the game.
@ -34,6 +35,131 @@ interface Orchestrator {
}
/**
* Draws a star shape by moving the mouse between points.
*
* This method takes in a center point and calculates offset points around it.
* It then moves the mouse between each point in sequence to draw a star shape.
*
* The offset points are calculated by moving left/right and up/down from the
* center point by a fixed offset amount.
*
* @param p The center point of the star.
*/
fun drawStar(p: Point) {
val offset = 100
val top = Point(p.x, p.y - offset * 2)
val topright = Point(p.x + offset * 2, p.y + offset)
val bottomright = Point(p.x + offset * 2, p.y)
val topleft = Point(p.x - offset * 2, p.y + offset)
val bottomleft = Point(p.x - offset * 2, p.y)
val points = arrayListOf(top, bottomleft, topright, topleft, bottomright)
for (i in 0 until 10) {
for (point in points) {
automaton.moveMouse(point)
automaton.sleep(32)
}
}
}
/**
* Moves the mouse to a point, left clicks, and sleeps.
*
* This method moves the mouse to the provided point location, left clicks
* at that location, sleeps for the specified duration plus a random variance.
*
* Typical usage is to provide a point where something needs to be clicked,
* along with a sleep duration to wait after clicking.
*
* The random variance in sleep time helps match real human actions.
*
* @param p The point location to move the mouse and left click.
* @param sleepDuration The base duration in ms to sleep after clicking.
* @param sleepDurationVariance The allowed variance in the sleep duration.
*/
fun moveMouseLeftClickAndSleep(p: Point, sleepDuration: Long, sleepDurationVariance: Long) {
automaton.moveMouse(p)
automaton.sleepWithVariance(100, 50)
//left click
automaton.mouseClick(InputEvent.BUTTON1_DOWN_MASK)
automaton.sleepWithVariance(sleepDuration, sleepDurationVariance)
}
/**
* Performs a countdown loop announcing each step.
*
* This method counts down from a provided number of seconds to 0.
*
* It calls the provided [announceFn] function on each step, passing the current
* step number.
* A sleep of 1 second is added between each step.
* Typical usage is to print a countdown message within [announceFn].
* @param nSeconds The number of seconds to count down.
* @param announceFn A callback function called each step, passed step number.
*/
fun countDownInSeconds(nSeconds: Int, announceFn: (step: Int) -> Unit) {
for (i in nSeconds downTo 0) {
announceFn(i)
automaton.sleep(1000)
}
}
/**
* Gets the mouse pointer location after a delay.
*
* This method uses a countdown loop to wait for the specified number of
* seconds, printing a countdown as it goes.
*
* After the delay, it calls the automation library's getPointerLocation()
* method to retrieve the current mouse pointer coordinates.
*
* The delay helps ensure any prior mouse movements have settled before
* sampling the location.
*
* @param delayInSeconds The number of seconds to wait before sampling pointer location.
* @return The mouse pointer location after the delay as a Point.
*/
fun getPointerLocationAfterDelay(delayInSeconds: Int): Point {
countDownInSeconds(delayInSeconds) {
print("\rtaking pointer snapshot in $it...")
if (it == 0) {
println("\r ")
}
}
return automaton.getPointerLocation()
}
/**
* Prompts the user to position the mouse and returns that position.
*
* This method prints a prompt message, then waits for the user to position
* the mouse.
*
* It then returns the current mouse position as a Point after a slight delay.
*
* The delay allows the mouse to settle before sampling its position.
*
* @param prompt The message to display to prompt user to position mouse.
* @return The Point position of the mouse after user positions it.
*/
fun promptUserForPoint(prompt: String): Point {
println(prompt)
countDownInSeconds(5) {
print("\rtaking point snapshot in $it... ")
if (it == 0) {
println("\r ")
}
}
return automaton.getPointerLocation()
}
/**
* Performs a repetitive task multiple times.
*
@ -67,21 +193,6 @@ interface Orchestrator {
val finish = System.currentTimeMillis()
println("Finished everything in ${HelperFunctions.prettyTimeString(finish - start)}")
}
/**
* Prompts the user to input a point and returns the point.
*
* @param prompt The message to display to the user asking for a point input.
* @return The Point object based on the user's input.
*/
fun promptUserForPoint(prompt: String): Point
/**
* Draws a star shape at the specified point on the screen.
*
* @param p The center point of the star to draw.
*/
fun drawStar(p: Point)
}

View File

@ -1,75 +1,8 @@
import java.awt.Point
import java.awt.event.InputEvent
import java.awt.event.KeyEvent
/**
* Interface for coordinating RuneScape bot actions.
*
* This interface defines methods for orchestrating RuneScape gameplay like banking,
* traveling, and crafting. It provides the core logic needed for automating
* RuneScape tasks.
*/
interface RSCoordinator {
/**
* Handles the crafting process when standing at the bank.
*
* This method takes care of the workflow when standing at the bank:
* - Opens the bank interface using the bank preset hotkey.
* - Waits for the crafting duration.
* - Closes the bank interface.
*
* @param bankPoint The [Point] location of the bank on screen.
* @param bankPresetHotkey The hotkey used to open the bank interface.
* @param craftingDialogueHotkey The hotkey used to open the crafting dialogue.
* @param waitDurationMillis The base duration in milliseconds to wait while crafting.
* @param waitDurationVarianceMillis Random variance in milliseconds to add to the wait duration.
*/
fun processAtBank(
bankPoint: Point,
bankPresetHotkey: Int,
craftingDialogueHotkey: Int,
waitDurationMillis: Long,
waitDurationVariance: Long
)
/**
* Handles the crafting process when at a station near the bank.
*
* This method takes care of the workflow when at the crafting station:
* - Travels from the bank to the station.
* - Waits for the crafting duration.
* - Travels back to the bank when done.
*
* @param bankPoint The [Point] location of the bank.
* @param craftingStationPoint The [Point] location of the crafting station.
* @param bankPresetHotkey The hotkey to open the bank interface.
* @param travelDurationMillis The base travel time between bank and station.
* @param travelDurationVarianceMillis Random variance to add to the travel time.
* @param waitDurationMillis The base duration to wait while crafting.
* @param waitDurationVarianceMillis Random variance to add to the wait duration.
*/
fun processAtStationNearBank(
bankPoint: Point,
craftingStationPoint: Point,
bankPresetHotkey: Int,
travelDurationMillis: Long,
travelDurationVarianceMillis: Long,
waitDurationMillis: Long,
waitDurationVarianceMillis: Long
)
/**
* Gets the bank location point.
*
* @return The [Point] representing the x,y screen coordinates of the bank location.
*/
fun getBankPoint(): Point
}
interface RSOrchestrator : Orchestrator, RSCoordinator{
interface RSOrchestrator : Orchestrator{
companion object {
/**
@ -125,11 +58,74 @@ interface RSOrchestrator : Orchestrator, RSCoordinator{
}
}
/**
* Gets an instance of the [RSOrchestrator].
*
* This provides access to the orchestrator instance that can be used to
* coordinate bot actions.
*
* @return The [RSOrchestrator] instance.
*/
fun getInstance(): RSOrchestrator{
return RSAgent()
}
} //end of companion object
/**
* Handles the crafting process when standing at the bank.
*
* This method takes care of the workflow when standing at the bank:
* - Opens the bank interface using the bank preset hotkey.
* - Waits for the crafting duration.
* - Closes the bank interface.
*
* @param bankPoint The [Point] location of the bank on screen.
* @param bankPresetHotkey The hotkey used to open the bank interface.
* @param craftingDialogueHotkey The hotkey used to open the crafting dialogue.
* @param waitDurationMillis The base duration in milliseconds to wait while crafting.
* @param waitDurationVarianceMillis Random variance in milliseconds to add to the wait duration.
*/
fun processAtBank(
bankPoint: Point,
bankPresetHotkey: Int,
craftingDialogueHotkey: Int,
waitDurationMillis: Long,
waitDurationVariance: Long
)
/**
* Handles the crafting process when at a station near the bank.
*
* This method takes care of the workflow when at the crafting station:
* - Travels from the bank to the station.
* - Waits for the crafting duration.
* - Travels back to the bank when done.
*
* @param bankPoint The [Point] location of the bank.
* @param craftingStationPoint The [Point] location of the crafting station.
* @param bankPresetHotkey The hotkey to open the bank interface.
* @param travelDurationMillis The base travel time between bank and station.
* @param travelDurationVarianceMillis Random variance to add to the travel time.
* @param waitDurationMillis The base duration to wait while crafting.
* @param waitDurationVarianceMillis Random variance to add to the wait duration.
*/
fun processAtStationNearBank(
bankPoint: Point,
craftingStationPoint: Point,
bankPresetHotkey: Int,
travelDurationMillis: Long,
travelDurationVarianceMillis: Long,
waitDurationMillis: Long,
waitDurationVarianceMillis: Long
)
/**
* Gets the bank location point.
*
* @return The [Point] representing the x,y screen coordinates of the bank location.
*/
fun getBankPoint(): Point
}
/**
@ -277,31 +273,6 @@ private class RSAgent(override val automaton: Automaton = RobotController()) : R
automaton.sleepWithVariance(waitDurationMillis, waitDurationVarianceMillis)
}
/**
* Prompts the user to position the mouse and returns that position.
*
* This method prints a prompt message, then waits for the user to position
* the mouse.
*
* It then returns the current mouse position as a Point after a slight delay.
*
* The delay allows the mouse to settle before sampling its position.
*
* @param prompt The message to display to prompt user to position mouse.
* @return The Point position of the mouse after user positions it.
*/
override fun promptUserForPoint(prompt: String): Point {
println(prompt)
countDown(5) {
print("\rtaking point snapshot in $it... ")
if (it == 0) {
println("\r ")
}
}
return automaton.getPointerLocation()
}
/*==============================================================================================================
cheater functions
@ -324,100 +295,6 @@ private class RSAgent(override val automaton: Automaton = RobotController()) : R
return promptUserForPoint("Hold your mouse over the bank...")
}
/**
* Performs a countdown loop announcing each step.
*
* This method counts down from a provided number of seconds to 0.
*
* It calls the provided [announceFn] function on each step, passing the current
* step number.
* A sleep of 1 second is added between each step.
* Typical usage is to print a countdown message within [announceFn].
* @param nSeconds The number of seconds to count down.
* @param announceFn A callback function called each step, passed step number.
*/
fun countDown(nSeconds: Int, announceFn: (step: Int) -> Unit) {
for (i in nSeconds downTo 0) {
announceFn(i)
automaton.sleep(1000)
}
}
/**
* Gets the mouse pointer location after a delay.
*
* This method uses a countdown loop to wait for the specified number of
* seconds, printing a countdown as it goes.
*
* After the delay, it calls the automation library's getPointerLocation()
* method to retrieve the current mouse pointer coordinates.
*
* The delay helps ensure any prior mouse movements have settled before
* sampling the location.
*
* @param delayInSeconds The number of seconds to wait before sampling pointer location.
* @return The mouse pointer location after the delay as a Point.
*/
fun getPointerLocationAfter(delayInSeconds: Int): Point {
countDown(delayInSeconds) {
print("\rtaking pointer snapshot in $it...")
if (it == 0) {
println("\r ")
}
}
return automaton.getPointerLocation()
}
/**
* Gets the current mouse pointer location and returns it as a val declaration string.
*
* This method uses the [getPointerLocationAfter] method to get the current
* mouse pointer location after a small delay.
*
* It then formats this location into a string declaring a val with the provided
* variable name, like:
*
* ```
* val myPoint = Point(123, 456)
* ```
*
* The delay before getting the pointer location helps ensure the mouse has
* settled after any prior movements.
*
* @param varName The name to use for the variable in the declaration string.
* @return A string declaring a val with the pointer location.
*/
fun getPointerLocationAsValDeclarationString(varName: String): String {
val info = getPointerLocationAfter(5)
return "val $varName = Point(${info.x}, ${info.y})"
}
/**
* Moves the mouse to a point, left clicks, and sleeps.
*
* This method moves the mouse to the provided point location, left clicks
* at that location, sleeps for the specified duration plus a random variance.
*
* Typical usage is to provide a point where something needs to be clicked,
* along with a sleep duration to wait after clicking.
*
* The random variance in sleep time helps match real human actions.
*
* @param p The point location to move the mouse and left click.
* @param dur The base duration in ms to sleep after clicking.
* @param durRange The allowed variance in the sleep duration.
*/
fun moveMouseLeftClickAndSleep(p: Point, dur: Long, durRange: Long) {
automaton.moveMouse(p)
automaton.sleepWithVariance(100, 50)
//left click
automaton.mouseClick(InputEvent.BUTTON1_DOWN_MASK)
automaton.sleepWithVariance(dur, durRange)
}
/**
* Sleeps for a specified number of game ticks.
*
@ -435,30 +312,4 @@ private class RSAgent(override val automaton: Automaton = RobotController()) : R
automaton.sleepWithVariance(latencyPadding + baseWaitTime, 150)
}
/**
* Draws a star shape by moving the mouse between points.
*
* This method takes in a center point and calculates offset points around it.
* It then moves the mouse between each point in sequence to draw a star shape.
*
* The offset points are calculated by moving left/right and up/down from the
* center point by a fixed offset amount.
*
* @param p The center point of the star.
*/
override fun drawStar(p: Point) {
val offset = 100
val top = Point(p.x, p.y - offset * 2)
val topright = Point(p.x + offset * 2, p.y + offset)
val bottomright = Point(p.x + offset * 2, p.y)
val topleft = Point(p.x - offset * 2, p.y + offset)
val bottomleft = Point(p.x - offset * 2, p.y)
val points = arrayListOf(top, bottomleft, topright, topleft, bottomright)
for (i in 0 until 10) {
for (point in points) {
automaton.moveMouse(point)
automaton.sleep(32)
}
}
}
}