2021-01-02 06:52:51 +00:00
|
|
|
var socket = io.connect();
|
2021-01-02 07:29:03 +00:00
|
|
|
var lastupdate;
|
|
|
|
var grid;
|
2021-01-02 06:52:51 +00:00
|
|
|
|
2020-12-29 00:48:50 +00:00
|
|
|
$(document).ready(function (){
|
2021-01-02 07:29:03 +00:00
|
|
|
grid = document.getElementById("container")
|
2020-12-29 00:48:50 +00:00
|
|
|
|
|
|
|
socket.on('connect', function () {
|
2021-01-02 07:29:03 +00:00
|
|
|
socket.emit('recieved', {});
|
2020-12-29 00:48:50 +00:00
|
|
|
});
|
|
|
|
|
2021-01-02 11:21:53 +00:00
|
|
|
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-02 06:52:51 +00:00
|
|
|
updateLeagues(json);
|
2020-12-31 22:56:08 +00:00
|
|
|
});
|
2021-01-02 07:29:03 +00:00
|
|
|
});
|
2020-12-31 22:56:08 +00:00
|
|
|
|
2021-01-02 07:29:03 +00:00
|
|
|
const updateGames = (json, filter) => {
|
2021-01-01 10:04:10 +00:00
|
|
|
|
2021-01-02 07:29:03 +00:00
|
|
|
filterjson = [];
|
|
|
|
for (var game of json) {
|
|
|
|
if (game.league == filter || filter == "All") {
|
|
|
|
filterjson.push(game);
|
2020-12-31 22:56:08 +00:00
|
|
|
}
|
2021-01-02 07:29:03 +00:00
|
|
|
}
|
2020-12-30 20:10:29 +00:00
|
|
|
|
2021-01-02 07:29:03 +00:00
|
|
|
if (filterjson.length == 0) {
|
|
|
|
$('#footer div').html("No games right now. Why not head over to Discord and start one?");
|
|
|
|
} else {
|
|
|
|
$('#footer div').html("");
|
|
|
|
}
|
2020-12-30 20:10:29 +00:00
|
|
|
|
2021-01-02 16:51:47 +00:00
|
|
|
var searchparams = new URLSearchParams(window.location.search);
|
|
|
|
if (searchparams.has('game') && filterjson.some(x => x.timestamp == searchparams.get('game')) && grid.children[0].timestamp != searchparams.get('game')) {
|
|
|
|
var game = filterjson.find(x => x.timestamp == searchparams.get('game'))
|
|
|
|
var oldbox = Array.prototype.slice.call(grid.children).find(x => x.timestamp == game.timestamp)
|
|
|
|
if (oldbox) { clearBox(oldbox) }
|
|
|
|
insertGame(0, game)
|
|
|
|
}
|
|
|
|
|
2021-01-02 07:29:03 +00:00
|
|
|
//replace games that have ended with empty slots
|
|
|
|
for (var slotnum = 0; slotnum < grid.children.length; slotnum++) {
|
|
|
|
if (grid.children[slotnum].className == "game" && !filterjson.some((x) => x.timestamp == grid.children[slotnum].timestamp)) {
|
2021-01-02 16:51:47 +00:00
|
|
|
clearBox(grid.children[slotnum])
|
2021-01-01 01:19:57 +00:00
|
|
|
}
|
2021-01-02 07:29:03 +00:00
|
|
|
}
|
2021-01-01 01:19:57 +00:00
|
|
|
|
2021-01-02 07:29:03 +00:00
|
|
|
for (var game of filterjson) {
|
|
|
|
//updates game in list
|
|
|
|
for (var slotnum = 0; slotnum < grid.children.length; slotnum++) {
|
|
|
|
if (grid.children[slotnum].timestamp == game.timestamp) {
|
|
|
|
insertGame(slotnum, game);
|
2021-01-01 10:04:10 +00:00
|
|
|
};
|
2021-01-02 07:29:03 +00:00
|
|
|
};
|
2021-01-01 10:04:10 +00:00
|
|
|
|
2021-01-02 07:29:03 +00:00
|
|
|
//adds game to list if not there already
|
2021-01-02 16:51:47 +00:00
|
|
|
if (!Array.prototype.slice.call(grid.children).some(x => x.timestamp == game.timestamp)) {
|
2021-01-02 07:29:03 +00:00
|
|
|
for (var slotnum = 0; true; slotnum++) { //this is really a while loop but shh don't tell anyone
|
|
|
|
if (slotnum >= grid.children.length) {
|
2021-01-05 05:26:33 +00:00
|
|
|
insertEmpty(grid);
|
2021-01-02 07:29:03 +00:00
|
|
|
}
|
|
|
|
if (grid.children[slotnum].className == "emptyslot") {
|
|
|
|
insertGame(slotnum, game);
|
|
|
|
break;
|
2020-12-29 00:48:50 +00:00
|
|
|
};
|
2021-01-02 07:29:03 +00:00
|
|
|
};
|
|
|
|
}
|
2020-12-30 23:53:15 +00:00
|
|
|
|
2021-01-05 05:26:33 +00:00
|
|
|
fillgrid(grid)
|
|
|
|
};
|
2021-01-02 07:29:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const insertEmpty = (grid) => {
|
|
|
|
var newBox = document.createElement("DIV");
|
|
|
|
newBox.className = "emptyslot";
|
|
|
|
grid.appendChild(newBox);
|
|
|
|
}
|
|
|
|
|
|
|
|
const insertGame = (gridboxnum, game) => {
|
|
|
|
var thisBox = grid.children[gridboxnum];
|
|
|
|
thisBox.className = "game";
|
|
|
|
thisBox.timestamp = game.timestamp;
|
2021-01-02 22:36:19 +00:00
|
|
|
thisBox.innerHTML = game.html;
|
|
|
|
twemoji.parse(thisBox);
|
2021-01-02 07:29:03 +00:00
|
|
|
};
|
|
|
|
|
2021-01-02 16:51:47 +00:00
|
|
|
const insertLeague = (league) => {
|
|
|
|
var btn = document.createElement("BUTTON");
|
|
|
|
btn.className = "filter";
|
|
|
|
btn.innerHTML = league;
|
|
|
|
$('#filters').append(btn);
|
|
|
|
return btn;
|
|
|
|
}
|
|
|
|
|
|
|
|
const clearBox = (box) => {
|
|
|
|
box.className = "emptyslot";
|
|
|
|
box.timestamp = null;
|
|
|
|
box.innerHTML = "";
|
|
|
|
}
|
|
|
|
|
2021-01-05 05:26:33 +00:00
|
|
|
const fillgrid = (grid) => {
|
|
|
|
var gridwidth = window.getComputedStyle(grid).getPropertyValue('grid-template-columns').split(" ").length //hack to get number of grid columns
|
|
|
|
|
|
|
|
// add cells to fill last row
|
|
|
|
while (grid.children.length % gridwidth != 0) {
|
|
|
|
insertEmpty(grid)
|
|
|
|
}
|
|
|
|
|
|
|
|
//remove last rows if not needed
|
|
|
|
while (grid.children.length > gridwidth && Array.prototype.slice.call(grid.children).slice(grid.children.length - gridwidth).every( x => x.className == 'emptyslot')) {
|
|
|
|
for (var i = 0; i < gridwidth; i++) {
|
|
|
|
grid.removeChild(grid.children[grid.children.length-1]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-02 07:29:03 +00:00
|
|
|
const updateLeagues = (games) => {
|
|
|
|
//get all leagues
|
|
|
|
var leagues = []
|
|
|
|
for (var game of games) {
|
|
|
|
if (game.league != "" && !leagues.includes(game.league)) {
|
|
|
|
leagues.push(game.league)
|
|
|
|
}
|
2020-12-30 23:53:15 +00:00
|
|
|
}
|
2020-12-29 00:48:50 +00:00
|
|
|
|
2021-01-02 07:29:03 +00:00
|
|
|
//remove leagues no longer present
|
|
|
|
$('#filters .filter').each(function(index) {
|
|
|
|
if (!leagues.includes($(this).text())) {
|
|
|
|
if (this.id != 'selected_filter' && $(this).text() != "All") { //don't remove the currently selected filter or the "all" filter
|
|
|
|
$(this).remove();
|
2021-01-02 06:52:51 +00:00
|
|
|
}
|
2021-01-02 07:29:03 +00:00
|
|
|
} else {
|
|
|
|
leagues.splice(leagues.indexOf($(this).text()), 1);
|
2021-01-02 06:52:51 +00:00
|
|
|
}
|
2021-01-02 07:29:03 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
// add leagues not already present
|
|
|
|
for (var league of leagues) { // we removed the entries that are already there in the loop above
|
2021-01-02 16:51:47 +00:00
|
|
|
insertLeague(league)
|
2021-01-02 07:29:03 +00:00
|
|
|
}
|
2021-01-02 06:52:51 +00:00
|
|
|
|
2021-01-02 07:29:03 +00:00
|
|
|
//add click handlers to each filter
|
|
|
|
$('#filters .filter').each(function(index) {
|
|
|
|
this.onclick = function() {
|
|
|
|
if ($('#filters #selected_filter').text() == 'All') {
|
|
|
|
updateGames([], ""); // clear grid when switching off of All, to make games collapse to top
|
2021-01-02 06:52:51 +00:00
|
|
|
}
|
2021-01-02 07:29:03 +00:00
|
|
|
$('#filters #selected_filter').attr('id', '');
|
|
|
|
this.id = 'selected_filter';
|
2021-01-02 06:52:51 +00:00
|
|
|
|
2021-01-02 07:29:03 +00:00
|
|
|
var search = new URLSearchParams();
|
2021-01-02 16:51:47 +00:00
|
|
|
search.append('league', this.textContent);
|
|
|
|
history.pushState({}, "", "/" + (this.textContent != 'All' ? "?" + search.toString() : ""));
|
2021-01-02 07:29:03 +00:00
|
|
|
updateGames(lastupdate, this.textContent);
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
2021-01-02 06:52:51 +00:00
|
|
|
|
|
|
|
window.onpopstate = function(e) {
|
2021-01-02 16:51:47 +00:00
|
|
|
var searchparams = new URLSearchParams(window.location.search);
|
2021-01-02 07:29:03 +00:00
|
|
|
updateLeagues(lastupdate);
|
|
|
|
$('#filters #selected_filter').attr('id', '');
|
2021-01-02 16:51:47 +00:00
|
|
|
if (searchparams.has('league')) {
|
|
|
|
var filter_found = false
|
|
|
|
$('#filters .filter').each(function(i) { if (this.textContent == searchparams.get('league')) { this.id = 'selected_filter'; filter_found = true }});
|
|
|
|
if (!filter_found) { insertLeague(searchparams.get('league')).id = 'selected_filter' }
|
|
|
|
|
|
|
|
updateGames(lastupdate, searchparams.get('league'));
|
2021-01-02 06:52:51 +00:00
|
|
|
} else {
|
2021-01-02 07:29:03 +00:00
|
|
|
$('#filters .filter').each(function(i) { if (this.textContent == 'All') { this.id = 'selected_filter' }})
|
|
|
|
updateGames(lastupdate, "All");
|
2021-01-02 06:52:51 +00:00
|
|
|
}
|
2021-01-05 05:26:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
window.addEventListener('resize', function(e) {
|
|
|
|
fillgrid(grid)
|
|
|
|
})
|