This repository was archived by the owner on Oct 1, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstandings.js
More file actions
127 lines (114 loc) · 3.98 KB
/
standings.js
File metadata and controls
127 lines (114 loc) · 3.98 KB
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
exports.Standings = class {
constructor (listOfTeamnames, runIds, runs, numberOfRunsToCount) {
this.standings = [];
this.listOfTeamnames = listOfTeamnames;
this.runIds = runIds;
this.runs = runs;
this.numberOfRunsToCount = numberOfRunsToCount;
this.initializeStandings();
this.insertRunsIntoStandings();
this.calculateTotalScoreAndCreateRanking();
}
initializeStandings () {
this.standings = [];
for (let i=0; i<this.listOfTeamnames.length; i++) {
this.standings.push({
rank: null,
teamname: this.listOfTeamnames[i],
score: null,
time: null,
runs: []
});
for (let j=0; j<this.runIds.length; j++) {
this.standings[i].runs.push({
id: this.runIds[j],
score: null,
time: null
});
}
}
}
insertRunsIntoStandings () {
for (let run of this.runs) {
let team = this.standings.find((team) => team.teamname === run.teamname);
if (team) {
// correct team in standings found, now find correct run in standings (by id)
// try to find by round first, otherwise by arena
let runStanding = team.runs.find((runStanding) => runStanding.id == run.round) ||
team.runs.find((runStanding) => runStanding.id == run.arena);
if (runStanding) {
runStanding.score = run.score;
runStanding.time = run.time_duration;
} else {
console.log("couldn't match to run in standings: ", run);
}
} else {
console.log("teamname not found in standings: ", run);
}
}
}
calculateTotalScoreAndCreateRanking () {
this.calculateScoreForEachTeam();
this.sortTeamsByScore();
this.setRankForEachTeam();
}
calculateScoreForEachTeam () {
for (let team of this.standings) {
team.runs.sort((run1, run2) => compareByScoreAndTime(run1, run2));
team.score = 0;
team.time = 0;
for (let i=0; i<this.numberOfRunsToCount; i++) {
team.score += team.runs[i].score;
team.time += team.runs[i].time;
}
}
}
sortTeamsByScore () {
this.standings.sort((team1, team2) => compareByScoreAndTime(team1, team2));
}
setRankForEachTeam () {
let rank = 1;
for (let i=0; i<this.standings.length; i++) {
if (i > 0 && compareByScoreAndTime(this.standings[i], this.standings[i-1]) !== 0) {
rank = i+1;
}
this.standings[i].rank = rank;
}
}
getStandingsAsTable () {
let table = [];
table.push(["#","Team","Punktzahl","Zeit"]);
for (let runId of this.runIds) {
table[0].push("Punkte ("+String(runId).slice(-1)+")");
table[0].push("Zeit ("+String(runId).slice(-1)+")");
}
for (let team of this.standings) {
table.push([
team.rank,
team.teamname,
team.score,
team.time
]);
for (let runId of this.runIds) {
let run = team.runs.find((run) => run.id === runId);
table[table.length-1].push(run.score);
table[table.length-1].push(run.time);
}
}
return table;
}
}
let compareByScoreAndTime = function (run1, run2) {
// sort from best/highest to worst: -1 if run1 is better, +1 if worse
if (run1.score > run2.score) {
return -1;
} else if (run1.score < run2.score) {
return 1;
} else if (run1.time < run2.time) {
return -1;
} else if (run1.time > run2.time) {
return 1;
} else {
return 0;
}
};