def load_complications(self):
        x = XmlController()
        x.load_file('preferences/preferences.xml')
        extract = x.get_dataset('complications',False, True)

        for array in extract:
            complication = Stat(name=array[0], category=array[1], description=array[2], type='complication')
            self.__complications[array[0]]=complication
 def load_effects(self):
     x = XmlController()
     x.load_file('preferences/effects.xml')
     
     effects = x.get_dataset('effects',False,True)
     num = 0
     for array in effects:
         pass
    def load_table(self, table_name):
        x=XmlController()
        x.load_file('preferences/preferences.xml')
        table = x.read_table(table_name)
        t = Table() 
        #print(table_name)
        for option in table:
            t.add_option(option[0], option[1],option[2],True)

        #print(t)
        return copy.deepcopy(t)
    def load_perks_talents(self):
        x=XmlController()
        x.load_file('preferences/preferences.xml')
        extract = x.get_dataset('perks',False, True)

        for array in extract:
            perk = Stat(name=array[0], description=array[1])
            self.__perks[array[0]]=perk

        extract = x.get_dataset('talents', False, True)

        for array in extract:
            talent = Stat(name=array[0], description=array[1])
            self.__talents[array[0]]=talent
    def load_stats(self):
        x = XmlController()
        x.load_file('preferences/preferences.xml')
        stats = x.get_dataset("primary_stats", True)

        for key, value in stats.items():
            self.__stats[key] = Stat(name=key, type='stat',description=value)

        params = ['name', 'first_source','second_source', 'multiplier','divider']
        derived_stats = x.get_dataset('secondary_stats',False,False,True,params)
        
        for array in derived_stats:
            sources = [array[1], array[2]]
            derived = Stat(name=array[0],type='derived',source_stats=sources, multiplier=array[3], divider=array[4])
            self.__derived_stats[array[0]]=derived
    def convert_from_xml(self, filepath):
        x = XmlController()
        x.load_file(filepath)

        all_tags = ['age', 'type', 'disaster_type', 'amount', 'prison_time', 'effect', 'betrayel_type', 'accident_type'
                   ,'death_type', 'accusation', 'hunter', 'corporation_type', 'complication', 'lucky_type', 'connection_type'
                  , 'friend_type', 'relation', 'reaction', 'enemy_type', 'cause', 'resources', 'love_type', 'mutual_feelings'
                 , 'tragic_type', 'problem' ]
        events = x.get_dataset('lifepath', False, False, True, tag_params=all_tags, tag_collection_with_dict=True)
        #print(str(events))
        
        #OH GOD THE HORROR 
        #print('the horror')

        
        lifepath = []
        
        for event in events:
            datakeys = []
            age = event['age']
            type = event['type']

            datakeys.append(age)

            #print('type is ' +type)

            if type == 'disaster' or type== 'lucky':
                #print('if statements for disaster and lucky')
                datakeys.append('1')
                if type == 'lucky':
                    datakeys.append('1')
                    lucky_type = str.lower(event['lucky_type'])
                    if lucky_type =='powerful connection':
                        datakeys.append('1')
                        connection = str.lower(event['connection_type'])
                        if connection=='it\'s in a local security force.':
                            datakeys.append('1')
                        elif connection=='it\'s in a local altcult leader\'s office.':
                            datakeys.append('2')
                        elif connection=='it\'s in the city mgr\'s office.':
                            datakeys.append('3')
                    elif lucky_type=='fortune':
                        datakeys.append('2')
                        amount = event['amount']
                        datakeys.append(amount)
                    elif lucky_type=='sensei':
                        datakeys.append('4')
                    elif lucky_type=='teacher':
                        datakeys.append('5')
                    elif lucky_type=='nomads':
                        datakeys.append('7')
                    elif lucky_type=='altcult contact':
                        datakeys.append('8')
                    elif lucky_type=='boostergang':
                        datakeys.append('9')
                    elif lucky_type=='combat teacher':
                        datakeys.append('10')
                elif type=='disaster':
                    datakeys.append('2')
                    disaster_type = str.lower(event['disaster_type'])

                    if disaster_type == 'debt':
                        datakeys.append('1')
                        amount = event['amount']
                        datakeys.append(amount)
                    elif disaster_type == 'prison/hostage':
                        datakeys.append('2')
                        prison_time = event['prison_time']
                        datakeys.append(prison_time)
                    elif disaster_type == 'drugs/illness':
                        datakeys.append('3')
                    elif disaster_type == 'betrayel':
                        datakeys.append('4')
                        betrayel_type = str.lower(event['betrayel_type'])

                        if betrayel_type == 'you are being blackmailed.':
                            datakeys.append('1')
                        elif betrayel_type == 'your secret was exposed':
                            datakeys.append('2')
                        elif betrayel_type == 'you were betrayed by a close friend in either romance or career (you choose).':
                            datakeys.append('3')
                    elif disaster_type == 'accident':
                        datakeys.append('5')
                        accident_type = str.lower(event['accident_type'])

                        if accident_type == 'you were terribly maimed and must subtract -2 from your dex.':
                            datakeys.append('1')
                        elif accident_type == 'you were hospitalized for x months that year.':
                            datakeys.append('2')
                        elif accident_type == 'you have lost x months of memory of that year.':
                            datakeys.append('3')
                        elif accident_type == 'you constantly relive nightmares of the accident and wake up screaming.':
                            datakeys.append('4')
                    elif disaster_type == 'death':
                        datakeys.append('6')
                        death_type = str.lower(event['death_type'])
                        if death_type == 'they died accidentally.':
                            datakeys.append('1')
                        elif death_type == 'they were murdered by unknown parties.':
                            datakeys.append('2')
                        elif death_type == 'they were murdered and you know who did it. you just need the proof.':
                            datakeys.append('3')
                    elif disaster_type == 'false accusation':
                        datakeys.append('7')
                        accu_type = str.lower(event['accusation'])
                        if accu_type == 'the accusation is theft':
                            datakeys.append('1')
                        elif accu_type == 'the accusation is cowardice':
                            datakeys.append('2')
                        elif accu_type == 'the accusation is murder':
                            datakeys.append('3')
                        elif accu_type == 'the accusation is rape':
                            datakeys.append('4')
                        elif accu_type == 'the accusation is lying or betrayel':
                            datakeys.append('5')
                    elif disaster_type == 'crime':
                        datakeys.append('8')
                        hunter = str.lower(event['hunter'])
                        if hunter == 'only a couple local renta-cops want you':
                            datakeys.append('1')
                        elif hunter == 'it\'s an entire local Security force':
                            datakeys.append('2')
                        elif hunter == 'it\'s an Altcult Militia':
                            datakeys.append('3')
                        elif hunter == 'it\'s the FBI or equivalent national police force':
                            datakeys.append('4')
                    elif disaster_type == 'corporation':
                        datakeys.append('9')
                        corp_type = str.lower(event['corporation_type'])

                        if corp_type == 'it\'s a small, local firm':
                            datakeys.append('1')
                        elif corp_type == 'it\'s a larger corp with offices Citywide':
                            datakeys.append('2')
                        elif corp_type == 'it\'s a big, national corp with agents in most major cities':
                            datakeys.append('3')
                        elif corp_type == 'it\'s a huge multinational megacorp with armies, ninja and spies everywhere':
                            datakeys.append('4')
                    elif disaster_type == 'breakdown':
                        datakeys.append('10')
                        compl = str.lower(event['complication'])
                        if compl == 'it is some type of nervous disorder, probably from a bioplague- lose 1 pt. ref':
                            datakeys.append('1')
                        if compl == 'it is some kind of mental problem; you suffer anxiety attacks and phobias. lose 1 pt from your pre stat':
                            datakeys.append('2')
                        if compl == 'it is a major psychosis. you hear voices, are violent, irrational, depressive. lose 1 pt from your pre, 1 from ref':
                            datakeys.append('3')

                    
            elif type == 'friend' or type =='enemy':
                #print('friends 4ever?')
                datakeys.append('2')
                if type == 'friend':
                    datakeys.append('1')
                    friend_type = str.lower(event['friend_type'])
                    if friend_type == 'like a big brother/sister to you':
                        datakeys.append('1')
                    elif friend_type == 'like a kid sister/brother to you':
                        datakeys.append('2')
                    elif friend_type == 'a teacher or mentor':
                        datakeys.append('3')
                    elif friend_type == 'a partner or co-worker':
                        datakeys.append('4')
                    elif friend_type == 'an old lover (choose which one)':
                        datakeys.append('5')
                    elif friend_type == 'an old enemy (choose which one)':
                        datakeys.append('6')
                    elif friend_type == 'like a foster parent to you':
                        datakeys.append('7')
                    elif friend_type == 'a relative':
                        datakeys.append('8')
                    elif friend_type == 'reconnect with an old childhood friend':
                        datakeys.append('9')
                    elif friend_type == 'met through a common interest':
                        datakeys.append('10')
                elif type == 'enemy':
                    datakeys.append('2')
                    cause = str.lower(event['cause'])
                    e_type = str.lower(event['enemy_type'])
                    relation = str.lower(event['relation'])
                    reaction = str.lower(event['reaction'])
                    resource = str.lower(event['resources'])

                    e_types = self.prefs.table('enemy_who')
                    causes = self.prefs.table('enemy_cause')
                    relations = self.prefs.table('enemy_hate')
                    reactions = self.prefs.table('enemy_do')
                    resources = self.prefs.table('enemy_resources')

                    values = [e_type, cause, relation, reaction, resource]
                    tables = [e_types, causes, relations, reactions, resources]

                    for table in tables:
                        for i in range(1, table.size()+1):
                            text = table.get_option(i)
                            for value in values:
                                if str.lower(text) == value:
                                    datakeys.append(str(i))
                                    #print('lifepath xml: ' + str.lower(text) + ' == ' + str(value))
                                else:
                                    #print('lifepath xml: ' + str.lower(text) + ' != ' + value)
                                    pass
                            #print(datakeys)

            elif type == 'love':
                datakeys.append('3')
                love_type = str.lower(event['love_type'])
                if love_type == 'happy':
                    datakeys.append('1')
                elif love_type == 'tragic love':
                    datakeys.append('2')
                    tragic_type = str.lower(event['tragic_type'])
                    mutuals = str.lower(event['mutual_feelings'])
                    tragic_table = self.prefs.table('love_tragic')

                    for i in range(1,11):
                        text = str.lower(tragic_table.get_option(i))
                        #print(text + ' == ' + tragic_type)
                        if text == tragic_type:
                            datakeys.append(str(i))

                    mutual_table = self.prefs.table('love_mutual')
                    for i in range(1, 11):
                        text = str.lower(mutual_table.get_option(i))
                        if text == mutuals:
                            datakeys.append(str(i))

                    #print(datakeys)
                    #print(event)
                elif love_type == 'with problems':
                    datakeys.append('3')
                    problem = str.lower(event['problem'])
                    problem_table = self.prefs.table('love_problems')
                    for i in range(1, 10):
                        text = str.lower(problem_table.get_option(i))
                        if text == problem:
                            datakeys.append(str(i))
                elif love_type == 'hot dates':
                    datakeys.append('4')
                elif love_type == 'complicated':
                    datakeys.append('5')
                    compl = str.lower(event['complication'])
                    comp_table = self.prefs.table('love_complicated')
                    for i in range(1, 10):
                        text = str.lower(comp_table.get_option(i))
                        if text == compl:
                            datakeys.append(str(i))          
            else:
                datakeys.append('4')
            lifepath.append(datakeys)
        self.events = lifepath
        #print(lifepath)
        return lifepath
    def load_character(self, filepath, character):
        x=XmlController()
        x.load_file(filepath)
        player = x.get_node_value('info','player')
        fname = x.get_node_value('info', 'first_name')
        sname = x.get_node_value('info', 'second_name')
        lname = x.get_node_value('info', 'last_name')
        alias = x.get_node_value('info', 'alias')
        age = x.get_node_value('info', 'age')

        personality_array = x.get_dataset('personality',False, simple=True, simple_no_dict=True)
        
        for i in range(0,len(personality_array[0])):
            key =personality_array[0][i]
            value = personality_array[1][i]

            character.add_personality(value, key)

        family_dict = x.get_dataset('family', False, simple=True)

        for key, value in family_dict.items():
            #print('key is ' +str(key)+ ' and value is ' +str(value))
            character.set_attribute(str(key), str(value))
        
        tag_params = ['age', 'gender', 'relation']
        siblings_array = x.get_dataset('family', False, False, True, tag_params)
        #print(str(siblings_array))

        siblings_names = x.get_dataset('family', False, simple=True, simple_no_dict=True)
        #print(str(siblings_names))
        names = []
        for i in range(len(siblings_names[0])):
            key = siblings_names[0][i]
            value = siblings_names[1][i]

            if key=='sibling':
                names.append(value)
        sibling_num=0
        for array in siblings_array:
            if len(array)!=0:
                #print(sibling_num)
                character.add_sibling(names[sibling_num], array[1], array[0], array[2])
                    
                sibling_num = sibling_num + 1

        stats = x.get_dataset('attributes', dictionary = True, dict_tag_name='type')
        skills = x.get_dataset('skills',True, dict_tag_name='type')
        talents = x.get_dataset('talents', True, dict_tag_name='type')
        perks = x.get_dataset('perks', True, dict_tag_name='type')

        comp_params = ['type', 'frequency', 'importance', 'intensity']
        complications = x.get_dataset('complications', False, False, True, comp_params)

        for array in complications:
            character.add_complication(array[0], array[3], array[1], array[2])

        


        character.set_attribute('player',player)
        character.set_attribute('fname', fname)
        character.set_attribute('sname', sname)
        character.set_attribute('lname', lname)
        character.set_attribute('alias', alias)
        character.set_attribute('age', age)
        character.add_stat_collection(stats, 'stat')
        character.add_stat_collection(skills, 'skill')
        #print(str(talents))
        character.add_stat_collection(talents, 'talent')
        character.add_stat_collection(perks, 'perk')

        cyberwear = x.get_cyberwear_from_character()
        character.add_cyberwear_collection(copy.deepcopy(cyberwear))

        items = x.get_dataset('items', False, False, False, simple=True, simple_no_dict=True, simple_with_param=True, simple_param='type')
        character.add_item_collection(copy.deepcopy(items))

        lp = Lifepath(self)
        lp.convert_from_xml(filepath)

        character.set_attribute('lifepath', lp)
 def load_tables(self):
     x = XmlController()
     x.load_file('preferences/preferences.xml')
     tables = x.read_table_names()
     for name in tables:
         self.__tables[name] = self.load_table(name)
 def load_from_xml(self, filename, root_name):
     x = XmlController()
     x.load_file(filename)
     return x.get_dataset(root_name, True, dict_tag_name='type')
    def save_to_xml(self, root_name, childname, values_dict, filepath):
        x = XmlController()
        x.create_root(root_name)
        for key, value in values_dict.items():
            x.create_sub_element(childname, 'root')
            x.set_text(value, childname)
            x.set_value('type',key, childname)

        x.save_file(filepath)