From 0ad973dc79b92ba5e0b3df313c0fe847da7b149b Mon Sep 17 00:00:00 2001 From: Bryce Allen Date: Thu, 2 Apr 2015 23:27:39 -0500 Subject: [PATCH] add other item types --- bin/genrewards.py | 2 +- mhapi/db.py | 25 ++++++++++++++++++------- mhapi/rewards.py | 5 ++++- mhapi/web/wsgi.py | 2 +- 4 files changed, 24 insertions(+), 10 deletions(-) diff --git a/bin/genrewards.py b/bin/genrewards.py index 0a7385f..0806321 100755 --- a/bin/genrewards.py +++ b/bin/genrewards.py @@ -33,7 +33,7 @@ if __name__ == '__main__': db_path = os.path.join(db_path, "..", "db", "mh4u.db") db = MHDB(db_path) - items = db.get_item_names() + items = db.get_item_names(rewards.ITEM_TYPES) # write all names json to /items.json items_file = os.path.join(outdir, "items.json") diff --git a/mhapi/db.py b/mhapi/db.py index 3bba446..354c153 100644 --- a/mhapi/db.py +++ b/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): diff --git a/mhapi/rewards.py b/mhapi/rewards.py index 517007a..c84ab63 100644 --- a/mhapi/rewards.py +++ b/mhapi/rewards.py @@ -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 diff --git a/mhapi/web/wsgi.py b/mhapi/web/wsgi.py index 0eed938..a8fd35f 100644 --- a/mhapi/web/wsgi.py +++ b/mhapi/web/wsgi.py @@ -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: