forked from omicron/d2warehouse
make d2warehouse.db ready for the flask webui
- Add a function to automatically initialize the db with the schema if it isn't yet initialized - Remove the DROP TABLE queries from the schema in case the above^ goes wrong somehow - Make Item.write_to_db and Item.load_from_db take a db argument so that multiple threads can be supported - Add a few properties to d2warehouse.Item to help display items
This commit is contained in:
@@ -32,6 +32,13 @@ def close_db() -> None:
|
|||||||
_db = None
|
_db = None
|
||||||
|
|
||||||
|
|
||||||
|
def init_db() -> None:
|
||||||
|
db = get_db()
|
||||||
|
count = db.execute("SELECT count(*) FROM sqlite_master").fetchone()[0]
|
||||||
|
if count == 0:
|
||||||
|
create_db()
|
||||||
|
|
||||||
|
|
||||||
def create_db() -> None:
|
def create_db() -> None:
|
||||||
db = get_db()
|
db = get_db()
|
||||||
with open(_schema_path, encoding="utf-8") as f:
|
with open(_schema_path, encoding="utf-8") as f:
|
||||||
|
|||||||
@@ -122,6 +122,62 @@ class Item:
|
|||||||
quantity: int | None = None
|
quantity: int | None = None
|
||||||
stats: list[Stat] | None = None
|
stats: list[Stat] | None = None
|
||||||
|
|
||||||
|
@property
|
||||||
|
def basename(self) -> str:
|
||||||
|
return lookup_basetype(self.code)["name"]
|
||||||
|
|
||||||
|
@property
|
||||||
|
def name(self) -> str:
|
||||||
|
match self.quality:
|
||||||
|
case Quality.LOW:
|
||||||
|
return f"{self.low_quality} {self.basename}"
|
||||||
|
case Quality.NORMAL | None:
|
||||||
|
return self.basename
|
||||||
|
case Quality.HIGH:
|
||||||
|
return f"Superior {self.basename}"
|
||||||
|
case Quality.MAGIC:
|
||||||
|
return f"<prefix> {self.basename} <suffix>"
|
||||||
|
case Quality.SET:
|
||||||
|
assert self.set_id is not None
|
||||||
|
return lookup_set_item(self.set_id)["name"]
|
||||||
|
case Quality.RARE:
|
||||||
|
# FIXME
|
||||||
|
return "<rare> <name>"
|
||||||
|
case Quality.UNIQUE:
|
||||||
|
assert self.unique_id is not None
|
||||||
|
return lookup_unique(self.unique_id)["name"]
|
||||||
|
case Quality.CRAFTED:
|
||||||
|
# FIXME
|
||||||
|
return "<crafted> <name>"
|
||||||
|
case _:
|
||||||
|
# TODO: In 3.11 replace this with assert_never
|
||||||
|
assert False, "Should be unreachable"
|
||||||
|
|
||||||
|
@property
|
||||||
|
def color(self) -> str:
|
||||||
|
if self.is_runeword:
|
||||||
|
return "runeword"
|
||||||
|
match self.quality:
|
||||||
|
case Quality.LOW:
|
||||||
|
return "low"
|
||||||
|
case Quality.NORMAL | None:
|
||||||
|
return "normal"
|
||||||
|
case Quality.HIGH:
|
||||||
|
return "normal"
|
||||||
|
case Quality.MAGIC:
|
||||||
|
return "magic"
|
||||||
|
case Quality.SET:
|
||||||
|
return "set"
|
||||||
|
case Quality.RARE:
|
||||||
|
return "rare"
|
||||||
|
case Quality.UNIQUE:
|
||||||
|
return "unique"
|
||||||
|
case Quality.CRAFTED:
|
||||||
|
return "crafted"
|
||||||
|
case _:
|
||||||
|
# TODO: In 3.11 replace this with assert_never
|
||||||
|
assert False, "Should be unreachable"
|
||||||
|
|
||||||
def print(self, indent=5, with_raw=False):
|
def print(self, indent=5, with_raw=False):
|
||||||
properties = []
|
properties = []
|
||||||
base_name = lookup_basetype(self.code)["name"]
|
base_name = lookup_basetype(self.code)["name"]
|
||||||
@@ -235,7 +291,9 @@ class Item:
|
|||||||
reqs["lvl"] = max(reqs["lvl"], socket_reqs["lvl"])
|
reqs["lvl"] = max(reqs["lvl"], socket_reqs["lvl"])
|
||||||
return reqs
|
return reqs
|
||||||
|
|
||||||
def write_to_db(self, socketed_into=None, commit=True) -> int:
|
def write_to_db(self, socketed_into=None, commit=True, db=None) -> int:
|
||||||
|
if db is None:
|
||||||
|
db = get_db()
|
||||||
name = lookup_basetype(self.code)["name"]
|
name = lookup_basetype(self.code)["name"]
|
||||||
# FIXME: handle magic & rare names
|
# FIXME: handle magic & rare names
|
||||||
if self.is_runeword:
|
if self.is_runeword:
|
||||||
@@ -250,7 +308,6 @@ class Item:
|
|||||||
)
|
)
|
||||||
|
|
||||||
req = self.requirements()
|
req = self.requirements()
|
||||||
db = get_db()
|
|
||||||
cur = db.cursor()
|
cur = db.cursor()
|
||||||
cur.execute(
|
cur.execute(
|
||||||
"""INSERT INTO item (itembase_name, socketed_into, raw_data, raw_version,
|
"""INSERT INTO item (itembase_name, socketed_into, raw_data, raw_version,
|
||||||
@@ -343,8 +400,9 @@ class Item:
|
|||||||
|
|
||||||
return item_id
|
return item_id
|
||||||
|
|
||||||
def load_from_db(id: int) -> "Item":
|
def load_from_db(id: int, db=None) -> "Item":
|
||||||
db = get_db()
|
if db is None:
|
||||||
|
db = get_db()
|
||||||
row = db.execute(
|
row = db.execute(
|
||||||
"""SELECT raw_data, raw_version, is_identified, is_socketed,
|
"""SELECT raw_data, raw_version, is_identified, is_socketed,
|
||||||
is_beginner, is_simple, is_ethereal, is_personalized, is_runeword, code,
|
is_beginner, is_simple, is_ethereal, is_personalized, is_runeword, code,
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
DROP TABLE IF EXISTS item_stat;
|
--DROP TABLE IF EXISTS item_stat;
|
||||||
DROP TABLE IF EXISTS item_affix;
|
--DROP TABLE IF EXISTS item_affix;
|
||||||
DROP TABLE IF EXISTS item_extra;
|
--DROP TABLE IF EXISTS item_extra;
|
||||||
DROP TABLE IF EXISTS item;
|
--DROP TABLE IF EXISTS item;
|
||||||
|
|
||||||
CREATE TABLE item (
|
CREATE TABLE item (
|
||||||
id INTEGER PRIMARY KEY,
|
id INTEGER PRIMARY KEY,
|
||||||
|
|||||||
Reference in New Issue
Block a user