player gets added to lobby upon lobby creation
This commit is contained in:
parent
789e3912d3
commit
e6968b6c8d
3 changed files with 34 additions and 19 deletions
|
@ -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);
|
||||
}
|
||||
|
||||
|
|
|
@ -34,7 +34,7 @@ impl App {
|
|||
pub async fn create_player(
|
||||
State(db): State<AppState>,
|
||||
json_body: Result<Json<Message>, JsonRejection>,
|
||||
) -> Result<axum::Json<Message>, StatusCode> {
|
||||
) -> Result<axum::Json<Message>, String> {
|
||||
match json_body {
|
||||
Ok(Json(message)) => {
|
||||
println!("{:?}", message);
|
||||
|
@ -44,7 +44,7 @@ impl App {
|
|||
.lock().expect("ciao");
|
||||
|
||||
if players.contains_key(&player.name) {
|
||||
return Err(StatusCode::UNAVAILABLE_FOR_LEGAL_REASONS)
|
||||
return Err(String::from("player already in db"))
|
||||
}
|
||||
|
||||
players.insert(
|
||||
|
@ -55,42 +55,57 @@ impl App {
|
|||
|
||||
Ok(Json(Message::new("server", MessageKind::CreatePlayer, &player.name).expect("failed creating message")))
|
||||
}
|
||||
Err(_) => Err(StatusCode::UNPROCESSABLE_ENTITY)
|
||||
Err(_) => Err(String::from("failed parsing json message"))
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn create_lobby(
|
||||
State(db): State<AppState>,
|
||||
json_body: Result<Json<Message>, JsonRejection>,
|
||||
) -> Result<axum::Json<Message>, StatusCode> {
|
||||
) -> Result<axum::Json<Message>, String> {
|
||||
match json_body {
|
||||
Ok(Json(message)) => {
|
||||
println!("{:?}", message);
|
||||
let lobby: Lobby = Lobby::new();
|
||||
|
||||
let mut lobby: Lobby = Lobby::new();
|
||||
|
||||
let mut lobbies = db.lobbies
|
||||
.lock().expect("ciao");
|
||||
.lock()
|
||||
.expect("failed to lock lobbies db");
|
||||
|
||||
if lobbies.contains_key(&lobby.id) {
|
||||
return Err(StatusCode::UNAVAILABLE_FOR_LEGAL_REASONS)
|
||||
return Err(String::from("lobby already in db"))
|
||||
}
|
||||
|
||||
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(
|
||||
lobby.get_id(),
|
||||
Bytes::copy_from_slice(lobby.encode().expect(&StatusCode::EXPECTATION_FAILED.to_string()).as_ref())
|
||||
Bytes::copy_from_slice(lobby.encode().expect("failed copying lobby from bytes slice").as_ref())
|
||||
);
|
||||
println!("{:?}", lobbies);
|
||||
|
||||
Ok(Json(Message::new("server", MessageKind::CreateLobby, &lobby.id).expect("failed creating message")))
|
||||
},
|
||||
Err(_) => Err(StatusCode::UNPROCESSABLE_ENTITY),
|
||||
Err(_) => Err(String::from("failed to parse json message")),
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn delete_player(
|
||||
State(db): State<AppState>,
|
||||
Path(player_id): Path<String>
|
||||
) -> Result<axum::response::Json<Message>, StatusCode> {
|
||||
) -> Result<axum::response::Json<Message>, String> {
|
||||
{
|
||||
let mut players = db.players
|
||||
.lock().expect("ciao");
|
||||
|
@ -101,7 +116,7 @@ impl App {
|
|||
println!("{:?}", players);
|
||||
Ok(Json(Message::new("server", MessageKind::DeletePlayer, "user deleted").expect("a")))
|
||||
},
|
||||
false => Err(StatusCode::NOT_FOUND),
|
||||
false => Err(String::from("player not found")),
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -110,7 +125,7 @@ impl App {
|
|||
pub async fn delete_lobby(
|
||||
State(db): State<AppState>,
|
||||
Path(lobby_id): Path<String>
|
||||
) -> Result<axum::response::Json<Message>, StatusCode> {
|
||||
) -> Result<axum::response::Json<Message>, String> {
|
||||
{
|
||||
let mut lobbies = db.lobbies
|
||||
.lock().expect("ciao");
|
||||
|
@ -121,7 +136,7 @@ impl App {
|
|||
println!("{:?}", lobbies);
|
||||
Ok(Json(Message::new("server", MessageKind::DeleteLobby, "lobby deleted").expect("a")))
|
||||
},
|
||||
false => Err(StatusCode::NOT_FOUND),
|
||||
false => Err(String::from("lobby not found in db")),
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -130,7 +145,7 @@ impl App {
|
|||
pub async fn join_lobby(
|
||||
State(db): State<AppState>,
|
||||
Path(lobby_id): Path<String>
|
||||
) -> Result<axum::response::Json<Message>, StatusCode> {
|
||||
) -> Result<axum::response::Json<Message>, String> {
|
||||
todo!()
|
||||
}
|
||||
|
||||
|
|
|
@ -89,9 +89,9 @@ async fn handle_key_event(mut client: &mut Client, key_event: KeyEvent) {
|
|||
}
|
||||
},
|
||||
KeyCode::Char('2') => {
|
||||
if client.lobby != None {
|
||||
if client.lobby != None || client.user == None {
|
||||
client.popup_title = "Error".to_string();
|
||||
client.popup_content = "lobby already_created".to_string();
|
||||
client.popup_content = "lobby already_created or player not yet created".to_string();
|
||||
client.popup = true;
|
||||
} else {
|
||||
let addr = client.addr.clone();
|
||||
|
@ -99,7 +99,7 @@ async fn handle_key_event(mut client: &mut Client, key_event: KeyEvent) {
|
|||
let message = Message::new(
|
||||
"",
|
||||
MessageKind::CreateLobby,
|
||||
""
|
||||
&client.user_name,
|
||||
).unwrap();
|
||||
|
||||
Client::send(&mut client, message, &addr, &path).await.unwrap();
|
||||
|
|
Loading…
Add table
Reference in a new issue