Advertisement
Guest User

Taimanin bot

a guest
Dec 18th, 2018
491
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 18.09 KB | None | 0 0
  1. from selenium import webdriver
  2. import random
  3. from selenium.webdriver.common.action_chains import ActionChains
  4. from selenium.webdriver.support.ui import Select
  5. from selenium.webdriver.common.keys import Keys
  6. import time
  7. import numpy as np
  8.  
  9. username = "ChangeMe"
  10. email = "ChangeMe"
  11. password = "ChangeMe"
  12.  
  13. def go_to_quests():
  14.     browser.find_element_by_class_name('top_menu_1').click()
  15.     time.sleep(random.uniform(0.2, 0.6))
  16.  
  17. def go_to_stage(chapter_number, stage_number):
  18.     """This is pretty much hard coded for stage 1-1.
  19.    If you want it to work on other stages, fix the
  20.    scrolling functions so that the right thing is in
  21.    view."""
  22.     loaded = wait_until_present('scroll_content1', 10)
  23.    
  24.     if loaded:
  25.         quests = browser.find_element_by_id('scroll_content1')
  26.         browser.execute_script('arguments[0].scrollTop = arguments[0].scrollHeight', quests)
  27.         time.sleep(random.uniform(0.2, 0.6))
  28.         chapter_id = "chapter" + str(chapter_number)
  29.         browser.find_element_by_id(chapter_id).click()
  30.         time.sleep(random.uniform(0.2, 0.6))
  31.  
  32.         stages = browser.find_element_by_id('scroll_content2')
  33.         browser.execute_script('arguments[0].scrollTop = arguments[0].scrollHeight', stages)
  34.         time.sleep(random.uniform(0.2, 0.6))
  35.         stage_id = "stage" + str(stage_number) + "-state"
  36.         browser.find_element_by_id('stage1-state').click()
  37.         time.sleep(random.uniform(4, 5)) # Wait to start clicking through quest
  38.     else:
  39.         print("Some kinda lag")
  40.  
  41. def go_to_1_1():
  42.     browser.find_element_by_class_name('top_menu_1').click()
  43.     time.sleep(random.uniform(4,6))
  44.  
  45.     quests = browser.find_element_by_id('scroll_content1')
  46.     browser.execute_script('arguments[0].scrollTop = arguments[0].scrollHeight', quests)
  47.     time.sleep(random.uniform(0.2, 1.5))
  48.     browser.find_element_by_id('chapter1').click()
  49.     time.sleep(random.uniform(0.2, 1.5))
  50.  
  51.     stages = browser.find_element_by_id('scroll_content2')
  52.     browser.execute_script('arguments[0].scrollTop = arguments[0].scrollHeight', stages)
  53.     time.sleep(random.uniform(0.2, 1.5))
  54.     browser.find_element_by_id('stage1-state').click()
  55.     time.sleep(random.uniform(0.2, 1.5))
  56.  
  57. def hit_below_quest_button():
  58.     quest_button = browser.find_element_by_class_name('top_menu_1')
  59.  
  60.     action = ActionChains(browser)
  61.     action.move_to_element_with_offset(quest_button, 0, 200)
  62.     action.click()
  63.     action.perform()
  64.  
  65. def found_boss(username):
  66.     page = browser.execute_script("return document.documentElement.outerHTML")
  67.     found_my_boss = False
  68.    
  69.     if 'friend_frame' in page:
  70.         bosses = browser.find_elements_by_class_name('friend_frame')
  71.  
  72.         if len(bosses)>0: # Should be from above check. Double checking.
  73.             for i, boss in enumerate(bosses):
  74.                 html = boss.get_attribute('innerHTML')
  75.                 if username in html:
  76.                     found_my_boss = True
  77.  
  78.     return found_my_boss
  79.  
  80. def goto_main_page():
  81.     my_page = browser.find_element_by_id('mypage')
  82.  
  83.     action = ActionChains(browser)
  84.     action.move_to_element_with_offset(my_page, 5, 5)
  85.     action.click()
  86.     action.perform()
  87.  
  88. def goto_req_assistance():
  89.     html = browser.execute_script("return document.documentElement.outerHTML")
  90.  
  91.     if 'boss_alerts_1' in html:
  92.         request_assistance = browser.find_element_by_id('boss_alerts_1')
  93.         request_assistance.click()
  94.     else:
  95.         print("There are currently no requests for assistance")
  96.  
  97. def press_battle_button():
  98.     # Go to battle partner select screen
  99.     browser.find_element_by_id('battle_start_button').click()
  100.  
  101. def find_partner():
  102.     bp_list = ['C00150', 'C00070', 'C00048',
  103.                'C00043', 'C00092', 'C00057',
  104.                'C00143', 'C00146'] # These are currently demons
  105.  
  106.     friend_frame_list = browser.find_elements_by_class_name('friend_frame')
  107.  
  108.     for i, choice in enumerate(friend_frame_list):
  109.         acceptable = False
  110.         html = choice.get_attribute("innerHTML")
  111.         for card in bp_list:
  112.             if card in html:
  113.                 choice.click()
  114.                 return True
  115.    
  116.     go_back_from_battle_partners()
  117.    
  118.     return False
  119.  
  120. def go_back_from_battle_partners():
  121.     back_btn = browser.find_element_by_xpath('//*[@id="main_frame_raid"]/div[2]')
  122.     back_btn.click()
  123.  
  124. def get_boss_health():
  125.     gauge = browser.find_elements_by_class_name('red_gage_center_bar')
  126.     pct = int(gauge[1].get_attribute('style')[7:-2]) # First red gauge is your stamina
  127.     return pct
  128.  
  129. def retry_battle():
  130.     retry = browser.find_element_by_class_name('decision_button_column_1')
  131.     retry.click()
  132.  
  133. def request_assistance():
  134.     loaded = wait_until_present('raid_help_button', 10)
  135.    
  136.     if loaded:
  137.         hit_below_quest_button() # Presses request assistance
  138.    
  139.     #time.sleep(5)
  140.    
  141.     loaded = wait_until_present('back_button_column_1', 10)
  142.     time.sleep(1)
  143.    
  144.     if loaded:
  145.         back_btn = browser.find_element_by_class_name("back_button_column_1")
  146.         back_btn.click()
  147.    
  148.     time.sleep(5)
  149.  
  150. def sell_cards():
  151.     sell_button = browser.find_elements_by_class_name('decision_button_column_1')[1]
  152.     sell_button.click()
  153.  
  154.     time.sleep(5)
  155.  
  156.     number_left = int(browser.find_element_by_id('number_of_card').text)
  157.  
  158.     while int(browser.find_element_by_id('number_of_card').text)>0:
  159.         try:
  160.             card = browser.find_element_by_id('card_image')
  161.             card.click()
  162.             time.sleep(0.1)
  163.         except:
  164.             continue
  165.  
  166.     sell_cards_button = browser.find_element_by_id('button_sell_confirm')
  167.     sell_cards_button.click()
  168.  
  169.     time.sleep(5)
  170.  
  171.     final_sell_button = browser.find_element_by_id('button_sell_result')
  172.     final_sell_button.click()
  173.    
  174.     time.sleep(5)
  175.  
  176. def skip_battle_animation():
  177.     canvas = browser.find_element_by_id('canvas')
  178.  
  179.     action = ActionChains(browser)
  180.     action.move_to_element_with_offset(canvas, 230, 80)
  181.     action.click()
  182.     action.perform()
  183.  
  184.     time.sleep(5)
  185.  
  186. def fill_bp():
  187.     strong_atk = browser.find_element_by_id('quest_attack_3')
  188.     strong_atk.click()
  189.  
  190.     time.sleep(5)
  191.  
  192.     # Click on use
  193.     use = browser.find_element_by_id('IT006')
  194.     use_button = use.find_element_by_xpath('..')
  195.     use_button.click()
  196.  
  197.     time.sleep(5)
  198.  
  199.     # Confirm
  200.     browser.find_element_by_class_name('decision_button_column_2').click()
  201.  
  202.     time.sleep(5)
  203.    
  204.     # Return to boss battle
  205.     browser.find_element_by_class_name('back_button_column_1').click()
  206.  
  207. def get_bp():
  208.     return int(browser.find_element_by_id('top_bp_num').text[0])
  209.  
  210. def get_medals():
  211.     return int(browser.find_element_by_id('top_medal_num').text)
  212.  
  213. def get_cards():
  214.     parentElement = browser.find_element_by_id('productList')
  215.     elementList = parentElement.find_elements_by_tag_name("li")
  216.     return len(elementList)
  217.  
  218. def get_gold():
  219.     return int(browser.find_element_by_id('gold_num').text)
  220.  
  221. def get_quest_stats():
  222.     quest_stats = {'Medals': get_medals(),
  223.                    'Gold': get_gold(),
  224.                    'Cards': get_cards()}
  225.     return quest_stats
  226.  
  227. def click_screen():
  228.     browser.find_element_by_id('canvas').click()
  229.  
  230. def refill_stam_during_quest():
  231.     select = Select(browser.find_element_by_xpath('//*[@id="scroll_content9"]/div[2]/form/div[1]/select'))
  232.     select.select_by_value('1')
  233.     time.sleep(3)
  234.     use = browser.find_element_by_id('IT004')
  235.     use_button = use.find_element_by_xpath('..')
  236.     use_button.click()
  237.     time.sleep(3) # This is brittle. Do a better check
  238.  
  239.     # Use 1 item?
  240.     browser.find_element_by_xpath('//*[@id="main_frame_item"]/div[5]/a[1]/div').click()
  241.     time.sleep(3) # Brittle. Do better check.
  242.  
  243.     # Return to quest
  244.     browser.find_element_by_xpath('//*[@id="main_frame_item"]/div[5]/a/div').click()
  245.     time.sleep(3) # Brittle. Do better check.
  246.  
  247. def should_battle():
  248.     boss_status = browser.find_element_by_class_name('quest_boss_status_1').text
  249.    
  250.     found_red_oni = "Red Oni" in boss_status
  251.     found_speed_demon = "Speed Demon" in boss_status
  252.    
  253.     return found_red_oni or found_speed_demon
  254.  
  255. def setup_kill(bp_required):
  256.     print("Kill setup")
  257.     partner_found = False
  258.    
  259.     while not partner_found:
  260.         print("Finding partner")
  261.         time.sleep(5)
  262.         press_battle_button()
  263.         time.sleep(5)
  264.         partner_found = find_partner()
  265.    
  266.     time.sleep(5) # To run enough_bp()
  267.    
  268.     if not enough_bp():
  269.         print("Restocking bp")
  270.         time.sleep(5)
  271.         fill_bp()
  272.    
  273.     time.sleep(5)
  274.  
  275. def attempt_kill(bp_required):
  276.     print("Kill attempt")
  277.     button_id_string = 'quest_attack_' + str(bp_required)
  278.     attack_button = browser.find_element_by_id(button_id_string)
  279.     attack_button.click()
  280.    
  281.     time.sleep(10)
  282.    
  283.     skip_battle_animation()
  284.  
  285. def kill_boss():
  286.     """Should be called on the screen with the battle button.
  287.    Will either kill boss, returning True, or reduce hp to <50%,
  288.    returning False."""
  289.    
  290.     health = 100
  291.    
  292.     while True:
  293.         loaded = wait_until_present('<div id="raid_time_rimit"', 30)
  294.         if loaded:
  295.             health = get_boss_health()
  296.            
  297.             bp = bp_needed()
  298.            
  299.             if health == 100: # First attack
  300.                 setup_kill(bp)
  301.                 attempt_kill(bp)
  302.             elif health < 50: # We've done enough damage
  303.                 retry_battle()
  304.                 return False
  305.             elif health < 100: # (n+1)st attack, not yet reached <50% hp
  306.                 retry_battle()
  307.                 setup_kill(1)
  308.                 attempt_kill(1)
  309.         else:
  310.             """This should mean the boss has been killed."""
  311.             page = browser.execute_script("return document.documentElement.outerHTML")
  312.             if 'Raid Boss Defeated' in page:
  313.                 return True
  314.             # Else:
  315.                 # Some kind of error. Should not reach this.
  316.  
  317. def worth_attacking():
  318.     """If we're using bp pots, we may want to neglect some speed demons.
  319.    If we're using natural bp regen, we'll wait for any of them."""
  320.     boss_status = browser.find_element_by_class_name('quest_boss_status_1').text
  321.    
  322.     found_red_oni = "Red Oni" in boss_status
  323.     found_speed_demon = "Speed Demon" in boss_status
  324.    
  325.     return found_red_oni or found_speed_demon
  326.  
  327. def boss_level():
  328.     boss_info = browser.find_element_by_class_name('quest_boss_status_1').text
  329.     # I.E, "Dark Beast Lv7"
  330.     parts = boss_info.split(" ")
  331.     # I.E, "Dark", "Beast", "Lv7"
  332.     level = int(parts[-1][2:])
  333.     # I.E, "7"
  334.     return level
  335.  
  336. def bp_needed():
  337.     if boss_level() < 40:
  338.         return 1
  339.     else:
  340.         return 2
  341.  
  342. def enough_bp():
  343.     return get_bp() >= bp_needed()
  344.  
  345. def regen_bp(bp_to_regen):
  346.     """Very naive implementation. Adjust this to keep from logging out."""
  347.     regen_time = 20 * 60 * bp_to_regen
  348.     print("Would sleep for", regen_time, "seconds")
  349.     #time.sleep(regen_time)
  350.  
  351. def wait_until_present(string_to_find, time_limit):
  352.     #html = browser.execute_script("return document.documentElement.outerHTML")
  353.    
  354.     time_waited = 0
  355.    
  356.     while time_waited < time_limit:
  357.         if string_to_find in browser.execute_script("return document.documentElement.outerHTML"):
  358.             time.sleep(random.uniform(0.5, 1))
  359.             return True
  360.         else:
  361.             time_waited += 0.1
  362.    
  363.     return False
  364.  
  365. def goto_req_assistance():
  366.     html = browser.execute_script("return document.documentElement.outerHTML")
  367.  
  368.     if 'boss_alerts_1' in html:
  369.         request_assistance = browser.find_element_by_id('boss_alerts_1')
  370.         request_assistance.click()
  371.         return True
  372.     else:
  373.         return False
  374.  
  375. def is_boss_dead():
  376.     """This has a name too similar to the one in the main
  377.    loop. Change things to make more sense."""
  378.     """This has to start on a page other than the main page.
  379.    If you start this on the main page it will return True
  380.    much too quickly."""
  381.     goto_main_page()
  382.  
  383.     loaded = wait_until_present('<div class="button-news"', 10)
  384.  
  385.     if loaded:
  386.         html = browser.execute_script("return document.documentElement.outerHTML")
  387.  
  388.         if 'boss_alerts_1' in html:
  389.             """This sometimes gives me a not clickable error.
  390.            Fix it to click using actions."""
  391.             request_assistance = browser.find_element_by_id('boss_alerts_1')
  392.             request_assistance.click()
  393.         else:
  394.             return True
  395.     else:
  396.         return None
  397.    
  398.     loaded = wait_until_present('Raid Boss List', 10)
  399.    
  400.     if loaded:
  401.         return not found_boss(username)
  402.     else:
  403.         return None
  404.  
  405. def loop_until_boss_is_dead():
  406.     start = time.time()
  407.     waiting = time.time() - start
  408.  
  409.     while not is_boss_dead():
  410.         if waiting > 60:
  411.             break
  412.         else:
  413.             waiting = time.time() - start
  414.             print("Have been waiting", int(waiting), "seconds")
  415.  
  416.         time.sleep(5)
  417.  
  418.     print("End loop")
  419.  
  420. """This starts the bot and logs in."""
  421.  
  422. browser = webdriver.Chrome('/Users/christopher/Downloads/chromedriver')
  423.  
  424. time.sleep(5)
  425.  
  426. browser.get('https://www.nutaku.net/games/taimanin-asagi-battle-arena/play/')
  427.  
  428. time.sleep(5)
  429.  
  430. email_field = browser.find_element_by_id('s-email')
  431. pw_field = browser.find_element_by_id('s-password')
  432.  
  433. email_field.send_keys(email)
  434. pw_field.send_keys(password)
  435.  
  436. pw_field.send_keys(Keys.ENTER)
  437.  
  438. time.sleep(5)
  439.  
  440. try:
  441.     browser.find_element_by_id('pnCancel').click() # Might have to frame select first?
  442. except:
  443.     pass
  444.  
  445. time.sleep(5)
  446.  
  447. # Start Game
  448.  
  449. browser.switch_to.frame('game_frame')
  450. browser.find_element_by_class_name("game_start_over").click()
  451.  
  452. time.sleep(5)
  453.  
  454. """This is the main loop.
  455. Does quests, finds/kills bosses, requests assistance where appropriate, etc."""
  456.  
  457. looking_for_boss = True
  458. engaging_boss = False
  459. checking_boss = False
  460. counter = 0
  461. errors = []
  462.  
  463. while counter < 1:
  464.     try:
  465.         looking_counter = 0
  466.         while looking_for_boss:
  467.             looking_counter += 1
  468.            
  469.             if looking_counter > 25:
  470.                 looking_for_boss = False
  471.                 engaging_boss = False
  472.                 checking_boss = True
  473.             try:
  474.                 stats = get_quest_stats()
  475.  
  476.                 click_screen()
  477.  
  478.                 time.sleep(4) # Wait a bit
  479.  
  480.                 new_stats = get_quest_stats()
  481.             except:
  482.                 pass
  483.  
  484.             html = browser.execute_script("return document.documentElement.outerHTML")
  485.  
  486.             if "Sell Cards" in html:
  487.                 print("Selling cards")
  488.                 sell_cards()
  489.                 go_to_quests()
  490.                 go_to_stage(1,1)
  491.             elif new_stats['Gold'] > stats['Gold']:
  492.                 print('Got gold')
  493.             elif new_stats['Medals'] > stats['Medals']:
  494.                 print('Got medals')
  495.             elif new_stats['Cards'] > stats['Cards']:
  496.                 print('Got cards')
  497.             elif "Stam Potion" in html:
  498.                 print("Topping up stamina")
  499.                 refill_stam_during_quest()
  500.                 go_to_stage(1,1)
  501.             #elif level up:...
  502.             else:
  503.                 """At this point it's either a boss or some kind of error like lag."""
  504.                 hit_below_quest_button()
  505.                 time.sleep(5)
  506.                 html = browser.execute_script("return document.documentElement.outerHTML")
  507.  
  508.                 if "Raid Boss List" in html:
  509.                     looking_for_boss = False
  510.                     engaging_boss = True
  511.                 else:
  512.                     print("Error. Lag or level up?")
  513.                     looking_for_boss = False
  514.                     engaging_boss = False
  515.                     checking_boss = True
  516.                     #counter = 1000
  517.                     # Add error flag here
  518.                 #print('Breaking out of loop')
  519.                 #break
  520.  
  521.         while engaging_boss:
  522.             print("Starting boss loop.")
  523.             hit_below_quest_button() # Select first boss, which by default will be yours
  524.             time.sleep(5) # Improve this wait condition
  525.  
  526.             boss_is_dead = False
  527.             if worth_attacking():
  528.                 boss_is_dead = kill_boss()
  529.                 time.sleep(5)
  530.  
  531.             if boss_is_dead: # Handles bosses you didn't fight, too.
  532.                 looking_for_boss = True
  533.                 engaging_boss = False
  534.                 go_to_quests()
  535.                 go_to_stage(1,1)
  536.             else:
  537.                 request_assistance()
  538.                 checking_boss = True
  539.                 # Keep checking until boss is dead, then restart
  540.             engaging_boss = False
  541.             print("Finished engaging boss")
  542.  
  543.         while checking_boss:
  544.             go_to_quests()
  545.             start = time.time()
  546.             waiting = time.time() - start
  547.  
  548.             while not is_boss_dead():
  549.                 if waiting > 600:
  550.                     time.sleep(300)
  551.                 elif waiting > 60:
  552.                     time.sleep(30)
  553.                 else:
  554.                     #waiting = time.time() - start
  555.                     #print("Have been waiting", int(waiting), "seconds")
  556.                     time.sleep(5)
  557.                
  558.                 waiting = time.time() - start
  559.                 print("Have been waiting", int(waiting), "seconds")
  560.  
  561.                 #time.sleep(5)
  562.            
  563.             go_to_quests()
  564.             go_to_stage(1,1)
  565.             checking_boss = False
  566.             looking_for_boss = True
  567.  
  568.             print("End loop")
  569.     except Exception as err:
  570.         print(type(err))
  571.         print(err)
  572.         looking_for_boss = False
  573.         engaging_boss = False
  574.         checking_boss = True
  575.         try: # Sometimes it gets stuck on the button after requesting assistance.
  576.             loaded = wait_until_present('back_button_column_1', 10)
  577.             time.sleep(1)
  578.  
  579.             if loaded:
  580.                 back_btn = browser.find_element_by_class_name("back_button_column_1")
  581.                 back_btn.click()
  582.  
  583.             time.sleep(5)
  584.         except:
  585.             pass
  586.    
  587.     counter += 1
  588.     print(counter)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement