def get_new_card(self, rarity=None, exclude=(), available_for_auction=None): cards_types = relations.CARD_TYPE.records if available_for_auction is None: available_for_auction=self._hero.is_premium if not self._hero.is_premium: cards_types = [card for card in cards_types if not card.availability.is_FOR_PREMIUMS] if rarity: cards_types = [card for card in cards_types if card.rarity == rarity] cards_choices = [logic.create_card(type=card_type, available_for_auction=available_for_auction) for card_type in cards_types if effects.EFFECTS[card_type].available()] if exclude: cards_choices = [card for card in cards_choices if not any(card.is_same_effect(excluded_card) for excluded_card in exclude)] prioritites = [(card, card.type.rarity.priority) for card in cards_choices] card = random_value_by_priority(prioritites) if card: self.add_card(card) return card
def get_new_card(self, rarity=None, exclude=(), available_for_auction=None): cards_types = relations.CARD_TYPE.records if available_for_auction is None: available_for_auction = self.is_next_card_premium() if not self.is_next_card_premium(): cards_types = [card for card in cards_types if not card.availability.is_FOR_PREMIUMS] # choose only from ingame cards cards_types = [card for card in cards_types if card.in_game] if rarity: cards_types = [card for card in cards_types if card.rarity == rarity] cards_choices = [logic.create_card(type=card_type, available_for_auction=available_for_auction) for card_type in cards_types if effects.EFFECTS[card_type].available()] if exclude: cards_choices = [card for card in cards_choices if not any(card.is_same_effect(excluded_card) for excluded_card in exclude)] prioritites = [(card, card.type.rarity.priority) for card in cards_choices] card = random_value_by_priority(prioritites) if card: self.add_card(card) return card
def get_new_artifact_data(self): artifact_type = random_value_by_priority([(record, record.priority) for record in self.ARTIFACT_TYPE_CHOICES.records][-self.INTERVAL:]) if artifact_type.is_LOOT: return artifacts_storage.loot, artifact_type.rarity return artifacts_storage.artifacts, artifact_type.rarity
def repair_artifact(self): artifacts = self.artifacts_to_break() if not artifacts: artifacts = self.artifacts_to_break(from_all=True) if not artifacts: return None choices = [] for artifact in artifacts: slot = artifact.type.equipment_slot if self.preferences.favorite_item == slot: choices.append((slot, c.SPECIAL_SLOT_REPAIR_PRIORITY)) if self.preferences.equipment_slot == slot: choices.append((slot, c.SPECIAL_SLOT_REPAIR_PRIORITY)) else: choices.append((slot, c.NORMAL_SLOT_REPAIR_PRIORITY)) slot = random_value_by_priority(choices) artifact = self.equipment.get(slot) artifact.repair_it() return artifact
def try_companion_strike(attacker, defender, messanger): if not attacker.has_companion: return False if random.random() > c.COMPANIONS_BATTLE_STRIKE_PROBABILITY: return False abilities = attacker.companion.modify_attribute( heroes_relations.MODIFIERS.ADDITIONAL_ABILITIES, heroes_relations.MODIFIERS.ADDITIONAL_ABILITIES.default() ) battle_abilities = [ ability for ability in abilities if ability.TYPE.is_BATTLE and ability.ACTIVATION_TYPE.is_ACTIVE ] if not battle_abilities: return False ability = random_value_by_priority([(ability, ability.priority) for ability in battle_abilities]) companion_actor = CompanionActor(attacker, contexts.BattleContext()) _strike(ability, companion_actor, defender, messanger) return True
def try_companion_strike(attacker, defender, messenger): if not attacker.has_companion: return False if attacker.companion.is_dead: return False if random.random() > c.COMPANIONS_BATTLE_STRIKE_PROBABILITY: return False abilities = attacker.companion.modify_attribute( heroes_relations.MODIFIERS.ADDITIONAL_ABILITIES, heroes_relations.MODIFIERS.ADDITIONAL_ABILITIES.default()) battle_abilities = [ ability for ability in abilities if ability.TYPE.is_BATTLE and ability.ACTIVATION_TYPE.is_ACTIVE ] if not battle_abilities: return False ability = random_value_by_priority([(ability, ability.priority) for ability in battle_abilities]) companion_actor = CompanionActor(attacker, contexts.BattleContext()) _strike(ability, companion_actor, defender, messenger) return True
def get_rarity_type(self, hero): choices = ((relations.RARITY.NORMAL, relations.RARITY.NORMAL.probability), (relations.RARITY.RARE, relations.RARITY.RARE.probability * hero.rare_artifact_probability_multiplier), (relations.RARITY.EPIC, relations.RARITY.EPIC.probability * hero.epic_artifact_probability_multiplier)) return random_value_by_priority(choices)
def switch_spending(self): spending_candidates = self.spending_priorities() if self.companion is None or self.companion_heal_disabled(): spending_candidates[relations.ITEMS_OF_EXPENDITURE.HEAL_COMPANION] = 0 self.next_spending = random_value_by_priority(list(spending_candidates.items())) self.quests.mark_updated()
def switch_spending(self): spending_candidates = self.spending_priorities() if self.companion is None or self.companion_heal_disabled(): spending_candidates[relations.ITEMS_OF_EXPENDITURE.HEAL_COMPANION] = 0 self._model.next_spending = random_value_by_priority(list(spending_candidates.items())) self.quests.mark_updated()
def update_job(job, actor_id): power = job.load_power(actor_id) if not job.is_completed(power): return () inner_circle = job.load_inner_circle(actor_id) job_effect = job.get_apply_effect_method(power) effect_kwargs = { 'actor_type': 'place', 'actor_name': job.get_project_name(actor_id), 'positive_heroes': inner_circle.positive_heroes, 'negative_heroes': inner_circle.negative_heroes, 'job_power': job.get_job_power(actor_id) } effect_kwargs.update(job.get_objects(actor_id)) after_update_operations = job_effect(**effect_kwargs) effects_priorities = dict(job.get_effects_priorities(actor_id)) if job.effect in effects_priorities: del effects_priorities[job.effect] new_effect = utils_logic.random_value_by_priority( effects_priorities.items()) if power.positive > power.negative: impact = tt_api_impacts.PowerImpact( type=tt_api_impacts.IMPACT_TYPE.JOB, actor_type=job.ACTOR_TYPE, actor_id=actor_id, target_type=job.POSITIVE_TARGET_TYPE, target_id=actor_id, amount=-power.positive) else: impact = tt_api_impacts.PowerImpact( type=tt_api_impacts.IMPACT_TYPE.JOB, actor_type=job.ACTOR_TYPE, actor_id=actor_id, target_type=job.NEGATIVE_TARGET_TYPE, target_id=actor_id, amount=-power.negative) politic_power_logic.add_power_impacts([impact]) job.name = create_name(job.ACTOR, new_effect) job.created_at_turn = turn.number() job.effect = new_effect job.power_required = job.NORMAL_POWER * new_effect.power_modifier return after_update_operations
def get_new_artifact_data(self): artifact_type = random_value_by_priority([ (record, record.priority) for record in self.ARTIFACT_TYPE_CHOICES.records ][-self.INTERVAL:]) if artifact_type.is_LOOT: return artifacts_storage.loot, artifact_type.rarity return artifacts_storage.artifacts, artifact_type.rarity
def choose_ability(self): choice_abilities = [ (ability, ability.priority) for ability in self.actor.abilities.active_abilities if ability.can_be_used(self) ] choice_abilities += [ (ability, ability.priority) for ability in self.actor.additional_abilities if ability.activation_type.is_ACTIVE and ability.can_be_used(self) ] return random_value_by_priority(choice_abilities)
def job_artifact(self, place_id, person_id, message_type, job_power): rarity = utils_logic.random_value_by_priority(((artifacts_relations.RARITY.RARE, c.RARE_ARTIFACT_PROBABILITY), (artifacts_relations.RARITY.EPIC, c.EPIC_ARTIFACT_PROBABILITY * job_power))) artifact, unequipped, sell_price = self.receive_artifact(equip=False, better=True, prefered_slot=False, prefered_item=True, archetype=True, rarity_type=rarity) self.add_message(message_type, diary=True, hero=self, artifact=artifact, **self.get_job_variables(place_id, person_id))
def job_artifact(self, place_id, person_id, message_type, job_power): rarity = utils_logic.random_value_by_priority(((artifacts_relations.RARITY.RARE, c.RARE_ARTIFACT_PROBABILITY), (artifacts_relations.RARITY.EPIC, c.EPIC_ARTIFACT_PROBABILITY * job_power))) artifact, unequipped, sell_price = self.receive_artifact(equip=False, better=True, prefered_slot=False, prefered_item=False, archetype=True, rarity_type=rarity) self.add_message(message_type, diary=True, hero=self, artifact=artifact, **self.get_job_variables(place_id, person_id))
def process_bot(self, bot, enemy): from the_tale.game.pvp.abilities import ABILITIES, Flame properties = self.get_bot_pvp_properties() if random.uniform(0.0, 1.0) > properties['ability_chance']: return used_ability_type = random_value_by_priority(properties['priorities'].items()) if used_ability_type == Flame.TYPE and enemy.pvp.energy_speed == 1: return ABILITIES[used_ability_type](hero=bot, enemy=enemy).use()
def test_random_value_by_priority(self): counter = collections.Counter() choices = [('0', 0), ('a', 1), ('b', 10), ('c', 100)] counter.update( [random_value_by_priority(choices) for i in xrange(10000)]) self.assertTrue(counter['0'] == 0) self.assertTrue(counter['a']) self.assertTrue(counter['b']) self.assertTrue(counter['c']) self.assertTrue( counter['0'] < counter['a'] < counter['b'] < counter['c'])
def choose_mob(self, mobs_choices): global_actions_mobs = [] normal_mobs = [] for mob in mobs_choices: if mob.global_action_probability > 0: global_actions_mobs.append(mob) else: normal_mobs.append(mob) action_probability = sum(mob.global_action_probability for mob in global_actions_mobs if mob.global_action_probability) if random.random() > action_probability: return random.choice(normal_mobs) return random_value_by_priority((mob, mob.global_action_probability) for mob in global_actions_mobs)
def test_random_value_by_priority(self): counter = collections.Counter() choices = [('0', 0), ('a', 1), ('b', 10), ('c', 100)] counter.update([random_value_by_priority(choices) for i in xrange(10000)]) self.assertTrue(counter['0'] == 0) self.assertTrue(counter['a']) self.assertTrue(counter['b']) self.assertTrue(counter['c']) self.assertTrue(counter['0'] < counter['a'] < counter['b'] < counter['c'])
def get_random_mob(self, hero, mercenary=None, is_boss=False): self.sync() mobs = self._get_mobs_choices(level=hero.level, terrain=hero.position.get_terrain(), mercenary=mercenary) if not mobs: return None mob_record = common_logic.random_value_by_priority(mobs) return objects.Mob(record_id=mob_record.id, level=hero.level, is_boss=is_boss, action_type=hero.actions.current_action.ui_type, terrain=hero.position.get_terrain())
def create_card(allow_premium_cards, rarity=None, exclude=(), available_for_auction=False): cards_types = list(cards.CARD.records) if not allow_premium_cards: cards_types = [card for card in cards_types if not card.availability.is_FOR_PREMIUMS] if rarity: cards_types = [card for card in cards_types if card.rarity == rarity] cards_choices = [card.effect.create_card(available_for_auction=available_for_auction, type=card) for card in cards_types if card.effect.available(card)] if exclude: cards_choices = [card for card in cards_choices if not any(card.is_same_effect(excluded_card) for excluded_card in exclude)] prioritites = [(card, card.type.rarity.priority) for card in cards_choices] return random_value_by_priority(prioritites)
def sharp(self, distribution, max_power, force=False): choices = [] if force or self.power.physic < max_power.physic: choices.append(('physic', distribution.physic)) if force or self.power.magic < max_power.magic: choices.append(('magic', distribution.magic)) if not choices: return False if random_value_by_priority(choices) == 'physic': self.power.physic += 1 else: self.power.magic += 1 self.max_integrity -= int(self.max_integrity * c.ARTIFACT_SHARP_MAX_INTEGRITY_LOST_FRACTION) self.integrity = min(self.integrity, self.max_integrity) return True
def update_job(self): from . import logic if not self.job.is_completed(): return () job_effect = self.job.get_apply_effect_method() after_update_operations = job_effect(**self.politic_power.job_effect_kwargs(self)) job_effects_priorities = self.job_effects_priorities() if self.job.effect in job_effects_priorities: del job_effects_priorities[self.job.effect] new_effect = utils_logic.random_value_by_priority(job_effects_priorities.items()) self.job.new_job(new_effect, normal_power=logic.NORMAL_PLACE_JOB_POWER) return after_update_operations
def get_reward_type(self): return random_value_by_priority([(record, record.priority) for record in relations.RANDOM_PREMIUM_CHEST_REWARD.records])
def get_reward_type(self): return random_value_by_priority([ (record, record.priority) for record in relations.RANDOM_PREMIUM_CHEST_REWARD.records ])
def get_rarity_type(self, hero): choices = ( (relations.RARITY.NORMAL, relations.RARITY.NORMAL.probability), (relations.RARITY.RARE, relations.RARITY.RARE.probability * hero.rare_artifact_probability_multiplier), (relations.RARITY.EPIC, relations.RARITY.EPIC.probability * hero.epic_artifact_probability_multiplier)) return random_value_by_priority(choices)