Skip to content

Commit

Permalink
reloaded
Browse files Browse the repository at this point in the history
  • Loading branch information
nalbam committed Aug 30, 2019
1 parent 6bff632 commit ed481c8
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 14 deletions.
61 changes: 48 additions & 13 deletions static/league.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,37 +7,62 @@ let lb_items = document.querySelector('.lb-items');

let items = [];

function clear() {
while (lb_items.lastChild) {
lb_items.removeChild(lb_items.lastChild);
}
}

function reload() {
let url = '/leaderboard/' + league;
console.log(`reload ${url}`);
// console.log(`reload ${url}`);
$.ajax({
url: url,
type: 'get',
success: function (res, status) {
if (res) {
reloaded(res);
print(res);
}
}
});
}

function print(res) {
items = res;
function reloaded(res) {
let isNewRacer = false;
if (items.length > 0 && items.length !== res.items.length) {
isNewRacer = true;
}

clear();
let newRecordPos;
let newRecordName;
let newRecordTime;

addRow('lb-header', 'Position', 'Name', 'Time')
res.items.sort(compare);

for (let i = 0; i < items.length; i++) {
if (items[i].lapTime !== res.items[i].lapTime) {
newRecordPos = i + 1;
newRecordName = res.items[i].racerName;
newRecordTime = res.items[i].lapTime;
break;
}
}

if (isNewRacer && !newRecordName) {
let j = res.items.length - 1;
newRecordPos = res.items.length;
newRecordName = res.items[j].racerName;
newRecordTime = res.items[j].lapTime;
}

if (isNewRacer || newRecordName) {
console.log(`new ${isNewRacer} ${newRecordPos} ${newRecordName} ${newRecordTime}`);
}

items.sort(compare);
items = res.items;
}

function print(res) {
clear(res.title);
addRow('lb-header', 'Position', 'Name', 'Time')

let pos = 0;
items.forEach(function (item) {
res.items.forEach(function (item) {
pos++;
addRow('lb-row', pos, item.racerName, item.lapTime);
});
Expand All @@ -59,6 +84,13 @@ function sec(t) {
return ((+a[0]) * 60) + (+a[1]);
}

function clear(title) {
lb_title.innerText = title;
while (lb_items.lastChild) {
lb_items.removeChild(lb_items.lastChild);
}
}

function addRow(className, position, racerName, lapTime) {
let row = document.createElement('div');
row.classList.add(className);
Expand All @@ -82,4 +114,7 @@ socket.on('league', function (name) {

$(function () {
reload();
setInterval(function () {
reload();
}, 30000);
});
2 changes: 1 addition & 1 deletion views/league.ejs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
</div>
</figure>

<h1 class="lb-title">AWS DeepRacer League</h1>
<h1 class="lb-title"></h1>

<div class="lb-container">
<div class="lb-items lb-initial">
Expand Down

0 comments on commit ed481c8

Please sign in to comment.