-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstandings.js
346 lines (310 loc) · 11.7 KB
/
standings.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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
/**
* EuroLeague Standings & Tie-Break (Node.js)
*
* HOW TO RUN FROM COMMAND LINE:
* 1. Make sure you have Node.js installed.
* 2. Install the "xmldom" package if you don't have it:
* npm install xmldom
* 3. Place your "results.xml" file in the same directory as this script.
* 4. (Optional) Edit the array "overtimeGameIDs" below if you have games that went to OT.
* 5. Run:
* node euroleague_standings.js
*
* This script will:
* - Read results.xml from disk.
* - Parse the <game> entries.
* - Apply EuroLeague tie-break rules:
* (a) Best head-to-head record among tied teams
* (b) Head-to-head points difference among tied teams
* (c) Overall points difference
* (d) Total points scored
* (e) Sum of quotients (pointsFor / pointsAgainst) across all games
* ... reapplying tie-break among only those teams still tied after each step.
* - Exclude all points/differences from any game that is listed as "overtime" in
* the "overtimeGameIDs" array.
* - Print the standings with columns:
* TEAM | W | L | OverallDiff | H2H_W | H2H_L | H2H_Diff
*/
import { DOMParser } from "xmldom";
// -----------------------------
// Customize your OT games here
// -----------------------------
// If you know a certain <game> was decided in overtime, list its <gamenumber>
// (or whatever unique identifier you prefer). For example:
const euroleagueOvertimeGameIDs = [
35, 75, 107, 117, 182, 190, 195
];
const eurocupOvertimeGameIDs = [
1, 12, 41, 54, 105, 113, 115, 135
];
// -----------------------------
// 2) Generate Standings
// -----------------------------
function generateStandings(xmlData, overtimeIDs, filter) {
const parser = new DOMParser();
const xmlDoc = parser.parseFromString(xmlData, "application/xml");
const gameNodes = xmlDoc.getElementsByTagName("game");
// Data structure to store team stats
// { TEAM_CODE: {
// name: string,
// code: string,
// wins: number,
// losses: number,
// ptsFor: number,
// ptsAgainst: number,
// sumOfQuotients: number,
// h2h: {
// [opponentCode]: {
// ptsFor: number,
// ptsAgainst: number,
// wins: number,
// losses: number
// }
// }
// }
// }
const teams = {};
function initTeamIfNotExists(teamCode, teamName) {
if (!teams[teamCode]) {
teams[teamCode] = {
name: teamName,
code: teamCode,
wins: 0,
losses: 0,
ptsFor: 0,
ptsAgainst: 0,
sumOfQuotients: 0,
h2h: {}
};
}
}
// -----------------------------
// 3) Process each <game> entry
// -----------------------------
for (let i = 0; i < gameNodes.length; i++) {
const game = gameNodes[i];
const homeTeamName = game.getElementsByTagName("hometeam")[0].textContent;
const homeTeamCode = game.getElementsByTagName("homecode")[0].textContent;
const awayTeamName = game.getElementsByTagName("awayteam")[0].textContent;
const awayTeamCode = game.getElementsByTagName("awaycode")[0].textContent;
const homeScore = parseInt(game.getElementsByTagName("homescore")[0].textContent, 10);
const awayScore = parseInt(game.getElementsByTagName("awayscore")[0].textContent, 10);
const played = game.getElementsByTagName("played")[0].textContent === "true";
const gameNumber = parseInt(game.getElementsByTagName("gamenumber")[0].textContent, 10);
if (!played) {
// If the game hasn't been played, skip it.
continue;
}
// Filter logic
if (filter) {
const field = filter.field;
const value = filter.value;
const actualValue = game.getElementsByTagName(field)[0].textContent;
if (actualValue !== value) {
continue;
}
}
// Ensure we have entries for both teams
initTeamIfNotExists(homeTeamCode, homeTeamName);
initTeamIfNotExists(awayTeamCode, awayTeamName);
// Determine the winner (for W/L record), ignoring OT points logic
// because the outcome doesn't change (someone won).
if (homeScore > awayScore) {
teams[homeTeamCode].wins += 1;
teams[awayTeamCode].losses += 1;
} else if (awayScore > homeScore) {
teams[awayTeamCode].wins += 1;
teams[homeTeamCode].losses += 1;
}
// If a tie is possible, handle that as well.
// (Normally basketball doesn't end in a tie.)
// Head-to-head structure init
if (!teams[homeTeamCode].h2h[awayTeamCode]) {
teams[homeTeamCode].h2h[awayTeamCode] = { ptsFor: 0, ptsAgainst: 0, wins: 0, losses: 0 };
}
if (!teams[awayTeamCode].h2h[homeTeamCode]) {
teams[awayTeamCode].h2h[homeTeamCode] = { ptsFor: 0, ptsAgainst: 0, wins: 0, losses: 0 };
}
if (homeScore > awayScore) {
teams[homeTeamCode].h2h[awayTeamCode].wins += 1;
teams[awayTeamCode].h2h[homeTeamCode].losses += 1;
} else {
teams[awayTeamCode].h2h[homeTeamCode].wins += 1;
teams[homeTeamCode].h2h[awayTeamCode].losses += 1;
}
// -----------------------------
// OT check:
// If the gameNumber is in the list of OT games,
// do NOT apply scoring stats or sumOfQuotients
// (i.e. "don't apply score difference updates").
// -----------------------------
const isOvertimeGame = overtimeIDs.includes(gameNumber);
if (!isOvertimeGame) {
// Update overall points for/against
teams[homeTeamCode].ptsFor += homeScore;
teams[homeTeamCode].ptsAgainst += awayScore;
teams[awayTeamCode].ptsFor += awayScore;
teams[awayTeamCode].ptsAgainst += homeScore;
// Sum of quotients for tie-break #5
if (awayScore > 0) {
teams[homeTeamCode].sumOfQuotients += (homeScore / awayScore);
}
if (homeScore > 0) {
teams[awayTeamCode].sumOfQuotients += (awayScore / homeScore);
}
// Update head-to-head points
teams[homeTeamCode].h2h[awayTeamCode].ptsFor += homeScore;
teams[homeTeamCode].h2h[awayTeamCode].ptsAgainst += awayScore;
teams[awayTeamCode].h2h[homeTeamCode].ptsFor += awayScore;
teams[awayTeamCode].h2h[homeTeamCode].ptsAgainst += homeScore;
}
}
// -----------------------------
// 4) Convert teams object to array
// -----------------------------
let teamArray = Object.values(teams);
// -----------------------------
// 5) Tie-Break function
// -----------------------------
function applyTieBreak(sortedGroup) {
if (sortedGroup.length <= 1) {
return sortedGroup;
}
// We'll build a local compare function that tries each step in order
function buildMiniTable(subGroup) {
// subGroup: array of team objects
const miniTable = {};
subGroup.forEach((teamObj) => {
miniTable[teamObj.code] = {
code: teamObj.code,
name: teamObj.name,
h2hWins: 0,
h2hLosses: 0,
h2hPointsFor: 0,
h2hPointsAgainst: 0,
};
});
// Accumulate only for teams within subGroup
subGroup.forEach((t) => {
const tCode = t.code;
subGroup.forEach((other) => {
if (other.code === tCode) return;
const oCode = other.code;
// If there's an h2h record, accumulate it
if (teams[tCode].h2h[oCode]) {
miniTable[tCode].h2hWins += teams[tCode].h2h[oCode].wins;
miniTable[tCode].h2hLosses += teams[tCode].h2h[oCode].losses;
miniTable[tCode].h2hPointsFor += teams[tCode].h2h[oCode].ptsFor;
miniTable[tCode].h2hPointsAgainst += teams[tCode].h2h[oCode].ptsAgainst;
}
});
});
return miniTable;
}
function compareTeams(a, b, subGroup) {
// Build mini-table from subGroup
const miniTable = buildMiniTable(subGroup);
// 1) Best record in head-to-head games
const aPercentage = miniTable[a.code].h2hWins / (miniTable[a.code].h2hWins + miniTable[a.code].h2hLosses);
const bPercentage = miniTable[b.code].h2hWins / (miniTable[b.code].h2hWins + miniTable[b.code].h2hLosses);
if (aPercentage !== bPercentage) {
return bPercentage - aPercentage;
}
// 2) Higher cumulative score difference in h2h
const aH2HDiff = miniTable[a.code].h2hPointsFor - miniTable[a.code].h2hPointsAgainst;
const bH2HDiff = miniTable[b.code].h2hPointsFor - miniTable[b.code].h2hPointsAgainst;
if (aH2HDiff !== bH2HDiff) {
return bH2HDiff - aH2HDiff;
}
// 3) Overall points difference
const aSeasonDiff = a.ptsFor - a.ptsAgainst;
const bSeasonDiff = b.ptsFor - b.ptsAgainst;
if (aSeasonDiff !== bSeasonDiff) {
return bSeasonDiff - aSeasonDiff;
}
// 4) Total points scored
if (a.ptsFor !== b.ptsFor) {
return b.ptsFor - a.ptsFor;
}
// 5) Higher sumOfQuotients
if (a.sumOfQuotients !== b.sumOfQuotients) {
return b.sumOfQuotients - a.sumOfQuotients;
}
// Still tied
return 0;
}
let tiebreaked = [...sortedGroup];
tiebreaked.sort((a, b) => compareTeams(a, b, tiebreaked));
// Because multiple teams might remain fully tied through all steps,
// we'll do a sub-block detection and re-sort. In practice, re-sorting
// with the same criteria won't magically separate them if there's
// truly no difference. We'll just keep them in the order they appear
// (or you could do random / alphabetical).
let finalOrder = [];
let i = 0;
while (i < tiebreaked.length) {
let block = [tiebreaked[i]];
let j = i + 1;
while (
j < tiebreaked.length &&
compareTeams(tiebreaked[j - 1], tiebreaked[j], tiebreaked) === 0 &&
compareTeams(tiebreaked[j], tiebreaked[j - 1], tiebreaked) === 0
) {
block.push(tiebreaked[j]);
j++;
}
if (block.length === 1) {
finalOrder.push(block[0]);
} else {
block.sort((a, b) => compareTeams(a, b, block));
finalOrder.push(...block);
}
i = j;
}
return finalOrder;
}
// -----------------------------
// 6) Sort by W (desc), then tie-break
// -----------------------------
teamArray.sort((a, b) => b.wins/b.losses - a.wins/a.losses);
let finalStandings = [];
let idx = 0;
while (idx < teamArray.length) {
let block = [teamArray[idx]];
let j = idx + 1;
while (j < teamArray.length && teamArray[j].wins === teamArray[idx].wins && teamArray[j].losses === teamArray[idx].losses) {
block.push(teamArray[j]);
j++;
}
if (block.length > 1) {
const tieBroken = applyTieBreak(block);
finalStandings.push(...tieBroken);
} else {
finalStandings.push(block[0]);
}
idx = j;
}
// ---------------------------------
// 7) Build display fields (H2H, etc)
// ---------------------------------
finalStandings.forEach(team => {
let h2hWins = 0, h2hLosses = 0, h2hPointsFor = 0, h2hPointsAgainst = 0;
Object.keys(team.h2h).forEach(oppCode => {
const opp = finalStandings.find(t => t.code === oppCode);
if (opp.wins == team.wins && opp.losses == team.losses) {
h2hWins += team.h2h[oppCode].wins;
h2hLosses += team.h2h[oppCode].losses;
h2hPointsFor += team.h2h[oppCode].ptsFor;
h2hPointsAgainst += team.h2h[oppCode].ptsAgainst;
}
});
team.h2hWins = h2hWins;
team.h2hLosses = h2hLosses;
team.h2hScoreDiff = h2hPointsFor - h2hPointsAgainst;
team.scoreDiff = team.ptsFor - team.ptsAgainst;
});
return {standings: finalStandings, teams: teamArray };
}
// Expose the function
export const generateEuroleagueStandingsFormXml = (xmlData) => generateStandings(xmlData, euroleagueOvertimeGameIDs);
export const generateEurocupStandingsFormXml = (xmlData, group) => generateStandings(xmlData, eurocupOvertimeGameIDs, {field: "group", value: group});