user and time can now be determined from archive

This commit is contained in:
Tristan Schneider 2024-01-28 18:14:28 +01:00
parent 82a487d3dd
commit dc3ee12e48

147
owe.js
View File

@ -2,7 +2,7 @@ var previousTextLength;
var config_data; var config_data;
var unread_words = 0; var unread_words = 0;
function loadSentences(alertIfNew = false, increaseUnread = true) { async function loadSentences(alertIfNew = false, increaseUnread = true) {
fetch("./data/sentences.txt", {headers: {"Cache-Control": "no-cache, no-store"}}) fetch("./data/sentences.txt", {headers: {"Cache-Control": "no-cache, no-store"}})
.then((response) => { .then((response) => {
return response.text().then((text) => { return response.text().then((text) => {
@ -44,7 +44,7 @@ function loadSentences(alertIfNew = false, increaseUnread = true) {
}); });
} }
function loadConfig() { async function loadConfig() {
fetch("./data/config.json", {headers: {"Cache-Control": "no-cache, no-store"}}).then((response) => { fetch("./data/config.json", {headers: {"Cache-Control": "no-cache, no-store"}}).then((response) => {
return response.text().then((text) => { return response.text().then((text) => {
config_data = JSON.parse(text); config_data = JSON.parse(text);
@ -108,30 +108,133 @@ function resetUnread() {
} }
function getNextPlayer() { function getNextPlayer() {
var spans = document.querySelector(".sentences").getElementsByTagName("span"); var sentencesDiv = document.querySelector(".sentences");
var lastspan = spans[spans.length - 1]; var spans = sentencesDiv.getElementsByTagName("span");
var lastplayer = lastspan.dataset.user;
var idx_next = (config_data.users.indexOf(lastplayer) + 1) % config_data.users.length; if (spans.length > 0) {
return config_data.users[idx_next]; var lastSpan = spans[spans.length - 1];
var lastPlayer = lastSpan.dataset.user;
var idxNext = (config_data.users.indexOf(lastPlayer) + 1) % config_data.users.length;
return config_data.users[idxNext];
} else {
// If there are no spans, determine the next user from the archive file
var archiveFileUrl = getArchiveFileUrl(); // Replace with the actual function that returns the archive file URL
return getLastUserFromArchive(archiveFileUrl)
.then(lastUser => {
if (lastUser) {
// Calculate the next user's index based on the list of users
var idxNext = (config_data.users.indexOf(lastUser) + 1) % config_data.users.length;
return config_data.users[idxNext];
} else {
// Handle the case when the archive file is empty or contains no spans
console.error("Archive file is empty or contains no spans.");
return null;
}
})
.catch(error => {
console.error("Error fetching archive file:", error);
return null;
});
}
} }
function getTimeSinceLast() {
var spans = document.querySelector(".sentences").getElementsByTagName("span"); function getLastUserFromArchive(archiveFileUrl) {
var lastspan = spans[spans.length - 1]; // Fetch the last span from the archive file and extract the username
var lasttime = lastspan.dataset.time; return getLastSpanFromArchive(archiveFileUrl)
return Date.now()/1e3 - lasttime; // in seconds .then(lastArchiveSpan => {
if (lastArchiveSpan) {
return lastArchiveSpan.dataset.user;
} else {
// Handle the case when the archive file is empty or contains no spans
console.error("Archive file is empty or contains no spans.");
return null;
}
})
.catch(error => {
console.error("Error fetching archive file:", error);
return null;
});
} }
function initializeLoginArea() { function getLastSpanFromArchive(archiveFileUrl) {
var loginArea = document.getElementById('login_area'); // Make an asynchronous request to fetch the content of the archive file
//loginArea.innerHTML = "Eingeloggt als NOCH NICHT IMPLEMENTIERT"; return fetch(archiveFileUrl)
loginArea.innerHTML = ""; // first clear it out .then(response => response.text())
loginArea.innerHTML += getNextPlayer() + " ist dran!<br>\n"; .then(htmlContent => {
var relative_time = getTimeSinceLast(); // Parse the HTML content to get the last span
var days = Math.floor(relative_time/(3600*24)); var parser = new DOMParser();
var hours = Math.floor(relative_time/(3600))%24; var doc = parser.parseFromString(htmlContent, 'text/html');
var minutes = Math.floor(relative_time/(60))%60; var archiveSpans = doc.querySelectorAll("span");
loginArea.innerHTML += "Schon seit " + days + " Tagen, " + hours + " Stunden und " + minutes + " Minuten!";
if (archiveSpans.length > 0) {
return archiveSpans[archiveSpans.length - 1];
} else {
// Return null if the archive file is empty or contains no spans
return null;
}
});
}
// bullshit implementation:
function getArchiveFileUrl() {
return "./data/sentences-archive-25-nov-23-26-jan-24.txt";
}
async function getTimeSinceLast() {
var sentencesDiv = document.querySelector(".sentences");
var spans = sentencesDiv.getElementsByTagName("span");
if (spans.length > 0) {
var lastSpan = spans[spans.length - 1];
var lastTime = lastSpan.dataset.time;
return Date.now() / 1e3 - lastTime; // in seconds
} else {
// If there are no spans, determine the last time from the archive file
var archiveFileUrl = getArchiveFileUrl(); // Replace with the actual function that returns the archive file URL
return getLastTimeFromArchive(archiveFileUrl);
}
}
async function getLastTimeFromArchive(archiveFileUrl) {
try {
// Fetch the last span from the archive file
var lastSpan = await getLastSpanFromArchive(archiveFileUrl);
if (lastSpan) {
var lastTime = lastSpan.dataset.time;
return Date.now() / 1e3 - lastTime; // in seconds
} else {
// Handle the case when the archive file is empty or contains no spans
console.error("Archive file is empty or contains no spans.");
return null;
}
} catch (error) {
console.error("Error fetching archive file:", error);
return null;
}
}
async function initializeLoginArea() {
var loginArea = document.getElementById('login_area');
// Use async/await to wait for the result of getNextPlayer
try {
var nextPlayer = await getNextPlayer();
var relative_time = await getTimeSinceLast();
// Clear the login area
loginArea.innerHTML = "";
loginArea.innerHTML += nextPlayer + " ist dran!<br>\n";
var days = Math.floor(relative_time / (3600 * 24));
var hours = Math.floor(relative_time / 3600) % 24;
var minutes = Math.floor(relative_time / 60) % 60;
loginArea.innerHTML += "Schon seit " + days + " Tagen, " + hours + " Stunden und " + minutes + " Minuten!";
} catch (error) {
// Handle any errors that may occur in the getNextPlayer function
console.error("Error in initializeLoginArea:", error);
}
} }
window.onfocus = resetUnread; window.onfocus = resetUnread;