sort by time

This commit is contained in:
Radek Davidek 2025-11-01 16:29:59 +01:00
parent 63a4549a8d
commit 843e2bf749
2 changed files with 57 additions and 50 deletions

View File

@ -22,9 +22,6 @@ import com.sun.net.httpserver.HttpServer;
public class BasketballServer { public class BasketballServer {
private static JsonNode cachedData;
private static final Object lock = new Object();
public static void main(String[] args) throws Exception { public static void main(String[] args) throws Exception {
fetchDataForDate("Sat Nov 01 2025 01:00:00 GMT+0100 (Central European Standard Time)"); // inicialní fetch fetchDataForDate("Sat Nov 01 2025 01:00:00 GMT+0100 (Central European Standard Time)"); // inicialní fetch
@ -60,7 +57,6 @@ public class BasketballServer {
ObjectMapper mapper = new ObjectMapper(); ObjectMapper mapper = new ObjectMapper();
JsonNode data = mapper.readTree(content.toString()); JsonNode data = mapper.readTree(content.toString());
synchronized (lock) { cachedData = data; }
System.out.println("Data fetched successfully for date: " + dateParam); System.out.println("Data fetched successfully for date: " + dateParam);
return data; return data;

View File

@ -65,7 +65,6 @@
<label for="date">Vyber datum:</label> <label for="date">Vyber datum:</label>
<input type="date" id="date" onchange="loadMatches()"/> <input type="date" id="date" onchange="loadMatches()"/>
<button onclick="loadMatches()">Načíst zápasy</button>
<label for="league">Vyber ligu:</label> <label for="league">Vyber ligu:</label>
<select id="league" onchange="filterMatches()"> <select id="league" onchange="filterMatches()">
@ -120,10 +119,11 @@ async function loadMatches() {
let closestRow = null; let closestRow = null;
const now = new Date(); const now = new Date();
const allMatches = [];
data.leagues.forEach(league => { data.leagues.forEach(league => {
// --- kontrola, zda liga má alespoň jeden zápas s TV odkazem ---
const hasTV = league.matches.some(m => m.links.tvcom && m.links.tvcom.url); const hasTV = league.matches.some(m => m.links.tvcom && m.links.tvcom.url);
if (!hasTV) return; // přeskočit ligu bez TV zápasů if (!hasTV) return;
// přidat do select boxu // přidat do select boxu
const option = document.createElement('option'); const option = document.createElement('option');
@ -132,53 +132,64 @@ async function loadMatches() {
select.appendChild(option); select.appendChild(option);
league.matches.forEach(match => { league.matches.forEach(match => {
if (!match.links.live && !match.links.tvcom) return; // přeskočit zápasy bez Live a TV if (!match.links.live && !match.links.tvcom) return;
const tr = document.createElement('tr'); allMatches.push({ league: league.name, match });
tr.dataset.league = league.name;
// doplnit https://cz.basketball pokud preview.url nezačíná http/https
let previewUrl = '';
if (match.links.preview && match.links.preview.url) {
previewUrl = match.links.preview.url;
if (!previewUrl.startsWith('http://') && !previewUrl.startsWith('https://')) {
previewUrl = 'https://cz.basketball' + previewUrl;
}
}
tr.innerHTML = `
<td>${match.status}</td>
<td>${league.name}</td>
<td>${match.home.name}</td>
<td>${match.away.name}</td>
<td>${previewUrl ? '<a href="'+previewUrl+'">Preview</a>' : ''}</td>
<td>${match.links.live && match.links.live.url ? '<a href="'+match.links.live.url+'" target="_blank">Live</a>' : ''}</td>
<td>${match.links.tvcom && match.links.tvcom.url ? '<a href="'+match.links.tvcom.url+'" target="_blank">TV</a>' : ''}</td>
`;
// zvýraznit zápasy s Nymburkem
if ((match.home.name && match.home.name.includes('Nymburk')) ||
(match.away.name && match.away.name.includes('Nymburk'))) {
tr.classList.add('nymburk');
}
// zvýraznit nejbližší zápas (Live zápasy)
if (match.links.live && match.links.live.url) {
const [hours, minutes] = match.status.split(':').map(Number);
if (!isNaN(hours) && !isNaN(minutes)) {
const matchDate = new Date(now.getFullYear(), now.getMonth(), now.getDate(), hours, minutes);
const diff = matchDate - now;
if (diff >= 0 && diff < closestTimeDiff) {
closestTimeDiff = diff;
closestRow = tr;
}
}
}
tbody.appendChild(tr);
}); });
}); });
// --- Seřadit podle času ---
allMatches.sort((a, b) => {
const parseTime = s => {
if (!s || !s.match(/^\d{1,2}:\d{2}$/)) return Infinity;
const [h, m] = s.split(':').map(Number);
return h * 60 + m;
};
return parseTime(a.match.status) - parseTime(b.match.status);
});
// --- Vytvořit tabulku ---
allMatches.forEach(({ league, match }) => {
const tr = document.createElement('tr');
tr.dataset.league = league;
let previewUrl = '';
if (match.links.preview && match.links.preview.url) {
previewUrl = match.links.preview.url;
if (!previewUrl.startsWith('http://') && !previewUrl.startsWith('https://')) {
previewUrl = 'https://cz.basketball' + previewUrl;
}
}
tr.innerHTML = `
<td>${match.status}</td>
<td>${league}</td>
<td>${match.home.name}</td>
<td>${match.away.name}</td>
<td>${previewUrl ? '<a href="'+previewUrl+'">Preview</a>' : ''}</td>
<td>${match.links.live && match.links.live.url ? '<a href="'+match.links.live.url+'" target="_blank">Live</a>' : ''}</td>
<td>${match.links.tvcom && match.links.tvcom.url ? '<a href="'+match.links.tvcom.url+'" target="_blank">TV</a>' : ''}</td>
`;
if ((match.home.name && match.home.name.includes('Nymburk')) ||
(match.away.name && match.away.name.includes('Nymburk'))) {
tr.classList.add('nymburk');
}
const [hours, minutes] = match.status.split(':').map(Number);
if (!isNaN(hours) && !isNaN(minutes)) {
const matchDate = new Date(now.getFullYear(), now.getMonth(), now.getDate(), hours, minutes);
const diff = matchDate - now;
if (diff >= 0 && diff < closestTimeDiff) {
closestTimeDiff = diff;
closestRow = tr;
}
}
tbody.appendChild(tr);
});
if (closestRow) closestRow.classList.add('highlight'); if (closestRow) closestRow.classList.add('highlight');
} }