Skip to content

Commit

Permalink
Merge pull request #1 from SivWatt/Refactor
Browse files Browse the repository at this point in the history
Refactor
  • Loading branch information
SivWatt authored Feb 23, 2020
2 parents 5074f10 + 5664623 commit 4667eb7
Show file tree
Hide file tree
Showing 11 changed files with 202 additions and 147 deletions.
335 changes: 196 additions & 139 deletions League.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,113 +12,229 @@
import tkinter
from tkinter import ttk


# variables used in report script
teamY = [ 130, 165, 200, 235 ]
enemyY = [ 310, 345, 380, 415, 450 ]
cwd = os.path.dirname(sys.argv[0]) if os.path.dirname(sys.argv[0]) != "" else "."
debuglog = open('debuglog.log', 'w')

class PreloadImage:
def loadFolderImage(path):
reportIcons = listdir(path)
###########################################################
# Class definitions:

# new class for storing images like report icons, checkboxes...
class Images:
# static variables
checkboxIm = None
commentTextIm = None
cancelIm = None
reportConfirmIm = None
reportButtonIm = None
leagueIm = None
reportText = None

# load all the static variables
@staticmethod
def initialize(cwd):
Images.checkboxIm = Image.open(cwd + '/image/' + 'checkbox.PNG')
Images.commentTextIm = Image.open(cwd + '/image/' + 'comment.PNG')
Images.cancelIm = Image.open(cwd + '/image/' + 'cancel.PNG')
Images.reportConfirmIm = Image.open(cwd + '/image/' + 'reportConfirm.PNG')
Images.reportButtonIm = Images.loadImageFromFolder(cwd + '/image/reportIcon')
Images.leagueIm = Images.loadImageFromFolder(cwd + '/image/AramIcon')
with open(cwd + '/' + 'reportText.txt', encoding='utf8') as file:
Images.reportText = file.read()

# static method that load images from folder.
@staticmethod
def loadImageFromFolder(path):
files = listdir(path)
images = []
for i in reportIcons:
for i in files:
im = Image.open(path + '/' + i)
images.append(im)

return images
# end of class Image

def windowEnumerationHandler(hwnd, top_windows):
top_windows.append((hwnd, win32gui.GetWindowText(hwnd)))
# class for storing window rect and handler
class WindowProperty:
#static variables
hwnd = None
rect = None

def getWindowHndlr(f):
top_windows = []
win32gui.EnumWindows(f, top_windows)
for i in top_windows:
if "league of legend" in i[1].lower():
#win32gui.ShowWindow(i[0], 9)
#win32gui.SetForegroundWindow(i[0])
break
return i[0]
# method passed to win32gui
@staticmethod
def windowEnumHandler(hwnd, top_windows):
top_windows.append((hwnd, win32gui.GetWindowText(hwnd)))

def getWindowSize(hwnd):
rect = win32gui.GetWindowRect(hwnd)
return (rect[0], rect[1], rect[2]-rect[0], rect[3]-rect[1])

# bring league window to the front
@staticmethod
def bringFront(hwnd):
win32gui.ShowWindow(hwnd, 9)
win32gui.SetForegroundWindow(hwnd)

checkboxIm = Image.open(cwd + '/image/' + 'checkbox.PNG')
commentTextIm = Image.open(cwd + '/image/' + 'comment.PNG')
cancelIm = Image.open(cwd + '/image/' + 'cancel.PNG')
reportComfirmIm = Image.open(cwd + '/image/' + 'reportConfirm.PNG')
with open(cwd + '/' + 'reportText.txt', encoding='utf8') as file:
reportText = file.read()
reportButtonIms = loadFolderImage(cwd + '/image/reportIcon')
leaugeIcons = loadFolderImage(cwd + '/image/AramIcon')
windowHndlr = getWindowHndlr(windowEnumerationHandler)
windowSize = getWindowSize(windowHndlr)


def findLeagueIcon(windowSize):
for im in PreloadImage.leaugeIcons:
icon = pyautogui.locateCenterOnScreen(im, region=windowSize, confidence=0.9)
# initialize the static variables
@staticmethod
def initialize():
top_windows = []
win32gui.EnumWindows(WindowProperty.windowEnumHandler, top_windows)
for i in top_windows:
if "league of legend" in i[1].lower():
DLOG('find window: {}'.format(i[1].lower()))
WindowProperty.hwnd = i[0]
r = win32gui.GetWindowRect(i[0])
WindowProperty.rect = (r[0], r[1], r[2] - r[0], r[3] - r[1])
break
# end of class WindowProperty

# a class to store y-axis value
class Coordinate:
teamY = [ 130, 165, 200, 235 ]
enemyY = [ 310, 345, 380, 415, 450 ]
# end of class Coordinate

