This repository was archived by the owner on Jan 16, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathroutes.py
111 lines (81 loc) · 3.62 KB
/
routes.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
from flask import render_template, abort, request, flash
from helpers import *
from cne import app
@app.route('/')
@app.route('/<int:item_id>-<item_slug>')
@app.route('/<int:game_version>')
@app.route('/<int:game_version>/<int:item_id>-<item_slug>')
def home(game_version=1, item_id=None, item_slug=None):
if game_version not in (1, 2):
game_version = 1
items = load_json(app.config['ITEMS_FILE'].format(game_version=game_version))
recipes = load_json(app.config['RECIPES_FILE'].format(game_version=game_version))
items = merge_recipe_items_in_items(items, recipes)
items = set_items_permalink(items, game_version=game_version)
if game_version == 1:
images = get_images(game_version=game_version)
items = merge_images_in_items(items, images)
permalink_item = get_item_by_id(items, item_id) if item_id is not None else None
return render_template(
'home.html',
items=items,
game_version=game_version,
permalink_item=permalink_item
)
@app.route('/recipes-editor')
def recipes_editor():
if not app.config['DEBUG']: # Can only edit crafting recipes locally
abort(404)
game_version = 1 # TODO
items = load_json(app.config['ITEMS_FILE'].format(game_version=game_version))
recipes = load_json(app.config['RECIPES_FILE'].format(game_version=game_version))
items = get_items_with_recipe(items) # Only get items with a crafting recipe
# Highlight items with crafting recipe but not in the Craft N' Escape recipes file
# Also highlight items with no up-to-date crafting recipe in comparison of the game's one
items = get_items_for_recipes_editor(items, recipes)
if game_version == 1:
images = get_images(game_version=game_version)
items = merge_images_in_items(items, images)
return render_template(
'recipes_editor/home.html',
items=items
)
@app.route('/recipes-editor/<item_id>', methods=['GET', 'POST'])
def recipes_editor_item(item_id):
if not app.config['DEBUG']: # Can only edit crafting recipes locally
abort(404)
game_version = 1 # TODO
items = load_json(app.config['ITEMS_FILE'].format(game_version=game_version))
recipes = load_json(app.config['RECIPES_FILE'].format(game_version=game_version))
# Highlight items with crafting recipe but not in the Craft N' Escape recipes file
# Also highlight items with no up-to-date crafting recipe in comparison of the game's one
items = get_items_for_recipes_editor(items, recipes)
if game_version == 1:
images = get_images(game_version=game_version)
items = merge_images_in_items(items, images)
if item_id not in items:
abort(404)
if request.method == 'POST':
recipe_hash = request.form.get('_recipe_hash')
recipe_items = json.loads(request.form.get('recipe_items'))
try:
if item_id not in recipes:
recipes[item_id] = {}
recipes[item_id]['_recipe_hash'] = recipe_hash
recipes[item_id]['items'] = recipe_items
save_json(app.config['RECIPES_FILE'].format(game_version=game_version), recipes)
flash('Recipe saved successfully.', 'success')
except Exception as e:
flash('Error saving this recipe: {}'.format(e), 'error')
current_item = items[item_id]
if item_id in recipes:
current_recipe = recipes[item_id]
else:
current_recipe = {'items': []}
return render_template(
'recipes_editor/item.html',
current_item=current_item,
current_item_id=item_id,
current_recipe=current_recipe,
items=items
)