This commit is contained in:
Adam 2024-11-16 04:19:21 -05:00
parent 7b6e8a8e9b
commit 36032f5718
5 changed files with 59 additions and 33 deletions

View file

@ -46,8 +46,8 @@ pub fn CreateGame() -> impl IntoView {
let request_new_game = move |_| { let request_new_game = move |_| {
set_websocket_send( set_websocket_send(
to_string(&NewGameRequest { to_string(&NewGameRequest {
name: input_game_name(), game_name: input_game_name(),
packs: selected_packs(), game_packs: selected_packs().into_iter().collect::<Vec<String>>(),
}) })
.unwrap(), .unwrap(),
); );

View file

@ -1,6 +1,5 @@
use prost::Message; use prost::Message;
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use std::collections::HashSet;
/// Judge decision /// Judge decision
#[derive(Clone, Serialize, Deserialize, Message)] #[derive(Clone, Serialize, Deserialize, Message)]
@ -110,78 +109,98 @@ pub struct GameStateMeta {
} }
/// Game browser meta /// Game browser meta
#[derive(Clone, Debug, Serialize, Deserialize)] #[derive(Clone, Message, Serialize, Deserialize)]
pub struct GameBrowserMeta { pub struct GameBrowserMeta {
#[prost(string, tag = "1")]
pub uuid: String, pub uuid: String,
#[prost(string, tag = "2")]
pub name: String, pub name: String,
#[prost(string, tag = "3")]
pub host: String, pub host: String,
pub players: usize, #[prost(uint32, tag = "4")]
pub players: u32,
#[prost(bytes, tag = "5")]
pub packs: Vec<u8>, pub packs: Vec<u8>,
} }
/// Card Pack Meta /// Card Pack Meta
#[derive(Clone, Debug, Serialize, Deserialize)] #[derive(Clone, Message, Serialize, Deserialize)]
pub struct CardPackMeta { pub struct CardPackMeta {
#[prost(string, tag = "1")]
pub name: String, pub name: String,
#[prost(string, tag = "2")]
pub pack: String, pub pack: String,
pub num_white: usize, #[prost(uint32, tag = "3")]
pub num_black: usize, pub num_white: u32,
#[prost(uint32, tag = "4")]
pub num_black: u32,
} }
/// Card Packs Meta /// Card Packs Meta
#[derive(Clone, Debug, Serialize, Deserialize)] #[derive(Clone, Message, Serialize, Deserialize)]
pub struct CardPacksMeta { pub struct CardPacksMeta {
#[prost(message, repeated, tag = "1")]
pub official_meta: Vec<CardPackMeta>, pub official_meta: Vec<CardPackMeta>,
#[prost(message, repeated, tag = "2")]
pub unofficial_meta: Vec<CardPackMeta>, pub unofficial_meta: Vec<CardPackMeta>,
} }
/// Games update /// Games update
#[derive(Serialize, Deserialize, Debug)] #[derive(Message, Serialize, Deserialize)]
pub struct GamesUpdate { pub struct GamesUpdate {
#[prost(message, repeated, tag = "1")]
pub games: Vec<GameBrowserMeta>, pub games: Vec<GameBrowserMeta>,
} }
/// Chat update /// Chat update
#[derive(Serialize, Deserialize, Debug)] #[derive(Message, Serialize, Deserialize)]
pub struct ChatUpdate { pub struct ChatUpdate {
#[prost(string, tag = "1")]
pub room: String, pub room: String,
#[prost(string, repeated, tag = "2")]
pub users: Vec<String>, pub users: Vec<String>,
} }
/// User login request (to change name) /// User login request (to change name)
#[derive(Serialize, Deserialize, Debug)] #[derive(Message, Serialize, Deserialize)]
pub struct UserLogInRequest { pub struct UserLogInRequest {
#[prost(string, tag = "1")]
pub username: String, pub username: String,
} }
/// Response to user name change containing new name /// Response to user name change containing new name
#[derive(Serialize, Deserialize, Debug)] #[derive(Message, Serialize, Deserialize)]
pub struct UserUpdate { pub struct UserUpdate {
#[prost(string, tag = "1")]
pub username: String, pub username: String,
} }
/// Chat message /// Chat message
#[derive(Serialize, Deserialize, Debug)] #[derive(Message, Serialize, Deserialize)]
pub struct ChatMessage { pub struct ChatMessage {
#[prost(string, tag = "1")]
pub text: String, pub text: String,
} }
/// Server user count /// Server user count
#[derive(Serialize, Deserialize, Debug)] #[derive(Message, Serialize, Deserialize)]
pub struct ServerOnlineUsers { pub struct ServerOnlineUsers {
pub online_users: usize, #[prost(uint32, tag = "1")]
pub online_users: u32,
} }
/// Server games count /// Server games count
#[derive(Serialize, Deserialize, Debug)] #[derive(Message, Serialize, Deserialize)]
pub struct ServerActiveGames { pub struct ServerActiveGames {
pub active_games: usize, #[prost(uint32, tag = "1")]
pub active_games: u32,
} }
/// New game request structure /// New game request structure
#[derive(Debug, Serialize, Deserialize)] #[derive(Message, Serialize, Deserialize)]
pub struct NewGameRequest { pub struct NewGameRequest {
/// Game name #[prost(string, tag = "1")]
pub name: String, pub game_name: String,
pub packs: HashSet<String>, #[prost(string, repeated, tag = "2")]
pub game_packs: Vec<String>,
} }

View file

@ -310,10 +310,10 @@ impl GameHandler {
/// Creates a new game /// Creates a new game
async fn create_new_game(&mut self, new_game: NewGameRequest, addr: SocketAddr) { 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!"); tracing::error!("New game cards are empty!");
return; return;
} else if new_game.name.is_empty() { } else if new_game.game_name.is_empty() {
tracing::error!("New game name is empty!"); tracing::error!("New game name is empty!");
return; return;
} }
@ -322,10 +322,10 @@ impl GameHandler {
let new_game_name; let new_game_name;
let max_game_name_len = 32; let max_game_name_len = 32;
if new_game.name.len() > max_game_name_len { if new_game.game_name.len() > max_game_name_len {
new_game_name = new_game.name[..max_game_name_len].to_string() new_game_name = new_game.game_name[..max_game_name_len].to_string()
} else { } else {
new_game_name = new_game.name new_game_name = new_game.game_name
} }
// Create manifest // Create manifest
@ -333,7 +333,7 @@ impl GameHandler {
name: new_game_name, name: new_game_name,
host: host.clone(), host: host.clone(),
packs: new_game packs: new_game
.packs .game_packs
.into_iter() .into_iter()
.map(|pack| u8::from_str_radix(&pack, 10).unwrap()) .map(|pack| u8::from_str_radix(&pack, 10).unwrap())
.collect(), .collect(),
@ -374,7 +374,7 @@ impl GameHandler {
uuid: game.uuid.to_string(), uuid: game.uuid.to_string(),
name: game.name.clone(), name: game.name.clone(),
host: game.host.read().unwrap().name.clone(), host: game.host.read().unwrap().name.clone(),
players: game.players.len(), players: game.players.len().try_into().unwrap(),
packs: game.packs.clone(), packs: game.packs.clone(),
}) })
.collect::<Vec<GameBrowserMeta>>(); .collect::<Vec<GameBrowserMeta>>();
@ -391,7 +391,7 @@ impl GameHandler {
/// Broadcast updated game count /// Broadcast updated game count
fn broadcast_game_count(&self) { fn broadcast_game_count(&self) {
let tx = self.state.broadcast_tx.clone(); 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(); let msg = to_string(&ServerActiveGames { active_games }).unwrap();
tokio::spawn(async move { tokio::spawn(async move {
if let Err(e) = tx.send(msg) { if let Err(e) = tx.send(msg) {

View file

@ -201,8 +201,8 @@ pub fn load_cards_from_json(
let meta = CardPackMeta { let meta = CardPackMeta {
name: sets.name, name: sets.name,
pack: pack.expect("No card pack number!").to_string(), pack: pack.expect("No card pack number!").to_string(),
num_white, num_white: num_white.try_into().unwrap(),
num_black, num_black: num_black.try_into().unwrap(),
}; };
if sets.official { if sets.official {

View file

@ -315,7 +315,14 @@ impl UserHandler {
/// Broadcast updated user count /// Broadcast updated user count
fn broadcast_user_count(&self) { fn broadcast_user_count(&self) {
let tx = self.state.broadcast_tx.clone(); 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(); let msg = to_string(&ServerOnlineUsers { online_users }).unwrap();
tokio::spawn(async move { tx.send(msg) }); tokio::spawn(async move { tx.send(msg) });
} }