Merge branch 'gear'; fix #76; fix #77

bradl/monsters-adult-gold-dragon
James Miller 2020-02-24 22:27:20 -06:00
commit 7271ce5ac0
3 changed files with 1592 additions and 0 deletions

View File

@ -0,0 +1,52 @@
import sqlite3
import yaml
def main():
conn = sqlite3.connect('../../pf2.db')
conn.row_factory = sqlite3.Row
c = conn.cursor()
c.execute("select * from gear;")
res = [dict(row) for row in c.fetchall()]
for i in res:
if i['hands'] == None or i['hands'] == '':
i['hands'] = 0
reslist = []
for i in res:
if i['descr'] == '':
i['descr'] = None
tmp = {
"name": i['name'],
'level': int(i['level']),
"price_gp": i['price'],
"bulk": i['bulk'],
"descr": i['descr'],
'hands': int(i['hands']),
"source": [
{
'abbr': 'CRB',
'page_start': int(i['sources_pages']),
'page_stop': int(i['sources_pages'])
},
],
"traits": None
}
reslist.append(tmp)
# print(reslist)
tmpd = {'gear': reslist}
# now dump to yaml
final = yaml.safe_dump(tmpd, allow_unicode=True)
with open('tmp-gear.yaml', 'w') as f:
f.write(final)
# NOTE the 4 traits were added by hand manually to the final tmp-gear.yaml and not via this script
if __name__ == '__main__':
main()

1419
data/yaml/gear.yaml 100644

File diff suppressed because it is too large Load Diff

View File

@ -105,6 +105,127 @@ def main():
data = yaml.full_load(yl)
do_ammo(data, conn)
# move on to ammo
with open('gear.yaml') as yl:
data = yaml.full_load(yl)
do_gear(data, conn)
def do_gear(data, conn):
table = """
CREATE TABLE gear(
"gear_id" INTEGER PRIMARY KEY,
"name" TEXT NOT NULL UNIQUE,
"level" INTEGER,
"price_gp" REAL,
"bulk" REAL,
"hands" INTEGER,
"descr" TEXT
);
"""
c = conn.cursor()
c.execute(table)
table = """
CREATE TABLE gear_traits(
id INTEGER PRIMARY KEY,
gear_id INTEGER NOT NULL,
trait_id INTEGER NOT NULL,
FOREIGN KEY (gear_id) REFERENCES gear(gear_id),
FOREIGN KEY (trait_id) REFERENCES trait(trait_id)
);
"""
c.execute(table)
table = """
CREATE TABLE sourceentry_gear (
id INTEGER PRIMARY KEY,
sourceentry_id INTEGER NOT NULL,
gear_id INTEGER NOT NULL,
UNIQUE (sourceentry_id, gear_id), -- prevent duplicates
FOREIGN KEY (sourceentry_id) REFERENCES sourceentry(sourceentry_id),
FOREIGN KEY (gear_id) REFERENCES gear(gear_id)
);
"""
c.execute(table)
# insert basics into gear table
inp_data = []
for i in data['gear']:
# print(i)
inp_data.append((i['bulk'], i['descr'], i['hands'], i['level'], i['name'], i['price_gp']))
stmt = "INSERT INTO gear(bulk, descr, hands, level, name, price_gp) VALUES (?,?,?,?,?,?)"
try:
conn.executemany(stmt, inp_data)
except sqlite3.Error as e:
print("Error creating gear: {}".format(e))
except:
print("Error creating gear something other than sqlite3 error")
else:
conn.commit()
# link the traits to the gear_traits table
traitslist = []
for i in data['gear']:
if i['traits'] == None:
continue
else:
for j in i['traits']:
traitslist.append((i['name'], j))
# print(traitslist)
stmt = """
INSERT INTO gear_traits(gear_id, trait_id)
VALUES (
(SELECT gear_id FROM gear WHERE name=?),
(SELECT trait_id FROM trait WHERE short_name=?)
);
"""
try:
conn.executemany(stmt, traitslist)
except sqlite3.Error as e:
print("Error creating gear_traits data: {}".format(e))
except:
print("Error creating gear_traits data something other than sqlite3 error")
else:
conn.commit()
# do the sourceentry linking
for i in data['gear']:
srcentrydata = []
for j in i['source']:
abbr = j['abbr']
page_start = j['page_start']
# Not all YAML entries have page_stop data
if 'page_stop' in j:
page_stop = j['page_stop']
else:
page_stop = page_start
srcentrydata.append((abbr, page_start, page_stop))
util_insert_into_sourceentry(srcentrydata, conn)
link_sourceentry_gear(i['name'], srcentrydata, conn)
def link_sourceentry_gear(name, srcentrydata, conn):
stmt = """
INSERT INTO sourceentry_gear (sourceentry_id, gear_id)
SELECT sourceentry_id, gear_id
FROM sourceentry, gear
WHERE sourceentry.source_id=(SELECT source_id FROM source WHERE abbr=?)
AND sourceentry.page_start=?
AND sourceentry.page_stop=?
AND gear.name=?;
"""
# print(srcentrydata)
for i in srcentrydata:
# print("i is:{}".format(i))
d = (i[0], i[1], i[2], name)
# print(d)
try:
conn.execute(stmt, d)
except Exception as e:
print("Error linking sourceentry to gear: {}".format(e))
else:
conn.commit()
def do_ammo(data, conn):
table = """
CREATE TABLE ammunition (