当前位置: 首页>>代码示例>>Python>>正文


Python LogicStorage.save_changed_data方法代码示例

本文整理汇总了Python中the_tale.game.logic_storage.LogicStorage.save_changed_data方法的典型用法代码示例。如果您正苦于以下问题:Python LogicStorage.save_changed_data方法的具体用法?Python LogicStorage.save_changed_data怎么用?Python LogicStorage.save_changed_data使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在the_tale.game.logic_storage.LogicStorage的用法示例。


在下文中一共展示了LogicStorage.save_changed_data方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: MakeChoiceTaskTest

# 需要导入模块: from the_tale.game.logic_storage import LogicStorage [as 别名]
# 或者: from the_tale.game.logic_storage.LogicStorage import save_changed_data [as 别名]

#.........这里部分代码省略.........
        task = self.create_task(option_uid="unknown_choice")
        self.assertEqual(task.process(FakePostpondTaskPrototype(), self.storage), POSTPONED_TASK_LOGIC_RESULT.ERROR)
        self.assertTrue(task.state.is_WRONG_POINT)

    @mock.patch(
        "questgen.quests.quests_base.QuestsBase._available_quests", lambda *argv, **kwargs: [QuestWith2ChoicePoints]
    )
    def test_no_quests(self):
        self.turn_to_quest(self.storage, self.hero.id)

        result, account_id, bundle_id = register_user("test_user_2", "[email protected]", "111111")
        self.storage.load_account_data(AccountPrototype.get_by_id(account_id))

        task = self.create_task(option_uid=self.option_1_1_uid, account_id=account_id)

        self.assertEqual(task.process(FakePostpondTaskPrototype(), self.storage), POSTPONED_TASK_LOGIC_RESULT.ERROR)
        self.assertTrue(task.state.is_QUEST_NOT_FOUND)

    @mock.patch(
        "questgen.quests.quests_base.QuestsBase._available_quests", lambda *argv, **kwargs: [QuestWith2ChoicePoints]
    )
    def test_wrong_point(self):
        task = self.create_task(option_uid=self.option_2_1_uid)
        self.assertEqual(task.process(FakePostpondTaskPrototype(), self.storage), POSTPONED_TASK_LOGIC_RESULT.ERROR)
        self.assertTrue(task.state.is_WRONG_POINT)

    @mock.patch(
        "questgen.quests.quests_base.QuestsBase._available_quests", lambda *argv, **kwargs: [QuestWith2ChoicePoints]
    )
    def test_already_chosen(self):
        task = self.create_task(option_uid=self.option_1_1_uid)
        self.assertEqual(task.process(FakePostpondTaskPrototype(), self.storage), POSTPONED_TASK_LOGIC_RESULT.SUCCESS)
        self.assertTrue(task.state.is_PROCESSED)

        task = self.create_task(option_uid=self.option_1_2_uid)
        self.assertEqual(task.process(FakePostpondTaskPrototype(), self.storage), POSTPONED_TASK_LOGIC_RESULT.ERROR)
        self.assertTrue(task.state.is_ALREADY_CHOSEN)

    @mock.patch(
        "questgen.quests.quests_base.QuestsBase._available_quests", lambda *argv, **kwargs: [QuestWith2ChoicePoints]
    )
    def test_success(self):
        self.hero.quests.updated = False

        self.assertTrue(all(not action.replane_required for action in self.hero.actions.actions_list))
        task = self.create_task(option_uid=self.option_1_1_uid)
        self.assertEqual(task.process(FakePostpondTaskPrototype(), self.storage), POSTPONED_TASK_LOGIC_RESULT.SUCCESS)
        self.assertTrue(task.state.is_PROCESSED)
        self.assertTrue(all(action.replane_required for action in self.hero.actions.actions_list))

        self.assertTrue(self.hero.quests.updated)

    @mock.patch(
        "questgen.quests.quests_base.QuestsBase._available_quests", lambda *argv, **kwargs: [QuestWith2ChoicePoints]
    )
    def test_choose_second_choice_before_first_completed(self):
        task = self.create_task(option_uid=self.option_1_2_uid)
        self.assertEqual(task.process(FakePostpondTaskPrototype(), self.storage), POSTPONED_TASK_LOGIC_RESULT.SUCCESS)
        self.assertTrue(task.state.is_PROCESSED)

        task = self.create_task(option_uid=self.option_2_1_uid)
        self.assertEqual(task.process(FakePostpondTaskPrototype(), self.storage), POSTPONED_TASK_LOGIC_RESULT.ERROR)
        self.assertTrue(task.state.is_WRONG_POINT)

    @mock.patch(
        "questgen.quests.quests_base.QuestsBase._available_quests", lambda *argv, **kwargs: [QuestWith2ChoicePoints]
    )
    def test_choose_second_choice_after_first_completed(self):
        task = self.create_task(option_uid=self.option_1_2_uid)
        self.assertEqual(task.process(FakePostpondTaskPrototype(), self.storage), POSTPONED_TASK_LOGIC_RESULT.SUCCESS)
        self.assertTrue(task.state.is_PROCESSED)

        current_time = TimePrototype.get_current_time()

        while True:
            self.assertNotEqual(self.hero.actions.current_action, ActionIdlenessPrototype.TYPE)

            task = self.create_task(option_uid=self.option_2_1_uid)

            if task.process(FakePostpondTaskPrototype(), self.storage) == POSTPONED_TASK_LOGIC_RESULT.ERROR:
                break

            self.storage.process_turn()
            self.storage.save_changed_data()
            current_time.increment_turn()

    @mock.patch(
        "questgen.quests.quests_base.QuestsBase._available_quests", lambda *argv, **kwargs: [QuestWith2ChoicePoints]
    )
    def test_no_choices(self):
        task = self.create_task(option_uid=self.option_1_1_uid)

        knowledge_base = self.hero.quests.current_quest.knowledge_base
        finish_state = knowledge_base.filter(facts.Finish).next()
        self.hero.quests.current_quest.machine.pointer.change_in_knowlege_base(
            knowledge_base, state=finish_state.uid, jump=None
        )

        self.assertEqual(task.process(FakePostpondTaskPrototype(), self.storage), POSTPONED_TASK_LOGIC_RESULT.ERROR)
        self.assertTrue(task.state.is_NO_CHOICES_IN_QUEST)
开发者ID:Jazzis18,项目名称:the-tale,代码行数:104,代码来源:test_quest_choice_task.py

示例2: LogicStorageTests

# 需要导入模块: from the_tale.game.logic_storage import LogicStorage [as 别名]
# 或者: from the_tale.game.logic_storage.LogicStorage import save_changed_data [as 别名]
class LogicStorageTests(testcase.TestCase):

    def setUp(self):
        super(LogicStorageTests, self).setUp()

        self.p1, self.p2, self.p3 = create_test_map()

        self.storage = LogicStorage()

        self.account_1 = self.accounts_factory.create_account()
        self.account_2 = self.accounts_factory.create_account()

        self.storage.load_account_data(self.account_1)
        self.storage.load_account_data(self.account_2)

        self.hero_1 = self.storage.accounts_to_heroes[self.account_1.id]
        self.hero_2 = self.storage.accounts_to_heroes[self.account_2.id]

        self.action_idl_1 = self.hero_1.actions.current_action
        self.action_idl_2 = self.hero_2.actions.current_action

        self.bundle_1_id = self.action_idl_1.bundle_id
        self.bundle_2_id = self.action_idl_2.bundle_id


    def test_load_account_data(self):
        self.assertEqual(len(self.storage.heroes), 2)
        self.assertEqual(len(self.storage.accounts_to_heroes), 2)
        self.assertEqual(self.storage.bundles_to_accounts, {self.hero_1.actions.current_action.bundle_id: set([self.account_1.id]),
                                                            self.hero_2.actions.current_action.bundle_id: set([self.account_2.id])})

        action_regenerate = actions_prototypes.ActionRegenerateEnergyPrototype.create(hero=self.hero_1)

        self.assertEqual(self.action_idl_1.storage, self.storage)
        self.assertEqual(action_regenerate.storage, self.storage)

        storage = LogicStorage()
        storage.load_account_data(AccountPrototype.get_by_id(self.account_1.id))
        storage.load_account_data(AccountPrototype.get_by_id(self.account_2.id))
        self.assertEqual(len(storage.heroes), 2)
        self.assertEqual(len(storage.accounts_to_heroes), 2)
        self.assertEqual(storage.bundles_to_accounts, {self.hero_1.actions.current_action.bundle_id: set([self.account_1.id]),
                                                       self.hero_2.actions.current_action.bundle_id: set([self.account_2.id])})


    def test_load_account_data_with_meta_action(self):
        bundle_id = 666

        meta_action_battle = meta_actions.ArenaPvP1x1.create(self.storage, self.hero_1, self.hero_2)

        proxy_action_1 = actions_prototypes.ActionMetaProxyPrototype.create(hero=self.hero_1, _bundle_id=bundle_id, meta_action=meta_action_battle)
        proxy_action_2 = actions_prototypes.ActionMetaProxyPrototype.create(hero=self.hero_2, _bundle_id=bundle_id, meta_action=meta_action_battle)

        self.assertEqual(len(self.storage.meta_actions), 1)
        self.assertEqual(len(self.storage.meta_actions_to_actions), 1)
        self.assertEqual(self.storage.meta_actions_to_actions[meta_action_battle.uid], set([LogicStorage.get_action_uid(proxy_action_1),
                                                                                            LogicStorage.get_action_uid(proxy_action_2)]))

        self.storage.save_changed_data()

        self.assertIs(self.hero_1.actions.current_action.meta_action, self.hero_2.actions.current_action.meta_action)
        self.assertIs(self.hero_1.actions.current_action.saved_meta_action, self.hero_2.actions.current_action.saved_meta_action)

        storage = LogicStorage()
        storage.load_account_data(AccountPrototype.get_by_id(self.account_1.id))
        storage.load_account_data(AccountPrototype.get_by_id(self.account_2.id))

        self.assertEqual(len(storage.meta_actions), 1)
        self.assertEqual(len(storage.meta_actions_to_actions), 1)
        self.assertEqual(storage.meta_actions_to_actions[meta_action_battle.uid], set([LogicStorage.get_action_uid(proxy_action_1),
                                                                                       LogicStorage.get_action_uid(proxy_action_2)]))

        self.assertEqual(storage.bundles_to_accounts, {self.hero_1.actions.current_action.bundle_id: set([self.account_1.id, self.account_2.id])})

        hero_1 = storage.accounts_to_heroes[self.account_1.id]
        hero_2 = storage.accounts_to_heroes[self.account_2.id]

        self.assertIs(hero_1.actions.current_action.meta_action, hero_2.actions.current_action.meta_action)
        self.assertIsNot(hero_1.actions.current_action.saved_meta_action, hero_2.actions.current_action.saved_meta_action)
        self.assertEqual(hero_1.actions.current_action.saved_meta_action.serialize(), hero_2.actions.current_action.saved_meta_action.serialize())


    def test_add_duplicate_hero(self):
        self.assertRaises(exceptions.HeroAlreadyRegisteredError, self.storage._add_hero, self.hero_1)


    def test_action_release_account_data(self):

        actions_prototypes.ActionRegenerateEnergyPrototype.create(hero=self.hero_1)

        self.storage.skipped_heroes.add(self.hero_1.id)

        self.storage.release_account_data(self.account_1.id)

        self.assertEqual(len(self.storage.heroes), 1)
        self.assertEqual(len(self.storage.accounts_to_heroes), 1)
        self.assertEqual(self.storage.bundles_to_accounts, {self.hero_2.actions.current_action.bundle_id: set([self.account_2.id])})
        self.assertEqual(self.storage.heroes.values()[0].id, self.hero_2.id)
        self.assertFalse(self.storage.skipped_heroes)

