/** * Yea i know but it works for now */ class ResultService { constructor(alertBox, rankingTable, endpoint) { this.alertBox = alertBox; this.rankingTable = rankingTable; this.endpoint = endpoint; this.hash = 0; this.loop(); } async loop() { try { await this.updateTable(); this.alertBox.classList.add("hidden"); } catch (error) { console.log(error); this.hash = 0; this.alertBox.innerHTML = "Noe gikk galt: " + error; this.alertBox.classList.remove("hidden"); } setTimeout(() => { this.loop(); }, 1000); } async updateTable() { let response = await fetch(this.endpoint + this.hash); if (response.status === 204) { return; } let JSONResponse = await response.json(); this.hash = JSONResponse.hash; 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(); let tableHTML = ""; tableHTML += "
# | "; tableHTML += "Lag | "; tableHTML += "Bedrift | "; tableHTML += "Runde | "; tableHTML += "Tid | "; tableHTML += "Passert | "; tableHTML += "
---|---|---|---|---|---|
" + iterator + " | "; tableHTML += "" + row.team.name + " | "; tableHTML += "" + row.team.company + " | "; tableHTML += "" + ((row.round === 0) ? 'START' : row.round) + " | "; tableHTML += "" + ((row.trip === null) ? 'START' : row.trip) + " | "; tableHTML += "" + (new Date(row.time * 1000).toLocaleString()) + " | "; tableHTML += "