From be5d3ca633788006057d1364e08c380c06305bc9 Mon Sep 17 00:00:00 2001 From: Bryce Allen Date: Thu, 2 Jul 2015 08:55:53 -0500 Subject: [PATCH] weaponplanner: add upgrade links - add 'children' field to weapon JSON, with list of dicts containing 'id' and 'name' keys for weapons that can be upgraded from current weapon. --- bin/mkjsonapi.py | 48 ++++++++++++++++++++++++----------- mhapi/db.py | 15 ++++++++++- web/templates/weaponstats.ejs | 9 +++++++ 3 files changed, 56 insertions(+), 16 deletions(-) diff --git a/bin/mkjsonapi.py b/bin/mkjsonapi.py index e25d03a..6deeb07 100755 --- a/bin/mkjsonapi.py +++ b/bin/mkjsonapi.py @@ -4,14 +4,27 @@ import os import json import sys import errno -from collections import defaultdict import urllib +import argparse import _pathfix from mhapi.db import MHDB from mhapi import model +ENTITIES = "item weapon monster armor skilltree skill decoration".split() + +def parse_args(argv=None): + parser = argparse.ArgumentParser(description= + "Create static JSON files that mimic a REST API for monster hunter data" + ) + parser.add_argument("-o", "--outpath", + help="output base directory, defaults to web/jsonapi/" + " in project root") + parser.add_argument("entities", nargs="*", + help=", ".join(ENTITIES)) + return parser.parse_args(argv) + def mkdirs_p(path): try: @@ -162,8 +175,11 @@ def weapon_json(db, path): for w in weapons: weapon_path = file_path(path, w) w.update_indexes(indexes) + data = w.as_data() + child_weapons = db.get_weapons_by_parent(w.id) + data["children"] = [dict(id=c.id, name=c.name) for c in child_weapons] with open(weapon_path, "w") as f: - w.json_dump(f) + json.dump(data, f, cls=model.ModelJSONEncoder, indent=2) tree_path = os.path.join(path, "%s_tree.json" % w.id) costs = model.get_costs(db, w) @@ -176,7 +192,7 @@ def weapon_json(db, path): write_index_file(path, indexes) -def items_json(db, path): +def item_json(db, path): items = db.get_items() mkdirs_p(path) write_list_file(path, items) @@ -194,20 +210,22 @@ def items_json(db, path): def main(): db = MHDB(_pathfix.db_path) - if len(sys.argv) > 1: - outpath = sys.argv[1] - else: - outpath = os.path.join(_pathfix.web_path, "jsonapi") + args = parse_args() + + if not args.outpath: + args.outpath = os.path.join(_pathfix.web_path, "jsonapi") - weapon_json(db, os.path.join(outpath, "weapon")) - 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")) - skill_json(db, os.path.join(outpath, "skill")) - decoration_json(db, os.path.join(outpath, "decoration")) + if args.entities: + for entity in args.entities: + if entity not in ENTITIES: + print "Unknown entity: %s" % entity + sys.exit(1) + else: + args.entities = ENTITIES - #quest_json(db, os.path.join(outpath, "quest")) + for entity in args.entities: + fn = globals()["%s_json" % entity] + fn(db, os.path.join(args.outpath, entity)) if __name__ == '__main__': diff --git a/mhapi/db.py b/mhapi/db.py index 99f5510..c0134fa 100644 --- a/mhapi/db.py +++ b/mhapi/db.py @@ -301,7 +301,8 @@ class MHDB(object): def get_weapon(self, weapon_id, get_components=False): weapon = self._query_one("weapon", """ - SELECT * FROM weapons + SELECT items._id, items.name, items.buy, weapons.* + FROM weapons LEFT JOIN items ON weapons._id = items._id WHERE weapons._id=? """, (weapon_id,), model_cls=model.Weapon) @@ -320,6 +321,18 @@ class MHDB(object): self._add_components(weapon) return weapon + def get_weapons_by_parent(self, parent_id, get_components=False): + weapons = self._query_all("weapon_by_parent", """ + SELECT items._id, items.name, items.buy, weapons.* + FROM weapons + LEFT JOIN items ON weapons._id = items._id + WHERE weapons.parent_id=? + """, (parent_id,), model_cls=model.Weapon) + if get_components: + for weapon in weapons: + self._add_components(weapon) + return weapons + def get_armors(self): return self._query_all("armors", """ SELECT items._id, items.name, items.buy, armor.* diff --git a/web/templates/weaponstats.ejs b/web/templates/weaponstats.ejs index cbaf4e6..e19b1e6 100644 --- a/web/templates/weaponstats.ejs +++ b/web/templates/weaponstats.ejs @@ -33,4 +33,13 @@ +<% if (children.length) { %> + -> + <% for(var i=0; i + <%= children[i]["name"] %> + <% if (i != children.length - 1) { %>|<% } %> + <% } %> + +<% } %>