80 lines
2.4 KiB
JavaScript
80 lines
2.4 KiB
JavaScript
const express = require('express');
|
|
const cors = require('cors');
|
|
const fs = require('fs');
|
|
|
|
const app = express();
|
|
app.use(cors());
|
|
app.use(express.json());
|
|
|
|
const questions = JSON.parse(fs.readFileSync('./questions.json', 'utf-8'));
|
|
|
|
function shuffle(array) {
|
|
return array.sort(() => Math.random() - 0.5);
|
|
}
|
|
|
|
// Hilfsfunktion zur Auswahl einer täglichen Frage
|
|
function getDailyQuestion() {
|
|
const today = new Date().toISOString().split('T')[0]; // z. B. "2025-06-16"
|
|
// Einfache Hash-Funktion: Summe der ASCII-Werte des Datumsstrings
|
|
const hash = today.split('').reduce((sum, char) => sum + char.charCodeAt(0), 0);
|
|
const index = hash % questions.length; // Modulo für Index in questions
|
|
return questions[index];
|
|
}
|
|
|
|
// Neue Session starten
|
|
app.get('/api/new-session', (req, res) => {
|
|
const isDaily = req.query.daily === 'true';
|
|
let question;
|
|
|
|
if (isDaily) {
|
|
question = getDailyQuestion();
|
|
} else {
|
|
question = questions[Math.floor(Math.random() * questions.length)];
|
|
}
|
|
|
|
const shuffledHints = shuffle([...question.hints]);
|
|
|
|
res.json({
|
|
id: question.id,
|
|
hint: shuffledHints[0],
|
|
totalHints: shuffledHints.length,
|
|
digits: String(question.answer).length,
|
|
answer: String(question.answer),
|
|
allHints: shuffledHints,
|
|
link: question.link || null,
|
|
isDailyChallenge: isDaily // Neues Feld
|
|
});
|
|
});
|
|
|
|
// Rateversuch mit Rundenfortschritt
|
|
app.post('/api/guess', (req, res) => {
|
|
const { id, guess, round } = req.body;
|
|
const question = questions.find(q => q.id === id);
|
|
|
|
if (!question) {
|
|
return res.status(404).json({ message: "Frage nicht gefunden." });
|
|
}
|
|
|
|
const numericGuess = Number(guess);
|
|
let feedback = "";
|
|
|
|
if (numericGuess === question.answer) {
|
|
feedback = "Richtig";
|
|
} else if (numericGuess > question.answer) {
|
|
feedback = numericGuess - question.answer > 100 ? "Viel zu hoch" : "Zu hoch";
|
|
} else {
|
|
feedback = question.answer - numericGuess > 100 ? "Viel zu niedrig" : "Zu niedrig";
|
|
}
|
|
|
|
res.json({
|
|
correct: numericGuess === question.answer,
|
|
feedback,
|
|
nextHint: question.hints[round] || null,
|
|
explanation: numericGuess === question.answer || round === 4 ? question.explanation : null,
|
|
link: question.link || null
|
|
});
|
|
});
|
|
|
|
app.listen(3000, () => {
|
|
console.log("✅ FaktenZahl Backend läuft auf http://localhost:3000");
|
|
}); |