Advertisement
Guest User

Untitled

a guest
Jan 19th, 2019
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 6.63 KB | None | 0 0
  1. #! python3
  2. from PIL import Image
  3. import os, sys, time, random, pytesseract, pyautogui
  4.  
  5. print('At any time, press Ctrl-C to quit the bot.\n')
  6.  
  7. clientDetected = False
  8. clientPlaying = False
  9.  
  10. pid = None
  11. window  = None
  12. actualDesktop   = None
  13. windowDesktop   = None
  14. displaySize = None
  15. windowSize  = None
  16. windowOffset    = None
  17.  
  18. def getDirectionAxis(fromX, fromY, toX, toY):
  19.     if (toX == fromX):
  20.         if (toY < fromY):
  21.             return  0
  22.         else:
  23.             return 4
  24.     else:
  25.         if (toY == fromY):
  26.             if (toX < fromX):
  27.                 return 6
  28.             else:
  29.                 return 2
  30.         else:
  31.             if (toX < fromX):
  32.                 if (toY < fromY):
  33.                     return 7
  34.                 else:
  35.                     return 5
  36.             else:
  37.                 if (toY < fromY):
  38.                     return 1
  39.                 else:
  40.                     return 3
  41.  
  42. def getDirName(directionAxis):
  43.     return {
  44.         0: 'N',
  45.         1: 'NE',
  46.         2: 'E',
  47.         3: 'SE',
  48.         4: 'S',
  49.         5: 'SW',
  50.         6: 'W',
  51.         7: 'NW',
  52.     }[directionAxis]
  53.  
  54. def getPid(program):
  55.     os.system('(pidof ' + program + ') > .tmp')
  56.     pid = open('.tmp', 'r').read()
  57.     return pid.strip()
  58.  
  59. def getWindow(pid):
  60.     os.system('(xdotool search --pid ' + pid + ') > .tmp')
  61.     window = open('.tmp', 'r').read()
  62.     return window.strip()
  63.  
  64. def getActualDesktop():
  65.     os.system('(xdotool get_desktop) > .tmp')
  66.     desktop = open('.tmp', 'r').read()
  67.     return desktop.strip()
  68.  
  69. def goToDesktop(desktop):
  70.     os.system('(xdotool set_desktop ' + desktop + ') > .tmp')
  71.     return True
  72.  
  73. def getWindowDesktop(window):
  74.     os.system('(xdotool get_desktop_for_window ' + window + ') > .tmp')
  75.     windowDesktop = open('.tmp', 'r').read()
  76.     return windowDesktop.strip()
  77.  
  78. def setFocus(window):
  79.     os.system('(xdotool windowfocus ' + window + ') > .tmp')
  80.     os.system('(xdotool windowactivate ' + window + ') > .tmp')
  81.     return True
  82.  
  83. def maximizeWindow(window):
  84.     os.system('(xdotool windowmove ' + window + ' 0 0) > .tmp')
  85.     os.system('(xdotool windowsize ' + window + ' $(xdotool getdisplaygeometry)) > .tmp')
  86.     os.system('(xdotool windowactivate ' + window + ') > .tmp')
  87.     return True
  88.  
  89. def minimizeWindow(window):
  90.     os.system('(xdotool windowminimize ' + window + ') > .tmp')
  91.     return True
  92.  
  93. def setWindowPos(window, x, y):
  94.     os.system('(xdotool windowmove ' + window + ' ' + str(x) + ' ' + str(y) +') > .tmp')
  95.     return True
  96.     return True
  97.  
  98. def resizeWindow(window, x, y):
  99.     os.system('(xdotool windowsize ' + window + ' ' + str(x) + ' ' + str(y) + ') > .tmp')
  100.     return True
  101.  
  102. def getWindowTitle(window):
  103.     os.system('(xdotool getwindowname ' + window + ') > .tmp')
  104.     windowTitle = open('.tmp', 'r').read()
  105.     return windowTitle.strip()
  106.  
  107. def getWindowsOffset(window):
  108.     os.system('(xdotool getwindowgeometry --shell ' + window + ' | grep X=) > .tmp')
  109.     windowX = open('.tmp', 'r').read().replace('X=', '')
  110.  
  111.     os.system('(xdotool getwindowgeometry --shell ' + window + ' | grep Y=) > .tmp')
  112.     windowY = open('.tmp', 'r').read().replace('Y=', '')
  113.     return [int(windowX), int(windowY)]
  114.  
  115. def getWindowsSize(window):
  116.     os.system('(xdotool getwindowgeometry --shell ' + window + ' | grep WIDTH=) > .tmp')
  117.     windowWIDTH = open('.tmp', 'r').read().replace('WIDTH=', '')
  118.  
  119.     os.system('(xdotool getwindowgeometry --shell ' + window + ' | grep HEIGHT=) > .tmp')
  120.     windowHEIGHT = open('.tmp', 'r').read().replace('HEIGHT=', '')
  121.     return [int(windowWIDTH), int(windowHEIGHT)]
  122.  
  123. def getDisplaySize():
  124.     os.system('(xdotool getdisplaygeometry --shell | grep WIDTH=) > .tmp')
  125.     windowWIDTH = open('.tmp', 'r').read().replace('WIDTH=', '')
  126.  
  127.     os.system('(xdotool getdisplaygeometry --shell | grep HEIGHT=) > .tmp')
  128.     windowHEIGHT = open('.tmp', 'r').read().replace('HEIGHT=', '')
  129.     return [int(windowWIDTH), int(windowHEIGHT)]
  130.  
  131. def clearImage(image):
  132.     pixels = image.load()
  133.  
  134.     for i in range(image.size[0]):
  135.         for j in range(image.size[1]):
  136.             x,y,z = pixels[i,j][0],pixels[i,j][1],pixels[i,j][2]
  137.             x,y,z = abs(x-255), abs(y-255), abs(z-255)
  138.  
  139.             # Black Range
  140.             if (x >= 100 and x <= 254):
  141.                 if (y >= 100 and y <= 254):
  142.                     if (y >= 100 and y <= 254):
  143.                         x,y,z=255,255,255
  144.  
  145.             pixels[i,j] = (x,y,z)
  146.    
  147.     width, height = image.size
  148.     image = image.resize(((width * 2), (height * 2)), Image.NEAREST)
  149.  
  150.     return image
  151.  
  152. def detectGameOpen():
  153.     global clientDetected, clientPlaying
  154.     clientDetected = False
  155.     clientPlaying = False
  156.  
  157.     pid = getPid('Tibia/client')
  158.     if (pid != False and pid != ""):
  159.         window = getWindow(pid)
  160.         if (window != False and window != ""):
  161.             clientDetected = True
  162.             title = getWindowTitle(window).split(" - ")
  163.             if (len(title) == 2):
  164.                 clientPlaying = True
  165.  
  166. def manageWindows():
  167.     global pid, window, actualDesktop, windowDesktop, displaySize, windowSize, windowOffset
  168.  
  169.     pid = getPid('Tibia/client')
  170.     window = getWindow(pid)
  171.  
  172.     actualDesktop = getActualDesktop()
  173.     windowDesktop = getWindowDesktop(window)
  174.  
  175.     if (actualDesktop != windowDesktop):
  176.         goToDesktop(windowDesktop)
  177.         time.sleep(1)
  178.  
  179.     displaySize = getDisplaySize()
  180.     windowSize = getWindowsSize(window)
  181.  
  182.     # setWindowPos(window, random.randint(0,100), random.randint(0,100))
  183.     # time.sleep(1)
  184.  
  185.     # if (windowSize[0] != 1024 and windowSize[1] != 768):
  186.     #   resizeWindow(window, 1024, 768)
  187.     #   time.sleep(1)
  188.  
  189.     windowOffset = getWindowsOffset(window)
  190.  
  191.     setFocus(window)
  192.     time.sleep(2)
  193.  
  194. def start():
  195.     while True:
  196.         print("clientDetected: ", clientDetected)
  197.         print("clientPlaying: ", clientPlaying)
  198.  
  199.         if (clientDetected == False):
  200.             print('Detecting Tibia Client')
  201.             detectGameOpen()
  202.  
  203.         if (clientDetected == False and clientPlaying == False):
  204.             print('Could not find the Tibia Client');
  205.             time.sleep(15)
  206.        
  207.         if (clientDetected == True and clientPlaying == False):
  208.             print('Account not logged')
  209.             time.sleep(15)
  210.  
  211.         if (clientDetected == True and clientPlaying == True):
  212.             return False
  213.  
  214. # main start
  215. try:
  216.     start()
  217.     manageWindows()
  218.  
  219.     dialog = pyautogui.locateOnScreen('./images/store_button.png');
  220.  
  221.     if (dialog != None):
  222.         dialogX = dialog[0]
  223.         dialogY = dialog[1]
  224.  
  225.         while True:
  226.             # pega poção
  227.             pyautogui.moveTo(202, 93, 1, pyautogui.easeInBounce)
  228.             pyautogui.click(button='left', clicks=1, interval=0.0)
  229.             pyautogui.dragTo(1827, 603, 1, button='left')
  230.  
  231.             time.sleep(0.25)
  232.  
  233.             # Consome as 100 poções usando magia
  234.             for x in range(0,100):
  235.                 pyautogui.press('f9')
  236.                 time.sleep(2.1)
  237.                 pyautogui.press('f9')
  238.                 time.sleep(2.1)
  239.                 pyautogui.press('f2')
  240.                 time.sleep(0.25)
  241.                 # pass
  242.  
  243.             # Guarda os frascos vazios     
  244.             pyautogui.moveTo(1770, 541, 1, pyautogui.easeInBounce)
  245.             pyautogui.click(button='left', clicks=1, interval=0.0)
  246.             pyautogui.dragTo(255, 534, 1, button='left')
  247.  
  248.             time.sleep(0.25)
  249.     else:
  250.         print("Client not detected")
  251.  
  252.  
  253. except KeyboardInterrupt:
  254.     print('\n\nBot stoped...\n\n')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement