This commit is contained in:
Topvennie 2024-09-30 16:39:43 +02:00
commit 0edf5c544f
No known key found for this signature in database
4 changed files with 113 additions and 0 deletions

1
.gitignore vendored Normal file
View file

@ -0,0 +1 @@
.venv

37
main.py Normal file
View file

@ -0,0 +1,37 @@
import requests
from flask import Flask, jsonify, render_template
users = {"local_klink": "Francis"}
def get_data():
res = requests.get("https://botbattle.be/api/leaderboard")
return res.json()
def filter_date(data):
data = sorted(data, key=lambda x: x["elo"], reverse=True)
filtered = list(filter(lambda x: x["username"] in users.keys(), data))
[dict.update(item, position=index + 1, name=users[item["username"]]) for index, item in enumerate(filtered)]
return filtered
app = Flask(__name__)
@app.route("/")
def index():
data = get_data()
filtered = filter_date(data)
return render_template("index.html", data=filtered)
@app.route("/leaderboard")
def leaderboard():
data = get_data()
filtered = filter_date(data)
return jsonify(filtered)
if __name__ == "__main__":
app.run()

15
templates/header.html Normal file
View file

@ -0,0 +1,15 @@
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<link
rel="stylesheet"
href="https://cdn.jsdelivr.net/npm/bulma@1.0.2/css/bulma.min.css"
/>
<title>Ceneka leaderboard</title>
</head>
<body>
{% block content %} {% endblock %}
</body>
</html>

60
templates/index.html Normal file
View file

@ -0,0 +1,60 @@
{% extends "header.html" %} {% block content %}
<header>
<h1 class="title">Leaderboard</h1>
</header>
<table class="table">
<thead>
<tr>
<th>Position</th>
<th>Name</th>
<th>Elo</th>
<th>Username</th>
</tr>
</thead>
<tbody id="leaderboard-body">
{% for row in data %}
<tr>
<td>{{ row['position'] }}</td>
<td>{{ row['name'] }}</td>
<td>{{ row['elo'] }}</td>
<td>{{ row['username'] }}</td>
</tr>
{% endfor %}
</tbody>
</table>
<script>
async function fetchLeaderboardData() {
try {
const response = await fetch("http://localhost:5000/leaderboard");
if (!response.ok) return;
const data = await response.json();
const tableBody = document.getElementById("leaderboard-body");
tableBody.innerHTML = "";
data.forEach((row) => {
const tr = document.createElement("tr");
tr.innerHTML = `
<td>${row.position}</td>
<td>${row.name}</td>
<td>${row.elo}</td>
<td>${row.username}</td>
`;
tableBody.appendChild(tr);
});
} catch (error) {
console.error("Failed to fetch leaderboard data:", error);
}
}
// Poll the server every 5 seconds
setInterval(fetchLeaderboardData, 5000);
// Initial load
fetchLeaderboardData();
</script>
{% endblock %}