This repository has been archived on 2023-01-06. You can view files and clone it, but cannot push or open issues or pull requests.
web/public/static/js/ResultService.js

122 lines
2.9 KiB
JavaScript
Raw Normal View History

2022-05-08 20:00:11 +00:00
/**
* Yea i know but it works for now
*/
class ResultService {
constructor(alertBox, rankingTable, endpoint) {
this.alertBox = alertBox;
2022-04-26 19:55:10 +00:00
this.rankingTable = rankingTable;
2022-05-08 20:00:11 +00:00
this.endpoint = endpoint;
2022-04-26 19:55:10 +00:00
this.hash = 0;
2022-05-08 20:00:11 +00:00
2022-04-26 13:00:38 +00:00
this.loop();
}
2022-05-08 20:00:11 +00:00
async loop() {
2022-04-26 13:00:38 +00:00
try {
2022-05-08 20:00:11 +00:00
2022-04-26 13:00:38 +00:00
await this.updateTable();
2022-05-08 20:00:11 +00:00
2022-04-26 19:55:10 +00:00
this.alertBox.classList.add("hidden");
2022-05-08 20:00:11 +00:00
2022-04-26 13:00:38 +00:00
} catch (error) {
2022-05-08 20:00:11 +00:00
2022-04-26 13:00:38 +00:00
console.log(error);
2022-05-08 20:00:11 +00:00
this.hash = 0;
2022-04-26 19:55:10 +00:00
this.alertBox.innerHTML = "<b>Noe gikk galt: </b>" + error;
this.alertBox.classList.remove("hidden");
2022-04-26 13:00:38 +00:00
}
setTimeout(() => {
this.loop();
}, 1000);
}
2022-05-08 20:00:11 +00:00
async updateTable() {
2022-04-26 13:00:38 +00:00
let response = await fetch(this.endpoint + this.hash);
2022-05-08 20:00:11 +00:00
if (response.status === 204) {
2022-04-26 13:00:38 +00:00
return;
}
2022-05-08 20:00:11 +00:00
let JSONResponse = await response.json();
2022-04-26 13:00:38 +00:00
2022-05-08 20:00:11 +00:00
this.hash = JSONResponse.hash;
2022-04-26 13:00:38 +00:00
2022-05-08 20:00:11 +00:00
let tableRows = [];
let tempLaps = [];
JSONResponse.data.laps.forEach(lap => {
let roundTripTime = null;
let prevLap = tempLaps[lap.id];
let round = 0;
if (prevLap) {
roundTripTime = lap.time - prevLap.time;
prevLap.round += 1;
lap.round = prevLap.round;
round = lap.round;
} else {
lap.round = 0;
}
tempLaps[lap.id] = lap;
tableRows.push({
team: {
name: JSONResponse.data.map.team[lap.id].name,
company: JSONResponse.data.map.team[lap.id].company
},
trip: roundTripTime,
time: lap.time + JSONResponse.data.map.time_reference,
round: round
});
});
tableRows = tableRows.reverse();
2022-04-26 13:00:38 +00:00
let tableHTML = "";
tableHTML += "<table>";
tableHTML += "<tr>";
2022-04-26 19:55:10 +00:00
tableHTML += "<th>#</th>";
2022-04-26 13:00:38 +00:00
tableHTML += "<th>Lag</th>";
2022-04-27 10:57:26 +00:00
tableHTML += "<th>Bedrift</th>";
2022-05-08 20:00:11 +00:00
tableHTML += "<th>Runde</th>";
2022-04-26 13:00:38 +00:00
tableHTML += "<th>Tid</th>";
2022-05-08 20:00:11 +00:00
tableHTML += "<th>Passert</th>";
2022-04-26 13:00:38 +00:00
tableHTML += "</tr>";
2022-05-08 20:00:11 +00:00
let iterator = tableRows.length + 1;
tableRows.forEach(row => {
iterator--;
2022-04-26 21:20:32 +00:00
2022-04-26 13:00:38 +00:00
tableHTML += "<tr>";
2022-04-26 19:55:10 +00:00
tableHTML += "<td>" + iterator + "</td>";
2022-05-08 20:00:11 +00:00
tableHTML += "<td>" + row.team.name + "</td>";
tableHTML += "<td>" + row.team.company + "</td>";
tableHTML += "<td>" + ((row.round === 0) ? 'START' : row.round) + "</td>";
tableHTML += "<td>" + ((row.trip === null) ? 'START' : row.trip) + "</td>";
tableHTML += "<td>" + (new Date(row.time * 1000).toLocaleString()) + "</td>";
2022-04-26 13:00:38 +00:00
tableHTML += "</tr>";
});
tableHTML += "<table>";
2022-04-26 19:55:10 +00:00
this.rankingTable.innerHTML = tableHTML;
2022-04-26 13:00:38 +00:00
}
2022-05-08 20:00:11 +00:00
2022-04-26 13:00:38 +00:00
}