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 anyhow::Result;
|
||||||
use ratatui::{widgets::ListState, DefaultTerminal};
|
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)]
|
#[derive(Debug, Default)]
|
||||||
pub struct Client {
|
pub struct Client<'a> {
|
||||||
pub addr: String,
|
pub addr: String,
|
||||||
pub client: reqwest::Client,
|
pub client: reqwest::Client,
|
||||||
pub user: Option<String>,
|
pub user: Option<String>,
|
||||||
pub user_name: String,
|
pub user_name: String,
|
||||||
pub exit: bool,
|
pub exit: bool,
|
||||||
|
pub popup_user_input: bool,
|
||||||
pub popup: bool,
|
pub popup: bool,
|
||||||
pub popup_title: String,
|
pub popup_title: String,
|
||||||
pub popup_content: String,
|
pub popup_content: String,
|
||||||
pub response: String,
|
pub response: String,
|
||||||
pub lobby: Option<String>,
|
pub lobby: Option<String>,
|
||||||
pub lobby_id: String,
|
pub lobby_id: String,
|
||||||
|
pub textarea: TextArea<'a>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Client {
|
impl Client<'_> {
|
||||||
pub async fn run(&mut self, terminal: &mut DefaultTerminal) -> anyhow::Result<()> {
|
pub async fn run(&mut self, terminal: &mut DefaultTerminal) -> anyhow::Result<()> {
|
||||||
self.addr = "http://127.0.0.1:8080".to_string();
|
self.addr = "http://127.0.0.1:8080".to_string();
|
||||||
self.client = reqwest::Client::new();
|
self.client = reqwest::Client::new();
|
||||||
|
|
|
@ -114,7 +114,7 @@ impl App {
|
||||||
true => {
|
true => {
|
||||||
players.remove(&player_id);
|
players.remove(&player_id);
|
||||||
println!("{:?}", players);
|
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")),
|
false => Err(String::from("player not found")),
|
||||||
}
|
}
|
||||||
|
|
163
src/ui.rs
163
src/ui.rs
|
@ -1,3 +1,4 @@
|
||||||
|
use crossterm::event::KeyModifiers;
|
||||||
use derive_setters::Setters;
|
use derive_setters::Setters;
|
||||||
use ratatui::{
|
use ratatui::{
|
||||||
layout::{
|
layout::{
|
||||||
|
@ -8,12 +9,29 @@ use ratatui::{
|
||||||
},
|
},
|
||||||
style::{Color, Style, Stylize},
|
style::{Color, Style, Stylize},
|
||||||
text::{Line, Span, Text},
|
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,
|
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:
|
const HELP: &str = r#"bindings:
|
||||||
h - show help
|
h - show help
|
||||||
|
@ -23,6 +41,7 @@ q - quit
|
||||||
3 - send player to server
|
3 - send player to server
|
||||||
4 - send lobby to server"#;
|
4 - send lobby to server"#;
|
||||||
|
|
||||||
|
// popup things
|
||||||
#[derive(Debug, Default, Setters)]
|
#[derive(Debug, Default, Setters)]
|
||||||
struct Popup<'a> {
|
struct Popup<'a> {
|
||||||
#[setters(into)]
|
#[setters(into)]
|
||||||
|
@ -34,6 +53,7 @@ struct Popup<'a> {
|
||||||
style: Style,
|
style: Style,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// popup things
|
||||||
impl Widget for Popup<'_> {
|
impl Widget for Popup<'_> {
|
||||||
fn render(self, area: Rect, buf: &mut ratatui::prelude::Buffer)
|
fn render(self, area: Rect, buf: &mut ratatui::prelude::Buffer)
|
||||||
where
|
where
|
||||||
|
@ -54,81 +74,96 @@ impl Widget for Popup<'_> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Default)]
|
async fn handle_key_event(mut client: &mut Client<'_>, key_event: KeyEvent) {
|
||||||
pub enum CurrentScreen {
|
match &client.popup_user_input {
|
||||||
#[default] Menu,
|
true => {
|
||||||
Lobbies,
|
match key_event.code {
|
||||||
}
|
KeyCode::Esc => client.popup_user_input = false,
|
||||||
|
KeyCode::Char(c) => {
|
||||||
async fn handle_key_event(mut client: &mut Client, key_event: KeyEvent) {
|
todo!()
|
||||||
match &client.popup {
|
|
||||||
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.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();
|
|
||||||
}
|
|
||||||
},
|
},
|
||||||
_ => (),
|
_ => (),
|
||||||
}
|
}
|
||||||
}
|
},
|
||||||
true => {
|
false => {
|
||||||
match key_event.code {
|
match &client.popup {
|
||||||
_ => client.popup = false,
|
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()? {
|
match event::read()? {
|
||||||
Event::Key(key) if key.kind == KeyEventKind::Press => {
|
Event::Key(key) if key.kind == KeyEventKind::Press => {
|
||||||
handle_key_event(client, key).await
|
handle_key_event(client, key).await
|
||||||
}
|
}
|
||||||
_ => {}
|
_ => ()
|
||||||
}
|
}
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn ui(frame: &mut Frame, app: &Client) {
|
pub fn ui(frame: &mut Frame, app: &mut Client) {
|
||||||
|
// main ui
|
||||||
let chunks = Layout::default()
|
let chunks = Layout::default()
|
||||||
.direction(Direction::Vertical)
|
.direction(Direction::Vertical)
|
||||||
.constraints([
|
.constraints([
|
||||||
|
@ -142,6 +177,8 @@ pub fn ui(frame: &mut Frame, app: &Client) {
|
||||||
" create player ".into(),
|
" create player ".into(),
|
||||||
"2".blue().bold(),
|
"2".blue().bold(),
|
||||||
" create lobby ".into(),
|
" create lobby ".into(),
|
||||||
|
"3".blue().bold(),
|
||||||
|
" search lobby ".into(),
|
||||||
"<Q>".blue().bold(),
|
"<Q>".blue().bold(),
|
||||||
" quit ".into(),
|
" quit ".into(),
|
||||||
]);
|
]);
|
||||||
|
@ -188,6 +225,7 @@ pub fn ui(frame: &mut Frame, app: &Client) {
|
||||||
chunks[1],
|
chunks[1],
|
||||||
);
|
);
|
||||||
|
|
||||||
|
// popup things
|
||||||
let popup_area = Rect {
|
let popup_area = Rect {
|
||||||
x: frame.area().width / 4,
|
x: frame.area().width / 4,
|
||||||
y: frame.area().height / 3,
|
y: frame.area().height / 3,
|
||||||
|
@ -204,4 +242,7 @@ pub fn ui(frame: &mut Frame, app: &Client) {
|
||||||
.border_style(Style::new().red());
|
.border_style(Style::new().red());
|
||||||
frame.render_widget(popup, popup_area);
|
frame.render_widget(popup, popup_area);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if app.popup_user_input == true {
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue