From fc18c2ce6d279ac5c6b2ec600ec83233fa8fc4de Mon Sep 17 00:00:00 2001 From: Sune Date: Fri, 9 Feb 2024 21:52:55 +0100 Subject: [PATCH] fix(cups): standings issue with skips --- src/pages/cups/index.jsx | 1 + src/utils/cups.js | 99 ++++++++++++++-------------------------- 2 files changed, 34 insertions(+), 66 deletions(-) diff --git a/src/pages/cups/index.jsx b/src/pages/cups/index.jsx index 676a8519..dd5b4234 100644 --- a/src/pages/cups/index.jsx +++ b/src/pages/cups/index.jsx @@ -40,6 +40,7 @@ const Cups = () => {
Finished Cups
{cupList .filter(c => c.Finished === 1) + .sort((a, b) => b.CupGroupIndex - a.CupGroupIndex) .map(c => ( { - let lastPoints = 0; - let drawPos = 0; - return standings - .map(s => { - const totalEvents = forceSkip ? finishedEvents : cup.Events; - if (s.Events <= totalEvents - cup.Skips) { - return s; - } - let allPoints = s.AllPoints; - let points = s.Points; - let allPointsDetailed = s.AllPointsDetailed; - for (let i = 0; i < s.Events - (totalEvents - cup.Skips); i += 1) { - const min = Math.min(...allPoints); - const removeIndex = allPoints.findIndex(ap => ap === min); - allPoints.splice(removeIndex, 1); - points -= min; - const skippedLevel = allPointsDetailed.find(apd => apd.Points === min); - allPointsDetailed = allPointsDetailed.map(apd => { - if (apd.LevelIndex === skippedLevel.LevelIndex) { - return { ...apd, Skipped: true }; - } - return apd; - }); - } - return { - ...s, - AllPoints: allPoints, - Points: points, - AllPointsDetailed: allPointsDetailed, - }; - }) - .sort((a, b) => b.Points - a.Points) - .map((s, i) => { - let position = i + 1; - if (lastPoints === s.Points) { - if (!drawPos) { - drawPos = i; +const calcSkipStandings = (standings, forceSkip, finishedEvents, cup) => { + return standings.map(s => { + const totalEvents = forceSkip ? finishedEvents : cup.Events; + if (s.Events <= totalEvents - cup.Skips) { + return s; + } + let allPoints = s.AllPoints; + let points = s.Points; + let allPointsDetailed = s.AllPointsDetailed; + for (let i = 0; i < s.Events - (totalEvents - cup.Skips); i += 1) { + const min = Math.min(...allPoints); + const removeIndex = allPoints.findIndex(ap => ap === min); + allPoints.splice(removeIndex, 1); + points -= min; + const skippedLevel = allPointsDetailed.find(apd => apd.Points === min); + allPointsDetailed = allPointsDetailed.map(apd => { + if (apd.LevelIndex === skippedLevel.LevelIndex) { + return { ...apd, Skipped: true }; } - position = drawPos; - } else if (drawPos) { - drawPos = 0; - } - lastPoints = s.Points; - return { - ...s, - Position: s.Position - ? { ...s.Position, [`${eventIndex + 1}`]: position } - : { [`${eventIndex + 1}`]: position }, - FinalPosition: position, - }; - }); + return apd; + }); + } + return { + ...s, + AllPoints: allPoints, + Points: points, + AllPointsDetailed: allPointsDetailed, + }; + }); }; const calcStandings = (standings, eventIndex) => { @@ -367,18 +338,14 @@ export const calculateStandings = (events, cup, simple, forceSkip = false) => { } } }); - if (cup.Skips) { - standings = calcSkipStandings( - standings, - forceSkip, - finishedEvents, - cup, - eventIndex, - ); - } else { + if (eventIndex < completedEvents.length - 1) { standings = calcStandings(standings, eventIndex); } }); + if (cup.Skips) { + standings = calcSkipStandings(standings, forceSkip, finishedEvents, cup); + } + standings = calcStandings(standings, completedEvents.length); return { player: standings, team: teamStandings.sort((a, b) => b.Points - a.Points),