#.........这里部分代码省略.........
开发者ID:alexudracul,项目名称:the-tale,代码行数:103,代码来源:test_logic_storage.py

示例3: Worker

# 需要导入模块: from the_tale.game.logic_storage import LogicStorage [as 别名]
# 或者: from the_tale.game.logic_storage.LogicStorage import save_changed_data [as 别名]
class Worker(workers.BaseWorker):
    STOP_SIGNAL_REQUIRED = False

    def initialize(self):
        # worker initialized by supervisor
        pass

    def cmd_initialize(self, turn_number, worker_id):
        self.send_cmd('initialize', {'turn_number': turn_number, 'worker_id': worker_id})

    def process_initialize(self, turn_number, worker_id):

        if self.initialized:
            self.logger.warn('WARNING: game already initialized, do reinitialization')

        self.storage = LogicStorage()

        self.initialized = True
        self.turn_number = turn_number
        self.queue = []
        self.worker_id = worker_id

        self.logger.info('GAME INITIALIZED')

        environment.workers.supervisor.cmd_answer('initialize', self.worker_id)

    def cmd_next_turn(self, turn_number):
        return self.send_cmd('next_turn', data={'turn_number': turn_number})

    # @profile.profile_decorator('/home/tie/repos/mine/the-tale/profile.info')
    def process_next_turn(self, turn_number):

        self.turn_number += 1

        if turn_number != self.turn_number:
            raise LogicException('dessinchonization: workers turn number (%d) not equal to command turn number (%d)' % (self.turn_number, turn_number))


        if TimePrototype.get_current_turn_number() != self.turn_number:
            raise LogicException('dessinchonization: workers turn number (%d) not equal to saved turn number (%d)' % (self.turn_number,
                                                                                                                      TimePrototype.get_current_turn_number()))

        self.storage.process_turn(logger=self.logger)
        self.storage.save_changed_data(logger=self.logger)

        for hero_id in self.storage.skipped_heroes:
            hero = self.storage.heroes[hero_id]
            if hero.actions.current_action.bundle_id in self.storage.ignored_bundles:
                continue
            environment.workers.supervisor.cmd_account_release_required(hero.account_id)

        environment.workers.supervisor.cmd_answer('next_turn', self.worker_id)

        if game_settings.COLLECT_GARBAGE and self.turn_number % game_settings.COLLECT_GARBAGE_PERIOD == 0:
            self.logger.info('GC: start')
            gc.collect()
            self.logger.info('GC: end')

    def release_account(self, account_id):
        if account_id not in self.storage.accounts_to_heroes:
            environment.workers.supervisor.cmd_account_released(account_id)
            return

        hero = self.storage.accounts_to_heroes[account_id]
        bundle_id = hero.actions.current_action.bundle_id

        if bundle_id in self.storage.ignored_bundles:
            return

        with self.storage.on_exception(self.logger,
                                       message='LogicWorker.process_release_account catch exception, while processing hero %d, try to save all bundles except %d',
                                       data=(hero.id, bundle_id),
                                       excluded_bundle_id=bundle_id):
            self.storage.release_account_data(account_id)
            environment.workers.supervisor.cmd_account_released(account_id)

    def cmd_stop(self):
        return self.send_cmd('stop')

    def process_stop(self):
        # no need to save data, since they automaticaly saved on every turn
        self.initialized = False
        self.storage.save_all(logger=self.logger)
        environment.workers.supervisor.cmd_answer('stop', self.worker_id)
        self.stop_required = True
        self.logger.info('LOGIC STOPPED')

    def cmd_register_account(self, account_id):
        return self.send_cmd('register_account', {'account_id': account_id})

    def process_register_account(self, account_id):
        from the_tale.accounts.prototypes import AccountPrototype
        account = AccountPrototype.get_by_id(account_id)
        if account is None:
            raise LogicException('can not get account with id "%d"' % (account_id,))
        self.storage.load_account_data(account)

    def cmd_release_account(self, account_id):
        return self.send_cmd('release_account', {'account_id': account_id})

#.........这里部分代码省略.........
开发者ID:Alkalit,项目名称:the-tale,代码行数:103,代码来源:logic.py


注:本文中的the_tale.game.logic_storage.LogicStorage.save_changed_data方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。