diff --git a/Cargo.lock b/Cargo.lock index 642cb19..2378b0f 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -577,6 +577,7 @@ dependencies = [ "tower 0.5.2", "tracing", "tracing-subscriber", + "tui-textarea", ] [[package]] @@ -2459,6 +2460,17 @@ version = "0.2.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "e421abadd41a4225275504ea4d6566923418b7f05506fbc9c0fe86ba7396114b" +[[package]] +name = "tui-textarea" +version = "0.7.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0a5318dd619ed73c52a9417ad19046724effc1287fb75cdcc4eca1d6ac1acbae" +dependencies = [ + "crossterm", + "ratatui", + "unicode-width 0.2.0", +] + [[package]] name = "typenum" version = "1.17.0" diff --git a/Cargo.toml b/Cargo.toml index 040fd7d..a9f7310 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -18,7 +18,7 @@ path = "src/bin/client.rs" [dependencies] axum = "0.8.1" tower = "0.5.2" -reqwest = "0.12.0" +reqwest = {version = "0.12.0", features = ["json"]} hyper = { version = "1", features = ["full"] } http-body-util = "0.1" hyper-util = { version = "0.1", features = ["full"] } @@ -36,3 +36,4 @@ tracing = "0.1.41" tracing-subscriber = "0.3.19" ratatui = "0.29.0" derive_setters = "0.1.6" +tui-textarea = "0.7.0" diff --git a/src/client.rs b/src/client.rs index 468ddd5..6879aff 100644 --- a/src/client.rs +++ b/src/client.rs @@ -1,19 +1,20 @@ +use anyhow::Result; use ratatui::DefaultTerminal; -use crate::{lobby::Lobby, message::{Message, MessageKind}, player::Player, ui::ui}; +use crate::{message::{Message, MessageKind}, player::Player, ui::ui}; #[derive(Debug, Default)] pub struct Client { pub addr: String, pub client: reqwest::Client, - pub user: Option, - pub name: String, + pub user: Option, + pub user_name: String, pub exit: bool, pub popup: bool, pub popup_title: String, pub popup_content: String, pub response: String, - pub lobby: Option, + pub lobby: Option, pub lobby_id: String, } @@ -34,24 +35,25 @@ impl Client { self.exit = true; } - pub async fn send_player(&mut self, player: &Player, addr: &str) -> anyhow::Result<()> { - let player_message = Message::new( - &player.get_name(), - MessageKind::CreatePlayer, - player.encode()? - )?; - + pub async fn send(&mut self, message: Message, addr: &str, path: &str) -> anyhow::Result<()> { let response = self.client - .post([&addr, "/create/player"].concat()) + .post([&addr, path].concat()) .header("Content-Type", "application/json") - .body(player_message.encode()?) + .body(message.encode()?) .send().await; if let Err(e) = &response { self.response = e.to_string(); return Ok(()) } else { - self.response = format!("{:?}", response?); + let response: Message = response?.json().await?; + self.response = format!("{:?}", &response); + + match response.message_kind { + MessageKind::CreatePlayer => self.user = Some(response.content), + MessageKind::CreateLobby => self.lobby = Some(response.content), + _ => () + } } Ok(()) diff --git a/src/lib.rs b/src/lib.rs index c781f55..5c51f6f 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -10,3 +10,4 @@ pub mod constant; pub mod db; pub mod router; pub mod ui; +pub mod entity; diff --git a/src/router.rs b/src/router.rs index 7123709..8bb20d3 100644 --- a/src/router.rs +++ b/src/router.rs @@ -38,23 +38,22 @@ impl App { match json_body { Ok(Json(message)) => { println!("{:?}", message); - let player: Player = serde_json::from_str(&message.content).expect(&StatusCode::PRECONDITION_FAILED.to_string()); - { - let mut players = db.players - .lock().expect("ciao"); + let player: Player = Player::new().unwrap(); - if players.contains_key(&player.name) { - return Err(StatusCode::UNAVAILABLE_FOR_LEGAL_REASONS) - } + let mut players = db.players + .lock().expect("ciao"); - players.insert( - player.get_name(), - Bytes::copy_from_slice(player.encode().expect(&StatusCode::EXPECTATION_FAILED.to_string()).as_ref()) - ); - println!("{:?}", db.players); + if players.contains_key(&player.name) { + return Err(StatusCode::UNAVAILABLE_FOR_LEGAL_REASONS) } - Ok(Json(Message::new("server", MessageKind::CreatePlayer, "user created").expect("failed creating message"))) + players.insert( + player.get_name(), + Bytes::copy_from_slice(player.encode().expect(&StatusCode::EXPECTATION_FAILED.to_string()).as_ref()) + ); + println!("{:?}", db.players); + + Ok(Json(Message::new("server", MessageKind::CreatePlayer, &player.name).expect("failed creating message"))) } Err(_) => Err(StatusCode::UNPROCESSABLE_ENTITY) } @@ -67,23 +66,22 @@ impl App { match json_body { Ok(Json(message)) => { println!("{:?}", message); - let lobby: Lobby = serde_json::from_str(&message.content).expect(&StatusCode::PRECONDITION_FAILED.to_string()); - { - let mut lobbies = db.lobbies - .lock().expect("ciao"); + let lobby: Lobby = Lobby::new(); - if lobbies.contains_key(&lobby.id) { - return Err(StatusCode::UNAVAILABLE_FOR_LEGAL_REASONS) - } + let mut lobbies = db.lobbies + .lock().expect("ciao"); - lobbies.insert( - lobby.get_id(), - Bytes::copy_from_slice(lobby.encode().expect(&StatusCode::EXPECTATION_FAILED.to_string()).as_ref()) - ); - println!("{:?}", lobbies); + if lobbies.contains_key(&lobby.id) { + return Err(StatusCode::UNAVAILABLE_FOR_LEGAL_REASONS) } - Ok(Json(Message::new("server", MessageKind::CreateLobby, "lobby created").expect("failed creating message"))) + lobbies.insert( + lobby.get_id(), + Bytes::copy_from_slice(lobby.encode().expect(&StatusCode::EXPECTATION_FAILED.to_string()).as_ref()) + ); + println!("{:?}", lobbies); + + Ok(Json(Message::new("server", MessageKind::CreateLobby, &lobby.id).expect("failed creating message"))) }, Err(_) => Err(StatusCode::UNPROCESSABLE_ENTITY), } diff --git a/src/ui.rs b/src/ui.rs index 9a11f7b..b41d2ca 100644 --- a/src/ui.rs +++ b/src/ui.rs @@ -13,7 +13,7 @@ use ratatui::{ }; use ratatui::crossterm::event::{self, Event, KeyCode, KeyEvent, KeyEventKind}; -use crate::{client::Client, player::Player}; +use crate::{client::Client, message::{Message, MessageKind}, player::Player}; const HELP: &str = r#"bindings: h - show help @@ -70,8 +70,16 @@ async fn handle_key_event(mut client: &mut Client, key_event: KeyEvent) { client.popup_content = "user already_created".to_string(); client.popup = true; } else { - client.user = Some(Player::new().expect("toto")); - client.name = client.user.clone().unwrap().name; + let addr = client.addr.clone(); + let path = "/create/player"; + let message = Message::new( + "", + MessageKind::CreatePlayer, + "" + ).unwrap(); + + Client::send(&mut client, message, &addr, &path).await.unwrap(); + client.user_name = client.user.clone().unwrap(); } }, KeyCode::Char('2') => { @@ -80,16 +88,18 @@ async fn handle_key_event(mut client: &mut Client, key_event: KeyEvent) { client.popup_content = "lobby already_created".to_string(); client.popup = true; } else { - client.user = Some(Player::new().expect("toto")); - client.name = client.user.clone().unwrap().name; + let addr = client.addr.clone(); + let path = "/create/lobby"; + let message = Message::new( + "", + MessageKind::CreateLobby, + "" + ).unwrap(); + + Client::send(&mut client, message, &addr, &path).await.unwrap(); + client.lobby_id = client.lobby.clone().unwrap(); } }, - KeyCode::Char('3') => { - if let Some(player) = client.user.clone() { - let addr = client.addr.clone(); - Client::send_player(&mut client, &player, &addr).await.unwrap(); - } - } _ => (), } } @@ -107,7 +117,7 @@ pub async fn handle_events(client: &mut Client) -> anyhow::Result<()> { handle_key_event(client, key).await } _ => {} - }; + } Ok(()) } @@ -139,7 +149,7 @@ pub fn ui(frame: &mut Frame, app: &Client) { ]), Line::from(vec![ "username: ".into(), - app.name.clone().yellow(), + app.user_name.clone().yellow(), ]), Line::from(vec![ "lobby: ".into(),