Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def find_hero(heroId=None, name=None, patron=None, s=None):
if heroId:
return s.query(TavernHero).filter(TavernHero.id == heroId).first()
elif name:
return s.query(TavernHero).filter(TavernHero.name == name).first()
elif patron:
s.add(patron)
return s.query(TavernHero).filter(TavernHero.patron.id == patron.id).first()
def reset_pool(s=None):
unhired = set(s.query(TavernHero).filter(TavernHero.activity.in_([
HeroActivity.VisitingTavern, HeroActivity.CommonPool
]), TavernHero.patron == None).all())
for hero in unhired:
pool_controller.degrade_cost(hero)
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)
def print_debug(s=None):
for hero in s.query(TavernHero).all():
print("There is a hero {} that is {}".format(hero.info_string(), hero.activity))
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
def start_adventure(hero_id, dungeon_id, hiring_tavern_id=None, s=None):
hero = s.query(TavernHero).filter(TavernHero.id == hero_id).first()
dungeon = s.query(TavernDungeon).filter(TavernDungeon.id == dungeon_id).first()
tavern = None if hiring_tavern_id is None else s.query(Tavern).filter(Tavern.id == hiring_tavern_id).first()
if not (hero.activity == HeroActivity.Hired and tavern is not None and hero.employer == tavern) and not (tavern is None and hero.activity == HeroActivity.Elsewhere):
raise TavernException('Hero {} cannot start an adventure with tavern {} from state {}'.format(
hero, tavern, hero.activity))
if not dungeon.active:
raise TavernException('Cannot send hero to adventure in inactive dungeon {}'.format(dungeon))
adventure = TavernAdventure(hero=hero, dungeon=dungeon, employer=tavern,
floor=dungeon_controller.get_floor(dungeon.id, 0, s=s),
active=True, money_gained=0, start_tick=tick.current_tick(s=s))
s.add(adventure)
pool_controller.change_hero_activity(hero, HeroActivity.Adventuring, s=s)
s.add(logs.adventure_started(hero, dungeon, tavern))
def find_hero(heroId=None, name=None, patron=None, s=None):
if heroId:
return s.query(TavernHero).filter(TavernHero.id == heroId).first()
elif name:
return s.query(TavernHero).filter(TavernHero.name == name).first()
elif patron:
s.add(patron)
return s.query(TavernHero).filter(TavernHero.patron.id == patron.id).first()
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
def heal_heroes(s=None):
for hero in s.query(TavernHero).filter(TavernHero.activity.in_([
HeroActivity.Elsewhere, HeroActivity.CommonPool, HeroActivity.VisitingTavern]),
TavernHero.injured == True):
if (hero.patron is None and random.random() < constants.HERO_HEAL_CHANCE) or \
(hero.patron is not None and random.random() < constants.RESIDENT_HERO_HEAL_CHANCE):
pool_controller.heal_hero(hero, s=s)
def make_long_description(entity, s=None):
if isinstance(entity, Tavern):
return hq_controller.tavern_details(entity, s=s)
if isinstance(entity, TavernHero):
return pool_controller.hero_details(entity, s=s)
if isinstance(entity, TavernDungeon):
return dungeon_controller.dungeon_details(entity, s=s)