Merge branch 'ammunition' related to #75
commit
8632fbcd35
|
@ -0,0 +1,43 @@
|
|||
ammunition:
|
||||
- amount: 10
|
||||
bulk: 0.1
|
||||
descr: These projectiles are the ammunition for bows. The shaft of an arrow is made
|
||||
of wood. It is stabilized in flight by fletching at one end and bears a metal
|
||||
head on the other.
|
||||
name: Arrows
|
||||
price_gp: 0.1
|
||||
source:
|
||||
- abbr: CRB
|
||||
page_start: 282
|
||||
page_stop: 282
|
||||
- amount: 10
|
||||
bulk: 0.1
|
||||
descr: These thin, light darts are typically made of hardwood and stabilized with
|
||||
fletching of down or fur. They are often hollow so they can be used to deliver
|
||||
poison.
|
||||
name: Blowgun Darts
|
||||
price_gp: 0.05
|
||||
source:
|
||||
- abbr: CRB
|
||||
page_start: 281
|
||||
page_stop: 281
|
||||
- amount: 10
|
||||
bulk: 0.1
|
||||
descr: Shorter than traditional arrows but similar in construction, bolts are the
|
||||
ammunition used by crossbows.
|
||||
name: Bolts
|
||||
price_gp: 0.1
|
||||
source:
|
||||
- abbr: CRB
|
||||
page_start: 281
|
||||
page_stop: 281
|
||||
- amount: 10
|
||||
bulk: 0.1
|
||||
descr: These are small metal balls, typically either iron or lead, designed to be
|
||||
used as ammunition in slings.
|
||||
name: Sling Bullets
|
||||
price_gp: 0.01
|
||||
source:
|
||||
- abbr: CRB
|
||||
page_start: 281
|
||||
page_stop: 281
|
|
@ -0,0 +1,50 @@
|
|||
import sqlite3
|
||||
import yaml
|
||||
|
||||
|
||||
def main():
|
||||
conn = sqlite3.connect('../../pf2.db')
|
||||
|
||||
conn.row_factory = sqlite3.Row
|
||||
c = conn.cursor()
|
||||
c.execute("select * from ammunition;")
|
||||
res = [dict(row) for row in c.fetchall()]
|
||||
|
||||
# for i in res:
|
||||
# print(i)
|
||||
|
||||
reslist = []
|
||||
for i in res:
|
||||
tmp = {
|
||||
"name":
|
||||
i['name'],
|
||||
"price_gp":
|
||||
i['price_gp'],
|
||||
"amount":
|
||||
i['amount'],
|
||||
"bulk":
|
||||
i['bulk'],
|
||||
"descr":
|
||||
i['descr'],
|
||||
"source": [
|
||||
{
|
||||
'abbr': 'CRB',
|
||||
'page_start': int(i['sources_pages']),
|
||||
'page_stop': int(i['sources_pages'])
|
||||
},
|
||||
]
|
||||
}
|
||||
reslist.append(tmp)
|
||||
|
||||
# print(reslist)
|
||||
|
||||
tmpd = {'ammunition': reslist}
|
||||
|
||||
# now dump to yaml
|
||||
final = yaml.safe_dump(tmpd, allow_unicode=True)
|
||||
with open('tmp-ammo.yaml', 'w') as f:
|
||||
f.write(final)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
|
@ -100,6 +100,88 @@ def main():
|
|||
data = yaml.full_load(yl)
|
||||
do_armor(data, conn)
|
||||
|
||||
# move on to ammo
|
||||
with open('ammunition.yaml') as yl:
|
||||
data = yaml.full_load(yl)
|
||||
do_ammo(data, conn)
|
||||
|
||||
def do_ammo(data, conn):
|
||||
table = """
|
||||
CREATE TABLE ammunition (
|
||||
ammunition_id INTEGER PRIMARY KEY,
|
||||
"name" TEXT NOT NULL UNIQUE,
|
||||
price_gp REAL,
|
||||
amount INTEGER,
|
||||
bulk REAL,
|
||||
descr TEXT
|
||||
);
|
||||
"""
|
||||
c = conn.cursor()
|
||||
c.execute(table)
|
||||
|
||||
table = """
|
||||
CREATE TABLE sourceentry_ammunition (
|
||||
id INTEGER PRIMARY KEY,
|
||||
sourceentry_id INTEGER NOT NULL,
|
||||
ammunition_id INTEGER NOT NULL,
|
||||
UNIQUE (sourceentry_id, ammunition_id), -- prevent duplicates
|
||||
FOREIGN KEY (sourceentry_id) REFERENCES sourceentry(sourceentry_id),
|
||||
FOREIGN KEY (ammunition_id) REFERENCES ammunition(ammunition_id)
|
||||
);
|
||||
"""
|
||||
c.execute(table)
|
||||
|
||||
# insert basics into ammunition table
|
||||
inp_data = []
|
||||
for i in data['ammunition']:
|
||||
# print(i)
|
||||
inp_data.append((i['amount'], i['bulk'], i['descr'], i['name'], i['price_gp']))
|
||||
|
||||
stmt = "INSERT INTO ammunition(amount, bulk, descr, name, price_gp) VALUES (?,?,?,?,?)"
|
||||
try:
|
||||
conn.executemany(stmt, inp_data)
|
||||
except sqlite3.Error as e:
|
||||
print("Error creating ammunition: {}".format(e))
|
||||
except:
|
||||
print("Error creating ammunition something other than sqlite3 error")
|
||||
else:
|
||||
conn.commit()
|
||||
|
||||
for i in data['ammunition']:
|
||||
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_ammunition(i['name'], srcentrydata, conn)
|
||||
|
||||
def link_sourceentry_ammunition(name, srcentrydata, conn):
|
||||
stmt = """
|
||||
INSERT INTO sourceentry_ammunition (sourceentry_id, ammunition_id)
|
||||
SELECT sourceentry_id, ammunition_id
|
||||
FROM sourceentry, ammunition
|
||||
WHERE sourceentry.source_id=(SELECT source_id FROM source WHERE abbr=?)
|
||||
AND sourceentry.page_start=?
|
||||
AND sourceentry.page_stop=?
|
||||
AND ammunition.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 ammunition: {}".format(e))
|
||||
else:
|
||||
conn.commit()
|
||||
|
||||
def do_armor(data, conn):
|
||||
# Create the 3 tables
|
||||
|
|
Loading…
Reference in New Issue