This repository was archived by the owner on Jul 11, 2022. It is now read-only.
Repository navigation
Expand file tree
/
Copy pathbasic_weapons.py
More file actions
388 lines (339 loc) · 17.5 KB
/
Copy pathbasic_weapons.py
File metadata and controls
388 lines (339 loc) · 17.5 KB
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
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
import os
import sys
import shutil
import copy
import re
from bs4 import BeautifulSoup, Tag, NavigableString
from helpers.create_db import create_db
def list_sorter(entry):
section_id = entry["section_id"]
name = entry["name"]
return (section_id, name)
def write_lib(filepath, entry_list):
with open(filepath, mode='w', encoding='UTF-8', errors='strict', buffering=1) as file:
section_str = ''
entry_str = ''
name_lower = ''
file.write('<?xml version="1.0" encoding="ISO-8859-1"?>\n')
file.write('<root version="2.9">\n')
# Reference part
# These are the individual cards that appear when you click on a table entry
file.write('\t<reference static="true">\n')
file.write('\t\t<weapon>\n')
# Create individual item entries
for entry_dict in sorted(entry_list, key=list_sorter):
name_lower = re.sub('\W', '', entry_dict["name"].lower())
file.write(f'\t\t\t<{name_lower}>\n')
file.write(f'\t\t\t\t<name type="string">{entry_dict["name"]}</name>\n')
file.write(f'\t\t\t\t<type type="string">{entry_dict["type"]}</type>\n')
file.write(f'\t\t\t\t<prof type="string">{entry_dict["prof"]}</prof>\n')
file.write(f'\t\t\t\t<heft type="string">{entry_dict["heft"]}</heft>\n')
file.write(f'\t\t\t\t<profbonus type="number">{entry_dict["profbonus"]}</profbonus>\n')
file.write(f'\t\t\t\t<damage type="string">{entry_dict["damage"]}</damage>\n')
file.write(f'\t\t\t\t<range type="number">{entry_dict["range"]}</range>\n')
file.write(f'\t\t\t\t<cost type="number">{entry_dict["cost"]}</cost>\n')
file.write(f'\t\t\t\t<weight type="number">{entry_dict["weight"]}</weight>\n')
file.write(f'\t\t\t\t<group type="string">{entry_dict["group"]}</group>\n')
file.write(f'\t\t\t\t<properties type="string">{entry_dict["properties"]}</properties>\n')
file.write(f'\t\t\t\t<description type="formattedtext">{entry_dict["description"]}\n\t\t\t\t</description>\n')
file.write(f'\t\t\t</{name_lower}>\n')
# Close out the Reference part
file.write('\t\t</weapon>\n')
file.write('\t</reference>\n')
# Library part - not dynamic
# This controls the right-hand menu on the Modules screen
file.write('\t<library>\n')
file.write('\t\t<lib4ebasicweapons>\n')
file.write('\t\t\t<name type="string">4E Basic Weapons</name>\n')
file.write('\t\t\t<categoryname type="string">4E Core</categoryname>\n')
file.write('\t\t\t<entries>\n')
file.write('\t\t\t\t<a0001weapons>\n')
file.write('\t\t\t\t\t<librarylink type="windowreference">\n')
file.write('\t\t\t\t\t\t<class>reference_classweapontablelist</class>\n')
file.write('\t\t\t\t\t\t<recordname>weaponlists.core@4E Basic Weapons</recordname>\n')
file.write('\t\t\t\t\t</librarylink>\n')
file.write('\t\t\t\t\t<name type="string">Items - Weapons</name>\n')
file.write('\t\t\t\t</a0001weapons>\n')
file.write('\t\t\t</entries>\n')
file.write('\t\t</lib4ebasicweapons>\n')
file.write('\t</library>\n')
# counters for Item List
section_id = 0
item_id = 0
# Item List part
# This controls the table that appears when you click on a Library menu
file.write('\t<weaponlists>\n')
file.write('\t\t<core>\n')
file.write('\t\t\t<description type="string">Weapons Table</description>\n')
file.write('\t\t\t<groups>\n')
# Create individual item entries
for entry_dict in sorted(entry_list, key=list_sorter):
item_id += 1
entry_str = "00000"[0:len("00000")-len(str(item_id))] + str(item_id)
name_lower = re.sub('\W', '', entry_dict["name"].lower())
#Check for new section
if entry_dict["section_id"] != section_id:
section_id = entry_dict["section_id"]
if section_id != 1:
section_str = "000"[0:len("000")-len(str(section_id - 1))] + str(section_id - 1)
file.write('\t\t\t\t\t</weapons>\n')
file.write(f'\t\t\t\t</section{section_str}>\n')
section_str = "000"[0:len("000")-len(str(section_id))] + str(section_id)
file.write(f'\t\t\t\t<section{section_str}>\n')
file.write(f'\t\t\t\t\t<description type="string">{entry_dict["prof"]} {entry_dict["type"]} Weapons</description>\n')
file.write(f'\t\t\t\t\t<subdescription type="string">{entry_dict["heft"]}</subdescription>\n')
file.write('\t\t\t\t\t<weapons>\n')
file.write(f'\t\t\t\t\t\t<a{entry_str}{name_lower}>\n')
file.write('\t\t\t\t\t\t\t<link type="windowreference">\n')
file.write('\t\t\t\t\t\t\t\t<class>referenceweapon</class>\n')
file.write(f'\t\t\t\t\t\t\t\t<recordname>reference.weapon.{name_lower}@4E Basic Weapons</recordname>\n')
file.write('\t\t\t\t\t\t\t</link>\n')
file.write(f'\t\t\t\t\t\t\t<name type="string">{entry_dict["name"]}</name>\n')
file.write(f'\t\t\t\t\t\t\t<profbonus type="number">{entry_dict["profbonus"]}</profbonus>\n')
file.write(f'\t\t\t\t\t\t\t<range type="number">{entry_dict["range"]}</range>\n')
file.write(f'\t\t\t\t\t\t\t<cost type="string">{entry_dict["cost"]}</cost>\n')
file.write(f'\t\t\t\t\t\t\t<weight type="string">{entry_dict["weight"]}</weight>\n')
file.write(f'\t\t\t\t\t\t\t<group type="string">{entry_dict["group"]}</group>\n')
file.write(f'\t\t\t\t\t\t\t<properties type="string">{entry_dict["properties"]}</properties>\n')
file.write(f'\t\t\t\t\t\t\t<damage type="string">{entry_dict["damage"]}</damage>\n')
file.write(f'\t\t\t\t\t\t</a{entry_str}{name_lower}>\n')
# Close out the last section
file.write('\t\t\t\t\t</weapons>\n')
file.write(f'\t\t\t\t</section{section_str}>\n')
# Close out Item List part
file.write('\t\t\t</groups>\n')
file.write('\t\t</core>\n')
file.write('\t</weaponlists>\n')
file.write('</root>\n')
return str(item_id)
if __name__ == '__main__':
# Pull data from Portable Compendium
db = []
try:
db = create_db('ddiItem.sql', "','")
except:
print("Error reading data source.")
print(f"{len(db)} entries recovered")
# Convert data to FG format
print("converting to FG format...")
# Initialize all modules databases
export_list = []
if not db:
print("NO DATA FOUND IN SOURCES, MAKE SURE YOU HAVE COPIED YOUR 4E PORTABLE COMPENDIUM DATA TO SOURCES!")
input('Press enter to close.')
sys.exit(0)
# List of possible Weapon properties
properties_list = ['^Brutal 1', '^Brutal 2', '^Defensive', '^Heavy Thrown', '^High Crit', '^Light Thrown', '^Load Free', '^Load Minor', '^Load Move', '^Load Standard', '^Off-Hand', '^Reach[^i]', '^Small', '^Stout', '^Versatile']
for i, row in enumerate(db, start=1):
# Parse the HTML text
html = row['Txt']
html = html.replace('\\r\\n','\r\n').replace('\\','')
parsed_html = BeautifulSoup(html, features="html.parser")
# Retrieve the data with dedicated columns
name_str = row['Name'].replace('\\', '')
# Initialize the other tag data
category_str = ''
cost_str = ''
damage_str = ''
description_str = ''
group_str = ''
heft_str = ''
prof_str = ''
profbonus_str = ''
properties_str = ''
range_str = ''
section_id = 100
type_str = ''
weight_str = ''
# Type - Basic weapons and Superior implements
# brute force the Type/Heft
if type_lbl := parsed_html.find(string=re.compile('^(Simple|Military|Superior|Improvised.*handed.*weapon).*')):
type_lbl = re.sub(' *$', '', type_lbl)
if type_lbl == 'Simple one-handed melee weapon':
section_id = 1
prof_str = 'Simple'
type_str = 'Melee'
heft_str = 'One-Handed'
elif type_lbl == 'Simple two-handed melee weapon':
section_id = 2
prof_str = 'Simple'
type_str = 'Melee'
heft_str = 'Two-Handed'
elif type_lbl == 'Military one-handed melee weapon':
section_id = 3
prof_str = 'Military'
type_str = 'Melee'
heft_str = 'One-Handed'
elif type_lbl == 'Military two-handed melee weapon':
section_id = 4
prof_str = 'Military'
type_str = 'Melee'
heft_str = 'Two-Handed'
elif type_lbl == 'Superior one-handed melee weapon':
section_id = 5
prof_str = 'Superior'
type_str = 'Melee'
heft_str = 'One-Handed'
elif type_lbl == 'Superior two-handed melee weapon':
section_id = 6
prof_str = 'Superior'
type_str = 'Melee'
heft_str = 'Two-Handed'
# Garotte typo
elif type_lbl == 'Superior two-handed weapon':
section_id = 6
prof_str = 'Superior'
type_str = 'Melee'
heft_str = 'Two-Handed'
elif type_lbl == 'Superior double melee weapon':
section_id = 7
prof_str = 'Superior'
type_str = 'Melee'
heft_str = 'Double'
elif type_lbl == 'Improvised one-handed melee weapon':
section_id = 8
prof_str = 'Improvised'
type_str = 'Melee'
heft_str = 'One-Handed'
elif type_lbl == 'Improvised two-handed melee weapon':
section_id = 9
prof_str = 'Improvised'
type_str = 'Melee'
heft_str = 'Two-Handed'
elif type_lbl == 'Simple one-handed ranged weapon':
section_id = 10
prof_str = 'Simple'
type_str = 'Ranged'
heft_str = 'One-Handed'
elif type_lbl == 'Simple two-handed ranged weapon':
section_id = 11
prof_str = 'Simple'
type_str = 'Ranged'
heft_str = 'Two-Handed'
elif type_lbl == 'Military one-handed ranged weapon':
section_id = 12
prof_str = 'Military'
type_str = 'Ranged'
heft_str = 'One-Handed'
elif type_lbl == 'Military two-handed ranged weapon':
section_id = 13
prof_str = 'Military'
type_str = 'Ranged'
heft_str = 'Two-Handed'
elif type_lbl == 'Superior one-handed ranged weapon':
section_id = 14
prof_str = 'Superior'
type_str = 'Ranged'
heft_str = 'One-Handed'
elif type_lbl == 'Superior two-handed ranged weapon':
section_id = 15
prof_str = 'Superior'
type_str = 'Ranged'
heft_str = 'Two-Handed'
elif type_lbl == 'Improvised one-handed ranged weapon':
section_id = 16
prof_str = 'Improvised'
type_str = 'Ranged'
heft_str = 'One-Handed'
elif type_lbl == 'Improvised two-handed ranged weapon':
section_id = 17
prof_str = 'Improvised'
type_str = 'Ranged'
heft_str = 'Two-Handed'
else:
section_id = 100
type_str = type_lbl
# Records to be processed
if section_id < 99:
print(str(i) + ' ' + name_str)
# Cost
if cost_lbl := parsed_html.find(string=re.compile('^Cost:.*')):
cost_str = re.sub('[^\.\d]', '', cost_lbl.string.replace('5 sp', '0.5 gp'))
elif cost_lbl := parsed_html.find(string='Price'):
cost_str = re.sub('[^\.\d]', '', cost_lbl.parent.next_sibling.replace('5 sp', '0.5 gp'))
# Damage
if damage_lbl := parsed_html.find(string='Damage'):
damage_str = damage_lbl.parent.next_sibling.replace(': ', '')
# Description
# implements have a description tag
if description_lbl := parsed_html.find(string='Description'):
description_str = description_lbl.parent.next_sibling.replace(': ', '')
# otherwise look for a 'detail' div
elif detail_div := parsed_html.find('div', id='detail'):
for el_str in detail_div.stripped_strings:
# if we hit these we have gone too far
if re.search('^(Properties|Published|' + group_str + ')', el_str):
break
# skip over these ones
if re.search('^(:|Cost|Damage|Group|Range|Weight|Proficient|' + name_str + '|' + type_lbl + ')', el_str):
continue
# otherwise append non-empty values to the Description
if re.sub('\s', '', el_str) != '':
description_str += '\\n' if description_str != '' else ''
description_str += re.sub('^[:\s]*', '', el_str)
# Description (Published In)
if description_lbl := parsed_html.find('p', class_='publishedIn'):
description_str += re.sub('\s\s', ' ', description_lbl.text) if description_str == '' else '\\n' + re.sub('\s\s', ' ', description_lbl.text)
# clean up extraneous spaces
description_str = re.sub('\s\s', ' ', description_str.strip())
# Group
if group_lbl := parsed_html.find(string='Group'):
group_str = group_lbl.parent.next_sibling.next_sibling.next_sibling.replace(' (', '')
# Proficiency Bonus
if profbonus_lbl := parsed_html.find(string=re.compile('^Proficient:.*')):
profbonus_str = int(profbonus_lbl.string.replace('Proficient: ', ''))
# Properties
# might get false positives as it's just checking each property regex against the whole text
for prop in properties_list:
if properties_lbl := parsed_html.find(string=re.compile(prop + '.*')):
properties_str += '\\n' if properties_str != '' else ''
properties_str += prop.replace('[^i]', '').replace('\\', '').replace('^', '')
# Range
# strip the heading and long range bracket
if range_lbl := parsed_html.find(string=re.compile('^Range:.*')):
range_str = re.sub('(Range: |-|/.*)', '', range_lbl.string)
if range_str != '':
range_str = int(range_str)
# Weight
# might have a separate bold label
if weight_lbl := parsed_html.find(string='Weight'):
weight_str = weight_lbl.parent.next_sibling.replace(': ', '').replace('1/10', '0.1').replace('1/2', '0.5').replace(' lb.', '').replace(' lb', '')
# might be inside a single tag
elif weight_lbl := parsed_html.find(string=re.compile('^Weight:.*')):
weight_str = weight_lbl.string.replace('Weight: ', '').replace('1–', '').replace('6–', '').replace('—', '0').replace('1/10', '0.1').replace('1/2', '0.5').replace(' lb.', '').replace(' lb', '')
# Build the item dictionary
export_dict = {}
export_dict['cost'] = float(cost_str) if cost_str != '' else 0
export_dict['damage'] = damage_str
export_dict['description'] = re.sub('’', '\'', description_str)
export_dict['group'] = group_str
export_dict['heft'] = heft_str
export_dict['name'] = re.sub('’', '\'', name_str)
export_dict['prof'] = prof_str
export_dict['profbonus'] = profbonus_str
export_dict['properties'] = properties_str
export_dict['range'] = range_str
export_dict['section_id'] = section_id
export_dict['type'] = type_str
export_dict['weight'] = float(weight_str) if weight_str != '' else 0
# Append a copy of generated item dictionary
export_list.append(copy.deepcopy(export_dict))
print(str(len(db)) + " entries parsed.")
print(str(len(export_list)) + " export entries.")
# Write FG XML database files
write_id = write_lib('export/basic_weapons/data/client.xml', export_list)
print(write_id + " module entries written. Job done.")
try:
os.remove('export/mods/4E_Basic_Weapons.mod')
except FileNotFoundError:
print("Cleanup not needed.")
try:
shutil.make_archive('export/mods/4E_Basic_Weapons', 'zip', 'export/basic_weapons/data/')
os.rename('export/mods/4E_Basic_Weapons.zip', 'export/mods/4E_Basic_Weapons.mod')
print("\nDatabase added and module generated!")
print("You can find it in the 'export\\mods' folder\n")
except Exception as e:
print(f"Error creating zipped .mod file:\n{e}")
print("\nManually zip the contents of the 'export\\basic_weapon\\data' folder to create the mod.")
print("Rename the complete filename (including extension) to '4E_Basic_Weapons.mod'.\n")
input('Press enter to close.')