2020-12-29 00:48:50 +00:00
|
|
|
$(document).ready(function (){
|
|
|
|
var socket = io.connect();
|
2020-12-31 22:56:08 +00:00
|
|
|
var lastupdate;
|
2020-12-29 00:48:50 +00:00
|
|
|
var grid = document.getElementById("container");
|
|
|
|
|
|
|
|
|
|
|
|
socket.on('connect', function () {
|
|
|
|
socket.emit('recieved', { data: 'I\'m connected!' });
|
|
|
|
});
|
|
|
|
|
|
|
|
socket.on("states_update", function (json) { //json is an object containing all game updates
|
2020-12-31 22:56:08 +00:00
|
|
|
lastupdate = json;
|
|
|
|
updateGames(json, $('#selected_filter').text());
|
|
|
|
|
2021-01-01 08:32:47 +00:00
|
|
|
console.log(json)
|
2020-12-31 22:56:08 +00:00
|
|
|
//get all leagues
|
|
|
|
leagues = []
|
2021-01-01 08:32:47 +00:00
|
|
|
for (var i in json) {
|
|
|
|
if (json[i].league != "" && !leagues.includes(json[i].league)) {
|
|
|
|
leagues.push(json[i].league)
|
2020-12-31 22:56:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//remove leagues no longer present
|
|
|
|
$('#filters .filter').each(function(index) {
|
|
|
|
if (!leagues.includes($(this).text())) {
|
2021-01-01 01:19:57 +00:00
|
|
|
if ($(this).attr('id') != 'selected_filter' && $(this).text() != "All") { //don't remove the currently selected filter or the "all" filter
|
2020-12-31 22:56:08 +00:00
|
|
|
$(this).remove();
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
leagues.splice(leagues.indexOf($(this).text()), 1);
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
// add leagues not already present
|
2021-01-01 08:32:47 +00:00
|
|
|
for (var i in leagues) { // we removed the entries that are already there in the loop above
|
|
|
|
$('#filters').append("<button class='filter'>"+leagues[i]+"</button>");
|
2020-12-31 22:56:08 +00:00
|
|
|
}
|
2021-01-01 01:19:57 +00:00
|
|
|
|
|
|
|
//add click handlers to each filter
|
|
|
|
$('#filters .filter').each(function(index) {
|
|
|
|
$(this).click(function() {
|
|
|
|
if ($('#filters #selected_filter').text() == 'All') {
|
|
|
|
updateGames(Object(), ""); // clear grid when switching off of All, to make games collapse to top
|
|
|
|
}
|
|
|
|
$('#filters #selected_filter').attr('id', '');
|
|
|
|
$(this).attr('id', 'selected_filter');
|
|
|
|
updateGames(lastupdate, $(this).text());
|
|
|
|
})
|
|
|
|
})
|
2020-12-31 22:56:08 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
const updateGames = (json, filter) => {
|
2021-01-01 08:32:47 +00:00
|
|
|
filterjson = [];
|
|
|
|
for (var i in json) {
|
|
|
|
if (json[i].league == filter || filter == "All") {
|
|
|
|
filterjson.push(json[i]);
|
2020-12-31 22:56:08 +00:00
|
|
|
}
|
|
|
|
}
|
2020-12-30 20:10:29 +00:00
|
|
|
|
2021-01-01 08:32:47 +00:00
|
|
|
console.log(filterjson);
|
|
|
|
|
|
|
|
if (filterjson.length == 0) {
|
2020-12-30 20:10:29 +00:00
|
|
|
$('#footer div').html("No games right now. Why not head over to Discord and start one?");
|
|
|
|
} else {
|
|
|
|
$('#footer div').html("");
|
|
|
|
}
|
|
|
|
|
2021-01-01 01:19:57 +00:00
|
|
|
//replace games that have ended with empty slots
|
|
|
|
for (var slotnum = 0; slotnum < grid.children.length; slotnum++) {
|
2021-01-01 08:32:47 +00:00
|
|
|
if (grid.children[slotnum].className == "game" && !filterjson.some((x) => x.timestamp == grid.children[slotnum].timestamp)) {
|
2021-01-01 01:19:57 +00:00
|
|
|
grid.children[slotnum].className = "emptyslot";
|
|
|
|
grid.children[slotnum].timestamp = null;
|
|
|
|
grid.children[slotnum].innerHTML = "";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-01 08:32:47 +00:00
|
|
|
for (var i in filterjson) {
|
2020-12-31 22:56:08 +00:00
|
|
|
//adds game to list if not there already
|
2021-01-01 08:32:47 +00:00
|
|
|
if (!Array.prototype.slice.call(grid.children).some((x) => x.timestamp == filterjson[i].timestamp)) {
|
2020-12-31 06:33:03 +00:00
|
|
|
for (var slotnum = 0; true; slotnum++) { //this is really a while loop but shh don't tell anyone
|
2020-12-30 08:50:29 +00:00
|
|
|
if (slotnum >= grid.children.length) {
|
|
|
|
for (var i = 0; i < 3; i ++) {
|
2020-12-30 23:53:15 +00:00
|
|
|
insertEmpty(grid);
|
2020-12-30 08:50:29 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if (grid.children[slotnum].className == "emptyslot") {
|
2021-01-01 08:32:47 +00:00
|
|
|
insertGame(slotnum, filterjson[i]);
|
2020-12-29 00:48:50 +00:00
|
|
|
break;
|
|
|
|
};
|
|
|
|
};
|
2021-01-01 01:19:57 +00:00
|
|
|
}
|
2020-12-29 00:48:50 +00:00
|
|
|
|
2020-12-31 22:56:08 +00:00
|
|
|
//updates game in list
|
2020-12-31 06:33:03 +00:00
|
|
|
for (var slotnum = 0; slotnum < grid.children.length; slotnum++) {
|
2021-01-01 08:32:47 +00:00
|
|
|
if (grid.children[slotnum].timestamp == filterjson[i].timestamp) {
|
|
|
|
insertGame(slotnum, filterjson[i]);
|
2020-12-29 00:48:50 +00:00
|
|
|
};
|
|
|
|
};
|
|
|
|
};
|
2020-12-30 23:53:15 +00:00
|
|
|
|
2020-12-31 22:56:08 +00:00
|
|
|
//remove last rows if not needed
|
2020-12-31 08:45:42 +00:00
|
|
|
while (grid.children[grid.children.length-1].className == "emptyslot" &&
|
|
|
|
grid.children[grid.children.length-2].className == "emptyslot" &&
|
|
|
|
grid.children[grid.children.length-3].className == "emptyslot" &&
|
|
|
|
grid.children.length > 3) {
|
|
|
|
for (var i = 0; i < 3; i++) {
|
|
|
|
grid.removeChild(grid.children[grid.children.length-1]);
|
|
|
|
}
|
2020-12-30 23:53:15 +00:00
|
|
|
}
|
2020-12-31 22:56:08 +00:00
|
|
|
}
|
2020-12-31 02:32:07 +00:00
|
|
|
|
2020-12-30 23:53:15 +00:00
|
|
|
const insertEmpty = (grid) => {
|
|
|
|
newBox = document.createElement("DIV");
|
|
|
|
newBox.className = "emptyslot";
|
|
|
|
grid.appendChild(newBox);
|
|
|
|
}
|
2020-12-29 00:48:50 +00:00
|
|
|
|
2021-01-01 08:32:47 +00:00
|
|
|
const insertGame = (gridboxnum, game) => {
|
2020-12-29 00:48:50 +00:00
|
|
|
var thisBox = grid.children[gridboxnum];
|
2021-01-01 08:32:47 +00:00
|
|
|
thisBox.innerHTML = game.html;
|
2020-12-30 08:50:29 +00:00
|
|
|
thisBox.className = "game";
|
2021-01-01 08:32:47 +00:00
|
|
|
thisBox.timestamp = game.timestamp;
|
2020-12-29 00:48:50 +00:00
|
|
|
};
|
|
|
|
});
|