Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from selenium import webdriver
- import random
- from selenium.webdriver.common.action_chains import ActionChains
- from selenium.webdriver.support.ui import Select
- from selenium.webdriver.common.keys import Keys
- import time
- import numpy as np
- username = "ChangeMe"
- email = "ChangeMe"
- password = "ChangeMe"
- def go_to_quests():
- browser.find_element_by_class_name('top_menu_1').click()
- time.sleep(random.uniform(0.2, 0.6))
- def go_to_stage(chapter_number, stage_number):
- """This is pretty much hard coded for stage 1-1.
- If you want it to work on other stages, fix the
- scrolling functions so that the right thing is in
- view."""
- loaded = wait_until_present('scroll_content1', 10)
- if loaded:
- quests = browser.find_element_by_id('scroll_content1')
- browser.execute_script('arguments[0].scrollTop = arguments[0].scrollHeight', quests)
- time.sleep(random.uniform(0.2, 0.6))
- chapter_id = "chapter" + str(chapter_number)
- browser.find_element_by_id(chapter_id).click()
- time.sleep(random.uniform(0.2, 0.6))
- stages = browser.find_element_by_id('scroll_content2')
- browser.execute_script('arguments[0].scrollTop = arguments[0].scrollHeight', stages)
- time.sleep(random.uniform(0.2, 0.6))
- stage_id = "stage" + str(stage_number) + "-state"
- browser.find_element_by_id('stage1-state').click()
- time.sleep(random.uniform(4, 5)) # Wait to start clicking through quest
- else:
- print("Some kinda lag")
- def go_to_1_1():
- browser.find_element_by_class_name('top_menu_1').click()
- time.sleep(random.uniform(4,6))
- quests = browser.find_element_by_id('scroll_content1')
- browser.execute_script('arguments[0].scrollTop = arguments[0].scrollHeight', quests)
- time.sleep(random.uniform(0.2, 1.5))
- browser.find_element_by_id('chapter1').click()
- time.sleep(random.uniform(0.2, 1.5))
- stages = browser.find_element_by_id('scroll_content2')
- browser.execute_script('arguments[0].scrollTop = arguments[0].scrollHeight', stages)
- time.sleep(random.uniform(0.2, 1.5))
- browser.find_element_by_id('stage1-state').click()
- time.sleep(random.uniform(0.2, 1.5))
- def hit_below_quest_button():
- quest_button = browser.find_element_by_class_name('top_menu_1')
- action = ActionChains(browser)
- action.move_to_element_with_offset(quest_button, 0, 200)
- action.click()
- action.perform()
- def found_boss(username):
- page = browser.execute_script("return document.documentElement.outerHTML")
- found_my_boss = False
- if 'friend_frame' in page:
- bosses = browser.find_elements_by_class_name('friend_frame')
- if len(bosses)>0: # Should be from above check. Double checking.
- for i, boss in enumerate(bosses):
- html = boss.get_attribute('innerHTML')
- if username in html:
- found_my_boss = True
- return found_my_boss
- def goto_main_page():
- my_page = browser.find_element_by_id('mypage')
- action = ActionChains(browser)
- action.move_to_element_with_offset(my_page, 5, 5)
- action.click()
- action.perform()
- def goto_req_assistance():
- html = browser.execute_script("return document.documentElement.outerHTML")
- if 'boss_alerts_1' in html:
- request_assistance = browser.find_element_by_id('boss_alerts_1')
- request_assistance.click()
- else:
- print("There are currently no requests for assistance")
- def press_battle_button():
- # Go to battle partner select screen
- browser.find_element_by_id('battle_start_button').click()
- def find_partner():
- bp_list = ['C00150', 'C00070', 'C00048',
- 'C00043', 'C00092', 'C00057',
- 'C00143', 'C00146'] # These are currently demons
- friend_frame_list = browser.find_elements_by_class_name('friend_frame')
- for i, choice in enumerate(friend_frame_list):
- acceptable = False
- html = choice.get_attribute("innerHTML")
- for card in bp_list:
- if card in html:
- choice.click()
- return True
- go_back_from_battle_partners()
- return False
- def go_back_from_battle_partners():
- back_btn = browser.find_element_by_xpath('//*[@id="main_frame_raid"]/div[2]')
- back_btn.click()
- def get_boss_health():
- gauge = browser.find_elements_by_class_name('red_gage_center_bar')
- pct = int(gauge[1].get_attribute('style')[7:-2]) # First red gauge is your stamina
- return pct
- def retry_battle():
- retry = browser.find_element_by_class_name('decision_button_column_1')
- retry.click()
- def request_assistance():
- loaded = wait_until_present('raid_help_button', 10)
- if loaded:
- hit_below_quest_button() # Presses request assistance
- #time.sleep(5)
- loaded = wait_until_present('back_button_column_1', 10)
- time.sleep(1)
- if loaded:
- back_btn = browser.find_element_by_class_name("back_button_column_1")
- back_btn.click()
- time.sleep(5)
- def sell_cards():
- sell_button = browser.find_elements_by_class_name('decision_button_column_1')[1]
- sell_button.click()
- time.sleep(5)
- number_left = int(browser.find_element_by_id('number_of_card').text)
- while int(browser.find_element_by_id('number_of_card').text)>0:
- try:
- card = browser.find_element_by_id('card_image')
- card.click()
- time.sleep(0.1)
- except:
- continue
- sell_cards_button = browser.find_element_by_id('button_sell_confirm')
- sell_cards_button.click()
- time.sleep(5)
- final_sell_button = browser.find_element_by_id('button_sell_result')
- final_sell_button.click()
- time.sleep(5)
- def skip_battle_animation():
- canvas = browser.find_element_by_id('canvas')
- action = ActionChains(browser)
- action.move_to_element_with_offset(canvas, 230, 80)
- action.click()
- action.perform()
- time.sleep(5)
- def fill_bp():
- strong_atk = browser.find_element_by_id('quest_attack_3')
- strong_atk.click()
- time.sleep(5)
- # Click on use
- use = browser.find_element_by_id('IT006')
- use_button = use.find_element_by_xpath('..')
- use_button.click()
- time.sleep(5)
- # Confirm
- browser.find_element_by_class_name('decision_button_column_2').click()
- time.sleep(5)
- # Return to boss battle
- browser.find_element_by_class_name('back_button_column_1').click()
- def get_bp():
- return int(browser.find_element_by_id('top_bp_num').text[0])
- def get_medals():
- return int(browser.find_element_by_id('top_medal_num').text)
- def get_cards():
- parentElement = browser.find_element_by_id('productList')
- elementList = parentElement.find_elements_by_tag_name("li")
- return len(elementList)
- def get_gold():
- return int(browser.find_element_by_id('gold_num').text)
- def get_quest_stats():
- quest_stats = {'Medals': get_medals(),
- 'Gold': get_gold(),
- 'Cards': get_cards()}
- return quest_stats
- def click_screen():
- browser.find_element_by_id('canvas').click()
- def refill_stam_during_quest():
- select = Select(browser.find_element_by_xpath('//*[@id="scroll_content9"]/div[2]/form/div[1]/select'))
- select.select_by_value('1')
- time.sleep(3)
- use = browser.find_element_by_id('IT004')
- use_button = use.find_element_by_xpath('..')
- use_button.click()
- time.sleep(3) # This is brittle. Do a better check
- # Use 1 item?
- browser.find_element_by_xpath('//*[@id="main_frame_item"]/div[5]/a[1]/div').click()
- time.sleep(3) # Brittle. Do better check.
- # Return to quest
- browser.find_element_by_xpath('//*[@id="main_frame_item"]/div[5]/a/div').click()
- time.sleep(3) # Brittle. Do better check.
- def should_battle():
- boss_status = browser.find_element_by_class_name('quest_boss_status_1').text
- found_red_oni = "Red Oni" in boss_status
- found_speed_demon = "Speed Demon" in boss_status
- return found_red_oni or found_speed_demon
- def setup_kill(bp_required):
- print("Kill setup")
- partner_found = False
- while not partner_found:
- print("Finding partner")
- time.sleep(5)
- press_battle_button()
- time.sleep(5)
- partner_found = find_partner()
- time.sleep(5) # To run enough_bp()
- if not enough_bp():
- print("Restocking bp")
- time.sleep(5)
- fill_bp()
- time.sleep(5)
- def attempt_kill(bp_required):
- print("Kill attempt")
- button_id_string = 'quest_attack_' + str(bp_required)
- attack_button = browser.find_element_by_id(button_id_string)
- attack_button.click()
- time.sleep(10)
- skip_battle_animation()
- def kill_boss():
- """Should be called on the screen with the battle button.
- Will either kill boss, returning True, or reduce hp to <50%,
- returning False."""
- health = 100
- while True:
- loaded = wait_until_present('<div id="raid_time_rimit"', 30)
- if loaded:
- health = get_boss_health()
- bp = bp_needed()
- if health == 100: # First attack
- setup_kill(bp)
- attempt_kill(bp)
- elif health < 50: # We've done enough damage
- retry_battle()
- return False
- elif health < 100: # (n+1)st attack, not yet reached <50% hp
- retry_battle()
- setup_kill(1)
- attempt_kill(1)
- else:
- """This should mean the boss has been killed."""
- page = browser.execute_script("return document.documentElement.outerHTML")
- if 'Raid Boss Defeated' in page:
- return True
- # Else:
- # Some kind of error. Should not reach this.
- def worth_attacking():
- """If we're using bp pots, we may want to neglect some speed demons.
- If we're using natural bp regen, we'll wait for any of them."""
- boss_status = browser.find_element_by_class_name('quest_boss_status_1').text
- found_red_oni = "Red Oni" in boss_status
- found_speed_demon = "Speed Demon" in boss_status
- return found_red_oni or found_speed_demon
- def boss_level():
- boss_info = browser.find_element_by_class_name('quest_boss_status_1').text
- # I.E, "Dark Beast Lv7"
- parts = boss_info.split(" ")
- # I.E, "Dark", "Beast", "Lv7"
- level = int(parts[-1][2:])
- # I.E, "7"
- return level
- def bp_needed():
- if boss_level() < 40:
- return 1
- else:
- return 2
- def enough_bp():
- return get_bp() >= bp_needed()
- def regen_bp(bp_to_regen):
- """Very naive implementation. Adjust this to keep from logging out."""
- regen_time = 20 * 60 * bp_to_regen
- print("Would sleep for", regen_time, "seconds")
- #time.sleep(regen_time)
- def wait_until_present(string_to_find, time_limit):
- #html = browser.execute_script("return document.documentElement.outerHTML")
- time_waited = 0
- while time_waited < time_limit:
- if string_to_find in browser.execute_script("return document.documentElement.outerHTML"):
- time.sleep(random.uniform(0.5, 1))
- return True
- else:
- time_waited += 0.1
- return False
- def goto_req_assistance():
- html = browser.execute_script("return document.documentElement.outerHTML")
- if 'boss_alerts_1' in html:
- request_assistance = browser.find_element_by_id('boss_alerts_1')
- request_assistance.click()
- return True
- else:
- return False
- def is_boss_dead():
- """This has a name too similar to the one in the main
- loop. Change things to make more sense."""
- """This has to start on a page other than the main page.
- If you start this on the main page it will return True
- much too quickly."""
- goto_main_page()
- loaded = wait_until_present('<div class="button-news"', 10)
- if loaded:
- html = browser.execute_script("return document.documentElement.outerHTML")
- if 'boss_alerts_1' in html:
- """This sometimes gives me a not clickable error.
- Fix it to click using actions."""
- request_assistance = browser.find_element_by_id('boss_alerts_1')
- request_assistance.click()
- else:
- return True
- else:
- return None
- loaded = wait_until_present('Raid Boss List', 10)
- if loaded:
- return not found_boss(username)
- else:
- return None
- def loop_until_boss_is_dead():
- start = time.time()
- waiting = time.time() - start
- while not is_boss_dead():
- if waiting > 60:
- break
- else:
- waiting = time.time() - start
- print("Have been waiting", int(waiting), "seconds")
- time.sleep(5)
- print("End loop")
- """This starts the bot and logs in."""
- browser = webdriver.Chrome('/Users/christopher/Downloads/chromedriver')
- time.sleep(5)
- browser.get('https://www.nutaku.net/games/taimanin-asagi-battle-arena/play/')
- time.sleep(5)
- email_field = browser.find_element_by_id('s-email')
- pw_field = browser.find_element_by_id('s-password')
- email_field.send_keys(email)
- pw_field.send_keys(password)
- pw_field.send_keys(Keys.ENTER)
- time.sleep(5)
- try:
- browser.find_element_by_id('pnCancel').click() # Might have to frame select first?
- except:
- pass
- time.sleep(5)
- # Start Game
- browser.switch_to.frame('game_frame')
- browser.find_element_by_class_name("game_start_over").click()
- time.sleep(5)
- """This is the main loop.
- Does quests, finds/kills bosses, requests assistance where appropriate, etc."""
- looking_for_boss = True
- engaging_boss = False
- checking_boss = False
- counter = 0
- errors = []
- while counter < 1:
- try:
- looking_counter = 0
- while looking_for_boss:
- looking_counter += 1
- if looking_counter > 25:
- looking_for_boss = False
- engaging_boss = False
- checking_boss = True
- try:
- stats = get_quest_stats()
- click_screen()
- time.sleep(4) # Wait a bit
- new_stats = get_quest_stats()
- except:
- pass
- html = browser.execute_script("return document.documentElement.outerHTML")
- if "Sell Cards" in html:
- print("Selling cards")
- sell_cards()
- go_to_quests()
- go_to_stage(1,1)
- elif new_stats['Gold'] > stats['Gold']:
- print('Got gold')
- elif new_stats['Medals'] > stats['Medals']:
- print('Got medals')
- elif new_stats['Cards'] > stats['Cards']:
- print('Got cards')
- elif "Stam Potion" in html:
- print("Topping up stamina")
- refill_stam_during_quest()
- go_to_stage(1,1)
- #elif level up:...
- else:
- """At this point it's either a boss or some kind of error like lag."""
- hit_below_quest_button()
- time.sleep(5)
- html = browser.execute_script("return document.documentElement.outerHTML")
- if "Raid Boss List" in html:
- looking_for_boss = False
- engaging_boss = True
- else:
- print("Error. Lag or level up?")
- looking_for_boss = False
- engaging_boss = False
- checking_boss = True
- #counter = 1000
- # Add error flag here
- #print('Breaking out of loop')
- #break
- while engaging_boss:
- print("Starting boss loop.")
- hit_below_quest_button() # Select first boss, which by default will be yours
- time.sleep(5) # Improve this wait condition
- boss_is_dead = False
- if worth_attacking():
- boss_is_dead = kill_boss()
- time.sleep(5)
- if boss_is_dead: # Handles bosses you didn't fight, too.
- looking_for_boss = True
- engaging_boss = False
- go_to_quests()
- go_to_stage(1,1)
- else:
- request_assistance()
- checking_boss = True
- # Keep checking until boss is dead, then restart
- engaging_boss = False
- print("Finished engaging boss")
- while checking_boss:
- go_to_quests()
- start = time.time()
- waiting = time.time() - start
- while not is_boss_dead():
- if waiting > 600:
- time.sleep(300)
- elif waiting > 60:
- time.sleep(30)
- else:
- #waiting = time.time() - start
- #print("Have been waiting", int(waiting), "seconds")
- time.sleep(5)
- waiting = time.time() - start
- print("Have been waiting", int(waiting), "seconds")
- #time.sleep(5)
- go_to_quests()
- go_to_stage(1,1)
- checking_boss = False
- looking_for_boss = True
- print("End loop")
- except Exception as err:
- print(type(err))
- print(err)
- looking_for_boss = False
- engaging_boss = False
- checking_boss = True
- try: # Sometimes it gets stuck on the button after requesting assistance.
- loaded = wait_until_present('back_button_column_1', 10)
- time.sleep(1)
- if loaded:
- back_btn = browser.find_element_by_class_name("back_button_column_1")
- back_btn.click()
- time.sleep(5)
- except:
- pass
- counter += 1
- print(counter)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement