use crate::api::{greeting, Message, SocketAddr}; use crate::AppState; use crate::Arc; use crate::CAHGame; use crate::CAHPlayer; use serde::Deserialize; /// 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, } /// Game join request structure pub struct GameJoinRequest { /// Game id pub id: u8, // increase later /// Game password pub password: String, /// Player info pub player: CAHPlayer, } pub async fn message_handler(message: Message, state: &Arc, who: SocketAddr) { let tx = &state.tx; match message { Message::Text(text) => { tracing::debug!("{who}: {}", text); if let Ok(new_game) = serde_json::from_str::(&text) { tracing::debug!("{:#?}", &new_game); // create game if let Ok(new_game_object) = CAHGame::new(new_game) { state.games.lock().unwrap().push(new_game_object); let _update = tx.send(greeting(&state)); } else { let _res = tx.send(format!("error creating game")); } } else { // just echo let msg = format! {"{who}: {text}"}; tracing::debug!("{msg}"); let _res = tx.send(msg); } } Message::Binary(data) => { tracing::debug!("Binary: {:?}", data) } Message::Close(c) => { if let Some(cf) = c { tracing::debug!( "Close received from {who} with code: {} and reason: {}", cf.code, cf.reason ) } else { tracing::debug!("close received without close frame") } let msg = format!("{who} left."); tracing::debug!("{msg}"); let _ = tx.send(msg); } Message::Pong(ping) => { tracing::debug!("Pong received with: {:?}", ping); } Message::Ping(pong) => { tracing::debug!("Pong received with: {:?}", pong); } } }