def test_linked_choices(self): start = facts.Start(uid='start', type='test', nesting=0) choice_1 = facts.Choice(uid='choice_1') choice_2 = facts.Choice(uid='choice_2') finish_1 = facts.Finish(uid='finish_1', results={}, nesting=0, start='start') finish_2 = facts.Finish(uid='finish_2', results={}, nesting=0, start='start') option_1 = facts.Option(state_from=choice_1.uid, state_to=finish_1.uid, type='opt_1', markers=()) option_2 = facts.Option(state_from=choice_1.uid, state_to=choice_2.uid, type='opt_2', markers=()) option_2_1 = facts.Option(state_from=choice_2.uid, state_to=finish_1.uid, type='opt_2_1', markers=()) option_2_2 = facts.Option(state_from=choice_2.uid, state_to=finish_2.uid, type='opt_2_2', markers=()) facts_list = [ start, choice_1, choice_2, finish_1, finish_2, option_1, option_2, option_2_1, option_2_2, facts.OptionsLink(options=(option_1.uid, option_2_1.uid)), facts.OptionsLink(options=(option_2.uid, option_2_2.uid)) ] self.kb += facts_list transformators.determine_default_choices(self.kb) self.check_in_knowledge_base(self.kb, facts_list) self.assertEqual(len(list(self.kb.filter(facts.ChoicePath))), 2) self.assertEqual( len(set([path.choice for path in self.kb.filter(facts.ChoicePath)])), 2) chosen_options = set( [path.option for path in self.kb.filter(facts.ChoicePath)]) self.assertTrue( chosen_options == set([option_1.uid, option_2_1.uid]) or chosen_options == set([option_2.uid, option_2_2.uid]))
def test_linked_choices__linked_option_with_processed_choice(self): is_raised = False for i in xrange(100): kb = KnowledgeBase() start = facts.Start(uid='start', type='test', nesting=0) choice_1 = facts.Choice(uid='choice_1') choice_2 = facts.Choice(uid='choice_2') finish_1 = facts.Finish(uid='finish_1', results={}, nesting=0, start='start') finish_2 = facts.Finish(uid='finish_2', results={}, nesting=0, start='start') option_1 = facts.Option(state_from=choice_1.uid, state_to=finish_1.uid, type='opt_1', markers=()) option_2 = facts.Option(state_from=choice_1.uid, state_to=choice_2.uid, type='opt_2', markers=()) option_2_1 = facts.Option(state_from=choice_2.uid, state_to=finish_1.uid, type='opt_2_1', markers=()) option_2_2 = facts.Option(state_from=choice_2.uid, state_to=finish_2.uid, type='opt_2_2', markers=()) facts_list = [ start, facts.Jump(state_from=start.uid, state_to=choice_1.uid), choice_1, choice_2, finish_1, finish_2, option_1, option_2, option_2_1, option_2_2, facts.OptionsLink(options=(option_2.uid, option_2_2.uid)) ] kb += facts_list try: transformators.determine_default_choices(kb) except exceptions.LinkedOptionWithProcessedChoiceError: is_raised = True self.assertEqual(len(list(kb.filter(facts.ChoicePath))), 1) self.assertEqual( [path.choice for path in kb.filter(facts.ChoicePath)], [choice_2.uid]) self.assertEqual( [path.option for path in kb.filter(facts.ChoicePath)], [option_2_1.uid]) break self.assertFalse(is_raised)
def test_linked_choices__option_with_two_links(self): start = facts.Start(uid='start', type='test', nesting=0) choice_1 = facts.Choice(uid='choice_1') choice_2 = facts.Choice(uid='choice_2') finish_1 = facts.Finish(uid='finish_1', results={}, nesting=0, start='start') finish_2 = facts.Finish(uid='finish_2', results={}, nesting=0, start='start') option_1 = facts.Option(state_from=choice_1.uid, state_to=finish_1.uid, type='opt_1', markers=()) option_2 = facts.Option(state_from=choice_1.uid, state_to=choice_2.uid, type='opt_2', markers=()) option_2_1 = facts.Option(state_from=choice_2.uid, state_to=finish_1.uid, type='opt_2_1', markers=()) option_2_2 = facts.Option(state_from=choice_2.uid, state_to=finish_2.uid, type='opt_2_2', markers=()) facts_list = [ start, choice_1, choice_2, finish_1, finish_2, option_1, option_2, option_2_1, option_2_2, facts.OptionsLink(options=(option_1.uid, option_2_1.uid)), facts.OptionsLink(options=(option_2.uid, option_2_2.uid, option_1.uid)) ] self.kb += facts_list self.assertRaises(exceptions.OptionWithTwoLinksError, transformators.determine_default_choices, self.kb) self.check_in_knowledge_base(self.kb, facts_list) self.assertEqual(len(list(self.kb.filter(facts.ChoicePath))), 0)
def test_broken_linked_options(self): choice_1 = facts.Choice(uid='choice_1') choice_2 = facts.Choice(uid='choice_2') o_1_2 = facts.Option(state_from=choice_1.uid, state_to=choice_2.uid, type='o', markers=()) o_1_f1 = facts.Option(state_from=choice_1.uid, state_to='st_finish_1', type='o', markers=()) o_2_f1 = facts.Option(state_from=choice_2.uid, state_to='st_finish_1', type='o', markers=()) o_2_f2 = facts.Option(state_from=choice_2.uid, state_to='st_finish_2', type='o', markers=()) facts_list = [ facts.Start(uid='start', type='test', nesting=0), facts.Jump(state_from='start', state_to=choice_1.uid), choice_1, o_1_f1, facts.Finish(uid='st_finish_1', results={}, nesting=0, start='start') ] self.kb += facts_list broken = [ choice_2, o_1_2, o_2_f1, o_2_f2, facts.Finish(uid='st_finish_2', results={}, nesting=1, start='start') ] self.kb += broken self.kb += [facts.OptionsLink(options=[o_1_2.uid, o_2_f2.uid])] transformators.remove_broken_states(self.kb) self.check_in_knowledge_base(self.kb, facts_list) self.check_not_in_knowledge_base(self.kb, broken)
def construct(cls, nesting, selector, initiator, initiator_position, receiver, receiver_position): hero = selector.heroes()[0] ns = selector._kb.get_next_ns() black_market = selector.new_place() start = facts.Start(uid=ns+'start', type=cls.TYPE, nesting=nesting, description=u'Начало: караван', require=[requirements.LocatedIn(object=hero.uid, place=initiator_position.uid)], actions=[actions.Message(type='intro')]) participants = [facts.QuestParticipant(start=start.uid, participant=initiator.uid, role=ROLES.INITIATOR), facts.QuestParticipant(start=start.uid, participant=receiver.uid, role=ROLES.RECEIVER), facts.QuestParticipant(start=start.uid, participant=black_market.uid, role=ROLES.ANTAGONIST_POSITION)] path_percents_1 = random.uniform(0.1, 0.3) path_percents_2 = random.uniform(0.4, 0.6) path_percents_3 = random.uniform(0.7, 0.9) first_moving = facts.State(uid=ns+'first_moving', description=u'двигаемся с караваном', require=[requirements.LocatedOnRoad(object=hero.uid, place_from=initiator_position.uid, place_to=receiver_position.uid, percents=path_percents_1)]) caravan_choice = facts.Choice(uid=ns+'caravan_choice', description=u'Решение: защитить или ограбить') first_defence = facts.Choice(uid=ns+'first_defence', description=u'первая защита', require=[requirements.LocatedOnRoad(object=hero.uid, place_from=initiator_position.uid, place_to=receiver_position.uid, percents=path_percents_2)], actions=[actions.Message(type='defence')]) first_defence_continue = facts.Question(uid=ns+'first_defence_continue', description=u'удалось ли защитить караван?', condition=[requirements.IsAlive(object=hero.uid)], actions=[actions.Fight()]) second_moving = facts.State(uid=ns+'second_moving', description=u'двигаемся с караваном', require=[requirements.LocatedOnRoad(object=hero.uid, place_from=initiator_position.uid, place_to=receiver_position.uid, percents=path_percents_3)]) second_defence = facts.Question(uid=ns+'second_defence', description=u'вторая защита', condition=(requirements.IsAlive(object=hero.uid),), actions=(actions.Message(type='defence'), actions.Fight(), )) finish_defence = facts.Finish(uid=ns+'finish_defence', start=start.uid, results={ initiator.uid: RESULTS.SUCCESSED, receiver.uid: RESULTS.SUCCESSED, black_market.uid: RESULTS.NEUTRAL }, nesting=nesting, description=u'Караван приходит в точку назначения', require=[requirements.LocatedIn(object=hero.uid, place=receiver_position.uid)], actions=[actions.GiveReward(object=hero.uid, type='finish_defence'), actions.GivePower(object=initiator.uid, power=1), actions.GivePower(object=receiver.uid, power=1)]) move_to_attack = facts.State(uid=ns+'move_to_attack', description=u'ведём караван в засаду', require=[requirements.LocatedOnRoad(object=hero.uid, place_from=initiator_position.uid, place_to=receiver_position.uid, percents=path_percents_2)]) attack = facts.Question(uid=ns+'attack', description=u'нападение', condition=(requirements.IsAlive(object=hero.uid),), actions=(actions.Message(type='attack'), actions.Fight(mercenary=True), )) run = facts.State(uid=ns+'run', description=u'скрываемся с места преступления', actions=(actions.MoveNear(object=hero.uid),)) fight = facts.Question(uid=ns+'fight', description=u'защита награбленного', condition=(requirements.IsAlive(object=hero.uid),), actions=(actions.Message(type='fight'), actions.Fight(mercenary=True), )) hide = facts.State(uid=ns+'hide', description=u'прячемся', actions=(actions.Message(type='hide'), actions.MoveNear(object=hero.uid))) finish_attack = facts.Finish(uid=ns+'finish_attack', start=start.uid, results={ initiator.uid: RESULTS.FAILED, receiver.uid: RESULTS.FAILED, black_market.uid: RESULTS.SUCCESSED }, nesting=nesting, description=u'Продать товар на чёрном рынке', require=[requirements.LocatedIn(object=hero.uid, place=black_market.uid)], actions=[actions.GiveReward(object=hero.uid, type='finish_attack', scale=1.5), actions.GivePower(object=initiator.uid, power=-1), actions.GivePower(object=receiver.uid, power=-1), actions.GivePower(object=black_market.uid, power=1)]) finish_defence_failed = facts.Finish(uid=ns+'finish_defence_failed', start=start.uid, results={ initiator.uid: RESULTS.NEUTRAL, receiver.uid: RESULTS.NEUTRAL, black_market.uid: RESULTS.NEUTRAL }, nesting=nesting, description=u'Герой не смог защитить караван', actions=[actions.Message(type='finish_defence_failed'), actions.GivePower(object=initiator.uid, power=-1), actions.GivePower(object=receiver.uid, power=-1)]) finish_attack_failed = facts.Finish(uid=ns+'finish_attack_failed', start=start.uid, results={ initiator.uid: RESULTS.NEUTRAL, receiver.uid: RESULTS.NEUTRAL, black_market.uid: RESULTS.NEUTRAL }, nesting=nesting, description=u'Герой не смог ограбить караван', actions=[actions.Message(type='finish_attack_failed'), actions.GivePower(object=initiator.uid, power=1), actions.GivePower(object=receiver.uid, power=1)]) caravan_choice__first_defence = facts.Option(state_from=caravan_choice.uid, state_to=first_defence.uid, type='jump_defence', markers=[relations.OPTION_MARKERS.HONORABLE], start_actions=[actions.Message(type='choose_defence'),]) caravan_choice__move_to_attack = facts.Option(state_from=caravan_choice.uid, state_to=move_to_attack.uid, type='jump_attack', markers=[relations.OPTION_MARKERS.DISHONORABLE], start_actions=[actions.Message(type='choose_attack'),]) first_defence__first_defence_continue = facts.Option(state_from=first_defence.uid, state_to=first_defence_continue.uid, type='jump_defence', markers=[relations.OPTION_MARKERS.HONORABLE], ) first_defence__move_to_attack = facts.Option(state_from=first_defence.uid, state_to=move_to_attack.uid, type='jump_attack', markers=[relations.OPTION_MARKERS.DISHONORABLE], start_actions=[actions.Message(type='choose_attack'),]) line = [ start, facts.Jump(state_from=start.uid, state_to=first_moving.uid, start_actions=(actions.Message(type='first_moving'), )), first_moving, facts.Jump(state_from=first_moving.uid, state_to=caravan_choice.uid), caravan_choice, caravan_choice__first_defence, caravan_choice__move_to_attack, first_defence, first_defence__first_defence_continue, first_defence__move_to_attack, first_defence_continue, facts.Answer(state_from=first_defence_continue.uid, state_to=second_moving.uid, condition=True), facts.Answer(state_from=first_defence_continue.uid, state_to=finish_defence_failed.uid, condition=False), second_moving, facts.Jump(state_from=second_moving.uid, state_to=second_defence.uid), second_defence, facts.Answer(state_from=second_defence.uid, state_to=finish_defence.uid, condition=True), facts.Answer(state_from=second_defence.uid, state_to=finish_defence_failed.uid, condition=False), finish_defence, move_to_attack, facts.Jump(state_from=move_to_attack.uid, state_to=attack.uid), attack, facts.Answer(state_from=attack.uid, state_to=run.uid, condition=True, start_actions=(actions.Message(type='start_run'),)), facts.Answer(state_from=attack.uid, state_to=finish_attack_failed.uid, condition=False), run, facts.Jump(state_from=run.uid, state_to=fight.uid), fight, facts.Answer(state_from=fight.uid, state_to=hide.uid, condition=True, start_actions=(actions.Message(type='start_hide'),)), facts.Answer(state_from=fight.uid, state_to=finish_attack_failed.uid, condition=False), hide, facts.Jump(state_from=hide.uid, state_to=finish_attack.uid), finish_attack, facts.OptionsLink(options=(caravan_choice__first_defence.uid, first_defence__first_defence_continue.uid)), finish_defence_failed, finish_attack_failed ] line.extend(participants) return line
def test_linked_choices__without_path(self): # in some cases, not all linked choices can have default path start = facts.Start(uid='start', type='test', nesting=0) choice_1 = facts.Choice(uid='choice_1') choice_2 = facts.Choice(uid='choice_2') finish_1 = facts.Finish(uid='finish_1', results={}, nesting=0, start='start') finish_2 = facts.Finish(uid='finish_2', results={}, nesting=0, start='start') option_1 = facts.Option(state_from=choice_1.uid, state_to=finish_1.uid, type='opt_1', markers=()) option_2 = facts.Option(state_from=choice_1.uid, state_to=choice_2.uid, type='opt_2', markers=()) option_2_1 = facts.Option(state_from=choice_2.uid, state_to=finish_1.uid, type='opt_2_1', markers=()) option_2_2 = facts.Option(state_from=choice_2.uid, state_to=finish_2.uid, type='opt_2_2', markers=()) facts_list = [ start, choice_1, choice_2, finish_1, finish_2, option_1, option_2, option_2_1, option_2_2, facts.OptionsLink(options=(option_1.uid, option_2_1.uid)), facts.OptionsLink(options=(option_2.uid, option_2_2.uid)) ] self.kb += facts_list choices = [ facts.ChoicePath(choice=choice_1.uid, option=option_2.uid, default=True) ] # defaul path specified only for one choice self.kb += choices transformators.change_choice(self.kb, option_1.uid, default=False) self.check_in_knowledge_base(self.kb, facts_list) self.check_not_in_knowledge_base(self.kb, choices) self.assertEqual(len(list(self.kb.filter(facts.ChoicePath))), 2) self.assertEqual( len(set([path.choice for path in self.kb.filter(facts.ChoicePath)])), 2) self.assertEqual( set([path.option for path in self.kb.filter(facts.ChoicePath)]), set([option_1.uid, option_2_1.uid]))
def construct(cls, nesting, selector, initiator, initiator_position, receiver, receiver_position): hero = selector.heroes()[0] ns = selector._kb.get_next_ns() start = facts.Start( uid=ns + 'start', type=cls.TYPE, nesting=nesting, description=u'Начало: задание на шпионаж', require=[ requirements.LocatedIn(object=hero.uid, place=initiator_position.uid), requirements.LocatedIn(object=receiver.uid, place=receiver_position.uid) ], actions=[actions.Message(type='intro')]) participants = [ facts.QuestParticipant(start=start.uid, participant=initiator.uid, role=ROLES.INITIATOR), facts.QuestParticipant(start=start.uid, participant=receiver.uid, role=ROLES.RECEIVER) ] start_spying = facts.Choice( uid=ns + 'start_spying', description=u'Прибытие в город цели', require=[ requirements.LocatedIn(object=hero.uid, place=receiver_position.uid) ], actions=[actions.Message(type='arrived_to_target')]) spying_middle = facts.Choice(uid=ns + 'spying_middle', description=u'Шпионаж', actions=[ actions.MoveNear( object=hero.uid, place=receiver_position.uid) ]) continue_spying = facts.State(uid=ns + 'continue_spying', description=u'Продолжить шпионаж') success_spying = facts.State( uid=ns + 'success_spying', description=u'шпионим без происшествий', require=[ requirements.LocatedNear(object=hero.uid, place=receiver_position.uid) ], actions=[ actions.Message(type='success_spying'), actions.MoveNear(object=hero.uid, place=receiver_position.uid) ]) witness = facts.State( uid=ns + 'witness', description=u'героя заметил один из работников цели', require=[ requirements.LocatedNear(object=hero.uid, place=receiver_position.uid) ], actions=[ actions.Message(type='witness'), actions.MoveNear(object=hero.uid, place=receiver_position.uid) ]) witness_fight = facts.Question( uid=ns + 'witness_fight', description=u'удалось ли победить свидетеля?', condition=[requirements.IsAlive(object=hero.uid)], actions=[ actions.Message(type='witness_fight'), actions.Fight(mercenary=True) ]) open_up = facts.State(uid=ns + 'open_up', description=u'Раскрыться', require=[ requirements.LocatedIn( object=hero.uid, place=receiver_position.uid) ], actions=[actions.Message(type='open_up')]) report_data = facts.Finish( uid=ns + 'report_data', start=start.uid, results={ initiator.uid: RESULTS.SUCCESSED, receiver.uid: RESULTS.FAILED }, nesting=nesting, description=u'Сообщить сообранную информацию', require=[ requirements.LocatedIn(object=hero.uid, place=initiator_position.uid) ], actions=[ actions.GiveReward(object=hero.uid, type='report_data'), actions.GivePower(object=initiator.uid, power=1), actions.GivePower(object=receiver.uid, power=-1) ]) finish_spying_choice = facts.Choice( uid=ns + 'finish_spying_choice', description=u'Варианты выбора завершения шпионажа') blackmail_finish = facts.Finish( uid=ns + 'blackmail_finish', start=start.uid, results={ initiator.uid: RESULTS.NEUTRAL, receiver.uid: RESULTS.FAILED }, nesting=nesting, description=u'Шантажировать самостоятельно', require=[ requirements.LocatedIn(object=hero.uid, place=receiver_position.uid) ], actions=[ actions.GiveReward(object=hero.uid, type='blackmail_finish', scale=1.25), actions.GivePower(object=receiver.uid, power=-1) ]) witness_failed = facts.Finish( uid=ns + 'witness_failed', start=start.uid, results={ initiator.uid: RESULTS.NEUTRAL, receiver.uid: RESULTS.NEUTRAL }, nesting=nesting, description=u'свидетель смог скрыться', actions=[actions.Message(type='witness_failed')]) open_up_finish = facts.Finish( uid=ns + 'open_up_finish', start=start.uid, results={ initiator.uid: RESULTS.FAILED, receiver.uid: RESULTS.SUCCESSED }, nesting=nesting, description=u'Завершить задание и остатсья в городе цели', require=[ requirements.LocatedIn(object=hero.uid, place=receiver_position.uid) ], actions=[ actions.GiveReward(object=hero.uid, type='open_up_finish'), actions.GivePower(object=initiator.uid, power=-1), actions.GivePower(object=receiver.uid, power=1) ]) open_up_lying = facts.Finish( uid=ns + 'open_up_lying', start=start.uid, results={ initiator.uid: RESULTS.FAILED, receiver.uid: RESULTS.SUCCESSED }, nesting=nesting, description=u'Вернуться к заказчику и сообщить ложную информацию', require=[ requirements.LocatedIn(object=hero.uid, place=initiator_position.uid) ], actions=[ actions.GiveReward(object=hero.uid, type='open_up_lying', scale=1.5), actions.GivePower(object=initiator.uid, power=-1.5), actions.GivePower(object=receiver.uid, power=1.5) ]) start_spying__spying_middle = facts.Option( state_from=start_spying.uid, state_to=spying_middle.uid, type='spy', markers=[relations.OPTION_MARKERS.HONORABLE], start_actions=[ actions.Message(type='start_spying'), ]) start_spying__spying_middle__blackmail = facts.Option( state_from=start_spying.uid, state_to=spying_middle.uid, type='blackmail', markers=[relations.OPTION_MARKERS.DISHONORABLE], start_actions=[ actions.Message(type='start_spying'), ]) start_spying__open_up = facts.Option( state_from=start_spying.uid, state_to=open_up.uid, type='open_up', markers=[relations.OPTION_MARKERS.DISHONORABLE], start_actions=[ actions.Message(type='start_open_up'), ]) spying_middle__continue_spying = facts.Option( state_from=spying_middle.uid, state_to=continue_spying.uid, type='spy', markers=[relations.OPTION_MARKERS.HONORABLE]) spying_middle__continue_spying__blackmail = facts.Option( state_from=spying_middle.uid, state_to=continue_spying.uid, type='blackmail', markers=[relations.OPTION_MARKERS.DISHONORABLE]) spying_middle__open_up = facts.Option( state_from=spying_middle.uid, state_to=open_up.uid, type='open_up', markers=[relations.OPTION_MARKERS.DISHONORABLE], start_actions=[ actions.Message(type='start_open_up'), ]) finish_spying__report_data = facts.Option( state_from=finish_spying_choice.uid, state_to=report_data.uid, type='spy', markers=[relations.OPTION_MARKERS.HONORABLE], start_actions=[actions.Message(type='go_report_data')]) finish_spying__blackmail = facts.Option( state_from=finish_spying_choice.uid, state_to=blackmail_finish.uid, type='blackmail', markers=[relations.OPTION_MARKERS.DISHONORABLE], start_actions=[actions.Message(type='go_blackmail')]) line = [ start, start_spying, spying_middle, success_spying, continue_spying, open_up, report_data, open_up_finish, open_up_lying, witness, witness_fight, witness_failed, finish_spying_choice, blackmail_finish, facts.Jump(state_from=start.uid, state_to=start_spying.uid), facts.Jump(state_from=continue_spying.uid, state_to=success_spying.uid), facts.Jump(state_from=continue_spying.uid, state_to=witness.uid), start_spying__spying_middle, start_spying__spying_middle__blackmail, start_spying__open_up, spying_middle__continue_spying, spying_middle__continue_spying__blackmail, spying_middle__open_up, finish_spying__report_data, finish_spying__blackmail, facts.Jump( state_from=success_spying.uid, state_to=finish_spying_choice.uid ), #, start_actions=[actions.Message(type='move_to_report_data'),]), facts.Jump(state_from=witness.uid, state_to=witness_fight.uid), facts.Jump(state_from=open_up.uid, state_to=open_up_finish.uid), facts.Jump(state_from=open_up.uid, state_to=open_up_lying.uid, start_actions=[ actions.Message(type='move_to_report_lie'), ]), facts.OptionsLink(options=(start_spying__spying_middle.uid, spying_middle__continue_spying.uid, finish_spying__report_data.uid)), facts.OptionsLink( options=(start_spying__spying_middle__blackmail.uid, spying_middle__continue_spying__blackmail.uid, finish_spying__blackmail.uid)), facts.Answer(state_from=witness_fight.uid, state_to=finish_spying_choice.uid, condition=True), facts.Answer(state_from=witness_fight.uid, state_to=witness_failed.uid, condition=False), facts.Event(uid=ns + 'open_up_variants', description=u'Варианты окончания раскрытия', members=(open_up_finish.uid, open_up_lying.uid)), facts.Event(uid=ns + 'spying_variants', description=u'Варианты событий при шпионаже', members=(success_spying.uid, witness.uid)), ] line.extend(participants) return line