-
Notifications
You must be signed in to change notification settings - Fork 0
/
lobby_finder.py
106 lines (84 loc) · 3 KB
/
lobby_finder.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
from PIL import Image, ImageTk
import pyautogui
import time
import threading
import tkinter as tk
import sys
from os import path
class Properties:
BUTTON_OFFSET_PX = (0, -20)
RESOLUTION = [0, 0, 1920, 1080]
def print_log(txt):
global DEBUG_PRINT
if DEBUG_PRINT:
print(txt)
def getScreenshot(resolution, scale=1) -> Image:
# Takes a screenshot based on provided region.
screenshot = pyautogui.screenshot(region=resolution)
return screenshot
DEBUG_PRINT = True
SEARCH_LOBBY = False
# To work with the bundled app
if hasattr(sys, '_MEIPASS'):
base_path = path.join(sys._MEIPASS, "assets/")
else:
base_path = "./assets/"
root = tk.Tk()
root.title("League of Legends - lobby finder")
ico = Image.open(path.join(base_path, "lol_icon.ico"))
photo = ImageTk.PhotoImage(ico)
root.wm_iconphoto(False, photo)
languages = ["spanish", "english"]
buttons_images = [path.join(base_path, f"accept_button_{lng}.png") for lng in languages]
match_found_images = [Image.open(image) for image in buttons_images]
def start_lobby_search():
"""Run the search for lobby on a new thread."""
global SEARCH_LOBBY, THREAD_TASK
# To avoid starting several threads
if not SEARCH_LOBBY:
SEARCH_LOBBY = True
THREAD_TASK = threading.Thread(target=accept_lobby)
THREAD_TASK.start()
def stop_lobby_search():
"""Stops the thread searching for lobby."""
global SEARCH_LOBBY
try:
# Clean the variable
SEARCH_LOBBY = False
THREAD_TASK.join()
except NameError:
pass
def accept_lobby():
"""Search for a lobby."""
global SEARCH_LOBBY
start_time = time.time()
while SEARCH_LOBBY:
for match_found_image in match_found_images:
button_pos_aux = pyautogui.locateOnScreen(match_found_image, confidence=0.5)
if button_pos_aux:
button_pos = pyautogui.center(button_pos_aux) + Properties.BUTTON_OFFSET_PX
pyautogui.moveTo(button_pos)
pyautogui.click(button="left", clicks=1, interval=0.1)
print_log(f"Match founded! \t \
Elapsed time: {time.time()-start_time}")
SEARCH_LOBBY = False
break
time.sleep(1)
def on_closing():
"""Handle closing event."""
stop_lobby_search()
root.destroy()
btn_initialize = tk.Button(root, text="Search for lobby",
width=30, height=8, activebackground="green",
command=start_lobby_search)
btn_initialize.grid(column=0, row=0, sticky="nsew")
btn_stop = tk.Button(root, text="Stop searching lobby",
width=30, height=8, activebackground="green",
command=stop_lobby_search)
btn_stop.grid(column=1, row=0, sticky="nsew")
# To make the buttons resize them self
root.grid_columnconfigure([0,1], weight=1)
root.grid_rowconfigure(0, weight=1)
# Configure callback when closing the window
root.protocol("WM_DELETE_WINDOW", on_closing)
root.mainloop()