Example #1
0
def create_person(place,
                  race,
                  type,
                  utg_name,
                  gender,
                  personality_cosmetic=None,
                  personality_practical=None):

    if personality_cosmetic is None:
        personality_cosmetic = relations.PERSONALITY_COSMETIC.random()

    if personality_practical is None:
        personality_practical = relations.PERSONALITY_PRACTICAL.random()

    person = objects.Person(
        id=None,
        created_at_turn=turn.number(),
        updated_at_turn=turn.number(),
        updated_at=datetime.datetime.now(),
        place_id=place.id,
        gender=gender,
        race=race,
        type=type,
        attrs=attributes.Attributes(),
        personality_cosmetic=personality_cosmetic,
        personality_practical=personality_practical,
        politic_power=PersonPoliticPower.create(),
        utg_name=utg_name,
        job=PersonJob.create(normal_power=NORMAL_PERSON_JOB_POWER),
        moved_at_turn=turn.number())
    person.refresh_attributes()
    place.refresh_attributes()
    save_person(person, new=True)
    return person
Example #2
0
def create_place(x, y, size, utg_name, race, is_frontier=False):
    place = objects.Place(id=None,
                          x=x,
                          y=y,
                          updated_at=datetime.datetime.now(),
                          updated_at_turn=turn.number(),
                          created_at=datetime.datetime.now(),
                          created_at_turn=turn.number(),
                          habit_honor=habits.Honor(raw_value=0),
                          habit_honor_positive=0,
                          habit_honor_negative=0,
                          habit_peacefulness=habits.Peacefulness(raw_value=0),
                          habit_peacefulness_positive=0,
                          habit_peacefulness_negative=0,
                          is_frontier=is_frontier,
                          description='',
                          race=race,
                          persons_changed_at_turn=turn.number(),
                          attrs=attributes.Attributes(size=size),
                          utg_name=utg_name,
                          races=races.Races(),
                          nearest_cells=[],
                          effects=EffectsContainer(),
                          job=jobs_logic.create_job(PlaceJob),
                          modifier=modifiers.CITY_MODIFIERS.NONE)
    place.refresh_attributes()
    save_place(place, new=True)
    return place
Example #3
0
def create_person(place, race, type, utg_name, gender, personality_cosmetic=None, personality_practical=None):

    if personality_cosmetic is None:
        personality_cosmetic = relations.PERSONALITY_COSMETIC.random()

    if personality_practical is None:
        personality_practical = relations.PERSONALITY_PRACTICAL.random()

    person = objects.Person(id=None,
                            created_at_turn=turn.number(),
                            updated_at_turn=turn.number(),
                            updated_at=datetime.datetime.now(),
                            place_id=place.id,
                            gender=gender,
                            race=race,
                            type=type,
                            attrs=attributes.Attributes(),
                            personality_cosmetic=personality_cosmetic,
                            personality_practical=personality_practical,
                            utg_name=utg_name,
                            job=jobs_logic.create_job(PersonJob),
                            moved_at_turn=turn.number())
    person.refresh_attributes()
    save_person(person, new=True)
    return person
Example #4
0
    def test_region_for_turn(self):
        turn.increment()

        self.check_ajax_error(self.request_ajax_json(logic.region_url(turn.number())), 'no_region_found')

        update_map(index=1)

        self.check_ajax_ok(self.request_ajax_json(logic.region_url(turn.number())))
Example #5
0
    def initialize(self):
        self.clean_queues()

        self.gracefull_stop_required = False

        PostponedTaskPrototype.reset_all()

        self.logic_workers = {worker.name: worker for worker in (environment.workers.logic_1, environment.workers.logic_2)}

        self.logger.info('initialize logic')

        self.logic_multicast('initialize', arguments=dict(turn_number=turn.number()), worker_id=True, wait_answer=True)

        if conf.game_settings.ENABLE_WORKER_HIGHLEVEL:
            self.logger.info('initialize highlevel')
            environment.workers.highlevel.cmd_initialize(turn_number=turn.number(), worker_id='highlevel')
            self.wait_answers_from('initialize', workers=['highlevel'])
        else:
            self.logger.info('skip initialization of highlevel')

        if conf.game_settings.ENABLE_PVP:
            self.logger.info('initialize pvp balancer')
            environment.workers.pvp_balancer.cmd_initialize(worker_id='pvp_balancer')
            self.wait_answers_from('initialize', workers=['pvp_balancer'])
        else:
            self.logger.info('skip initialization of pvp balancer')

        self.logger.info('child workers initialized')

        self.logger.info('register task')

        self.tasks = {}
        self.accounts_for_tasks = {}
        self.accounts_owners = {}
        self.accounts_queues = {}
        self.logic_accounts_number = {logic_worker_name: 0 for logic_worker_name in self.logic_workers.keys()}

        for task_model in models.SupervisorTask.objects.filter(state=relations.SUPERVISOR_TASK_STATE.WAITING).iterator():
            task = prototypes.SupervisorTaskPrototype(task_model)
            self.register_task(task, release_accounts=False)

        self.logger.info('distribute accounts')

        for account_id in Account.objects.all().order_by('id').values_list('id', flat=True).iterator():
            self.register_account(account_id)

        self.initialized = True
        self.wait_next_turn_answer = False

        prototypes.GameState.start()

        self.logger.info('SUPERVISOR INITIALIZED')
