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 |_| {
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::<Vec<String>>(),
})
.unwrap(),
);

View file

@ -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<u8>,
}
/// 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<CardPackMeta>,
#[prost(message, repeated, tag = "2")]
pub unofficial_meta: Vec<CardPackMeta>,
}
/// Games update
#[derive(Serialize, Deserialize, Debug)]
#[derive(Message, Serialize, Deserialize)]
pub struct GamesUpdate {
#[prost(message, repeated, tag = "1")]
pub games: Vec<GameBrowserMeta>,
}
/// 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<String>,
}
/// 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<String>,
#[prost(string, tag = "1")]
pub game_name: String,
#[prost(string, repeated, tag = "2")]
pub game_packs: Vec<String>,
}

View file

@ -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::<Vec<GameBrowserMeta>>();
@ -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) {

View file

@ -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 {

View file

@ -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) });
}