File size: 8,863 Bytes
760ff9a
c8a4cd8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
760ff9a
c8a4cd8
 
 
 
 
 
 
760ff9a
c8a4cd8
760ff9a
 
 
 
 
 
 
 
 
 
c8a4cd8
 
760ff9a
 
 
 
 
 
 
 
 
 
c8a4cd8
 
760ff9a
c8a4cd8
 
 
760ff9a
c8a4cd8
 
760ff9a
 
 
 
 
 
 
 
c8a4cd8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
760ff9a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c8a4cd8
 
760ff9a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c8a4cd8
760ff9a
 
 
 
c8a4cd8
760ff9a
 
 
 
 
 
 
c8a4cd8
760ff9a
 
 
 
 
 
 
 
 
c8a4cd8
760ff9a
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270

# split text along : into key values.
def dict_from_text(data):
    lines = data.strip().split("\n")
    current_item = {}
    for line in lines:
        if ":" in line:
                key, value = line.split(":")
                key = key.strip()
                value = value.strip(", ")
                current_item[key] = value
    return current_item

def list_of_dict_from_text(data):
    
    lines = data.strip().split("\n")
    output_list = []
    # current_category = None
    current_item = {}

    for line in lines:
        line = line.strip()
        # print(f"line = {line}")
        if not line:
            continue
        if "name :" in line:
            if current_item: 
                output_list.append(current_item)
            current_item = {"name": line.split(":")[1].strip(", ")}
        
        elif ":" in line:
            key, value = line.split(":")
            key = key.strip()
            value = value.strip(", ")
            if key == "secrets" :
                value = list_from_text(value)
            current_item[key] = value
    
    if current_item:
        output_list.append(current_item)
    
    return output_list

def list_from_text(data): 
    # print(f"Incoming Text: {data}")
    value = [v.strip().strip("'").strip("\n") for v in data.split(",")]
    # print(f"Returned List : {value}")
    return value
        
#Function to process text from Key Value pairs into User Friendly text in a format that can be converted back to a dictionary
def format_qualities(qualities):
    # print(f"Formatting Start,{type(qualities)} ")
    formatted_text = ""
    if type(qualities) == list:
        # print("List")
        for item in qualities: 
            if type(item) == dict :  
                # print("List item Dictionary") 
                for key, value in item.items():
                    if type(value) == list:
                        formatted_text += f"{key} : "
                        for i in value:
                            formatted_text += f"{i} , "
                        formatted_text = formatted_text.rstrip(" ,")
                        formatted_text += "\n"
                        
                    else : 
                        formatted_text += f"{key} : {value}\n"
                formatted_text = formatted_text.rstrip(",")
                formatted_text += "\n"

            if type(item) == list:
                # print("List item List") 
                for i in item:
                    formatted_text += f"{i} , "
                formatted_text = formatted_text.rstrip(" ,")
            if type(item) == str:
                print(f"List : {item}") 
                formatted_text += f"{item} , "
            
                
        formatted_text = formatted_text.rstrip(",").rstrip(" ,")
        formatted_text += "\n "
        return formatted_text
    
    elif type(qualities) == dict: 
        for key, value in qualities.items():
            formatted_text += f"{key} : {value}\n"
            formatted_text = formatted_text.rstrip(",")
    return formatted_text

