-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
submit_matches.py
141 lines (121 loc) · 5.3 KB
/
submit_matches.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
import csv
import requests
import os
from dotenv import load_dotenv
from fuzzywuzzy import fuzz
from prompt_toolkit import prompt
from prompt_toolkit.validation import Validator, ValidationError
# Load environment variables
load_dotenv()
API_KEY = os.getenv('SRC_API_TOKEN')
API_BASE_URL = 'https://secondrobotics.org'
def get_valid_players(game_mode_code):
url = f"{API_BASE_URL}/api/ranked/{game_mode_code}/players/"
headers = {'X-API-KEY': API_KEY}
response = requests.get(url, headers=headers)
if response.status_code == 200:
return response.json()
else:
print(f"Failed to fetch valid players: {response.status_code}")
return None
def find_closest_match(name, all_users):
best_match = None
best_ratio = 0
for user in all_users:
ratio_display = fuzz.ratio(name.lower(), user['display_name'].lower())
ratio_username = fuzz.ratio(name.lower(), user['username'].lower())
max_ratio = max(ratio_display, ratio_username)
if max_ratio > best_ratio:
best_ratio = max_ratio
best_match = user
return best_match, best_ratio
def validate_player_id(text):
if text.isdigit() and len(text) == 18:
return True
return False
class PlayerIdValidator(Validator):
def validate(self, document):
text = document.text
if not validate_player_id(text):
raise ValidationError(message='Please enter a valid 18-digit Discord ID')
def process_csv(file_path, all_users):
player_mappings = {}
matches = []
dummy_id = "000000000000000000" # Dummy player ID
with open(file_path, 'r') as csvfile:
reader = csv.reader(csvfile)
next(reader) # Skip header row if present
for row in reader:
match = []
for player in row[:6]: # Process all 6 players
player = player.strip()
if player in player_mappings:
match.append(player_mappings[player])
elif validate_player_id(player):
match.append(int(player))
player_mappings[player] = int(player)
else:
closest_match, ratio = find_closest_match(player, all_users)
if ratio == 100:
match.append(int(closest_match['id']))
player_mappings[player] = int(closest_match['id'])
else:
print(f"\nNo exact match found for '{player}'.")
print(f"Closest match: {closest_match['display_name']} (ID: {closest_match['id']})")
confirmation = prompt(f"Accept this match? (y/n/i for ignore): ").lower()
if confirmation == 'y':
match.append(int(closest_match['id']))
player_mappings[player] = int(closest_match['id'])
elif confirmation == 'i':
print(f"Ignoring player '{player}' and using dummy player.")
match.append(dummy_id)
player_mappings[player] = dummy_id
else:
manual_id = prompt("Enter the correct Discord ID: ", validator=PlayerIdValidator())
match.append(int(manual_id))
player_mappings[player] = int(manual_id)
match.extend([int(row[6]), int(row[7])]) # Add scores
matches.append(match)
return matches
def submit_match(red_alliance, blue_alliance, red_score, blue_score, game_mode_code):
url = f"{API_BASE_URL}/api/ranked/{game_mode_code}/match/"
headers = {
'Content-Type': 'application/json',
'X-API-KEY': API_KEY
}
payload = {
"red_alliance": red_alliance,
"blue_alliance": blue_alliance,
"red_score": red_score,
"blue_score": blue_score
}
response = requests.post(url, json=payload, headers=headers)
if response.status_code == 200:
return response.json()
else:
print(f"Error submitting match: Status code {response.status_code}")
print(f"Response content: {response.text}")
return None
if __name__ == "__main__":
csv_file_path = "matches.csv" # Replace with your CSV file path
game_mode_code = "CR3v3" # Replace with the appropriate game mode code
all_users = get_valid_players(game_mode_code)
if not all_users:
print("Failed to fetch valid players. Exiting.")
exit(1)
print(f"Fetched {len(all_users)} valid players")
matches = process_csv(csv_file_path, all_users)
print("\nAll players processed. Ready to submit matches.")
confirmation = prompt("Do you want to submit these matches? (y/n): ").lower()
if confirmation == 'y':
for match in matches:
red_alliance = match[:3]
blue_alliance = match[3:6]
red_score, blue_score = match[6:]
result = submit_match(red_alliance, blue_alliance, red_score, blue_score, game_mode_code)
if result:
print(f"Match submitted successfully: {result}")
else:
print("Failed to submit match. Please check the error message above.")
else:
print("Match submission cancelled.")