Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def hide_dungeon(dungeon, s=None):
if dungeon.secret:
raise DungeonSecretException(dungeon)
dungeon.secret = True
s.add(logs.dungeon_hidden(dungeon))
return dungeon
def pay_tab(hero, s=None):
if hero.activity != HeroActivity.VisitingTavern or hero.visiting is None:
raise TavernException("Hero {} not at Tavern.".format(hero))
if hero.money >= constants.HERO_BAR_TAB:
hero.money -= constants.HERO_BAR_TAB
hero.visiting.money += constants.HERO_BAR_TAB
s.add(logs.hero_paid_tab(hero, constants.HERO_BAR_TAB, hero.visiting.owner))
elif hero.money > 0:
s.add(logs.hero_short_on_tab(hero, constants.HERO_BAR_TAB, hero.money, hero.visiting.owner))
hero.visiting.money += hero.money
hero.money = 0
else:
s.add(logs.hero_broke_tab(hero, hero.visiting.owner))
available = available - set(visitors)
pool_heroes = random.sample(available, min(constants.POOL_SIZE, len(available)))
available = available - set(pool_heroes)
random.shuffle(taverns)
for hero, tavern in zip(visitors, taverns):
pool_controller.change_hero_activity(hero, HeroActivity.VisitingTavern, tavern=tavern, s=s)
s.add(logs.hero_visiting(hero, tavern))
for hero in pool_heroes:
pool_controller.change_hero_activity(hero, HeroActivity.CommonPool, s=s)
for hero in available:
pool_controller.change_hero_activity(hero, HeroActivity.Elsewhere, s=s)
s.add(logs.pool_refreshed(visitors, pool_heroes))
available = set(s.query(TavernHero).filter(TavernHero.activity.in_([
HeroActivity.VisitingTavern, HeroActivity.CommonPool, HeroActivity.Elsewhere
]), TavernHero.patron == None).all())
taverns = hq_controller.get_taverns(s=s)
# Divide heroes into three groups: those visiting taverns, those in the pool, and those elsewhere.
visitors = random.sample(available, len(taverns))
available = available - set(visitors)
pool_heroes = random.sample(available, min(constants.POOL_SIZE, len(available)))
available = available - set(pool_heroes)
random.shuffle(taverns)
for hero, tavern in zip(visitors, taverns):
pool_controller.change_hero_activity(hero, HeroActivity.VisitingTavern, tavern=tavern, s=s)
s.add(logs.hero_visiting(hero, tavern))
for hero in pool_heroes:
pool_controller.change_hero_activity(hero, HeroActivity.CommonPool, s=s)
for hero in available:
pool_controller.change_hero_activity(hero, HeroActivity.Elsewhere, s=s)
s.add(logs.pool_refreshed(visitors, pool_heroes))
def end_adventure(adventure, s=None):
hero = adventure.hero
dungeon = adventure.dungeon
tavern = adventure.employer
if not adventure.active:
raise TavernException('Adventure {} is not active.'.format(adventure))
tavern.money += adventure.money_gained
hero.money += adventure.money_gained
pool_controller.increase_cost(hero, adventure.money_gained * constants.MONEY_GAINED_TO_HERO_COST)
adventure.active = False
adventure.end_tick = tick.current_tick(s=s)
s.add(logs.adventure_ended(hero, dungeon, tavern, adventure.money_gained))
def give_money(tavern, money, s=None):
tavern.money += money
s.add(logs.tavern_got_money(tavern, money))
def process_active_adventures(s=None):
for adventure in s.query(TavernAdventure).filter(TavernAdventure.active == True).all():
floor = adventure.floor
if len(floor.monsters) is 0:
adventure_controller.advance_floor(adventure, s=s)
continue
enemy = random.choice(floor.monsters)
result = battle_result(adventure.hero, enemy)
if result == BattleOutcome.INJURED:
if not adventure.hero.injured:
s.add(logs.hero_injured_by_monster(adventure, enemy))
s.add(logs.hero_injured_by_monster(adventure, enemy, adventure.employer.owner))
pool_controller.change_hero_activity(adventure.hero, HeroActivity.Elsewhere, s=s)
adventure_controller.end_adventure(adventure, s=s)
pool_controller.injure_hero(adventure.hero, s=s)
else:
s.add(logs.hero_killed_by_monster(adventure, enemy))
s.add(logs.hero_killed_by_monster(adventure, enemy, adventure.employer.owner))
adventure_controller.fail_adventure(adventure, s=s)
pool_controller.kill_hero(adventure.hero, s=s)
elif result == BattleOutcome.FLEE:
s.add(logs.hero_fled_monster(adventure, enemy, player=adventure.employer.owner))
elif result == BattleOutcome.WIN:
s.add(logs.hero_defeated_monster(adventure, enemy, player=adventure.employer.owner))
dungeon_controller.kill_monster(enemy, s=s)
elif result == BattleOutcome.WIN_LOOT:
loot = dungeon_controller.monster_gold(enemy, s=s)
def process_active_adventures(s=None):
for adventure in s.query(TavernAdventure).filter(TavernAdventure.active == True).all():
floor = adventure.floor
if len(floor.monsters) is 0:
adventure_controller.advance_floor(adventure, s=s)
continue
enemy = random.choice(floor.monsters)
result = battle_result(adventure.hero, enemy)
if result == BattleOutcome.INJURED:
if not adventure.hero.injured:
s.add(logs.hero_injured_by_monster(adventure, enemy))
s.add(logs.hero_injured_by_monster(adventure, enemy, adventure.employer.owner))
pool_controller.change_hero_activity(adventure.hero, HeroActivity.Elsewhere, s=s)
adventure_controller.end_adventure(adventure, s=s)
pool_controller.injure_hero(adventure.hero, s=s)
else:
s.add(logs.hero_killed_by_monster(adventure, enemy))
s.add(logs.hero_killed_by_monster(adventure, enemy, adventure.employer.owner))
adventure_controller.fail_adventure(adventure, s=s)
pool_controller.kill_hero(adventure.hero, s=s)
elif result == BattleOutcome.FLEE:
s.add(logs.hero_fled_monster(adventure, enemy, player=adventure.employer.owner))
elif result == BattleOutcome.WIN:
s.add(logs.hero_defeated_monster(adventure, enemy, player=adventure.employer.owner))
dungeon_controller.kill_monster(enemy, s=s)
elif result == BattleOutcome.WIN_LOOT:
loot = dungeon_controller.monster_gold(enemy, s=s)
s.add(logs.hero_looted_monster(adventure, enemy, loot, player=adventure.employer.owner))
def ensure_hero_count(s=None):
alive = s.query(TavernHero).filter(TavernHero.activity != HeroActivity.Dead, TavernHero.patron == None).count()
idle = s.query(TavernHero).filter(TavernHero.activity.in_(
[HeroActivity.CommonPool, HeroActivity.Elsewhere]), TavernHero.patron == None).count()
to_create = max(constants.HEROES_MIN_ALIVE - alive, constants.HEROES_MIN_IDLE -
idle, hq_controller.count_taverns(s=s) - idle)
created = 0
for i in range(to_create):
hero = pool_controller.generate_hero(s=s)
created += 1
s.add(logs.make_arrival_log(hero))
return created