def parse_text_to_store_dict(store_name,
                            store_description,
                            store_reputation,
                            store_backstory,
                            store_sd_prompt,
                            store_location,
                            store_type,
                            store_size,
                            store_owner,
                            store_employees,
                            store_hours,
                            store_services,
                            store_specialties,
                            store_customers,
                            store_quests,
                            store_rumors,
                            store_security,
                            store_inventory):
    
    # Print variable, and check it's value and if length greater than 0.
    # Check for dict, str, list
       
    
    print(f"{store_inventory}")
    
    # Parse store description key : values with string values first
    store_dict = {}
    if store_name : 
        store_dict['store_name'] = store_name
    else: store_dict['store_name'] = ""

    if store_description : 
        store_dict['store_description'] = store_description
    else: store_dict['store_description'] = ""

    if store_reputation : 
        store_dict['store_reputation'] = store_reputation
    else: store_dict['store_reputation'] = ""

    if store_backstory : 
        store_dict['store_backstory'] = store_backstory
    else: store_dict['store_backstory'] = ""

    if store_type : 
        store_dict['store_type'] = store_type
    else: store_dict['store_type'] = ""

    if store_size : 
        store_dict['store_size'] = store_size
    else: store_dict['store_size'] = ""

    if store_hours : 
        store_dict['store_hours'] = store_hours
    else: store_dict['store_hours'] = ""

    if store_sd_prompt : 
        store_dict['store_sd_prompt'] = store_sd_prompt
    else: store_dict['store_sd_prompt'] = ""

    if store_location :
        store_dict['store_location'] = dict_from_text(store_location)
    else: store_dict['store_location'] = ''

    if store_owner :
        store_dict['store_owners'] = list_of_dict_from_text(store_owner)
    else: store_dict['store_owners'] = ''

    if store_employees :
        store_dict['store_employees'] = list_of_dict_from_text(store_employees)
    else: store_dict['store_employees'] = ''

    if store_quests :
        store_dict['store_quests'] = list_of_dict_from_text(store_quests)
    else: store_dict['store_quests'] = ''

    if store_customers :
        store_dict['store_customers'] = list_of_dict_from_text(store_customers)
    else: store_dict['store_customers'] = ''

    if store_rumors :
        store_dict['store_rumors'] = list_from_text(store_rumors)

    if store_services :
        store_dict['store_services'] = list_of_dict_from_text(store_services)
    else: store_dict['store_services'] = ''

    if store_specialties :
        store_dict['store_specialties'] = list_of_dict_from_text(store_specialties)
    else: store_dict['store_specialties'] = ''

    if store_security :
        store_dict['store_security'] = list_of_dict_from_text(store_security)
    else: store_dict['store_security'] = ''

    return store_dict

def format_inventory(inventory):
    formatted_text = ""
    print(f"Formatting Inventory ,{type(inventory)} ")
    # Iteration through item split_text keys in the inventory dictionary
    for item_split_text, item_list in inventory.items():
        
        formatted_text += f"{item_split_text} \n\n  "
        # Iterate through List of Dictionaries of item qualities
        for item in item_list:
            # print(item)
            # print(type(item))
            if type(item) == dict:
                 for key, value in item.items():
                    if type(value) == list:
                        formatted_text += f"{key} :"
                        for i in value :
                            formatted_text += f" '{i}', "
                        formatted_text = formatted_text.rstrip(", ")
                        formatted_text += "\n"
                        
                    else:
                        formatted_text += f"{key} : {value},\n"
            formatted_text += "\n"  
                
    return formatted_text

# Take in the text from the inventory textbox, and reformat into a dictionary object
def parse_text_to_inventory_dict(data):
    inventory_categories = [
    "core_inventory",
    "weapons",
    "armor",
    "potions",
    "scrolls",
    "magical_items",
    "mundane_items",
    "miscellaneous_items"
    ]
        
    lines = data.strip().split("\n")
    inventory = {}
    current_category = None
    current_item = {}

    for line in lines:
        line = line.strip()
        # print(f"line = {line}")
        if not line:
            continue
        
        if line in inventory_categories:
            # print(f"Current Category : {line}")
            current_category = f"{line}"
            inventory[f"{current_category}"] = []

        elif "name :" in line:
            if current_item: 
                inventory[f"{current_category}"].append(current_item)
            current_item = {"name": line.split(":")[1].strip(", ")}
            # print(current_item)
        
        elif ":" in line:
            key, value = line.split(":")
            key = key.strip()
            value = value.strip(", ")
            if key == "properties":
                value = [v.strip().strip("'") for v in value.split(",")]
            current_item[key] = value

        # print(f"Inventory Dictionary = {inventory}")
    
    if current_item:
        inventory[current_category].append(current_item)
    
    return inventory