From f6fa342ac43035b8f30502561264122b2f85bfd1 Mon Sep 17 00:00:00 2001 From: Bryce Allen Date: Wed, 20 May 2015 11:05:32 -0500 Subject: [PATCH] new web index, refactor, data fixes --- bin/mhweaponbuild.py | 32 +----- bin/mkjsonapi.py | 15 ++- db/mh4u.db | Bin 2669568 -> 2669568 bytes db/set_weapon_costs.py | 44 ++++++++ db/weapons-delta.csv | 3 + mhapi/db.py | 8 +- mhapi/model.py | 56 ++++++++++ web/index.html | 105 ++++++++++--------- web/js/common.js | 109 ++++++++++++++++++++ web/{armor.html => outfitters.html} | 7 +- web/{index-static.html => recommends.html} | 42 ++------ web/{weapontree.html => weaponplanner.html} | 71 +++++++------ 12 files changed, 341 insertions(+), 151 deletions(-) create mode 100755 db/set_weapon_costs.py create mode 100644 db/weapons-delta.csv create mode 100644 web/js/common.js rename web/{armor.html => outfitters.html} (99%) rename web/{index-static.html => recommends.html} (64%) rename web/{weapontree.html => weaponplanner.html} (64%) diff --git a/bin/mhweaponbuild.py b/bin/mhweaponbuild.py index 99f5086..36dad2b 100755 --- a/bin/mhweaponbuild.py +++ b/bin/mhweaponbuild.py @@ -7,7 +7,7 @@ import json import _pathfix from mhapi.db import MHDB -from mhapi.model import ModelJSONEncoder +from mhapi.model import ModelJSONEncoder, get_costs def parse_args(argv): @@ -23,36 +23,6 @@ def parse_args(argv): return parser.parse_args(argv) -def get_costs(db, weapon): - """ - Get a list of alternative ways of making a weapon, as a list of dicts - containing item counts. The dicts also contain special keys _zenny - for the total zenny needed, and _path for a list of weapons that - make up the upgrade path. - """ - costs = [] - if weapon.parent_id: - parent_weapon = db.get_weapon(weapon.parent_id, True) - costs = get_costs(db, parent_weapon) - for cost in costs: - for item in weapon.upgrade_components: - if item.type == "Weapon": - continue - if item.name not in cost["components"]: - cost["components"][item.name] = 0 - cost["components"][item.name] += item.quantity - cost["zenny"] += weapon.upgrade_cost - cost["path"] += [weapon] - if weapon.creation_cost: - create_cost = dict(zenny=weapon.creation_cost, - path=[weapon], - components={}) - for item in weapon.create_components: - create_cost["components"][item.name] = item.quantity - costs = [create_cost] + costs - return costs - - if __name__ == '__main__': args = parse_args(None) diff --git a/bin/mkjsonapi.py b/bin/mkjsonapi.py index 8868b24..2856743 100755 --- a/bin/mkjsonapi.py +++ b/bin/mkjsonapi.py @@ -139,7 +139,7 @@ def skilltree_json(db, path): def weapon_json(db, path): - weapons = db.get_weapons() + weapons = db.get_weapons(get_components=True) mkdirs_p(path) write_list_file(path, weapons) @@ -150,6 +150,14 @@ def weapon_json(db, path): with open(weapon_path, "w") as f: w.json_dump(f) + tree_path = os.path.join(path, "%s_tree.json" % w.id) + costs = model.get_costs(db, w) + for cost in costs: + cost["path"] = [dict(name=w.name, id=w.id) + for w in cost["path"]] + with open(tree_path, "w") as f: + json.dump(costs, f, cls=model.ModelJSONEncoder, indent=2) + write_index_file(path, indexes) @@ -176,12 +184,15 @@ def main(): else: outpath = os.path.join(_pathfix.web_path, "jsonapi") - items_json(db, os.path.join(outpath, "item")) weapon_json(db, os.path.join(outpath, "weapon")) + sys.exit(0) + + items_json(db, os.path.join(outpath, "item")) monster_json(db, os.path.join(outpath, "monster")) armor_json(db, os.path.join(outpath, "armor")) skilltree_json(db, os.path.join(outpath, "skilltree")) decoration_json(db, os.path.join(outpath, "decoration")) + #quest_json(db, os.path.join(outpath, "quest")) diff --git a/db/mh4u.db b/db/mh4u.db index dd24b8bae184207adb5de29622fd26780bd83817..e9575fc72784c4ba5910180676a8cc385be63860 100644 GIT binary patch delta 254 zcmWN=yGjE=7zWUPX1uJ)Ceig4O*UC|Rs|bN%ZL(*wS7KoTOS}6pCVW&2&0`?*rx~s zf>>J!K8CR3wka&!17|RPR)gYgL-p>O2dZ}gLJ^uU!~`)(OcB#WK+F&&qD+KDg{TrW zVwRXAB4VCcAQp)wVwtEDE5s_%Aeux>w1_s55NpIbu`z;u$)diPd+D2PJ=A;ZO&!Xo z+kdDX1!u!nTXd}5Zf>m(AKgiAJO1>?erOfD_g|ZMpWEYc*#r_uN6fW}eE6OI>57Py fiiPu0-Q8qq;&!sMYSiV^RX0nEo4%WEy?*@zXuMN` delta 270 zcmW;AyGp}Q00!W55-&+FT5qxTmS|5+!No_Y1S>k}8~jB&6vRbwlsf!K5upW3CP75p z1xrAE0YSmh7pMv;bP|d^yEl#QS^M+;>f^F`S`G_C%RxDG HAKv}|PeWi| diff --git a/db/set_weapon_costs.py b/db/set_weapon_costs.py new file mode 100755 index 0000000..8d9d47f --- /dev/null +++ b/db/set_weapon_costs.py @@ -0,0 +1,44 @@ +#!/usr/bin/env python + +import os.path +import codecs +import csv + +import _pathfix + +from mhapi.db import MHDB + + +def set_upgrade_cost(db, item_id, upgrade_cost): + print "upgrade_cost", item_id, upgrade_cost + cur = db.cursor() + cur.execute("""UPDATE weapons SET + upgrade_cost=? WHERE _id=?""", + (upgrade_cost, item_id)) + + +def set_creation_cost(db, item_id, creation_cost): + print "creation_cost", item_id, creation_cost + cur = db.cursor() + cur.execute("""UPDATE weapons SET + creation_cost=? WHERE _id=?""", + (creation_cost, item_id)) + + +if __name__ == '__main__': + db = MHDB() + delta_file_path = os.path.join(_pathfix.db_path, "weapons-delta.csv") + + with open(delta_file_path) as f: + reader = csv.DictReader(f) + for row in reader: + item_id = row["id"] + creation_cost = row["creation_cost"] + upgrade_cost = row["upgrade_cost"] + if creation_cost: + set_creation_cost(db, item_id, creation_cost) + if upgrade_cost: + set_upgrade_cost(db, item_id, upgrade_cost) + + db.commit() + db.close() diff --git a/db/weapons-delta.csv b/db/weapons-delta.csv new file mode 100644 index 0000000..24fbf83 --- /dev/null +++ b/db/weapons-delta.csv @@ -0,0 +1,3 @@ +id,name,creation_cost,upgrade_cost +6302,"Defender's Lance",3225,2150 +6808,"Demonlord Cudgel",,100000 diff --git a/mhapi/db.py b/mhapi/db.py index 4f7e9a2..15fe228 100644 --- a/mhapi/db.py +++ b/mhapi/db.py @@ -284,11 +284,15 @@ class MHDB(object): WHERE monster_id=? """, (monster_id,), collection_cls=model.MonsterDamage) - def get_weapons(self): - return self._query_all("weapons", """ + def get_weapons(self, get_components=False): + results = self._query_all("weapons", """ SELECT * FROM weapons LEFT JOIN items ON weapons._id = items._id """, model_cls=model.Weapon) + if results and get_components: + for r in results: + self._add_components(r) + return results def _add_components(self, item_data): ccomps = self.get_item_components(item_data.id, "Create") diff --git a/mhapi/model.py b/mhapi/model.py index 502b2f7..3a686e3 100644 --- a/mhapi/model.py +++ b/mhapi/model.py @@ -507,3 +507,59 @@ def _break_find(part, parts, breaks): if matches: return matches[0] return None + + +def get_costs(db, weapon): + """ + Get a list of alternative ways of making a weapon, as a list of dicts + containing item counts. The dicts also contain special keys _zenny + for the total zenny needed, and _path for a list of weapons that + make up the upgrade path. + """ + costs = [] + if weapon.parent_id: + if not weapon.upgrade_cost: + # db has errors where upgrade cost is listed as create + # cost and components are listed under create. Assume + # parent_id is correct, and they are upgrade only. + if not weapon.upgrade_components and weapon.create_components: + weapon.upgrade_components = weapon.create_components + weapon.create_components = [] + weapon.upgrade_cost = weapon.creation_cost + weapon.creation_cost = 0 + try: + upgrade_cost = int(weapon.upgrade_cost) + except ValueError: + upgrade_cost = 0 + print "WARN: bad upgrade cost for '%s' (%s): '%s'" \ + % (weapon.name, weapon.id, weapon.upgrade_cost) + except UnicodeError: + upgrade_cost = 0 + cost_display = urllib.quote(weapon.upgrade_cost) + print "WARN: bad upgrade cost for '%s' (%s): '%s'" \ + % (weapon.name, weapon.id, cost_display) + parent_weapon = db.get_weapon(weapon.parent_id, True) + costs = get_costs(db, parent_weapon) + for cost in costs: + cost["zenny"] += upgrade_cost + cost["path"] += [weapon] + for item in weapon.upgrade_components: + if item.type == "Weapon": + continue + if item.name not in cost["components"]: + cost["components"][item.name] = 0 + cost["components"][item.name] += item.quantity + if weapon.create_components: + try: + zenny = int(weapon.creation_cost) + except ValueError: + print "WARN: bad creation cost for '%s': '%s'" \ + % (weapon.name, weapon.creation_cost) + zenny = weapon.upgrade_cost or 0 + create_cost = dict(zenny=zenny, + path=[weapon], + components={}) + for item in weapon.create_components: + create_cost["components"][item.name] = item.quantity + costs = [create_cost] + costs + return costs diff --git a/web/index.html b/web/index.html index 5b83879..a220ea2 100644 --- a/web/index.html +++ b/web/index.html @@ -1,65 +1,74 @@ - Monster Hunter Item Search + Poogie's Toolbox - + - function setup_autocomplete() { - $.getJSON("/mhapi/item_name_list", - function(data) { - $("#item").autocomplete({ source: data }); - }); - } - - function update_search() { - var item_name = $.trim($("#item").val()); - - $.get("/mhapi/rewards", - { "item_name": item_name }, - function(data) { - $("#output").text(data); - }); + -
-
- - - - Understanding Results - (source) -
-
- -
+
+
+ Poogie Recommends + + + +

+
+
+
+
+ Poogie's Weapon Planner + + + + + + + + + +
+
+
+
+
+ Poogie Outfitters + Armor set planner +
diff --git a/web/js/common.js b/web/js/common.js new file mode 100644 index 0000000..684b2c7 --- /dev/null +++ b/web/js/common.js @@ -0,0 +1,109 @@ +WEAPON_NAME_IDX = {}; +WEAPON_TYPE_IDX = {}; + +(function($) { + $.QueryString = (function(a) { + if (a == "") return {}; + var b = {}; + for (var i = 0; i < a.length; ++i) + { + var p=a[i].split('='); + if (p.length != 2) continue; + b[p[0]] = decodeURIComponent(p[1].replace(/\+/g, " ")); + } + return b; + })(window.location.search.substr(1).split('&')) +})(jQuery); + + +function encode_utf8(s) { + return unescape(encodeURIComponent(s)); +} + + +function get_base_path() { + var path = document.location.pathname; + return path.substring(0, path.lastIndexOf('/')); +} + + +function normalize_name(s) { + var chars = s.split(""); + var cap_next = true; + var i; + for (i=0; i + +