mirror of
https://github.com/shadoll/sLogos.git
synced 2025-12-20 03:26:59 +00:00
Add common logic for achievements, global stats, session management, settings, and sound in quizzes
This commit is contained in:
9
src/quizLogic/quizAchievements.js
Normal file
9
src/quizLogic/quizAchievements.js
Normal file
@@ -0,0 +1,9 @@
|
|||||||
|
// quizAchievements.js
|
||||||
|
// Common achievement logic for all quizzes
|
||||||
|
|
||||||
|
export function updateAchievementCount(achievementsComponent) {
|
||||||
|
if (achievementsComponent) {
|
||||||
|
return achievementsComponent.getAchievementCount();
|
||||||
|
}
|
||||||
|
return { unlocked: 0, total: 0 };
|
||||||
|
}
|
||||||
46
src/quizLogic/quizGlobalStats.js
Normal file
46
src/quizLogic/quizGlobalStats.js
Normal file
@@ -0,0 +1,46 @@
|
|||||||
|
// quizGlobalStats.js
|
||||||
|
// Common global statistics logic for all quizzes
|
||||||
|
|
||||||
|
export function loadGlobalStats(key) {
|
||||||
|
const savedGlobalStats = localStorage.getItem(key);
|
||||||
|
if (savedGlobalStats) {
|
||||||
|
try {
|
||||||
|
return JSON.parse(savedGlobalStats);
|
||||||
|
} catch (e) {
|
||||||
|
console.error("Error loading global stats:", e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
export function updateGlobalStats(key, quizName, isCorrect, isSkipped = false) {
|
||||||
|
let globalStats = {};
|
||||||
|
const savedGlobalStats = localStorage.getItem(key);
|
||||||
|
if (savedGlobalStats) {
|
||||||
|
try {
|
||||||
|
globalStats = JSON.parse(savedGlobalStats);
|
||||||
|
} catch (e) {
|
||||||
|
console.error("Error parsing global stats:", e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (!globalStats[quizName]) {
|
||||||
|
globalStats[quizName] = { correct: 0, wrong: 0, total: 0, skipped: 0 };
|
||||||
|
}
|
||||||
|
if (!globalStats.overall) {
|
||||||
|
globalStats.overall = { correct: 0, wrong: 0, total: 0, skipped: 0 };
|
||||||
|
}
|
||||||
|
globalStats[quizName].total++;
|
||||||
|
globalStats.overall.total++;
|
||||||
|
if (isSkipped) {
|
||||||
|
globalStats[quizName].skipped++;
|
||||||
|
globalStats.overall.skipped++;
|
||||||
|
} else if (isCorrect) {
|
||||||
|
globalStats[quizName].correct++;
|
||||||
|
globalStats.overall.correct++;
|
||||||
|
} else {
|
||||||
|
globalStats[quizName].wrong++;
|
||||||
|
globalStats.overall.wrong++;
|
||||||
|
}
|
||||||
|
localStorage.setItem(key, JSON.stringify(globalStats));
|
||||||
|
return globalStats;
|
||||||
|
}
|
||||||
23
src/quizLogic/quizSession.js
Normal file
23
src/quizLogic/quizSession.js
Normal file
@@ -0,0 +1,23 @@
|
|||||||
|
// quizSession.js
|
||||||
|
// Common session management logic for all quizzes
|
||||||
|
|
||||||
|
export function saveSessionState(key, state) {
|
||||||
|
localStorage.setItem(key, JSON.stringify(state));
|
||||||
|
}
|
||||||
|
|
||||||
|
export function loadSessionState(key, defaultState) {
|
||||||
|
const savedState = localStorage.getItem(key);
|
||||||
|
if (savedState) {
|
||||||
|
try {
|
||||||
|
return JSON.parse(savedState);
|
||||||
|
} catch (e) {
|
||||||
|
console.error("Error loading session state:", e);
|
||||||
|
return defaultState;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return defaultState;
|
||||||
|
}
|
||||||
|
|
||||||
|
export function clearSessionState(key) {
|
||||||
|
localStorage.removeItem(key);
|
||||||
|
}
|
||||||
19
src/quizLogic/quizSettings.js
Normal file
19
src/quizLogic/quizSettings.js
Normal file
@@ -0,0 +1,19 @@
|
|||||||
|
// quizSettings.js
|
||||||
|
// Common settings management logic for all quizzes
|
||||||
|
|
||||||
|
export function saveSettings(key, settings) {
|
||||||
|
localStorage.setItem(key, JSON.stringify(settings));
|
||||||
|
}
|
||||||
|
|
||||||
|
export function loadSettings(key, defaultSettings) {
|
||||||
|
const savedSettings = localStorage.getItem(key);
|
||||||
|
if (savedSettings) {
|
||||||
|
try {
|
||||||
|
return JSON.parse(savedSettings);
|
||||||
|
} catch (e) {
|
||||||
|
console.error("Error loading settings:", e);
|
||||||
|
return defaultSettings;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return defaultSettings;
|
||||||
|
}
|
||||||
41
src/quizLogic/quizSound.js
Normal file
41
src/quizLogic/quizSound.js
Normal file
@@ -0,0 +1,41 @@
|
|||||||
|
// quizSound.js
|
||||||
|
// Common sound logic for all quizzes
|
||||||
|
|
||||||
|
export function playCorrectSound(soundEnabled) {
|
||||||
|
if (!soundEnabled) return;
|
||||||
|
try {
|
||||||
|
const audioContext = new (window.AudioContext || window.webkitAudioContext)();
|
||||||
|
const oscillator = audioContext.createOscillator();
|
||||||
|
const gainNode = audioContext.createGain();
|
||||||
|
oscillator.connect(gainNode);
|
||||||
|
gainNode.connect(audioContext.destination);
|
||||||
|
oscillator.frequency.setValueAtTime(523.25, audioContext.currentTime);
|
||||||
|
oscillator.frequency.setValueAtTime(659.25, audioContext.currentTime + 0.1);
|
||||||
|
oscillator.frequency.setValueAtTime(783.99, audioContext.currentTime + 0.2);
|
||||||
|
gainNode.gain.setValueAtTime(0.3, audioContext.currentTime);
|
||||||
|
gainNode.gain.exponentialRampToValueAtTime(0.001, audioContext.currentTime + 0.4);
|
||||||
|
oscillator.start(audioContext.currentTime);
|
||||||
|
oscillator.stop(audioContext.currentTime + 0.4);
|
||||||
|
} catch (e) {
|
||||||
|
console.log("Audio not supported:", e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
export function playWrongSound(soundEnabled) {
|
||||||
|
if (!soundEnabled) return;
|
||||||
|
try {
|
||||||
|
const audioContext = new (window.AudioContext || window.webkitAudioContext)();
|
||||||
|
const oscillator = audioContext.createOscillator();
|
||||||
|
const gainNode = audioContext.createGain();
|
||||||
|
oscillator.connect(gainNode);
|
||||||
|
gainNode.connect(audioContext.destination);
|
||||||
|
oscillator.frequency.setValueAtTime(400, audioContext.currentTime);
|
||||||
|
oscillator.frequency.setValueAtTime(300, audioContext.currentTime + 0.15);
|
||||||
|
gainNode.gain.setValueAtTime(0.3, audioContext.currentTime);
|
||||||
|
gainNode.gain.exponentialRampToValueAtTime(0.001, audioContext.currentTime + 0.3);
|
||||||
|
oscillator.start(audioContext.currentTime);
|
||||||
|
oscillator.stop(audioContext.currentTime + 0.3);
|
||||||
|
} catch (e) {
|
||||||
|
console.log("Audio not supported:", e);
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user