2017-11-16 23:08:14 +01:00
|
|
|
"use strict";
|
|
|
|
|
2017-12-11 01:26:27 +01:00
|
|
|
/*
|
2017-11-16 23:08:14 +01:00
|
|
|
* This file contains utility functions, data structures and
|
|
|
|
* stuff not related to what this project actually does.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* Define global structure for the loading dialog */
|
|
|
|
var loadingDialog = {
|
|
|
|
id: "#loading-dialog",
|
|
|
|
progressItems: 1,
|
|
|
|
progressItemsDone: 0,
|
|
|
|
// Initialize+Show the loading dialog with a number of items to progress
|
|
|
|
show: function(progressItems = 1) {
|
2017-12-01 14:04:32 +01:00
|
|
|
console.log("show");
|
2017-11-16 23:08:14 +01:00
|
|
|
this.progressItems = progressItems;
|
|
|
|
this.progressItemsDone = 0;
|
|
|
|
this._updateProgressBar();
|
2017-12-17 10:38:48 +01:00
|
|
|
//$(this.id).modal('show');
|
2017-11-16 23:08:14 +01:00
|
|
|
},
|
|
|
|
// Function to signal that another item was progressed
|
|
|
|
itemFinished: function() {
|
2017-12-01 14:04:32 +01:00
|
|
|
|
2017-11-16 23:08:14 +01:00
|
|
|
this.progressItemsDone++;
|
|
|
|
this._updateProgressBar();
|
|
|
|
},
|
|
|
|
// Hide the dialog
|
|
|
|
hide: function() {
|
2017-12-01 14:04:32 +01:00
|
|
|
console.log("hide");
|
2017-12-17 10:38:48 +01:00
|
|
|
//$(this.id).modal('hide');
|
2017-11-16 23:08:14 +01:00
|
|
|
},
|
|
|
|
// Private function to update the progress bar shown
|
|
|
|
_updateProgressBar: function() {
|
|
|
|
var percentage = (this.progressItemsDone / this.progressItems) * 100;
|
|
|
|
if(percentage < 0 || isNaN(percentage)) percentage = 0;
|
|
|
|
if(percentage > 100) percentage = 100;
|
2017-12-11 01:26:27 +01:00
|
|
|
$(this.id + " .progress-bar").attr("style", "width: " + percentage + "%;");
|
2017-11-16 23:08:14 +01:00
|
|
|
},
|
|
|
|
};
|
2017-12-11 01:26:27 +01:00
|
|
|
|
|
|
|
// transforms the raceData to a format, with which lineDataDefinition can work
|
2017-12-12 21:20:09 +01:00
|
|
|
function transformRaceDataToLineData(raceData){
|
2017-12-11 01:26:27 +01:00
|
|
|
// define the lines
|
|
|
|
var lineData = [];
|
|
|
|
raceData.drivers.forEach((driver, drivIn)=>{
|
|
|
|
lineData.push();
|
|
|
|
var lapsOfDriverInLineDataFormat = [];
|
2017-12-12 21:20:09 +01:00
|
|
|
lapsOfDriverInLineDataFormat.push({'lap': 0, 'position': getPositionOfQualifying(raceData, driver) });
|
2017-12-11 01:26:27 +01:00
|
|
|
raceData.lapTimes.forEach((lap, lapIn) => {
|
2017-12-12 21:20:09 +01:00
|
|
|
var drivPos = processor.getPositionOfDriver(driver, lap, raceData.drivers.length + 1 );
|
|
|
|
if( drivPos < raceData.drivers.length + 1 ){
|
2017-12-13 00:12:11 +01:00
|
|
|
lapsOfDriverInLineDataFormat.push({ 'lap': lapIn, 'position': processor.getPositionOfDriver(driver, lap, raceData.drivers.length + 1 ), 'driverId': driver.driverId});
|
2017-12-12 21:20:09 +01:00
|
|
|
}
|
2017-12-11 01:26:27 +01:00
|
|
|
});
|
|
|
|
lineData.splice(drivIn, 0, lapsOfDriverInLineDataFormat);
|
|
|
|
});
|
|
|
|
return lineData;
|
|
|
|
}
|
2017-12-12 21:20:09 +01:00
|
|
|
|
2017-12-13 00:12:11 +01:00
|
|
|
function transformPitStopDataToPointData(raceData){
|
|
|
|
var pointData = [];
|
|
|
|
raceData.pitStops.forEach(pitStop => {
|
2017-12-15 14:00:44 +01:00
|
|
|
|
|
|
|
var randomData = raceData['lapTimes'].get(pitStop.lap);
|
|
|
|
var position = null; //TODO
|
|
|
|
for(var i = 0; i< randomData.length;i++){
|
|
|
|
if(randomData[i].driverId == pitStop.driverId){
|
|
|
|
position = randomData[i].position;
|
2017-12-16 01:18:24 +01:00
|
|
|
break;
|
2017-12-15 14:00:44 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
//console.log(position);
|
|
|
|
pointData.push({'position': position, "lap": pitStop.lap});
|
2017-12-13 00:12:11 +01:00
|
|
|
});
|
|
|
|
return pointData;
|
|
|
|
}
|
|
|
|
|
|
|
|
// eigentlich war für TickData gedacht
|
|
|
|
function getDriverCodeAndPositionArray(raceData, lapNumber){
|
|
|
|
var posDriverCode = [];
|
|
|
|
if(lapNumber == 0){
|
|
|
|
raceData.qualifying.forEach(qualData => {
|
|
|
|
posDriverCode.push(getDriverCodeById(raceData, qualData.driverId) + " " + qualData.position)
|
|
|
|
});
|
|
|
|
}
|
|
|
|
return posDriverCode;
|
|
|
|
}
|
|
|
|
|
2017-12-19 21:35:47 +01:00
|
|
|
function getDriverCodeById(raceData, driverId){
|
|
|
|
return raceData.drivers.filter(driv => driv.driverId == driverId)[0].code;
|
2017-12-13 00:12:11 +01:00
|
|
|
}
|
|
|
|
|
2017-12-19 21:35:47 +01:00
|
|
|
function removeDuplicates(result,obj){
|
2017-12-17 10:38:48 +01:00
|
|
|
if (result.indexOf(obj) < 0 ) result.push(obj);
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2017-12-28 14:59:07 +01:00
|
|
|
function getValidEndingStatusIds(){
|
|
|
|
var results = [];
|
|
|
|
var allStatus = queries.getStatus();
|
|
|
|
results.push(1);
|
|
|
|
for(var key in allStatus){
|
|
|
|
if(key === undefined) continue;
|
|
|
|
if(allStatus[key].status.match(/^\+[0-9]+/g)){
|
|
|
|
results.push(parseInt(key));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return results;
|
|
|
|
}
|
|
|
|
|
2017-12-12 21:20:09 +01:00
|
|
|
function getColorValue(index, all){
|
|
|
|
var r = 0;
|
|
|
|
var g = 0;
|
|
|
|
var b = 0;
|
|
|
|
|
|
|
|
var step = 255*3 / all;
|
|
|
|
var colorValue = index * step;
|
|
|
|
|
|
|
|
return "hsl(" + colorValue + ", " + 100 + "%, 35% )";
|
|
|
|
}
|
2017-12-29 16:55:54 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
|
|
data - own user data
|
|
|
|
pagename - the page where the image will be taken from
|
|
|
|
imagesize - target image size
|
|
|
|
callback(data,imageURL) - callback that will be called. Arguments are the provided
|
|
|
|
user data (data) and the final imageURL.
|
|
|
|
|
|
|
|
**/
|
|
|
|
function getImageFromWikipedia(data,pageName,imagesize,callback){
|
|
|
|
$.ajax({
|
|
|
|
url: "https://en.wikipedia.org/w/api.php",
|
|
|
|
data: {
|
|
|
|
format: "json",
|
|
|
|
action: "query",
|
|
|
|
titles: pageName,
|
|
|
|
prop:"pageimages",
|
|
|
|
pithumbsize:imagesize,
|
|
|
|
},
|
|
|
|
dataType: 'jsonp',
|
|
|
|
success: function (dataResult) {
|
|
|
|
for(var key in dataResult.query.pages){
|
|
|
|
var d = dataResult.query.pages[key];
|
|
|
|
callback(data,d.thumbnail.source);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
};
|
2017-12-30 00:46:43 +01:00
|
|
|
|
|
|
|
function renderRaceInfoBox(race) {
|
|
|
|
var raceInfo = race.raceInfo;
|
2017-12-30 01:09:10 +01:00
|
|
|
var circuit = preprocessor.getResults().circuits[raceInfo.circuitId];
|
2017-12-30 00:46:43 +01:00
|
|
|
//console.log(raceInfo);
|
|
|
|
var content = "";
|
|
|
|
|
2017-12-30 11:49:30 +01:00
|
|
|
content = "<h1 data-toggle=\"tooltip\" data-placement=\"top\" title=\"#"+raceInfo.raceId+"\">"+raceInfo.name+" "+raceInfo.date.getFullYear()+"</h1>";
|
2017-12-30 01:09:10 +01:00
|
|
|
content += "<h5>"+circuit.name+" ("+circuit.location+", "+circuit.country+")</h5>";
|
|
|
|
content += "<h6>"+raceInfo.date.toLocaleDateString("de-DE")+"</h6>"
|
|
|
|
content += "<div class=\"text-right\">";
|
|
|
|
content += "<a class=\"btn btn-primary\" target=\"_blank\" href=\""+raceInfo.url+"\" role=\"button\">See Race on Wikipedia</a> ";
|
2017-12-30 11:31:54 +01:00
|
|
|
content += "<a class=\"btn btn-primary\" target=\"_blank\" href=\""+circuit.url+"\" role=\"button\">See Circuit on Wikipedia</a> ";
|
|
|
|
content += "<a class=\"btn btn-primary\" target=\"_blank\" href=\"https://www.google.com/maps/?q="+circuit.lat+","+circuit.lng+"\" role=\"button\">See Circuit on GoogleMaps</a>";
|
2017-12-30 01:09:10 +01:00
|
|
|
content += "</div>";
|
2017-12-30 00:46:43 +01:00
|
|
|
return content;
|
|
|
|
}
|
2017-12-30 01:16:47 +01:00
|
|
|
|
|
|
|
function renderDriverInfoBox(race) {
|
|
|
|
var raceInfo = race.raceInfo;
|
2017-12-30 01:40:59 +01:00
|
|
|
var drivers = race.drivers;
|
2017-12-30 11:47:25 +01:00
|
|
|
var statusData = preprocessor.getResults().status
|
2017-12-30 02:04:15 +01:00
|
|
|
// Assign results to drivers
|
|
|
|
for(var ri in race.results) {
|
|
|
|
var driverResult = race.results[ri];
|
|
|
|
for(var di in drivers) {
|
|
|
|
var driver = drivers[di];
|
|
|
|
// :-(
|
|
|
|
if(driverResult.driverId == driver.driverId) {
|
|
|
|
Object.assign(drivers[di], driverResult);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
var content = "";
|
2017-12-30 01:40:59 +01:00
|
|
|
// Table header
|
2017-12-30 01:48:44 +01:00
|
|
|
content += "<table id=\"driver-table\" class=\"table table-striped table-bordered\">";
|
2017-12-30 01:40:59 +01:00
|
|
|
content += "<thead>";
|
|
|
|
content += "<tr>";
|
2017-12-30 02:04:15 +01:00
|
|
|
content += "<th scope=\"col\">Rank</th>";
|
2017-12-30 01:40:59 +01:00
|
|
|
content += "<th scope=\"col\">Code</th>";
|
2017-12-30 02:04:15 +01:00
|
|
|
content += "<th scope=\"col\">Points</th>";
|
2017-12-30 11:47:25 +01:00
|
|
|
content += "<th scope=\"col\">Fastest Lap Speed</th>";
|
|
|
|
content += "<th scope=\"col\">Status</th>";
|
2017-12-30 01:40:59 +01:00
|
|
|
content += "<th scope=\"col\">Forename</th>";
|
|
|
|
content += "<th scope=\"col\">Surname</th>";
|
|
|
|
content += "<th scope=\"col\">Nationality</th>";
|
|
|
|
content += "<th scope=\"col\">Birthday</th>";
|
|
|
|
content += "<th scope=\"col\">Wikipedia</th>";
|
|
|
|
content += "</tr>";
|
|
|
|
content += "</thead>";
|
|
|
|
|
|
|
|
// Table body
|
|
|
|
content += "<tbody>";
|
|
|
|
for(var di in drivers) {
|
|
|
|
var driver = drivers[di];
|
2017-12-30 02:04:15 +01:00
|
|
|
// Replace NaN with something proper
|
|
|
|
if(isNaN(driver.position)) {
|
|
|
|
driver.position = "-/-";
|
|
|
|
}
|
2017-12-30 11:47:25 +01:00
|
|
|
driver.sortableRank = driver.rank;
|
|
|
|
if(driver.sortableRank < 1) driver.sortableRank = 999;
|
|
|
|
if(isNaN(driver.fastestLapSpeed)) driver.fastestLapSpeed = 0;
|
|
|
|
//console.log(driver);
|
2017-12-30 01:40:59 +01:00
|
|
|
content += "<tr>";
|
2017-12-30 11:47:25 +01:00
|
|
|
content += "<th scope=\"row\" data-sort=\""+driver.sortableRank+"\">"+driver.rank+"</th>";
|
2017-12-30 01:40:59 +01:00
|
|
|
content += "<td>"+driver.code+"</td>";
|
2017-12-30 02:04:15 +01:00
|
|
|
content += "<td>"+driver.points+"</td>";
|
2017-12-30 11:47:25 +01:00
|
|
|
content += "<td>"+driver.fastestLapSpeed+"</td>";
|
|
|
|
content += "<td>"+statusData[driver.statusId].status+"</td>";
|
2017-12-30 01:40:59 +01:00
|
|
|
content += "<td>"+driver.forename+"</td>";
|
|
|
|
content += "<td>"+driver.surname+"</td>";
|
|
|
|
content += "<td>"+driver.nationality+"</td>";
|
|
|
|
content += "<td>"+driver.dob.toLocaleDateString("de-DE")+"</td>";
|
|
|
|
content += "<td><a class=\"btn btn-primary\" target=\"_blank\" href=\""+driver.url+"\" role=\"button\">Wikipedia</a></td>";
|
|
|
|
content += "</tr>";
|
|
|
|
}
|
|
|
|
content += "</tbody>";
|
|
|
|
content += "</table>";
|
|
|
|
|
2017-12-30 01:16:47 +01:00
|
|
|
return content;
|
|
|
|
}
|