forked from RamtinHaf/CTF-Workshop
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
94 lines (83 loc) · 3.37 KB
/
index.html
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Flag Scoreboard</title>
<link rel="stylesheet" type="text/css" href="style.css">
</head>
<body>
<div class="container">
<h1>Flag Scoreboard</h1>
<form id="flagForm">
<input type="text" id="username" placeholder="Username" required>
<input type="text" id="flag" placeholder="Flag" required>
<button type="submit">Submit Flag</button>
</form>
<div id="allUsersScoreboard">
<!-- All users' scores will be displayed here -->
</div>
</div>
<script>
document.getElementById('flagForm').addEventListener('submit', function(event) {
event.preventDefault();
const username = document.getElementById('username').value;
const flag = document.getElementById('flag').value;
submitFlag(username, flag);
});
function submitFlag(username, flag) {
fetch('https://ctf-backend-2-154b3aee57f1.herokuapp.com/submit-flag', {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({
username,
flag
})
})
.then(response => {
if (!response.ok) {
throw new Error('Flag submission failed: ' + response.statusText);
}
return response.json();
})
.then(data => {
alert(data.message);
updateAllUsersScoreboard();
})
.catch((error) => {
console.error('Error:', error);
alert('An error occurred. Please try again later.');
});
}
function updateAllUsersScoreboard() {
fetch('https://ctf-backend-2-154b3aee57f1.herokuapp.com/scores')
.then(response => response.json())
.then(scores => {
const scoreboardElement = document.getElementById('allUsersScoreboard');
let scoreboardHTML = '<h2>All Users Scoreboard</h2>';
const sortedScores = Object.keys(scores).map(username => ({
username,
score: scores[username]
})).sort((a, b) => b.score - a.score);
sortedScores.forEach((user, index) => {
let medal = '';
if (index === 0) {
medal = '🥇';
} else if (index === 1) {
medal = '🥈';
} else if (index === 2) {
medal = '🥉';
}
scoreboardHTML += `<div class="${index < 3 ? 'top-three' : ''}">${medal} ${user.username}: ${user.score} points</div>`;
});
scoreboardElement.innerHTML = scoreboardHTML;
})
.catch((error) => {
console.error('Error fetching scores:', error);
});
}
document.addEventListener('DOMContentLoaded', updateAllUsersScoreboard);
</script>
</body>
</html>