diff --git a/js/statistics.js b/js/statistics.js index d6f237a..efd8d93 100644 --- a/js/statistics.js +++ b/js/statistics.js @@ -6,17 +6,26 @@ function attachRaceStatistics(enhancedLapData, raceData){ var textArr = []; var statisticsPerRow = 3; + var selectionType = { + minimum : "min", + maximum : "max" + }; + // empty statistics Container before adding new Statistics $(statisticsContainer).empty(); console.log(raceData); console.log(enhancedLapData); - textArr.push(avgSymbol + " Pitstop time: " + getAvgPitStopTime(raceData)); - textArr.push(avgSymbol + " Lap time: " + getAvgLapTime(raceData)); - textArr.push(avgSymbol + " Pitstops per Driver: " + getAvgPitStopsPerDriver(raceData)); - textArr.push("Fastest Lap: " + getFastestLapWithDriverCode(raceData)); - textArr.push("Fastest Pitstop: " + getFastestPitStopTimeWithDriverCode(raceData)); + textArr.push(avgSymbol + " Lap time: " + getAvgLapTime(raceData)); + textArr.push(avgSymbol + " Pitstop time: " + getAvgPitStopTime(raceData)); + textArr.push(avgSymbol + " Pitstops per Driver: " + getAvgPitStopsPerDriver(raceData)); + textArr.push("Fastest Lap: " + getFastestOrSlowestLapWithDriverCode(raceData, selectionType.minimum)); + textArr.push("Fastest Pitstop: " + getFastestOrSlowestPitStopTimeWithDriverCode(raceData, selectionType.minimum)); + textArr.push("Most Pitstops: " + getMostPitstops(raceData)); + textArr.push("Slowest Lap: " + getFastestOrSlowestLapWithDriverCode(raceData, selectionType.maximum)); + textArr.push("Slowest Pitstop: " + getFastestOrSlowestPitStopTimeWithDriverCode(raceData, selectionType.maximum)); + textArr.push("") // TODO: Addd more statistics textArr.forEach((elem, i) =>{ @@ -31,75 +40,126 @@ function attachRaceStatistics(enhancedLapData, raceData){ .html(elem) ); }); -} -function getFastestLapWithDriverCode(raceData){ - if(raceData.lapTimes.size > 0){ - var minLapTime = Number.POSITIVE_INFINITY; - var driverCode = ""; - raceData.lapTimes.forEach((lap) => { - lap.forEach((driverInLap) => { - if( driverInLap.milliseconds < minLapTime ){ - minLapTime = Math.min(minLapTime, driverInLap.milliseconds); - driverCode = getDriverCodeById(raceData, driverInLap.driverId); + function getFastestOrSlowestLapWithDriverCode(raceData, selecType){ + if(raceData.lapTimes.size > 0){ + var lapTime; + if(selecType == selectionType.minimum){ + lapTime = Number.POSITIVE_INFINITY; + }else{ + lapTime = 0; + } + var driverCode = ""; + raceData.lapTimes.forEach((lap) => { + lap.forEach((driverInLap) => { + if(selecType == selectionType.minimum){ + if( driverInLap.milliseconds < lapTime ){ + lapTime = Math.min(lapTime, driverInLap.milliseconds); + driverCode = getDriverCodeById(raceData, driverInLap.driverId); + } + }else{ + if( driverInLap.milliseconds > lapTime ){ + lapTime = Math.max(lapTime, driverInLap.milliseconds); + driverCode = getDriverCodeById(raceData, driverInLap.driverId); + } + } + }); + }); + + var returnStr = ((lapTime % 60000) / 1000).toFixed(2) + " " + driverCode; + if(((lapTime % 60000) / 1000).toFixed(2) < 10){ + returnStr = "0" + returnStr; + } + if(Math.floor(lapTime / 60000) > 0){ + returnStr = Math.floor(lapTime / 60000) + ":" + returnStr; + } + return returnStr; + } + } + + function getMostPitstops(raceData){ + if(raceData.pitStops.length > 0){ + var maxStops = 0; + var driverCode = ""; + raceData.pitStops.forEach((current) =>{ + if(current.stop > maxStops){ + maxStops = current.stop; + driverCode = getDriverCodeById(raceData, current.driverId); } }); - }); - - var returnStr = ((minLapTime % 60000) / 1000).toFixed(2) + " " + driverCode; - if(Math.floor(minLapTime / 60000) > 0){ - returnStr = Math.floor(minLapTime / 60000) + ":" + returnStr; + return maxStops + " " + driverCode; } - return returnStr; } -} -function getFastestPitStopTimeWithDriverCode(raceData){ - if(raceData.pitStops.length > 0){ - var minPitStopTime = Number.POSITIVE_INFINITY; - var driverCode = ""; - raceData.pitStops.forEach((current) => { - if( current.milliseconds < minPitStopTime ){ - minPitStopTime = Math.min(minPitStopTime, current.milliseconds); - driverCode = getDriverCodeById(raceData, current.driverId); + function getFastestOrSlowestPitStopTimeWithDriverCode(raceData, selecType){ + if(raceData.pitStops.length > 0){ + var pitStopTime; + if(selecType == selectionType.minimum){ + pitStopTime = Number.POSITIVE_INFINITY; + }else{ + pitStopTime = 0; } - }); - return ((minPitStopTime % 60000) / 1000).toFixed(2) + " " + driverCode; - } -} - - -function getAvgPitStopsPerDriver(raceData){ - if(raceData.pitStops.length > 0){ - return (raceData.pitStops.length / raceData.drivers.length).toFixed(2); - } -} - -function getAvgPitStopTime(raceData){ - if(raceData.pitStops.length > 0){ - var sumAllPitStops = 0; - raceData.pitStops.map((current) => sumAllPitStops += current.milliseconds); - var avgPitStopTimes = sumAllPitStops / raceData.pitStops.length; - return ((avgPitStopTimes % 60000) / 1000).toFixed(2); - } -} - -function getAvgLapTime(raceData){ - if(raceData.lapTimes.size > 0){ - var sumAllLapTimes = 0; - var amountLapsAllDriver = 0; - raceData.lapTimes.forEach((lap, lapInd) => { - lap.forEach((driverInLap, driverInLapInd) => { - sumAllLapTimes += driverInLap.milliseconds; - amountLapsAllDriver++; + var driverCode = ""; + raceData.pitStops.forEach((current) => { + if(selecType == selectionType.minimum){ + if( current.milliseconds < pitStopTime ){ + pitStopTime = Math.min(pitStopTime, current.milliseconds); + driverCode = getDriverCodeById(raceData, current.driverId); + } + }else{ + if( current.milliseconds > pitStopTime ){ + pitStopTime = Math.max(pitStopTime, current.milliseconds); + driverCode = getDriverCodeById(raceData, current.driverId); + } + } }); - }); - var avgRoundTimes = sumAllLapTimes / amountLapsAllDriver; - - var returnStr = ((avgRoundTimes % 60000) / 1000).toFixed(2); - if(Math.floor(avgRoundTimes / 60000) > 0){ - returnStr = Math.floor(avgRoundTimes / 60000) + ":" + returnStr; + var returnStr = ((pitStopTime % 60000) / 1000).toFixed(2) + " " + driverCode; + if(((pitStopTime % 60000) / 1000).toFixed(2) < 10){ + returnStr = "0" + returnStr; + } + if(Math.floor(pitStopTime / 60000) > 0){ + returnStr = Math.floor(pitStopTime / 60000) + ":" + returnStr; + } + return returnStr; } - return returnStr; } + + function getAvgPitStopsPerDriver(raceData){ + if(raceData.pitStops.length > 0){ + return (raceData.pitStops.length / raceData.drivers.length).toFixed(2); + } + } + + function getAvgPitStopTime(raceData){ + if(raceData.pitStops.length > 0){ + var sumAllPitStops = 0; + raceData.pitStops.map((current) => sumAllPitStops += current.milliseconds); + var avgPitStopTimes = sumAllPitStops / raceData.pitStops.length; + return ((avgPitStopTimes % 60000) / 1000).toFixed(2); + } + } + + function getAvgLapTime(raceData){ + if(raceData.lapTimes.size > 0){ + var sumAllLapTimes = 0; + var amountLapsAllDriver = 0; + raceData.lapTimes.forEach((lap, lapInd) => { + lap.forEach((driverInLap, driverInLapInd) => { + sumAllLapTimes += driverInLap.milliseconds; + amountLapsAllDriver++; + }); + }); + var avgRoundTimes = sumAllLapTimes / amountLapsAllDriver; + + var returnStr = ((avgRoundTimes % 60000) / 1000).toFixed(2); + if(((avgRoundTimes % 60000) / 1000).toFixed(2) < 10){ + returnStr = "0" + returnStr; + } + if(Math.floor(avgRoundTimes / 60000) > 0){ + returnStr = Math.floor(avgRoundTimes / 60000) + ":" + returnStr; + } + return returnStr; + } + } + }