def look_around(clientAndroid, ps): print "Performing look around" #get image for training to mark objects before img = clientAndroid.get_screen_as_array(True) pgutil.save_array_as_png(img, "/opt/for_training/before", "main") #some helper variables phone_center = ps.getCoord(pgconst.COORDS_CENTER) r0 = 50 r1 = int(phone_center[0] * 0.8) c = (phone_center[0], int(phone_center[1] * 2 * 0.625)) #click around center performing actions per sector #click_donut(clientAndroid, ps, c, r0, r1, ps.sectorsCount) click_zones(clientAndroid, ps, c) # #click small circle around center # click_circle(clientAndroid, ps, c, 35, 10) # print "Waiting for 1 sec after clicks around center" # sleep(1) # do_relevant_action(clientAndroid, ps) #get image for training to mark objects after img = clientAndroid.get_screen_as_array(True) pgutil.save_array_as_png(img, "/opt/for_training/after", "main") print "Look around is finished" return
def exit_from_catch_ok(clientAndroid, ps, result, attempt): #Click OK button for the given item, result[1][2][1] should give y of the center of the match region (x, y) = ps.getCoord(pgconst.COORDS_CENTER) clientAndroid.send_touch((x, result[1][2][1]), 2) img = clientAndroid.get_screen_as_array() pgutil.save_array_as_png(img, ps.saveDir, "got_pkm_" + str(attempt)) #exit from stats screen clientAndroid.send_touch(ps.getCoord(pgconst.COORDS_EXIT_BUTTON)) #check if we are on a main screen img = clientAndroid.get_screen_as_array() r = pgutil.is_main_map(img, ps) if r[0]: return 1 return 0
def join_gym(clientAndroid, ps, result): print "Processing join gym" #Click join button, result[1][2] should contain coordinates of the join button clientAndroid.send_touch(result[1][2], 1) j = 0 while (j < 10): j = j + 1 print "Checking join gym screen j: %d..." % j result = pgutil.identify_join_gym_screen(clientAndroid, ps) if result is not None: if result[0] == pgconst.SCREEN_HAS_GYM_JOIN: clientAndroid.send_touch(result[1][2]) print "Got join button" elif result[0] == pgconst.SCREEN_POKEMONS_SELECTION: clientAndroid.send_touch( ps.getCoord(pgconst.COORDS_TOP_CP_POKEMON), 1) print "Selected pokemon to join" elif result[0] == pgconst.SCREEN_GYM_CONFIRM_BUTTON: clientAndroid.send_touch(result[1][2], 5) print "Got join confirm button" sleep(2) elif result[0] == pgconst.SCREEN_HAS_EXIT_BUTTON: print "Got exit button after gym joining" clientAndroid.send_touch(result[1][2], 5) elif result[0] == pgconst.SCREEN_MAIN_MAP: print "Processing join gym, done! Got Main map" return else: img = clientAndroid.get_screen_as_array(True) pgutil.save_array_as_png(img, ps.saveDir, "join_gym_strange") print "Processing join gym, done"
def catch_pokemon(clientAndroid, ps): print "Processing catch pokemon" if ps.skipPokemons: clientAndroid.send_touch( ps.getCoord(pgconst.COORDS_LEAVE_CATCH_POKEMON_BUTTON)) print "not catching, as specified in ini file" return 1 i = 0 while (i < 4): #Throw ball if i == 2 or i == 4: clientAndroid.send_swipe( ps.getScript(pgconst.SCRIPT_THROW_BALL_LONG)) # elif i == 4 or i == 8: # clientAndroid.send_swipe(ps.getScript(pgconst.SCRIPT_THROW_BALL_SHORT)) else: clientAndroid.send_swipe( ps.getScript(pgconst.SCRIPT_THROW_BALL_NORMAL)) i = i + 1 j = 0 #this loop should handle situations while ball is shaking - instead of just waiting we keep checking screen while (j < 15): j = j + 1 print "Checking catch screen j: %d..." % j #First attempt to identify catch pokemon screen result = pgutil.identify_catch_screen(clientAndroid, ps) if result is not None: if result[0] == pgconst.SCREEN_CAUGTH_POKEMON_POPUP: res = exit_from_catch_ok(clientAndroid, ps, result, i) #1 indicates that we caught pokemon and exited popups if (res == 1): print "Caught pokemon on attempt: %d" % i return 2 elif result[0] == pgconst.SCREEN_POKEMON_STATS_POPUP: clientAndroid.send_touch( ps.getCoord(pgconst.COORDS_EXIT_BUTTON)) print "Got to pokemon stats on attempt: %d" % i return 1 elif result[0] == pgconst.SCREEN_CATCHING_POKEMON: print "Did not catch pokemon on attempt: %d, continuing..." % i break elif result[0] == pgconst.SCREEN_MAIN_MENU: #pokemon run away print "Somehow got to main menu after attempts: %d!" % i clientAndroid.send_touch( ps.getCoord(pgconst.COORDS_EXIT_BUTTON)) return 1 elif result[0] == pgconst.SCREEN_MAIN_MAP: #pokemon run away print "Pokemon run away after attemps %d!" % i return 1 elif result[0] == pgconst.SCREEN_NO_POKEBALLS: print "Out of pokeballs, exiting catching, attemp: %d!" % i #If we got no pokeballs screen then we stop catching until program restart ps.skipPokemons = True clientAndroid.send_touch( ps.getCoord(pgconst.COORDS_LEAVE_CATCH_POKEMON_BUTTON)) return 1 elif result[0] == pgconst.SCREEN_SHOP: #that is just in case clientAndroid.send_touch( ps.getCoord(pgconst.COORDS_EXIT_BUTTON)) print "Got to the shopping screen, exiting catching, attemp: %d!" % i #If we got shop screen then we stop catching until program restart ps.skipPokemons = True return 1 elif result[0] == pgconst.SCREEN_HAS_EXIT_BUTTON: #that is just in case clientAndroid.send_touch( ps.getCoord(pgconst.COORDS_EXIT_BUTTON)) print "Strange state in the catching pokemon process, exiting catching, attemp: %d!" % i return 1 print "Was not able to catch pokemon after, attempt: %d, exiting catching" % i img = clientAndroid.get_screen_as_array() #we got unknown screen, save it pgutil.save_array_as_png(img, ps.saveDir, "missed_catch") clientAndroid.send_touch( ps.getCoord(pgconst.COORDS_LEAVE_CATCH_POKEMON_BUTTON)) return 0
def do_relevant_action(clientAndroid, ps): # print "start doing actions" count = 0 result = None #some mitigations for unknown screens while (count < 3): count = count + 1 #get screen img = clientAndroid.get_screen_as_array() #identify screen result = pgutil.identify_screen(img, ps) pgutil.print_identified_screen(result) #do actions based on the screen if result is None: sleep(1) else: break if result is None: clientAndroid.send_touch(ps.getCoord(pgconst.COORDS_EXIT_BUTTON)) #we got unknown screen, save it pgutil.save_array_as_png(img, ps.saveDir, "look_around_unknown_screen") return 0 res = result[0] if result[0] == pgconst.SCREEN_INSIDE_POKESTOP: collect_pokestop(clientAndroid, ps) elif result[0] == pgconst.SCREEN_INSIDE_GYM: clientAndroid.send_touch(ps.getCoord(pgconst.COORDS_EXIT_BUTTON)) elif result[0] == pgconst.SCREEN_CATCHING_POKEMON or result[ 0] == pgconst.SCREEN_NO_POKEBALLS: r = catch_pokemon(clientAndroid, ps) if r == 0: #that means that we failed catch because it escapes - do not try that zone anymore to avoid wasting balls return pgconst.SCREEN_MAIN_MAP elif result[0] == pgconst.SCREEN_HAS_EXIT_BUTTON: clientAndroid.send_touch(ps.getCoord(pgconst.COORDS_EXIT_BUTTON)) elif result[0] == pgconst.SCREEN_MAIN_MAP: #do nothing as it is required screen print "Main screen, do nothing" elif result[0] == pgconst.SCREEN_SHOP: clientAndroid.send_touch(ps.getCoord(pgconst.COORDS_EXIT_BUTTON)) #If we got shop screen then we stop catching until program restart ps.skipPokemons = True elif result[0] == pgconst.SCREEN_POKEMON_STATS_POPUP: clientAndroid.send_touch(ps.getCoord(pgconst.COORDS_EXIT_BUTTON)) elif result[0] == pgconst.SCREEN_GYM_TOO_FAR: clientAndroid.send_touch(ps.getCoord(pgconst.COORDS_EXIT_BUTTON)) elif result[0] == pgconst.SCREEN_HAS_GYM_JOIN: join_gym(clientAndroid, ps, result) elif result[0] == pgconst.SCREEN_PASSENGER: #Click PASSENGER button, result[1][2][1] should give y of the center of the match region (x, y) = ps.getCoord(pgconst.COORDS_CENTER) clientAndroid.send_touch((x, result[1][2][1]), 2) elif result[0] == pgconst.SCREEN_ANDROID_HOME: #We have to exit from the application and start from the beginning sys.exit(1) if res == 0: print "was not able to perform proper action " pgutil.save_array_as_png(img, ps.saveDir, "look_around_unknown_acions") # print "finished doing actions" return res
def clear_bag(clientAndroid, items, ps): print "started clear_bag process, item categories to delete: " + str(items) #Check if we are on the main screen img = clientAndroid.get_screen_as_array() if img is None: print "clear_bag Error! Can't capture the screen." return if not pgutil.is_main_map(img, ps)[0]: print "clear_bag Error! wrong start screen." pgutil.save_array_as_png(img, ps.saveDir, "wrong_main_screen") return clientAndroid.send_touch(ps.getCoord(pgconst.COORDS_MAIN_MENU_BUTTON)) clientAndroid.send_touch(ps.getCoord(pgconst.COORDS_ITEMS_BUTTON)) print "Checking items..." #check items in the loop using scroll for i in range(0, 8): #should be initial items list screen img = clientAndroid.get_screen_as_array() resutls = pgutil.is_items_visible(img, items, ps) if resutls is not None: for r in resutls: #Remove detected item from the list of items items = items & (~r[0]) #Click delete button for the given item, r[1][2][1] should give y of the center of the match region (x, y) = ps.getCoord(pgconst.COORDS_DELETE_ITEM) clientAndroid.send_touch((x, r[1][2][1])) #Click select how many items for j in range(0, 25): clientAndroid.send_touch( ps.getCoord(pgconst.COORDS_DISCARD_PLUS_BUTTON)) #this is coordinates of yes button clientAndroid.send_touch( ps.getCoord(pgconst.COORDS_DISCARD_YES_BUTTON)) print "deleted item: " + str(r[0]) #Exit if we already processed all selected items if items <= 0: sleep(2) break #Scroll clientAndroid.send_swipe(ps.getScript(pgconst.SCRIPT_SCROLL_ITEMS)) sleep(1) #Exiting Items menu #this is coordinates of yes button clientAndroid.send_touch( ps.getCoord(pgconst.COORDS_CLOSE_ITEMS_MENU_BUTTON)) print "Finished clearing bag" return
while (i < 10): i = i + 1 img = clientAndroid.get_screen_as_array() r = pgutil.match_template( img, ps.getTemplate(pgconst.TEMPLATE_ANDROID_PG_ICON), pgconst.MIN_RECOGNITION_VAL) if r[0]: print "got Android screen, launching pg..." clientAndroid.send_touch(r[2]) status = 1 break if status < 1: print "Error! Can't get PG icon, exiting..." pgutil.save_array_as_png(img, ps.saveDir, "problem_getting_android_screen") sys.exit(1) else: #reset status status = 0 #check if we got main screen i = 0 img0 = None sameCount = 0 while (i < 100): i = i + 1 img = clientAndroid.get_screen_as_array() r = pgutil.match_template( img, ps.getTemplate(pgconst.TEMPLATE_POKEYBALL_MAP_SCREEN), pgconst.MIN_RECOGNITION_VAL)