diff --git a/src/client.rs b/src/client.rs index 6bad4b8..741650a 100644 --- a/src/client.rs +++ b/src/client.rs @@ -1,13 +1,12 @@ use anyhow::Result; -use ratatui::DefaultTerminal; +use ratatui::{widgets::ListState, DefaultTerminal}; -use crate::{message::{Message, MessageKind}, player::Player, ui::{ui, CurrentScreen}}; +use crate::{lobby::Lobby, message::{Message, MessageKind}, player::Player, ui::{ui, CurrentScreen}}; #[derive(Debug, Default)] pub struct Client { pub addr: String, pub client: reqwest::Client, - pub current_screen: CurrentScreen, pub user: Option, pub user_name: String, pub exit: bool, diff --git a/src/message.rs b/src/message.rs index 71e31d0..d0f872c 100644 --- a/src/message.rs +++ b/src/message.rs @@ -19,6 +19,7 @@ pub enum MessageKind { DeleteLobby, JoinLobby, QuitLobby, + GetLobbies, } impl Message { diff --git a/src/router.rs b/src/router.rs index 031581b..715fd8a 100644 --- a/src/router.rs +++ b/src/router.rs @@ -133,4 +133,5 @@ impl App { ) -> Result, StatusCode> { todo!() } + } diff --git a/src/ui.rs b/src/ui.rs index 95e0a2f..4ed8c7b 100644 --- a/src/ui.rs +++ b/src/ui.rs @@ -8,7 +8,7 @@ use ratatui::{ }, style::{Color, Style, Stylize}, text::{Line, Span, Text}, - widgets::{Block, Borders, Clear, List, ListItem, Paragraph, Widget, Wrap}, + widgets::{Block, Borders, Clear, List, ListItem, ListState, Paragraph, Widget, Wrap}, Frame, }; use ratatui::crossterm::event::{self, Event, KeyCode, KeyEvent, KeyEventKind}; @@ -57,61 +57,56 @@ impl Widget for Popup<'_> { #[derive(Debug, Default)] pub enum CurrentScreen { #[default] Menu, - LobbyList, + Lobbies, } async fn handle_key_event(mut client: &mut Client, key_event: KeyEvent) { match &client.popup { false => { - match client.current_screen { - CurrentScreen::Menu => { - 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.user != None { - client.popup_title = "Error".to_string(); - client.popup_content = "user already_created".to_string(); - client.popup = true; - } else { - let addr = client.addr.clone(); - let path = "/create/player"; - let message = Message::new( - "", - MessageKind::CreatePlayer, - "" - ).unwrap(); + 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.user != None { + client.popup_title = "Error".to_string(); + client.popup_content = "user already_created".to_string(); + client.popup = true; + } else { + 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') => { - if client.lobby != None { - client.popup_title = "Error".to_string(); - client.popup_content = "lobby already_created".to_string(); - client.popup = true; - } else { - 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(); - } - }, - _ => (), + Client::send(&mut client, message, &addr, &path).await.unwrap(); + client.user_name = client.user.clone().unwrap(); } - } - CurrentScreen::LobbyList => todo!(), + }, + KeyCode::Char('2') => { + if client.lobby != None { + client.popup_title = "Error".to_string(); + client.popup_content = "lobby already_created".to_string(); + client.popup = true; + } else { + 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(); + } + }, + _ => (), } } true => { @@ -134,70 +129,64 @@ pub async fn handle_events(client: &mut Client) -> anyhow::Result<()> { } pub fn ui(frame: &mut Frame, app: &Client) { - match app.current_screen { - CurrentScreen::Menu => { - - let chunks = Layout::default() - .direction(Direction::Vertical) - .constraints([ - Constraint::Min(3), - Constraint::Min(1), - ]) - .split(frame.area()); + let chunks = Layout::default() + .direction(Direction::Vertical) + .constraints([ + Constraint::Min(3), + Constraint::Min(1), + ]) + .split(frame.area()); - let instructions = Line::from(vec![ - " 1".blue().bold(), - " create player ".into(), - "2".blue().bold(), - " create lobby ".into(), - "".blue().bold(), - " quit ".into(), - ]); + let instructions = Line::from(vec![ + " 1".blue().bold(), + " create player ".into(), + "2".blue().bold(), + " create lobby ".into(), + "".blue().bold(), + " quit ".into(), + ]); - let infos = Text::from(vec![ - Line::from(vec![ - "connected to: ".into(), - app.addr.clone().yellow(), - ]), - Line::from(vec![ - "username: ".into(), - app.user_name.clone().yellow(), - ]), - Line::from(vec![ - "lobby: ".into(), - app.lobby_id.clone().yellow(), - ]), - ]); + let infos = Text::from(vec![ + Line::from(vec![ + "connected to: ".into(), + app.addr.clone().yellow(), + ]), + Line::from(vec![ + "username: ".into(), + app.user_name.clone().yellow(), + ]), + Line::from(vec![ + "lobby: ".into(), + app.lobby_id.clone().yellow(), + ]), + ]); - let top_block_title = Line::from(" durak tui test fre ".bold()); - let top_block = Block::default() - .title(top_block_title.centered()) - .borders(Borders::ALL) - .style(Style::default()); - frame.render_widget(Paragraph::new(infos).block(top_block), chunks[0]); + let top_block_title = Line::from(" durak tui test fre ".bold()); + let top_block = Block::default() + .title(top_block_title.centered()) + .borders(Borders::ALL) + .style(Style::default()); + frame.render_widget(Paragraph::new(infos).block(top_block), chunks[0]); - let server_message = Text::from(vec![ - Line::from(vec![ - "message: ".into(), - app.response.clone().blue(), - ]) - ]); + let server_message = Text::from(vec![ + Line::from(vec![ + "message: ".into(), + app.response.clone().blue(), + ]) + ]); - 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()); - frame.render_widget( - Paragraph::new(server_message) - .wrap(Wrap { trim: true, }) - .block(btm_block), - chunks[1], - ); - } - CurrentScreen::LobbyList => todo!(), - } + 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()); + frame.render_widget( + Paragraph::new(server_message) + .wrap(Wrap { trim: true, }) + .block(btm_block), + chunks[1], + ); let popup_area = Rect { x: frame.area().width / 4,