2022-04-26 13:00:38 +00:00
|
|
|
class ResultService
|
|
|
|
{
|
2022-04-26 19:55:10 +00:00
|
|
|
constructor(alertBox, rankingTable, endpoint)
|
2022-04-26 13:00:38 +00:00
|
|
|
{
|
2022-04-26 21:20:32 +00:00
|
|
|
this.alertBox = alertBox;
|
2022-04-26 19:55:10 +00:00
|
|
|
this.rankingTable = rankingTable;
|
2022-04-26 21:20:32 +00:00
|
|
|
this.endpoint = endpoint;
|
2022-04-26 19:55:10 +00:00
|
|
|
this.hash = 0;
|
2022-04-26 13:00:38 +00:00
|
|
|
this.loop();
|
|
|
|
}
|
|
|
|
|
|
|
|
async loop()
|
|
|
|
{
|
|
|
|
try {
|
|
|
|
await this.updateTable();
|
2022-04-26 19:55:10 +00:00
|
|
|
this.alertBox.classList.add("hidden");
|
2022-04-26 13:00:38 +00:00
|
|
|
} catch (error) {
|
|
|
|
console.log(error);
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
async updateTable()
|
|
|
|
{
|
|
|
|
let response = await fetch(this.endpoint + this.hash);
|
|
|
|
|
|
|
|
if (response.status === 204)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
let json = await response.json();
|
|
|
|
|
|
|
|
this.hash = json.hash;
|
|
|
|
|
|
|
|
let data = json.data;
|
2022-04-26 21:20:32 +00:00
|
|
|
data.times = data.times.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-04-26 13:00:38 +00:00
|
|
|
tableHTML += "<th>Tid</th>";
|
2022-04-27 10:57:26 +00:00
|
|
|
tableHTML += "<th>Runder</th>";
|
2022-04-26 13:00:38 +00:00
|
|
|
tableHTML += "</tr>";
|
|
|
|
|
2022-04-26 19:55:10 +00:00
|
|
|
let iterator = 0;
|
2022-04-26 21:20:32 +00:00
|
|
|
data.times.forEach(element => {
|
2022-04-26 19:55:10 +00:00
|
|
|
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-04-27 10:57:26 +00:00
|
|
|
tableHTML += "<td>" + data.map.team[element.id].name + "</td>";
|
|
|
|
tableHTML += "<td>" + data.map.team[element.id].company + "</td>";
|
2022-04-26 13:00:38 +00:00
|
|
|
tableHTML += "<td>" + element.time + "</td>";
|
2022-04-27 10:57:26 +00:00
|
|
|
tableHTML += "<td>" + data.map.team[element.id].rounds+ "</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
|
|
|
}
|
|
|
|
}
|