results
This commit is contained in:
1 parent
8e2f6dac0c
commit
d6f5a70e52
3 files changed
+64
-5
No files matched your search
+8
-2
@@ -390,7 +390,6 @@ window.loadStudentResults = async function() {
|
|||||||
userResults.forEach((result, index) => {
|
userResults.forEach((result, index) => {
|
||||||
const li = document.createElement("li");
|
const li = document.createElement("li");
|
||||||
let answersHTML = '';
|
let answersHTML = '';
|
||||||
|
|
||||||
// Check if result and answers exist and are valid
|
// Check if result and answers exist and are valid
|
||||||
if (result && result.answers && Array.isArray(result.answers)) {
|
if (result && result.answers && Array.isArray(result.answers)) {
|
||||||
result.answers.forEach(entry => {
|
result.answers.forEach(entry => {
|
||||||
@@ -402,8 +401,15 @@ window.loadStudentResults = async function() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
const timestamp = result.timestamp ? new Date(result.timestamp).toLocaleString() : 'Unknown time';
|
const timestamp = result.timestamp ? new Date(result.timestamp).toLocaleString() : 'Unknown time';
|
||||||
|
// Label result type
|
||||||
|
let typeLabel = '';
|
||||||
|
if (result.type === 'typing') {
|
||||||
|
typeLabel = '<span style="color: #007bff; font-weight: bold;">[Typing Practice]</span> ';
|
||||||
|
} else {
|
||||||
|
typeLabel = '<span style="color: #28a745; font-weight: bold;">[Game]</span> ';
|
||||||
|
}
|
||||||
li.innerHTML = `
|
li.innerHTML = `
|
||||||
<strong>${username}</strong> (Session ${index + 1}) — Score: ${result?.score || 'N/A'}, Completed: ${result?.completed ? '✅' : '❌'}<br>
|
${typeLabel}<strong>${username}</strong> (Session ${index + 1}) — Score: ${result?.score || 'N/A'}, Completed: ${result?.completed ? '✅' : '❌'}<br>
|
||||||
<em>Date:</em> ${timestamp}<br>
|
<em>Date:</em> ${timestamp}<br>
|
||||||
<em>Answers:</em><br>${answersHTML}
|
<em>Answers:</em><br>${answersHTML}
|
||||||
<hr style="margin: 1rem 0; opacity: 0.3;">
|
<hr style="margin: 1rem 0; opacity: 0.3;">
|
||||||
|
|||||||
+32
-3
@@ -40,7 +40,11 @@ window.startTypingPractice = async function() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
window.showNextWord = function() {
|
|
||||||
|
// Track answers for results
|
||||||
|
typingState.answers = [];
|
||||||
|
|
||||||
|
window.showNextWord = async function() {
|
||||||
const promptEl = document.getElementById('typingPrompt');
|
const promptEl = document.getElementById('typingPrompt');
|
||||||
const inputEl = document.getElementById('typingInput');
|
const inputEl = document.getElementById('typingInput');
|
||||||
if (!promptEl || !inputEl) return;
|
if (!promptEl || !inputEl) return;
|
||||||
@@ -48,6 +52,8 @@ window.showNextWord = function() {
|
|||||||
if (!typingState.words.length) {
|
if (!typingState.words.length) {
|
||||||
promptEl.textContent = '🎉 Practice complete!';
|
promptEl.textContent = '🎉 Practice complete!';
|
||||||
inputEl.disabled = true;
|
inputEl.disabled = true;
|
||||||
|
// Save results to backend
|
||||||
|
await saveTypingPracticeResults();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -64,13 +70,36 @@ window.showNextWord = function() {
|
|||||||
inputEl.focus();
|
inputEl.focus();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async function saveTypingPracticeResults() {
|
||||||
|
const user = JSON.parse(localStorage.getItem('loggedInUser'));
|
||||||
|
if (!user) return;
|
||||||
|
const result = {
|
||||||
|
score: typingState.answers.filter(a => a.correct).length,
|
||||||
|
completed: true,
|
||||||
|
answers: typingState.answers
|
||||||
|
};
|
||||||
|
try {
|
||||||
|
await fetch('/saveTypingResults', {
|
||||||
|
method: 'POST',
|
||||||
|
headers: { 'Content-Type': 'application/json' },
|
||||||
|
body: JSON.stringify({ username: user.username, result })
|
||||||
|
});
|
||||||
|
console.log('Typing practice results saved');
|
||||||
|
} catch (e) {
|
||||||
|
console.error('Failed to save typing results', e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
window.submitTyping = function() {
|
window.submitTyping = function() {
|
||||||
const input = document.getElementById('typingInput')?.value.trim() || '';
|
const input = document.getElementById('typingInput')?.value.trim() || '';
|
||||||
const feedback = document.getElementById('typingFeedback');
|
const feedback = document.getElementById('typingFeedback');
|
||||||
if (!feedback) return;
|
if (!feedback) return;
|
||||||
|
|
||||||
if (input.toLowerCase() === typingState.currentWord.toLowerCase()) {
|
const correct = input.toLowerCase() === typingState.currentWord.toLowerCase();
|
||||||
|
typingState.answers.push({ word: typingState.currentWord, input, correct });
|
||||||
|
|
||||||
|
if (correct) {
|
||||||
feedback.textContent = '✅ Correct!';
|
feedback.textContent = '✅ Correct!';
|
||||||
feedback.style.color = 'green';
|
feedback.style.color = 'green';
|
||||||
typingState.words.shift();
|
typingState.words.shift();
|
||||||
|
|||||||
@@ -407,6 +407,30 @@ app.post('/saveResults', (req, res) => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
// 👥 Get users
|
// 👥 Get users
|
||||||
|
// --- Typing Practice Results ---
|
||||||
|
// Save typing practice results for a user
|
||||||
|
app.post('/saveTypingResults', (req, res) => {
|
||||||
|
const { username, result } = req.body;
|
||||||
|
if (
|
||||||
|
typeof username !== 'string' ||
|
||||||
|
typeof result !== 'object' ||
|
||||||
|
typeof result.score !== 'number' ||
|
||||||
|
typeof result.completed !== 'boolean' ||
|
||||||
|
!Array.isArray(result.answers)
|
||||||
|
) return res.status(400).send("Invalid result format");
|
||||||
|
|
||||||
|
const resultsPath = path.join(DATA_DIR, files.results);
|
||||||
|
const allResults = readJsonSafe(resultsPath);
|
||||||
|
if (!allResults[username]) allResults[username] = [];
|
||||||
|
|
||||||
|
allResults[username].push({
|
||||||
|
...result,
|
||||||
|
timestamp: new Date().toISOString(),
|
||||||
|
type: 'typing' // Mark as typing practice result
|
||||||
|
});
|
||||||
|
fs.writeFileSync(resultsPath, JSON.stringify(allResults, null, 2));
|
||||||
|
res.send(`✅ Typing results archived for ${username}`);
|
||||||
|
});
|
||||||
app.get('/getUsers', (req, res) => {
|
app.get('/getUsers', (req, res) => {
|
||||||
const users = readJsonSafe(path.join(DATA_DIR, files.users), []);
|
const users = readJsonSafe(path.join(DATA_DIR, files.users), []);
|
||||||
res.json(users);
|
res.json(users);
|
||||||
|
|||||||
Reference in new issue
Block a user