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=TimePrototype.get_current_turn_number(), created_at=datetime.datetime.now(), created_at_turn=TimePrototype.get_current_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=TimePrototype.get_current_turn_number(), politic_power=PlacePoliticPower.create(), attrs=attributes.Attributes(size=size), utg_name=utg_name, races=races.Races(), nearest_cells=[], effects=EffectsContainer(), job=PlaceJob.create(normal_power=NORMAL_PLACE_JOB_POWER), modifier=modifiers.CITY_MODIFIERS.NONE) place.refresh_attributes() save_place(place, new=True) return place
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=TimePrototype.get_current_turn_number(), updated_at_turn=TimePrototype.get_current_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=TimePrototype.get_current_turn_number(), ) person.refresh_attributes() place.refresh_attributes() save_person(person, new=True) return person
def test_push_power(self): self.place.push_power(0, 10) self.assertEqual(self.place.power, 10) TimePrototype.get_current_time().increment_turn() self.place.push_power(1, 1) self.assertEqual(self.place.power, 1 + 10*float(places_settings.POWER_HISTORY_LENGTH-1)/places_settings.POWER_HISTORY_LENGTH) TimePrototype(places_settings.POWER_HISTORY_LENGTH-1).save() self.place.push_power(places_settings.POWER_HISTORY_LENGTH-1, 100) self.assertEqual(self.place.power, 100 + 10*1.0/places_settings.POWER_HISTORY_LENGTH + 2.0/places_settings.POWER_HISTORY_LENGTH) TimePrototype(places_settings.POWER_HISTORY_LENGTH).save() self.place.push_power(places_settings.POWER_HISTORY_LENGTH, 1000) self.assertEqual(self.place.power, 1000 + 100*float(places_settings.POWER_HISTORY_LENGTH-1)/places_settings.POWER_HISTORY_LENGTH + 1.0/places_settings.POWER_HISTORY_LENGTH) TimePrototype(places_settings.POWER_HISTORY_LENGTH+1).save() self.place.push_power(places_settings.POWER_HISTORY_LENGTH+1, 10000) self.assertEqual(self.place.power, 10000 + 1000*float(places_settings.POWER_HISTORY_LENGTH-1)/places_settings.POWER_HISTORY_LENGTH + 100*float(places_settings.POWER_HISTORY_LENGTH-2)/places_settings.POWER_HISTORY_LENGTH) TimePrototype(places_settings.POWER_HISTORY_LENGTH+2).save() self.place.push_power(places_settings.POWER_HISTORY_LENGTH+2, 100000) self.assertEqual(round(self.place.power, 5), round(100000 + 10000*float(places_settings.POWER_HISTORY_LENGTH-1)/places_settings.POWER_HISTORY_LENGTH + 1000*float(places_settings.POWER_HISTORY_LENGTH-2)/places_settings.POWER_HISTORY_LENGTH + 100*float(places_settings.POWER_HISTORY_LENGTH-3)/places_settings.POWER_HISTORY_LENGTH, 5) )
def test_sync_data__power_from_bonuses_2(self): person_1 = self.p1.persons[0] self.assertEqual(self.p1.power, 0) self.assertEqual(person_1.power, 0) person_1.push_power(TimePrototype.get_current_turn_number(), 10000) self.p1.push_power(TimePrototype.get_current_turn_number(), 10000) self.worker.process_change_power( person_id=person_1.id, power_delta=-1000, place_id=None, positive_bonus=0, negative_bonus=0 ) self.worker.sync_data() self.assertEqual(self.p1.power, 10000 - 1000 * 2 * 1.5) self.assertEqual(person_1.power, 10000 - 1000 * 2) self.worker.process_change_power( place_id=self.p1.id, power_delta=100, person_id=None, positive_bonus=0, negative_bonus=0 ) self.worker.sync_data() self.assertEqual(self.p1.power, 10000 - 1000 * 2 * 1.5 + 100 * 1.25) self.assertEqual(person_1.power, 10000 - 1000 * 2)
def setUp(self): super(TestPrototypeEnd, self).setUp() bill_data = PlaceRenaming(place_id=self.place1.id, name_forms=names.generator.get_test_name('new_name_1')) self.bill = BillPrototype.create(self.account1, 'bill-1-caption', 'bill-1-rationale', bill_data, chronicle_on_accepted='chronicle-on-accepted') self.bill.state = relations.BILL_STATE.ACCEPTED TimePrototype.get_current_time().increment_turn()
def create_place(self, x, y, size, roads_to, persons=(), name_forms=None, is_frontier=False): # pylint: disable=R0914 place_power = int(max(place.power for place in places_storage.all()) * float(size) / places_settings.MAX_SIZE) place_power_steps = int(places_settings.POWER_HISTORY_LENGTH / c.MAP_SYNC_TIME) place_power_per_step = (place_power / place_power_steps) + 1 place = PlacePrototype.create( x=x, y=y, utg_name=name_forms.word, is_frontier=is_frontier, size=size) initial_turn = TimePrototype.get_current_turn_number() - places_settings.POWER_HISTORY_LENGTH for i in xrange(place_power_steps): place.push_power(int(initial_turn+i*c.MAP_SYNC_TIME), int(place_power_per_step)) for person_name_forms, power_percent, race, gender, tp in persons: person = PersonPrototype.create(place=place, race=race, gender=gender, tp=tp, name_forms=person_name_forms) person_power = place_power * power_percent person_power_steps = int(persons_conf.settings.POWER_HISTORY_LENGTH / c.MAP_SYNC_TIME) person_power_per_step = (person_power / person_power_steps) + 1 initial_turn = TimePrototype.get_current_turn_number() - persons_conf.settings.POWER_HISTORY_LENGTH for i in xrange(person_power_steps): person.push_power(int(initial_turn+i*c.MAP_SYNC_TIME), int(person_power_per_step)) person.save() place.sync_persons(force_add=True) power_delta = self.INITIAL_PERSON_POWER for person in place.persons: person.fill_power_evenly(power_delta) person.save() power_delta /= 2 place.sync_race() place.save() for destination in roads_to: Road.objects.create(point_1=place._model, point_2=destination._model) persons_storage.update_version() places_storage.update_version() roads_storage.update_version() persons_storage.refresh() places_storage.refresh() roads_storage.refresh() return place
def test_increment_turn(self): time = TimePrototype.get_current_time() self.assertEqual(time.turn_number, 0) time.increment_turn() self.assertEqual(time.turn_number, 1) time.save() time = TimePrototype.get_current_time() self.assertEqual(time.turn_number, 1)
def test_creation(self): Setting.objects.all().delete() settings.refresh() settings_number = Setting.objects.all().count() time = TimePrototype.get_current_time() self.assertEqual(time.turn_number, 0) self.assertEqual(Setting.objects.all().count(), settings_number+1) time = TimePrototype.get_current_time() self.assertEqual(time.turn_number, 0) self.assertEqual(Setting.objects.all().count(), settings_number+1)
def test_move_out_game(self): self.assertEqual(self.person.place.persons_changed_at_turn, self.persons_changed_at_turn) TimePrototype.get_current_time().increment_turn() current_time = datetime.datetime.now() self.assertTrue(self.person.out_game_at < current_time) self.assertEqual(self.person.state, PERSON_STATE.IN_GAME) self.person.move_out_game() self.assertTrue(self.person.out_game_at > current_time) self.assertEqual(self.person.state, PERSON_STATE.OUT_GAME) self.assertEqual(self.person.place.persons_changed_at_turn, TimePrototype.get_current_turn_number())
def test_user_form__move_delay(self): data = {'caption': 'caption-caption', 'rationale': 'rationale', 'chronicle_on_accepted': 'chronicle-on-accepted', 'person': self.person_2.id, 'new_place': self.place3.id} form = self.bill.data.get_user_form_update(post=data, owner_id=self.account.id) self.assertFalse(form.is_valid()) TimePrototype.get_current_time().increment_turn() form = self.bill.data.get_user_form_update(post=data, owner_id=self.account.id) self.assertTrue(form.is_valid())
def test_full__double_energy(self): self.hero.change_energy(-self.hero.energy) current_time = TimePrototype.get_current_time() while len(self.hero.actions.actions_list) != 1: self.storage.process_turn(continue_steps_if_needed=False) current_time.increment_turn() self.assertTrue(self.action_idl.leader) self.assertEqual(self.hero.energy, self.hero.preferences.energy_regeneration_type.amount * 2) self.assertEqual(self.hero.need_regenerate_energy, False) self.assertEqual(self.hero.last_energy_regeneration_at_turn, TimePrototype.get_current_turn_number()-1) self.storage._test_save()
def create_message(self, uid): return heroes_messages.MessageSurrogate(turn_number=TimePrototype.get_current_turn_number(), timestamp=time.time(), key=None, externals=None, message='message {}'.format(uid), position='position {}'.format(uid))
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 create(cls, person, utg_name): 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 cls._model_class.objects.filter(person_id=person.id).delete() x, y = random.choice(list(cls.get_available_positions(person.place.x, person.place.y))) model = models.Building.objects.create(x=x, y=y, data=s11n.to_json({'name': utg_name.serialize()}), place_id=person.place_id, person_id=person.id, state=relations.BUILDING_STATE.WORKING, created_at_turn=TimePrototype.get_current_turn_number(), type=person.type.building_type) prototype = cls(model=model) storage.buildings.add_item(prototype.id, prototype) storage.buildings.update_version() return prototype
def add_power_evenly(self, delta): cells_number = len(self.power_points) turn_number = TimePrototype.get_current_turn_number() self.power_points[:] = [ (turn, value+delta/cells_number / (1 - float(turn_number- turn) / history_length)) for turn, value in self.power_points ]
def apply(self): if not self.state.is_VOTING: raise exceptions.ApplyBillInWrongStateError(bill_id=self.id) if not self.approved_by_moderator: raise exceptions.ApplyUnapprovedBillError(bill_id=self.id) if self.time_before_voting_end != datetime.timedelta(seconds=0): raise exceptions.ApplyBillBeforeVoteWasEndedError(bill_id=self.id) self.recalculate_votes() self._model.min_votes_percents_required = bills_settings.MIN_VOTES_PERCENT results_text = 'Итоги голосования: %d «за», %d «против» (итого %.1f%% «за»), %d «воздержалось».' % (self.votes_for, self.votes_against, round(self.votes_for_percents, 3)*100, self.votes_refrained) self._model.voting_end_at = datetime.datetime.now() self.applyed_at_turn = TimePrototype.get_current_turn_number() with transaction.atomic(): if self.is_percents_barier_not_passed: self.state = BILL_STATE.REJECTED self.save() PostPrototype.create(ThreadPrototype(self._model.forum_thread), get_system_user(), 'Законопроект отклонён.\n\n%s' % results_text, technical=True) signals.bill_processed.send(self.__class__, bill=self) return False self.data.apply(self) self.state = BILL_STATE.ACCEPTED with achievements_storage.verify(type=ACHIEVEMENT_TYPE.POLITICS_ACCEPTED_BILLS, object=self.owner): self.save() PostPrototype.create(ThreadPrototype(self._model.forum_thread), get_system_user(), 'Законопроект принят. Изменения вступят в силу в ближайшее время.\n\n%s' % results_text, technical=True) for actor in self.data.actors: if isinstance(actor, places_objects.Place): actor.effects.add(effects.Effect(name='закон №{}'.format(self.id), attribute=places_relations.ATTRIBUTE.STABILITY, value=-self.type.stability)) logic.initiate_actual_bills_update(self._model.owner_id) signals.bill_processed.send(self.__class__, bill=self) return True
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 game_time = TimePrototype.get_current_time() data = {'mode': 'pve', 'turn': game_time.ui_info(), 'game_state': GameState.state().value, 'map_version': map_info_storage.version, 'account': None, 'enemy': None } if account: battle = Battle1x1Prototype.get_by_account_id(account.id) data['account'] = _form_game_account_info(game_time, 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(game_time, AccountPrototype.get_by_id(battle.enemy_id), in_pvp_queue=False, is_own=False, client_turns=client_turns) return data
def quest_test_method(self): # defends from first quest rule self.hero.statistics.change_quests_done(1) self.hero.save() current_time = TimePrototype.get_current_time() test_upgrade_equipment = random.randint(0, 1) # test child quest or upgrade equipment for SearchSmith while self.hero.actions.current_action.TYPE != ActionQuestPrototype.TYPE or not self.hero.quests.has_quests: if quest == SearchSmith and test_upgrade_equipment: self.hero._model.money = QuestPrototype.upgrade_equipment_cost(self.hero) * 2 self.hero._model.next_spending = ITEMS_OF_EXPENDITURE.INSTANT_HEAL self.storage.process_turn() current_time.increment_turn() # test if quest is serializable s11n.to_json(self.hero.quests.current_quest.serialize()) self.complete_quest() self.assertEqual(self.hero.actions.current_action.TYPE, ActionIdlenessPrototype.TYPE) if quest == SearchSmith and test_upgrade_equipment: self.assertTrue(self.hero.statistics.money_spend_for_artifacts > 0 or self.hero.statistics.money_spend_for_sharpening > 0)
def test_sequence_move(self): current_time = TimePrototype.get_current_time() while self.hero.actions.current_action != self.action_idl: self.storage.process_turn(continue_steps_if_needed=False) current_time.increment_turn() self.assertEqual(self.hero.position.road.point_1_id, self.p2.id) self.assertEqual(self.hero.position.road.point_2_id, self.p3.id) real_percents = None prototypes.ActionMoveToPrototype.create(hero=self.hero, destination=self.p1, break_at=0.9) while self.hero.actions.current_action != self.action_idl: real_percents = self.hero.actions.current_action.percents self.storage.process_turn(continue_steps_if_needed=False) current_time.increment_turn() self.assertEqual(round(real_percents, 1), 0.9) self.assertEqual(self.hero.position.road.point_1_id, self.p1.id) self.assertEqual(self.hero.position.road.point_2_id, self.p2.id) prototypes.ActionMoveToPrototype.create(hero=self.hero, destination=self.p2) while self.hero.position.place is None or self.hero.position.place.id != self.p2.id: self.storage.process_turn(continue_steps_if_needed=False) current_time.increment_turn() self.assertEqual(self.hero.actions.current_action.TYPE, prototypes.ActionInPlacePrototype.TYPE) self.storage._test_save()
def _end_battle(self, hero_1_health, hero_2_health): self.hero_1.health = hero_1_health self.hero_2.health = hero_2_health current_time = TimePrototype.get_current_time() self.meta_action_battle.process() current_time.increment_turn() self.meta_action_battle.process()
def test_not_enough_voices_percents(self): current_time = TimePrototype.get_current_time() current_time.increment_turn() current_time.increment_turn() 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, current_time.turn_number) self.check_place(self.place1.id, self.place1.name, self.place1.utg_name.forms)
def update(self, text): self._model.text = text self._model.updated_at_turn = TimePrototype.get_current_turn_number() self.save() self.thread.update() self.thread.subcategory.update()
def test_initialize(self): self.assertEqual(self.person.place.persons_changed_at_turn, self.persons_changed_at_turn) self.assertEqual(self.person.friends_number, 0) self.assertEqual(self.person.enemies_number, 0) self.assertEqual(self.person.created_at_turn, TimePrototype.get_current_turn_number() - 1) self.assertTrue(self.person.is_stable)
def test_sell_and_continue(self): old_money = self.hero.money artifact = artifacts_storage.generate_artifact_from_list(artifacts_storage.artifacts, self.hero.level, rarity=RARITY.NORMAL) self.hero.bag.put_artifact(artifact) artifact = artifacts_storage.generate_artifact_from_list(artifacts_storage.artifacts, self.hero.level, rarity=RARITY.NORMAL) self.hero.bag.put_artifact(artifact) self.assertEqual(self.hero.bag.occupation, 2) self.action_trade.percents_barier = 2 current_time = TimePrototype.get_current_time() self.storage.process_turn() self.assertEqual(len(self.hero.actions.actions_list), 2) self.assertEqual(self.hero.actions.current_action, self.action_trade) self.assertEqual(self.hero.bag.occupation, 1) self.assertTrue(self.hero.money > old_money) old_money = self.hero.money current_time.increment_turn() self.storage.process_turn(continue_steps_if_needed=False) self.assertEqual(len(self.hero.actions.actions_list), 1) self.assertEqual(self.hero.actions.current_action, self.action_idl) self.assertEqual(self.hero.bag.occupation, 0) self.assertTrue(self.hero.money > old_money) self.storage._test_save()
def test_stop_when_quest_required_replane(self): self.action_idl.percents = 0.0 self.assertFalse(self.action_trade.replane_required) artifact = artifacts_storage.generate_artifact_from_list(artifacts_storage.artifacts, self.hero.level, rarity=RARITY.NORMAL) self.hero.bag.put_artifact(artifact) artifact = artifacts_storage.generate_artifact_from_list(artifacts_storage.artifacts, self.hero.level, rarity=RARITY.NORMAL) self.hero.bag.put_artifact(artifact) self.action_trade.percents_barier = 2 self.assertEqual(self.hero.bag.occupation, 2) current_time = TimePrototype.get_current_time() self.storage.process_turn() self.assertEqual(self.hero.bag.occupation, 1) self.assertEqual(len(self.hero.actions.actions_list), 2) self.assertEqual(self.hero.actions.current_action, self.action_trade) self.action_trade.replane_required = True current_time.increment_turn() self.storage.process_turn(continue_steps_if_needed=False) self.assertEqual(self.hero.bag.occupation, 1) self.assertEqual(len(self.hero.actions.actions_list), 1) self.assertEqual(self.action_trade.state, self.action_trade.STATE.PROCESSED) self.assertEqual(self.hero.actions.current_action, self.action_idl)
def create(cls, owner, caption, rationale, bill, chronicle_on_accepted): model = Bill.objects.create(owner=owner._model, type=bill.type, caption=caption, rationale=rationale, created_at_turn=TimePrototype.get_current_turn_number(), technical_data=s11n.to_json(bill.serialize()), state=BILL_STATE.VOTING, chronicle_on_accepted=chronicle_on_accepted, votes_for=1) # author always wote for bill bill_prototype = cls(model) text = u'Обсуждение [url="%s%s"]закона[/url]' % (project_settings.SITE_URL, reverse('game:bills:show', args=[model.id]) ) thread = ThreadPrototype.create(SubCategoryPrototype.get_by_uid(bills_settings.FORUM_CATEGORY_UID), caption=caption, author=get_system_user(), text=bill_prototype.bill_info_text(text), technical=True, markup_method=MARKUP_METHOD.POSTMARKUP) model.forum_thread = thread._model model.save() ActorPrototype.update_actors(bill_prototype, bill_prototype.data.actors) VotePrototype.create(owner, bill_prototype, VOTE_TYPE.FOR) signals.bill_created.send(sender=cls, bill=bill_prototype) return bill_prototype
def create_message(self, message, turn_delta=0, time_delta=0, position=u'some position info'): return messages.MessageSurrogate(turn_number=TimePrototype.get_current_turn_number() + turn_delta, timestamp=time.time() + time_delta, key=None, externals=None, message=message, position=position)
def test_processed(self): current_time = TimePrototype.get_current_time() self.assertEqual(self.hero.journal.messages_number(), 2) with self.check_calls_count("the_tale.game.heroes.logic.push_message_to_diary", 0): self.storage.process_turn() current_time.increment_turn() self.assertEqual(self.hero.journal.messages_number(), 3) self.storage.process_turn() current_time.increment_turn() self.assertEqual(self.hero.journal.messages_number(), 4) self.storage.process_turn(continue_steps_if_needed=False) current_time.increment_turn() self.assertEqual(self.hero.journal.messages_number(), 5) self.assertTrue(self.hero.actions.current_action.TYPE.is_IDLENESS) self.storage._test_save()
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=TimePrototype.get_current_turn_number(), state=POST_STATE.DEFAULT) prototype = cls(model=thread_model) subcategory.update() MessagePrototype.create(ForumThreadHandler(thread_id=prototype.id)) return prototype
def process_turn(self, logger=None, continue_steps_if_needed=True): self.switch_caches() timestamp = time.time() turn_number = TimePrototype.get_current_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))
def test_buy_meal__from_moveto(self): prototypes.ActionMoveToPrototype.create(hero=self.hero, destination=self.place_3) current_time = TimePrototype.get_current_time() with contextlib.nested( self.check_decreased(lambda: self.hero.money), self.check_increased(lambda: self.hero.statistics.money_spend_for_companions)): while self.hero.actions.current_action.TYPE != prototypes.ActionInPlacePrototype.TYPE: current_time.increment_turn() self.storage.process_turn(continue_steps_if_needed=False)
def test_companion_healing_by_hero__not_healed(self): current_time = TimePrototype.get_current_time() self.hero.companion.health = 1 with self.check_delta(lambda: self.hero.companion.health, c.COMPANIONS_HEALTH_PER_HEAL): self.action_heal_companion.state = self.action_heal_companion.STATE.PROCESSED self.storage.process_turn(continue_steps_if_needed=False) current_time.increment_turn()
def test_buy_meal(self): self.hero.position.last_place_visited_turn = TimePrototype.get_current_turn_number( ) - c.TURNS_IN_HOUR * 12 with contextlib.nested( self.check_decreased(lambda: self.hero.money), self.check_delta( lambda: self.hero.statistics.money_spend_for_companions, int(f.expected_gold_in_day(self.hero.level) / 2 * 0.5) + 1), self.check_increased(lambda: len(self.hero.messages))): prototypes.ActionInPlacePrototype.create(hero=self.hero)
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 = TimePrototype.get_current_turn_number() self.effect = effect self.power_required = normal_power * effect.power_modifier
def test_full_move(self): current_time = TimePrototype.get_current_time() while self.hero.position.place is None or self.hero.position.place.id != self.p3.id: self.storage.process_turn(continue_steps_if_needed=False) current_time.increment_turn() self.assertEqual(self.hero.actions.current_action.TYPE, prototypes.ActionInPlacePrototype.TYPE) self.storage._test_save()
def test_user_form__move_delay(self): data = { 'caption': 'caption-caption', 'rationale': 'rationale', 'chronicle_on_accepted': 'chronicle-on-accepted', 'person': self.person_2.id, 'new_place': self.place3.id } form = self.bill.data.get_user_form_update(post=data, owner_id=self.account.id) self.assertFalse(form.is_valid()) TimePrototype.get_current_time().increment_turn() form = self.bill.data.get_user_form_update(post=data, owner_id=self.account.id) self.assertTrue(form.is_valid())
def test_full__double_energy(self): self.hero.change_energy(-self.hero.energy) current_time = TimePrototype.get_current_time() while len(self.hero.actions.actions_list) != 1: self.storage.process_turn(continue_steps_if_needed=False) current_time.increment_turn() self.assertTrue(self.action_idl.leader) self.assertEqual( self.hero.energy, f.angel_energy_regeneration_amount( self.hero.preferences.energy_regeneration_type) * 2) self.assertEqual(self.hero.need_regenerate_energy, False) self.assertEqual(self.hero.last_energy_regeneration_at_turn, TimePrototype.get_current_turn_number() - 1) self.storage._test_save()
def test_not_enough_money(self): self.hero.position.last_place_visited_turn = TimePrototype.get_current_turn_number( ) - 1 with contextlib.nested( self.check_delta(lambda: self.hero.money, -self.hero.money), self.check_delta( lambda: self.hero.statistics.money_spend_for_companions, self.hero.money), self.check_increased(lambda: len(self.hero.messages))): prototypes.ActionInPlacePrototype.create(hero=self.hero)
def move_out_game(self): self._model.out_game_at = datetime.datetime.now() self._model.state = PERSON_STATE.OUT_GAME self.place.persons_changed_at_turn = TimePrototype.get_current_turn_number( ) building = buildings_storage.get_by_person_id(self.id) if building: building.destroy()
def test_full_quest(self): current_time = TimePrototype.get_current_time() # just test that quest will be ended while not self.action_idl.leader: self.storage.process_turn() current_time.increment_turn() self.storage._test_save() self.assertFalse(self.hero.quests.has_quests)
def _start_quest(self, start, hero): hero.quests.update_history(start.type, TimePrototype.get_current_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))
def test_post_created_at_turn(self): current_turn = TimePrototype.get_current_time() current_turn.increment_turn() current_turn.increment_turn() ThreadPrototype.create(self.subcategory, 'thread-2-caption', self.account, 'thread-2-text') self.assertEqual(PostPrototype._db_latest().created_at_turn, current_turn.turn_number)
def test_lighting_when_mob_killed(self): current_time = TimePrototype.get_current_time() action_battle = actions_prototypes.ActionBattlePvE1x1Prototype.create( hero=self.hero, mob=mobs_storage.create_mob_for_hero(self.hero)) current_time.increment_turn() self.storage.process_turn() action_battle.mob.health = 0 self.assertFalse(HELP_CHOICES.LIGHTING in action_battle.help_choices)
def test_created_at_turn(self): current_turn = TimePrototype.get_current_time() current_turn.increment_turn() current_turn.increment_turn() post = PostPrototype.create(thread=self.thread, author=self.account, text='post-1-text') self.assertEqual(post.created_at_turn, current_turn.turn_number)
def test_is_old__not_own_hero(self): self.assertFalse( form_game_info(self.account_1, is_own=False)['account']['is_old']) TimePrototype(turn_number=666).save() self.assertTrue( form_game_info(self.account_1, is_own=False)['account']['is_old']) HeroPrototype.get_by_account_id(self.account_1.id).save() self.assertFalse( form_game_info(self.account_1, is_own=False)['account']['is_old'])
def test_full(self): current_time = TimePrototype.get_current_time() while len(self.hero.actions.actions_list) != 1: self.storage.process_turn(continue_steps_if_needed=False) current_time.increment_turn() self.assertTrue(self.action_idl.leader) self.storage._test_save()
def test_full_battle(self): current_time = TimePrototype.get_current_time() while len(self.hero.actions.actions_list) != 1: self.storage.process_turn() current_time.increment_turn() self.assertTrue(self.action_idl.leader) self.storage._test_save()
def test_approved(self): current_time = TimePrototype.get_current_time() current_time.increment_turn() current_time.increment_turn() current_time.increment_turn() 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 = linguistics_helpers.get_word_post_data( self.bill.data.name_forms, prefix='name') data.update({'approved': True}) form = PlaceRenaming.ModeratorForm(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.sync(force=True) self.place1.sync_parameters() self.assertTrue(self.place1.stability < 1.0) self.assertEqual(bill.applyed_at_turn, current_time.turn_number) self.check_place(self.place1.id, u'new_name_1-нс,ед,им', self.bill.data.name_forms.forms)
def ui_info(self, with_info=False): current_turn = TimePrototype.get_current_turn_number() messages = [] for message in self.messages: if message.turn_number > current_turn: break messages.append(message.ui_info(with_info=with_info)) return messages
def test_inplace(self): current_time = TimePrototype.get_current_time() self.storage.process_turn(continue_steps_if_needed=False) self.hero.position.percents = 1 current_time.increment_turn() self.storage.process_turn(continue_steps_if_needed=False) self.assertEqual(self.hero.actions.current_action.TYPE, prototypes.ActionInPlacePrototype.TYPE) self.storage._test_save()
def hero_position_from_model(hero_model): return position.Position(last_place_visited_turn=TimePrototype.get_current_turn_number(), # TODO: get from model moved_out_place=False, # TODO: get from model place_id=hero_model.pos_place_id, road_id=hero_model.pos_road_id, previous_place_id=hero_model.pos_previous_place_id, invert_direction=hero_model.pos_invert_direction, percents=hero_model.pos_percents, from_x=hero_model.pos_from_x, from_y=hero_model.pos_from_y, to_x=hero_model.pos_to_x, to_y=hero_model.pos_to_y)
def save(self): self._model.saved_at_turn = TimePrototype.get_current_turn_number() self._model.saved_at = datetime.datetime.now() self.serialize_companion() self._model.data = s11n.to_json(self.data) if self.bag.updated: self.bag.serialize() if self.equipment.updated: self._model.raw_power_physic = self.power.physic self._model.raw_power_magic = self.power.magic self.equipment.serialize() if self.abilities.updated: self.abilities.serialize() if self.places_history.updated: self.places_history.serialize() if self.cards.updated: self.cards.serialize() if self.messages.updated: self._model.messages = s11n.to_json(self.messages.serialize()) self.messages.updated = False if self.diary.updated: self.diary.serialize() if self.actions.updated: self.actions.on_save() self._model.actions = s11n.to_json(self.actions.serialize()) self.actions.updated = False if self.quests.updated: self._model.quest_created_time = self.quests.min_quest_created_time self.quests.serialize() if self.pvp.updated: self.pvp.serialize() if self.preferences.updated: self._model.preferences = s11n.to_json( self.preferences.serialize()) self.preferences.updated = False self._model.stat_politics_multiplier = self.politics_power_multiplier( ) if self.can_change_all_powers() else 0 database.raw_save(self._model)
def test_is_old__not_own_hero(self): self.assertFalse( form_game_info(self.account_1, is_own=False)['account']['is_old']) TimePrototype(turn_number=666).save() self.assertTrue( form_game_info(self.account_1, is_own=False)['account']['is_old']) heroes_logic.save_hero( heroes_logic.load_hero(account_id=self.account_1.id)) self.assertFalse( form_game_info(self.account_1, is_own=False)['account']['is_old'])
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=TimePrototype.get_current_turn_number(), effect=effect, positive_power=0, negative_power=0, power_required=normal_power * effect.power_modifier)
def create_message(self, message, turn_delta=0, time_delta=0, position='some position info'): return messages.MessageSurrogate( turn_number=TimePrototype.get_current_turn_number() + turn_delta, timestamp=time.time() + time_delta, key=None, externals=None, message=message, position=position)
def test_push_message(self): current_time = TimePrototype.get_current_time() self.messages.push_message(self.create_message('1')) self.messages.push_message(self.create_message('2')) current_time.increment_turn() self.messages.push_message(self.create_message('3')) self.assertEqual([msg.message for msg in self.messages.messages], ['1', '2', '3'])
def push_power(self, turn, value): points = getattr(self, name) if points and points[-1][0] > turn: raise exception_class(message=u'can not push power to place "%s" - current push turn number (%d) less then latest (%d) ' % (self.name, points[-1][0], turn)) points.append((turn, value)) current_turn_number = TimePrototype.get_current_turn_number() while points and points[0][0] < current_turn_number - history_length: points.pop(0)
def check_companion_healing_by_hero(self, companion_type): current_time = TimePrototype.get_current_time() self.hero.companion.health = 1 with self.check_delta( lambda: self.hero.companion.health, c.COMPANIONS_HEALTH_PER_HEAL + c.COMPANIONS_REGEN_BY_HERO): self.companion_record.type = companion_type self.action_heal_companion.state = self.action_heal_companion.STATE.PROCESSED self.storage.process_turn(continue_steps_if_needed=False) current_time.increment_turn()
def create(cls, place_id, road_id): return cls( last_place_visited_turn=TimePrototype.get_current_turn_number(), moved_out_place=False, place_id=place_id, road_id=road_id, previous_place_id=None, invert_direction=False, percents=0, from_x=None, from_y=None, to_x=None, to_y=None)
def test_sync_data__power_from_bonuses_2(self): person_1 = self.p1.persons[0] self.assertEqual(self.p1.power, 0) self.assertEqual(person_1.power, 0) person_1.push_power(TimePrototype.get_current_turn_number(), 10000) self.p1.push_power(TimePrototype.get_current_turn_number(), 10000) self.worker.process_change_power(person_id=person_1.id, power_delta=-1000, place_id=None, positive_bonus=0, negative_bonus=0) self.worker.sync_data() self.assertEqual(self.p1.power, 10000 - 1000 * 2 * 1.5) self.assertEqual(person_1.power, 10000 - 1000 * 2) self.worker.process_change_power(place_id=self.p1.id, power_delta=100, person_id=None, positive_bonus=0, negative_bonus=0) self.worker.sync_data() self.assertEqual(self.p1.power, 10000 - 1000 * 2 * 1.5 + 100 * 1.25) self.assertEqual(person_1.power, 10000 - 1000 * 2)