@@ -13,29 +13,11 @@ export function splitBy<T>(array: T[], key: keyof T): T[][] {
13
13
return Object . values ( obj ) ;
14
14
}
15
15
16
- interface Ranking {
17
- [ prop : string ] : number ,
18
- rank : number ,
19
- id : number ,
20
- played : number ,
21
- wins : number ,
22
- draws : number ,
23
- losses : number ,
24
- forfeits : number ,
25
- scoreFor : number ,
26
- scoreAgainst : number ,
27
- scoreDifference : number ,
28
- points : number ,
29
- }
30
-
31
- interface Header {
32
- value : string ,
33
- tooltip : string ,
16
+ export function isMajorRound ( roundNumber : number ) {
17
+ return roundNumber === 1 || roundNumber % 2 === 0 ;
34
18
}
35
19
36
- type Headers = { [ name in keyof Ranking ] : Header } ;
37
-
38
- const headers : Headers = {
20
+ const headers : RankingHeaders = {
39
21
'rank' : {
40
22
value : '#' ,
41
23
tooltip : 'Rank' ,
@@ -82,78 +64,78 @@ const headers: Headers = {
82
64
} ,
83
65
}
84
66
85
- export function isMajorRound ( roundNumber : number ) {
86
- return roundNumber === 1 || roundNumber % 2 === 0 ;
87
- }
88
-
89
- export function rankingHeader ( name : keyof Ranking ) : Header {
67
+ export function rankingHeader ( name : keyof RankingItem ) : Header {
90
68
return headers [ name ] ;
91
69
}
92
70
93
- export function getRanking ( matches : Match [ ] , pointsFormula ?: ( ranking : Ranking ) => number ) : Ranking [ ] {
94
- const teams : { [ id : number ] : Ranking } = { } ;
95
-
96
- function processTeam ( current : ParticipantResult | null , other : ParticipantResult | null ) {
97
- if ( ! current || current . id === null ) return ;
71
+ export function getRanking ( matches : Match [ ] , formula ?: RankingFormula ) : Ranking {
72
+ formula = formula || (
73
+ ( item : RankingItem ) => 3 * item . wins + 1 * item . draws + 0 * item . losses
74
+ ) ;
98
75
99
- const state = teams [ current . id ] || {
100
- rank : 0 ,
101
- id : 0 ,
102
- played : 0 ,
103
- wins : 0 ,
104
- draws : 0 ,
105
- losses : 0 ,
106
- forfeits : 0 ,
107
- scoreFor : 0 ,
108
- scoreAgainst : 0 ,
109
- scoreDifference : 0 ,
110
- points : 0 ,
111
- } ;
76
+ const rankingMap : RankingMap = { } ;
112
77
113
- state . id = current . id ;
114
- state . played ++ ;
78
+ for ( const match of matches ) {
79
+ processTeam ( rankingMap , formula , match . opponent1 , match . opponent2 ) ;
80
+ processTeam ( rankingMap , formula , match . opponent2 , match . opponent1 ) ;
81
+ }
115
82
116
- if ( current . result === 'win' )
117
- state . wins ++ ;
83
+ return createRanking ( rankingMap ) ;
84
+ }
118
85
119
- if ( current . result === 'draw' )
120
- state . draws ++ ;
86
+ function createRanking ( rankingMap : RankingMap ) {
87
+ const ranking = Object . values ( rankingMap ) . sort ( ( a , b ) => b . points - a . points ) ;
88
+
89
+ const rank = {
90
+ value : 0 ,
91
+ lastPoints : - 1 ,
92
+ } ;
121
93
122
- if ( current . result === 'loss' )
123
- state . losses ++ ;
94
+ for ( const item of ranking ) {
95
+ item . rank = rank . lastPoints !== item . points ? ++ rank . value : rank . value ;
96
+ rank . lastPoints = item . points ;
97
+ }
124
98
125
- if ( current . forfeit )
126
- state . forfeits ++ ;
99
+ return ranking ;
100
+ }
127
101
128
- state . scoreFor += current . score || 0 ;
129
- state . scoreAgainst += other && other . score || 0 ;
130
- state . scoreDifference = state . scoreFor - state . scoreAgainst ;
102
+ function processTeam ( rankingMap : RankingMap , formula : RankingFormula , current : ParticipantResult | null , other : ParticipantResult | null ) {
103
+ if ( ! current || current . id === null ) return ;
104
+
105
+ const state = rankingMap [ current . id ] || {
106
+ rank : 0 ,
107
+ id : 0 ,
108
+ played : 0 ,
109
+ wins : 0 ,
110
+ draws : 0 ,
111
+ losses : 0 ,
112
+ forfeits : 0 ,
113
+ scoreFor : 0 ,
114
+ scoreAgainst : 0 ,
115
+ scoreDifference : 0 ,
116
+ points : 0 ,
117
+ } ;
131
118
132
- const formula = pointsFormula || (
133
- ( ranking : Ranking ) => 3 * ranking . wins + 1 * ranking . draws + 0 * ranking . losses
134
- ) ;
119
+ state . id = current . id ;
120
+ state . played ++ ;
135
121
136
- state . points = formula ( state ) ;
122
+ if ( current . result === 'win' )
123
+ state . wins ++ ;
137
124
138
- teams [ current . id ] = state ;
139
- }
125
+ if ( current . result === 'draw' )
126
+ state . draws ++ ;
140
127
141
- for ( const match of matches ) {
142
- processTeam ( match . opponent1 , match . opponent2 ) ;
143
- processTeam ( match . opponent2 , match . opponent1 ) ;
144
- }
128
+ if ( current . result === 'loss' )
129
+ state . losses ++ ;
145
130
146
- const rankings = Object . values ( teams ) . sort ( ( a , b ) => b . points - a . points ) ;
131
+ if ( current . forfeit )
132
+ state . forfeits ++ ;
147
133
148
- let rank = {
149
- value : 0 ,
150
- lastPoints : - 1 ,
151
- } ;
134
+ state . scoreFor += current . score || 0 ;
135
+ state . scoreAgainst += other && other . score || 0 ;
136
+ state . scoreDifference = state . scoreFor - state . scoreAgainst ;
152
137
153
- for ( const ranking of rankings ) {
154
- ranking . rank = rank . lastPoints !== ranking . points ? ++ rank . value : rank . value ;
155
- rank . lastPoints = ranking . points ;
156
- }
138
+ state . points = formula ( state ) ;
157
139
158
- return rankings ;
140
+ rankingMap [ current . id ] = state ;
159
141
}
0 commit comments