57 lines
1.7 KiB
Python
57 lines
1.7 KiB
Python
from flask import Flask, render_template, jsonify
|
|
import os, json
|
|
|
|
app = Flask(__name__)
|
|
DATA_FOLDER = "data"
|
|
GAME_DATA_FOLDER = "game_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})
|
|
|
|
@app.route("/api/buildings")
|
|
def api_buildings():
|
|
"""Return building id → Greek name map"""
|
|
path = os.path.join(GAME_DATA_FOLDER, "buildings_filtered.json")
|
|
with open(path, encoding="utf-8") as f:
|
|
data = json.load(f)
|
|
return jsonify(data)
|
|
|
|
@app.route("/api/units")
|
|
def api_units():
|
|
"""Return unit id → Greek name map"""
|
|
path = os.path.join(GAME_DATA_FOLDER, "units_filtered.json")
|
|
with open(path, encoding="utf-8") as f:
|
|
data = json.load(f)
|
|
return jsonify(data)
|
|
|
|
if __name__ == "__main__":
|
|
app.run(debug=True)
|