Example #6
0
    def test_creation(self):
        Setting.objects.all().delete()
        settings.refresh()

        settings_number = Setting.objects.all().count()

        self.assertEqual(turn.number(), 0)
        self.assertEqual(Setting.objects.all().count(), settings_number)

        turn.increment()

        self.assertEqual(turn.number(), 1)
        self.assertEqual(Setting.objects.all().count(), settings_number + 1)
Example #7
0
    def process_next_turn(self):
        self.wait_answer_from_next_turn()

        turn.increment()

        self.logic_multicast('next_turn', arguments=dict(turn_number=turn.number()))
        self.wait_next_turn_answer = True

        try:
            if conf.game_settings.ENABLE_WORKER_HIGHLEVEL:
                environment.workers.highlevel.cmd_next_turn(turn_number=turn.number())
        except amqp_exceptions.WaitAnswerTimeoutError:
            self.logger.error('next turn timeout while getting answer from highlevel')
            self._force_stop()
            raise
Example #8
0
def save_person(person, new=False):

    data = {'name': person.utg_name.serialize(),
            'job': person.job.serialize(),
            'moved_at_turn': person.moved_at_turn,
            'attributes': person.attrs.serialize(),
            'personality': {'cosmetic': person.personality_cosmetic.value,
                            'practical': person.personality_practical.value}}

    arguments = {'place_id': person.place_id,
                 'gender': person.gender,
                 'race': person.race,
                 'type': person.type,
                 'data': s11n.to_json(data),
                 'created_at_turn': person.created_at_turn,
                 'updated_at_turn': person.updated_at_turn}

    if new:
        person_model = models.Person.objects.create(**arguments)

        person.id = person_model.id

        # TODO: that string was in .create method, is it needed here?
        person.place.persons_changed_at_turn = turn.number()

        storage.persons.add_item(person.id, person)
    else:
        models.Person.objects.filter(id=person.id).update(**arguments)

    storage.persons.update_version()
Example #9
0
def create_building(person, utg_name, position=None):
    from the_tale.game.places import storage

    building = storage.buildings.get_by_person_id(person.id)

    if building:
        return building

    # remove any destroyed buildings for person
    models.Building.objects.filter(person_id=person.id).delete()

    if position is None:
        position = random.choice(
            list(get_available_positions(person.place.x, person.place.y)))

    x, y = position

    building = objects.Building(id=None,
                                x=x,
                                y=y,
                                type=person.type.building_type,
                                integrity=1.0,
                                created_at_turn=turn.number(),
                                state=relations.BUILDING_STATE.WORKING,
                                utg_name=utg_name,
                                person_id=person.id)

    save_building(building, new=True)

    return building
Example #10
0
    def create(cls, author, caption, text):

        model = models.Post.objects.create(author=author._model,
                                           caption=caption,
                                           text=text,
                                           state=relations.POST_STATE.ACCEPTED,
                                           created_at_turn=turn.number(),
                                           votes=1)

        thread = ForumThreadPrototype.create(
            ForumSubCategoryPrototype.get_by_uid(
                conf.settings.FORUM_CATEGORY_UID),
            caption=caption,
            author=get_system_user(),
            text='обсуждение [url="%s%s"]произведения[/url]' %
            (project_settings.SITE_URL,
             reverse('blogs:posts:show', args=[model.id])),
            markup_method=MARKUP_METHOD.POSTMARKUP)

        model.forum_thread = thread._model
        model.save()

        post = cls(model)

        VotePrototype.create(post, author)

        for tag_id in conf.settings.DEFAULT_TAGS:
            models.Tagged.objects.create(post_id=post.id, tag_id=tag_id)

        return post
