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

143
owe.js
View File

@ -2,7 +2,7 @@ var previousTextLength;
var config_data;
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"}})
.then((response) => {
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) => {
return response.text().then((text) => {
config_data = JSON.parse(text);
@ -108,30 +108,133 @@ function resetUnread() {
}
function getNextPlayer() {
var spans = document.querySelector(".sentences").getElementsByTagName("span");
var lastspan = spans[spans.length - 1];
var lastplayer = lastspan.dataset.user;
var idx_next = (config_data.users.indexOf(lastplayer) + 1) % config_data.users.length;
return config_data.users[idx_next];
var sentencesDiv = document.querySelector(".sentences");
var spans = sentencesDiv.getElementsByTagName("span");
if (spans.length > 0) {
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");
var lastspan = spans[spans.length - 1];
var lasttime = lastspan.dataset.time;
return Date.now()/1e3 - lasttime; // in seconds
function getLastUserFromArchive(archiveFileUrl) {
// Fetch the last span from the archive file and extract the username
return getLastSpanFromArchive(archiveFileUrl)
.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) {
// Make an asynchronous request to fetch the content of the archive file
return fetch(archiveFileUrl)
.then(response => response.text())
.then(htmlContent => {
// Parse the HTML content to get the last span
var parser = new DOMParser();
var doc = parser.parseFromString(htmlContent, 'text/html');
var archiveSpans = doc.querySelectorAll("span");
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');
//loginArea.innerHTML = "Eingeloggt als NOCH NICHT IMPLEMENTIERT";
loginArea.innerHTML = ""; // first clear it out
loginArea.innerHTML += getNextPlayer() + " ist dran!<br>\n";
var relative_time = getTimeSinceLast();
var days = Math.floor(relative_time/(3600*24));
var hours = Math.floor(relative_time/(3600))%24;
var minutes = Math.floor(relative_time/(60))%60;
// 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;