|
1 | | -import { Match, ParticipantResult, GroupType, MatchGame } from 'brackets-model'; |
2 | | -import { RankingHeader, Ranking, RankingFormula, RankingItem, RankingMap, Side, MatchWithMetadata } from './types'; |
| 1 | +import { Match, GroupType, MatchGame, RankingItem } from 'brackets-model'; |
| 2 | +import { RankingHeader, Side, MatchWithMetadata } from './types'; |
3 | 3 | import { t } from './lang'; |
4 | 4 |
|
5 | 5 | /** |
@@ -171,103 +171,6 @@ export function rankingHeader(itemName: keyof RankingItem): RankingHeader { |
171 | 171 | return t(`ranking.${itemName}`, { returnObjects: true }) as RankingHeader; |
172 | 172 | } |
173 | 173 |
|
174 | | -/** |
175 | | - * Calculates the ranking based on a list of matches and a formula. |
176 | | - * |
177 | | - * @param matches The list of matches. |
178 | | - * @param formula The points formula to apply. |
179 | | - */ |
180 | | -export function getRanking(matches: Match[], formula?: RankingFormula): Ranking { |
181 | | - formula = formula || ( |
182 | | - (item: RankingItem): number => 3 * item.wins + 1 * item.draws + 0 * item.losses |
183 | | - ); |
184 | | - |
185 | | - const rankingMap: RankingMap = {}; |
186 | | - |
187 | | - for (const match of matches) { |
188 | | - processParticipant(rankingMap, formula, match.opponent1, match.opponent2); |
189 | | - processParticipant(rankingMap, formula, match.opponent2, match.opponent1); |
190 | | - } |
191 | | - |
192 | | - return createRanking(rankingMap); |
193 | | -} |
194 | | - |
195 | | -/** |
196 | | - * Processes a participant and edits the ranking map. |
197 | | - * |
198 | | - * @param rankingMap The ranking map to edit. |
199 | | - * @param formula The points formula to apply. |
200 | | - * @param current The current participant. |
201 | | - * @param other The opponent. |
202 | | - */ |
203 | | -function processParticipant(rankingMap: RankingMap, formula: RankingFormula, current: ParticipantResult | null, other: ParticipantResult | null): void { |
204 | | - if (!current || current.id === null) return; |
205 | | - |
206 | | - const state = rankingMap[current.id] || { |
207 | | - rank: 0, |
208 | | - id: 0, |
209 | | - played: 0, |
210 | | - wins: 0, |
211 | | - draws: 0, |
212 | | - losses: 0, |
213 | | - forfeits: 0, |
214 | | - scoreFor: 0, |
215 | | - scoreAgainst: 0, |
216 | | - scoreDifference: 0, |
217 | | - points: 0, |
218 | | - }; |
219 | | - |
220 | | - state.id = current.id; |
221 | | - |
222 | | - if (current.forfeit || current.result) |
223 | | - state.played++; |
224 | | - |
225 | | - if (current.result === 'win') |
226 | | - state.wins++; |
227 | | - |
228 | | - if (current.result === 'draw') |
229 | | - state.draws++; |
230 | | - |
231 | | - if (current.result === 'loss') |
232 | | - state.losses++; |
233 | | - |
234 | | - if (current.forfeit) |
235 | | - state.forfeits++; |
236 | | - |
237 | | - state.scoreFor += current.score || 0; |
238 | | - state.scoreAgainst += other && other.score || 0; |
239 | | - state.scoreDifference = state.scoreFor - state.scoreAgainst; |
240 | | - |
241 | | - state.points = formula(state); |
242 | | - |
243 | | - rankingMap[current.id] = state; |
244 | | -} |
245 | | - |
246 | | -/** |
247 | | - * Creates the final ranking based on a ranking map. (Sort + Total points) |
248 | | - * |
249 | | - * @param rankingMap The ranking map (object). |
250 | | - */ |
251 | | -function createRanking(rankingMap: RankingMap): RankingItem[] { |
252 | | - const ranking = Object.values(rankingMap).sort((a, b) => a.points !== b.points |
253 | | - ? b.points - a.points |
254 | | - : a.played !== b.played |
255 | | - ? b.played - a.played |
256 | | - : b.scoreDifference - a.scoreDifference); |
257 | | - |
258 | | - const rank = { |
259 | | - value: 0, |
260 | | - lastPoints: -1, |
261 | | - }; |
262 | | - |
263 | | - for (const item of ranking) { |
264 | | - item.rank = rank.lastPoints !== item.points ? ++rank.value : rank.value; |
265 | | - rank.lastPoints = item.points; |
266 | | - } |
267 | | - |
268 | | - return ranking; |
269 | | -} |
270 | | - |
271 | 174 | /** |
272 | 175 | * Indicates whether the input is a match. |
273 | 176 | * |
|
0 commit comments