second try at joining lobby

This commit is contained in:
clizia 2025-05-14 16:55:48 +02:00
parent 16343712d9
commit 72bd4ce74c
3 changed files with 114 additions and 65 deletions

View file

@ -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();

View file

@ -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")),
}

View file

@ -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,22 +74,29 @@ 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) {
async fn handle_key_event(mut client: &mut Client<'_>, key_event: KeyEvent) {
match &client.popup_user_input {
true => {
match key_event.code {
KeyCode::Esc => client.popup_user_input = false,
KeyCode::Char(c) => {
todo!()
},
_ => (),
}
},
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();
@ -88,6 +115,7 @@ async fn handle_key_event(mut client: &mut Client, key_event: KeyEvent) {
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();
@ -106,6 +134,10 @@ async fn handle_key_event(mut client: &mut Client, key_event: KeyEvent) {
client.lobby_id = client.lobby.clone().unwrap();
}
},
// popup for lobby search
KeyCode::Char('3') => {
client.popup_user_input = true;
}
_ => (),
}
}
@ -115,20 +147,23 @@ async fn handle_key_event(mut client: &mut Client, key_event: KeyEvent) {
}
}
}
}
}
}
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 {
}
}