Compare commits

..

5 Commits

17 changed files with 345 additions and 77 deletions

View File

@ -2,10 +2,8 @@ const moment = require('moment');
const io = require('./socketio')
let timerInterval;
// let duration = moment.duration(7, 'minutes'); // Initial duration
// let durationLeft = moment.duration(7, 'minutes'); // durationLeft after the timer has been started
let duration = moment.duration(7, 'seconds');
let durationLeft = moment.duration(10, 'seconds');
let duration = moment.duration(7, 'minutes'); // Initial duration
let durationLeft = moment.duration(7, 'minutes'); // durationLeft after the timer has been started
let isPaused = true; // Status of the timer
@ -15,29 +13,38 @@ function pause() {};
function reset() {};
function end() {};
function print() {};
function getValues() {};
function start() {
if(isPaused == true)
{
isPaused = false
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 // Set status of the timer
timerInterval = setInterval(() => { // Create Intervalfunction every 1000ms
durationLeft.subtract(1, 'second'); // Subtract a second from from the timer
console.log(print()); // Print durationLeft
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()
durationLeft.subtract(1, 'second'); // Subtract a second from from the timer
}, 1000);
}
return true
} else
return false
}
function pause() {
isPaused = true; // Set status of the timer
clearInterval(timerInterval); // End the execution
if(!isPaused) {
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
io.sockets.emit('timerDurationLeft', print()) // Emit durationLeft to all connected sockets
}
@ -48,13 +55,21 @@ function end() {
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 seconds = (durationLeft.seconds() < 10) ? "0" + durationLeft.seconds() : durationLeft.seconds(); // Create leading zeros for numbers <10
return minutes + ":" + seconds
}
function getValues() {
return {
isPaused: isPaused,
duration: duration,
durationLeft: durationLeft,
print: print()
};
}
module.exports = {
duration, durationLeft, isPaused, start, pause, reset, end, print
};
duration, durationLeft, isPaused, start, pause, reset, end, print, getValues
}

View File

@ -1,11 +1,83 @@
let wss = "ws://" + window.location.hostname + ":3001" // Build socketio endpoint from window.location
const socket = io(wss); // Connect to socketio server
function timerStart()
{
fetch("/admin/timerStart", {
method: "POST",
headers: {'Content-Type': 'text/html'},
body: ""
}).then(res => {
console.log(res);
})
socket.on('connected', (message) => { // Once client is connected to websocket
console.log("Connected");
initialUpdate() // Request important values to update admin frontend
});
socket.on('timerDurationLeft', (message) => {
console.log(message) // Log durationLeft in client console
document.getElementById("durationLeft").innerHTML = message; // Display durationLeft in corresponding html Element
});
socket.on('timerEnded', (message) => {
initialUpdate(); // Update admin frontend to set correct buttonstatus
});
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
if(values.isPaused) { // Set button state depending on timer status
document.getElementById("timerStartBtn").disabled = false
document.getElementById("timerPauseBtn").disabled = true
} else {
document.getElementById("timerStartBtn").disabled = true
document.getElementById("timerPauseBtn").disabled = false
}
}
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
}

View File

@ -11,5 +11,5 @@ socket.on('timerDurationLeft', (message) => {
});
socket.on('timerEnded', (message) => {
document.getElementById("durationLeft").innerHTML = "STOP"; // Display "STOP" in same html element as the duration
document.getElementById("durationLeft").innerHTML = "ENDE"; // Display "STOP" in same html element as the duration
});

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

View File

@ -0,0 +1,6 @@
/*!
* Font Awesome Free 6.5.2 by @fontawesome - https://fontawesome.com
* License - https://fontawesome.com/license/free (Icons: CC BY 4.0, Fonts: SIL OFL 1.1, Code: MIT License)
* Copyright 2024 Fonticons, Inc.
*/
:host,:root{--fa-style-family-classic:"Font Awesome 6 Free";--fa-font-solid:normal 900 1em/1 "Font Awesome 6 Free"}@font-face{font-family:"Font Awesome 6 Free";font-style:normal;font-weight:900;font-display:block;src:url(../webfonts/fa-solid-900.woff2) format("woff2"),url(../webfonts/fa-solid-900.ttf) format("truetype")}.fa-solid,.fas{font-weight:900}

View File

@ -1 +1 @@
@font-face{font-family:seven segment;font-style:normal;font-weight:400;src:local('Seven Segment'),url('fonts/Seven\ Segment.woff') format('woff')}
@font-face{font-family:seven segment;font-style:normal;font-weight:400;src:local('Seven Segment'),url('webfonts/Seven\ Segment.woff') format('woff')}

View File

