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.
This commit is contained in:
@@ -4,14 +4,27 @@ import os
|
|||||||
import json
|
import json
|
||||||
import sys
|
import sys
|
||||||
import errno
|
import errno
|
||||||
from collections import defaultdict
|
|
||||||
import urllib
|
import urllib
|
||||||
|
import argparse
|
||||||
|
|
||||||
import _pathfix
|
import _pathfix
|
||||||
|
|
||||||
from mhapi.db import MHDB
|
from mhapi.db import MHDB
|
||||||
from mhapi import model
|
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):
|
def mkdirs_p(path):
|
||||||
try:
|
try:
|
||||||
@@ -162,8 +175,11 @@ def weapon_json(db, path):
|
|||||||
for w in weapons:
|
for w in weapons:
|
||||||
weapon_path = file_path(path, w)
|
weapon_path = file_path(path, w)
|
||||||
w.update_indexes(indexes)
|
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:
|
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)
|
tree_path = os.path.join(path, "%s_tree.json" % w.id)
|
||||||
costs = model.get_costs(db, w)
|
costs = model.get_costs(db, w)
|
||||||
@@ -176,7 +192,7 @@ def weapon_json(db, path):
|
|||||||
write_index_file(path, indexes)
|
write_index_file(path, indexes)
|
||||||
|
|
||||||
|
|
||||||
def items_json(db, path):
|
def item_json(db, path):
|
||||||
items = db.get_items()
|
items = db.get_items()
|
||||||
mkdirs_p(path)
|
mkdirs_p(path)
|
||||||
write_list_file(path, items)
|
write_list_file(path, items)
|
||||||
@@ -194,20 +210,22 @@ def items_json(db, path):
|
|||||||
def main():
|
def main():
|
||||||
db = MHDB(_pathfix.db_path)
|
db = MHDB(_pathfix.db_path)
|
||||||
|
|
||||||
if len(sys.argv) > 1:
|
args = parse_args()
|
||||||
outpath = sys.argv[1]
|
|
||||||
|
if not args.outpath:
|
||||||
|
args.outpath = os.path.join(_pathfix.web_path, "jsonapi")
|
||||||
|
|
||||||
|
if args.entities:
|
||||||
|
for entity in args.entities:
|
||||||
|
if entity not in ENTITIES:
|
||||||
|
print "Unknown entity: %s" % entity
|
||||||
|
sys.exit(1)
|
||||||
else:
|
else:
|
||||||
outpath = os.path.join(_pathfix.web_path, "jsonapi")
|
args.entities = ENTITIES
|
||||||
|
|
||||||
weapon_json(db, os.path.join(outpath, "weapon"))
|
for entity in args.entities:
|
||||||
items_json(db, os.path.join(outpath, "item"))
|
fn = globals()["%s_json" % entity]
|
||||||
monster_json(db, os.path.join(outpath, "monster"))
|
fn(db, os.path.join(args.outpath, entity))
|
||||||
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"))
|
|
||||||
|
|
||||||
#quest_json(db, os.path.join(outpath, "quest"))
|
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
|
|||||||
15
mhapi/db.py
15
mhapi/db.py
@@ -301,7 +301,8 @@ class MHDB(object):
|
|||||||
|
|
||||||
def get_weapon(self, weapon_id, get_components=False):
|
def get_weapon(self, weapon_id, get_components=False):
|
||||||
weapon = self._query_one("weapon", """
|
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
|
LEFT JOIN items ON weapons._id = items._id
|
||||||
WHERE weapons._id=?
|
WHERE weapons._id=?
|
||||||
""", (weapon_id,), model_cls=model.Weapon)
|
""", (weapon_id,), model_cls=model.Weapon)
|
||||||
@@ -320,6 +321,18 @@ class MHDB(object):
|
|||||||
self._add_components(weapon)
|
self._add_components(weapon)
|
||||||
return 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):
|
def get_armors(self):
|
||||||
return self._query_all("armors", """
|
return self._query_all("armors", """
|
||||||
SELECT items._id, items.name, items.buy, armor.*
|
SELECT items._id, items.name, items.buy, armor.*
|
||||||
|
|||||||
@@ -33,4 +33,13 @@
|
|||||||
<span style="width:<%= sharpness_plus[6] %>px" class="purple"></span>
|
<span style="width:<%= sharpness_plus[6] %>px" class="purple"></span>
|
||||||
</div>
|
</div>
|
||||||
</td>
|
</td>
|
||||||
|
<% if (children.length) { %>
|
||||||
|
<tr><td colspan="5"> ->
|
||||||
|
<% for(var i=0; i<children.length; i++) { %>
|
||||||
|
<a href="weaponplanner.html?weapon=<%= encodeURIComponent(children[i]['name']) %>"
|
||||||
|
><%= children[i]["name"] %></a>
|
||||||
|
<% if (i != children.length - 1) { %>|<% } %>
|
||||||
|
<% } %>
|
||||||
|
</td></tr>
|
||||||
|
<% } %>
|
||||||
</table>
|
</table>
|
||||||
|
|||||||
Reference in New Issue
Block a user