Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from selenium import webdriver
- from selenium.webdriver.common.keys import Keys
- import time, random
- driver = webdriver.Firefox()
- url= 'http://crawl.akrasiac.org:8080/#play-dcss-git'
- GAME = None
- POTIONS = []
- SCROLLS = []
- RINGS = []
- AMULETS = []
- def setupGame():
- global GAME
- global driver
- driver.get(url)
- driver.execute_script("document.getElementById('username').value='perbot'")
- driver.execute_script("document.getElementById('password').value='bottarn'")
- time.sleep(1)
- submit = driver.find_element_by_id('submit')
- submit.click()
- GAME = driver.find_element_by_xpath("/html/body")
- time.sleep(2)
- char = random.randint(1,1)
- if char == 1:
- GAME.send_keys('eac')
- print("Kobold Gladiator, rapier")
- elif char == 2:
- GAME.send_keys('gaa')
- print("Kobold fighter, rapier")
- elif char == 3:
- GAME.send_keys('jcf')
- print("Troll monk, claws")
- elif char == 4:
- GAME.send_keys('gac')
- print("Kobold fighter, axe")
- time.sleep(1)
- def newGame():
- link = driver.find_element_by_link_text('DCSS trunk')
- link.click()
- time.sleep(2)
- char = random.randint(1,1)
- if char == 1:
- GAME.send_keys('eac')
- print("Kobold Gladiator, rapier")
- elif char == 2:
- GAME.send_keys('gaa')
- print("Kobold fighter, rapier")
- elif char == 3:
- GAME.send_keys('jcf')
- print("Troll monk, claws")
- elif char == 4:
- GAME.send_keys('gac')
- print("Kobold fighter, axe")
- time.sleep(3)
- GAME.send_keys('mb')
- time.sleep(1)
- GAME.send_keys(Keys.ESCAPE)
- def random_walk():
- direction = random.randint(1,8)
- return {1: 1,
- 2: 2,
- 3: 3,
- 4: 6,
- 5: 9,
- 6: 8,
- 7: 7,
- 8: 4}[direction]
- def checkPots():
- global POTIONS
- GAME.send_keys('q')
- print('q')
- time.sleep(0.2)
- try:
- POTIONS = driver.find_element_by_id('menu_contents_inner').text.splitlines()
- except Exception as e:
- print(e)
- POTIONS = []
- GAME.send_keys(Keys.ESCAPE)
- print(POTIONS)
- time.sleep(0.2)
- def checkScrolls():
- global SCROLLS
- GAME.send_keys('r')
- time.sleep(0.2)
- try:
- SCROLLS = driver.find_element_by_id('menu_contents_inner').text.splitlines()
- except:
- SCROLLS = []
- GAME.send_keys(Keys.ESCAPE)
- print(SCROLLS)
- time.sleep(0.2)
- def checkJewelry():
- global JEWELRY
- GAME.send_keys('P')
- time.sleep(0.2)
- try:
- JEWELRY = driver.find_element_by_id('menu_contents_inner').text.splitlines()
- except:
- JEWELRY = []
- GAME.send_keys(Keys.ESCAPE)
- print(JEWELRY)
- time.sleep(0.2)
- def handleStarvation():
- GAME.send_keys('e')
- time.sleep(0.2)
- food = driver.find_element_by_id('menu_contents_inner').text.splitlines()
- if len(food) > 1:
- return food[1][1:2]
- return Keys.ESCAPE+'.'
- def cure_pot():
- global POTIONS
- time.sleep(0.2)
- curing = [x for x in POTIONS if "curing" in x]
- if curing:
- print("Taking curing, lethaaal", curing[0][1])
- checkPots()
- return curing[0][1]
- return Keys.ESCAPE+'.'
- def quaffMight():
- global POTIONS
- time.sleep(0.2)
- if not POTIONS or not [x for x in POTIONS if "might" in x]:
- return Keys.ESCAPE + Keys.TAB
- status = driver.find_element_by_id('stats_status_lights').text.splitlines()
- if "Might" not in status:
- print("Quaffing might, nastyyy", [x for x in POTIONS if "might" in x][0][1])
- command = "q"+[x for x in POTIONS if "might" in x][0][1]
- checkPots()
- return command
- else:
- return Keys.ESCAPE + Keys.TAB
- def panic_mode():
- global POTIONS
- time.sleep(0.2)
- status = driver.find_element_by_id('stats_status_lights').text.splitlines()
- pots_to_use = ''
- used = False
- healing = [x for x in POTIONS if "heal wounds" in x]
- if healing:
- pots_to_use += 'q'
- pots_to_use += healing[0][1]
- used = True
- might = [x for x in POTIONS if "might" in x]
- if "Might" not in status and might:
- print("Drinking might pot because Might not in status lolers", status)
- pots_to_use += 'q'
- pots_to_use += might[0][1]
- used = True
- if used:
- print(pots_to_use)
- checkPots()
- return pots_to_use + Keys.ESCAPE
- return Keys.ESCAPE + Keys.TAB
- def readScrolls():
- GAME.send_keys('r')
- time.sleep(0.2)
- scrolls = []
- try:
- scrolls = driver.find_element_by_id('menu_contents_inner').text.splitlines()
- scrolls = [scroll[1:] for scroll in scrolls if "label" in scroll
- or "enchant" in scroll
- or "identify" in scroll]
- except:
- GAME.send_keys(Keys.ESCAPE)
- time.sleep(0.2)
- return False
- GAME.send_keys(Keys.ESCAPE)
- time.sleep(0.2)
- more_scrolls = len(scrolls) > 0
- for scroll in scrolls:
- GAME.send_keys('r')
- print('r')
- time.sleep(0.2)
- GAME.send_keys(scroll[0])
- print(scroll[0])
- time.sleep(0.2)
- messages = driver.find_element_by_id('messages').text.splitlines()
- last_message = messages[-1]
- if "unidentified" in last_message:
- continue
- if "enchant" in last_message or "enchant" in scroll:
- print("Enchant scroll!")
- time.sleep(0.2)
- GAME.send_keys(Keys.SPACE)
- time.sleep(0.2)
- enchantables = driver.find_element_by_id('menu_contents_inner').text.splitlines()
- GAME.send_keys(enchantables[1][1])
- print(enchantables[1][1])
- time.sleep(0.2)
- elif "recharging" in last_message:
- print("Recharge! Ignoring!")
- time.sleep(0.2)
- GAME.send_keys(Keys.ESCAPE)
- time.sleep(0.2)
- GAME.send_keys(Keys.ESCAPE)
- time.sleep(0.2)
- GAME.send_keys('Y')
- time.sleep(0.2)
- elif "identify" in last_message or "identify" in scroll:
- print("Identifying!")
- time.sleep(0.2)
- GAME.send_keys(Keys.SPACE)
- time.sleep(0.2)
- identifiables = driver.find_element_by_id('menu_contents_inner').text.splitlines()
- time.sleep(0.2)
- try:
- pot_index = identifiables.index('Potions')
- except:
- pot_index = 0
- identifiables = identifiables[pot_index+1:]
- GAME.send_keys(identifiables[0][1])
- print(identifiables[0][1])
- time.sleep(0.2)
- elif "Blink" in last_message or "Blink" in messages[-2]:
- print("Blinkscroll! Ignoring!")
- time.sleep(0.2)
- GAME.send_keys(Keys.ESCAPE)
- time.sleep(0.2)
- GAME.send_keys('Y')
- print('Y')
- time.sleep(0.2)
- messages = driver.find_element_by_id('messages').text.splitlines()
- last_message = messages[-1]
- if "unidentified" in last_message:
- return False
- return more_scrolls
- def decideMove(previous_move):
- try:
- hp_bar = driver.find_element_by_id('stats_hp_bar_full')
- hp_bar = hp_bar.get_attribute('style')[7:10]
- if hp_bar[-1] != '0':
- hp_bar = hp_bar[:-1]
- if hp_bar == "0%" or hp_bar == "0p":
- hp_bar = driver.find_element_by_id('stats_hp_bar_increase')
- hp_bar = hp_bar.get_attribute('style')[7:10]
- if hp_bar[-1] != '0':
- hp_bar = hp_bar[:-1]
- if hp_bar == "0%" or hp_bar == "0p":
- hp_bar = driver.find_element_by_id('stats_hp_bar_poison')
- hp_bar = hp_bar.get_attribute('style')[7:10]
- if hp_bar[-1] != '0':
- hp_bar = hp_bar[:-1]
- if hp_bar == "0%" or hp_bar == "0p":
- hp_bar = driver.find_element_by_id('stats_hp_bar_decrease')
- hp_bar = hp_bar.get_attribute('style')[7:10]
- if hp_bar[-1] != '0':
- hp_bar = hp_bar[:-1]
- if hp_bar[1] == '.':
- hp_bar = hp_bar[0]
- elif hp_bar[1] == '%':
- hp_bar = hp_bar[0]
- elif hp_bar[1] == 'p':
- hp_bar = hp_bar[0]
- except:
- pass
- more = driver.find_element_by_id('more')
- more = more.get_attribute('style')
- if 'none' not in more:
- return Keys.SPACE
- last_message = ""
- try:
- messages = driver.find_element_by_id('messages').text.splitlines()
- last_message = messages[-1]
- except:
- pass
- try:
- nasties = driver.find_element_by_class_name('nasty').text
- except:
- nasties = []
- status = driver.find_element_by_id('stats_status_lights').text.splitlines()
- if "You die..." in last_message:
- return "game over!"
- elif "Beogh" in status:
- print("Beogh found!")
- GAME.send_keys('a')
- time.sleep(0.2)
- GAME.send_keys('Y')
- time.sleep(0.2)
- GAME.send_keys(Keys.SPACE)
- time.sleep(0.2)
- GAME.send_keys(Keys.SPACE)
- time.sleep(0.2)
- GAME.send_keys('Y')
- time.sleep(0.2)
- return Keys.SPACE
- elif "Shop" in last_message or "entrance" in last_message:
- print("Shop, leaving!")
- return Keys.ESCAPE
- elif "Starving" in status:
- print("Starvin in status works")
- return handleStarvation()
- elif "too hungry" in last_message:
- print("Starving!")
- return handleStarvation()
- elif "Increase (S)trength, " in last_message and previous_move != 'd':
- print("Statup!")
- return 'd'
- elif "Done explor" in last_message or "Partly explor" in last_message:
- print("Reading scrolls!")
- while readScrolls():
- pass
- print("Checking pots!")
- checkPots()
- print("Checking scrolls!")
- checkScrolls()
- print("Checking jewelry!")
- checkJewelry()
- print("Next floor!")
- return "G>"
- elif " confused" in last_message:
- print("Waiting for confusion to pass!")
- return "."+Keys.SPACE
- elif "lethal" in last_message:
- print("Lethal poison, taking cure!")
- return cure_pot()
- elif "No reachable target" in last_message and "nearby" in messages[-2]:
- print("Can't reach!")
- GAME.send_keys(Keys.SPACE)
- return random_walk()
- elif "target in view" in last_message:
- print("No enemy near, exploration time!")
- return 'o'
- elif "[Y]" in last_message or "Keep eating" in last_message:
- print("Whatever, yes, yes, yes!")
- return 'Y'
- elif "strange disturbance" in last_message or "travel-excluded" in last_message:
- return random_walk()
- elif int(hp_bar) <= 25:
- print("PANIC!")
- return panic_mode()
- elif "strange disturbance" in last_message or "travel-excluded" in last_message or "smoke" in last_message:
- print("INVIS! or excluded lol")
- return random_walk()
- if nasties and "Might" not in status:
- print("Strong dudes near, get mighty")
- return quaffMight()
- else:
- print("Whack 'em!")
- return Keys.TAB
- def main():
- next_move = '.'
- setupGame()
- while(True):
- time.sleep(0.2)
- while (True):
- try:
- next_move = decideMove(next_move)
- break
- except Exception as e:
- print(e)
- time.sleep(0.5)
- pass
- try:
- print(next_move)
- except:
- pass
- if next_move == "game over!":
- GAME.send_keys(Keys.ESCAPE)
- time.sleep(0.5)
- GAME.send_keys(Keys.ESCAPE)
- time.sleep(2)
- newGame()
- else:
- GAME.send_keys(next_move)
- if __name__=="__main__":
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement