diff --git a/assets/web/index.html b/assets/web/index.html index 577061c..db6b233 100644 --- a/assets/web/index.html +++ b/assets/web/index.html @@ -21,6 +21,20 @@ return false; } } + document.getElementById('login-username').onkeypress = (e) => { + if(e.key == 'Enter') { + document.getElementById('login-pass').focus(); + return false; + } + } + document.getElementById('login-pass').onkeypress = (e) => { + if(e.key == 'Enter') { + onLoginClick(); + return false; + } + } + // focus on the username field for the sake of just pressing enter multiple times + document.getElementById('login-username').focus(); } tavern.onlogin = (s) => { @@ -29,8 +43,8 @@ let game = document.getElementById('game'); login.style.display = 'none'; game.style.display = 'flex'; - // get all chat history as soon as we get in - tavern.get_chat_history(0, 0); + // get last 50 msgs (i think that is enough for now) when we get in + tavern.get_last_msgs(50); } else { alert("Invalid username or password!"); @@ -55,6 +69,7 @@ history.removeChild(exists); } history.appendChild(msg); + msg.scrollIntoView(); } function onLoginClick() { let username = document.getElementById('login-username').value; diff --git a/assets/web/socket.js b/assets/web/socket.js index 3c9d904..d660fab 100644 --- a/assets/web/socket.js +++ b/assets/web/socket.js @@ -37,4 +37,8 @@ tavern.simple_msg = (msg, token) => { tavern.get_chat_history = (from, amount) => { if(!tavern.connected || tavern.loggedIn) { return false; } tavern.socket.send(JSON.stringify({ get_chat_history: { from: from, amount: amount } })) +} +tavern.get_last_msgs = (amount) => { + if(!tavern.connected || tavern.loggedIn) { return false; } + tavern.socket.send(JSON.stringify({ get_last_messages: { amount: amount } })) } \ No newline at end of file diff --git a/src/api/mod.rs b/src/api/mod.rs index c65e844..5f63bdb 100644 --- a/src/api/mod.rs +++ b/src/api/mod.rs @@ -14,6 +14,7 @@ pub enum Request { Login(login::LoginRequest), Message(ChatMessage), GetChatHistory { amount: usize, from: usize }, + GetLastMessages { amount: usize, }, Quit, Shutdown } diff --git a/src/lib.rs b/src/lib.rs index 388583d..0cb9552 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -57,6 +57,15 @@ impl GameServer { .collect(); _ = broadcast.send(api::Response::GetChatHistory(history)); }, + api::Request::GetLastMessages { mut amount } => { + if amount == 0 { amount = self.chat.len(); } + let start = if amount >= self.chat.len() { self.chat.len() } else { self.chat.len() - amount }; + let history: Vec = self.chat.iter() + .skip(start) + .map(|m| m.1.clone()) + .collect(); + _ = broadcast.send(api::Response::GetChatHistory(history)); + }, } } _ = broadcast.send(api::Response::Shutdown); diff --git a/src/main.rs b/src/main.rs index 754ad60..e6a775e 100644 --- a/src/main.rs +++ b/src/main.rs @@ -70,11 +70,11 @@ async fn handle_socket(mut socket: ws::WebSocket, msend: mpsc::Sender<(String, R println!("Got unauthorized message: {:?}", t); match req { // TODO: Actual signing in mechanism with multiple ids :) - Request::Login(r) => if r.username == "rusty" { + Request::Login(r) => if r.username == "rusty" || r.username == "honey" { _ = socket.send(Message::Text( serde_json::to_string(&Response::Login(open_tavern::api::login::LoginResult { success: true })).unwrap() )).await; - id = Some(String::from("rusty")); + id = Some(String::from(r.username)); break; } else {