Files
d2warehouse/contrib/items.py

93 lines
2.8 KiB
Python

import json
import csv
import os
import sys
path = sys.argv[1] if len(sys.argv) >= 2 else "."
items = {}
item_patches = {
"tbk": {"class": "tome"},
"ibk": {"class": "tome"},
}
# build code -> names map
with open(os.path.join(path, "item-names.json"), encoding="utf-8-sig") as f:
names = json.load(f)
names = []
for entry in names:
code = entry["Key"]
if len(code) != 3 or not code.islower():
continue
name = entry["enUS"]
names[code] = {"name": name}
# Extract items
with open(os.path.join(path, "armor.txt"), newline="") as f:
reader = csv.DictReader(f, delimiter="\t")
for row in reader:
if row["name"] == "Expansion":
continue
code = row["code"]
assert code not in items
items[code] = {
"name": names[code] if code in names else row["name"],
"class": "armor",
"type": row["type"],
"stackable": row["stackable"] == "1",
"width": int(row["invwidth"]),
"height": int(row["invheight"]),
"req_str": int(row["reqstr"]),
"req_dex": int(row["reqdex"]),
"req_lvl": int(row["levelreq"]),
}
with open(os.path.join(path, "weapons.txt"), newline="") as f:
reader = csv.DictReader(f, delimiter="\t")
for row in reader:
if row["name"] == "Expansion":
continue
code = row["code"]
if code in items:
print("Already in", code)
assert code not in items
items[code] = {
"name": names[code] if code in names else row["name"],
"class": "weapon",
"type": row["type"],
"stackable": row["stackable"] == "1",
"width": int(row["invwidth"]),
"height": int(row["invheight"]),
"req_str": 0 if len(row["reqstr"]) == 0 else int(row["reqstr"]),
"req_dex": 0 if len(row["reqdex"]) == 0 else int(row["reqdex"]),
"req_lvl": int(row["levelreq"]),
}
with open(os.path.join(path, "misc.txt"), newline="") as f:
reader = csv.DictReader(f, delimiter="\t")
for row in reader:
if row["name"] == "Expansion":
continue
code = row["code"]
assert code not in items
items[code] = {
"name": names[code] if code in names else row["name"],
"class": "misc",
"type": row["type"],
"stackable": row["stackable"] == "1",
"width": int(row["invwidth"]),
"height": int(row["invheight"]),
"req_str": 0,
"req_dex": 0,
"req_lvl": int(row["levelreq"]),
}
for code, patch in item_patches.items():
items[code].update(patch)
with open("items.json", "w", newline="\n") as f:
json.dump(items, f, indent=4)
f.write("\n")