1
0
wa-dyn-map/wa-dyn-map.py
2021-02-26 16:02:20 +01:00

42 lines
1.2 KiB
Python

from flask import Flask, jsonify, abort
from pathlib import Path
import json
app = Flask(__name__)
base_dir = Path('../Prediger-workadventure').resolve()
print(base_dir)
@app.route('/dyn/<string:code>/<path:map>')
def dyn_map(code, map):
p = (base_dir / map).resolve()
print(p)
if not base_dir < p:
abort(404)
if p.is_file() and p.suffix == ".json":
with p.open() as f:
map = json.load(f)
if "layers" in map:
for i in range(len(map["layers"])):
if "properties" in map["layers"][i]:
for j in range(len(map["layers"][i]["properties"])):
if "name" in map["layers"][i]["properties"][j] and map["layers"][i]["properties"][j]["name"] == "jitsiRoom" and "value" in map["layers"][i]["properties"][j]:
map["layers"][i]["properties"][j]["value"] = map["layers"][i]["properties"][j]["value"] + "_" + code
if "tilesets" in map:
for i in range(len(map["tilesets"])):
if "image" in map["tilesets"][i]:
map["tilesets"][i]["image"] = "../../map/" + map["tilesets"][i]["image"]
return jsonify(map)
abort(404)
if __name__ == "__main__":
app.run()