Add app.py
This commit is contained in:
39
app.py
Normal file
39
app.py
Normal file
@@ -0,0 +1,39 @@
|
||||
from flask import Flask, render_template, jsonify
|
||||
import os, json
|
||||
|
||||
app = Flask(__name__)
|
||||
DATA_FOLDER = "data"
|
||||
|
||||
def load_all_data():
|
||||
players = {}
|
||||
all_attacks = []
|
||||
|
||||
for fname in os.listdir(DATA_FOLDER):
|
||||
if fname.endswith(".json"):
|
||||
player_name = fname.rsplit(".", 1)[0]
|
||||
try:
|
||||
with open(os.path.join(DATA_FOLDER, fname), encoding="utf-8") as f:
|
||||
data = json.load(f)
|
||||
players[player_name] = data
|
||||
|
||||
# Collect incoming attacks if present
|
||||
if "incoming_attacks" in data:
|
||||
for attack in data["incoming_attacks"]:
|
||||
attack["player"] = player_name
|
||||
all_attacks.append(attack)
|
||||
except json.JSONDecodeError:
|
||||
players[player_name] = {"error": "Invalid JSON"}
|
||||
|
||||
return players, all_attacks
|
||||
|
||||
@app.route("/")
|
||||
def index():
|
||||
return render_template("index.html")
|
||||
|
||||
@app.route("/api/data")
|
||||
def api_data():
|
||||
players, attacks = load_all_data()
|
||||
return jsonify({"players": players, "attacks": attacks})
|
||||
|
||||
if __name__ == "__main__":
|
||||
app.run(debug=True)
|
||||
Reference in New Issue
Block a user