def handle_set_info(new_card: Card, existing_card: Card): """For matching cards, merges the set data from the newly imported card to that of the existing card. If the sets do not match, a new set will be added to the card object. If the sets do match, the sets conditions will be merged. :param new_card: the card being imported :param existing_card: the card that has already been imported :return: updated card with adjusted set data :rtype: Card """ new_card_set = SetInfo() try: new_card_set = new_card.get_set_info()[0] except IndexError: logger.error('Error occurred with card: ' + new_card.get_name(), IndexError) if new_card_set == SetInfo(): return None no_match_found = True existing_sets = existing_card.get_set_info() existing_set: SetInfo for existing_set in existing_sets: if DataImport.do_sets_match(existing_set, new_card_set): no_match_found = False new_condition: str = DataImport.get_key_from_value(new_card_set.get_conditions(), 1) updated_conditions = DataImport.handle_conditions(existing_set.get_conditions(), new_condition) existing_set.set_conditions(updated_conditions) break if no_match_found: existing_sets.append(new_card_set) existing_card.set_set_info(existing_sets) return existing_card
def test_sets_partial_match(self): set1: SetInfo = SetInfo(set_number='STON-ENSE1', edition='LE') set2: SetInfo = SetInfo(set_number='STON-ENSE1', edition='FE') result = DataImport.do_sets_match(set1, set2) self.assertFalse(result) set1: SetInfo = SetInfo(set_number='STON-ENSE1', edition='FE') set2: SetInfo = SetInfo(set_number='DP04-EN012', edition='FE') result = DataImport.do_sets_match(set1, set2) self.assertFalse(result)
def test_define_card_object(self): header = [ 'name', 'race_type', 'attribute', 'level', 'attack', 'defense', 'edition', 'set_number', 'pass_code', 'condition' ] indexes = DataImport.init_indexes(header) card_info = [ 'Blue-Eyes White Dragon', 'Dragon', 'Light', 8, 3000, 2500, None, 'SDK-001', 89631139, 'LP' ] card_object = DataImport.define_card_object(indexes, card_info) predefined_set_object = SetInfo(set_number='SDK-001', conditions={ 'NM': 0, 'LP': 1, 'MP': 0, 'HP': 0, 'D': 0 }) predefined_card_object = Card(name='Blue-Eyes White Dragon', race='Dragon', monster_type='Normal Monster', attribute='Light', level=8, attack=3000, defense=2500, pass_code=89631139, set_info=[predefined_set_object]) self.assertEqual(card_object, predefined_card_object)
def do_sets_match(set1: SetInfo, set2: SetInfo): """Verifies if the sets of 2 of the same cards match. This is based on the cards set number and edition. :param set1: set1 :type set1: SetInfo :param set2: set2 :type set2: SetInfo :return: returns True if card sets match, else returns false. :rtype: bool """ match = False if set1.get_set_number() == set2.get_set_number() \ and set1.get_edition() == set2.get_edition() \ and set1.get_rarity() == set2.get_rarity(): match = True return match
def test_sets_do_match(self): set1: SetInfo = SetInfo(set_number='CDIP-EN035', edition='FE') set2: SetInfo = SetInfo(set_number='CDIP-EN035', edition='FE') result = DataImport.do_sets_match(set1, set2) self.assertTrue(result)
class TestHandleSetInfo(unittest.TestCase): _test_set_info = SetInfo(set_number='STON-ENSE1', conditions={ 'NM': 1, 'LP': 0, 'MP': 0, 'HP': 0, 'D': 0 }, edition='LE') _test_card = Card(name='Cyber End Dragon', monster_type='Machine / Fusion / Effect', attribute='Light', level=10, attack=4000, defense=2800, pass_code=1546123, set_info=[_test_set_info]) def test_different_sets(self): test_hsi = TestHandleSetInfo test_set_info2 = test_hsi._test_set_info test_set_info2.set_edition('FE') test_card2 = test_hsi._test_card test_card2.set_set_info([test_set_info2]) result_card = DataImport.handle_set_info(test_hsi._test_card, test_card2) expected_card = test_hsi._test_card expected_card.set_set_info([test_set_info2, test_hsi._test_set_info]) self.assertEqual(result_card, expected_card) def test_same_sets(self): test_hsi = TestHandleSetInfo result_card = DataImport.handle_set_info(test_hsi._test_card, test_hsi._test_card) expected_set_info = test_hsi._test_set_info expected_set_info.set_conditions({ 'NM': 2, 'LP': 0, 'MP': 0, 'HP': 0, 'D': 0 }) expected_card = test_hsi._test_card expected_card.set_set_info([expected_set_info]) self.assertEqual(result_card, expected_card) def test_similar_sets(self): test_hsi = TestHandleSetInfo set_info1 = test_hsi._test_set_info set_info1.set_conditions({'NM': 0, 'LP': 0, 'MP': 0, 'HP': 1, 'D': 0}) card1 = test_hsi._test_card card1.set_set_info([set_info1]) set_info2 = test_hsi._test_set_info set_info2.set_conditions({'NM': 1, 'LP': 1, 'MP': 0, 'HP': 0, 'D': 0}) card2 = test_hsi._test_card card2.set_set_info([set_info2]) result_card = DataImport.handle_set_info(card1, card2) expected_set_info = test_hsi._test_set_info expected_set_info.set_conditions({ 'NM': 1, 'LP': 1, 'MP': 0, 'HP': 1, 'D': 0 }) expected_card = test_hsi._test_card expected_card.set_set_info([expected_set_info]) self.assertEqual(result_card, expected_card) def test_same_set_different_editions(self): test_hsi = TestHandleSetInfo set_info1 = test_hsi._test_set_info card1 = test_hsi._test_card set_info2 = test_hsi._test_set_info set_info2.set_conditions({'NM': 0, 'LP': 0, 'MP': 0, 'HP': 0, 'D': 1}) set_info2.set_edition(None) card2 = test_hsi._test_card card2.set_set_info([set_info2]) result_card = DataImport.handle_set_info(card1, card2) expected_card = test_hsi._test_card expected_card.set_set_info([set_info2, set_info1]) self.assertEqual(result_card, expected_card)
def define_card_object(indexes: dict, card_info: list): """Creates card object and sets card attributes from imported information. :param indexes: indexes of imported data :type indexes: dict :param card_info: imported card data in list format :type card_info: list :return: newly created card object :rtype: Card """ card_object = Card() # Replace empty strings with None type convert = lambda i: i or None card_info = [convert(i) for i in card_info] # Align card object information if indexes['name'] is not None: card_object.set_name(card_info[indexes['name']]) if indexes['attribute'] is not None: card_object.set_attribute(card_info[indexes['attribute']]) if indexes['level'] is not None: card_object.set_level(int(card_info[indexes['level']])) if indexes['attack'] is not None: card_object.set_attack(int(card_info[indexes['attack']])) if indexes['defense'] is not None: card_object.set_defense(int(card_info[indexes['defense']])) if indexes['pass_code'] is not None: card_object.set_pass_code(card_info[indexes['pass_code']]) if indexes['description'] is not None: card_object.set_description(card_info[indexes['description']]) if indexes['race_type'] is not None: race_types: dict = DataImport.get_monster_race_and_type(card_info[indexes['race_type']]) card_object.set_race(race_types.get('race')) card_object.set_monster_type(race_types.get('monster_type')) # Align card set info with card object. There can be more than one card set per card object set_info = SetInfo() if indexes['set_number'] is not None: set_info.set_set_number(card_info[indexes['set_number']]) if indexes['edition'] is not None: set_info.set_edition(card_info[indexes['edition']]) if indexes['rarity'] is not None: set_info.set_rarity(card_info[indexes['rarity']]) # Conditions are handled a bit differently. They are stored in dictionary and are used to determine quantity new_condition = card_info[indexes['condition']] conditions = DataImport.handle_conditions(set_info.get_conditions(), new_condition) if indexes['condition'] is not None: set_info.set_conditions(conditions) card_object.append_set_info(set_info) return card_object