Example #11
0
    def test_created_at_turn(self):
        turn.increment()
        turn.increment()

        post = PostPrototype.create(thread=self.thread, author=self.account, text='post-1-text')

        self.assertEqual(post.created_at_turn, turn.number())
Example #12
0
def form_game_info(account=None, is_own=False, client_turns=None):
    from the_tale.accounts.prototypes import AccountPrototype
    from the_tale.game.prototypes import GameState
    from the_tale.game.pvp.prototypes import Battle1x1Prototype

    data = {'mode': 'pve',
            'turn': turn.ui_info(),
            'game_state': GameState.state().value,
            'map_version': map_info_storage.version,
            'account': None,
            'enemy': None}

    if account:
        turn_number = turn.number()

        battle = Battle1x1Prototype.get_by_account_id(account.id)
        data['account'] = _form_game_account_info(turn_number,
                                                  account,
                                                  in_pvp_queue=False if battle is None else battle.state.is_WAITING,
                                                  is_own=is_own,
                                                  client_turns=client_turns)

        if battle and battle.state.is_PROCESSING:
            data['mode'] = 'pvp'
            data['enemy'] = _form_game_account_info(turn_number,
                                                    AccountPrototype.get_by_id(battle.enemy_id),
                                                    in_pvp_queue=False,
                                                    is_own=False,
                                                    client_turns=client_turns)

    return data
Example #13
0
    def update(self, text):
        self._model.text = text
        self._model.updated_at_turn = turn.number()
        self.save()

        self.thread.update()
        self.thread.subcategory.update()
