From 37c996fc24244da1d740e9212859cbf5083a8bc4 Mon Sep 17 00:00:00 2001 From: clizia Date: Fri, 7 Mar 2025 17:03:50 +0100 Subject: [PATCH] sistemato --- src/bin/client.rs | 45 --------------------------------------------- src/client.rs | 1 + src/ui.rs | 44 ++++++++++++++++++++++++++++++++++---------- 3 files changed, 35 insertions(+), 55 deletions(-) diff --git a/src/bin/client.rs b/src/bin/client.rs index 7aefcd6..f35d109 100644 --- a/src/bin/client.rs +++ b/src/bin/client.rs @@ -19,51 +19,6 @@ use ratatui::{ Frame, }; -// async fn delete_player(player: &Player, client: reqwest::Client, addr: &str) -> anyhow::Result<()> { -// let player_id = &player.name; -// let response = client -// .get([&addr, "/delete/", &player_id].concat()) -// .send().await; - -// if let Err(e) = &response { -// println!("{e}\n"); -// return Ok(()) -// } - -// println!("{}", response?.text().await?); - -// Ok(()) -// } - -// async fn handle_terminal(player: &Player, stdout: &mut Stdout, client: reqwest::Client, addr: &str) -> anyhow::Result<()> { -// 'handler: loop { -// while let Ok(_event_happened) = poll(Duration::from_millis(2)) { -// match event::read().expect("diomerds") { -// TermEvent::Key(key) => { -// match (key.code, key.modifiers) { -// (KeyCode::Char('c'), KeyModifiers::CONTROL) => break 'handler, -// (KeyCode::Char('h'), KeyModifiers::NONE) => { -// for line in HELP.split('\n') { -// println!("{line}") -// } -// }, -// (KeyCode::Char('1'), KeyModifiers::NONE) => { -// send_player(&player, client.clone(), addr).await?; -// }, -// (KeyCode::Char('2'), KeyModifiers::NONE) => { -// delete_player(&player, client.clone(), addr).await?; -// }, -// (_, _) => println!("{:?}\n", key), -// } -// }, -// _ => continue, -// } -// } -// } - -// Ok(()) -// } - #[tokio::main] pub async fn main() -> anyhow::Result<()> { let mut stdout = std::io::stdout(); diff --git a/src/client.rs b/src/client.rs index cd4017b..468ddd5 100644 --- a/src/client.rs +++ b/src/client.rs @@ -14,6 +14,7 @@ pub struct Client { pub popup_content: String, pub response: String, pub lobby: Option, + pub lobby_id: String, } impl Client { diff --git a/src/ui.rs b/src/ui.rs index b8b33d4..9a11f7b 100644 --- a/src/ui.rs +++ b/src/ui.rs @@ -11,10 +11,18 @@ use ratatui::{ widgets::{Block, Borders, Clear, List, ListItem, Paragraph, Widget, Wrap}, Frame, }; -use ratatui::crossterm::{event::{self, Event, KeyCode, KeyEvent, KeyEventKind}}; +use ratatui::crossterm::event::{self, Event, KeyCode, KeyEvent, KeyEventKind}; use crate::{client::Client, player::Player}; +const HELP: &str = r#"bindings: +h - show help +q - quit +1 - create player +2 - create lobby +3 - send player to server +4 - send lobby to server"#; + #[derive(Debug, Default, Setters)] struct Popup<'a> { #[setters(into)] @@ -51,25 +59,35 @@ async fn handle_key_event(mut client: &mut Client, key_event: KeyEvent) { false => { match key_event.code { KeyCode::Char('q') => Client::exit(client), + KeyCode::Char('h') => { + client.popup_title = "help".to_string(); + client.popup_content = HELP.to_string(); + client.popup = true; + }, KeyCode::Char('1') => { - if !client.name.is_empty() { + if client.user != None { client.popup_title = "Error".to_string(); client.popup_content = "user already_created".to_string(); client.popup = true; - return + } else { + client.user = Some(Player::new().expect("toto")); + client.name = client.user.clone().unwrap().name; } - - client.user = Some(Player::new().expect("toto")); - client.name = client.user.clone().unwrap().name; }, KeyCode::Char('2') => { - todo!() + if client.lobby != None { + client.popup_title = "Error".to_string(); + 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; + } }, KeyCode::Char('3') => { if let Some(player) = client.user.clone() { let addr = client.addr.clone(); Client::send_player(&mut client, &player, &addr).await.unwrap(); - return } } _ => (), @@ -123,11 +141,15 @@ pub fn ui(frame: &mut Frame, app: &Client) { "username: ".into(), app.name.clone().yellow(), ]), + Line::from(vec![ + "lobby: ".into(), + app.lobby_id.clone().yellow(), + ]), ]); - let title = Line::from(" durak tui test fre ".bold()); + let top_block_title = Line::from(" durak tui test fre ".bold()); let top_block = Block::default() - .title(title.centered()) + .title(top_block_title.centered()) .borders(Borders::ALL) .style(Style::default()); frame.render_widget(Paragraph::new(infos).block(top_block), chunks[0]); @@ -139,7 +161,9 @@ pub fn ui(frame: &mut Frame, app: &Client) { ]) ]); + let btm_block_title = Line::from(" messaggini carini :3 ".bold()); let btm_block = Block::default() + .title(btm_block_title.centered()) .title_bottom(instructions.centered()) .borders(Borders::ALL) .style(Style::default());