Compare commits
2 Commits
master
..
d82127c5fb
| Author | SHA1 | Date | |
|---|---|---|---|
|
d82127c5fb
|
|||
|
36b0c0343d
|
@@ -3,4 +3,3 @@ hiscores.json
|
|||||||
loscores.json
|
loscores.json
|
||||||
pingscores.json
|
pingscores.json
|
||||||
.*
|
.*
|
||||||
address.js
|
|
||||||
|
|||||||
+5
-1
@@ -405,13 +405,17 @@ async fn handle_socket(mut socket: WebSocket, tx: &mpsc::UnboundedSender<Leaderb
|
|||||||
fn validate_name(input: &str) -> &str
|
fn validate_name(input: &str) -> &str
|
||||||
{
|
{
|
||||||
let input = input.trim();
|
let input = input.trim();
|
||||||
|
if input == "null"
|
||||||
|
{
|
||||||
|
return "anon";
|
||||||
|
}
|
||||||
// Length check
|
// Length check
|
||||||
if input.is_empty() || input.len() > 32
|
if input.is_empty() || input.len() > 32
|
||||||
{
|
{
|
||||||
return "anon";
|
return "anon";
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Allow only letters, numbers, _ and -
|
||||||
if input
|
if input
|
||||||
.bytes()
|
.bytes()
|
||||||
.all(|byte| byte.is_ascii_alphanumeric() || byte == b'_' || byte == b'-')
|
.all(|byte| byte.is_ascii_alphanumeric() || byte == b'_' || byte == b'-')
|
||||||
|
|||||||
+15
-39
@@ -9,26 +9,16 @@
|
|||||||
<button id="button" onclick="send_click()">0</button>
|
<button id="button" onclick="send_click()">0</button>
|
||||||
<div id="score-table"></div>
|
<div id="score-table"></div>
|
||||||
</body>
|
</body>
|
||||||
<script src="/address.js"> </script>
|
|
||||||
<script>
|
<script>
|
||||||
let score = 0;
|
const regex = new RegExp("^[a-zA-Z0-9_-]+$");
|
||||||
let locally = false;
|
const ws = new WebSocket('ws://deadvey.com:8084/ws');
|
||||||
// get the username
|
console.log(name)
|
||||||
let name = validate_data(prompt("Nickname for the leaderboard\nOnly alphanumeric, - or _ please\nPress cancel to just play locally.","anon") ?? set_local());
|
let latestMessage = 0;
|
||||||
let ws = "local";
|
|
||||||
if (!locally)
|
|
||||||
{
|
|
||||||
ws = new WebSocket(`ws://${ADDRESS}:8084/ws`);
|
|
||||||
};
|
|
||||||
ws.onopen = (event) => {
|
ws.onopen = (event) => {
|
||||||
if (!locally) { ws.send(name); };
|
let name = validate_data(prompt("Nickname for the leaderboard"));
|
||||||
|
ws.send(name);
|
||||||
}
|
}
|
||||||
ws.onmessage = (event) => {
|
const ws_leaderboard = new WebSocket('ws://deadvey.com:8084/ws-leaderboard');
|
||||||
console.log(event.data);
|
|
||||||
score = validate_data(event.data);
|
|
||||||
}
|
|
||||||
|
|
||||||
const ws_leaderboard = new WebSocket(`ws://${ADDRESS}:8084/ws-leaderboard`); // download the leaderboard
|
|
||||||
ws_leaderboard.onopen = (event) => {
|
ws_leaderboard.onopen = (event) => {
|
||||||
ws_leaderboard.send("1");
|
ws_leaderboard.send("1");
|
||||||
};
|
};
|
||||||
@@ -51,39 +41,25 @@
|
|||||||
});
|
});
|
||||||
tableDiv.appendChild(table);
|
tableDiv.appendChild(table);
|
||||||
};
|
};
|
||||||
|
ws.onmessage = (event) => {
|
||||||
|
console.log(event.data);
|
||||||
|
latestMessage = validate_data(event.data);
|
||||||
|
}
|
||||||
function send_click()
|
function send_click()
|
||||||
{
|
|
||||||
if (!locally)
|
|
||||||
{
|
{
|
||||||
ws.send("");
|
ws.send("");
|
||||||
if (score !== null)
|
if (latestMessage !== null) {
|
||||||
{
|
document.getElementById("button").textContent=latestMessage;
|
||||||
document.getElementById("button").textContent=score;
|
latestMessage = null;
|
||||||
score = null;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else // locally = true
|
|
||||||
{
|
|
||||||
if (Math.random() < (1/3)) { score = 0; } // fail
|
|
||||||
else { score += 1; } // success
|
|
||||||
document.getElementById("button").textContent=score;
|
|
||||||
|
|
||||||
}
|
|
||||||
}
|
|
||||||
function set_local()
|
|
||||||
{
|
|
||||||
score = 0
|
|
||||||
locally = true
|
|
||||||
return "anon"
|
|
||||||
}
|
|
||||||
function get_rand(min, max) {
|
function get_rand(min, max) {
|
||||||
return Math.round(Math.random() * (max - min) + min);
|
return Math.round(Math.random() * (max - min) + min);
|
||||||
}
|
}
|
||||||
window.addEventListener('beforeunload', () => {
|
window.addEventListener('beforeunload', () => {
|
||||||
ws.close();
|
ws.close();
|
||||||
}, !locally);
|
});
|
||||||
function validate_data(data) { // Only allow a-z, A-Z, 0-9, - and _ characters, sorry Ramón
|
function validate_data(data) { // Only allow a-z, A-Z, 0-9, - and _ characters, sorry Ramón
|
||||||
const regex = new RegExp("^[a-zA-Z0-9_-]+$");
|
|
||||||
if (regex.test(data)) { return data }
|
if (regex.test(data)) { return data }
|
||||||
else { return "anon" }
|
else { return "anon" }
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -15,9 +15,8 @@
|
|||||||
<p>Total number of resets and personal bests of each username</p>
|
<p>Total number of resets and personal bests of each username</p>
|
||||||
<div id="pingscore-table"></div>
|
<div id="pingscore-table"></div>
|
||||||
</body>
|
</body>
|
||||||
<script src="/address.js"></script>
|
|
||||||
<script>
|
<script>
|
||||||
const ws = new WebSocket(`ws://${ADDRESS}:8084/ws-leaderboard`);
|
const ws = new WebSocket('ws://deadvey.com:8084/ws-leaderboard');
|
||||||
const regex = new RegExp("^[a-zA-Z0-9_-]+$");
|
const regex = new RegExp("^[a-zA-Z0-9_-]+$");
|
||||||
ws.onopen = (event) => {
|
ws.onopen = (event) => {
|
||||||
ws.send('0'); // send all
|
ws.send('0'); // send all
|
||||||
|
|||||||
Reference in New Issue
Block a user