lobby list skeleton
This commit is contained in:
parent
ed77971e41
commit
b37ab4a37d
8 changed files with 477 additions and 368 deletions
593
Cargo.lock
generated
593
Cargo.lock
generated
File diff suppressed because it is too large
Load diff
|
@ -18,8 +18,11 @@ RUSTFLAGS="--cfg tokio_unstable" cargo run --bin server
|
|||
|
||||
- [x] move player creation on the server
|
||||
- [x] move lobby creation on the server
|
||||
- [ ] add clap configuration
|
||||
- [ ] delete player
|
||||
- [ ] delete lobby
|
||||
- [ ] join existing lobbies
|
||||
- [ ] actually do the game
|
||||
- [ ] fixing ui (perpetual)
|
||||
- [ ] add unit testing
|
||||
- [ ] reorganize modules
|
||||
|
|
|
@ -1,12 +1,13 @@
|
|||
use anyhow::Result;
|
||||
use ratatui::DefaultTerminal;
|
||||
|
||||
use crate::{message::{Message, MessageKind}, player::Player, ui::ui};
|
||||
use crate::{message::{Message, MessageKind}, player::Player, ui::{ui, CurrentScreen}};
|
||||
|
||||
#[derive(Debug, Default)]
|
||||
pub struct Client {
|
||||
pub addr: String,
|
||||
pub client: reqwest::Client,
|
||||
pub current_screen: CurrentScreen,
|
||||
pub user: Option<String>,
|
||||
pub user_name: String,
|
||||
pub exit: bool,
|
||||
|
|
10
src/entity.rs
Normal file
10
src/entity.rs
Normal file
|
@ -0,0 +1,10 @@
|
|||
use serde::{Serialize, Deserialize};
|
||||
|
||||
#[derive(Serialize, Deserialize)]
|
||||
pub struct Entity<T: Serialize>(T);
|
||||
|
||||
impl<T: Serialize> Entity<T> {
|
||||
pub fn encode(&self) -> anyhow::Result<String, serde_json::Error> {
|
||||
serde_json::to_string(&self)
|
||||
}
|
||||
}
|
|
@ -17,6 +17,8 @@ pub enum MessageKind {
|
|||
CreateLobby,
|
||||
DeletePlayer,
|
||||
DeleteLobby,
|
||||
JoinLobby,
|
||||
QuitLobby,
|
||||
}
|
||||
|
||||
impl Message {
|
||||
|
|
|
@ -126,4 +126,11 @@ impl App {
|
|||
}
|
||||
|
||||
}
|
||||
|
||||
pub async fn join_lobby(
|
||||
State(db): State<AppState>,
|
||||
Path(lobby_id): Path<String>
|
||||
) -> Result<axum::response::Json<Message>, StatusCode> {
|
||||
todo!()
|
||||
}
|
||||
}
|
||||
|
|
|
@ -33,6 +33,7 @@ impl Server {
|
|||
.route("/create/lobby", post(App::create_lobby))
|
||||
.route("/delete/player/{player_id}", get(App::delete_player))
|
||||
.route("/delete/lobby/{lobby_id}", get(App::delete_lobby))
|
||||
.route("/join/{lobby_id}", post(App::join_lobby))
|
||||
.with_state(app.db);
|
||||
|
||||
axum::serve(listener, router).await.unwrap();
|
||||
|
|
226
src/ui.rs
226
src/ui.rs
|
@ -54,53 +54,64 @@ impl Widget for Popup<'_> {
|
|||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Default)]
|
||||
pub enum CurrentScreen {
|
||||
#[default] Menu,
|
||||
LobbyList,
|
||||
}
|
||||
|
||||
async fn handle_key_event(mut client: &mut Client, key_event: KeyEvent) {
|
||||
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();
|
||||
match client.current_screen {
|
||||
CurrentScreen::Menu => {
|
||||
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.popup_title = "Error".to_string();
|
||||
client.popup_content = "lobby already_created".to_string();
|
||||
client.popup = true;
|
||||
} else {
|
||||
let addr = client.addr.clone();
|
||||
let path = "/create/lobby";
|
||||
let message = Message::new(
|
||||
"",
|
||||
MessageKind::CreateLobby,
|
||||
""
|
||||
).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.popup_title = "Error".to_string();
|
||||
client.popup_content = "lobby already_created".to_string();
|
||||
client.popup = true;
|
||||
} else {
|
||||
let addr = client.addr.clone();
|
||||
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();
|
||||
Client::send(&mut client, message, &addr, &path).await.unwrap();
|
||||
client.lobby_id = client.lobby.clone().unwrap();
|
||||
}
|
||||
},
|
||||
_ => (),
|
||||
}
|
||||
},
|
||||
_ => (),
|
||||
}
|
||||
CurrentScreen::LobbyList => todo!(),
|
||||
}
|
||||
}
|
||||
true => {
|
||||
|
@ -123,66 +134,70 @@ pub async fn handle_events(client: &mut Client) -> anyhow::Result<()> {
|
|||
}
|
||||
|
||||
pub fn ui(frame: &mut Frame, app: &Client) {
|
||||
let chunks = Layout::default()
|
||||
.direction(Direction::Vertical)
|
||||
.constraints([
|
||||
Constraint::Min(3),
|
||||
Constraint::Min(1),
|
||||
])
|
||||
.split(frame.area());
|
||||
match app.current_screen {
|
||||
CurrentScreen::Menu => {
|
||||
|
||||
let instructions = Line::from(vec![
|
||||
" 1".blue().bold(),
|
||||
" create player ".into(),
|
||||
"2".blue().bold(),
|
||||
" create lobby ".into(),
|
||||
"3".blue().bold(),
|
||||
" send player ".into(),
|
||||
"<Q>".blue().bold(),
|
||||
" quit ".into(),
|
||||
]);
|
||||
let chunks = Layout::default()
|
||||
.direction(Direction::Vertical)
|
||||
.constraints([
|
||||
Constraint::Min(3),
|
||||
Constraint::Min(1),
|
||||
])
|
||||
.split(frame.area());
|
||||
|
||||
let infos = Text::from(vec![
|
||||
Line::from(vec![
|
||||
"connected to: ".into(),
|
||||
app.addr.clone().yellow(),
|
||||
]),
|
||||
Line::from(vec![
|
||||
"username: ".into(),
|
||||
app.user_name.clone().yellow(),
|
||||
]),
|
||||
Line::from(vec![
|
||||
"lobby: ".into(),
|
||||
app.lobby_id.clone().yellow(),
|
||||
]),
|
||||
]);
|
||||
let instructions = Line::from(vec![
|
||||
" 1".blue().bold(),
|
||||
" create player ".into(),
|
||||
"2".blue().bold(),
|
||||
" create lobby ".into(),
|
||||
"<Q>".blue().bold(),
|
||||
" quit ".into(),
|
||||
]);
|
||||
|
||||
let top_block_title = Line::from(" durak tui test fre ".bold());
|
||||
let top_block = Block::default()
|
||||
.title(top_block_title.centered())
|
||||
.borders(Borders::ALL)
|
||||
.style(Style::default());
|
||||
frame.render_widget(Paragraph::new(infos).block(top_block), chunks[0]);
|
||||
let infos = Text::from(vec![
|
||||
Line::from(vec![
|
||||
"connected to: ".into(),
|
||||
app.addr.clone().yellow(),
|
||||
]),
|
||||
Line::from(vec![
|
||||
"username: ".into(),
|
||||
app.user_name.clone().yellow(),
|
||||
]),
|
||||
Line::from(vec![
|
||||
"lobby: ".into(),
|
||||
app.lobby_id.clone().yellow(),
|
||||
]),
|
||||
]);
|
||||
|
||||
let server_message = Text::from(vec![
|
||||
Line::from(vec![
|
||||
"message: ".into(),
|
||||
app.response.clone().blue(),
|
||||
])
|
||||
]);
|
||||
let top_block_title = Line::from(" durak tui test fre ".bold());
|
||||
let top_block = Block::default()
|
||||
.title(top_block_title.centered())
|
||||
.borders(Borders::ALL)
|
||||
.style(Style::default());
|
||||
frame.render_widget(Paragraph::new(infos).block(top_block), chunks[0]);
|
||||
|
||||
let btm_block_title = Line::from(" messaggini carini :3 ".bold());
|
||||
let btm_block = Block::default()
|
||||
.title(btm_block_title.centered())
|
||||
.title_bottom(instructions.centered())
|
||||
.borders(Borders::ALL)
|
||||
.style(Style::default());
|
||||
frame.render_widget(
|
||||
Paragraph::new(server_message)
|
||||
.wrap(Wrap { trim: true, })
|
||||
.block(btm_block),
|
||||
chunks[1],
|
||||
);
|
||||
let server_message = Text::from(vec![
|
||||
Line::from(vec![
|
||||
"message: ".into(),
|
||||
app.response.clone().blue(),
|
||||
])
|
||||
]);
|
||||
|
||||
let btm_block_title = Line::from(" messaggini carini :3 ".bold());
|
||||
let btm_block = Block::default()
|
||||
.title(btm_block_title.centered())
|
||||
.title_bottom(instructions.centered())
|
||||
.borders(Borders::ALL)
|
||||
.style(Style::default());
|
||||
frame.render_widget(
|
||||
Paragraph::new(server_message)
|
||||
.wrap(Wrap { trim: true, })
|
||||
.block(btm_block),
|
||||
chunks[1],
|
||||
);
|
||||
}
|
||||
CurrentScreen::LobbyList => todo!(),
|
||||
}
|
||||
|
||||
let popup_area = Rect {
|
||||
x: frame.area().width / 4,
|
||||
|
@ -191,16 +206,13 @@ pub fn ui(frame: &mut Frame, app: &Client) {
|
|||
height: frame.area().height / 3,
|
||||
};
|
||||
|
||||
match &app.popup {
|
||||
true => {
|
||||
let popup = Popup::default()
|
||||
.title(app.popup_title.clone())
|
||||
.content(app.popup_content.clone())
|
||||
.style(Style::new().yellow())
|
||||
.title_style(Style::new().white().bold())
|
||||
.border_style(Style::new().red());
|
||||
frame.render_widget(popup, popup_area);
|
||||
},
|
||||
_ => ()
|
||||
if app.popup == true {
|
||||
let popup = Popup::default()
|
||||
.title(app.popup_title.clone())
|
||||
.content(app.popup_content.clone())
|
||||
.style(Style::new().yellow())
|
||||
.title_style(Style::new().white().bold())
|
||||
.border_style(Style::new().red());
|
||||
frame.render_widget(popup, popup_area);
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue