diff --git a/League.py b/League.py index 6dd5da4..afedbc9 100644 --- a/League.py +++ b/League.py @@ -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() @@ -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. +########################################################### \ No newline at end of file diff --git a/RunReportTool.exe b/RunReportTool.exe index c60883a..85bd1d8 100644 Binary files a/RunReportTool.exe and b/RunReportTool.exe differ diff --git a/src/RunReportTool/.vs/RunReportTool/v15/.suo b/src/RunReportTool/.vs/RunReportTool/v15/.suo deleted file mode 100644 index ed9cff1..0000000 Binary files a/src/RunReportTool/.vs/RunReportTool/v15/.suo and /dev/null differ diff --git a/src/RunReportTool/.vs/RunReportTool/v15/Browse.VC.db b/src/RunReportTool/.vs/RunReportTool/v15/Browse.VC.db deleted file mode 100644 index 1cdbc38..0000000 Binary files a/src/RunReportTool/.vs/RunReportTool/v15/Browse.VC.db and /dev/null differ diff --git a/src/RunReportTool/.vs/RunReportTool/v15/ipch/1d6dc9c1d0b4d72c.ipch b/src/RunReportTool/.vs/RunReportTool/v15/ipch/1d6dc9c1d0b4d72c.ipch deleted file mode 100644 index 55ad99b..0000000 Binary files a/src/RunReportTool/.vs/RunReportTool/v15/ipch/1d6dc9c1d0b4d72c.ipch and /dev/null differ diff --git a/src/RunReportTool/.vs/RunReportTool/v15/ipch/AutoPCH/ac11dcfbb9a445d0/RUNREPORTTOOL.ipch b/src/RunReportTool/.vs/RunReportTool/v15/ipch/AutoPCH/ac11dcfbb9a445d0/RUNREPORTTOOL.ipch deleted file mode 100644 index 30818e3..0000000 Binary files a/src/RunReportTool/.vs/RunReportTool/v15/ipch/AutoPCH/ac11dcfbb9a445d0/RUNREPORTTOOL.ipch and /dev/null differ diff --git a/src/RunReportTool/.vs/RunReportTool/v15/ipch/AutoPCH/d0c497e09a7c68f0/RUNREPORTTOOL.ipch b/src/RunReportTool/.vs/RunReportTool/v15/ipch/AutoPCH/d0c497e09a7c68f0/RUNREPORTTOOL.ipch deleted file mode 100644 index 10a50b4..0000000 Binary files a/src/RunReportTool/.vs/RunReportTool/v15/ipch/AutoPCH/d0c497e09a7c68f0/RUNREPORTTOOL.ipch and /dev/null differ diff --git a/src/RunReportTool/.vs/RunReportTool/v15/ipch/c7f1e4852d69d55c.ipch b/src/RunReportTool/.vs/RunReportTool/v15/ipch/c7f1e4852d69d55c.ipch deleted file mode 100644 index 4cd5114..0000000 Binary files a/src/RunReportTool/.vs/RunReportTool/v15/ipch/c7f1e4852d69d55c.ipch and /dev/null differ diff --git a/src/RunReportTool/.vs/RunReportTool/v15/ipch/fc2602cf41f49b29.ipch b/src/RunReportTool/.vs/RunReportTool/v15/ipch/fc2602cf41f49b29.ipch deleted file mode 100644 index 34a771d..0000000 Binary files a/src/RunReportTool/.vs/RunReportTool/v15/ipch/fc2602cf41f49b29.ipch and /dev/null differ diff --git a/src/RunReportTool/RunReportTool/RunReportTool.cpp b/src/RunReportTool/RunReportTool/RunReportTool.cpp index be5c702..48a0460 100644 --- a/src/RunReportTool/RunReportTool/RunReportTool.cpp +++ b/src/RunReportTool/RunReportTool/RunReportTool.cpp @@ -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; } diff --git a/src/RunReportTool/RunReportTool/RunReportTool.vcxproj.user b/src/RunReportTool/RunReportTool/RunReportTool.vcxproj.user deleted file mode 100644 index be25078..0000000 --- a/src/RunReportTool/RunReportTool/RunReportTool.vcxproj.user +++ /dev/null @@ -1,4 +0,0 @@ - - - - \ No newline at end of file