def attack_hero(self, target=None): """Attack hero by autopilot.""" if self.hostile: target = target or level.hero self.attack_hero_flag = True log(10) path = self.fullpath(target.loc) log(20) loc = first(path) if loc: self.move_to(Loc(*loc))
def check_can_log_via_ssh_using_saved_pwd(step, name, user): ip = utils.nova_cli.get_instance_ip(name) assert_true(ip != '', name) assert_true(world.saved_root_password is not None) conf.log(conf.get_bash_log_file(), "load world.saved_root_password=%s" % world.saved_root_password) step_assert(step).assert_true( utils.ssh(ip, command="/bin/ls -l /", user=user, password=world.saved_root_password).successful())
def move_program(self, count): """Create a program to move in given direction (count) times.""" log("- - in move_program()") field.status(count) c = field.scr.getch() if 48 <= c <= 57: # example: 22l count = int('%d%d' % (count, c-48)) field.status(count) c = field.scr.getch() if str(c) in conf.directions: self.program = (count, c)
def populate(self, kind="random"): """Create monsters and place them on the level map.""" self.num = levels.current # make monsters self.monsters = [] # keep strategic view monsters when in tactical map # monster we are attacking now so that we can kill him in # strategic view if we kill him in tactical num_monsters = random.randrange(2,6) if conf.xmax < 10: num_monsters = 1 if conf.xmax < 2: num_monsters = 0 monster_level = round(self.num*random.random()*6) for i in range(num_monsters): if kind == "random" or random.random() > 0.75: b = being.rand() else: b = kind log(b) m = being.Being(b, field.random(), self.last_index) self.last_index += 1 m.place() m.team = 'monsters' log(m.loc) self.monsters.append(m) log(self.monsters) # make items #field.set(field.random(), Item('down')) num_items = int(round(self.num*random.random())) if conf.xmax < 10: num_items = 1 if conf.xmax < 2: num_items = 0 if num_items > 4: num_items = 4 for i in range(num_items): if random.choice((0,1)): field.set(field.random(), weapon.Weapon(weapon.rand(), self.last_index)) else: field.set(field.random(), armor.Armor(armor.rand(), self.last_index)) self.last_index += 1 if conf.mode == "tactical": return log("populate(): levels.current=%d, conf.levels: %d \n" % (levels.current, conf.levels)) down, up = None, None if levels.current == 1: down = field.random() field.set(down, Item("down")) elif levels.current+1 == conf.levels: up = field.random() field.set(up, Item("up")) else: up = field.random() down = field.random() field.set(down, Item("down")) field.set(up, Item("up")) self.down, self.up = down, up
def start_vm_instance_save_root_pwd(step, name,image, flavor): id_image_list = utils.nova_cli.get_image_id_list(image) assert_equals(len(id_image_list), 1, "There are %s images with name %s: %s" % (len(id_image_list), image, str(id_image_list))) id_flavor_list = utils.nova_cli.get_flavor_id_list(flavor) assert_equals(len(id_flavor_list), 1, "There are %s flavors with name %s: %s" % (len(id_flavor_list), flavor, str(id_flavor_list))) image_id = id_image_list[0] flavor_id = id_flavor_list[0] assert_true(image_id != '', image_id) assert_true(flavor_id != '', flavor_id) table = utils.nova_cli.start_vm_instance_return_output(name, image_id, flavor_id) assert_true(table is not None) passwords = table.select_values('Value', 'Property', 'adminPass') step_assert(step).assert_equals(len(passwords), 1, "there should be one and only one adminPass") world.saved_root_password = passwords[0] conf.log(conf.get_bash_log_file(),"store world.saved_root_password=%s" % world.saved_root_password)
def find_closest_monster(self): """ Used by hero's team. UNUSED Bug: finds closest by straight line even if it's blocked. Returns the monster instance. """ from level import level lst = [] for being in level.monsters: log(repr(self.loc)) log(being, repr(being.loc)) if being.alive: lst.append( (field.distance(self.loc, being.loc), being) ) lst.sort(key=itemgetter(0)) if lst: return lst[0][1]
def attack(self, target): """Attack target.""" log("attack(): self.kind, target.kind: %s, %s" % (self.kind, target.kind)) if self.team == target.team: return if conf.mode == "strategical": self.enter_tactical(target) return # alert target if target.program: target.program = None attack = self.attack_val # attack with weapon if self.weapon: attack += self.weapon.attack attack_text = ' with %s [%d]' % (self.weapon.kind, self.weapon.attack) else: attack_text = '' # attack armored foe defense = target.defense_val defense_text = '' if target.armor: defense += target.armor.defense defense_text = ' (protected by %s armor [%d])' % (target.armor.description, target.armor.defense) # cast a blow rand = random.random() mod = (attack*rand/1.5 - defense*rand/2.0) mod = max(0, mod) target.health -= round(mod) kill = '' if target.health <= 0: kill = "and killed him" field.text.append('%s%s hit %s%s for %d hp %s' % (self.kind, defense_text, target.kind, attack_text, mod, kill)) # if killed if target.health <= 0: self.experience += target.health + target.attack_val + target.defense_val target.die()
def __init__(self, host, command=None, user=None, key=None, password=None): options='-q -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null' user_prefix = '' if user is None else '%s@' % user if key is not None: options += ' -i %s' % key cmd = "ssh {options} {user_prefix}{host} {command}".format(options=options, user_prefix=user_prefix, host=host, command=command) conf.log(conf.get_bash_log_file(),cmd) if password is None: super(ssh,self).__init__(bash(cmd).output) else: super(ssh,self).__init__(self.__use_expect(cmd, password))
def __init__(self, host, command=None, user=None, key=None, password=None): options = '-q -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null' user_prefix = '' if user is None else '%s@' % user if key is not None: options += ' -i %s' % key cmd = "ssh {options} {user_prefix}{host} {command}".format( options=options, user_prefix=user_prefix, host=host, command=command) conf.log(conf.get_bash_log_file(), cmd) if password is None: super(ssh, self).__init__(bash(cmd).output) else: super(ssh, self).__init__(self.__use_expect(cmd, password))
def start_vm_instance_save_root_pwd(step, name, image, flavor): id_image_list = utils.nova_cli.get_image_id_list(image) assert_equals( len(id_image_list), 1, "There are %s images with name %s: %s" % (len(id_image_list), image, str(id_image_list))) id_flavor_list = utils.nova_cli.get_flavor_id_list(flavor) assert_equals( len(id_flavor_list), 1, "There are %s flavors with name %s: %s" % (len(id_flavor_list), flavor, str(id_flavor_list))) image_id = id_image_list[0] flavor_id = id_flavor_list[0] assert_true(image_id != '', image_id) assert_true(flavor_id != '', flavor_id) table = utils.nova_cli.start_vm_instance_return_output( name, image_id, flavor_id) assert_true(table is not None) passwords = table.select_values('Value', 'Property', 'adminPass') step_assert(step).assert_equals( len(passwords), 1, "there should be one and only one adminPass") world.saved_root_password = passwords[0] conf.log(conf.get_bash_log_file(), "store world.saved_root_password=%s" % world.saved_root_password)
def tactical_wall_check(self): all_near_wall = True party = [level.hoplite, level.fencer, level.mage] for hero in party: log("checking hero: %s" % (hero.kind)) if not hero.next_to("wall"): log("hero: %s is NOT close to a wall." % (hero.kind)) all_near_wall = False break else: log("hero: %s IS close to a wall." % (hero.kind)) hero.advance() return all_near_wall
def enter_tactical(self, target): """Enter tactical mode.""" if self.team == "monsters": level.attacking_monster = level.monsters.index(self) am_kind = self.kind if target.team == "monsters": log("attack() - target: %s %s" % (target.kind, target)) log("attack() - level.monsters: %s" % str(level.monsters)) level.attacking_monster = level.monsters.index(target) am_kind = target.kind log("level.attacking_monster: %s" % level.attacking_monster) level.save_monsters = deepcopy(level.monsters) log("level.save_monsters: %s" % level.save_monsters) cur = levels.current levels.list[cur] = field.fld, level.down, level.up field.load_map("local") if conf.xmax<10: field.blank() conf.mode = "tactical" level.populate(am_kind) while True: loc = field.random() if not field.next_to(loc, "wall"): break level.hoplite.place(loc) # very rarely a hero may be placed next to a wall, for hero in [level.fencer, level.mage]: for x in range(100): max_dist = random.randint(1,8) rloc = field.random() if (field.distance(loc, rloc) <= max_dist) and not field.next_to(rloc, "wall"): break hero.place(rloc) field.full_display()
def main(self): """ Main loop. Create hero's team; move them; create new levels when old ends. """ level_num = 1 field.load_map("empty") level.populate() # make hero's party level.hero = Being('party', field.random(), level.last_index) level.last_index += 1 level.hero.place() level.hoplite = Being("hoplite", Loc(1,1), level.last_index) level.last_index += 1 level.fencer = Being("fencer", Loc(1,2), level.last_index) level.last_index += 1 level.mage = Being("mage", Loc(1,3), level.last_index) level.last_index += 1 field.full_display() # game loop while True: time.sleep(0.01) monster = level.hero.find_closest_monster() if conf.mode == "strategical": party = [level.hero] elif conf.mode == "tactical": party = [level.hoplite, level.fencer, level.mage] level.hoplite.heal() level.fencer.heal() level.mage.heal() all_near_wall = True for cur_being in party: if not cur_being.alive: continue if not level.monsters and conf.mode == "tactical" and not test: break if cur_being.program: log("- - cur being (%s) has a program.." % cur_being.kind) times, direction = cur_being.program ok = cur_being.move(direction) # program may have been reseted in being.move() if cur_being.program: log("- - cur being STILL has a program..") times -= 1 if (not ok) or (times < 1): # bumped into something or end of program cur_being.program = None log("- - resetting program because bumped into something?..") else: cur_being.program = times, direction else: health_bar() # log("- - cur being (%s) has NO program..\n" % cur_being.kind) loc = cur_being.loc # log("kind: %s, x: %d y: %d;\n" % (cur_being.kind, x, y)) field.display() field.scr.move(loc.y-1, loc.x-1) c = field.scr.getch() if c in keymap.keys(): exec(commands[keymap[c]] + '()') elif 49 <= c <= 57: # e.g. '5l' moves 5 times to the left cur_being.move_program(c-48) move_res = cur_being.move(c) if type(move_res) == type(cur_being): # live monster monster = move_res # cur_being.attack(monster) if conf.mode == "strategical" and random.random() < ask_chance and not monster.asked: monster.ask(cur_being) else: cur_being.attack(monster) # check if all party is near a wall in tactical mode: if conf.mode == "tactical": all_near_wall = True party = [level.hoplite, level.fencer, level.mage] for hero in party: log("checking hero: %s" % (hero.kind)) if not hero.next_to("wall"): log("hero: %s is NOT close to a wall." % (hero.kind)) all_near_wall = False break else: log("hero: %s IS close to a wall." % (hero.kind)) hero.advance() if all_near_wall or conf.auto_combat: break field.msg() log("loop: all_near_wall: %s" % all_near_wall) if ((not level.monsters or all_near_wall or conf.auto_combat) and conf.mode == "tactical" and not test): we_won = False if not level.monsters or conf.auto_combat: we_won = True cur = levels.current field.fld, level.down, level.up = levels.list[cur] conf.mode = "strategical" level.monsters = level.save_monsters # killed the monster team.. level.hero.program = None attacker = level.monsters[level.attacking_monster] if we_won: attacker.die() else: # we need to try to move away from the enemy (running away!!) dir = field.get_rev_dir(level.hero.location, attacker.location) level.hero.move(dir) conf.auto_combat = False field.full_display() else: move(level_num) field.display() field.msg()
def __init__(self, step): self.step = step conf.log(conf.get_bash_log_file(),"asserting in step: %s" % step.sentence)
def command_output_function(): dst = os.path.join(debug.current_bunch_path(),file_to_save) conf.log(dst, bash(command).output_text())
def special_attack(self): """ Special - positional attack. Can only be used by mage and depends on position of other team and target. """ log("--- in special_attack()") if not self.kind == "mage": field.text.append("Only mage can perform special attacks.") return for name, a_dict in conf.special_attacks.items(): log("--- checking attack '%s'" % name) a_moves1 = a_dict["ally1"] a_moves2 = a_dict["ally2"] t_moves = a_dict["target"] rotations = [[a_moves1, a_moves2, t_moves]] # add 3 more rotations for i in range(1, 4): am1 = copy(a_moves1) am2 = copy(a_moves2) tm = copy(t_moves) for x in range(i): am1 = field.rotate_moves(am1) am2 = field.rotate_moves(am2) tm = field.rotate_moves(tm) rotations.append([am1, am2, tm]) no = True for (am1, am2, tm) in rotations: log("--- rotation: '%s'" % str((am1, am2, tm))) for a_mv in (am1, am2): log("--- a_mv: '%s'" % str(a_mv)) a_loc = field.get_loc(self.loc, a_mv) log("--- a_loc: '%s'" % str(a_loc)) items = field[a_loc] no = True for i in items: log("--- w.kind: '%s'" % str(w.kind)) if i.kind in ("hoplite", "fencer"): no = False if no: log("no!") break else: log("pass!") t_loc = field.get_loc(self.loc, tm) items = field[t_loc] no = True monster = None for i in items: if i.alive and (i.team == "monsters"): monster = i if name.startswith("arrow"): if "magical" in monster.specials: no = False elif name == "trap": if "animal" in monster.specials: log("--- animal in monster.specials") no = False else: no = False if no: log("target: no!") else: log("target: pass!") break # this special attack can't be done now, let's continue checking other attacks if no: continue log("- special_attack(): name = %s" % name) # we can perform this special attack! if name.startswith("arrow"): dmg = random.randint(5,15) attack_text = " with Arrow of Punishment" elif name == "trap": dmg = random.randint(5,15) attack_text = " with Trap of Hunter" elif name == "arc": dmg = random.randint(5,15) attack_text = " with Arc of Electrocution" monster.health -= dmg kill = "" if monster.health <= 0: kill = "and killed him" field.text.append('%s hit %s%s for %d hp %s' % (self.kind, monster.kind, attack_text, dmg, kill)) if monster.health <= 0: monster.die()
def next_to(self, kind): """Am I close to an object of given type.""" rc = field.next_to(self.loc, kind) log("- next_to(): rc = %s" % rc) return rc
def command_output_function(): dst = os.path.join(debug.current_bunch_path(), file_to_save) conf.log(dst, bash(command).output_text())
def __init__(self, step): self.step = step conf.log(conf.get_bash_log_file(), "asserting in step: %s" % step.sentence)
def main(self): """ Main loop. Create hero's team; move them; create new levels when old ends. """ level_num = 1 self.init_level() # game loop while True: time.sleep(0.01) monster = level.hero.find_closest_monster() if conf.mode == "strategical": party = [level.hero] elif conf.mode == "tactical": party = [level.hoplite, level.fencer, level.mage] level.hoplite.heal() level.fencer.heal() level.mage.heal() all_near_wall = True for cur_being in party: if not cur_being.alive: continue if not level.monsters and conf.mode == "tactical" and not test: break if cur_being.program: cur_being.handle_program() else: self.manual_move(cur_being) # check if all party is near a wall in tactical mode: if conf.mode == "tactical": all_near_wall = self.tactical_wall_check() if all_near_wall or conf.auto_combat: break field.msg() log("loop: all_near_wall: %s" % all_near_wall) if (not level.monsters or all_near_wall or conf.auto_combat) and conf.mode == "tactical" and not test: we_won = not level.monsters or conf.auto_combat cur = levels.current field.fld, level.down, level.up = levels.list[cur] conf.mode = "strategical" level.monsters = level.save_monsters # killed the monster team.. level.hero.program = None attacker = level.monsters[level.attacking_monster] if we_won: attacker.die() else: # we need to try to move away from the enemy (running away!!) dir = field.get_rev_dir(level.hero.location, attacker.location) level.hero.move(dir) conf.auto_combat = False field.full_display(level.party) else: self.move(level_num) field.full_display(level.hero) field.msg()
def check_can_log_via_ssh_using_saved_pwd(step, name, user): ip = utils.nova_cli.get_instance_ip(name) assert_true(ip != '', name) assert_true(world.saved_root_password is not None) conf.log(conf.get_bash_log_file(),"load world.saved_root_password=%s" % world.saved_root_password) step_assert(step).assert_true(utils.ssh(ip, command="/bin/ls -l /", user=user, password=world.saved_root_password).successful())