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 dd24b8b..e9575fc 100644 Binary files a/db/mh4u.db and b/db/mh4u.db differ 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 @@
-