-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
96 lines (80 loc) · 3.38 KB
/
index.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
import { startGame } from './game.js';
import { hideMenu, showLeaderboardModal, closeLeaderboardModal } from './ui.js';
// Запуск игры по нажатию на кнопку
document.getElementById('startButton').addEventListener('click', () => {
hideMenu(); // Скрываем меню
startGame(); // Стартуем игру
});
// Перезапуск игры по нажатию на кнопку Restart
document.getElementById('restartButton').addEventListener('click', startGame);
let finalScore = 0;
export function saveFinalScore(score) {
return finalScore = score;
}
document.getElementById('saveButton').addEventListener('click', async () => {
const playerName = document.getElementById('playerName').value;
if (playerName && finalScore) {
try {
const response = await fetch('https://snake-game-worker.shamshyrak-zholdasbek.workers.dev', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({ name: playerName, score: finalScore })
});
if (response.ok) {
alert('Score successfully saved!');
} else {
alert('Failed to save score');
}
} catch (error) {
console.error('Error saving score:', error);
alert('An error occurred while saving the score');
}
} else {
alert('Please enter a valid name and score');
}
});
// Логика для работы с таблицей лидеров
const leaderboardButton = document.getElementById('leaderboardButton');
const closeLeaderboardButton = document.getElementById('closeLeaderboardButton');
const leaderboardBody = document.getElementById('leaderboardBody');
// Открытие модалки таблицы лидеров
leaderboardButton.addEventListener('click', async () => {
showLeaderboardModal();
// Очищаем старые данные таблицы
leaderboardBody.innerHTML = '';
// Получаем топ-10 игроков с сервера
try {
const response = await fetch('https://snake-game-worker.shamshyrak-zholdasbek.workers.dev', {
method: 'GET',
});
if (response.ok) {
const leaderboardData = await response.json();
leaderboardData.forEach((entry, index) => {
const row = document.createElement('tr');
row.innerHTML = `
<td>${index + 1}</td>
<td>${entry.name}</td>
<td>${new Date(entry.date).toLocaleDateString()}</td>
<td>${entry.score}</td>
`;
leaderboardBody.appendChild(row);
});
} else {
alert('Failed to load leaderboard');
}
} catch (error) {
console.error('Error fetching leaderboard:', error);
alert('An error occurred while fetching the leaderboard');
}
});
// Закрытие модалки таблицы лидеров
closeLeaderboardButton.addEventListener('click', closeLeaderboardModal);
// Закрытие модалки при клике вне её
window.onclick = function (event) {
const leaderboardModal = document.getElementById('leaderboardModal');
if (event.target == leaderboardModal) {
closeLeaderboardModal();
}
};