129 lines
3.9 KiB
Rust
129 lines
3.9 KiB
Rust
use anyhow::Result;
|
|
use axum::{
|
|
body::Bytes, extract::{
|
|
rejection::JsonRejection, Json, Path, State
|
|
},
|
|
http::StatusCode,
|
|
Router
|
|
};
|
|
|
|
use crate::{lobby::Lobby, message::Message, player::Player};
|
|
use crate::db::AppState;
|
|
use crate::message::MessageKind;
|
|
|
|
pub struct App {
|
|
pub db: AppState,
|
|
pub router: Router<AppState>,
|
|
}
|
|
|
|
impl App {
|
|
pub fn new() -> Self {
|
|
let db = AppState::new();
|
|
let router: Router<AppState> = Router::new();
|
|
|
|
Self {
|
|
db,
|
|
router,
|
|
}
|
|
}
|
|
|
|
pub async fn echo(body: Bytes) -> Bytes {
|
|
body
|
|
}
|
|
|
|
pub async fn create_player(
|
|
State(db): State<AppState>,
|
|
json_body: Result<Json<Message>, JsonRejection>,
|
|
) -> Result<axum::Json<Message>, StatusCode> {
|
|
match json_body {
|
|
Ok(Json(message)) => {
|
|
println!("{:?}", message);
|
|
let player: Player = Player::new().unwrap();
|
|
|
|
let mut players = db.players
|
|
.lock().expect("ciao");
|
|
|
|
if players.contains_key(&player.name) {
|
|
return Err(StatusCode::UNAVAILABLE_FOR_LEGAL_REASONS)
|
|
}
|
|
|
|
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)
|
|
}
|
|
}
|
|
|
|
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 = Lobby::new();
|
|
|
|
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.id).expect("failed creating message")))
|
|
},
|
|
Err(_) => Err(StatusCode::UNPROCESSABLE_ENTITY),
|
|
}
|
|
}
|
|
|
|
pub async fn delete_player(
|
|
State(db): State<AppState>,
|
|
Path(player_id): Path<String>
|
|
) -> Result<axum::response::Json<Message>, StatusCode> {
|
|
{
|
|
let mut players = db.players
|
|
.lock().expect("ciao");
|
|
|
|
match players.contains_key(&player_id) {
|
|
true => {
|
|
players.remove(&player_id);
|
|
println!("{:?}", players);
|
|
Ok(Json(Message::new("server", MessageKind::DeletePlayer, "user deleted").expect("a")))
|
|
},
|
|
false => Err(StatusCode::NOT_FOUND),
|
|
}
|
|
}
|
|
|
|
}
|
|
|
|
pub async fn delete_lobby(
|
|
State(db): State<AppState>,
|
|
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),
|
|
}
|
|
}
|
|
|
|
}
|
|
}
|