Skip to content

Commit

Permalink
Check that lastID exists to prevent crash
Browse files Browse the repository at this point in the history
  • Loading branch information
Jared K committed Nov 2, 2022
1 parent 4801edd commit 461024f
Showing 1 changed file with 77 additions and 66 deletions.
143 changes: 77 additions & 66 deletions AFB.ahk
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,14 @@
/*
constants
*/
ONE_MIN := 1000 * 60
ONE_MIN := 60 * 1000
THREE_MINS := 3 * ONE_MIN
FIVE_MINS := 5 * ONE_MIN
SIXTEEN_MINS := 15 * ONE_MIN
DATA_DIR := A_WorkingDir . "\data"
RECONNECT_BUTTON_IMG := DATA_DIR . "\reconnect-button.png"
RECONNECT_TEXT_IMG := DATA_DIR . "\reconnect-text.png"
FIFTEEN_MINS := 15 * ONE_MIN

DATA_DIR := A_WorkingDir . "\data"
RECONNECT_BUTTON_IMG := DATA_DIR . "\reconnect-button.png"
RECONNECT_TEXT_IMG := DATA_DIR . "\reconnect-text.png"
RECONNECT_IMG_SEARCH_ARRAY := [RECONNECT_BUTTON_IMG, RECONNECT_TEXT_IMG]

DirCreate(DATA_DIR)
Expand All @@ -20,13 +21,13 @@ FileInstall("data\reconnect-text.png", RECONNECT_TEXT_IMG, 1)
globals
*/
IsRunning := false
TrayTip("Script is ready for input. AntiKick is disabled.", "AwayFromBlox")
SetTrayTip("Script is ready for input. Anti-kick is disabled.")

#MaxThreadsPerHotkey 2
F1:: {
global IsRunning := !IsRunning
If (IsRunning) {
TrayTip("AntiKick is enabled.", "AwayFromBlox")
SetTrayTip("Anti-kick is enabled.")
} Else {
Reload()
}
Expand All @@ -35,7 +36,7 @@ F1:: {
lastID := WinGetID("A")
robloxID := WinExist("Roblox")
if (robloxID) {
; BEGIN WORK
; BEGIN WORK - any automation must be done between the BlockInputs
BlockInput(True)

WinActivate(robloxID)
Expand All @@ -45,7 +46,9 @@ F1:: {

BreakAFK()

WinActivate(lastID)
if (lastID) {
WinActivate(lastID)
}
BlockInput(False)
; END OF WORK

Expand All @@ -55,68 +58,76 @@ F1:: {
sleep(FIVE_MINS)
}
}
}

/*
Core logic for "breaking" afk idle timer
*/
BreakAFK() {
; TODO allow action to be defined by user upon startup and/or config
send("{Space down}")
sleep(100)
send("{Space up}")
}
/*
Core logic for "breaking" afk idle timer
*/
BreakAFK() {
sleep(300)
; TODO allow action to be defined by user upon startup and/or config
send("{Space down}")
sleep(50)
send("{Space up}")
sleep(500)
send("{Space down}")
sleep(50)
send("{Space up}")
}

/*
Attempts to reconnect the user if the reconnection box is detected
*/
Reconnect(winWidth, winHeight) {
try {
for (searchImage in RECONNECT_IMG_SEARCH_ARRAY) {
if (ImageSearch(&foundX, &foundY, 0, 0, winWidth, winHeight, searchImage)) {
ClickImageMidPoint(searchImage, foundX, foundY)
return 1
/*
Attempts to reconnect the user if the reconnection box is detected
*/
Reconnect(winWidth, winHeight) {
try {
for (searchImage in RECONNECT_IMG_SEARCH_ARRAY) {
if (ImageSearch(&foundX, &foundY, 0, 0, winWidth, winHeight, searchImage)) {
ClickImageMidPoint(searchImage, foundX, foundY)
return 1
}
}
} catch as err {
MsgBox "Something went wrong when trying to check for reconnect button:`n" err.Message
Reload
}
} catch as exc {
MsgBox "Something went wrong when trying to check for reconnect button:`n" exc.Message
Reload
return 0
}
return 0
}

/*
Returns a random time between 3mins and 15mins
(avg 9mins: 1/2 AFK timer - 1 (this is for redundancy))
*/
GetIntervalMins() {
randomMins := Random(THREE_MINS, SIXTEEN_MINS)
return randomMins
}
/*
Returns a random time between 3mins and 15mins
(avg 9mins: 1/2 AFK timer - 1 (this is for redundancy))
*/
GetIntervalMins() {
randomMins := Random(THREE_MINS, FIFTEEN_MINS)
return randomMins
}

/*
Returns the middle of coordinates of an image
Alternative to loading GDI+ lib: https://www.autohotkey.com/boards/viewtopic.php?p=445556#p445556
*/
GetImageMidPoint(file) {
imgGui := Gui()
img := imgGui.Add("Picture",, file)
imgGui.Show("Hide")
ControlGetPos(,, &w, &h, img.hwnd)
imgGui.Destroy()
return [w/2, h/2]
}
/*
Returns the middle of coordinates of an image
Alternative to loading GDI+ lib: https://www.autohotkey.com/boards/viewtopic.php?p=445556#p445556
*/
GetImageMidPoint(file) {
imgGui := Gui()
img := imgGui.Add("Picture",, file)
imgGui.Show("Hide")
ControlGetPos(,, &w, &h, img.hwnd)
imgGui.Destroy()
return [w/2, h/2]
}

/*
Clicks the middle of an image with an offset
*/
ClickImageMidPoint(imageFile, xOffset, yOffset){
imageMidPoint := GetImageMidPoint(imageFile)
clickX := imageMidPoint[1] + xOffset
clickY := imageMidPoint[2] + yOffset

; ! Clicks seems to be flakey, this is why using two Click() instead of the 3rd parameter
Click(clickX, clickY)
sleep(350)
Click(clickX, clickY)
}
/*
Clicks the middle of an image with an offset
*/
ClickImageMidPoint(imageFile, xOffset, yOffset){
imageMidPoint := GetImageMidPoint(imageFile)
clickX := imageMidPoint[1] + xOffset
clickY := imageMidPoint[2] + yOffset

; ! Clicks seems to be flakey, this is why using two Click() instead of the 3rd parameter
Click(clickX, clickY)
sleep(350)
Click(clickX, clickY)
}

SetTrayTip(str, title := "AwayFromBlox") {
TrayTip(str, title)
}

0 comments on commit 461024f

Please sign in to comment.