reverted the idea of getting a lobby list from the server
This commit is contained in:
parent
b37ab4a37d
commit
789e3912d3
4 changed files with 101 additions and 111 deletions
|
@ -1,13 +1,12 @@
|
||||||
use anyhow::Result;
|
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)]
|
#[derive(Debug, Default)]
|
||||||
pub struct Client {
|
pub struct Client {
|
||||||
pub addr: String,
|
pub addr: String,
|
||||||
pub client: reqwest::Client,
|
pub client: reqwest::Client,
|
||||||
pub current_screen: CurrentScreen,
|
|
||||||
pub user: Option<String>,
|
pub user: Option<String>,
|
||||||
pub user_name: String,
|
pub user_name: String,
|
||||||
pub exit: bool,
|
pub exit: bool,
|
||||||
|
|
|
@ -19,6 +19,7 @@ pub enum MessageKind {
|
||||||
DeleteLobby,
|
DeleteLobby,
|
||||||
JoinLobby,
|
JoinLobby,
|
||||||
QuitLobby,
|
QuitLobby,
|
||||||
|
GetLobbies,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Message {
|
impl Message {
|
||||||
|
|
|
@ -133,4 +133,5 @@ impl App {
|
||||||
) -> Result<axum::response::Json<Message>, StatusCode> {
|
) -> Result<axum::response::Json<Message>, StatusCode> {
|
||||||
todo!()
|
todo!()
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
205
src/ui.rs
205
src/ui.rs
|
@ -8,7 +8,7 @@ use ratatui::{
|
||||||
},
|
},
|
||||||
style::{Color, Style, Stylize},
|
style::{Color, Style, Stylize},
|
||||||
text::{Line, Span, Text},
|
text::{Line, Span, Text},
|
||||||
widgets::{Block, Borders, Clear, List, ListItem, Paragraph, Widget, Wrap},
|
widgets::{Block, Borders, Clear, List, ListItem, ListState, Paragraph, Widget, Wrap},
|
||||||
Frame,
|
Frame,
|
||||||
};
|
};
|
||||||
use ratatui::crossterm::event::{self, Event, KeyCode, KeyEvent, KeyEventKind};
|
use ratatui::crossterm::event::{self, Event, KeyCode, KeyEvent, KeyEventKind};
|
||||||
|
@ -57,61 +57,56 @@ impl Widget for Popup<'_> {
|
||||||
#[derive(Debug, Default)]
|
#[derive(Debug, Default)]
|
||||||
pub enum CurrentScreen {
|
pub enum CurrentScreen {
|
||||||
#[default] Menu,
|
#[default] Menu,
|
||||||
LobbyList,
|
Lobbies,
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn handle_key_event(mut client: &mut Client, key_event: KeyEvent) {
|
async fn handle_key_event(mut client: &mut Client, key_event: KeyEvent) {
|
||||||
match &client.popup {
|
match &client.popup {
|
||||||
false => {
|
false => {
|
||||||
match client.current_screen {
|
match key_event.code {
|
||||||
CurrentScreen::Menu => {
|
KeyCode::Char('q') => Client::exit(client),
|
||||||
match key_event.code {
|
KeyCode::Char('h') => {
|
||||||
KeyCode::Char('q') => Client::exit(client),
|
client.popup_title = "help".to_string();
|
||||||
KeyCode::Char('h') => {
|
client.popup_content = HELP.to_string();
|
||||||
client.popup_title = "help".to_string();
|
client.popup = true;
|
||||||
client.popup_content = HELP.to_string();
|
},
|
||||||
client.popup = true;
|
KeyCode::Char('1') => {
|
||||||
},
|
if client.user != None {
|
||||||
KeyCode::Char('1') => {
|
client.popup_title = "Error".to_string();
|
||||||
if client.user != None {
|
client.popup_content = "user already_created".to_string();
|
||||||
client.popup_title = "Error".to_string();
|
client.popup = true;
|
||||||
client.popup_content = "user already_created".to_string();
|
} else {
|
||||||
client.popup = true;
|
let addr = client.addr.clone();
|
||||||
} else {
|
let path = "/create/player";
|
||||||
let addr = client.addr.clone();
|
let message = Message::new(
|
||||||
let path = "/create/player";
|
"",
|
||||||
let message = Message::new(
|
MessageKind::CreatePlayer,
|
||||||
"",
|
""
|
||||||
MessageKind::CreatePlayer,
|
).unwrap();
|
||||||
""
|
|
||||||
).unwrap();
|
|
||||||
|
|
||||||
Client::send(&mut client, message, &addr, &path).await.unwrap();
|
Client::send(&mut client, message, &addr, &path).await.unwrap();
|
||||||
client.user_name = client.user.clone().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();
|
|
||||||
}
|
|
||||||
},
|
|
||||||
_ => (),
|
|
||||||
}
|
}
|
||||||
}
|
},
|
||||||
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 => {
|
true => {
|
||||||
|
@ -134,70 +129,64 @@ pub async fn handle_events(client: &mut Client) -> anyhow::Result<()> {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn ui(frame: &mut Frame, app: &Client) {
|
pub fn ui(frame: &mut Frame, app: &Client) {
|
||||||
match app.current_screen {
|
let chunks = Layout::default()
|
||||||
CurrentScreen::Menu => {
|
.direction(Direction::Vertical)
|
||||||
|
.constraints([
|
||||||
|
Constraint::Min(3),
|
||||||
|
Constraint::Min(1),
|
||||||
|
])
|
||||||
|
.split(frame.area());
|
||||||
|
|
||||||
let chunks = Layout::default()
|
let instructions = Line::from(vec![
|
||||||
.direction(Direction::Vertical)
|
" 1".blue().bold(),
|
||||||
.constraints([
|
" create player ".into(),
|
||||||
Constraint::Min(3),
|
"2".blue().bold(),
|
||||||
Constraint::Min(1),
|
" create lobby ".into(),
|
||||||
])
|
"<Q>".blue().bold(),
|
||||||
.split(frame.area());
|
" quit ".into(),
|
||||||
|
]);
|
||||||
|
|
||||||
let instructions = Line::from(vec![
|
let infos = Text::from(vec![
|
||||||
" 1".blue().bold(),
|
Line::from(vec![
|
||||||
" create player ".into(),
|
"connected to: ".into(),
|
||||||
"2".blue().bold(),
|
app.addr.clone().yellow(),
|
||||||
" create lobby ".into(),
|
]),
|
||||||
"<Q>".blue().bold(),
|
Line::from(vec![
|
||||||
" quit ".into(),
|
"username: ".into(),
|
||||||
]);
|
app.user_name.clone().yellow(),
|
||||||
|
]),
|
||||||
|
Line::from(vec![
|
||||||
|
"lobby: ".into(),
|
||||||
|
app.lobby_id.clone().yellow(),
|
||||||
|
]),
|
||||||
|
]);
|
||||||
|
|
||||||
let infos = Text::from(vec![
|
let top_block_title = Line::from(" durak tui test fre ".bold());
|
||||||
Line::from(vec![
|
let top_block = Block::default()
|
||||||
"connected to: ".into(),
|
.title(top_block_title.centered())
|
||||||
app.addr.clone().yellow(),
|
.borders(Borders::ALL)
|
||||||
]),
|
.style(Style::default());
|
||||||
Line::from(vec![
|
frame.render_widget(Paragraph::new(infos).block(top_block), chunks[0]);
|
||||||
"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 server_message = Text::from(vec![
|
||||||
let top_block = Block::default()
|
Line::from(vec![
|
||||||
.title(top_block_title.centered())
|
"message: ".into(),
|
||||||
.borders(Borders::ALL)
|
app.response.clone().blue(),
|
||||||
.style(Style::default());
|
])
|
||||||
frame.render_widget(Paragraph::new(infos).block(top_block), chunks[0]);
|
]);
|
||||||
|
|
||||||
let server_message = Text::from(vec![
|
let btm_block_title = Line::from(" messaggini carini :3 ".bold());
|
||||||
Line::from(vec![
|
let btm_block = Block::default()
|
||||||
"message: ".into(),
|
.title(btm_block_title.centered())
|
||||||
app.response.clone().blue(),
|
.title_bottom(instructions.centered())
|
||||||
])
|
.borders(Borders::ALL)
|
||||||
]);
|
.style(Style::default());
|
||||||
|
frame.render_widget(
|
||||||
let btm_block_title = Line::from(" messaggini carini :3 ".bold());
|
Paragraph::new(server_message)
|
||||||
let btm_block = Block::default()
|
.wrap(Wrap { trim: true, })
|
||||||
.title(btm_block_title.centered())
|
.block(btm_block),
|
||||||
.title_bottom(instructions.centered())
|
chunks[1],
|
||||||
.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 popup_area = Rect {
|
let popup_area = Rect {
|
||||||
x: frame.area().width / 4,
|
x: frame.area().width / 4,
|
||||||
|
|
Loading…
Add table
Reference in a new issue