diff --git a/clients/leptos/src/components/browser/create_game.rs b/clients/leptos/src/components/browser/create_game.rs index 43402c1..0d37321 100644 --- a/clients/leptos/src/components/browser/create_game.rs +++ b/clients/leptos/src/components/browser/create_game.rs @@ -46,8 +46,8 @@ pub fn CreateGame() -> impl IntoView { let request_new_game = move |_| { set_websocket_send( to_string(&NewGameRequest { - name: input_game_name(), - packs: selected_packs(), + game_name: input_game_name(), + game_packs: selected_packs().into_iter().collect::>(), }) .unwrap(), ); diff --git a/lib/src/lib.rs b/lib/src/lib.rs index 655a326..ad66e87 100644 --- a/lib/src/lib.rs +++ b/lib/src/lib.rs @@ -1,6 +1,5 @@ use prost::Message; use serde::{Deserialize, Serialize}; -use std::collections::HashSet; /// Judge decision #[derive(Clone, Serialize, Deserialize, Message)] @@ -110,78 +109,98 @@ pub struct GameStateMeta { } /// Game browser meta -#[derive(Clone, Debug, Serialize, Deserialize)] +#[derive(Clone, Message, Serialize, Deserialize)] pub struct GameBrowserMeta { + #[prost(string, tag = "1")] pub uuid: String, + #[prost(string, tag = "2")] pub name: String, + #[prost(string, tag = "3")] pub host: String, - pub players: usize, + #[prost(uint32, tag = "4")] + pub players: u32, + #[prost(bytes, tag = "5")] pub packs: Vec, } /// Card Pack Meta -#[derive(Clone, Debug, Serialize, Deserialize)] +#[derive(Clone, Message, Serialize, Deserialize)] pub struct CardPackMeta { + #[prost(string, tag = "1")] pub name: String, + #[prost(string, tag = "2")] pub pack: String, - pub num_white: usize, - pub num_black: usize, + #[prost(uint32, tag = "3")] + pub num_white: u32, + #[prost(uint32, tag = "4")] + pub num_black: u32, } /// Card Packs Meta -#[derive(Clone, Debug, Serialize, Deserialize)] +#[derive(Clone, Message, Serialize, Deserialize)] pub struct CardPacksMeta { + #[prost(message, repeated, tag = "1")] pub official_meta: Vec, + #[prost(message, repeated, tag = "2")] pub unofficial_meta: Vec, } /// Games update -#[derive(Serialize, Deserialize, Debug)] +#[derive(Message, Serialize, Deserialize)] pub struct GamesUpdate { + #[prost(message, repeated, tag = "1")] pub games: Vec, } /// Chat update -#[derive(Serialize, Deserialize, Debug)] +#[derive(Message, Serialize, Deserialize)] pub struct ChatUpdate { + #[prost(string, tag = "1")] pub room: String, + #[prost(string, repeated, tag = "2")] pub users: Vec, } /// User login request (to change name) -#[derive(Serialize, Deserialize, Debug)] +#[derive(Message, Serialize, Deserialize)] pub struct UserLogInRequest { + #[prost(string, tag = "1")] pub username: String, } /// Response to user name change containing new name -#[derive(Serialize, Deserialize, Debug)] +#[derive(Message, Serialize, Deserialize)] pub struct UserUpdate { + #[prost(string, tag = "1")] pub username: String, } /// Chat message -#[derive(Serialize, Deserialize, Debug)] +#[derive(Message, Serialize, Deserialize)] pub struct ChatMessage { + #[prost(string, tag = "1")] pub text: String, } /// Server user count -#[derive(Serialize, Deserialize, Debug)] +#[derive(Message, Serialize, Deserialize)] pub struct ServerOnlineUsers { - pub online_users: usize, + #[prost(uint32, tag = "1")] + pub online_users: u32, } /// Server games count -#[derive(Serialize, Deserialize, Debug)] +#[derive(Message, Serialize, Deserialize)] pub struct ServerActiveGames { - pub active_games: usize, + #[prost(uint32, tag = "1")] + pub active_games: u32, } /// New game request structure -#[derive(Debug, Serialize, Deserialize)] +#[derive(Message, Serialize, Deserialize)] pub struct NewGameRequest { - /// Game name - pub name: String, - pub packs: HashSet, + #[prost(string, tag = "1")] + pub game_name: String, + #[prost(string, repeated, tag = "2")] + pub game_packs: Vec, } diff --git a/server/src/game_handler.rs b/server/src/game_handler.rs index 13006ff..59c5ab5 100644 --- a/server/src/game_handler.rs +++ b/server/src/game_handler.rs @@ -310,10 +310,10 @@ impl GameHandler { /// Creates a new game async fn create_new_game(&mut self, new_game: NewGameRequest, addr: SocketAddr) { - if new_game.packs.is_empty() { + if new_game.game_packs.is_empty() { tracing::error!("New game cards are empty!"); return; - } else if new_game.name.is_empty() { + } else if new_game.game_name.is_empty() { tracing::error!("New game name is empty!"); return; } @@ -322,10 +322,10 @@ impl GameHandler { let new_game_name; let max_game_name_len = 32; - if new_game.name.len() > max_game_name_len { - new_game_name = new_game.name[..max_game_name_len].to_string() + if new_game.game_name.len() > max_game_name_len { + new_game_name = new_game.game_name[..max_game_name_len].to_string() } else { - new_game_name = new_game.name + new_game_name = new_game.game_name } // Create manifest @@ -333,7 +333,7 @@ impl GameHandler { name: new_game_name, host: host.clone(), packs: new_game - .packs + .game_packs .into_iter() .map(|pack| u8::from_str_radix(&pack, 10).unwrap()) .collect(), @@ -374,7 +374,7 @@ impl GameHandler { uuid: game.uuid.to_string(), name: game.name.clone(), host: game.host.read().unwrap().name.clone(), - players: game.players.len(), + players: game.players.len().try_into().unwrap(), packs: game.packs.clone(), }) .collect::>(); @@ -391,7 +391,7 @@ impl GameHandler { /// Broadcast updated game count fn broadcast_game_count(&self) { let tx = self.state.broadcast_tx.clone(); - let active_games = self.games.len(); + let active_games: u32 = self.games.len().try_into().unwrap(); let msg = to_string(&ServerActiveGames { active_games }).unwrap(); tokio::spawn(async move { if let Err(e) = tx.send(msg) { diff --git a/server/src/lib.rs b/server/src/lib.rs index cb59bfd..7619425 100644 --- a/server/src/lib.rs +++ b/server/src/lib.rs @@ -201,8 +201,8 @@ pub fn load_cards_from_json( let meta = CardPackMeta { name: sets.name, pack: pack.expect("No card pack number!").to_string(), - num_white, - num_black, + num_white: num_white.try_into().unwrap(), + num_black: num_black.try_into().unwrap(), }; if sets.official { diff --git a/server/src/user_handler.rs b/server/src/user_handler.rs index 1ba5bf1..8bed24d 100644 --- a/server/src/user_handler.rs +++ b/server/src/user_handler.rs @@ -315,7 +315,14 @@ impl UserHandler { /// Broadcast updated user count fn broadcast_user_count(&self) { let tx = self.state.broadcast_tx.clone(); - let online_users = self.state.online_users.read().unwrap().len(); + let online_users: u32 = self + .state + .online_users + .read() + .unwrap() + .len() + .try_into() + .unwrap(); let msg = to_string(&ServerOnlineUsers { online_users }).unwrap(); tokio::spawn(async move { tx.send(msg) }); }