second try at joining lobby
This commit is contained in:
parent
16343712d9
commit
72bd4ce74c
3 changed files with 114 additions and 65 deletions
|
@ -1,24 +1,32 @@
|
|||
use anyhow::Result;
|
||||
use ratatui::{widgets::ListState, DefaultTerminal};
|
||||
use tui_textarea::TextArea;
|
||||
|
||||
use crate::{lobby::Lobby, message::{Message, MessageKind}, player::Player, ui::{ui, CurrentScreen}};
|
||||
use crate::{
|
||||
lobby::Lobby,
|
||||
message::{Message, MessageKind},
|
||||
player::Player,
|
||||
ui::ui,
|
||||
};
|
||||
|
||||
#[derive(Debug, Default)]
|
||||
pub struct Client {
|
||||
pub struct Client<'a> {
|
||||
pub addr: String,
|
||||
pub client: reqwest::Client,
|
||||
pub user: Option<String>,
|
||||
pub user_name: String,
|
||||
pub exit: bool,
|
||||
pub popup_user_input: bool,
|
||||
pub popup: bool,
|
||||
pub popup_title: String,
|
||||
pub popup_content: String,
|
||||
pub response: String,
|
||||
pub lobby: Option<String>,
|
||||
pub lobby_id: String,
|
||||
pub textarea: TextArea<'a>,
|
||||
}
|
||||
|
||||
impl Client {
|
||||
impl Client<'_> {
|
||||
pub async fn run(&mut self, terminal: &mut DefaultTerminal) -> anyhow::Result<()> {
|
||||
self.addr = "http://127.0.0.1:8080".to_string();
|
||||
self.client = reqwest::Client::new();
|
||||
|
|
|
@ -114,7 +114,7 @@ impl App {
|
|||
true => {
|
||||
players.remove(&player_id);
|
||||
println!("{:?}", players);
|
||||
Ok(Json(Message::new("server", MessageKind::DeletePlayer, "user deleted").expect("a")))
|
||||
Ok(Json(Message::new("server", MessageKind::DeletePlayer, "user deleted").expect("failed to create message")))
|
||||
},
|
||||
false => Err(String::from("player not found")),
|
||||
}
|
||||
|
|
161
src/ui.rs
161
src/ui.rs
|
@ -1,3 +1,4 @@
|
|||
use crossterm::event::KeyModifiers;
|
||||
use derive_setters::Setters;
|
||||
use ratatui::{
|
||||
layout::{
|
||||
|
@ -8,12 +9,29 @@ use ratatui::{
|
|||
},
|
||||
style::{Color, Style, Stylize},
|
||||
text::{Line, Span, Text},
|
||||
widgets::{Block, Borders, Clear, List, ListItem, ListState, Paragraph, Widget, Wrap},
|
||||
widgets::{
|
||||
Block,
|
||||
Borders,
|
||||
Clear,
|
||||
List,
|
||||
ListItem,
|
||||
ListState,
|
||||
Paragraph,
|
||||
Widget,
|
||||
Wrap
|
||||
},
|
||||
Frame,
|
||||
crossterm::event::{
|
||||
self,
|
||||
Event,
|
||||
KeyCode,
|
||||
KeyEvent,
|
||||
KeyEventKind
|
||||
}
|
||||
};
|
||||
use ratatui::crossterm::event::{self, Event, KeyCode, KeyEvent, KeyEventKind};
|
||||
use tui_textarea::{Key, Input, TextArea};
|
||||
|
||||
use crate::{client::Client, message::{Message, MessageKind}, player::Player};
|
||||
use crate::{client::Client, db::AppState, message::{Message, MessageKind}, player::Player};
|
||||
|
||||
const HELP: &str = r#"bindings:
|
||||
h - show help
|
||||
|
@ -23,6 +41,7 @@ q - quit
|
|||
3 - send player to server
|
||||
4 - send lobby to server"#;
|
||||
|
||||
// popup things
|
||||
#[derive(Debug, Default, Setters)]
|
||||
struct Popup<'a> {
|
||||
#[setters(into)]
|
||||
|
@ -34,6 +53,7 @@ struct Popup<'a> {
|
|||
style: Style,
|
||||
}
|
||||
|
||||
// popup things
|
||||
impl Widget for Popup<'_> {
|
||||
fn render(self, area: Rect, buf: &mut ratatui::prelude::Buffer)
|
||||
where
|
||||
|
@ -54,81 +74,96 @@ impl Widget for Popup<'_> {
|
|||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Default)]
|
||||
pub enum CurrentScreen {
|
||||
#[default] Menu,
|
||||
Lobbies,
|
||||
}
|
||||
|
||||
async fn handle_key_event(mut client: &mut Client, key_event: KeyEvent) {
|
||||
match &client.popup {
|
||||
false => {
|
||||
async fn handle_key_event(mut client: &mut Client<'_>, key_event: KeyEvent) {
|
||||
match &client.popup_user_input {
|
||||
true => {
|
||||
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.user == None {
|
||||
client.popup_title = "Error".to_string();
|
||||
client.popup_content = "lobby already_created or player not yet created".to_string();
|
||||
client.popup = true;
|
||||
} else {
|
||||
let addr = client.addr.clone();
|
||||
let path = "/create/lobby";
|
||||
let message = Message::new(
|
||||
"",
|
||||
MessageKind::CreateLobby,
|
||||
&client.user_name,
|
||||
).unwrap();
|
||||
|
||||
Client::send(&mut client, message, &addr, &path).await.unwrap();
|
||||
client.lobby_id = client.lobby.clone().unwrap();
|
||||
}
|
||||
KeyCode::Esc => client.popup_user_input = false,
|
||||
KeyCode::Char(c) => {
|
||||
todo!()
|
||||
},
|
||||
_ => (),
|
||||
}
|
||||
}
|
||||
true => {
|
||||
match key_event.code {
|
||||
_ => client.popup = false,
|
||||
},
|
||||
false => {
|
||||
match &client.popup {
|
||||
false => {
|
||||
match key_event.code {
|
||||
// quit
|
||||
KeyCode::Char('q') => Client::exit(client),
|
||||
KeyCode::Char('h') => {
|
||||
client.popup_title = "help".to_string();
|
||||
client.popup_content = HELP.to_string();
|
||||
client.popup = true;
|
||||
},
|
||||
// player creation
|
||||
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();
|
||||
}
|
||||
},
|
||||
// lobby creation
|
||||
KeyCode::Char('2') => {
|
||||
if client.lobby != None || client.user == None {
|
||||
client.popup_title = "Error".to_string();
|
||||
client.popup_content = "lobby already_created or player not yet created".to_string();
|
||||
client.popup = true;
|
||||
} else {
|
||||
let addr = client.addr.clone();
|
||||
let path = "/create/lobby";
|
||||
let message = Message::new(
|
||||
"",
|
||||
MessageKind::CreateLobby,
|
||||
&client.user_name,
|
||||
).unwrap();
|
||||
|
||||
Client::send(&mut client, message, &addr, &path).await.unwrap();
|
||||
client.lobby_id = client.lobby.clone().unwrap();
|
||||
}
|
||||
},
|
||||
// popup for lobby search
|
||||
KeyCode::Char('3') => {
|
||||
client.popup_user_input = true;
|
||||
}
|
||||
_ => (),
|
||||
}
|
||||
}
|
||||
true => {
|
||||
match key_event.code {
|
||||
_ => client.popup = false,
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn handle_events(client: &mut Client) -> anyhow::Result<()> {
|
||||
pub async fn handle_events(client: &mut Client<'_>) -> anyhow::Result<()> {
|
||||
match event::read()? {
|
||||
Event::Key(key) if key.kind == KeyEventKind::Press => {
|
||||
handle_key_event(client, key).await
|
||||
}
|
||||
_ => {}
|
||||
_ => ()
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn ui(frame: &mut Frame, app: &Client) {
|
||||
pub fn ui(frame: &mut Frame, app: &mut Client) {
|
||||
// main ui
|
||||
let chunks = Layout::default()
|
||||
.direction(Direction::Vertical)
|
||||
.constraints([
|
||||
|
@ -142,6 +177,8 @@ pub fn ui(frame: &mut Frame, app: &Client) {
|
|||
" create player ".into(),
|
||||
"2".blue().bold(),
|
||||
" create lobby ".into(),
|
||||
"3".blue().bold(),
|
||||
" search lobby ".into(),
|
||||
"<Q>".blue().bold(),
|
||||
" quit ".into(),
|
||||
]);
|
||||
|
@ -188,6 +225,7 @@ pub fn ui(frame: &mut Frame, app: &Client) {
|
|||
chunks[1],
|
||||
);
|
||||
|
||||
// popup things
|
||||
let popup_area = Rect {
|
||||
x: frame.area().width / 4,
|
||||
y: frame.area().height / 3,
|
||||
|
@ -204,4 +242,7 @@ pub fn ui(frame: &mut Frame, app: &Client) {
|
|||
.border_style(Style::new().red());
|
||||
frame.render_widget(popup, popup_area);
|
||||
}
|
||||
|
||||
if app.popup_user_input == true {
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue