-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
151 lines (130 loc) · 4.81 KB
/
main.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
import './style.css';
import { fetchLocation } from './src/api/ipinfo.js';
import { generateQuestions } from './src/api/gemini.js';
import { QuizManager } from './src/quiz/quizManager.js';
import { QuizUI } from './src/ui/quizUI.js';
import { LeaderboardUI } from './src/ui/leaderboardUI.js';
import { LeaderboardService } from './src/services/leaderboardService.js';
import { LANGUAGES } from './src/constants/languages.js';
import { languageService } from './src/services/languageService.js';
const quizManager = new QuizManager();
const quizUI = new QuizUI();
const leaderboardService = new LeaderboardService();
const leaderboardUI = new LeaderboardUI(document.getElementById('leaderboard-container'));
//Initialize Ip info API
if (typeof axios === "undefined") {
console.error("Axios failed to load.");
}
// عنصر الرابط
const merchLink = document.getElementById("merch-link");
// استدعاء الوظيفة
fetchLocation()
.then(data => {
const userCountry = data.country; // الدولة حسب رمز ISO (مثل "EG" لمصر)
// تغيير الرابط بناءً على الدولة
if (userCountry === "EG") {
merchLink.href = "https://konohalot.shop"; // رابط مصر
} else {
merchLink.href = "https://konohalot.printify.me"; // رابط دولي
}
})
.catch(error => {
console.error("Failed to fetch location:", error.message);
merchLink.href = "https://konohalot.printify.me"; // رابط افتراضي
});
// End ip switcher
// Initialize language selector
const languageSelect = document.getElementById('language-select');
LANGUAGES.forEach(lang => {
const option = document.createElement('option');
option.value = lang.code;
option.textContent = `${lang.flag} ${lang.name}`;
languageSelect.appendChild(option);
});
// Start Quiz Handler
async function handleStartQuiz() {
const topic = quizUI.elements.topicInput.value.trim();
if (!topic) {
quizUI.showError('Please enter a topic');
return;
}
try {
quizUI.showLoading();
const questions = await generateQuestions(topic);
quizManager.setQuestions(questions);
quizUI.hideLoading();
quizUI.showQuizContent();
quizUI.displayQuestion(quizManager.getCurrentQuestion());
updateProgress();
} catch (error) {
quizUI.showError(error.message);
}
}
// Update progress bar
function updateProgress() {
const progressElement = document.getElementById('progress-fill');
const progress = ((quizManager.currentIndex + 1) / quizManager.questions.length) * 100;
progressElement.style.width = `${progress}%`;
}
// Handle option selection
function handleOptionSelect(event) {
if (!event.target.classList.contains('option') || event.target.style.pointerEvents === 'none') {
return;
}
const selectedOption = event.target.textContent;
const result = quizManager.checkAnswer(selectedOption);
quizUI.markAnswer(selectedOption, result);
}
// Handle next question
function handleNextQuestion() {
if (quizManager.nextQuestion()) {
quizUI.displayQuestion(quizManager.getCurrentQuestion());
quizUI.elements.nextQuestionButton.classList.add('hidden');
updateProgress();
} else {
const { score, total } = quizManager.getScore();
quizUI.showResult(score, total);
}
}
// Handle save score
function handleSaveScore() {
const nameInput = document.getElementById('player-name');
const name = nameInput.value.trim();
if (!name) {
alert('Please enter your name');
return;
}
const { score, total } = quizManager.getScore();
const { rank, totalPlayers } = leaderboardService.addScore({
name,
score,
total,
topic: quizUI.elements.topicInput.value
});
quizUI.showFinalScore();
document.getElementById('rank-info').textContent = `Global Rank: ${rank} of ${totalPlayers}`;
}
// Event Listeners
quizUI.elements.startQuizButton.addEventListener('click', handleStartQuiz);
quizUI.elements.optionsContainer.addEventListener('click', handleOptionSelect);
quizUI.elements.nextQuestionButton.addEventListener('click', handleNextQuestion);
document.getElementById('save-score').addEventListener('click', handleSaveScore);
quizUI.elements.restartQuizButton.addEventListener('click', () => {
quizUI.reset();
});
languageSelect.addEventListener('change', (e) => {
languageService.setLanguage(e.target.value);
});
document.getElementById('show-leaderboard').addEventListener('click', () => {
const scores = leaderboardService.getScores();
leaderboardUI.show(scores);
});
document.getElementById('share-score').addEventListener('click', () => {
const { score, total } = quizManager.getScore();
const scoreData = leaderboardService.getScores().find(s => s.score === score);
if (scoreData) {
const shareUrl = `${window.location.origin}?score=${scoreData.id}`;
navigator.clipboard.writeText(shareUrl);
alert('Share link copied to clipboard!');
}
});