un po di lavoro sul server
This commit is contained in:
parent
c1e724a9fc
commit
9c19dfb08d
5 changed files with 65 additions and 12 deletions
|
@ -1,6 +1,6 @@
|
|||
use ratatui::DefaultTerminal;
|
||||
|
||||
use crate::{message::{Message, MessageKind}, player::Player, ui::ui};
|
||||
use crate::{lobby::Lobby, message::{Message, MessageKind}, player::Player, ui::ui};
|
||||
|
||||
#[derive(Debug, Default)]
|
||||
pub struct Client {
|
||||
|
@ -13,6 +13,7 @@ pub struct Client {
|
|||
pub popup_title: String,
|
||||
pub popup_content: String,
|
||||
pub response: String,
|
||||
pub lobby: Option<Lobby>,
|
||||
}
|
||||
|
||||
impl Client {
|
||||
|
|
|
@ -1,10 +1,12 @@
|
|||
use rand::seq::SliceRandom;
|
||||
use rand::rng;
|
||||
use serde::Deserialize;
|
||||
use serde::Serialize;
|
||||
|
||||
use crate::card::Card;
|
||||
use crate::card::Suit;
|
||||
|
||||
#[derive(PartialEq, Eq, Hash, Clone)]
|
||||
#[derive(Debug, Serialize, Deserialize, PartialEq, Eq, Hash, Clone)]
|
||||
pub struct Deck {
|
||||
pub deck: Vec<Card>,
|
||||
}
|
||||
|
|
|
@ -1,7 +1,8 @@
|
|||
use crate::{deck::Deck, player::Player};
|
||||
use petname::Generator;
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
#[derive(Eq, PartialEq, Hash, Clone)]
|
||||
#[derive(Debug, Serialize, Deserialize, Eq, PartialEq, Hash, Clone)]
|
||||
pub struct Lobby {
|
||||
pub id: String,
|
||||
pub players: Vec<Player>,
|
||||
|
@ -37,4 +38,10 @@ impl Lobby {
|
|||
pub fn get_id(&self) -> String {
|
||||
self.id.clone()
|
||||
}
|
||||
|
||||
pub fn encode(&self) -> anyhow::Result<String> {
|
||||
let lobby_json = serde_json::to_string(self)?;
|
||||
|
||||
Ok(lobby_json)
|
||||
}
|
||||
}
|
||||
|
|
|
@ -7,7 +7,7 @@ use axum::{
|
|||
Router
|
||||
};
|
||||
|
||||
use crate::{message::Message, player::Player};
|
||||
use crate::{lobby::Lobby, message::Message, player::Player};
|
||||
use crate::db::AppState;
|
||||
use crate::message::MessageKind;
|
||||
|
||||
|
@ -38,7 +38,7 @@ impl App {
|
|||
match json_body {
|
||||
Ok(Json(message)) => {
|
||||
println!("{:?}", message);
|
||||
let player: Player = serde_json::from_str(&message.content).expect("niente json");
|
||||
let player: Player = serde_json::from_str(&message.content).expect(&StatusCode::PRECONDITION_FAILED.to_string());
|
||||
{
|
||||
let mut players = db.players
|
||||
.lock().expect("ciao");
|
||||
|
@ -49,17 +49,46 @@ impl App {
|
|||
|
||||
players.insert(
|
||||
player.get_name(),
|
||||
Bytes::copy_from_slice(player.encode().expect("failed encoding player").as_ref())
|
||||
Bytes::copy_from_slice(player.encode().expect(&StatusCode::EXPECTATION_FAILED.to_string()).as_ref())
|
||||
);
|
||||
println!("{:?}", db.players);
|
||||
}
|
||||
|
||||
println!("{:?}", db.players);
|
||||
Ok(Json(Message::new("server", MessageKind::CreatePlayer, "user created").expect("failed creating message")))
|
||||
}
|
||||
Err(_) => Err(StatusCode::UNPROCESSABLE_ENTITY)
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn create_lobby(
|
||||
State(db): State<AppState>,
|
||||
json_body: Result<Json<Message>, JsonRejection>,
|
||||
) -> Result<axum::Json<Message>, StatusCode> {
|
||||
match json_body {
|
||||
Ok(Json(message)) => {
|
||||
println!("{:?}", message);
|
||||
let lobby: Lobby = serde_json::from_str(&message.content).expect(&StatusCode::PRECONDITION_FAILED.to_string());
|
||||
{
|
||||
let mut lobbies = db.lobbies
|
||||
.lock().expect("ciao");
|
||||
|
||||
if lobbies.contains_key(&lobby.id) {
|
||||
return Err(StatusCode::UNAVAILABLE_FOR_LEGAL_REASONS)
|
||||
}
|
||||
|
||||
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 created").expect("failed creating message")))
|
||||
},
|
||||
Err(_) => Err(StatusCode::UNPROCESSABLE_ENTITY),
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn delete_player(
|
||||
State(db): State<AppState>,
|
||||
Path(player_id): Path<String>
|
||||
|
@ -80,10 +109,23 @@ impl App {
|
|||
|
||||
}
|
||||
|
||||
pub async fn create_lobby(
|
||||
pub async fn delete_lobby(
|
||||
State(db): State<AppState>,
|
||||
json_body: Result<Json<Message>, JsonRejection>,
|
||||
) -> Result<axum::Json<Message>, StatusCode> {
|
||||
todo!()
|
||||
Path(lobby_id): Path<String>
|
||||
) -> Result<axum::response::Json<Message>, StatusCode> {
|
||||
{
|
||||
let mut lobbies = db.lobbies
|
||||
.lock().expect("ciao");
|
||||
|
||||
match lobbies.contains_key(&lobby_id) {
|
||||
true => {
|
||||
lobbies.remove(&lobby_id);
|
||||
println!("{:?}", lobbies);
|
||||
Ok(Json(Message::new("server", MessageKind::DeleteLobby, "lobby deleted").expect("a")))
|
||||
},
|
||||
false => Err(StatusCode::NOT_FOUND),
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
|
|
@ -31,7 +31,8 @@ impl Server {
|
|||
.route("/echo", post(App::echo))
|
||||
.route("/create/player", post(App::create_player))
|
||||
.route("/create/lobby", post(App::create_lobby))
|
||||
.route("/delete/{player-id}", get(App::delete_player))
|
||||
.route("/delete/{player_id}", get(App::delete_player))
|
||||
.route("/delete/{lobby_id}", get(App::delete_lobby))
|
||||
.with_state(app.db);
|
||||
|
||||
axum::serve(listener, router).await.unwrap();
|
||||
|
|
Loading…
Add table
Reference in a new issue