-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathscript.js
More file actions
58 lines (52 loc) · 1.34 KB
/
script.js
File metadata and controls
58 lines (52 loc) · 1.34 KB
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
let playerName = '';
function startGame() {
playerName = document.getElementById('player-name').value;
if (playerName) {
document.getElementById('player-form').style.display = 'none';
// Your existing game start logic here
} else {
alert('Please enter your name');
}
}
function endGame(score) {
// Your existing end game logic here
// Save score to server
fetch('http://localhost:5000/api/scores', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({ playerName, score }),
})
.then(response => response.json())
.then(data => {
console.log('Score saved:', data);
fetchLeaderboard();
})
.catch((error) => {
console.error('Error:', error);
});
}
function fetchLeaderboard() {
fetch('http://localhost:5000/api/leaderboard')
.then(response => response.json())
.then(data => {
displayLeaderboard(data);
})
.catch((error) => {
console.error('Error:', error);
});
}
function displayLeaderboard(scores) {
// Create and display a leaderboard on your page
}
function sendScore(score) {
fetch('/api/score', {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({ score })
}).then(response => response.json())
.then(data => console.log(data.message));
}