add other item types
This commit is contained in:
25
mhapi/db.py
25
mhapi/db.py
@@ -69,11 +69,13 @@ class MHDB(object):
|
||||
def close(self):
|
||||
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", """
|
||||
SELECT _id, name FROM items
|
||||
WHERE type IN ('Bone', 'Flesh', 'Sac/Fluid')
|
||||
""")
|
||||
WHERE type IN (%s)
|
||||
""" % placeholders, *item_types)
|
||||
return v
|
||||
|
||||
def get_item(self, item_id):
|
||||
@@ -97,18 +99,27 @@ class MHDB(object):
|
||||
""", item_id)
|
||||
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
|
||||
list of matching items.
|
||||
|
||||
Not memoized.
|
||||
"""
|
||||
cursor = self.conn.execute("""
|
||||
query = """
|
||||
SELECT * FROM items
|
||||
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()
|
||||
|
||||
def get_monster_by_name(self, name):
|
||||
|
||||
@@ -19,6 +19,9 @@ STRAT_SHINY = "shiny"
|
||||
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):
|
||||
if min_v == max_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
|
||||
continue
|
||||
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:
|
||||
print(" ", row["name"], file=err_out)
|
||||
return None
|
||||
|
||||
@@ -98,7 +98,7 @@ class App(object):
|
||||
|
||||
resp.content_type = "application/json"
|
||||
resp.body_file.write("[")
|
||||
items = self.db.get_item_names()
|
||||
items = self.db.get_item_names(rewards.ITEM_TYPES)
|
||||
first = True
|
||||
for item in items:
|
||||
if first:
|
||||
|
||||
Reference in New Issue
Block a user