More smoothness and persistence

This commit is contained in:
MeBeBruno committed 2026-10-04 22:47:53 +02:00
1 parent f924016478
commit aeb9173825
10 files changed
+2113 -1776

No files matched your search

+384 -299
View File
@@ -1,330 +1,415 @@
let websocket_connection;
let pending_administrator_form = null;
let stopwatch_interval_identifier = null;
const application_state = {
websocket_connection: null,
pending_administrator_form: null,
stopwatch_interval_identifier: null
};
function initialize_lucide_icons() {
if (typeof lucide !== "undefined") {
lucide.createIcons();
}
/* --- Deterministische Funktionen ("ehrliche" Funktionen) --- */
function calculate_time_difference(start_time_milliseconds, current_time_milliseconds) {
return current_time_milliseconds - start_time_milliseconds;
}
function update_game_stopwatch() {
const stopwatch_element = document.getElementById("game-stopwatch");
if (!stopwatch_element) {
return;
}
function format_stopwatch_string(difference_in_milliseconds) {
if (difference_in_milliseconds < 0) {
return '00:00 Minuten';
}
const start_time_string = stopwatch_element.getAttribute("data-start-time");
if (!start_time_string) {
return;
}
const total_seconds = Math.floor(difference_in_milliseconds / 1000);
const days_passed = Math.floor(total_seconds / 86400);
const hours_passed = Math.floor((total_seconds % 86400) / 3600);
const minutes_passed = Math.floor((total_seconds % 3600) / 60);
const seconds_passed = total_seconds % 60;
const start_date = new Date(start_time_string);
if (isNaN(start_date.getTime())) {
return;
}
let output_string = '';
const current_date = new Date();
const difference_in_milliseconds = current_date.getTime() - start_date.getTime();
if (days_passed === 1) {
output_string += '1 Tag und ';
} else if (days_passed > 1) {
output_string += days_passed + ' Tage und ';
}
if (difference_in_milliseconds < 0) {
stopwatch_element.textContent = "00:00 Minuten";
return;
}
const padded_minutes = String(minutes_passed).padStart(2, '0');
const padded_seconds = String(seconds_passed).padStart(2, '0');
const total_seconds = Math.floor(difference_in_milliseconds / 1000);
const days_passed = Math.floor(total_seconds / 86400);
const hours_passed = Math.floor((total_seconds % 86400) / 3600);
const minutes_passed = Math.floor((total_seconds % 3600) / 60);
const seconds_passed = total_seconds % 60;
if (hours_passed === 1) {
output_string += '1:' + padded_minutes + ':' + padded_seconds + ' Stunde';
} else if (hours_passed > 1) {
output_string += hours_passed + ':' + padded_minutes + ':' + padded_seconds + ' Stunden';
} else {
output_string += padded_minutes + ':' + padded_seconds + ' Minuten';
}
let output_string = "";
if (days_passed === 1) {
output_string += "1 Tag und ";
} else if (days_passed > 1) {
output_string += days_passed + " Tage und ";
}
const padded_minutes = String(minutes_passed).padStart(2, "0");
const padded_seconds = String(seconds_passed).padStart(2, "0");
if (hours_passed === 1) {
output_string += "1:" + padded_minutes + ":" + padded_seconds + " Stunde";
} else if (hours_passed > 1) {
output_string += hours_passed + ":" + padded_minutes + ":" + padded_seconds + " Stunden";
} else {
output_string += padded_minutes + ":" + padded_seconds + " Minuten";
}
stopwatch_element.textContent = output_string;
return output_string;
}
function initialize_application_behavior() {
initialize_lucide_icons();
if (stopwatch_interval_identifier) {
clearInterval(stopwatch_interval_identifier);
}
stopwatch_interval_identifier = setInterval(update_game_stopwatch, 1000);
update_game_stopwatch();
const tab_buttons = document.querySelectorAll(".tab-button");
tab_buttons.forEach(function(current_button) {
current_button.addEventListener("click", function() {
const target_identifier = current_button.getAttribute("data-target");
const parent_container = current_button.closest(".application-content");
parent_container.querySelectorAll(".tab-button").forEach(function(button_element) {
button_element.classList.remove("active-tab");
});
parent_container.querySelectorAll(".tab-content").forEach(function(content_element) {
content_element.classList.remove("active-content");
});
current_button.classList.add("active-tab");
const target_panel = document.getElementById(target_identifier);
if (target_panel) {
target_panel.classList.add("active-content");
}
});
});
function synchronize_element_nodes(current_node, updated_node) {
if (current_node.nodeType !== updated_node.nodeType) {
current_node.replaceWith(updated_node.cloneNode(true));
return;
}
if (current_node.nodeType === Node.TEXT_NODE) {
if (current_node.nodeValue !== updated_node.nodeValue) {
current_node.nodeValue = updated_node.nodeValue;
}
return;
}
if (current_node.nodeType !== Node.ELEMENT_NODE) {
return;
}
const player_forms = document.querySelectorAll(".asynchronous-form");
player_forms.forEach(function(current_form) {
current_form.addEventListener("submit", function(form_submission_event) {
form_submission_event.preventDefault();
execute_form_submission(current_form);
});
});
if (current_node.tagName !== updated_node.tagName) {
current_node.replaceWith(updated_node.cloneNode(true));
return;
}
if (current_node.namespaceURI !== updated_node.namespaceURI) {
current_node.replaceWith(updated_node.cloneNode(true));
return;
}
const administrator_forms = document.querySelectorAll(".admin-asynchronous-form");
administrator_forms.forEach(function(current_form) {
current_form.addEventListener("submit", function(form_submission_event) {
form_submission_event.preventDefault();
pending_administrator_form = current_form;
const administrator_modal = document.getElementById("admin-confirmation-modal");
if (administrator_modal) {
const time_input = document.getElementById("modal_execution_time");
if (time_input) {
time_input.value = "";
}
administrator_modal.classList.add("active-modal");
}
});
});
const is_active_input = document.activeElement === current_node;
const is_input_element = ['INPUT', 'TEXTAREA', 'SELECT'].includes(current_node.tagName);
const confirm_administrator_button = document.getElementById("confirm-admin-action");
if (confirm_administrator_button) {
confirm_administrator_button.addEventListener("click", function() {
if (pending_administrator_form) {
const time_input = document.getElementById("modal_execution_time");
if (time_input && time_input.value) {
let hidden_time_input = pending_administrator_form.querySelector("input[name='execution_time']");
if (!hidden_time_input) {
hidden_time_input = document.createElement("input");
hidden_time_input.type = "hidden";
hidden_time_input.name = "execution_time";
pending_administrator_form.appendChild(hidden_time_input);
}
hidden_time_input.value = time_input.value;
} else {
let hidden_time_input = pending_administrator_form.querySelector("input[name='execution_time']");
if (hidden_time_input) {
hidden_time_input.remove();
}
}
execute_form_submission(pending_administrator_form);
pending_administrator_form = null;
const administrator_modal = document.getElementById("admin-confirmation-modal");
if (administrator_modal) {
administrator_modal.classList.remove("active-modal");
}
}
});
}
if (is_input_element && !is_active_input) {
if (current_node.value !== updated_node.value) {
current_node.value = updated_node.value;
}
}
const cancel_administrator_button = document.getElementById("cancel-admin-action");
if (cancel_administrator_button) {
cancel_administrator_button.addEventListener("click", function() {
pending_administrator_form = null;
const administrator_modal = document.getElementById("admin-confirmation-modal");
if (administrator_modal) {
administrator_modal.classList.remove("active-modal");
}
});
}
const current_attributes = current_node.attributes;
const updated_attributes = updated_node.attributes;
const trigger_elimination_button = document.getElementById("trigger-elimination");
if (trigger_elimination_button) {
trigger_elimination_button.addEventListener("click", function() {
const elimination_modal = document.getElementById("elimination-confirm-modal");
if (elimination_modal) {
elimination_modal.classList.add("active-modal");
}
});
}
for (let index = 0; index < updated_attributes.length; index++) {
const attribute = updated_attributes[index];
if (current_node.getAttribute(attribute.name) !== attribute.value) {
if (!(is_input_element && is_active_input && attribute.name === 'value')) {
current_node.setAttribute(attribute.name, attribute.value);
}
}
}
const proceed_elimination_button = document.getElementById("proceed-elimination");
if (proceed_elimination_button) {
proceed_elimination_button.addEventListener("click", function() {
websocket_connection.emit("request_elimination");
const elimination_modal = document.getElementById("elimination-confirm-modal");
if (elimination_modal) {
elimination_modal.classList.remove("active-modal");
}
const waiting_modal = document.getElementById("waiting-modal");
if (waiting_modal) {
waiting_modal.classList.add("active-modal");
}
});
}
for (let index = current_attributes.length - 1; index >= 0; index--) {
const attribute = current_attributes[index];
if (!updated_node.hasAttribute(attribute.name)) {
current_node.removeAttribute(attribute.name);
}
}
const cancel_elimination_button = document.getElementById("cancel-elimination");
if (cancel_elimination_button) {
cancel_elimination_button.addEventListener("click", function() {
const elimination_modal = document.getElementById("elimination-confirm-modal");
if (elimination_modal) {
elimination_modal.classList.remove("active-modal");
}
});
}
const current_children = Array.from(current_node.childNodes);
const updated_children = Array.from(updated_node.childNodes);
const max_length = Math.max(current_children.length, updated_children.length);
const confirm_button = document.getElementById("confirm-elimination");
if (confirm_button) {
confirm_button.addEventListener("click", function() {
websocket_connection.emit("confirm_elimination");
const modal_element = document.getElementById("verification-modal");
if (modal_element) {
modal_element.classList.remove("active-modal");
}
});
}
for (let index = 0; index < max_length; index++) {
const current_child = current_children[index];
const updated_child = updated_children[index];
const reject_button = document.getElementById("reject-elimination");
if (reject_button) {
reject_button.addEventListener("click", function() {
websocket_connection.emit("reject_elimination");
const modal_element = document.getElementById("verification-modal");
if (modal_element) {
modal_element.classList.remove("active-modal");
}
});
}
const acknowledge_rejection_button = document.getElementById("acknowledge-rejection");
if (acknowledge_rejection_button) {
acknowledge_rejection_button.addEventListener("click", function() {
const rejection_modal = document.getElementById("rejection-modal");
if (rejection_modal) {
rejection_modal.classList.remove("active-modal");
}
});
}
const open_name_modal_button = document.getElementById("trigger-name-change-modal");
if (open_name_modal_button) {
open_name_modal_button.addEventListener("click", function() {
const name_modal = document.getElementById("name-change-modal");
if (name_modal) {
name_modal.classList.add("active-modal");
}
});
}
const close_name_modal_button = document.getElementById("cancel-name-change");
if (close_name_modal_button) {
close_name_modal_button.addEventListener("click", function() {
const name_modal = document.getElementById("name-change-modal");
if (name_modal) {
name_modal.classList.remove("active-modal");
}
});
}
if (!current_child && updated_child) {
current_node.appendChild(updated_child.cloneNode(true));
} else if (current_child && !updated_child) {
current_node.removeChild(current_child);
} else if (current_child && updated_child) {
synchronize_element_nodes(current_child, updated_child);
}
}
}
function execute_form_submission(target_form) {
const active_tab_element = document.querySelector(".active-tab");
let active_target = "control-panel";
if (active_tab_element) {
active_target = active_tab_element.getAttribute("data-target");
}
fetch(target_form.action, {
method: target_form.method,
body: new FormData(target_form)
})
.then(function(network_response) { return network_response.text(); })
.then(function(markup_text) {
replace_main_content(markup_text);
const new_tab_button = document.querySelector(".tab-button[data-target='" + active_target + "']");
if (new_tab_button) {
new_tab_button.click();
}
});
/* --- Architektur & State-Management ("unehrliche" Funktionen) --- */
function trigger_dom_update(markup_text) {
const document_parser = new DOMParser();
const parsed_document = document_parser.parseFromString(markup_text, 'text/html');
const current_container = document.querySelector('.application-content');
const new_container = parsed_document.querySelector('.application-content');
if (!current_container || !new_container) {
return;
}
const current_active_tab = document.querySelector('.active-tab');
const active_tab_target = current_active_tab ? current_active_tab.getAttribute('data-target') : null;
const active_modals = [];
document.querySelectorAll('.active-modal').forEach(function(modal) {
active_modals.push(modal.id);
});
synchronize_element_nodes(current_container, new_container);
if (active_tab_target) {
document.querySelectorAll('.tab-button').forEach(function(button) { button.classList.remove('active-tab'); });
document.querySelectorAll('.tab-content').forEach(function(content) { content.classList.remove('active-content'); });
const restored_tab = document.querySelector('.tab-button[data-target="' + active_tab_target + '"]');
const restored_content = document.getElementById(active_tab_target);
if (restored_tab && restored_content) {
restored_tab.classList.add('active-tab');
restored_content.classList.add('active-content');
}
}
active_modals.forEach(function(modal_identifier) {
const restored_modal = document.getElementById(modal_identifier);
if (restored_modal) {
restored_modal.classList.add('active-modal');
}
});
if (typeof lucide !== 'undefined') {
lucide.createIcons();
}
}
function replace_main_content(markup_text) {
const document_parser = new DOMParser();
const parsed_document = document_parser.parseFromString(markup_text, "text/html");
const current_container = document.querySelector(".application-content");
const new_container = parsed_document.querySelector(".application-content");
if (current_container && new_container) {
current_container.innerHTML = new_container.innerHTML;
initialize_application_behavior();
}
function fetch_and_synchronize_dom() {
fetch(window.location.href)
.then(function(network_response) {
if (!network_response.ok) {
throw new Error('Network response failure');
}
return network_response.text();
})
.then(function(markup_text) {
trigger_dom_update(markup_text);
})
.catch(function(error_object) {
console.error('DOM Synchronization failed:', error_object);
});
}
document.addEventListener("DOMContentLoaded", function() {
if ("serviceWorker" in navigator) {
navigator.serviceWorker.register("/service_worker.js");
}
function execute_asynchronous_form_submission(target_form) {
fetch(target_form.action, {
method: target_form.method,
body: new FormData(target_form)
})
.then(function(network_response) {
if (!network_response.ok) {
throw new Error('Form submission failure');
}
return network_response.text();
})
.then(function(markup_text) {
trigger_dom_update(markup_text);
})
.catch(function(error_object) {
console.error('Form execution failed:', error_object);
});
}
if (typeof io !== "undefined") {
websocket_connection = io();
websocket_connection.on("game_state_changed", function() {
if (document.querySelector(".game-section") || document.querySelector(".administrator-section")) {
fetch(window.location.href)
.then(function(network_response) { return network_response.text(); })
.then(function(markup_text) {
replace_main_content(markup_text);
});
}
});
function update_game_stopwatch_ui() {
const stopwatch_element = document.getElementById('game-stopwatch');
if (!stopwatch_element) {
return;
}
websocket_connection.on("admin_state_changed", function() {
if (document.querySelector(".administrator-section")) {
fetch(window.location.href)
.then(function(network_response) { return network_response.text(); })
.then(function(markup_text) {
replace_main_content(markup_text);
});
}
});
const start_time_string = stopwatch_element.getAttribute('data-start-time');
if (!start_time_string) {
return;
}
websocket_connection.on("elimination_verification_requested", function() {
fetch(window.location.href)
.then(function(network_response) { return network_response.text(); })
.then(function(markup_text) {
replace_main_content(markup_text);
const new_modal_element = document.getElementById("verification-modal");
if (new_modal_element) {
new_modal_element.classList.add("active-modal");
}
});
});
const start_date = new Date(start_time_string);
if (isNaN(start_date.getTime())) {
return;
}
websocket_connection.on("elimination_rejected", function() {
const waiting_modal = document.getElementById("waiting-modal");
if (waiting_modal) {
waiting_modal.classList.remove("active-modal");
}
const rejection_modal = document.getElementById("rejection-modal");
if (rejection_modal) {
rejection_modal.classList.add("active-modal");
}
});
}
initialize_application_behavior();
const current_date = new Date();
const difference = calculate_time_difference(start_date.getTime(), current_date.getTime());
stopwatch_element.textContent = format_stopwatch_string(difference);
}
function modify_modal_state(modal_identifier, is_active) {
const modal = document.getElementById(modal_identifier);
if (modal) {
if (is_active) {
modal.classList.add('active-modal');
} else {
modal.classList.remove('active-modal');
}
}
}
function write_text_to_clipboard(text_payload) {
if (!navigator.clipboard) {
console.error('Clipboard API unvailable.');
return;
}
navigator.clipboard.writeText(text_payload).catch(function(error_object) {
console.error('Clipboard action failed:', error_object);
});
}
/* --- Globale Event-Delegation --- */
function handle_global_clicks(mouse_event) {
const target_element = mouse_event.target;
const tab_button = target_element.closest('.tab-button');
if (tab_button) {
const target_identifier = tab_button.getAttribute('data-target');
const parent_container = tab_button.closest('.application-content');
parent_container.querySelectorAll('.tab-button').forEach(function(button) {
button.classList.remove('active-tab');
});
parent_container.querySelectorAll('.tab-content').forEach(function(content) {
content.classList.remove('active-content');
});
tab_button.classList.add('active-tab');
const target_panel = document.getElementById(target_identifier);
if (target_panel) {
target_panel.classList.add('active-content');
}
return;
}
const action_mapping = {
'trigger-elimination': function() { modify_modal_state('elimination-confirm-modal', true); },
'cancel-elimination': function() { modify_modal_state('elimination-confirm-modal', false); },
'proceed-elimination': function() {
if (application_state.websocket_connection) {
application_state.websocket_connection.emit('request_elimination');
}
modify_modal_state('elimination-confirm-modal', false);
},
'trigger-revoke-elimination': function() {
if (application_state.websocket_connection) {
application_state.websocket_connection.emit('revoke_elimination_request');
}
},
'confirm-elimination': function() {
if (application_state.websocket_connection) {
application_state.websocket_connection.emit('confirm_elimination');
}
modify_modal_state('verification-modal', false);
},
'reject-elimination': function() {
if (application_state.websocket_connection) {
application_state.websocket_connection.emit('reject_elimination');
}
modify_modal_state('verification-modal', false);
},
'acknowledge-rejection': function() { modify_modal_state('rejection-modal', false); },
'trigger-name-change-modal': function() { modify_modal_state('name-change-modal', true); },
'cancel-name-change-modal': function() { modify_modal_state('name-change-modal', false); },
'trigger-help-modal': function() { modify_modal_state('help-modal', true); },
'close-help-modal': function() { modify_modal_state('help-modal', false); },
'copy-identifier-button': function(button_element) {
const identifier = button_element.getAttribute('data-identifier');
write_text_to_clipboard(identifier);
const icon = button_element.querySelector('i, svg');
if (icon) {
icon.setAttribute('data-lucide', 'check');
if (typeof lucide !== 'undefined') {
lucide.createIcons();
}
}
},
'confirm-admin-action': function() {
if (application_state.pending_administrator_form) {
const time_input = document.getElementById('modal_execution_time');
if (time_input && time_input.value) {
let hidden_input = application_state.pending_administrator_form.querySelector('input[name="execution_time"]');
if (!hidden_input) {
hidden_input = document.createElement('input');
hidden_input.type = 'hidden';
hidden_input.name = 'execution_time';
application_state.pending_administrator_form.appendChild(hidden_input);
}
hidden_input.value = time_input.value;
} else {
const hidden_input = application_state.pending_administrator_form.querySelector('input[name="execution_time"]');
if (hidden_input) {
hidden_input.remove();
}
}
execute_asynchronous_form_submission(application_state.pending_administrator_form);
application_state.pending_administrator_form = null;
modify_modal_state('admin-confirmation-modal', false);
}
},
'cancel-admin-action': function() {
application_state.pending_administrator_form = null;
modify_modal_state('admin-confirmation-modal', false);
}
};
for (const [identifier, action_function] of Object.entries(action_mapping)) {
const button = target_element.closest('#' + identifier);
if (button) {
action_function(button);
return;
}
}
}
function handle_global_submits(submit_event) {
const target_form = submit_event.target;
if (target_form.classList.contains('asynchronous-form')) {
submit_event.preventDefault();
execute_asynchronous_form_submission(target_form);
} else if (target_form.classList.contains('admin-asynchronous-form')) {
submit_event.preventDefault();
application_state.pending_administrator_form = target_form;
const time_input = document.getElementById('modal_execution_time');
if (time_input) {
time_input.value = '';
}
modify_modal_state('admin-confirmation-modal', true);
}
}
/* --- Initialisierung --- */
document.addEventListener('DOMContentLoaded', function() {
if ('serviceWorker' in navigator) {
navigator.serviceWorker.register('/service_worker.js').catch(function(error_object) {
console.error('Service Worker Registration failed:', error_object);
});
}
if (typeof lucide !== 'undefined') {
lucide.createIcons();
}
if (application_state.stopwatch_interval_identifier) {
clearInterval(application_state.stopwatch_interval_identifier);
}
application_state.stopwatch_interval_identifier = setInterval(update_game_stopwatch_ui, 1000);
update_game_stopwatch_ui();
document.addEventListener('click', handle_global_clicks);
document.addEventListener('submit', handle_global_submits);
if (typeof io !== 'undefined') {
application_state.websocket_connection = io();
application_state.websocket_connection.on('game_state_changed', function() {
if (document.querySelector('.game-section') || document.querySelector('.administrator-section')) {
fetch_and_synchronize_dom();
}
});
application_state.websocket_connection.on('admin_state_changed', function() {
if (document.querySelector('.administrator-section')) {
fetch_and_synchronize_dom();
}
});
application_state.websocket_connection.on('elimination_verification_requested', function() {
fetch_and_synchronize_dom();
setTimeout(function() {
modify_modal_state('verification-modal', true);
}, 50);
});
application_state.websocket_connection.on('elimination_rejected', function() {
modify_modal_state('rejection-modal', true);
});
application_state.websocket_connection.on('elimination_revoked', function() {
modify_modal_state('verification-modal', false);
fetch_and_synchronize_dom();
});
}
});