Add a hash of the stash to form data

This commit is contained in:
2023-10-28 15:19:32 +02:00
parent 5cca2ef748
commit 86d2a684f5
3 changed files with 17 additions and 10 deletions

View File

@@ -1,3 +1,4 @@
import hashlib
from flask import Flask, redirect, abort, render_template, request from flask import Flask, redirect, abort, render_template, request
from pathlib import Path from pathlib import Path
from d2warehouse.parser import parse_stash from d2warehouse.parser import parse_stash
@@ -19,7 +20,7 @@ def save_path() -> Path:
path = Path.home() / "Saved Games/Diablo II Resurrected" path = Path.home() / "Saved Games/Diablo II Resurrected"
if not path.exists(): if not path.exists():
raise RuntimeError("Save path `{path}` does not exist") raise RuntimeError(f"Save path `{path}` does not exist")
return path return path
@@ -42,8 +43,12 @@ def list_stash(stash_name: str):
abort(404) abort(404)
path = save_path() / STASH_FILES[stash_name] path = save_path() / STASH_FILES[stash_name]
stash_data = path.read_bytes() stash_data = path.read_bytes()
stash_hash = hashlib.sha256(stash_data).hexdigest()
stash = parse_stash(stash_data) stash = parse_stash(stash_data)
return render_template("list_stash.html", stash_name=stash_name, stash=stash)
return render_template(
"list_stash.html", stash_name=stash_name, stash=stash, stash_hash=stash_hash
)
@app.route("/stash/<stash_name>/store", methods=["POST"]) @app.route("/stash/<stash_name>/store", methods=["POST"])
@@ -58,15 +63,16 @@ def stash_store_items(stash_name: str):
return 500 return 500
stash_data = stash_path.read_bytes() stash_data = stash_path.read_bytes()
stash_hash = hashlib.sha256(stash_data).hexdigest()
if request.form.get("stash_hash") != stash_hash:
return "wrong stash hash", 400
stash = parse_stash(stash_data) stash = parse_stash(stash_data)
items = [] items = []
for item_location in request.form.keys(): locs = [y for x in request.form.keys() if (y := re.match(r"item_(\d+)_(\d+)", x))]
match = re.match(r"(\d+)_(\d+)", item_location) for item_location in locs:
if not match: tab_idx, item_idx = int(item_location.group(1)), int(item_location.group(2))
# TODO: Handle this condition
return "invalid position"
tab_idx, item_idx = int(match.group(1)), int(match.group(2))
if tab_idx > len(stash.tabs) or item_idx > len(stash.tabs[tab_idx].items): if tab_idx > len(stash.tabs) or item_idx > len(stash.tabs[tab_idx].items):
# TODO: Handle this condition # TODO: Handle this condition
return "invalid position (2)" return "invalid position (2)"

View File

@@ -1,5 +1,5 @@
<div class="item"> <div class="item">
<input type="checkbox" name="{{tabloop.index0}}_{{itemloop.index0}}" value="remove" /> ({{tabloop.index0}}, {{itemloop.index0}}) <input type="checkbox" name="item_{{tabloop.index0}}_{{itemloop.index0}}" value="remove" /> ({{tabloop.index0}}, {{itemloop.index0}})
<ul> <ul>
<li class="name color-{{item.color}}">{{item.name}}</li> <li class="name color-{{item.color}}">{{item.name}}</li>
{% if item.quality and item.quality >= 5 %} {% if item.quality and item.quality >= 5 %}

View File

@@ -17,7 +17,8 @@
{% endfor %} {% endfor %}
</div> </div>
{% endfor %} {% endfor %}
<input type="submit" value="Remove items"> <input type="submit" value="Store items">
<input type="hidden" name="stash_hash" value="{{stash_hash}}" />
</form> </form>
</body> </body>
</html> </html>