-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
197 lines (175 loc) · 6.15 KB
/
script.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
import scheduleData from './schedule.js';
const timerDisplay = document.getElementById('timer');
const startBtn = document.getElementById('startBtn');
const resetBtn = document.getElementById('resetBtn');
const prevMatchBtn = document.getElementById('prevMatch');
const nextMatchBtn = document.getElementById('nextMatch');
const currentMatchDisplay = document.getElementById('currentMatch');
const team1Display = document.getElementById('team1');
const team2Display = document.getElementById('team2');
const team3Display = document.getElementById('team3');
const team4Display = document.getElementById('team4');
const menuBtn = document.getElementById('menuBtn');
const modalOverlay = document.getElementById('modalOverlay');
const closeMenuBtn = document.getElementById('closeMenuBtn');
const scheduleTableBody = document.getElementById('scheduleTableBody');
const addMatchBtn = document.getElementById('addMatchBtn');
const discardChangesBtn = document.getElementById('discardChangesBtn');
const saveChangesBtn = document.getElementById('saveChangesBtn');
let originalSchedule = [];
let editedSchedule = [];
let timeLeft = 150; // 2 minutes and 30 seconds in seconds
let timerId;
let schedule = [];
let currentMatch = 1;
async function loadSchedule() {
schedule = await scheduleData.getSchedule();
updateMatchDisplay();
}
function updateTimerDisplay() {
const minutes = Math.floor(timeLeft / 60);
const seconds = timeLeft % 60;
timerDisplay.textContent = `${minutes}:${seconds.toString().padStart(2, '0')}`;
}
function updateMatchDisplay() {
const match = schedule[currentMatch - 1];
if (match) {
currentMatchDisplay.textContent = `Match ${match.match}`;
team1Display.textContent = `${match.team1}`;
team2Display.textContent = `${match.team2}`;
team3Display.textContent = `${match.team3}`;
team4Display.textContent = `${match.team4}`;
}
}
function startTimer() {
startBtn.disabled = true;
timerId = setInterval(() => {
timeLeft--;
updateTimerDisplay();
if (timeLeft === 0) {
clearInterval(timerId);
startBtn.disabled = false;
}
}, 1000);
}
function resetTimer() {
clearInterval(timerId);
timeLeft = 150;
updateTimerDisplay();
startBtn.disabled = false;
}
function prevMatch() {
if (currentMatch > 1) {
currentMatch--;
updateMatchDisplay();
resetTimer();
if (currentMatch === 1) {
prevMatchBtn.disabled = true;
}
nextMatchBtn.disabled = false;
}
}
function nextMatch() {
if (currentMatch < schedule.length) {
currentMatch++;
updateMatchDisplay();
resetTimer();
if (currentMatch === schedule.length) {
nextMatchBtn.disabled = true;
}
prevMatchBtn.disabled = false;
}
}
function renderScheduleTable() {
scheduleTableBody.innerHTML = '';
editedSchedule.forEach((match) => {
const row = document.createElement('tr');
row.innerHTML = `
<td>${match.match}</td>
<td><input type="number" value="${match.team1}" data-match="${match.match}" data-team="1"></td>
<td><input type="number" value="${match.team2}" data-match="${match.match}" data-team="2"></td>
<td><input type="number" value="${match.team3}" data-match="${match.match}" data-team="3"></td>
<td><input type="number" value="${match.team4}" data-match="${match.match}" data-team="4"></td>
<td><button class="delete-btn" data-match="${match.match}">Delete</button></td>
`;
scheduleTableBody.appendChild(row);
});
}
function openMenu() {
modalOverlay.style.display = 'flex';
originalSchedule = JSON.parse(JSON.stringify(schedule));
editedSchedule = JSON.parse(JSON.stringify(schedule));
renderScheduleTable();
}
function closeMenu() {
modalOverlay.style.display = 'none';
updateMatchDisplay();
}
function addNewMatch() {
const newMatch = {
match: editedSchedule.length + 1,
team1: 0,
team2: 0,
team3: 0,
team4: 0
};
editedSchedule.push(newMatch);
renderScheduleTable();
}
function updateMatch(matchNumber, teamNumber, value) {
const matchIndex = editedSchedule.findIndex(match => match.match === matchNumber);
if (matchIndex !== -1) {
editedSchedule[matchIndex][`team${teamNumber}`] = parseInt(value);
}
}
function deleteMatch(matchNumber) {
editedSchedule = editedSchedule.filter(match => match.match !== matchNumber);
editedSchedule.forEach((match, index) => {
match.match = index + 1;
});
renderScheduleTable();
}
function discardChanges() {
editedSchedule = JSON.parse(JSON.stringify(originalSchedule));
closeMenu();
}
async function saveChanges() {
schedule = JSON.parse(JSON.stringify(editedSchedule));
await scheduleData.updateFullSchedule(schedule);
closeMenu();
}
// Event Listeners
startBtn.addEventListener('click', startTimer);
resetBtn.addEventListener('click', resetTimer);
prevMatchBtn.addEventListener('click', prevMatch);
nextMatchBtn.addEventListener('click', nextMatch);
menuBtn.addEventListener('click', openMenu);
closeMenuBtn.addEventListener('click', closeMenu);
addMatchBtn.addEventListener('click', addNewMatch);
discardChangesBtn.addEventListener('click', discardChanges);
saveChangesBtn.addEventListener('click', saveChanges);
scheduleTableBody.addEventListener('input', (event) => {
if (event.target.tagName === 'INPUT') {
const matchNumber = parseInt(event.target.dataset.match);
const teamNumber = parseInt(event.target.dataset.team);
const value = event.target.value;
updateMatch(matchNumber, teamNumber, value);
}
});
scheduleTableBody.addEventListener('click', (event) => {
if (event.target.classList.contains('delete-btn')) {
const matchNumber = parseInt(event.target.dataset.match);
deleteMatch(matchNumber);
}
});
modalOverlay.addEventListener('click', (event) => {
if (event.target === modalOverlay) {
closeMenu();
}
});
// Initial load
loadSchedule().then(() => {
updateTimerDisplay();
updateMatchDisplay();
prevMatchBtn.disabled = true;
});