refactored things
This commit is contained in:
parent
37c996fc24
commit
686dbaac0f
6 changed files with 78 additions and 54 deletions
12
Cargo.lock
generated
12
Cargo.lock
generated
|
@ -577,6 +577,7 @@ dependencies = [
|
||||||
"tower 0.5.2",
|
"tower 0.5.2",
|
||||||
"tracing",
|
"tracing",
|
||||||
"tracing-subscriber",
|
"tracing-subscriber",
|
||||||
|
"tui-textarea",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
|
@ -2459,6 +2460,17 @@ version = "0.2.5"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "e421abadd41a4225275504ea4d6566923418b7f05506fbc9c0fe86ba7396114b"
|
checksum = "e421abadd41a4225275504ea4d6566923418b7f05506fbc9c0fe86ba7396114b"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "tui-textarea"
|
||||||
|
version = "0.7.0"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "0a5318dd619ed73c52a9417ad19046724effc1287fb75cdcc4eca1d6ac1acbae"
|
||||||
|
dependencies = [
|
||||||
|
"crossterm",
|
||||||
|
"ratatui",
|
||||||
|
"unicode-width 0.2.0",
|
||||||
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "typenum"
|
name = "typenum"
|
||||||
version = "1.17.0"
|
version = "1.17.0"
|
||||||
|
|
|
@ -18,7 +18,7 @@ path = "src/bin/client.rs"
|
||||||
[dependencies]
|
[dependencies]
|
||||||
axum = "0.8.1"
|
axum = "0.8.1"
|
||||||
tower = "0.5.2"
|
tower = "0.5.2"
|
||||||
reqwest = "0.12.0"
|
reqwest = {version = "0.12.0", features = ["json"]}
|
||||||
hyper = { version = "1", features = ["full"] }
|
hyper = { version = "1", features = ["full"] }
|
||||||
http-body-util = "0.1"
|
http-body-util = "0.1"
|
||||||
hyper-util = { version = "0.1", features = ["full"] }
|
hyper-util = { version = "0.1", features = ["full"] }
|
||||||
|
@ -36,3 +36,4 @@ tracing = "0.1.41"
|
||||||
tracing-subscriber = "0.3.19"
|
tracing-subscriber = "0.3.19"
|
||||||
ratatui = "0.29.0"
|
ratatui = "0.29.0"
|
||||||
derive_setters = "0.1.6"
|
derive_setters = "0.1.6"
|
||||||
|
tui-textarea = "0.7.0"
|
||||||
|
|
|
@ -1,19 +1,20 @@
|
||||||
|
use anyhow::Result;
|
||||||
use ratatui::DefaultTerminal;
|
use ratatui::DefaultTerminal;
|
||||||
|
|
||||||
use crate::{lobby::Lobby, message::{Message, MessageKind}, player::Player, ui::ui};
|
use crate::{message::{Message, MessageKind}, player::Player, ui::ui};
|
||||||
|
|
||||||
#[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 user: Option<Player>,
|
pub user: Option<String>,
|
||||||
pub name: String,
|
pub user_name: String,
|
||||||
pub exit: bool,
|
pub exit: 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<Lobby>,
|
pub lobby: Option<String>,
|
||||||
pub lobby_id: String,
|
pub lobby_id: String,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -34,24 +35,25 @@ impl Client {
|
||||||
self.exit = true;
|
self.exit = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn send_player(&mut self, player: &Player, addr: &str) -> anyhow::Result<()> {
|
pub async fn send(&mut self, message: Message, addr: &str, path: &str) -> anyhow::Result<()> {
|
||||||
let player_message = Message::new(
|
|
||||||
&player.get_name(),
|
|
||||||
MessageKind::CreatePlayer,
|
|
||||||
player.encode()?
|
|
||||||
)?;
|
|
||||||
|
|
||||||
let response = self.client
|
let response = self.client
|
||||||
.post([&addr, "/create/player"].concat())
|
.post([&addr, path].concat())
|
||||||
.header("Content-Type", "application/json")
|
.header("Content-Type", "application/json")
|
||||||
.body(player_message.encode()?)
|
.body(message.encode()?)
|
||||||
.send().await;
|
.send().await;
|
||||||
|
|
||||||
if let Err(e) = &response {
|
if let Err(e) = &response {
|
||||||
self.response = e.to_string();
|
self.response = e.to_string();
|
||||||
return Ok(())
|
return Ok(())
|
||||||
} else {
|
} else {
|
||||||
self.response = format!("{:?}", response?);
|
let response: Message = response?.json().await?;
|
||||||
|
self.response = format!("{:?}", &response);
|
||||||
|
|
||||||
|
match response.message_kind {
|
||||||
|
MessageKind::CreatePlayer => self.user = Some(response.content),
|
||||||
|
MessageKind::CreateLobby => self.lobby = Some(response.content),
|
||||||
|
_ => ()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
|
|
|
@ -10,3 +10,4 @@ pub mod constant;
|
||||||
pub mod db;
|
pub mod db;
|
||||||
pub mod router;
|
pub mod router;
|
||||||
pub mod ui;
|
pub mod ui;
|
||||||
|
pub mod entity;
|
||||||
|
|
|
@ -38,23 +38,22 @@ impl App {
|
||||||
match json_body {
|
match json_body {
|
||||||
Ok(Json(message)) => {
|
Ok(Json(message)) => {
|
||||||
println!("{:?}", message);
|
println!("{:?}", message);
|
||||||
let player: Player = serde_json::from_str(&message.content).expect(&StatusCode::PRECONDITION_FAILED.to_string());
|
let player: Player = Player::new().unwrap();
|
||||||
{
|
|
||||||
let mut players = db.players
|
|
||||||
.lock().expect("ciao");
|
|
||||||
|
|
||||||
if players.contains_key(&player.name) {
|
let mut players = db.players
|
||||||
return Err(StatusCode::UNAVAILABLE_FOR_LEGAL_REASONS)
|
.lock().expect("ciao");
|
||||||
}
|
|
||||||
|
|
||||||
players.insert(
|
if players.contains_key(&player.name) {
|
||||||
player.get_name(),
|
return Err(StatusCode::UNAVAILABLE_FOR_LEGAL_REASONS)
|
||||||
Bytes::copy_from_slice(player.encode().expect(&StatusCode::EXPECTATION_FAILED.to_string()).as_ref())
|
|
||||||
);
|
|
||||||
println!("{:?}", db.players);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
Ok(Json(Message::new("server", MessageKind::CreatePlayer, "user created").expect("failed creating message")))
|
players.insert(
|
||||||
|
player.get_name(),
|
||||||
|
Bytes::copy_from_slice(player.encode().expect(&StatusCode::EXPECTATION_FAILED.to_string()).as_ref())
|
||||||
|
);
|
||||||
|
println!("{:?}", db.players);
|
||||||
|
|
||||||
|
Ok(Json(Message::new("server", MessageKind::CreatePlayer, &player.name).expect("failed creating message")))
|
||||||
}
|
}
|
||||||
Err(_) => Err(StatusCode::UNPROCESSABLE_ENTITY)
|
Err(_) => Err(StatusCode::UNPROCESSABLE_ENTITY)
|
||||||
}
|
}
|
||||||
|
@ -67,23 +66,22 @@ impl App {
|
||||||
match json_body {
|
match json_body {
|
||||||
Ok(Json(message)) => {
|
Ok(Json(message)) => {
|
||||||
println!("{:?}", message);
|
println!("{:?}", message);
|
||||||
let lobby: Lobby = serde_json::from_str(&message.content).expect(&StatusCode::PRECONDITION_FAILED.to_string());
|
let lobby: Lobby = Lobby::new();
|
||||||
{
|
|
||||||
let mut lobbies = db.lobbies
|
|
||||||
.lock().expect("ciao");
|
|
||||||
|
|
||||||
if lobbies.contains_key(&lobby.id) {
|
let mut lobbies = db.lobbies
|
||||||
return Err(StatusCode::UNAVAILABLE_FOR_LEGAL_REASONS)
|
.lock().expect("ciao");
|
||||||
}
|
|
||||||
|
|
||||||
lobbies.insert(
|
if lobbies.contains_key(&lobby.id) {
|
||||||
lobby.get_id(),
|
return Err(StatusCode::UNAVAILABLE_FOR_LEGAL_REASONS)
|
||||||
Bytes::copy_from_slice(lobby.encode().expect(&StatusCode::EXPECTATION_FAILED.to_string()).as_ref())
|
|
||||||
);
|
|
||||||
println!("{:?}", lobbies);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
Ok(Json(Message::new("server", MessageKind::CreateLobby, "lobby created").expect("failed creating message")))
|
lobbies.insert(
|
||||||
|
lobby.get_id(),
|
||||||
|
Bytes::copy_from_slice(lobby.encode().expect(&StatusCode::EXPECTATION_FAILED.to_string()).as_ref())
|
||||||
|
);
|
||||||
|
println!("{:?}", lobbies);
|
||||||
|
|
||||||
|
Ok(Json(Message::new("server", MessageKind::CreateLobby, &lobby.id).expect("failed creating message")))
|
||||||
},
|
},
|
||||||
Err(_) => Err(StatusCode::UNPROCESSABLE_ENTITY),
|
Err(_) => Err(StatusCode::UNPROCESSABLE_ENTITY),
|
||||||
}
|
}
|
||||||
|
|
36
src/ui.rs
36
src/ui.rs
|
@ -13,7 +13,7 @@ use ratatui::{
|
||||||
};
|
};
|
||||||
use ratatui::crossterm::event::{self, Event, KeyCode, KeyEvent, KeyEventKind};
|
use ratatui::crossterm::event::{self, Event, KeyCode, KeyEvent, KeyEventKind};
|
||||||
|
|
||||||
use crate::{client::Client, player::Player};
|
use crate::{client::Client, message::{Message, MessageKind}, player::Player};
|
||||||
|
|
||||||
const HELP: &str = r#"bindings:
|
const HELP: &str = r#"bindings:
|
||||||
h - show help
|
h - show help
|
||||||
|
@ -70,8 +70,16 @@ async fn handle_key_event(mut client: &mut Client, key_event: KeyEvent) {
|
||||||
client.popup_content = "user already_created".to_string();
|
client.popup_content = "user already_created".to_string();
|
||||||
client.popup = true;
|
client.popup = true;
|
||||||
} else {
|
} else {
|
||||||
client.user = Some(Player::new().expect("toto"));
|
let addr = client.addr.clone();
|
||||||
client.name = client.user.clone().unwrap().name;
|
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') => {
|
KeyCode::Char('2') => {
|
||||||
|
@ -80,16 +88,18 @@ async fn handle_key_event(mut client: &mut Client, key_event: KeyEvent) {
|
||||||
client.popup_content = "lobby already_created".to_string();
|
client.popup_content = "lobby already_created".to_string();
|
||||||
client.popup = true;
|
client.popup = true;
|
||||||
} else {
|
} else {
|
||||||
client.user = Some(Player::new().expect("toto"));
|
let addr = client.addr.clone();
|
||||||
client.name = client.user.clone().unwrap().name;
|
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();
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
KeyCode::Char('3') => {
|
|
||||||
if let Some(player) = client.user.clone() {
|
|
||||||
let addr = client.addr.clone();
|
|
||||||
Client::send_player(&mut client, &player, &addr).await.unwrap();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
_ => (),
|
_ => (),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -107,7 +117,7 @@ pub async fn handle_events(client: &mut Client) -> anyhow::Result<()> {
|
||||||
handle_key_event(client, key).await
|
handle_key_event(client, key).await
|
||||||
}
|
}
|
||||||
_ => {}
|
_ => {}
|
||||||
};
|
}
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
@ -139,7 +149,7 @@ pub fn ui(frame: &mut Frame, app: &Client) {
|
||||||
]),
|
]),
|
||||||
Line::from(vec![
|
Line::from(vec![
|
||||||
"username: ".into(),
|
"username: ".into(),
|
||||||
app.name.clone().yellow(),
|
app.user_name.clone().yellow(),
|
||||||
]),
|
]),
|
||||||
Line::from(vec![
|
Line::from(vec![
|
||||||
"lobby: ".into(),
|
"lobby: ".into(),
|
||||||
|
|
Loading…
Add table
Reference in a new issue