diff options
Diffstat (limited to 'dropcalc/create_db.py')
-rwxr-xr-x | dropcalc/create_db.py | 69 |
1 files changed, 69 insertions, 0 deletions
diff --git a/dropcalc/create_db.py b/dropcalc/create_db.py new file mode 100755 index 0000000..be60fb6 --- /dev/null +++ b/dropcalc/create_db.py @@ -0,0 +1,69 @@ +#!/usr/bin/env python3 + +import csv +import mxl_types +from argparse import ArgumentParser +from pathlib import Path +from sqlalchemy import create_engine, text +from sqlalchemy.orm import Session + + +def generate_tcs(): + # TODO + # grab weapons, armor, misc? with spawnable=1 *and* who have itemtype with TreasureClass=1 (any itemtype? just direct?) + # qlvl 1-3 in weap3, 4-6 in weap6, 7-9 in weap9 ... 91-93 in weap93 + # armo3 ... armo99 + # vanilla has bow3 ... bow87 as well, unknown but probably based on the itemtype having Shoots and Quiver + # don't know if misc TCs are generated, neither median nor vanilla actually uses them if they are + pass + + +def create_db(folder): + engine = create_engine("sqlite+pysqlite:///:memory:", echo=False) + mxl_types.BaseDAO.metadata.create_all(engine) + + try1 = (Path(folder) / "Data" / "global" / "excel") + if try1.exists(): + data_folder = try1 + else: + data_folder = folder + + types = [ + mxl_types.ItemRatio, + mxl_types.ItemType, + mxl_types.Weapon, + mxl_types.Armor, + mxl_types.Misc, + mxl_types.UniqueItem, + mxl_types.SetItem, + mxl_types.TreasureClass, + ] + for mytype in types: + file = data_folder / mytype.filename + with file.open(encoding='windows-1252') as csvfile, Session(engine) as session: + reader = csv.DictReader(csvfile, dialect='excel-tab') + for csv_row in reader: + db_row = mytype(csv_row) + if not db_row.is_valid(): + continue + session.add(db_row) + session.commit() + count = session.scalar(text(f'select count(*) from {mytype.__tablename__}')) + print(f'Counted {count} {mytype.__tablename__}') + + +def handle_args(): + parser = ArgumentParser() + parser.add_argument('folder', help='Location of Median XL .txt files') + return parser.parse_args() + + +def main(): + args = handle_args() + create_db(args.folder) + + +if __name__ == '__main__': + import sys + + sys.exit(main()) |