@ -6,6 +6,7 @@ html, body {
body {
/* padding: 50px; */
font: 14px "Lucida Grande", Helvetica, Arial, sans-serif;
background-color: #47BAEA;
}
a {
@ -17,7 +18,8 @@ a {
font-family: 'Seven Segment', sans-serif;
font-weight: bold;
height: 100%;
font-size: 50vh
font-size: 50vh;
}
.score {

View File

@ -1,29 +1,49 @@
var express = require('express');
var router = express.Router();
let timer = require('../controllers/timer');
var timer = require('../controllers/timer');
router.post('/timerStart', function(req, res, next) {
console.log("Timer gestartet");
timer.start()
res.render('admin');
// Express router entpoint to start the timer
router.get('/timerStart', function(req, res, next) {
if(timer.start()) { // If successfully started 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 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) {
console.log("Timer Zurückgesetzt");
timer.reset();
res.render('admin');
timer.reset(req.body.duration); // Reset the timer with the duration in seconds received from the request
res.json(timer.getValues()); // Respond with all important values to update the frontend
});
router.post('/timerPause', function(req, res, next) {
console.log("Timer pausiert");
timer.pause();
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
});
// Express router endpoint to switch the score after halftime
router.get('/scoreSwitch', function(req, res, next) {
res.render('admin');
});
/* GET home page. */
router.get('/', function(req, res, next) {
res.render('admin', {});
res.render('admin');
});

View File

@ -3,23 +3,18 @@ var router = express.Router();
let io = require('../controllers/socketio')
const timer = require('../controllers/timer')
/* GET home page. */
router.get('/', function(req, res, next) {
res.render('index', { durationLeft: timer.print()});
res.render('index', { durationLeft: timer.print()});
});
io.on('connection', (socket) => {
console.log("A user connected");
socket.emit("Hello user from server");
socket.on('message', (message) => {
console.log(message)
})
console.log("A user connected");
socket.emit("Hello user from server");
socket.on('message', (message) => {
console.log(message)
})
})

View File

@ -4,27 +4,170 @@
<title>Scoreboard Admin</title>
<link rel='stylesheet' href='/stylesheets/bootstrap/bootstrap.min.css' />
<link rel='stylesheet' href='/stylesheets/style.css' />
<link href="/stylesheets/fontawesome/fontawesome.min.css" rel="stylesheet" />
<link href="/stylesheets/fontawesome/brands.min.css" rel="stylesheet" />
<link href="/stylesheets/fontawesome/solid.min.css" rel="stylesheet" />
</head>
<body>
Interface zur Bedienung des Zeit-/Scoreboards.
<div class="row">
<div class="col-lg-4">
<form method="post" action="/admin/timerStart">
<button class="btn btn-lg btn-success" type="submit" name="startButton">Start</button>
</form>
<body onload="initialUpdate()">
<div class="container">
<div class="row justify-content-center">
<div class="col-sm-12 col-md-4">
<div class="card text-center">
<div class="card-body">
<h1 id="durationLeft"></h1>
</div>
</div>
</div>
</div>
<div class="col-lg-4">
<form method="post" action="/admin/timerPause">
<button class="btn btn-lg btn-warning" type="submit" name="pauseButton">Pause</button>
</form>
</br>
<div class="row">
<div class="col-sm-12 col-md-4">
<div class="card text-center">
<div class="card-body">
<button class="btn btn-lg btn-success" onclick="timerStart()" id="timerStartBtn"><i class="fa-solid fa-play"></i> Start</button>
<button class="btn btn-lg btn-warning" onclick="timerPause()" id="timerPauseBtn"><i class="fa-solid fa-pause"></i> Pause</button>
</div>
</div>
</div>
<div class="col-sm-12 col-md-4">
<div class="card text-center">
<div class="card-body">
<div class="btn-group" role="group" aria-label="Basic example">
<button type="button" class="btn btn-secondary">-1m</button>
<button type="button" class="btn btn-secondary">-10s</button>
<button type="button" class="btn btn-secondary">-1s</button>
<button type="button" class="btn btn-secondary">+1s</button>
<button type="button" class="btn btn-secondary">+10s</button>
<button type="button" class="btn btn-secondary">+1m</button>
</div>
</div>
</div>
</div>
<div class="col-sm-12 col-md-4">
<div class="card text-center">
<div class="card-body">
<button class="btn btn-lg btn-danger" data-bs-toggle="modal" data-bs-target="#timerResetModal">
<i class="fa-solid fa-rotate-right"></i> Zurücksetzen
</button>
</div>
</div>
</div>
</div>
<div class="col-lg-4">
<form method="post" action="/admin/timerReset">
<button class="btn btn-lg btn-danger" type="submit" name="resetButton">Reset</button>
</form>
</br></br></br></br></br></br>
<div class="row text-center">
<div class="col-sm-12 col-md-12 col-lg-12">
<button class="btn btn-lg btn-danger" onclick="scoreSwitch()"><i class="fa-solid fa-rotate"></i> Seitenwechsel</button>
</div>
</div>
</div>
</body>
<script src='/javascripts/bootstrap/bootstrap.bundle.min.js'></script>
<script src="/javascripts/socket.io.min.js"></script>
<script src="/javascripts/moment.js"></script>
<script src="/javascripts/admin.js"></script>
<!-- Modal -->
<div class="modal fade" id="timerResetModal" tabindex="-1" aria-labelledby="timerResetModalLabel" aria-hidden="true">
<div class="modal-dialog modal-dialog-centered">
<div class="modal-content">
<div class="modal-header">
<h1 class="modal-title fs-5" id="timerResetModalLabel">Timer zurücksetzen?</h1>
<button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
</div>
<div class="modal-body">
<p>Möchtest du den Timer wirklich zurücksetzen?</p>
<p>Gewünschte Zeit:
<select class="form-select" id="timerSelectDuration" aria-label="Default select example">
<option value="0">0:00</option>
<option value="15">0:15</option>
<option value="30">0:30</option>
<option value="45">0:45</option>
<option value="60">1:00</option>
<option value="75">1:15</option>
<option value="90">1:30</option>
<option value="105">1:45</option>
<option value="120">2:00</option>
<option value="135">2:15</option>
<option value="150">2:30</option>
<option value="165">2:45</option>
<option value="180">3:00</option>
<option value="195">3:15</option>
<option value="210">3:30</option>
<option value="225">3:45</option>
<option value="240">4:00</option>
<option value="255">4:15</option>
<option value="270">4:30</option>
<option value="285">4:45</option>
<option value="300">5:00</option>
<option value="315">5:15</option>
<option value="330">5:30</option>
<option value="345">5:45</option>
<option value="360">6:00</option>
<option value="375">6:15</option>
<option value="390">6:30</option>
<option value="405">6:45</option>
<option selected value="420">7:00</option>
<option value="435">7:15</option>
<option value="450">7:30</option>
<option value="465">7:45</option>
<option value="480">8:00</option>
<option value="495">8:15</option>
<option value="510">8:30</option>
<option value="525">8:45</option>
<option value="540">9:00</option>
<option value="555">9:15</option>
<option value="570">9:30</option>
<option value="585">9:45</option>
<option value="600">10:00</option>
</select>
</p>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-danger" data-bs-dismiss="modal" onclick="timerReset()">Zurücksetzen</button>
<button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Schließen</button>
</div>
</div>
</div>
</div>
<!-- Modal -->
<!-- Timer start error toast -->
<div class="toast-container position-fixed bottom-0 end-0 p-3">
<div id="timerStartErrorToast" class="toast" role="alert" aria-live="assertive" aria-atomic="true">
<div class="toast-header">
<i class="fa-solid fa-triangle-exclamation"> </i>
<strong class="me-auto"> Fehler</strong>
<small></small>
<button type="button" class="btn-close" data-bs-dismiss="toast" aria-label="Close"></button>
</div>
<div class="toast-body">
Der Timer ist bereits abgelaufen. Zum neustarten bitte zurücksetzen!
</div>
</div>
</div>
<!-- Timer start error toast -->
<!-- Timer pause error toast -->
<div class="toast-container position-fixed bottom-0 end-0 p-3">
<div id="timerPauseErrorToast" class="toast" role="alert" aria-live="assertive" aria-atomic="true">
<div class="toast-header">
<i class="fa-solid fa-triangle-exclamation"> </i>
<strong class="me-auto"> Fehler</strong>
<small></small>
<button type="button" class="btn-close" data-bs-dismiss="toast" aria-label="Close"></button>
</div>
<div class="toast-body">
Der Timer ist bereits pausiert.
</div>
</div>
</div>
<!-- Timer pause error toast -->
</html>

View File

@ -8,19 +8,19 @@
</head>
<body>
<div class="d-flex flex-column align-items-center justify-content-center box-time" style="background-color:brown; ">
<div class="d-flex flex-column align-items-center justify-content-center box-time">
<div id="durationLeft" class="durationLeft">{{ durationLeft }}</div>
</div>
<div class="d-flex flex-sm-wrap flex-md-wrap text-center align-items-center justify-content-around box-etc" style="background-color:blueviolet;">
<div style="background-color:crimson;" class="flex-fill order-sm-1 order-md-1 order-lg-0">
<h1>L<br/>LLLLLLLLLLLLLL<br/>L<br/></h>
<div class="d-flex flex-sm-wrap flex-md-wrap text-center align-items-center justify-content-around box-etc">
<div class="flex-fill order-xs-1 order-sm-1 order-md-1 order-lg-0">
<h1>Team A</h>
</div>
<div style="background-color: darkgreen" class="flex-fill order-sm-0 order-md-0 order-lg-1">
<div class="flex-fill order-xs-0 order-sm-0 order-md-0 order-lg-1">
<div id="score" class="score">5:2</div>
</div>
<div style="background-color:coral" class="flex-fill order-sm-2 order-md-2 order-lg-2">
<h1>R<br/>R<br/>RRRRRRRRRRRRR<br/></h>
<div class="flex-fill order-xs-2 order-sm-2 order-md-2 order-lg-2">
<h1>Team B</h>
</div>
</div>