Untitled

mail@pastecode.io avatar
unknown
plain_text
2 years ago
11 kB
1
Indexable
Never
# -*- coding: utf-8 -*-
"""
Created on Thu Mar 23 00:09:06 2023

@author: salihcanturan
"""

# dogrulamaBOT değişkeni bağlı olmaksızın her zaman çalışır
dogrulamaBOT = True

# Katil metni için can barı koordinat screenshot[77:78, 679:680]
# metinBOT açık olsun mu, Metin konfigürasyonu yapılmalı
metinBOT = False
      
# dereceBOT açık olsun mu, Skill konfigürasyonu yapılması gerekebilir
dereceBOT = True  
 
# skillBOT 
skillBOT = False


from pytesseract import pytesseract
import threading
import pygetwindow   
from pynput.keyboard import Key, Controller
import win32api, win32con
from win32gui import GetWindowText, GetForegroundWindow
from PIL import ImageFont 
import pyautogui

import mss
import numpy as np
import cv2
import time
import pyuac
import keyboard
import pickle
import uuid
from unidecode import unidecode
from DatasetLoader import getItemIconDictionary
import re

path_to_tesseract = r'C:\Program Files\Tesseract-OCR\tesseract.exe'
pytesseract.tesseract_cmd = path_to_tesseract

print('Is user admin?', pyuac.isUserAdmin())
 
WINDOW_NAME = 'Harbi2 | shaula666'

titles = pygetwindow.getAllTitles()

program_active = 100 
stopApp = False 
screenshot = None 
dogrulama_screenshot = None
active_window = None 
resized = None
 
imageDataset = getItemIconDictionary()

controller = Controller()


def click(x, y):
    old = pyautogui.position()
    win32api.SetCursorPos((x,y)) 
    time.sleep(0.1) 
    win32api.mouse_event(win32con.MOUSEEVENTF_LEFTDOWN,x,y,0,0) 
    time.sleep(0.1)
    win32api.mouse_event(win32con.MOUSEEVENTF_LEFTUP,x,y,0,0)
    win32api.SetCursorPos(old)

def string_benzerligi(str1, str2):
    str1 = str1[:-4]
    str1 = str1.lower()
    str2 = str2.lower()
    
    str1 = re.sub('[^A-Za-z]+', '', unidecode(str1))
    str2 = re.sub('[^A-Za-z]+', '', unidecode(str2))
    
    toplam_benzerlik = 0
    for karakter in set(str1 + str2):
        toplam_benzerlik += min(str1.count(karakter), str2.count(karakter))
    return 100.0 * toplam_benzerlik / (len(str1) + len(str2)) + 1

def mse_benzerligi(img1, img2):
    return np.mean((img1[:,:,:3].astype("float") - img2.astype("float")) ** 2)

metinThreadDone = True
def metinBOTThread(region, ratio):
    global metinThreadDone
    metinThreadDone = False
    
    found = False
    c_x = len(screenshot) // 2
    c_y = len(screenshot[0]) // 2
    x = None
    y = None
    # Metin tespit kısa kodu tüm piksellere bakar.
    for i in range(100, len(screenshot)-50, 2):
        for j in range(0, len(screenshot[0]), 2):
            blue = screenshot[i, j][0]
            green = screenshot[i, j][1]
            red = screenshot[i, j][2]
            if abs(150 - blue) < 40 and abs(46 - green) < 20 and abs(95 - red) < 20:
                found = True
                if x == None:
                    x = int(region['left']+j*(1/ratio))
                    y = int(region['top']+i*(1/ratio))
                else:
                    old_dist = abs(c_y-y)**2 + abs(c_x-x)**2
                    curr_x = int(region['left']+j*(1/ratio))
                    curr_y = int(region['top']+i*(1/ratio))
                    if abs(c_x - curr_x) ** 2 + \
                       abs(c_y - curr_y) ** 2 < old_dist:
                        x = curr_x
                        y = curr_y
    
    if found:
        health_pixel = screenshot[77, 679, :]
        if health_pixel[0] == 19 and \
           health_pixel[1] == 19 and \
           health_pixel[2] == 187:
            click(x, y)
            time.sleep(2)
        else:
            click(x, y)
            time.sleep(2)
    else:
        controller.press('q')
        time.sleep(1.5)
        controller.release('q')
    
    metinThreadDone = True
     
def dereceBOTThread():
    # while True:
    #     controller.press('z')
    #     time.sleep(0.1)
    #     controller.release('z')
    #     time.sleep(0.1)
    # return
    
    global program_active
    ss = {'1': [0, 274],
          '2': [0, 174],
          '3': [0, 174]}
       
    curr_time = 0
    cooldown = 30
    
    while True and not stopApp and program_active > 0:
        if active_window != WINDOW_NAME:  
            controller.release(Key.space)
            continue 
        
        program_active -= 1
        controller.press(Key.space)
        controller.press('e')
        controller.press('w')
            
        if curr_time % 5  == 0:
            controller.press('4')
            time.sleep(0.1)
            for skill in ss:
                ss[skill][0] -= 0.1 
            controller.release('4')
            controller.press('1')
            time.sleep(0.1)
            controller.release('1')
            time.sleep(0.1)
            curr_time += 1
        if skillBOT:
            if cooldown > 0:
                cooldown -= 1
            else:
                for skill in ss:
                    if ss[skill][0] < 0 and cooldown <= 0:
                        controller.press('h')
                        time.sleep(0.2)
                        controller.release('h')
                        controller.press(skill)
                        time.sleep(0.2)
                        controller.release(skill)
                        controller.press('h')
                        time.sleep(0.2)
                        controller.release('h')
                        
                        for skill2 in ss:
                            ss[skill2][0] -= 0.4
                        
                        ss[skill][0] = ss[skill][1] 
                        print(ss[skill][0], ss[skill][1])
                        curr_time += 4
        
        curr_time += 1 
        time.sleep(0.1)
        for skill in ss: 
            ss[skill][0] -= 0.1
    
