add other item types
This commit is contained in:
@@ -33,7 +33,7 @@ if __name__ == '__main__':
|
|||||||
db_path = os.path.join(db_path, "..", "db", "mh4u.db")
|
db_path = os.path.join(db_path, "..", "db", "mh4u.db")
|
||||||
db = MHDB(db_path)
|
db = MHDB(db_path)
|
||||||
|
|
||||||
items = db.get_item_names()
|
items = db.get_item_names(rewards.ITEM_TYPES)
|
||||||
|
|
||||||
# write all names json to /items.json
|
# write all names json to /items.json
|
||||||
items_file = os.path.join(outdir, "items.json")
|
items_file = os.path.join(outdir, "items.json")
|
||||||
|
|||||||
25
mhapi/db.py
25
mhapi/db.py
@@ -69,11 +69,13 @@ class MHDB(object):
|
|||||||
def close(self):
|
def close(self):
|
||||||
return self.conn.close()
|
return self.conn.close()
|
||||||
|
|
||||||
def get_item_names(self):
|
def get_item_names(self, item_types):
|
||||||
|
item_types.sort()
|
||||||
|
placeholders = ", ".join(["?"] * len(item_types))
|
||||||
v = self._get_memoized("item_names", """
|
v = self._get_memoized("item_names", """
|
||||||
SELECT _id, name FROM items
|
SELECT _id, name FROM items
|
||||||
WHERE type IN ('Bone', 'Flesh', 'Sac/Fluid')
|
WHERE type IN (%s)
|
||||||
""")
|
""" % placeholders, *item_types)
|
||||||
return v
|
return v
|
||||||
|
|
||||||
def get_item(self, item_id):
|
def get_item(self, item_id):
|
||||||
@@ -97,18 +99,27 @@ class MHDB(object):
|
|||||||
""", item_id)
|
""", item_id)
|
||||||
return v[0] if v else None
|
return v[0] if v else None
|
||||||
|
|
||||||
def search_item_name(self, term, item_type):
|
def search_item_name(self, term, item_type=None):
|
||||||
"""
|
"""
|
||||||
Search for items containing @term somewhere in the name. Returns
|
Search for items containing @term somewhere in the name. Returns
|
||||||
list of matching items.
|
list of matching items.
|
||||||
|
|
||||||
Not memoized.
|
Not memoized.
|
||||||
"""
|
"""
|
||||||
cursor = self.conn.execute("""
|
query = """
|
||||||
SELECT * FROM items
|
SELECT * FROM items
|
||||||
WHERE name LIKE ?
|
WHERE name LIKE ?
|
||||||
AND type = ?
|
"""
|
||||||
""", ("%%%s%%" % term, item_type))
|
args = ["%%%s%%" % term]
|
||||||
|
if item_type is not None:
|
||||||
|
if isinstance(item_type, (list, tuple)):
|
||||||
|
query += "AND type IN (%s)" % (",".join(["?"] * len(item_type)))
|
||||||
|
args += item_type
|
||||||
|
else:
|
||||||
|
query += "AND type = ?"
|
||||||
|
args += [item_type]
|
||||||
|
|
||||||
|
cursor = self.conn.execute(query, args)
|
||||||
return cursor.fetchall()
|
return cursor.fetchall()
|
||||||
|
|
||||||
def get_monster_by_name(self, name):
|
def get_monster_by_name(self, name):
|
||||||
|
|||||||
@@ -19,6 +19,9 @@ STRAT_SHINY = "shiny"
|
|||||||
STRAT_CAP_OR_KILL = "cap/kill"
|
STRAT_CAP_OR_KILL = "cap/kill"
|
||||||
|
|
||||||
|
|
||||||
|
ITEM_TYPES = "Bone Bug Fish Flesh Meat Ore Plant Sac/Fluid".split()
|
||||||
|
|
||||||
|
|
||||||
def _format_range(min_v, max_v):
|
def _format_range(min_v, max_v):
|
||||||
if min_v == max_v:
|
if min_v == max_v:
|
||||||
return "%5.2f" % min_v
|
return "%5.2f" % min_v
|
||||||
@@ -37,7 +40,7 @@ def find_item(db, item_name, err_out):
|
|||||||
# single char terms aren't very useful, too many results
|
# single char terms aren't very useful, too many results
|
||||||
continue
|
continue
|
||||||
print("= Matching term '%s'" % term, file=err_out)
|
print("= Matching term '%s'" % term, file=err_out)
|
||||||
rows = db.search_item_name(term, "Flesh")
|
rows = db.search_item_name(term, ITEM_TYPES)
|
||||||
for row in rows:
|
for row in rows:
|
||||||
print(" ", row["name"], file=err_out)
|
print(" ", row["name"], file=err_out)
|
||||||
return None
|
return None
|
||||||
|
|||||||
@@ -98,7 +98,7 @@ class App(object):
|
|||||||
|
|
||||||
resp.content_type = "application/json"
|
resp.content_type = "application/json"
|
||||||
resp.body_file.write("[")
|
resp.body_file.write("[")
|
||||||
items = self.db.get_item_names()
|
items = self.db.get_item_names(rewards.ITEM_TYPES)
|
||||||
first = True
|
first = True
|
||||||
for item in items:
|
for item in items:
|
||||||
if first:
|
if first:
|
||||||
|
|||||||
Reference in New Issue
Block a user