Added comments, incDec function

This commit is contained in:
Julian Appel 2024-05-17 21:17:52 +02:00
parent 9f845ea234
commit 35a0785bf3
3 changed files with 39 additions and 5 deletions

View File

@ -13,8 +13,10 @@ function pause() {};
function reset() {}; function reset() {};
function end() {}; function end() {};
function print() {}; function print() {};
function incDec() {};
function getValues() {}; function getValues() {};
// Start timer not paused and not already finished
function start() { function start() {
if(isPaused == true && durationLeft.asSeconds() != 0) { // Only allow start if timer ist currently paused and durationLeft is not 0 seconds if(isPaused == true && durationLeft.asSeconds() != 0) { // Only allow start if timer ist currently paused and durationLeft is not 0 seconds
console.log("Timer gestartet") console.log("Timer gestartet")
@ -32,6 +34,7 @@ function start() {
return false return false
} }
// If possible pause timer, if not return false
function pause() { function pause() {
if(!isPaused) { if(!isPaused) {
console.log("Timer pausiert"); console.log("Timer pausiert");
@ -42,6 +45,7 @@ function pause() {
return false return false
} }
// Reset timer to passed value, and send durationLeft to all clients
function reset(newDuration) { function reset(newDuration) {
console.log("Timer Zurückgesetzt"); console.log("Timer Zurückgesetzt");
duration = moment.duration(newDuration, 'seconds').clone(); // Set initial duration to received duration in seconds duration = moment.duration(newDuration, 'seconds').clone(); // Set initial duration to received duration in seconds
@ -49,18 +53,31 @@ function reset(newDuration) {
io.sockets.emit('timerDurationLeft', print()) // Emit durationLeft to all connected sockets io.sockets.emit('timerDurationLeft', print()) // Emit durationLeft to all connected sockets
} }
// Stop timer and emit timerEndet to all clients
function end() { function end() {
isPaused = true // Set status of the timer isPaused = true // Set status of the timer
clearInterval(timerInterval); // End the execution clearInterval(timerInterval); // End the execution
io.sockets.emit('timerEnded', print()) // Emit end to all connected sockets io.sockets.emit('timerEnded', print()) // Emit end to all connected sockets
} }
// Return formatted timestamp in the format of 00:00
function print() { function print() {
var minutes = (durationLeft.minutes() < 10) ? "0" + durationLeft.minutes() : durationLeft.minutes(); // Create leading zeros for numbers <10 var minutes = (durationLeft.minutes() < 10) ? "0" + durationLeft.minutes() : durationLeft.minutes(); // Create leading zeros for numbers <10
var seconds = (durationLeft.seconds() < 10) ? "0" + durationLeft.seconds() : durationLeft.seconds(); // Create leading zeros for numbers <10 var seconds = (durationLeft.seconds() < 10) ? "0" + durationLeft.seconds() : durationLeft.seconds(); // Create leading zeros for numbers <10
return minutes + ":" + seconds return minutes + ":" + seconds
} }
// Increase or decrease the timer depending on the passed value in seconds
function incDec(value) {
if(Math.abs(value) >= durationLeft.asSeconds()) { // If abs from passed value is greater than seconds left on timer, end timer
end();
} else {
durationLeft.add(value, 'second');
io.sockets.emit('timerDurationLeft', print())
}
}
// Return all important timer values
function getValues() { function getValues() {
return { return {
isPaused: isPaused, isPaused: isPaused,
@ -71,5 +88,5 @@ function getValues() {
} }
module.exports = { module.exports = {
duration, durationLeft, isPaused, start, pause, reset, end, print, getValues duration, durationLeft, isPaused, start, pause, reset, end, print, incDec, getValues
} }

View File

@ -50,6 +50,7 @@ async function timerStart() {
} }
} }
// Request to pause the timer
async function timerPause() { async function timerPause() {
const response = await fetch("/admin/timerPause"); // Call API Endpoint /admin/timerStart const response = await fetch("/admin/timerPause"); // Call API Endpoint /admin/timerStart
if(response.status == 200) { if(response.status == 200) {
@ -63,6 +64,7 @@ async function timerPause() {
} }
} }
// Request to reset the timer with the specified time
async function timerReset() { async function timerReset() {
let newDuration = document.getElementById("timerSelectDuration").value; let newDuration = document.getElementById("timerSelectDuration").value;
const response = await fetch("/admin/timerReset", { // Call API Endpoint /admin/timerStart with selected new duration in seconds const response = await fetch("/admin/timerReset", { // Call API Endpoint /admin/timerStart with selected new duration in seconds
@ -72,7 +74,19 @@ async function timerReset() {
}); });
const data = await response.json(); // Wait for asyncronous transfer to complete and parse json (which got received by backend) const data = await response.json(); // Wait for asyncronous transfer to complete and parse json (which got received by backend)
console.log(data); // Print received data console.log(data); // Print received data
updateFrontend(data); // Update admin frontend with received values updateTimerFrontend(data); // Update admin frontend with received values
}
// Request to increase or decrease the timer depending on passed value in seconds
async function timerIncDec(value) {
const response = await fetch("/admin/timerIncDec", { // Call API Endpoint /admin/timerIncDec with value in seconds to increase or decrease the timer
method: 'POST',
headers: { "Content-Type": "application/json" },
body: JSON.stringify({value: value})
});
const data = await response.json(); // Wait for asyncronous transfer to complete and parse json (which got received by backend)
console.log(data); // Print received data
updateTimerFrontend(data); // Update admin frontend with received values
} }
// Scoreboardfunctions // Scoreboardfunctions

View File

@ -31,7 +31,12 @@ router.post('/timerReset', function(req, res, next) {
res.json(timer.getValues()); // Respond with all important values to update the frontend res.json(timer.getValues()); // Respond with all important values to update the frontend
}); });
// Express router endpoint to switch the score after halftime router.post('/timerIncDec', function(req, res, next) {
timer.incDec(req.body.value);
res.json(timer.getValues());
});
// Express router endpoint to get important timer values
router.get('/timerGetValues', function(req, res, next) { router.get('/timerGetValues', function(req, res, next) {
res.json(timer.getValues()); // Respond with important values for frontend res.json(timer.getValues()); // Respond with important values for frontend
}); });
@ -46,6 +51,4 @@ router.get('/', function(req, res, next) {
res.render('admin'); res.render('admin');
}); });
module.exports = router; module.exports = router;