k-mktr commited on
Commit
c095e16
1 Parent(s): 00922a6

Create leaderboard.py

Browse files
Files changed (1) hide show
  1. leaderboard.py +47 -0
leaderboard.py ADDED
@@ -0,0 +1,47 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from nc_py_api import Nextcloud
2
+ import json
3
+ from typing import Dict, Any
4
+ import os
5
+
6
+ import arena_config
7
+
8
+ # Initialize Nextcloud client
9
+ nc = Nextcloud(nextcloud_url=arena_config.NEXTCLOUD_URL, nc_auth_user=arena_config.NEXTCLOUD_USERNAME, nc_auth_pass=arena_config.NEXTCLOUD_PASSWORD)
10
+
11
+ def load_leaderboard() -> Dict[str, Any]:
12
+ try:
13
+ file_content = nc.files.download(arena_config.NEXTCLOUD_LEADERBOARD_PATH)
14
+ return json.loads(file_content.decode('utf-8'))
15
+ except Exception as e:
16
+ print(f"Error loading leaderboard: {str(e)}")
17
+ return {}
18
+
19
+ def save_leaderboard(leaderboard_data: Dict[str, Any]) -> bool:
20
+ try:
21
+ json_data = json.dumps(leaderboard_data, indent=2)
22
+ nc.files.upload(arena_config.NEXTCLOUD_LEADERBOARD_PATH, json_data.encode('utf-8'))
23
+ return True
24
+ except Exception as e:
25
+ print(f"Error saving leaderboard: {str(e)}")
26
+ return False
27
+
28
+ def update_leaderboard(winner: str, loser: str) -> Dict[str, Any]:
29
+ leaderboard = load_leaderboard()
30
+
31
+ if winner not in leaderboard:
32
+ leaderboard[winner] = {"wins": 0, "losses": 0, "opponents": {}}
33
+ if loser not in leaderboard:
34
+ leaderboard[loser] = {"wins": 0, "losses": 0, "opponents": {}}
35
+
36
+ leaderboard[winner]["wins"] += 1
37
+ leaderboard[winner]["opponents"].setdefault(loser, {"wins": 0, "losses": 0})["wins"] += 1
38
+
39
+ leaderboard[loser]["losses"] += 1
40
+ leaderboard[loser]["opponents"].setdefault(winner, {"wins": 0, "losses": 0})["losses"] += 1
41
+
42
+ save_leaderboard(leaderboard)
43
+ return leaderboard
44
+
45
+ # Function to get the current leaderboard
46
+ def get_current_leaderboard() -> Dict[str, Any]:
47
+ return load_leaderboard()