timerIncDec beendete den Timer auch beim Hinzufuegen von Zeit: die Pruefung verglich den Betrag des Werts mit der Restzeit, ohne die Richtung zu beachten. Bei 5s Restzeit fuehrte "+10s" damit zum Spielende statt zu 15s. Die Pruefung gilt jetzt nur noch fuer negative Werte. Beim Verringern unter 0 wird die Restzeit zusaetzlich sauber auf 00:00 gesetzt, sonst blieb sie stehen und der Timer war trotz "ENDE" auf dem Monitor wieder startbar. Im Debugfenster zeigten alle drei Label auf scoreSwitchEnable. Ein Klick auf den Text "Teamanzeige spiegeln" oder "Splashscreen anzeigen" blendete damit das Scoreboard aus. Jede Beschriftung zeigt jetzt auf ihren eigenen Schalter. scoreSetScore sendet den neuen Stand jetzt wie alterScore und clearScore an die Monitore; der Endpunkt wird vom Panel nicht verwendet, ist aber dokumentiert. alterScore laesst den Spielstand nicht mehr unter 0 laufen. Die toten Funktionsprototypen in timer.js und score.js sind entfernt: sie wurden durch Hoisting ohnehin ueberschrieben und listeten toggleReferenceMirrored und print schon nicht mehr auf. Ebenso die Wert-Snapshots duration, durationLeft und isPaused aus module.exports, die dauerhaft die Startwerte lieferten. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
89 lines
No EOL
3.8 KiB
JavaScript
89 lines
No EOL
3.8 KiB
JavaScript
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 isPaused = true; // Status of the timer
|
|
|
|
// Start timer not paused and not already finished
|
|
function start() {
|
|
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.asSeconds() == 0) // End timer if durationLeft == 00:00
|
|
end()
|
|
}, 1000);
|
|
return true
|
|
} else
|
|
return false
|
|
}
|
|
|
|
// If possible pause timer, if not return false
|
|
function pause() {
|
|
if(!isPaused) {
|
|
console.log("Timer pausiert");
|
|
isPaused = true; // Set status of the timer
|
|
clearInterval(timerInterval); // End the execution
|
|
return true
|
|
} else
|
|
return false
|
|
}
|
|
|
|
// Reset timer to passed value, and send durationLeft to all clients.
|
|
// Ein laufender Timer wird bewusst nicht angehalten, sondern zaehlt von der
|
|
// neuen Zeit weiter herunter.
|
|
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
|
|
}
|
|
|
|
// Stop timer and emit timerEndet to all clients
|
|
function end() {
|
|
isPaused = true // Set status of the timer
|
|
clearInterval(timerInterval); // End the execution
|
|
io.sockets.emit('timerEnded', print()) // Emit end to all connected sockets
|
|
}
|
|
|
|
// Return formatted timestamp in the format of 00:00
|
|
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
|
|
}
|
|
|
|
// Increase or decrease the timer depending on the passed value in seconds
|
|
function incDec(value) {
|
|
if(value < 0 && Math.abs(value) >= durationLeft.asSeconds()) { // Nur beim Verringern: faellt die Restzeit auf 0 oder darunter, Timer beenden
|
|
durationLeft = moment.duration(0, 'seconds'); // Restzeit sauber auf 00:00, sonst bliebe der Timer trotz "ENDE" startbar
|
|
end();
|
|
} else {
|
|
durationLeft.add(value, 'second');
|
|
io.sockets.emit('timerDurationLeft', print())
|
|
}
|
|
}
|
|
|
|
// Return all important timer values
|
|
function getValues() {
|
|
return {
|
|
isPaused: isPaused,
|
|
duration: duration,
|
|
durationLeft: durationLeft,
|
|
print: print()
|
|
};
|
|
}
|
|
|
|
// duration, durationLeft und isPaused werden bewusst nicht exportiert: Exporte
|
|
// werden beim Laden des Moduls einmal ausgewertet und wuerden dauerhaft die
|
|
// Startwerte liefern. Der aktuelle Stand kommt ueber getValues().
|
|
module.exports = {
|
|
start, pause, reset, end, print, incDec, getValues
|
|
} |