def updateScreenshotThread():
    global dogrulama_screenshot, screenshot 
    
    sct = mss.mss() 
    window = pygetwindow.getWindowsWithTitle(WINDOW_NAME)
    if len(window) == 0:
        return
    
    window = window[0]
    left, top = window.topleft
    right, bottom = window.bottomright
    
    region = {'top': top, 'left': left, 'width': right-left, 'height': bottom-top}
     
    raw_screenshot = sct.grab(region)
    raw_screenshot = np.array(raw_screenshot)[:,:,:3] 
     
    ratio = 1000 / raw_screenshot.shape[0]
     
    width = int(raw_screenshot.shape[1] * ratio)
    height = int(raw_screenshot.shape[0] * ratio + 0.001)
    
    raw_screenshot = cv2.resize(raw_screenshot, (width, height), interpolation=cv2.INTER_AREA)
    
    screenshot = raw_screenshot.copy()
    dogrulama_screenshot = raw_screenshot[203:446, 405:884, :]
    
    return ratio, region, np.sum(dogrulama_screenshot[20:30, 0:100]) / \
                          np.prod(dogrulama_screenshot[20:30, 0:100].shape) < 20


def mainThread():
    global stopApp, screenshot, active_window, metinThreadDone, resized, program_active     
    
    i = 0
    while True:
        program_active = 100  
        pack = updateScreenshotThread()
        if pack is None:
            time.sleep(0.5)
            continue

        ratio, region, validation_came = pack
        
        active_window = GetWindowText(GetForegroundWindow())
        
        if not validation_came:
            if metinThreadDone and active_window == WINDOW_NAME and metinBOT:
                metinThread = threading.Thread(target=metinBOTThread, args=(region, ratio,)).start()
                time.sleep(0.1)
            if resized is not None:
                cv2.imshow("DOGRULAMA TESPIT", resized)
            continue
        
        if screenshot is None:
            time.sleep(0.1)
            continue
        
        if active_window != WINDOW_NAME:
            print(GetWindowText(GetForegroundWindow()))
            cv2.imshow("DOGRULAMA TESPIT", resized)
            if cv2.waitKey(1) == ord('x'):
                stopApp=True
                cv2.destroyAllWindows()
                break 
            continue
        
        text_img = dogrulama_screenshot[5:40, 50:400, :]
        text = pytesseract.image_to_string(text_img, lang="tur")
        
        max_val = 0
        max_key = None
        for key in imageDataset:
            if max_val < string_benzerligi(key, text):
                max_val = string_benzerligi(key, text)
                max_key = key
        
        image_to_compare = imageDataset[max_key]

        resized = cv2.resize(dogrulama_screenshot, (dogrulama_screenshot.shape[1]*2, dogrulama_screenshot.shape[0]*2), interpolation=cv2.INTER_AREA)
        resized = cv2.rectangle(resized, (100, 10), ((800, 80)), (0, 255, 0), 1)
        
        slots = []
        for j in range(9):
            slots.append(cv2.resize(dogrulama_screenshot[45:165,10+j*52:50+j*52], (4, 12), interpolation=cv2.INTER_AREA))
        
        min_fark = 9999999
        match_idx = 0
        for idx, slot in enumerate(slots):
            if min_fark > mse_benzerligi(image_to_compare, slot):
                match_idx = idx
                min_fark = mse_benzerligi(image_to_compare, slot)
        
        resized = cv2.rectangle(resized, (124, 90), ((204, 330)), (0, 255, 0), -1)

        for j in range(9):
            resized[90:330, 20+104*j:100+104*j] = cv2.resize(slots[j], (80, 240), interpolation=cv2.INTER_AREA)
            
        for j in range(9):
            if j == match_idx:
                print('----------------')
                print(text.strip())
                print(max_key)
                print('----------------')
                click(int(region['left'] + (405 + 30+j*52) * (1/ratio)),
                      int(region['top'] + (203 + 105) * (1/ratio))
                      )
                time.sleep(0.1)
                
                cv2.rectangle(resized, (20+j*104, 90), ((100+j*104, 330)), (0, 255, 0), 5)
            else:
                cv2.rectangle(resized, (20+j*104, 90), ((100+j*104, 330)), (255, 0, 0), 1)
                
        font = cv2.FONT_HERSHEY_DUPLEX
        fontScale = 2
        thickness = 2
        cv2.putText(resized, re.sub('[^A-Za-z]+', '', unidecode(text)), (200, 70), font, 
                    fontScale, (0,255,0), thickness, cv2.LINE_AA)
        
        cv2.putText(resized, unidecode(max_key), (100, 400), font, 
                    fontScale, (0,255,0), thickness, cv2.LINE_AA)
        
        cv2.imshow("DOGRULAMA TESPIT", resized)

        if cv2.waitKey(1) == ord('x'):
            cv2.destroyAllWindows()
            stopApp=True
            break
        i += 1

if dereceBOT:
    threading.Thread(target=dereceBOTThread).start()
    
mainThread()