Example #14
0
    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 turn.number() != self.turn_number:
            raise LogicException('dessinchonization: workers turn number (%d) not equal to saved turn number (%d)' % (self.turn_number, 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')
Example #15
0
    def test_not_enough_voices_percents(self):

        turn.increment()
        turn.increment()

        VotePrototype.create(self.account2, self.bill,
                             relations.VOTE_TYPE.AGAINST)
        VotePrototype.create(self.account3, self.bill,
                             relations.VOTE_TYPE.REFRAINED)

        self.assertEqual(Post.objects.all().count(), 1)

        with self.check_not_changed(lambda: self.place1.attrs.stability):
            with mock.patch(
                    'the_tale.accounts.workers.accounts_manager.Worker.cmd_run_account_method'
            ) as cmd_run_account_method:
                self.assertFalse(self.bill.apply())

            self.assertEqual(cmd_run_account_method.call_count, 0)
            self.assertTrue(self.bill.state.is_REJECTED)

            self.assertEqual(Post.objects.all().count(), 2)

            bill = BillPrototype.get_by_id(self.bill.id)
            self.assertTrue(bill.state.is_REJECTED)

            places_storage.places.sync(force=True)

            self.place1.refresh_attributes()

        self.assertEqual(bill.applyed_at_turn, turn.number())

        self.check_place(self.place1.id, self.place1.name,
                         self.place1.utg_name.forms)
Example #16
0
    def process_turn(self, logger=None, continue_steps_if_needed=True):
        self.switch_caches()

        timestamp = time.time()

        turn_number = turn.number()

        processed_heroes = 0

        for hero in self.heroes.values():
            if hero.actions.current_action.bundle_id in self.ignored_bundles:
                continue

            if hero.id in self.skipped_heroes:
                continue

            if not hero.can_process_turn(turn_number):
                continue

            self.process_turn__single_hero(
                hero=hero,
                logger=logger,
                continue_steps_if_needed=continue_steps_if_needed)

            processed_heroes += 1

            if conf.game_settings.UNLOAD_OBJECTS:
                hero.unload_serializable_items(timestamp)

        if logger:
            logger.info('[next_turn] processed heroes: %d / %d' %
                        (processed_heroes, len(self.heroes)))
            if self.ignored_bundles:
                logger.info('[next_turn] ignore bundles: %r' %
                            list(self.ignored_bundles))
Example #17
0
def add_power_impacts(impacts):
    transaction = uuid.uuid4()
    current_turn = turn.number()

    inner_circle = []
    outer_circle = []
    jobs = []
    fame = []

    for impact in impacts:
        impact.transaction = transaction
        impact.turn = current_turn

        if impact.type.is_INNER_CIRCLE:
            inner_circle.append(impact)
        elif impact.type.is_OUTER_CIRCLE:
            outer_circle.append(impact)
        elif impact.type.is_JOB:
            jobs.append(impact)
        elif impact.type.is_FAME:
            fame.append(impact)
        else:
            raise NotImplementedError

    if inner_circle:
        tt_api_impacts.personal_impacts.cmd_add_power_impacts(inner_circle)

    if outer_circle:
        tt_api_impacts.crowd_impacts.cmd_add_power_impacts(outer_circle)

    if jobs:
        tt_api_impacts.job_impacts.cmd_add_power_impacts(jobs)

    if fame:
        tt_api_impacts.fame_impacts.cmd_add_power_impacts(fame)
Example #18
0
    def create(cls, subcategory, caption, author, text, markup_method=MARKUP_METHOD.POSTMARKUP, technical=False):

        from the_tale.post_service.prototypes import MessagePrototype
        from the_tale.post_service.message_handlers import ForumThreadHandler

        if isinstance(subcategory, int):
            subcategory = SubCategoryPrototype.get_by_id(subcategory)

        thread_model = Thread.objects.create(subcategory=subcategory._model,
                                             caption=caption,
                                             author=author._model,
                                             last_poster=author._model,
                                             technical=technical,
                                             posts_count=0)

        Post.objects.create(thread=thread_model,
                            author=author._model,
                            markup_method=markup_method,
                            technical=technical,
                            text=text,
                            created_at_turn=turn.number(),
                            state=POST_STATE.DEFAULT)

        prototype = cls(model=thread_model)

        subcategory.update()

        MessagePrototype.create(ForumThreadHandler(thread_id=prototype.id))

        return prototype
Example #19
0
    def process_rare_operations(self):
        from the_tale.accounts.achievements.storage import achievements_storage
        from the_tale.accounts.achievements.relations import ACHIEVEMENT_TYPE

        from the_tale.game.companions import storage as companions_storage
        from the_tale.game.companions import logic as companions_logic

        current_turn = turn.number()

        passed_interval = current_turn - self.last_rare_operation_at_turn

        if passed_interval < conf.heroes_settings.RARE_OPERATIONS_INTERVAL:
            return

        if self.companion is None and random.random(
        ) < float(passed_interval
                  ) / c.TURNS_IN_HOUR / c.COMPANIONS_GIVE_COMPANION_AFTER:
            companions_choices = [
                companion for companion in
                companions_storage.companions.enabled_companions()
                if any(ability.effect.TYPE.is_LEAVE_HERO
                       for ability in companion.abilities.start)
            ]
            if companions_choices:
                self.set_companion(
                    companions_logic.create_companion(
                        random.choice(companions_choices)))

        self.quests.sync_interfered_persons()

        with achievements_storage.verify(type=ACHIEVEMENT_TYPE.TIME,
                                         object=self):
            self.last_rare_operation_at_turn = current_turn
Example #20
0
    def on_help(self):
        current_turn = turn.number()

        if self.last_help_on_turn != current_turn:
            self.last_help_on_turn = current_turn
            self.helps_in_turn = 0

        self.helps_in_turn += 1
Example #21
0
 def create_message(self, uid):
     return heroes_messages.MessageSurrogate(
         turn_number=turn.number(),
         timestamp=time.time(),
         key=None,
         externals=None,
         message='message {}'.format(uid),
         position='position {}'.format(uid))
Example #22
0
    def test_post_created_at_turn(self):
        turn.increment(2)

        ThreadPrototype.create(self.subcategory, 'thread-2-caption',
                               self.account, 'thread-2-text')

        self.assertEqual(PostPrototype._db_latest().created_at_turn,
                         turn.number())
Example #23
0
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
Example #24
0
 def _start_quest(self, start, hero):
     hero.quests.update_history(start.type, turn.number())
     self.quests_stack.append(
         QuestInfo.construct(
             type=start.type,
             uid=start.uid,
             knowledge_base=self.machine.knowledge_base,
             experience=self.get_expirience_for_quest(start.uid, hero),
             power=self.get_politic_power_for_quest(start.uid, hero),
             hero=hero))
Example #25
0
    def new_job(self, effect, normal_power):
        if self.positive_power > self.negative_power:
            self.positive_power = 0
        else:
            self.negative_power = 0

        self.name = self.create_name(effect)
        self.created_at_turn = turn.number()
        self.effect = effect
        self.power_required = normal_power * effect.power_modifier
Example #26
0
    def test_approved(self):
        turn.increment()
        turn.increment()
        turn.increment()

        VotePrototype.create(self.account2, self.bill,
                             relations.VOTE_TYPE.AGAINST)
        VotePrototype.create(self.account3, self.bill, relations.VOTE_TYPE.FOR)
        VotePrototype.create(self.account4, self.bill,
                             relations.VOTE_TYPE.REFRAINED)

        ##################################
        # set name forms

        data = self.bill.user_form_initials
        data.update(
            linguistics_helpers.get_word_post_data(self.bill.data.name_forms,
                                                   prefix='name'))
        data['approved'] = True
        form = self.bill.data.get_moderator_form_update(data)

        self.assertTrue(form.is_valid())
        self.bill.update_by_moderator(form)
        ##################################

        self.assertEqual(Post.objects.all().count(), 1)

        with mock.patch(
                'the_tale.accounts.workers.accounts_manager.Worker.cmd_run_account_method'
        ) as cmd_run_account_method:
            self.assertTrue(self.bill.apply())

        self.assertEqual(cmd_run_account_method.call_args_list, [
            mock.call(account_id=self.bill.owner.id,
                      method_name=accounts_prototypes.AccountPrototype.
                      update_actual_bills.__name__,
                      data={})
        ])

        self.assertTrue(self.bill.state.is_ACCEPTED)

        self.assertEqual(Post.objects.all().count(), 2)

        bill = BillPrototype.get_by_id(self.bill.id)
        self.assertTrue(bill.state.is_ACCEPTED)

        places_storage.places.sync(force=True)

        self.place1.refresh_attributes()
        self.assertTrue(self.place1.attrs.stability < 1.0)

        self.assertEqual(bill.applyed_at_turn, turn.number())

        self.check_place(self.place1.id, 'new_name_1-нс,ед,им',
                         self.bill.data.name_forms.forms)
Example #27
0
def save_place(place, new=False):
    from the_tale.game.places import storage

    data = {
        'name': place.utg_name.serialize(),
        'attributes': place.attrs.serialize(),
        'races': place.races.serialize(),
        'nearest_cells': place.nearest_cells,
        'effects': place.effects.serialize(),
        'job': place.job.serialize()
    }

    arguments = {
        'x': place.x,
        'y': place.y,
        'updated_at_turn': turn.number(),
        'updated_at': datetime.datetime.now(),
        'is_frontier': place.is_frontier,
        'description': place.description,
        'data': s11n.to_json(data),
        'habit_honor_positive': place.habit_honor_positive,
        'habit_honor_negative': place.habit_honor_negative,
        'habit_peacefulness_positive': place.habit_peacefulness_positive,
        'habit_peacefulness_negative': place.habit_peacefulness_negative,
        'habit_honor': place.habit_honor.raw_value,
        'habit_peacefulness': place.habit_peacefulness.raw_value,
        'modifier': place._modifier,
        'race': place.race,
        'persons_changed_at_turn': place.persons_changed_at_turn
    }

    if new:
        place_model = models.Place.objects.create(
            created_at_turn=turn.number(), **arguments)
        place.id = place_model.id
        storage.places.add_item(place.id, place)
    else:
        models.Place.objects.filter(id=place.id).update(**arguments)

    storage.places.update_version()

    place.updated_at = datetime.datetime.now()
Example #28
0
def create_job(JobClass):
    effect = random.choice([
        effect for effect in effects.EFFECT.records
        if effect.group.is_ON_HEROES
    ])

    return JobClass(name=create_name(JobClass.ACTOR, effect),
                    created_at_turn=turn.number(),
                    effect=effect,
                    power_required=JobClass.NORMAL_POWER *
                    effect.power_modifier)
Example #29
0
 def create_message(self,
                    message,
                    turn_delta=0,
                    time_delta=0,
                    position='some position info'):
     return messages.MessageSurrogate(turn_number=turn.number() +
                                      turn_delta,
                                      timestamp=time.time() + time_delta,
                                      key=None,
                                      externals=None,
                                      message=message,
                                      position=position)
Example #30
0
    def create(cls, normal_power):
        effect = random.choice([
            effect for effect in effects.EFFECT.records
            if effect.group.is_ON_HEROES
        ])

        return cls(name=cls.create_name(effect),
                   created_at_turn=turn.number(),
                   effect=effect,
                   positive_power=0,
                   negative_power=0,
                   power_required=normal_power * effect.power_modifier)