Compare commits
No commits in common. "e6968b6c8dd2661a37dcb1c0b2684183f783d5ad" and "b37ab4a37daf31f06dc9cc02a7077492b81cf5eb" have entirely different histories.
e6968b6c8d
...
b37ab4a37d
5 changed files with 126 additions and 131 deletions
|
@ -1,12 +1,13 @@
|
||||||
use anyhow::Result;
|
use anyhow::Result;
|
||||||
use ratatui::{widgets::ListState, DefaultTerminal};
|
use ratatui::DefaultTerminal;
|
||||||
|
|
||||||
use crate::{lobby::Lobby, message::{Message, MessageKind}, player::Player, ui::{ui, CurrentScreen}};
|
use crate::{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,
|
||||||
|
|
|
@ -23,7 +23,7 @@ impl Lobby {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn join_lobby(&mut self, player: Player) {
|
pub fn join_lobby(mut self, player: Player) {
|
||||||
self.players.push(player);
|
self.players.push(player);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -19,7 +19,6 @@ pub enum MessageKind {
|
||||||
DeleteLobby,
|
DeleteLobby,
|
||||||
JoinLobby,
|
JoinLobby,
|
||||||
QuitLobby,
|
QuitLobby,
|
||||||
GetLobbies,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Message {
|
impl Message {
|
||||||
|
|
|
@ -34,7 +34,7 @@ impl App {
|
||||||
pub async fn create_player(
|
pub async fn create_player(
|
||||||
State(db): State<AppState>,
|
State(db): State<AppState>,
|
||||||
json_body: Result<Json<Message>, JsonRejection>,
|
json_body: Result<Json<Message>, JsonRejection>,
|
||||||
) -> Result<axum::Json<Message>, String> {
|
) -> Result<axum::Json<Message>, StatusCode> {
|
||||||
match json_body {
|
match json_body {
|
||||||
Ok(Json(message)) => {
|
Ok(Json(message)) => {
|
||||||
println!("{:?}", message);
|
println!("{:?}", message);
|
||||||
|
@ -44,7 +44,7 @@ impl App {
|
||||||
.lock().expect("ciao");
|
.lock().expect("ciao");
|
||||||
|
|
||||||
if players.contains_key(&player.name) {
|
if players.contains_key(&player.name) {
|
||||||
return Err(String::from("player already in db"))
|
return Err(StatusCode::UNAVAILABLE_FOR_LEGAL_REASONS)
|
||||||
}
|
}
|
||||||
|
|
||||||
players.insert(
|
players.insert(
|
||||||
|
@ -55,57 +55,42 @@ impl App {
|
||||||
|
|
||||||
Ok(Json(Message::new("server", MessageKind::CreatePlayer, &player.name).expect("failed creating message")))
|
Ok(Json(Message::new("server", MessageKind::CreatePlayer, &player.name).expect("failed creating message")))
|
||||||
}
|
}
|
||||||
Err(_) => Err(String::from("failed parsing json message"))
|
Err(_) => Err(StatusCode::UNPROCESSABLE_ENTITY)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn create_lobby(
|
pub async fn create_lobby(
|
||||||
State(db): State<AppState>,
|
State(db): State<AppState>,
|
||||||
json_body: Result<Json<Message>, JsonRejection>,
|
json_body: Result<Json<Message>, JsonRejection>,
|
||||||
) -> Result<axum::Json<Message>, String> {
|
) -> Result<axum::Json<Message>, StatusCode> {
|
||||||
match json_body {
|
match json_body {
|
||||||
Ok(Json(message)) => {
|
Ok(Json(message)) => {
|
||||||
println!("{:?}", message);
|
println!("{:?}", message);
|
||||||
let mut lobby: Lobby = Lobby::new();
|
let lobby: Lobby = Lobby::new();
|
||||||
|
|
||||||
let mut lobbies = db.lobbies
|
let mut lobbies = db.lobbies
|
||||||
.lock()
|
.lock().expect("ciao");
|
||||||
.expect("failed to lock lobbies db");
|
|
||||||
|
|
||||||
if lobbies.contains_key(&lobby.id) {
|
if lobbies.contains_key(&lobby.id) {
|
||||||
return Err(String::from("lobby already in db"))
|
return Err(StatusCode::UNAVAILABLE_FOR_LEGAL_REASONS)
|
||||||
}
|
}
|
||||||
|
|
||||||
let players = db.players
|
|
||||||
.lock()
|
|
||||||
.expect("failed to lock player db");
|
|
||||||
|
|
||||||
let player_bytes = players.get(&message.content);
|
|
||||||
|
|
||||||
let player: Player = if let None = player_bytes {
|
|
||||||
return Err(String::from("player not found or not yet created"));
|
|
||||||
} else {
|
|
||||||
Player::from(serde_json::from_slice(player_bytes.unwrap()).expect("failed"))
|
|
||||||
};
|
|
||||||
|
|
||||||
lobby.join_lobby(player);
|
|
||||||
|
|
||||||
lobbies.insert(
|
lobbies.insert(
|
||||||
lobby.get_id(),
|
lobby.get_id(),
|
||||||
Bytes::copy_from_slice(lobby.encode().expect("failed copying lobby from bytes slice").as_ref())
|
Bytes::copy_from_slice(lobby.encode().expect(&StatusCode::EXPECTATION_FAILED.to_string()).as_ref())
|
||||||
);
|
);
|
||||||
println!("{:?}", lobbies);
|
println!("{:?}", lobbies);
|
||||||
|
|
||||||
Ok(Json(Message::new("server", MessageKind::CreateLobby, &lobby.id).expect("failed creating message")))
|
Ok(Json(Message::new("server", MessageKind::CreateLobby, &lobby.id).expect("failed creating message")))
|
||||||
},
|
},
|
||||||
Err(_) => Err(String::from("failed to parse json message")),
|
Err(_) => Err(StatusCode::UNPROCESSABLE_ENTITY),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn delete_player(
|
pub async fn delete_player(
|
||||||
State(db): State<AppState>,
|
State(db): State<AppState>,
|
||||||
Path(player_id): Path<String>
|
Path(player_id): Path<String>
|
||||||
) -> Result<axum::response::Json<Message>, String> {
|
) -> Result<axum::response::Json<Message>, StatusCode> {
|
||||||
{
|
{
|
||||||
let mut players = db.players
|
let mut players = db.players
|
||||||
.lock().expect("ciao");
|
.lock().expect("ciao");
|
||||||
|
@ -116,7 +101,7 @@ impl App {
|
||||||
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("a")))
|
||||||
},
|
},
|
||||||
false => Err(String::from("player not found")),
|
false => Err(StatusCode::NOT_FOUND),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -125,7 +110,7 @@ impl App {
|
||||||
pub async fn delete_lobby(
|
pub async fn delete_lobby(
|
||||||
State(db): State<AppState>,
|
State(db): State<AppState>,
|
||||||
Path(lobby_id): Path<String>
|
Path(lobby_id): Path<String>
|
||||||
) -> Result<axum::response::Json<Message>, String> {
|
) -> Result<axum::response::Json<Message>, StatusCode> {
|
||||||
{
|
{
|
||||||
let mut lobbies = db.lobbies
|
let mut lobbies = db.lobbies
|
||||||
.lock().expect("ciao");
|
.lock().expect("ciao");
|
||||||
|
@ -136,7 +121,7 @@ impl App {
|
||||||
println!("{:?}", lobbies);
|
println!("{:?}", lobbies);
|
||||||
Ok(Json(Message::new("server", MessageKind::DeleteLobby, "lobby deleted").expect("a")))
|
Ok(Json(Message::new("server", MessageKind::DeleteLobby, "lobby deleted").expect("a")))
|
||||||
},
|
},
|
||||||
false => Err(String::from("lobby not found in db")),
|
false => Err(StatusCode::NOT_FOUND),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -145,8 +130,7 @@ impl App {
|
||||||
pub async fn join_lobby(
|
pub async fn join_lobby(
|
||||||
State(db): State<AppState>,
|
State(db): State<AppState>,
|
||||||
Path(lobby_id): Path<String>
|
Path(lobby_id): Path<String>
|
||||||
) -> Result<axum::response::Json<Message>, String> {
|
) -> Result<axum::response::Json<Message>, StatusCode> {
|
||||||
todo!()
|
todo!()
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
203
src/ui.rs
203
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, ListState, Paragraph, Widget, Wrap},
|
widgets::{Block, Borders, Clear, List, ListItem, Paragraph, Widget, Wrap},
|
||||||
Frame,
|
Frame,
|
||||||
};
|
};
|
||||||
use ratatui::crossterm::event::{self, Event, KeyCode, KeyEvent, KeyEventKind};
|
use ratatui::crossterm::event::{self, Event, KeyCode, KeyEvent, KeyEventKind};
|
||||||
|
@ -57,56 +57,61 @@ impl Widget for Popup<'_> {
|
||||||
#[derive(Debug, Default)]
|
#[derive(Debug, Default)]
|
||||||
pub enum CurrentScreen {
|
pub enum CurrentScreen {
|
||||||
#[default] Menu,
|
#[default] Menu,
|
||||||
Lobbies,
|
LobbyList,
|
||||||
}
|
}
|
||||||
|
|
||||||
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 key_event.code {
|
match client.current_screen {
|
||||||
KeyCode::Char('q') => Client::exit(client),
|
CurrentScreen::Menu => {
|
||||||
KeyCode::Char('h') => {
|
match key_event.code {
|
||||||
client.popup_title = "help".to_string();
|
KeyCode::Char('q') => Client::exit(client),
|
||||||
client.popup_content = HELP.to_string();
|
KeyCode::Char('h') => {
|
||||||
client.popup = true;
|
client.popup_title = "help".to_string();
|
||||||
},
|
client.popup_content = HELP.to_string();
|
||||||
KeyCode::Char('1') => {
|
client.popup = true;
|
||||||
if client.user != None {
|
},
|
||||||
client.popup_title = "Error".to_string();
|
KeyCode::Char('1') => {
|
||||||
client.popup_content = "user already_created".to_string();
|
if client.user != None {
|
||||||
client.popup = true;
|
client.popup_title = "Error".to_string();
|
||||||
} else {
|
client.popup_content = "user already_created".to_string();
|
||||||
let addr = client.addr.clone();
|
client.popup = true;
|
||||||
let path = "/create/player";
|
} else {
|
||||||
let message = Message::new(
|
let addr = client.addr.clone();
|
||||||
"",
|
let path = "/create/player";
|
||||||
MessageKind::CreatePlayer,
|
let message = Message::new(
|
||||||
""
|
"",
|
||||||
).unwrap();
|
MessageKind::CreatePlayer,
|
||||||
|
""
|
||||||
|
).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') => {
|
KeyCode::Char('2') => {
|
||||||
if client.lobby != None || client.user == None {
|
if client.lobby != None {
|
||||||
client.popup_title = "Error".to_string();
|
client.popup_title = "Error".to_string();
|
||||||
client.popup_content = "lobby already_created or player not yet created".to_string();
|
client.popup_content = "lobby already_created".to_string();
|
||||||
client.popup = true;
|
client.popup = true;
|
||||||
} else {
|
} else {
|
||||||
let addr = client.addr.clone();
|
let addr = client.addr.clone();
|
||||||
let path = "/create/lobby";
|
let path = "/create/lobby";
|
||||||
let message = Message::new(
|
let message = Message::new(
|
||||||
"",
|
"",
|
||||||
MessageKind::CreateLobby,
|
MessageKind::CreateLobby,
|
||||||
&client.user_name,
|
""
|
||||||
).unwrap();
|
).unwrap();
|
||||||
|
|
||||||
Client::send(&mut client, message, &addr, &path).await.unwrap();
|
Client::send(&mut client, message, &addr, &path).await.unwrap();
|
||||||
client.lobby_id = client.lobby.clone().unwrap();
|
client.lobby_id = client.lobby.clone().unwrap();
|
||||||
|
}
|
||||||
|
},
|
||||||
|
_ => (),
|
||||||
}
|
}
|
||||||
},
|
}
|
||||||
_ => (),
|
CurrentScreen::LobbyList => todo!(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
true => {
|
true => {
|
||||||
|
@ -129,64 +134,70 @@ 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) {
|
||||||
let chunks = Layout::default()
|
match app.current_screen {
|
||||||
.direction(Direction::Vertical)
|
CurrentScreen::Menu => {
|
||||||
.constraints([
|
|
||||||
Constraint::Min(3),
|
let chunks = Layout::default()
|
||||||
Constraint::Min(1),
|
.direction(Direction::Vertical)
|
||||||
])
|
.constraints([
|
||||||
.split(frame.area());
|
Constraint::Min(3),
|
||||||
|
Constraint::Min(1),
|
||||||
|
])
|
||||||
|
.split(frame.area());
|
||||||
|
|
||||||
let instructions = Line::from(vec![
|
let instructions = Line::from(vec![
|
||||||
" 1".blue().bold(),
|
" 1".blue().bold(),
|
||||||
" create player ".into(),
|
" create player ".into(),
|
||||||
"2".blue().bold(),
|
"2".blue().bold(),
|
||||||
" create lobby ".into(),
|
" create lobby ".into(),
|
||||||
"<Q>".blue().bold(),
|
"<Q>".blue().bold(),
|
||||||
" quit ".into(),
|
" quit ".into(),
|
||||||
]);
|
]);
|
||||||
|
|
||||||
let infos = Text::from(vec![
|
let infos = Text::from(vec![
|
||||||
Line::from(vec![
|
Line::from(vec![
|
||||||
"connected to: ".into(),
|
"connected to: ".into(),
|
||||||
app.addr.clone().yellow(),
|
app.addr.clone().yellow(),
|
||||||
]),
|
]),
|
||||||
Line::from(vec![
|
Line::from(vec![
|
||||||
"username: ".into(),
|
"username: ".into(),
|
||||||
app.user_name.clone().yellow(),
|
app.user_name.clone().yellow(),
|
||||||
]),
|
]),
|
||||||
Line::from(vec![
|
Line::from(vec![
|
||||||
"lobby: ".into(),
|
"lobby: ".into(),
|
||||||
app.lobby_id.clone().yellow(),
|
app.lobby_id.clone().yellow(),
|
||||||
]),
|
]),
|
||||||
]);
|
]);
|
||||||
|
|
||||||
let top_block_title = Line::from(" durak tui test fre ".bold());
|
let top_block_title = Line::from(" durak tui test fre ".bold());
|
||||||
let top_block = Block::default()
|
let top_block = Block::default()
|
||||||
.title(top_block_title.centered())
|
.title(top_block_title.centered())
|
||||||
.borders(Borders::ALL)
|
.borders(Borders::ALL)
|
||||||
.style(Style::default());
|
.style(Style::default());
|
||||||
frame.render_widget(Paragraph::new(infos).block(top_block), chunks[0]);
|
frame.render_widget(Paragraph::new(infos).block(top_block), chunks[0]);
|
||||||
|
|
||||||
let server_message = Text::from(vec![
|
let server_message = Text::from(vec![
|
||||||
Line::from(vec![
|
Line::from(vec![
|
||||||
"message: ".into(),
|
"message: ".into(),
|
||||||
app.response.clone().blue(),
|
app.response.clone().blue(),
|
||||||
])
|
])
|
||||||
]);
|
]);
|
||||||
|
|
||||||
let btm_block_title = Line::from(" messaggini carini :3 ".bold());
|
let btm_block_title = Line::from(" messaggini carini :3 ".bold());
|
||||||
let btm_block = Block::default()
|
let btm_block = Block::default()
|
||||||
.title(btm_block_title.centered())
|
.title(btm_block_title.centered())
|
||||||
.title_bottom(instructions.centered())
|
.title_bottom(instructions.centered())
|
||||||
.borders(Borders::ALL)
|
.borders(Borders::ALL)
|
||||||
.style(Style::default());
|
.style(Style::default());
|
||||||
frame.render_widget(
|
frame.render_widget(
|
||||||
Paragraph::new(server_message)
|
Paragraph::new(server_message)
|
||||||
.wrap(Wrap { trim: true, })
|
.wrap(Wrap { trim: true, })
|
||||||
.block(btm_block),
|
.block(btm_block),
|
||||||
chunks[1],
|
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