Spaces:
Running
Running
Create fun_stats.py
Browse files- fun_stats.py +30 -0
fun_stats.py
ADDED
@@ -0,0 +1,30 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import json
|
2 |
+
import os
|
3 |
+
|
4 |
+
LEADERBOARD_FILE = "leaderboard.json"
|
5 |
+
|
6 |
+
def load_leaderboard():
|
7 |
+
if not os.path.exists(LEADERBOARD_FILE):
|
8 |
+
return {}
|
9 |
+
with open(LEADERBOARD_FILE, 'r') as f:
|
10 |
+
return json.load(f)
|
11 |
+
|
12 |
+
def get_fun_stats() -> str:
|
13 |
+
leaderboard = load_leaderboard()
|
14 |
+
if not leaderboard:
|
15 |
+
return "No stats available yet."
|
16 |
+
|
17 |
+
total_battles = sum(data['wins'] + data['losses'] for data in leaderboard.values())
|
18 |
+
total_wins = sum(data['wins'] for data in leaderboard.values())
|
19 |
+
total_losses = sum(data['losses'] for data in leaderboard.values())
|
20 |
+
win_rate = (total_wins / total_battles * 100) if total_battles > 0 else 0
|
21 |
+
|
22 |
+
stats_html = f"""
|
23 |
+
<ul>
|
24 |
+
<li><strong>Total Battles:</strong> {total_battles}</li>
|
25 |
+
<li><strong>Total Wins:</strong> {total_wins}</li>
|
26 |
+
<li><strong>Total Losses:</strong> {total_losses}</li>
|
27 |
+
<li><strong>Overall Win Rate:</strong> {win_rate:.2f}%</li>
|
28 |
+
</ul>
|
29 |
+
"""
|
30 |
+
return stats_html
|