# end of Class definitions.
###########################################################

# write log to log file
def DLOG(msg):
file = open('debuglog.log', 'a')
file.write(time.strftime('%Y-%m-%d %H:%M:%S -\t', time.localtime()))
file.write(msg + '\n')
file.close()
# end of log()

# find league icon within a region, and return the position.
def findLeagueIcon(leaguRegion):
for im in Images.leagueIm:
icon = pyautogui.locateCenterOnScreen(im, region=leaguRegion, confidence=0.9)
if icon:
debuglog.write('find aram\n')
#DLOG('[findLeagueIcon] find league icon on window')
break
return icon
# end of findLeagueIcon()

# tkinter initialize
window = tkinter.Tk()
window.title('水水牌檢舉器')
window.configure(background='white')
window.iconbitmap(cwd + "/image/rsc/window_icon.ico")
window.geometry("+1600+700")

top_frame = ttk.Frame(window)
top_frame.pack()
bottom_frame = ttk.Frame(window)
bottom_frame.pack(side=tkinter.BOTTOM)
# end of tkinter initialize
# get a list of random numbers
def myRandom():
seed(time.time())
randomNumbers = []
for i in range(0,3):
rn = randint(0,6)
while rn in randomNumbers:
rn = randint(0,6)
randomNumbers.append(rn)
return randomNumbers
# end of myRandom()

###########################################################
# Button functions
def reportTeam():
PreloadImage.bringFront(PreloadImage.windowHndlr)
AramIcon = findLeagueIcon(PreloadImage.windowSize)
ys = teamY
WindowProperty.bringFront(WindowProperty.hwnd)
leagueIcon = findLeagueIcon(WindowProperty.rect)

if AramIcon:
for y in ys:
reportAPlayer(AramIcon.x, AramIcon.y + y)
DLOG('reportTeam() >>>')
if leagueIcon:
for y in Coordinate.teamY:
reportAPlayer(leagueIcon.x, leagueIcon.y + y)
else:
debuglog.write('aram not found\n')
DLOG('[reportTeam] League icon is not found')
DLOG('reportTeam() <<<')

def reportEnemy():
PreloadImage.bringFront(PreloadImage.windowHndlr)
AramIcon = findLeagueIcon(PreloadImage.windowSize)
ys = enemyY
WindowProperty.bringFront(WindowProperty.hwnd)
leaguIcon = findLeagueIcon(WindowProperty.rect)

if AramIcon:
for y in ys:
reportAPlayer(AramIcon.x, AramIcon.y + y)
DLOG('reportEnemy() >>>')
if leaguIcon:
for y in Coordinate.enemyY:
reportAPlayer(leaguIcon.x, leaguIcon.y + y)
else:
debuglog.write('aram not found\n')
DLOG('[reportEnemy] League icon is not found')
DLOG('reportEnemy() <<<')

def reportAll():
PreloadImage.bringFront(PreloadImage.windowHndlr)
AramIcon = findLeagueIcon(PreloadImage.windowSize)
ys = teamY + enemyY
WindowProperty.bringFront(WindowProperty.hwnd)
leaguIcon = findLeagueIcon(WindowProperty.rect)

DLOG('reportAll() >>>')
ys = Coordinate.teamY + Coordinate.enemyY

if AramIcon:
if leaguIcon:
for y in ys:
reportAPlayer(AramIcon.x, AramIcon.y + y)
reportAPlayer(leaguIcon.x, leaguIcon.y + y)
else:
debuglog.write('aram not found\n')
DLOG('[reportAll] League icon is not found')
DLOG('reportAll() <<<')

def closeWindow():
window.destroy()
# end of button functions
###########################################################

def reportAPlayer(posX, posY):
pyautogui.moveTo(posX, posY)

for im in Images.reportButtonIm:
reportButton = pyautogui.locateCenterOnScreen(im, region=(posX, posY - 20, 300, 100), confidence=0.8, grayscale=True)
if reportButton:
break

if reportButton:
#pyautogui.moveTo(reportButton)
pyautogui.mouseDown(reportButton,button='left',duration=1.0)
pyautogui.mouseUp(reportButton,button='left')

#locate report check boxes
checkboxes = list(pyautogui.locateAllOnScreen(Images.checkboxIm, region=WindowProperty.rect, confidence=0.7))

if checkboxes:
# get random numbers
rn = myRandom()

# click check boxes
for i in rn:
pyautogui.click(checkboxes[i])

# paste report text to comment text field
commentTextField = pyautogui.locateCenterOnScreen(Images.commentTextIm, region=WindowProperty.rect)
if commentTextField:
pyautogui.click(commentTextField)
pyperclip.copy(Images.reportText)
pyautogui.hotkey('ctrl','v')

