def create_random_quest_for_hero(hero_info, logger): start_time = time.time() normal_mode = True quests = shuffle_values_by_priority(hero_info.quests_priorities) excluded_quests = hero_info.excluded_quests quest_type, knowledge_base = try_to_create_random_quest_for_hero(hero_info, quests, excluded_quests, without_restrictions=False, logger=logger) if knowledge_base is None: normal_mode = False quest_type, knowledge_base = try_to_create_random_quest_for_hero(hero_info, quests, excluded_quests=[], without_restrictions=True, logger=logger) spent_time = time.time() - start_time logger.info(u'hero[%(hero_id).6d]: %(spent_time)s %(is_normal)s %(quest_type)20s (allowed: %(allowed)s) (excluded: %(excluded)s)' % {'hero_id': hero_info.id, 'spent_time': spent_time, 'is_normal': normal_mode, 'quest_type': quest_type, 'allowed': ', '.join(quest.quest_class.TYPE for quest in quests), 'excluded': ', '.join(excluded_quests)}) return knowledge_base
def test_shuffle_values_by_priority(self): counter = collections.Counter() choices = [('0', 0), ('a', 1), ('b', 10), ('c', 100)] for i in xrange(10000): for j, value in enumerate(shuffle_values_by_priority(choices)): counter.update([(value, j)]) self.assertTrue(counter[('c', 0)] > counter[('c', 1)] > counter[('c', 2)] > counter[('c', 3)]) self.assertTrue(counter[('b', 1)] > counter[('b', 0)] > counter[('b', 3)] and counter[('b', 1)] > counter[('b', 2)] > counter[('b', 3)]) self.assertTrue(counter[('a', 2)] > counter[('a', 0)] > counter[('a', 3)] and counter[('a', 2)] > counter[('a', 1)] > counter[('a', 0)])
def test_shuffle_values_by_priority(self): counter = collections.Counter() choices = [('0', 0), ('a', 1), ('b', 10), ('c', 100)] for i in xrange(10000): for j, value in enumerate(shuffle_values_by_priority(choices)): counter.update([(value, j)]) self.assertTrue(counter[('c', 0)] > counter[('c', 1)] > counter[( 'c', 2)] > counter[('c', 3)]) self.assertTrue( counter[('b', 1)] > counter[('b', 0)] > counter[('b', 3)] and counter[('b', 1)] > counter[('b', 2)] > counter[('b', 3)]) self.assertTrue( counter[('a', 2)] > counter[('a', 0)] > counter[('a', 3)] and counter[('a', 2)] > counter[('a', 1)] > counter[('a', 0)])
def create_random_quest_for_hero(hero_info, logger): start_time = time.time() normal_mode = True quests = shuffle_values_by_priority(hero_info.quests_priorities) excluded_quests = hero_info.excluded_quests quest_type, knowledge_base = try_to_create_random_quest_for_hero( hero_info, quests, excluded_quests, without_restrictions=False, logger=logger) if knowledge_base is None: normal_mode = False quest_type, knowledge_base = try_to_create_random_quest_for_hero( hero_info, quests, excluded_quests=[], without_restrictions=True, logger=logger) spent_time = time.time() - start_time logger.info( 'hero[%(hero_id).6d]: %(spent_time)s %(is_normal)s %(quest_type)20s (allowed: %(allowed)s) (excluded: %(excluded)s)' % { 'hero_id': hero_info.id, 'spent_time': spent_time, 'is_normal': normal_mode, 'quest_type': quest_type, 'allowed': ', '.join(quest.quest_class.TYPE for quest in quests), 'excluded': ', '.join(excluded_quests) }) return knowledge_base