def grant_empty_stats_to_char_full(self): stats_snapshot = ContractStats(assigned_character=self.char_full, is_snapshot=True, exp_cost=0) stats_snapshot.save() self.char_full.stats_snapshot = stats_snapshot self.char_full.save()
def grant_basic_stats_to_char(self, char): stats_snapshot = ContractStats(assigned_character=char, is_snapshot=True) stats_snapshot.save() char.stats_snapshot = stats_snapshot char.save() stats_diff = ContractStats(assigned_character=char) stats_diff.save() AssetDetails.objects.create( details="original deets", relevant_stats=stats_diff, relevant_asset=self.asset ) LiabilityDetails.objects.create( details="original deets", relevant_stats=stats_diff, relevant_liability=self.liability ) AttributeValue.objects.create( value=3, relevant_stats=stats_diff, relevant_attribute=self.attribute_str, ) AbilityValue.objects.create( value=2, relevant_stats=stats_diff, relevant_ability=self.ability, ) char.regen_stats_snapshot()
def test_stats_snapshot_assets( self): #TODO: test liabilities, traits, etc. self.grant_basic_stats_to_char_full() stats_diff = ContractStats(assigned_character=self.char_full) stats_diff.save() new_deets = AssetDetails.objects.create( details="new_details", relevant_stats=stats_diff, relevant_asset=self.asset, previous_revision=self.char_full.stats_snapshot.assetdetails_set. all()[0].previous_revision) new_deets.save() stats_diff.save() self.char_full.regen_stats_snapshot() self.assertEquals( self.char_full.stats_snapshot.assetdetails_set.all().count(), 1) self.assertEquals( self.char_full.stats_snapshot.assetdetails_set.all()[0].details, "new_details") stats_diff2 = ContractStats(assigned_character=self.char_full) stats_diff2.save() even_newer_deets = AssetDetails.objects.create( details="newer_details", relevant_stats=stats_diff2, relevant_asset=self.asset, previous_revision=new_deets) even_newer_deets.save() self.char_full.regen_stats_snapshot() self.assertEquals( self.char_full.stats_snapshot.assetdetails_set.all().count(), 1) self.assertEquals( self.char_full.stats_snapshot.assetdetails_set.all()[0].details, "newer_details") stats_history = self.char_full.contractstats_set.filter( is_snapshot=False).order_by("created_time").all() self.assertEquals(stats_history.count(), 3) snapshots = self.char_full.contractstats_set.filter( is_snapshot=True).all() self.assertEquals(snapshots.count(), 1)
def test_traits_and_quirks_cannot_have_snapshot_prev_rev(self): self.grant_basic_stats_to_char_full() stats_diff = ContractStats(assigned_character=self.char_full) stats_diff.save() with self.assertRaises(ValueError): AssetDetails.objects.create( details="new_details", relevant_stats=stats_diff, relevant_asset=self.asset, previous_revision=self.char_full.stats_snapshot. assetdetails_set.all()[0], is_deleted=True, ) with self.assertRaises(ValueError): AttributeValue.objects.create( value=1, relevant_stats=stats_diff, relevant_attribute=self.attribute_str, previous_revision=self.char_full.stats_snapshot. attributevalue_set.all()[0], )
def handle(self, *args, **options): characters = Character.objects.all() successful = 0 failure = 0 for character in characters: try: with transaction.atomic(): new_stats_rev = ContractStats(assigned_character=character) new_stats_rev.save() assets = character.stats_snapshot.assetdetails_set.all() for asset in assets: asset.relevant_asset.grant_element_if_needed(new_stats_rev, asset.details) liabilities = character.stats_snapshot.liabilitydetails_set.all() for liability in liabilities: liability.relevant_liability.grant_element_if_needed(new_stats_rev, liability.details) new_stats_rev.save() character.regen_stats_snapshot() successful = successful + 1 logger.info('Finished character %s id %s', str(character.name), str(character.id)) self.stdout.write( self.style.SUCCESS('Finished character %s id %s' % (character.name, character.id))) except Exception as inst: logger.error('Error regenerating stats snapshot for character %s id %s', str(character.name), str(character.id)) logger.exception(inst) failure = failure + 1 self.stdout.write(self.style.SUCCESS('Regenerated snapshots. %d successful. %d failed.' % (successful, failure)))
def test_stats_snapshot_assets_history(self): self.grant_basic_stats_to_char_full() stats_diff = ContractStats(assigned_character=self.char_full) stats_diff.save() new_deets = AssetDetails.objects.create( details="new_details", relevant_stats=stats_diff, relevant_asset=self.asset, previous_revision=self.char_full.stats_snapshot.assetdetails_set. all()[0].previous_revision) new_deets.save() stats_diff.save() self.char_full.regen_stats_snapshot() stats_diff2 = ContractStats(assigned_character=self.char_full) stats_diff2.save() even_newer_deets = AssetDetails.objects.create( details="newer_details", relevant_stats=stats_diff2, relevant_asset=self.asset, previous_revision=new_deets) even_newer_deets.save() self.char_full.regen_stats_snapshot() stats_history = self.char_full.contractstats_set.filter( is_snapshot=False).order_by("created_time").all() self.assertEquals(stats_history[0].assetdetails_set.all()[0].details, "original deets") self.assertEquals(stats_history[1].assetdetails_set.all()[0].details, "new_details") self.assertEquals(stats_history[2].assetdetails_set.all()[0].details, "newer_details") og_deets = stats_history[2].assetdetails_set.all( )[0].previous_revision.previous_revision self.assertEquals(stats_history[0].assetdetails_set.all()[0].id, og_deets.id) snapshots = self.char_full.contractstats_set.filter( is_snapshot=True).all() self.assertEquals(snapshots.count(), 1)
def test_ability_revision_linage(self): old_stats_rev = ContractStats(assigned_character=self.char_full) old_stats_rev.save() og_rev = AbilityValue.objects.create(relevant_stats=old_stats_rev, relevant_ability=self.ability, previous_revision=None, value=1) stats1 = ContractStats.objects.create( assigned_character=self.char_full) AbilityValue.objects.create(relevant_stats=stats1, relevant_ability=self.ability, previous_revision=None, value=1) stats2 = ContractStats.objects.create( assigned_character=self.char_full) AbilityValue.objects.create(relevant_stats=stats2, relevant_ability=self.ability, previous_revision=None, value=1) stats3 = ContractStats.objects.create( assigned_character=self.char_full) AbilityValue.objects.create(relevant_stats=stats3, relevant_ability=self.ability, previous_revision=og_rev, value=1) stats4 = ContractStats.objects.create( assigned_character=self.char_full) with self.assertRaises(ValueError): AbilityValue.objects.create(relevant_stats=stats4, relevant_ability=self.ability, previous_revision=og_rev, value=1) new_snapshot = ContractStats(assigned_character=self.char_full, is_snapshot=True) new_snapshot.save() AbilityValue.objects.create(relevant_stats=new_snapshot, relevant_ability=self.ability, previous_revision=og_rev, value=1) snapshot2 = ContractStats.objects.create( assigned_character=self.char_full, is_snapshot=True) AbilityValue.objects.create(relevant_stats=snapshot2, relevant_ability=self.ability, previous_revision=og_rev, value=1)
def grant_trauma_to_character(form, character): trauma = Trauma(description=form.cleaned_data['description']) new_stats_rev = ContractStats(assigned_character=character) new_stats_rev.save() trauma.save() trauma_rev = TraumaRevision(relevant_trauma=trauma, relevant_stats=new_stats_rev) new_stats_rev.save() trauma_rev.save() character.regen_stats_snapshot() return trauma_rev
def delete_trauma_rev(character, trauma_rev, used_xp): new_stats_rev = ContractStats(assigned_character=character) new_stats_rev.save() trauma_rev = TraumaRevision( relevant_trauma=trauma_rev.relevant_trauma, relevant_stats=new_stats_rev, previous_revision=trauma_rev, is_deleted=True, was_bought_off=used_xp, ) trauma_rev.save() new_stats_rev.save() character.regen_stats_snapshot()
def __save_stats_diff_from_post(POST, existing_character=None, new_character=None, user=None): if new_character: # Create the character's stats snapshot and save. stats_snapshot = ContractStats(assigned_character=new_character, is_snapshot=True) stats_snapshot.save() new_character.stats_snapshot = stats_snapshot new_character.save() AttributeFormSet = formset_factory(AttributeForm, extra=0) AbilityFormSet = formset_factory(AbilityForm, extra=1) attributes = Attribute.objects.order_by('name') asset_formsets = __get_asset_formsets(existing_character, POST=POST) liability_formsets = __get_liability_formsets(existing_character, POST=POST) if existing_character: new_stats_rev = ContractStats(assigned_character=existing_character) new_stats_rev.save() limit_formset = __get_limit_formset_for_edit(existing_character, POST) for asset_formset in asset_formsets: __save_edit_quirks_from_formset(formset=asset_formset, stats=new_stats_rev, is_asset=True) for liability_formset in liability_formsets: __save_edit_quirks_from_formset(formset=liability_formset, stats=new_stats_rev, is_asset=False) ability_formset = __get_ability_formset_for_edit( existing_character, AbilityFormSet, POST) __save_edit_abilities_from_formset(formset=ability_formset, stats=new_stats_rev) attribute_formset = __get_attribute_formset_for_edit( existing_character, AttributeFormSet, POST) __save_edit_attributes_from_formset(attribute_formset, new_stats_rev) source_formset = __get_source_formset_for_edit(existing_character, POST) __save_edit_sources_from_formset(source_formset, new_stats_rev, user=user) else: # new character new_stats_rev = ContractStats(assigned_character=new_character) new_stats_rev.save() limit_formset = __get_limit_formset_for_edit(new_character, POST) attribute_formset = AttributeFormSet(POST, initial=[{ 'attribute_id': x.id, 'value': 1, 'attribute': x } for x in attributes], prefix="attributes") default_abilities = Ability.objects.filter( is_primary=True).order_by('name') ability_formset = AbilityFormSet(POST, initial=[{ 'ability_id': x.id, 'value': 0, 'ability': x } for x in default_abilities], prefix="abilities") attribute_values = __attributes_from_form(attribute_formset, new_stats_rev) ability_values = __abilities_from_form(ability_formset, new_stats_rev) liabilities = __liabilities_from_formsets(liability_formsets, new_stats_rev) assets = __assets_from_formsets(asset_formsets, new_stats_rev) __limits_from_formset(limit_formset, new_stats_rev) new_stats_rev.save() return new_stats_rev
def test_cannot_clear_stats_revision(self): stats_diff = ContractStats(assigned_character=self.char_full) stats_diff.save() with self.assertRaises(ValueError): stats_diff.clear()
def test_trauma_revision_linage(self): test_trauma = Trauma.objects.create(description="test") old_stats_rev = ContractStats(assigned_character=self.char_full) old_stats_rev.save() og_rev = TraumaRevision( relevant_trauma=test_trauma, relevant_stats=old_stats_rev, previous_revision=None, ) og_rev.save() new_stats_rev = ContractStats(assigned_character=self.char_full) new_stats_rev.save() trauma_rev = TraumaRevision( relevant_trauma=test_trauma, relevant_stats=new_stats_rev, previous_revision=None, is_deleted=True, ) trauma_rev.save() trauma_rev2 = TraumaRevision( relevant_trauma=test_trauma, relevant_stats=new_stats_rev, previous_revision=None, is_deleted=True, ) trauma_rev2.save() trauma_rev3 = TraumaRevision( relevant_trauma=test_trauma, relevant_stats=new_stats_rev, previous_revision=og_rev, is_deleted=True, ) trauma_rev3.save() trauma_rev4 = TraumaRevision( relevant_trauma=test_trauma, relevant_stats=new_stats_rev, previous_revision=og_rev, is_deleted=True, ) with self.assertRaises(ValueError): trauma_rev4.save() new_snapshot = ContractStats(assigned_character=self.char_full, is_snapshot=True) new_snapshot.save() trauma_rev5 = TraumaRevision( relevant_trauma=test_trauma, relevant_stats=new_snapshot, previous_revision=og_rev, is_deleted=True, ) trauma_rev5.save() trauma_rev6 = TraumaRevision( relevant_trauma=test_trauma, relevant_stats=new_snapshot, previous_revision=og_rev, is_deleted=True, ) trauma_rev6.save()