Skip to content

Commit

Permalink
Merge pull request #1 from lippielip/dev
Browse files Browse the repository at this point in the history
reliabilty, output and error handling improvements
  • Loading branch information
lippielip authored Jan 20, 2022
2 parents 9649bf2 + f8e5107 commit 71cc49e
Showing 1 changed file with 31 additions and 24 deletions.
55 changes: 31 additions & 24 deletions src/antiafk.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,29 @@
import math
import os

############################################################
################ USER CHANGEABLE AREA ##################
######## MODIFY THESE VALUES TO YOUR LIKING ############
############################################################

GAME_TITLE = "FINAL FANTASY XIV"
LOADING_BAR_SCALE = 45
# the speed at which the timer refreshes [1 being once a second, 10 = 10 per second...]
TIME_GRANULARITY = 10

LOADING_BAR_SCALE = 30
MIN_DELAY = 120 * TIME_GRANULARITY
MAX_DELAY = 300 * TIME_GRANULARITY
SLEEP_TIME = 1/TIME_GRANULARITY

KEYS = {1:'a',
2: 'd',
3: 'w',
4: 's'}

############################################################
############################################################
############################################################

SLEEP_TIME = 1/TIME_GRANULARITY
global activated
activated = False

Expand Down Expand Up @@ -54,6 +64,7 @@ def get_current_window():
except IndexError:
return


def get_game_window():
try:
gameWindow = window.getWindowsWithTitle(GAME_TITLE)[0]
Expand All @@ -66,15 +77,20 @@ def activate_game_window(gameWindow):
if is_admin():
try:
gameWindow.activate()
except:
except AttributeError:
print("\u001b[31m[ERROR]:\033[0m Game client not found\n")
return
except BaseException as err:
print("\u001b[31m[ERROR]:\033[0m {}\n".format(err))
return


def return_to_original_window(originalWindow):
if is_admin():
try:
originalWindow.activate()
except:
except BaseException as err:
print("\u001b[31m[ERROR]:\033[0m {}\n".format(err))
return


Expand All @@ -101,53 +117,52 @@ def return_remaining_time(remainingTime):


def execute_movement():
rand_key = random.randint(1,4)
rand_key = random.randint(1, KEYS.__len__())
pyautogui.keyDown(KEYS[rand_key])
pyautogui.sleep(0.1)
pyautogui.keyUp(KEYS[rand_key])


def toggle_activated():
global activated

# toggles activation state
activated = not activated
# clear the screen
clear()

# display message of automatic window focus not working
if not is_admin():
print("\u001b[31mNot running as admin. Automatic window focus will not work.\033[0m")

# set the current state
activation_text = "\033[93minactive\033[0m"
if activated:
activation_text = "\033[92mactive\033[0m"

# print the current state
print("Anti-AFK Bot is: {}\n".format(activation_text))

# prevent multiple executions from spamming the hotkey
# TODO: implement correct Thread locking instead of checking for active threads
if threading.active_count() > 2:
return
threading.Thread(target=main).start()
threading.Thread(target=execution_loop).start()


def main():
def execution_loop():
# resets timer
timer = 0
# set an inital interval of 3 seconds to test
rand_interval = 3 * TIME_GRANULARITY
# repeat until hotkey is used

gamewindow = get_game_window()
while activated:
# output progress & execute movement
# output the progress bar
sys.stdout.write('\r')
gamewindow = get_game_window()
if not gamewindow:
gamewindow = get_game_window()
# check if the interval has been reached otherwise progress the progress bar
if (rand_interval - timer == 0):
sys.stdout.write("[{:{}}] Done.\033[K\n".format("="* ((timer * LOADING_BAR_SCALE) / rand_interval).__round__() , LOADING_BAR_SCALE))
sys.stdout.write("[{:{}}] Done\033[K\n".format("="* ((timer * LOADING_BAR_SCALE) / rand_interval).__round__() , LOADING_BAR_SCALE))

#get the current window the user is on
currentWindow = get_current_window()
Expand All @@ -163,7 +178,7 @@ def main():
timer = 0
rand_interval = random.randint(MIN_DELAY,MAX_DELAY)
print('')
print('Next input in {}'.format(return_remaining_time(rand_interval - timer)))
print('\033[93m[INFO]:\033[0m Next input in {}'.format(return_remaining_time(rand_interval - timer)))

else:
# output the progress bar
Expand All @@ -173,21 +188,13 @@ def main():
timer = timer + 1
time.sleep(SLEEP_TIME)

############################################################
################ USER CHANGEABLE AREA ##################
######## MODIFY KEYCODE AND/OR MODIFIER KEY ############
############################################################

combination_to_function = {
# duplicate this if you want to add more hotkey combos
# duplicate this if you want to add more hotkey combos && replace the vk value or modifier key
# unsure of your preferred keycode? Use this site: https://keycode.info/
frozenset([Key.shift, KeyCode(vk=48)]): toggle_activated,
}

############################################################
############################################################
############################################################

# The currently pressed keys (initially empty)
pressed_vks = set()

Expand Down Expand Up @@ -218,7 +225,6 @@ def on_press(key):
# If so, execute the function in a new thread



def on_release(key):
""" When a key is released """
try:
Expand All @@ -227,6 +233,7 @@ def on_release(key):
except KeyError:
pass


clear()
print('Anti-AFK Bot initialized. Enable/Disable with Shift+0')

Expand Down

0 comments on commit 71cc49e

Please sign in to comment.