diff --git a/Cargo.lock b/Cargo.lock index ed96348..24f3ccc 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -166,9 +166,9 @@ dependencies = [ [[package]] name = "cc" -version = "1.0.95" +version = "1.0.96" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d32a725bc159af97c3e629873bb9f88fb8cf8a4867175f76dc987815ea07c83b" +checksum = "065a29261d53ba54260972629f9ca6bffa69bac13cd1fed61420f7fa68b9f8bd" [[package]] name = "cfg-if" diff --git a/src/CAHd_game.rs b/src/CAHd_game.rs index e7eecc4..1714273 100644 --- a/src/CAHd_game.rs +++ b/src/CAHd_game.rs @@ -24,7 +24,7 @@ pub struct CAHCardBlack { } /// A CAH pack -#[derive(Serialize, Deserialize)] +#[derive(Debug, Serialize, Deserialize)] pub struct CAHCardSet { /// Name of the pack name: String, @@ -39,7 +39,7 @@ pub struct CAHCardSet { } /// Player roles -#[derive(Debug)] +#[derive(Debug, Deserialize)] pub enum PlayerRole { /// Player is host Host, @@ -50,7 +50,7 @@ pub enum PlayerRole { } /// A struct that represents a player -#[derive(Debug)] +#[derive(Debug, Deserialize)] pub struct CAHPlayer { /// Player's username pub name: String, @@ -86,13 +86,14 @@ pub struct CAHGame { } /// New game request structure +#[derive(Debug, Deserialize)] pub struct NewGameRequest { /// Game name pub name: String, /// Game host pub host: CAHPlayer, /// Chosen packs - pub packs: Vec, + pub packs: Vec, } /// Game join request structure @@ -128,7 +129,7 @@ impl CAHGame { println!("Creating game {}", &request.name); game.name = request.name; - game.build_decks(request.packs)?; + // game.build_decks(request.packs)?; game.create_player(request.host)?; game.deal_black()?; diff --git a/src/api.rs b/src/api.rs index 1ed7385..12d13ab 100644 --- a/src/api.rs +++ b/src/api.rs @@ -1,4 +1,5 @@ use crate::AppState; +use crate::CAHd_game::*; use axum::{ extract::{ ws::{Message, WebSocket, WebSocketUpgrade}, @@ -16,9 +17,6 @@ pub async fn websocket_handler( ws.on_upgrade(|socket| websocket(socket, state)) } -// This function deals with a single websocket connection, i.e., a single -// connected client / user, for which we will spawn two independent tasks (for -// receiving / sending chat messages). pub async fn websocket(stream: WebSocket, state: Arc) { // By splitting, we can send and receive at the same time. let (mut sender, mut receiver) = stream.split(); @@ -27,22 +25,55 @@ pub async fn websocket(stream: WebSocket, state: Arc) { let mut newplayer = String::new(); // Loop until a text message is found. while let Some(Ok(message)) = receiver.next().await { - if let Message::Text(name) = message { - // If newplayer that is sent by client is not taken, fill newplayer string. - check_username(&state, &mut newplayer, &name); - - // If not empty we want to quit the loop else we want to quit function. - if !newplayer.is_empty() { - break; - } else { - // Only send our client that newplayer is taken. - let _ = sender - .send(Message::Text(String::from("Username already taken."))) - .await; - - return; + match message { + Message::Text(text) => { + tracing::debug!("Text: {}", text); + // let message_str: &str = message.to_text().unwrap(); + tracing::debug!( + "{:#?}", + serde_json::from_str::(&text) + .unwrap() + .host + .name + ); + } + Message::Binary(data) => { + tracing::debug!("Binary: {:?}", data) + } + Message::Close(c) => { + if let Some(cf) = c { + tracing::debug!( + "Close received with code: {} and reason: {}", + cf.code, + cf.reason + ) + } else { + tracing::debug!("close sent without close frame") + } + } + Message::Pong(ping) => { + tracing::debug!("Pong sent with {:?}", ping); + } + Message::Ping(pong) => { + tracing::debug!("Pong sent with {:?}", pong); } } + // if let Message::Text(name) = message { + // // If newplayer that is sent by client is not taken, fill newplayer string. + // check_username(&state, &mut newplayer, &name); + // + // // If not empty we want to quit the loop else we want to quit function. + // if !newplayer.is_empty() { + // break; + // } else { + // // Only send our client that newplayer is taken. + // let _ = sender + // .send(Message::Text(String::from("Username already taken."))) + // .await; + // + // return; + // } + // } } // We subscribe *before* sending the "joined" message, so that we will also diff --git a/src/main.rs b/src/main.rs index 8eb3861..3eac610 100644 --- a/src/main.rs +++ b/src/main.rs @@ -54,7 +54,8 @@ fn test() -> Result<(), Box> { let test_game0 = NewGameRequest { name: "Test0".to_string(), host: test_player0, - packs: chosen_packs, + // packs: chosen_packs, + packs: vec![0], }; games.push(CAHGame::new(test_game0)?); @@ -117,6 +118,8 @@ async fn main() -> Result<(), Box> { &app_state.all_cards.lock().unwrap().len() ); + tracing::debug!("{} active games", &app_state.games.lock().unwrap().len()); + let app = Router::new() .route("/", get(index)) .route("/test", get(spawnclients)) diff --git a/test_client.html b/test_client.html index 2687c35..f411bc4 100644 --- a/test_client.html +++ b/test_client.html @@ -43,14 +43,15 @@