-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathAPIInterface.js
175 lines (141 loc) · 5.49 KB
/
APIInterface.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
async function requestAPI(url, apiKey) {
try {
const response = await fetch(url, {
headers: { "X-TBA-Auth-Key": apiKey },
});
if (!response.ok) {
throw new Error("Network response was not ok");
}
const data = await response.json();
return data; // Return the data directly
} catch (error) {
console.error(`${url} had a problem.`, error);
throw error; // Re-throw the error to propagate it to the caller
}
}
function resetStats() {
Array.from(team_stat_chart.children).forEach((child) => {
if (child.id != "team_stat_chart_labels") {
child.remove()
}
})
clearPlot()
}
function addEventsToEventList(event) {
new_option = option_template.cloneNode();
new_option.id = event_index;
new_option.textContent = event.name;
new_option.value = event.key;
event_dropdown.appendChild(new_option);
event_index += 1;
}
// Function to fetch team information from Blue Alliance API
async function fetchTeamInfo() {
const team_events_url = `https://www.thebluealliance.com/api/v3/team/frc${team_number}/events/${year}`;
resetStats();
try {
current_data = await requestAPI(team_events_url, APIKey);
console.log(current_data);
current_data.forEach((event) => addEventsToEventList(event));
} catch (error) {
console.error(error);
}
}
function addTeamToTeamList(team) {
new_option = team_option_template.cloneNode();
new_option.id = team_index;
new_option.textContent = `${team.team_number} - ${team.nickname}`;
new_option.value = team.key;
team_list.appendChild(new_option);
team_index += 1;
}
// Function to fetch team information from Blue Alliance API
async function fetchEventTeamsInfo() {
// API endpoint for team information
event_key = this.value;
const event_teams_url = `https://www.thebluealliance.com/api/v3/event/${event_key}/teams`;
resetStats();
document.querySelector("#team_list").innerHTML = document.querySelector("#team_option").outerHTML
try {
current_data = await requestAPI(event_teams_url, APIKey);
console.log(current_data);
current_data.forEach((team) => addTeamToTeamList(team));
} catch (error) {
console.error(error);
}
var team_option_list = document.getElementsByClassName("glassOption");
for (let i = 0; i < team_option_list.length; i++) {
team_option_list[i].onclick = () => {
const team_number = team_option_list[i].textContent.match(/\d+(?= - )/g)[0]
// Check if the element has the "selected" class
if (!team_option_list[i].classList.contains("selected")) {
// Add the "selected" class to indicate the item is selected
team_option_list[i].classList.add("selected");
selected_teams.push(team_number)
} else {
// Remove the "selected" class
team_option_list[i].classList.remove("selected");
selected_teams.splice(selected_teams.indexOf(team_number),1)
}
// Log whether the element is selected or not
console.log("Is selected: " + team_option_list[i].classList.contains("selected"));
console.log("I clicked on " + team_option_list[i].textContent);
updateStatGraph(team_number, team_option_list[i].classList.contains("selected"));
updateStatsTable()
updateRobotImages(team_number, team_option_list[i].classList.contains("selected"))
}
}
}
function getTeamStats (opr_list, rank_list, this_team_number, this_team_name) {
new_option = team_stat_chart_template.cloneNode(true);
new_option.id = team_stat_index;
new_option.childNodes.item(1).textContent = this_team_name;
new_option.childNodes.item(3).textContent = `${this_team_number.substring(3)}`;
rank_list.rankings.forEach((rank) => {
if (rank.team_key == this_team_number) {
new_option.childNodes.item(5).textContent = `${rank.rank}`;
}
});
new_option.childNodes.item(7).textContent = `${opr_list.oprs[this_team_number].toFixed(2)}`;
return new_option
}
async function updateStatsTable() {
// API endpoint for team information
const event_opr_url = `https://www.thebluealliance.com/api/v3/event/${event_key}/oprs`;
const event_rank_url = `https://www.thebluealliance.com/api/v3/event/${event_key}/rankings`;
var opr_list;
var rank_list;
team_stat_index = 0;
try {
opr_list = await requestAPI(event_opr_url, APIKey);
rank_list = await requestAPI(event_rank_url, APIKey);
option_list = document.getElementsByClassName("glassOption");
let rows = []
for (let i = 1; i < option_list.length; i++) {
if (option_list[i].classList.contains("selected")) {
rows.push(getTeamStats(opr_list, rank_list, option_list[i].value, option_list[i].textContent.split(" - ")[1]))
}
}
rows.sort((a,b) => b.childNodes.item(7).textContent - a.childNodes.item(7).textContent)
team_stat_chart.innerHTML = "";
team_stat_chart.appendChild(team_stat_chart_labels);
team_stat_chart.append(...rows)
} catch (error) {
console.error(error);
}
}
function updateRobotImages(team_number, selected) {
var container = document.querySelector("#team_pictures")
if (selected) {
const images = getImages()
let element = document.querySelector(".robot_image_container").cloneNode(true)
element.className = "robot_image_container"
element.querySelector("img")["src"] = images[team_number]
element.id = "robot_image_" + team_number
element.querySelector(".imageLabel").textContent = team_number
element.classList.remove("hidden")
container.append(element)
} else {
document.querySelector(`#robot_image_${team_number}`).remove()
}
}