-
Notifications
You must be signed in to change notification settings - Fork 0
/
get_data.js
172 lines (146 loc) · 3.89 KB
/
get_data.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
let YEAR = '2023';
let API_URL = 'https://api.openligadb.de/getmatchdata/bl1/' + YEAR;
async function getBLTable(team)
{
/**
* @description Gets details about overall performance for a team
* NOTE: the data is ALWAYS up-to-date!
*
* @param {String} team The team we're looking up
*
* @returns {Array} Returns a list containing GUV, tore,
* diff, punkte, platz
*/
// the shortName in data is just 'BVB' for 'dortmund'
if (team === 'Dortmund')
{
team = 'BVB';
}
const URL = 'https://api.openligadb.de/getbltable/bl1/' + YEAR;
const response = await fetch(URL, {
headers: {
'Accept': 'application/json'
}
});
let data = await response.json();
let finalData;
let platz;
for (let i = 0; i < data.length; i++)
{
if (data[i]['shortName'] === team)
{
finalData = data[i];
platz = i + 1;
break;
}
}
let wins = finalData['won'];
let ties = finalData['draw'];
let losses = finalData['lost'];
let GUV = wins+' | '+ties+' | '+losses;
let scored = finalData['goals'];
let scoredOn = finalData['opponentGoals'];
let tore = scored+':'+scoredOn;
let diff = finalData['goalDiff'];
if (diff >= 0)
{
diff = '+'+diff;
}
let punkte = finalData['points'];
gatheredData = [
GUV, tore, diff, punkte, platz
];
return gatheredData;
}
async function getData(team, game)
{
/**
* @description gets all of the data needed in updateTable()
*
* @param {String} team The team we're looking up
* @param {Integer} game What game we're going to look up
*
* @returns {Array} array full data that we're working with
*/
const URL = API_URL + '/' + team;
const response = await fetch(URL, {
headers: {
'Accept': 'application/json'
}
});
let data = await response.json();
// order is the same as it is on the table!! \\
// only get the simplified date
let date = data[game-1]['matchDateTime'].substring(0, 10);
let team1 = true; // if we're team 1
let enemyTeamName = data[game-1]['team2']['shortName'];
if (enemyTeamName === team) // if first team is diff then what we're looking for
{
team1 = false;
enemyTeamName = data[game-1]['team1']['shortName'];
}
if (enemyTeamName === 'BVB')
{
enemyTeamName = 'Dortmund';
}
// check if we're home or away
let ha;
if (team1)
{
ha = 'H';
} else {
ha = 'A';
}
let matchResults = data[game-1]['matchResults'][1];
let score;
let score1 = matchResults['pointsTeam1'];
let score2 = matchResults['pointsTeam2'];
if (team1) // if we're team 1
{
score = score1+'-'+score2;
} else {
score = score2+'-'+score1;
}
let BLTable = await getBLTable(team);
finalInfo = [
ha, game, date, enemyTeamName,
score, BLTable[0], BLTable[1],
BLTable[2], BLTable[3], BLTable[4],
];
return finalInfo;
}
async function updateTable(team, game)
{
/**
* @description updates the table. gets the data from getData()
*
* @param {String} team The team we're looking up
* @param {Integer} game What game we're going to look up
*/
let table = document.getElementById('dataTable');
let tableText = document.getElementById('tableText');
// don't know a better way to do this at this
// moment in time :)
let ha = document.getElementById('ha');
let spielt = document.getElementById('spielt');
let datum = document.getElementById('datum');
let gegner = document.getElementById('gegner');
let ergebnis = document.getElementById('ergebnis');
let guv = document.getElementById('guv');
let tore = document.getElementById('tore');
let diff = document.getElementById('diff');
let punkte = document.getElementById('punkte');
let platz = document.getElementById('platz');
let itemList = [
ha, spielt, datum, gegner, ergebnis,
guv, tore, diff, punkte, platz
];
let data = await getData(team, game);
for (let i = 0; i < itemList.length; i++)
{
itemList[i].textContent = String(data[i]);
}
table.classList.remove('hidden');
tableText.classList.remove('hidden');
return 0;
}