Advertisement
Guest User

Untitled

a guest
Jun 22nd, 2017
139
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 12.16 KB | None | 0 0
  1. from selenium import webdriver
  2. from selenium.webdriver.common.keys import Keys
  3. import time, random
  4. driver = webdriver.Firefox()
  5.  
  6. url= 'http://crawl.akrasiac.org:8080/#play-dcss-git'
  7. GAME = None
  8. POTIONS = []
  9. SCROLLS = []
  10. RINGS = []
  11. AMULETS = []
  12. def setupGame():
  13.     global GAME
  14.     global driver
  15.     driver.get(url)
  16.     driver.execute_script("document.getElementById('username').value='perbot'")
  17.     driver.execute_script("document.getElementById('password').value='bottarn'")
  18.     time.sleep(1)
  19.     submit = driver.find_element_by_id('submit')
  20.     submit.click()
  21.     GAME = driver.find_element_by_xpath("/html/body")
  22.     time.sleep(2)
  23.     char = random.randint(1,1)
  24.     if char == 1:
  25.         GAME.send_keys('eac')
  26.         print("Kobold Gladiator, rapier")
  27.     elif char == 2:
  28.         GAME.send_keys('gaa')
  29.         print("Kobold fighter, rapier")
  30.     elif char == 3:
  31.         GAME.send_keys('jcf')
  32.         print("Troll monk, claws")
  33.     elif char == 4:
  34.         GAME.send_keys('gac')
  35.         print("Kobold fighter, axe")
  36.     time.sleep(1)
  37.    
  38. def newGame():
  39.     link = driver.find_element_by_link_text('DCSS trunk')
  40.     link.click()
  41.     time.sleep(2)
  42.     char = random.randint(1,1)
  43.     if char == 1:
  44.         GAME.send_keys('eac')
  45.         print("Kobold Gladiator, rapier")
  46.     elif char == 2:
  47.         GAME.send_keys('gaa')
  48.         print("Kobold fighter, rapier")
  49.     elif char == 3:
  50.         GAME.send_keys('jcf')
  51.         print("Troll monk, claws")
  52.     elif char == 4:
  53.         GAME.send_keys('gac')
  54.         print("Kobold fighter, axe")
  55.     time.sleep(3)
  56.     GAME.send_keys('mb')
  57.     time.sleep(1)
  58.     GAME.send_keys(Keys.ESCAPE)
  59.  
  60. def random_walk():
  61.     direction = random.randint(1,8)
  62.     return {1: 1,
  63.             2: 2,
  64.             3: 3,
  65.             4: 6,
  66.             5: 9,
  67.             6: 8,
  68.             7: 7,
  69.             8: 4}[direction]
  70.  
  71. def checkPots():
  72.     global POTIONS
  73.     GAME.send_keys('q')
  74.     print('q')
  75.     time.sleep(0.2)
  76.     try:
  77.         POTIONS = driver.find_element_by_id('menu_contents_inner').text.splitlines()
  78.     except Exception as e:
  79.         print(e)
  80.         POTIONS = []
  81.     GAME.send_keys(Keys.ESCAPE)
  82.     print(POTIONS)
  83.     time.sleep(0.2)
  84.        
  85. def checkScrolls():
  86.     global SCROLLS
  87.     GAME.send_keys('r')
  88.     time.sleep(0.2)
  89.     try:
  90.         SCROLLS = driver.find_element_by_id('menu_contents_inner').text.splitlines()
  91.     except:
  92.         SCROLLS = []
  93.     GAME.send_keys(Keys.ESCAPE)
  94.     print(SCROLLS)
  95.     time.sleep(0.2)
  96.        
  97. def checkJewelry():
  98.     global JEWELRY
  99.     GAME.send_keys('P')
  100.     time.sleep(0.2)
  101.     try:
  102.         JEWELRY = driver.find_element_by_id('menu_contents_inner').text.splitlines()
  103.     except:
  104.         JEWELRY = []
  105.     GAME.send_keys(Keys.ESCAPE)
  106.     print(JEWELRY)
  107.     time.sleep(0.2)
  108.    
  109. def handleStarvation():
  110.     GAME.send_keys('e')
  111.     time.sleep(0.2)
  112.     food = driver.find_element_by_id('menu_contents_inner').text.splitlines()
  113.     if len(food) > 1:
  114.         return food[1][1:2]
  115.     return Keys.ESCAPE+'.'
  116.    
  117. def cure_pot():
  118.     global POTIONS
  119.     time.sleep(0.2)
  120.    
  121.     curing = [x for x in POTIONS if "curing" in x]
  122.     if curing:
  123.         print("Taking curing, lethaaal", curing[0][1])
  124.         checkPots()
  125.         return curing[0][1]
  126.        
  127.     return Keys.ESCAPE+'.'
  128.  
  129. def quaffMight():
  130.     global POTIONS
  131.     time.sleep(0.2)
  132.     if not POTIONS or not [x for x in POTIONS if "might" in x]:
  133.         return Keys.ESCAPE + Keys.TAB
  134.    
  135.     status = driver.find_element_by_id('stats_status_lights').text.splitlines()
  136.        
  137.     if "Might" not in status:
  138.         print("Quaffing might, nastyyy", [x for x in POTIONS if "might" in x][0][1])
  139.         command = "q"+[x for x in POTIONS if "might" in x][0][1]
  140.         checkPots()
  141.         return command
  142.     else:
  143.         return Keys.ESCAPE + Keys.TAB
  144.        
  145. def panic_mode():
  146.     global POTIONS
  147.     time.sleep(0.2)
  148.        
  149.     status = driver.find_element_by_id('stats_status_lights').text.splitlines()
  150.    
  151.     pots_to_use = ''
  152.     used = False
  153.     healing = [x for x in POTIONS if "heal wounds" in x]
  154.     if healing:
  155.         pots_to_use += 'q'
  156.         pots_to_use += healing[0][1]
  157.         used = True
  158.     might = [x for x in POTIONS if "might" in x]
  159.     if "Might" not in status and might:
  160.         print("Drinking might pot because Might not in status lolers", status)
  161.         pots_to_use += 'q'
  162.         pots_to_use += might[0][1]
  163.         used = True
  164.     if used:
  165.         print(pots_to_use)
  166.         checkPots()
  167.         return pots_to_use + Keys.ESCAPE
  168.     return Keys.ESCAPE + Keys.TAB
  169.    
  170. def readScrolls():
  171.     GAME.send_keys('r')
  172.     time.sleep(0.2)
  173.     scrolls = []
  174.     try:
  175.         scrolls = driver.find_element_by_id('menu_contents_inner').text.splitlines()
  176.         scrolls = [scroll[1:] for scroll in scrolls if "label" in scroll
  177.                                                     or "enchant" in scroll
  178.                                                     or "identify" in scroll]
  179.     except:
  180.         GAME.send_keys(Keys.ESCAPE)
  181.         time.sleep(0.2)
  182.         return False
  183.     GAME.send_keys(Keys.ESCAPE)
  184.     time.sleep(0.2)
  185.     more_scrolls = len(scrolls) > 0
  186.     for scroll in scrolls:
  187.         GAME.send_keys('r')
  188.         print('r')
  189.         time.sleep(0.2)
  190.         GAME.send_keys(scroll[0])
  191.         print(scroll[0])
  192.         time.sleep(0.2)
  193.         messages = driver.find_element_by_id('messages').text.splitlines()
  194.         last_message = messages[-1]
  195.         if "unidentified" in last_message:
  196.             continue
  197.         if "enchant" in last_message or "enchant" in scroll:  
  198.             print("Enchant scroll!")
  199.             time.sleep(0.2)
  200.             GAME.send_keys(Keys.SPACE)
  201.             time.sleep(0.2)
  202.             enchantables = driver.find_element_by_id('menu_contents_inner').text.splitlines()
  203.             GAME.send_keys(enchantables[1][1])
  204.             print(enchantables[1][1])
  205.             time.sleep(0.2)
  206.         elif "recharging" in last_message:
  207.             print("Recharge! Ignoring!")
  208.             time.sleep(0.2)
  209.             GAME.send_keys(Keys.ESCAPE)
  210.             time.sleep(0.2)
  211.             GAME.send_keys(Keys.ESCAPE)
  212.             time.sleep(0.2)
  213.             GAME.send_keys('Y')
  214.             time.sleep(0.2)
  215.         elif "identify" in last_message or "identify" in scroll:
  216.             print("Identifying!")
  217.             time.sleep(0.2)
  218.             GAME.send_keys(Keys.SPACE)
  219.             time.sleep(0.2)
  220.             identifiables = driver.find_element_by_id('menu_contents_inner').text.splitlines()
  221.             time.sleep(0.2)
  222.             try:
  223.                 pot_index = identifiables.index('Potions')
  224.             except:
  225.                 pot_index = 0
  226.             identifiables = identifiables[pot_index+1:]
  227.             GAME.send_keys(identifiables[0][1])
  228.             print(identifiables[0][1])
  229.             time.sleep(0.2)
  230.            
  231.         elif "Blink" in last_message or "Blink" in messages[-2]:
  232.             print("Blinkscroll! Ignoring!")
  233.             time.sleep(0.2)
  234.             GAME.send_keys(Keys.ESCAPE)
  235.             time.sleep(0.2)
  236.             GAME.send_keys('Y')
  237.             print('Y')
  238.             time.sleep(0.2)
  239.     messages = driver.find_element_by_id('messages').text.splitlines()
  240.     last_message = messages[-1]
  241.     if "unidentified" in last_message:
  242.         return False
  243.     return more_scrolls
  244.    
  245. def decideMove(previous_move):
  246.     try:
  247.         hp_bar = driver.find_element_by_id('stats_hp_bar_full')
  248.         hp_bar = hp_bar.get_attribute('style')[7:10]
  249.         if hp_bar[-1] != '0':
  250.             hp_bar = hp_bar[:-1]
  251.         if hp_bar == "0%" or hp_bar == "0p":
  252.             hp_bar = driver.find_element_by_id('stats_hp_bar_increase')
  253.             hp_bar = hp_bar.get_attribute('style')[7:10]
  254.             if hp_bar[-1] != '0':
  255.                 hp_bar = hp_bar[:-1]
  256.         if hp_bar == "0%" or hp_bar == "0p":
  257.             hp_bar = driver.find_element_by_id('stats_hp_bar_poison')
  258.             hp_bar = hp_bar.get_attribute('style')[7:10]
  259.             if hp_bar[-1] != '0':
  260.                 hp_bar = hp_bar[:-1]
  261.         if hp_bar == "0%" or hp_bar == "0p":
  262.             hp_bar = driver.find_element_by_id('stats_hp_bar_decrease')
  263.             hp_bar = hp_bar.get_attribute('style')[7:10]
  264.             if hp_bar[-1] != '0':
  265.                 hp_bar = hp_bar[:-1]
  266.         if hp_bar[1] == '.':
  267.             hp_bar = hp_bar[0]
  268.         elif hp_bar[1] == '%':
  269.             hp_bar = hp_bar[0]
  270.         elif hp_bar[1] == 'p':
  271.             hp_bar = hp_bar[0]
  272.     except:
  273.         pass
  274.     more = driver.find_element_by_id('more')
  275.     more = more.get_attribute('style')
  276.     if 'none' not in more:
  277.         return Keys.SPACE
  278.    
  279.     last_message = ""
  280.     try:    
  281.         messages = driver.find_element_by_id('messages').text.splitlines()
  282.         last_message = messages[-1]
  283.     except:
  284.         pass
  285.        
  286.     try:
  287.         nasties = driver.find_element_by_class_name('nasty').text
  288.     except:
  289.         nasties = []
  290.     status = driver.find_element_by_id('stats_status_lights').text.splitlines()
  291.    
  292.     if "You die..." in last_message:
  293.         return "game over!"
  294.     elif "Beogh" in status:
  295.         print("Beogh found!")
  296.         GAME.send_keys('a')
  297.         time.sleep(0.2)
  298.         GAME.send_keys('Y')
  299.         time.sleep(0.2)
  300.         GAME.send_keys(Keys.SPACE)
  301.         time.sleep(0.2)
  302.         GAME.send_keys(Keys.SPACE)
  303.         time.sleep(0.2)
  304.         GAME.send_keys('Y')
  305.         time.sleep(0.2)    
  306.         return Keys.SPACE
  307.     elif "Shop" in last_message or "entrance" in last_message:
  308.         print("Shop, leaving!")
  309.         return Keys.ESCAPE
  310.     elif "Starving" in status:
  311.         print("Starvin in status works")
  312.         return handleStarvation()
  313.     elif "too hungry" in last_message:
  314.         print("Starving!")
  315.         return handleStarvation()
  316.     elif "Increase (S)trength, " in last_message and previous_move != 'd':
  317.         print("Statup!")
  318.         return 'd'
  319.     elif "Done explor" in last_message or "Partly explor" in last_message:
  320.         print("Reading scrolls!")
  321.         while readScrolls():
  322.             pass
  323.         print("Checking pots!")
  324.         checkPots()
  325.         print("Checking scrolls!")
  326.         checkScrolls()
  327.         print("Checking jewelry!")
  328.         checkJewelry()
  329.         print("Next floor!")
  330.         return "G>"
  331.     elif " confused" in last_message:
  332.         print("Waiting for confusion to pass!")
  333.         return "."+Keys.SPACE
  334.     elif "lethal" in last_message:
  335.         print("Lethal poison, taking cure!")
  336.         return cure_pot()
  337.     elif "No reachable target" in last_message and "nearby" in messages[-2]:
  338.         print("Can't reach!")
  339.         GAME.send_keys(Keys.SPACE)
  340.         return random_walk()
  341.     elif "target in view" in last_message:
  342.         print("No enemy near, exploration time!")
  343.         return 'o'
  344.     elif "[Y]" in last_message or "Keep eating" in last_message:
  345.         print("Whatever, yes, yes, yes!")
  346.         return 'Y'
  347.     elif "strange disturbance" in last_message or "travel-excluded" in last_message:
  348.         return random_walk()
  349.     elif int(hp_bar) <= 25:
  350.         print("PANIC!")
  351.         return panic_mode()
  352.     elif "strange disturbance" in last_message or "travel-excluded" in last_message or "smoke" in last_message:
  353.         print("INVIS! or excluded lol")
  354.         return random_walk()
  355.     if nasties and "Might" not in status:
  356.         print("Strong dudes near, get mighty")
  357.         return quaffMight()
  358.     else:
  359.         print("Whack 'em!")
  360.         return Keys.TAB
  361.    
  362. def main():
  363.     next_move = '.'
  364.     setupGame()
  365.     while(True):
  366.         time.sleep(0.2)
  367.         while (True):
  368.             try:
  369.                 next_move = decideMove(next_move)
  370.                 break
  371.             except Exception as e:
  372.                 print(e)
  373.                 time.sleep(0.5)
  374.                 pass
  375.         try:
  376.             print(next_move)
  377.         except:
  378.             pass
  379.         if next_move == "game over!":
  380.             GAME.send_keys(Keys.ESCAPE)
  381.             time.sleep(0.5)
  382.             GAME.send_keys(Keys.ESCAPE)
  383.             time.sleep(2)
  384.             newGame()
  385.         else:
  386.             GAME.send_keys(next_move)
  387.        
  388.    
  389. if __name__=="__main__":
  390.     main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement