From e93c083f2f6b2a320b3809340c9e429d9f19ca47 Mon Sep 17 00:00:00 2001 From: sirsandmann Date: Sat, 30 Dec 2017 13:03:43 +0100 Subject: [PATCH] Added More Statistics. And remove them when new are added. --- js/statistics.js | 73 ++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 70 insertions(+), 3 deletions(-) diff --git a/js/statistics.js b/js/statistics.js index 3e03249..d6f237a 100644 --- a/js/statistics.js +++ b/js/statistics.js @@ -6,10 +6,17 @@ function attachRaceStatistics(enhancedLapData, raceData){ var textArr = []; var statisticsPerRow = 3; - //console.log(raceData); - //console.log(enhancedLapData); + // empty statistics Container before adding new Statistics + $(statisticsContainer).empty(); - textArr.push(avgSymbol + " Pitstop time: " + getAvgPitStopTime(raceData) + " sec"); + 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)); // TODO: Addd more statistics textArr.forEach((elem, i) =>{ @@ -24,8 +31,48 @@ 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); + } + }); + }); + + var returnStr = ((minLapTime % 60000) / 1000).toFixed(2) + " " + driverCode; + if(Math.floor(minLapTime / 60000) > 0){ + returnStr = Math.floor(minLapTime / 60000) + ":" + returnStr; + } + 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); + } + }); + 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){ @@ -36,3 +83,23 @@ function getAvgPitStopTime(raceData){ 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(Math.floor(avgRoundTimes / 60000) > 0){ + returnStr = Math.floor(avgRoundTimes / 60000) + ":" + returnStr; + } + return returnStr; + } +}