Files

326 lines
13 KiB
HTML

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<title data-i18n="draft.tournament_draft">Tournament Draft</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="/static/css/base.css">
<link rel="stylesheet" href="/static/css/navbar.css">
<link rel="stylesheet" href="/static/css/buttons.css">
<link rel="stylesheet" href="/static/css/components.css">
<link rel="stylesheet" href="/static/css/responsive.css">
<link rel="stylesheet" href="/static/css/pages/draft.css">
<script src="/static/js/i18n.js"></script>
</head>
<body>
<div class="navbar">
<div class="navbar-title">🏆 Razpored Turnirja</div>
<div class="navbar-controls">
<a href="/tournament" class="nav-btn">🏆 <span data-i18n="navigation.tournament">Tournament</span></a>
<a href="/tournament/draft" class="nav-btn active">📋 Razpored</a>
<a href="/results/calculator" class="nav-btn">🎯 <span data-i18n="navigation.calculator">Results Calculator</span></a>
<a href="/" class="nav-btn">✕</a>
</div>
</div>
<div class="main-container">
{% if tournament %}
<div class="tournament-header">
<img src="/static/logo.png" alt="Logo" class="print-logo" />
<div class="tournament-title">🎯 <span data-i18n="draft.shooting_tournament">Shooting Tournament</span></div>
<div class="tournament-stats">
{{ tournament.total_players }} <span data-i18n="draft.players">players</span> • {{ tournament.total_rounds }} <span data-i18n="draft.rounds">rounds</span>
</div>
<div class="print-meta">
<div>{{ tournament.total_players }} <span data-i18n="draft.players">igralcev</span></div>
<div id="printDate"></div>
</div>
{% if tournament.current_round %}
<div class="tournament-controls">
<button class="round-nav-btn" id="prevRoundBtn" onclick="changeRound(-1)">←</button>
<span class="current-round-display"><span data-i18n="draft.round">Round</span> {{ tournament.current_round }} / {{ tournament.total_rounds }}</span>
<button class="round-nav-btn" id="nextRoundBtn" onclick="changeRound(1)">→</button>
</div>
{% endif %}
<button class="nav-btn" onclick="printDraft()" title="Print">🖨️</button>
</div>
<div class="rounds-container" data-total-rounds="{{ tournament.total_rounds }}">
{% for round in tournament.rounds %}
{% set is_current = tournament.current_round == round.round_number %}
{% set is_completed = tournament.current_round > round.round_number %}
<div class="round-row {{ 'current' if is_current else ('completed' if is_completed else 'waiting') }}">
<div class="round-header">
<div class="round-title"><span data-i18n="draft.round">Round</span> {{ round.round_number }}</div>
{% if is_current %}
<div class="round-badge current-badge" data-i18n="draft.current">Current</div>
{% elif is_completed %}
<div class="round-badge completed-badge" data-i18n="draft.done">Done</div>
{% else %}
<div class="round-badge waiting-badge" data-i18n="draft.wait">Wait</div>
{% endif %}
<!-- Print-only fields -->
<div class="round-print-info">
<div class="round-time-field">
<span class="round-time-label"><span data-i18n="draft.time">Time</span>:</span>
<div class="round-time-input"></div>
</div>
<div class="round-checkbox-field">
<span class="round-checkbox-label"><span data-i18n="draft.completed">Completed</span>:</span>
<div class="round-checkbox"></div>
</div>
</div>
</div>
<div class="positions-container">
{% set slots = tournament.active_slots if tournament.active_slots else range(1, 7) %}
{% for idx in range(slots|length) %}
{% set slot_num = slots[idx] %}
{% set player = round.players[idx] if idx < round.players|length else none %}
<div class="position-card {{ 'filled' if player else 'empty' }}">
<div class="position-header">
<div class="position-number">{{ slot_num }}</div>
</div>
<div class="position-body">
{% if player %}
<div class="player-name">{{ player.name }}</div>
<div class="player-id">ID: {{ player.id }}</div>
{% else %}
<div class="player-name" data-i18n="draft.empty">Empty</div>
{% endif %}
</div>
</div>
{% endfor %}
</div>
<div class="print-done-check">
<span class="print-done-box"></span>
<span class="print-done-label">Done</span>
</div>
</div>
{% endfor %}
</div>
{% else %}
<div class="no-tournament">
<h2 data-i18n="draft.no_active_tournament">No Active Tournament</h2>
<p data-i18n="draft.setup_tournament_message">Go to Tournament Management to set up players and start a tournament.</p>
<a href="/tournament" class="nav-btn primary">🏆 <span data-i18n="draft.set_up_tournament">Set Up Tournament</span></a>
</div>
{% endif %}
</div>
<script>
// Tournament state
const tournamentActive = {{ 'true' if tournament else 'false' }};
let currentRound = {{ tournament.current_round if tournament else 1 }};
const totalRounds = {{ tournament.total_rounds if tournament else 1 }};
// TV view toggle — only active when loaded as TV display (?tv=1)
const isTvDisplay = new URLSearchParams(window.location.search).get('tv') === '1';
if (isTvDisplay) {
const btn = document.getElementById('btnSwitchView');
if (btn) btn.style.display = '';
}
async function switchToView() {
try {
await fetch('/api/tv/view', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ view: 'cameras' })
});
window.location.href = '/';
} catch (e) { console.error(e); }
}
function updateRoundNavigation() {
if (tournamentActive) {
const prevBtn = document.getElementById('prevRoundBtn');
const nextBtn = document.getElementById('nextRoundBtn');
if (prevBtn) prevBtn.disabled = currentRound <= 1;
if (nextBtn) nextBtn.disabled = currentRound >= totalRounds;
}
}
// Change round function
async function changeRound(direction) {
if (!tournamentActive) return;
const newRound = currentRound + direction;
if (newRound < 1 || newRound > totalRounds) return;
// Disable buttons during request
const prevBtn = document.getElementById('prevRoundBtn');
const nextBtn = document.getElementById('nextRoundBtn');
if (prevBtn) prevBtn.disabled = true;
if (nextBtn) nextBtn.disabled = true;
try {
const response = await fetch(`/api/tournament/round/${newRound}`, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
}
});
if (response.ok) {
currentRound = newRound;
updateRoundNavigation();
// Reload page to get new round players
setTimeout(() => {
window.location.reload();
}, 300);
} else {
console.error('Failed to change round');
alert('Failed to change round. Please try again.');
updateRoundNavigation();
}
} catch (error) {
console.error('Error changing round:', error);
alert('Error changing round. Please try again.');
updateRoundNavigation();
}
}
// Set print date
const printDateEl = document.getElementById('printDate');
if (printDateEl) {
const now = new Date();
printDateEl.textContent = now.toLocaleDateString('sl-SI', { day: '2-digit', month: '2-digit', year: 'numeric' });
}
// Print function
function printDraft() {
document.querySelectorAll('.round-row').forEach(el => {
el.classList.remove('current', 'completed', 'waiting');
el.classList.add('waiting');
});
window.print();
}
// Keyboard shortcuts
document.addEventListener('keydown', function(event) {
if (event.key === 'Escape') {
window.location.href = '/';
} else if (event.key === 'r' || event.key === 'R') {
event.preventDefault();
window.location.reload();
} else if (event.key === 'p' || event.key === 'P') {
event.preventDefault();
printDraft();
} else if (tournamentActive) {
if (event.key === 'ArrowLeft') {
event.preventDefault();
changeRound(-1);
} else if (event.key === 'ArrowRight') {
event.preventDefault();
changeRound(1);
}
}
});
// Initialize
document.addEventListener('DOMContentLoaded', function() {
updateRoundNavigation();
// Scroll to current round
setTimeout(() => {
const currentRoundRow = document.querySelector('.round-row.current');
if (currentRoundRow) {
currentRoundRow.scrollIntoView({
behavior: 'smooth',
block: 'start'
});
}
}, 300);
// Poll for state changes
let lastStateHash = null;
setInterval(async () => {
if (document.visibilityState !== 'visible') return;
try {
const response = await fetch('/api/dashboard/state');
if (!response.ok) return;
const data = await response.json();
// If tournament just became inactive, redirect to archive (not reload)
if (!data.tournament_active) {
if (data.archive_filename) {
window.location.href = `/archive/tournament/${data.archive_filename}`;
} else if (data.league_archive_filename) {
window.location.href = `/archive/league/${data.league_archive_filename}`;
} else {
window.location.href = '/results';
}
return;
}
// Follow TV view changes when used as TV display
if (isTvDisplay) {
if (data.tv_view === 'cameras') { window.location.href = '/'; return; }
if (data.tv_view === 'results') {
if (data.archive_filename) {
window.location.href = `/archive/tournament/${data.archive_filename}?tv=1`;
} else if (data.league_archive_filename) {
window.location.href = `/archive/league/${data.league_archive_filename}?tv=1`;
} else {
window.location.href = '/results?tv=1';
}
return;
}
}
// Reload on round change to keep draft display in sync
const hash = JSON.stringify({ round: data.current_round });
if (lastStateHash !== null && hash !== lastStateHash) {
window.location.reload();
}
lastStateHash = hash;
updateTvViewBtn(data.tv_view || 'cameras');
} catch (e) {}
}, 3000);
});
// ── TV VIEW TOGGLE ────────────────────────────────────────────────────────
let currentTvView = 'cameras';
function updateTvViewBtn(view) {
currentTvView = view;
const btn = document.getElementById('btnTvView');
if (!btn) return;
if (view === 'draft') {
btn.textContent = '📺';
btn.style.background = '#28a745';
btn.style.color = 'white';
btn.style.borderColor = '#1e7e34';
} else {
btn.textContent = '📺';
btn.style.background = '';
btn.style.color = '';
btn.style.borderColor = '';
}
}
async function toggleTvView() {
const newView = currentTvView === 'draft' ? 'cameras' : 'draft';
try {
await fetch('/api/tv/view', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ view: newView })
});
updateTvViewBtn(newView);
} catch (e) { console.error(e); }
}
// Load initial tv_view state
fetch('/api/dashboard/state').then(r => r.json()).then(d => updateTvViewBtn(d.tv_view || 'cameras')).catch(() => {});
</script>
</body>
</html>