forked from deadvey/button
added local play by pressing cancel on the JS prompt
removed the uncessesary null check in the validate_name
This commit is contained in:
@@ -3,3 +3,4 @@ hiscores.json
|
||||
loscores.json
|
||||
pingscores.json
|
||||
.*
|
||||
address.js
|
||||
|
||||
+5
-14
@@ -444,21 +444,12 @@ async fn handle_socket
|
||||
|
||||
fn validate_name(input: &str) -> &str {
|
||||
let input = input.trim();
|
||||
if input == "null"
|
||||
// Allow only letters, numbers, _ and -
|
||||
let re = Regex::new(r"^[a-zA-Z0-9_-]+$").unwrap();
|
||||
// Length check
|
||||
if input.is_empty() || input.len() > 32 || !re.is_match(input)
|
||||
{
|
||||
return "anon";
|
||||
}
|
||||
// Length check
|
||||
if input.is_empty() || input.len() > 32 {
|
||||
return "anon";
|
||||
}
|
||||
|
||||
// Allow only letters, numbers, _ and -
|
||||
let re = Regex::new(r"^[a-zA-Z0-9_-]+$").unwrap();
|
||||
|
||||
if re.is_match(input) {
|
||||
input
|
||||
} else {
|
||||
"anon"
|
||||
}
|
||||
input
|
||||
}
|
||||
|
||||
+40
-16
@@ -9,16 +9,26 @@
|
||||
<button id="button" onclick="send_click()">0</button>
|
||||
<div id="score-table"></div>
|
||||
</body>
|
||||
<script src="/address.js"> </script>
|
||||
<script>
|
||||
const regex = new RegExp("^[a-zA-Z0-9_-]+$");
|
||||
const ws = new WebSocket('ws://deadvey.com:8084/ws');
|
||||
console.log(name)
|
||||
let latestMessage = 0;
|
||||
let score = 0;
|
||||
let locally = false;
|
||||
// get the username
|
||||
let name = validate_data(prompt("Nickname for the leaderboard\nOnly alphanumeric, - or _ please\nPress cancel to just play locally.","anon") ?? set_local());
|
||||
let ws = "local";
|
||||
if (!locally)
|
||||
{
|
||||
ws = new WebSocket(`ws://${ADDRESS}:8084/ws`);
|
||||
};
|
||||
ws.onopen = (event) => {
|
||||
let name = validate_data(prompt("Nickname for the leaderboard"));
|
||||
ws.send(name);
|
||||
if (!locally) { ws.send(name); };
|
||||
}
|
||||
const ws_leaderboard = new WebSocket('ws://deadvey.com:8084/ws-leaderboard');
|
||||
ws.onmessage = (event) => {
|
||||
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.send("1");
|
||||
};
|
||||
@@ -41,25 +51,39 @@
|
||||
});
|
||||
tableDiv.appendChild(table);
|
||||
};
|
||||
ws.onmessage = (event) => {
|
||||
console.log(event.data);
|
||||
latestMessage = validate_data(event.data);
|
||||
}
|
||||
function send_click()
|
||||
{
|
||||
ws.send("");
|
||||
if (latestMessage !== null) {
|
||||
document.getElementById("button").textContent=latestMessage;
|
||||
latestMessage = null;
|
||||
if (!locally)
|
||||
{
|
||||
ws.send("");
|
||||
if (score !== null)
|
||||
{
|
||||
document.getElementById("button").textContent=score;
|
||||
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) {
|
||||
return Math.round(Math.random() * (max - min) + min);
|
||||
}
|
||||
window.addEventListener('beforeunload', () => {
|
||||
ws.close();
|
||||
});
|
||||
}, !locally);
|
||||
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 }
|
||||
else { return "anon" }
|
||||
}
|
||||
|
||||
@@ -15,8 +15,9 @@
|
||||
<p>Total number of resets and personal bests of each username</p>
|
||||
<div id="pingscore-table"></div>
|
||||
</body>
|
||||
<script src="/address.js"></script>
|
||||
<script>
|
||||
const ws = new WebSocket('ws://deadvey.com:8084/ws-leaderboard');
|
||||
const ws = new WebSocket(`ws://${ADDRESS}:8084/ws-leaderboard`);
|
||||
const regex = new RegExp("^[a-zA-Z0-9_-]+$");
|
||||
ws.onopen = (event) => {
|
||||
ws.send('0'); // send all
|
||||
|
||||
Reference in New Issue
Block a user