# cancel report for testing
#cancel = pyautogui.locateCenterOnScreen(Images.cancelIm, region=WindowProperty.rect)
#if cancel:
# pyautogui.click(cancel)
# return

# press report confirm button
reportConfirm = pyautogui.locateCenterOnScreen(Images.reportConfirmIm, region=WindowProperty.rect)
if reportConfirm:
pyautogui.moveTo(reportConfirm)
pyautogui.click(reportConfirm)
else:
DLOG('[reportAPlater] checkbox is not detected')
# cancel report and go on.
cancel = pyautogui.locateCenterOnScreen(Images.cancelIm, region=WindowProperty.rect)
if cancel:
pyautogui.click(cancel)
return
else:
DLOG('[reportAPlayer]report fail')


###########################################################
# main script starts:

# get the current working directory
cwd = os.path.dirname(sys.argv[0]) if os.path.dirname(sys.argv[0]) != "" else "."

# tkinter initialize
window = tkinter.Tk()
window.title('水水牌檢舉器')
window.configure(background='white')
window.iconbitmap(cwd + "/image/rsc/window_icon.ico")
window.geometry("+1600+700")

top_frame = ttk.Frame(window)
top_frame.pack()
bottom_frame = ttk.Frame(window)
bottom_frame.pack(side=tkinter.BOTTOM)
# end of tkinter initialize

# top frame
left_style = ttk.Style()
Expand All @@ -142,70 +258,11 @@ def closeWindow():
bottom_button = ttk.Button(text='Exit', style='B4.TButton', command = closeWindow)
bottom_button.pack(side=tkinter.BOTTOM)


def myRandom():
seed(time.time())
randomNumbers = []
for i in range(0,3):
rn = randint(0,6)
while rn in randomNumbers:
rn = randint(0,6)
randomNumbers.append(rn)
return randomNumbers



def reportAPlayer(posX, posY):
pyautogui.moveTo(posX, posY)

for im in PreloadImage.reportButtonIms:
reportButton = pyautogui.locateCenterOnScreen(im, region=(posX, posY - 20, 300, 100), confidence=0.8, grayscale=True)
if reportButton:
break

if reportButton:
#pyautogui.moveTo(reportButton)
pyautogui.mouseDown(reportButton,button='left',duration=1.0)
pyautogui.mouseUp(reportButton,button='left')

#locate report check boxes
checkboxes = list(pyautogui.locateAllOnScreen(PreloadImage.checkboxIm, region=PreloadImage.windowSize, confidence=0.7))

#TODO optimize here
if checkboxes:
pass
else:
return
# get random numbers
rn = myRandom()

# click check boxes
for i in rn:
pyautogui.click(checkboxes[i])

commentTextField = pyautogui.locateCenterOnScreen(PreloadImage.commentTextIm, region=PreloadImage.windowSize)
if commentTextField:
pyautogui.click(commentTextField)
pyperclip.copy(PreloadImage.reportText)
pyautogui.hotkey('ctrl','v')

# cancel report for testing
#cancel = pyautogui.locateCenterOnScreen(PreloadImage.cancelIm, region=PreloadImage.windowSize)
#if cancel:
# pyautogui.click(cancel)

reportConfirm = pyautogui.locateCenterOnScreen(PreloadImage.reportComfirmIm, region=PreloadImage.windowSize)
if reportConfirm:
pyautogui.moveTo(reportConfirm)
pyautogui.click(reportConfirm)
else:
debuglog.write('report fail\n')


#################################################
# main script:
# class initialization
Images().initialize(cwd)
WindowProperty().initialize()

window.mainloop()
debuglog.close()

#################################################
# main script ends.
###########################################################
Binary file modified RunReportTool.exe
Binary file not shown.
Binary file removed src/RunReportTool/.vs/RunReportTool/v15/.suo
Binary file not shown.
Binary file removed src/RunReportTool/.vs/RunReportTool/v15/Browse.VC.db
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
10 changes: 6 additions & 4 deletions src/RunReportTool/RunReportTool/RunReportTool.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,12 @@ namespace fs = filesystem;
int main(int argc, const char * argv[])
{
fs::path currPath = fs::current_path();
string strCmd = "start /b pythonw ";
wstring wstrCmd = L"start /b pythonw ";

strCmd += currPath.u8string();
strCmd += "\\League.py";
wstrCmd += currPath.wstring();
wstrCmd += L"\\League.py";

system( strCmd.c_str() );
_wsystem( wstrCmd.c_str() );

return 0;
}
Loading

0 comments on commit 4667eb7

Please sign in to comment.