-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrank.js
189 lines (155 loc) · 6.81 KB
/
rank.js
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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
document.addEventListener('DOMContentLoaded', () => {
const body = document.body;
const suggestedUsersSelect = document.getElementById('suggestedUsers');
const checkStatsButton = document.getElementById('check-stats');
const statsDisplay = document.getElementById('stats-display');
const clearButton = document.getElementById('clear-button');
// Automatically load saved user data
const savedUsers = JSON.parse(localStorage.getItem('savedUsers')) || [];
savedUsers.forEach(user => {
const option = document.createElement('option');
option.value = `${user.username}#${user.tag}`;
option.text = `${user.username}#${user.tag}`;
suggestedUsersSelect.add(option);
});
// Disable right-click context menu
document.addEventListener('contextmenu', (event) => {
event.preventDefault();
});
// Add event listener for the delete button
const deleteButton = document.getElementById('delete-button');
if (deleteButton) {
deleteButton.addEventListener('click', () => {
const selectedUser = suggestedUsersSelect.value;
if (selectedUser) {
// Split selectedUser to get username, tag, and region
const [savedUsername, tag] = selectedUser.split('#');
// Find the index of the selected user in savedUsers
const userIndex = savedUsers.findIndex(user => user.username === savedUsername && user.tag === tag);
if (userIndex !== -1) {
// Remove the selected user from savedUsers
savedUsers.splice(userIndex, 1);
localStorage.setItem('savedUsers', JSON.stringify(savedUsers));
// Update the dropdown menu
displaySavedUsers();
}
}
});
}
// Add event listener for the clear history button
const clearHistoryButton = document.getElementById('clear-history-button');
if (clearHistoryButton) {
clearHistoryButton.addEventListener('click', () => {
// Clear all saved users from local storage
localStorage.removeItem('savedUsers');
// Update the dropdown menu
displaySavedUsers();
});
}
// Add event listener for the clear button
if (clearButton) {
clearButton.addEventListener('click', () => {
// Clear the input fields for username and tag
usernameInput.value = '';
tagInput.value = '';
// Optional: Clear the suggested user selection in the dropdown
suggestedUsersSelect.value = '';
// Optional: Clear the stats display
statsDisplay.innerHTML = '';
statsDisplay.style.display = 'none';
});
}
// Valorant stats functionality
const usernameInput = document.getElementById('username');
const tagInput = document.getElementById('tag');
// Listen for input changes in the username field
usernameInput.addEventListener('input', () => {
// Clear the suggested user selection when a new username is entered
suggestedUsersSelect.value = '';
});
checkStatsButton.addEventListener('click', () => {
const enteredUsername = usernameInput.value.trim();
const selectedUser = suggestedUsersSelect.value;
const [savedUsername, tagRegion] = selectedUser ? selectedUser.split('#') : [enteredUsername, tagInput.value];
// Split tagRegion to get tag and region
let [tag, region] = tagRegion ? tagRegion.split('-') : ['', '']; // Initialize region with an empty string
if (region) {
// Use the saved region directly if available
searchStats(savedUsername, tag, region);
} else {
// Iterate through all regions if no saved region is available
const regions = ['na', 'br', 'ap', 'eu', 'kr', 'latam'];
let failedRequests = 0;
for (const currentRegion of regions) {
searchStats(savedUsername, tag, currentRegion)
.catch(() => {
// Count failed requests
failedRequests++;
// Check if all regions failed, then show the error
if (failedRequests === regions.length) {
showErrorNotification();
}
});
}
}
});
// Function to search for stats using the specified region
const searchStats = (savedUsername, tag, region) => {
const apiUrl = `https://api.henrikdev.xyz/valorant/v1/mmr/${region}/${savedUsername}/${tag}?api_key=HDEV-56826896-05f7-4a8d-82e0-b7a620c7123a`;
return fetch(apiUrl)
.then(response => {
if (response.ok) {
return response.json();
}
throw new Error(`Error fetching player stats for ${region}: ${response.statusText}`);
})
.then(data => {
// Display the stats for the successful response
const { currenttierpatched, ranking_in_tier, mmr_change_to_last_game, elo, images } = data.data;
const mmrChange = mmr_change_to_last_game;
const mmrChangeSign = mmrChange >= 0 ? '+' : '-';
const content = `
<p>Region: ${region}</p>
<p>Rank: ${currenttierpatched} (${ranking_in_tier}/100 RR)</p>
<p>MMR: ${elo}</p>
<p>Last Game RR Change: ${mmrChangeSign}${Math.abs(mmrChange)}</p>
<img src="${images.large}" alt="${currenttierpatched} rank icon">
`;
// Display stats in the #stats-display element
statsDisplay.innerHTML = content;
statsDisplay.style.display = 'block'; // Show the stats
// Save the username, tag, and region in local storage
saveUser(savedUsername, tag, region);
});
};
// Function to show an error notification
const showErrorNotification = () => {
// Show an error notification for invalid username or tag
alert('Invalid username or tag');
};
// Function to save the username, tag, and region in local storage
const saveUser = (username, tag) => {
const cleanUsername = username.replace(/\s/g, ''); // Remove spaces from the username
const existingUser = savedUsers.find(user => user.username === cleanUsername && user.tag === tag);
if (!existingUser) {
// Save the username, tag, and region in savedUsers
savedUsers.push({ username: cleanUsername, tag, });
localStorage.setItem('savedUsers', JSON.stringify(savedUsers));
// Update the dropdown menu
displaySavedUsers();
}
};
// Function to display saved users in the dropdown menu
const displaySavedUsers = () => {
const savedUsers = JSON.parse(localStorage.getItem('savedUsers')) || [];
// Clear existing options
suggestedUsersSelect.innerHTML = '';
// Add saved users to the dropdown menu
savedUsers.forEach(user => {
const option = document.createElement('option');
option.value = `${user.username}#${user.tag}`;
option.text = `${user.username}#${user.tag}`;
suggestedUsersSelect.appendChild(option);
});
};
});