Added important admin frontend functions with error handling, the corresponding api endpoints, and the timer logic
This commit is contained in:
parent
340ab1236e
commit
536c1272d8
@ -16,26 +16,35 @@ function print() {};
|
|||||||
|
|
||||||
|
|
||||||
function start() {
|
function start() {
|
||||||
if(isPaused == true)
|
if(isPaused == true && durationLeft.asSeconds() != 0) { // Only allow start if timer ist currently paused and durationLeft is not 0 seconds
|
||||||
{
|
console.log("Timer gestartet")
|
||||||
isPaused = false
|
isPaused = false // Set status of the timer
|
||||||
|
|
||||||
timerInterval = setInterval(() => { // Create Intervalfunction every 1000ms
|
timerInterval = setInterval(() => { // Create Intervalfunction every 1000ms
|
||||||
|
durationLeft.subtract(1, 'second'); // Subtract a second from from the timer
|
||||||
console.log(print()); // Print durationLeft
|
console.log(print()); // Print durationLeft
|
||||||
io.sockets.emit('timerDurationLeft', print()) // Emit durationLeft to all connected sockets
|
io.sockets.emit('timerDurationLeft', print()) // Emit durationLeft to all connected sockets
|
||||||
if(durationLeft.minutes() == 0 && durationLeft.seconds() == 0) // End timer if durationLeft == 00:00
|
if(durationLeft.asSeconds() == 0) // End timer if durationLeft == 00:00
|
||||||
end()
|
end()
|
||||||
durationLeft.subtract(1, 'second'); // Subtract a second from from the timer
|
|
||||||
}, 1000);
|
}, 1000);
|
||||||
}
|
return true
|
||||||
|
} else
|
||||||
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
function pause() {
|
function pause() {
|
||||||
isPaused = true; // Set status of the timer
|
if(!isPaused) {
|
||||||
clearInterval(timerInterval); // End the execution
|
console.log("Timer pausiert");
|
||||||
|
isPaused = true; // Set status of the timer
|
||||||
|
clearInterval(timerInterval); // End the execution
|
||||||
|
return true
|
||||||
|
} else
|
||||||
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
function reset() {
|
function reset(newDuration) {
|
||||||
|
console.log("Timer Zurückgesetzt");
|
||||||
|
duration = moment.duration(newDuration, 'seconds').clone(); // Set initial duration to received duration in seconds
|
||||||
durationLeft = duration.clone(); // Set durationLeft to the initial duration
|
durationLeft = duration.clone(); // Set durationLeft to the initial duration
|
||||||
io.sockets.emit('timerDurationLeft', print()) // Emit durationLeft to all connected sockets
|
io.sockets.emit('timerDurationLeft', print()) // Emit durationLeft to all connected sockets
|
||||||
}
|
}
|
||||||
@ -46,8 +55,7 @@ function end() {
|
|||||||
io.sockets.emit('timerEnded', print()) // Emit end to all connected sockets
|
io.sockets.emit('timerEnded', print()) // Emit end to all connected sockets
|
||||||
}
|
}
|
||||||
|
|
||||||
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
|
||||||
|
|||||||
@ -1,11 +1,66 @@
|
|||||||
|
function updateFrontend(values) {
|
||||||
|
values.duration = moment.duration(values.duration) // Create moment object from ISO 8601 string
|
||||||
|
values.durationLeft = moment.duration(values.durationLeft) // Create moment object from ISO 8601 string
|
||||||
|
document.getElementById("durationLeft").innerHTML = values.print // Display durationLeft as prettyfied string
|
||||||
|
|
||||||
function timerStart()
|
if(values.isPaused) { // Set button state depending on timer status
|
||||||
{
|
document.getElementById("timerStartBtn").disabled = false
|
||||||
fetch("/admin/timerStart", {
|
document.getElementById("timerPauseBtn").disabled = true
|
||||||
method: "POST",
|
} else {
|
||||||
headers: {'Content-Type': 'text/html'},
|
document.getElementById("timerStartBtn").disabled = true
|
||||||
body: ""
|
document.getElementById("timerPauseBtn").disabled = false
|
||||||
}).then(res => {
|
}
|
||||||
console.log(res);
|
}
|
||||||
})
|
|
||||||
|
async function initialUpdate() {
|
||||||
|
const response = await fetch("/admin/timerGetValues"); // Call API Endpoint /admin/timerGetValues
|
||||||
|
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
|
||||||
|
updateFrontend(data); // Update admin frontend with received values
|
||||||
|
}
|
||||||
|
|
||||||
|
// Timerfunctions
|
||||||
|
async function timerStart() {
|
||||||
|
const response = await fetch("/admin/timerStart"); // Call API Endpoint /admin/timerStart
|
||||||
|
if(response.status == 200) {
|
||||||
|
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
|
||||||
|
updateFrontend(data); // Update admin frontend with received values
|
||||||
|
} else {
|
||||||
|
//console.log("Error, Timer bereits abgelaufen: ", response.status);
|
||||||
|
const timerStartErrorToast = bootstrap.Toast.getOrCreateInstance(document.getElementById("timerStartErrorToast")); // Create toast instance byId timerStartErrorToast
|
||||||
|
timerStartErrorToast.show(); // Show error toast
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
async function timerPause() {
|
||||||
|
const response = await fetch("/admin/timerPause"); // Call API Endpoint /admin/timerStart
|
||||||
|
if(response.status == 200) {
|
||||||
|
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
|
||||||
|
updateFrontend(data); // Update admin frontend with received values
|
||||||
|
} else {
|
||||||
|
//console.log("Error, Timer bereits pausiert: ", response.status);
|
||||||
|
const timerPauseErrorToast = bootstrap.Toast.getOrCreateInstance(document.getElementById("timerPauseErrorToast")); // Create toast instance byId timerStartErrorToast
|
||||||
|
timerPauseErrorToast.show(); // Show error toast
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
async function timerReset() {
|
||||||
|
let newDuration = document.getElementById("timerSelectDuration").value;
|
||||||
|
const response = await fetch("/admin/timerReset", { // Call API Endpoint /admin/timerStart with selected new duration in seconds
|
||||||
|
method: 'POST',
|
||||||
|
headers: { "Content-Type": "application/json" },
|
||||||
|
body: JSON.stringify({duration: newDuration})
|
||||||
|
});
|
||||||
|
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
|
||||||
|
updateFrontend(data); // Update admin frontend with received values
|
||||||
|
}
|
||||||
|
|
||||||
|
// Scoreboardfunctions
|
||||||
|
async function scoreSwitch() {
|
||||||
|
const response = await fetch("/admin/scoreSwitch"); // Call API Endpoint /admin/timerStart
|
||||||
|
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
|
||||||
}
|
}
|
||||||
@ -3,27 +3,45 @@ var router = express.Router();
|
|||||||
|
|
||||||
let timer = require('../controllers/timer');
|
let timer = require('../controllers/timer');
|
||||||
|
|
||||||
router.post('/timerStart', function(req, res, next) {
|
// Express router entpoint to start the timer
|
||||||
console.log("Timer gestartet");
|
router.get('/timerStart', function(req, res, next) {
|
||||||
timer.start()
|
if(timer.start()) { // If successfully started the timer
|
||||||
res.render('admin');
|
res.status(200); // Set http status code to 200 (success)
|
||||||
|
res.json(timer.getValues()); // Respond with all important values to update the frontend
|
||||||
|
} else { // If not successfull, for example because timer is already running or not time is left
|
||||||
|
res.status(406); // Set http status code to 406 (not acceptable)
|
||||||
|
res.send(); // Respond with empty payload
|
||||||
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
// Express router endpoint to pause the timer
|
||||||
|
router.get('/timerPause', function(req, res, next) {
|
||||||
|
if(timer.pause()) { // If successfully paused the timer
|
||||||
|
res.status(200); // Set http status code to 200 (success)
|
||||||
|
res.json(timer.getValues()); // Respond with all important values to update the frontend
|
||||||
|
} else { // If not successfull, for example if timer already is paused
|
||||||
|
res.status(406); // Set http status code to 406 (not acceptable)
|
||||||
|
res.send(); // Respond with empty payload
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
// Express router endpoint to reset the timer
|
||||||
router.post('/timerReset', function(req, res, next) {
|
router.post('/timerReset', function(req, res, next) {
|
||||||
console.log("Timer Zurückgesetzt");
|
timer.reset(req.body.duration); // Reset the timer with the duration in seconds received from the request
|
||||||
timer.reset();
|
res.json(timer.getValues()); // Respond with all important values to update the frontend
|
||||||
res.render('admin');
|
});
|
||||||
|
|
||||||
|
// Express router endpoint to switch the score after halftime
|
||||||
|
router.get('/timerGetValues', function(req, res, next) {
|
||||||
|
res.json(timer.getValues()); // Respond with important values for frontend
|
||||||
});
|
});
|
||||||
|
|
||||||
router.post('/timerPause', function(req, res, next) {
|
|
||||||
console.log("Timer pausiert");
|
|
||||||
timer.pause();
|
|
||||||
res.render('admin');
|
res.render('admin');
|
||||||
});
|
});
|
||||||
|
|
||||||
/* GET home page. */
|
/* GET home page. */
|
||||||
router.get('/', function(req, res, next) {
|
router.get('/', function(req, res, next) {
|
||||||
res.render('admin', {});
|
res.render('admin');
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user