added seperate route for leaderboards

This commit is contained in:
2026-05-31 14:36:17 +01:00
parent 1f9bdb3ac0
commit 18e5127462
5 changed files with 119 additions and 59 deletions
BIN
View File
Binary file not shown.
+88 -32
View File
@@ -1,3 +1,4 @@
// TODO rename pingscores userscores
use std::
{
collections::HashMap,
@@ -18,15 +19,15 @@ use axum::
response::{Html, IntoResponse},
routing::get,
};
use futures::StreamExt as _;
use rand::random_bool;
use serde::{Deserialize, Serialize, de};
use serde_json::json;
use tokio::
{
sync::{Mutex, mpsc},
time::{Duration, sleep},
};
use futures::StreamExt as _;
use rand::random_bool;
use serde::{Deserialize, Serialize, de};
use serde_json::json;
#[derive(Deserialize, Serialize, Debug, Ord, Eq, PartialEq, PartialOrd, Clone)]
struct Entry
@@ -131,6 +132,7 @@ async fn main() -> anyhow::Result<()>
let app = Router::new()
.route("/", get(index))
.route("/ws", get(ws_handler))
.route("/ws-leaderboard", get(leaderboard_handler))
.route("/leaderboard", get(leaderboard))
.with_state
(AppState {
@@ -148,16 +150,6 @@ async fn main() -> anyhow::Result<()>
Ok(())
}
async fn index() -> Html<&'static str>
{
Html(include_str!("../index.html"))
}
async fn leaderboard() -> Html<&'static str>
{
Html(include_str!("../leaderboard.html"))
}
// receiver: 0 for hiscore, 1 for loscore, 2 for pingscore
async fn handle_hiscores
(
@@ -281,6 +273,76 @@ async fn handle_hiscores
}
}
async fn leaderboard_handler
(
ws: WebSocketUpgrade,
State(state): State<AppState>
)
-> impl IntoResponse
{
ws.on_upgrade
(|socket| async move {
handle_leaderboard
(
socket,
&state.hiscores,
&state.loscores,
&state.pingscores,
)
.await;
})
}
async fn handle_leaderboard
(
mut socket: WebSocket,
hiscores: &Mutex<Vec<Entry>>,
loscores: &Mutex<Vec<Entry>>,
pingscores: &Mutex<HashMap<String, (u64, u32)>>,
)
{
match socket.next().await
{
Some(Ok(Message::Text(selection))) =>
{
let msg;
match selection.as_str()
{
"0" => // all the leaderboards
{
msg =
{
json!
({
"hiscores": &*hiscores.lock().await,
"loscores": &*loscores.lock().await,
"pingscores": &*pingscores.lock().await
})
.to_string()
};
},
"1" => // just the hiscores table
{
msg = json! ({ "hiscores": &*hiscores.lock().await }).to_string()
},
"2" => // just the loscores table
{
msg = json! ({ "loscores": &*hiscores.lock().await }).to_string()
},
"3" => // just the pingscores table
{
msg = json! ({ "pingscores": &*hiscores.lock().await }).to_string()
},
_ => { msg = "Invalid leaderboard selection, please use 0,1,2 or 3".to_string() },
}
let _ = socket.send(Message::Text(msg.into())).await;
},
_ =>
{
let _ = socket.send(Message::Text("Invalid leaderboard selection, please use 0,1,2 or 3".into())).await;
},
}
}
async fn ws_handler
(
ws: WebSocketUpgrade,
@@ -294,9 +356,6 @@ async fn ws_handler
(
socket,
&state.tx,
&state.hiscores,
&state.loscores,
&state.pingscores,
)
.await;
})
@@ -306,22 +365,10 @@ async fn handle_socket
(
mut socket: WebSocket,
tx: &mpsc::Sender<LeaderboardUpdate>,
hiscores: &Mutex<Vec<Entry>>,
loscores: &Mutex<Vec<Entry>>,
pingscores: &Mutex<HashMap<String, (u64, u32)>>,
)
{
let mut value: u32 = 0;
let msg =
{
json!
({
"hiscores": &*hiscores.lock().await,
"loscores": &*loscores.lock().await,
"pingscores": &*pingscores.lock().await
})
.to_string()
};
let msg: String;
let Some(name) = socket.next().await else
{
@@ -344,8 +391,6 @@ async fn handle_socket
let mut resets: u32 = 0;
let mut prev: u32 = 0;
let _ = socket.send(Message::Text(msg.into())).await;
while let Some(msg) = socket.next().await
{
match msg
@@ -409,3 +454,14 @@ fn validate_name(input: &str) -> bool {
input.chars().all(|c| c.is_ascii_alphanumeric())
}
// static routes
async fn index() -> Html<&'static str>
{
Html(include_str!("../index.html"))
}
async fn leaderboard() -> Html<&'static str>
{
Html(include_str!("../leaderboard.html"))
}