From 4074e268b28ea77a7f93facc85d49176f6af1d9f Mon Sep 17 00:00:00 2001 From: Dorian Zedler Date: Thu, 28 Jul 2022 18:32:35 +0200 Subject: [PATCH] Fix: Allow progressive results --- src/data/DigitalrockApi.tsx | 4 +- src/data/SpeedFlowchart.tsx | 100 ++++++++++++++++++++---------------- 2 files changed, 57 insertions(+), 47 deletions(-) diff --git a/src/data/DigitalrockApi.tsx b/src/data/DigitalrockApi.tsx index 2738e2d..c10fd36 100644 --- a/src/data/DigitalrockApi.tsx +++ b/src/data/DigitalrockApi.tsx @@ -14,8 +14,8 @@ interface CompetitionList { * A class for dealing with the digitalrock api */ export class DigitalrockAPi { - private BASE_URL = 'https://www.digitalrock.de/egroupware/ranking/json.php?'; - // private BASE_URL = '/test.json?'; + // private BASE_URL = 'https://www.digitalrock.de/egroupware/ranking/json.php?'; + private BASE_URL = '/test.json?'; /** * function to get competitions diff --git a/src/data/SpeedFlowchart.tsx b/src/data/SpeedFlowchart.tsx index 80bf56a..be352f4 100644 --- a/src/data/SpeedFlowchart.tsx +++ b/src/data/SpeedFlowchart.tsx @@ -57,7 +57,6 @@ function getRoundName( */ function getRoundRank(name: string): number { const match = name.match(/1\/([842])/); - console.log(match); if (match === undefined || match === null || match.length !== 2) { return 2; } @@ -76,6 +75,8 @@ function computeWinnerOfPair(pair: SpeedRoundPair, roundIndex: number) { ) return; + console.log('Conputing winner of round ', roundIndex); + pair.laneA.result = pair.laneA.participant.results[roundIndex]; pair.laneB.result = pair.laneB.participant.results[roundIndex]; @@ -99,9 +100,11 @@ function getWinnerOfPair( roundNumber: number, ): Participant | undefined { computeWinnerOfPair(pair, roundNumber); - return pair.winner === 'A' - ? pair.laneA?.participant - : pair.laneB?.participant; + return { + ['A']: pair.laneA?.participant, + ['B']: pair.laneB?.participant, + ['']: undefined, + }[pair.winner ?? '']; } /** @@ -115,9 +118,11 @@ function getLooserOfPair( roundNumber: number, ): Participant | undefined { computeWinnerOfPair(pair, roundNumber); - return pair.winner === 'A' - ? pair.laneB?.participant - : pair.laneA?.participant; + return { + ['A']: pair.laneB?.participant, + ['B']: pair.laneA?.participant, + ['']: undefined, + }[pair.winner ?? '']; } /** @@ -125,6 +130,7 @@ function getLooserOfPair( * @param {number} roundIndex index of the new round * @param {string} roundName name of the new round * @param {SpeedRound} previousRound + * @param {number} roundRank * @param {boolean} takeLooser * @return {SpeedRound} */ @@ -132,38 +138,37 @@ function computeRoundFromPreviousRound( roundIndex: number, roundName: string, previousRound: SpeedRound, + roundRank: number, takeLooser = false, ): SpeedRound { const getAdvancingParticipant = takeLooser ? getLooserOfPair : getWinnerOfPair; - const nextRoundPairs = new Array(previousRound.pairs.length / 2) - .fill(0) - .map((_, i) => { - const laneAParticipant = getAdvancingParticipant( - previousRound.pairs[i * 2], - previousRound.roundIndex, - ); - const laneBParticipant = getAdvancingParticipant( - previousRound.pairs[i * 2 + 1], - previousRound.roundIndex, - ); + const nextRoundPairs = new Array(roundRank / 2).fill(0).map((_, i) => { + const laneAParticipant = getAdvancingParticipant( + previousRound.pairs[i * 2], + previousRound.roundIndex, + ); + const laneBParticipant = getAdvancingParticipant( + previousRound.pairs[i * 2 + 1], + previousRound.roundIndex, + ); - return { - laneA: - laneAParticipant === undefined - ? undefined - : { - participant: laneAParticipant, - }, - laneB: - laneBParticipant === undefined - ? undefined - : { - participant: laneBParticipant, - }, - }; - }); + return { + laneA: + laneAParticipant === undefined + ? undefined + : { + participant: laneAParticipant, + }, + laneB: + laneBParticipant === undefined + ? undefined + : { + participant: laneBParticipant, + }, + }; + }); return { pairs: nextRoundPairs, @@ -190,16 +195,14 @@ export function convertResultsToSpeedFlowchartResult( .map(number => parseInt(number)) .filter(number => number > 0); - console.log(`Have final rounds:`, roundIndices); - // process first round const firstRoundName = getRoundName(roundIndices[0], result.route_names); - const firstRoundNumber = getRoundRank( + const firstRoundRank = getRoundRank( getRoundName(roundIndices[0], result.route_names) ?? '', ); const getOpponent = (ofRank: number): Participant => { - return convertedParticipants[firstRoundNumber * 2 - 1 - ofRank]; + return convertedParticipants[firstRoundRank * 2 - 1 - ofRank]; }; // Should be: @@ -213,8 +216,7 @@ export function convertResultsToSpeedFlowchartResult( [1, 2], [1, 4, 2, 3], [1, 8, 4, 5, 2, 7, 3, 6], - ][firstRoundNumber / 4]; - console.log(ranksOfLaneAInOrder); + ][firstRoundRank / 4]; const firstRoundPairs = ranksOfLaneAInOrder.map(rank => { return { @@ -231,33 +233,41 @@ export function convertResultsToSpeedFlowchartResult( rounds.push(firstRound); + console.log(firstRound); + // compute following rounds - for (let i = 1; i < roundIndices.length - 2; i++) { + let roundIndex = roundIndices[1]; + for (let roundRank = firstRoundRank; roundRank > 2; roundRank /= 2) { + console.log('Processing rank', roundRank); rounds.push( computeRoundFromPreviousRound( - roundIndices[i], - result.route_names[roundIndices[i]] ?? '', - rounds[i - 1], + roundIndex, + result.route_names[roundIndex] ?? '', + rounds[rounds.length - 1], + roundRank, ), ); + roundIndex++; } // compute final and semi final - const semifinalRoundIndex = roundIndices[roundIndices.length - 2]; + const semifinalRoundIndex = roundIndex++; const semifinal = computeRoundFromPreviousRound( semifinalRoundIndex, result.route_names[semifinalRoundIndex] ?? '', rounds[rounds.length - 1], + 2, true, ); computeWinnerOfPair(semifinal.pairs[0], semifinalRoundIndex); rounds.push(semifinal); - const finalRoundIndex = roundIndices[roundIndices.length - 1]; + const finalRoundIndex = roundIndex; const final = computeRoundFromPreviousRound( finalRoundIndex, result.route_names[finalRoundIndex] ?? '', rounds[rounds.length - 2], + 2, ); computeWinnerOfPair(final.pairs[0], finalRoundIndex); rounds.push(final);