From 3db4da669b1805e55b12508a23ffe9f48c5517e3 Mon Sep 17 00:00:00 2001 From: Adam <24621027+adoyle0@users.noreply.github.com> Date: Sat, 17 Aug 2024 21:55:15 -0400 Subject: [PATCH] stuuuufff --- client/src/components/browser.rs | 1 + client/src/components/game.rs | 29 ++++++++++-- lib/src/lib.rs | 16 ++++++- server/src/game_handler.rs | 79 ++++++++++++++++++++++++++++++-- server/src/lib.rs | 2 +- server/src/main.rs | 4 +- server/src/message_handler.rs | 32 +++++++++++-- server/src/user_handler.rs | 2 +- 8 files changed, 148 insertions(+), 17 deletions(-) diff --git a/client/src/components/browser.rs b/client/src/components/browser.rs index 948592d..1409170 100644 --- a/client/src/components/browser.rs +++ b/client/src/components/browser.rs @@ -67,6 +67,7 @@ pub fn Browser() -> impl IntoView { +

prevent joining an already joined game

{move || { game_browser_context() .iter() diff --git a/client/src/components/game.rs b/client/src/components/game.rs index ef03eb1..63d81d8 100644 --- a/client/src/components/game.rs +++ b/client/src/components/game.rs @@ -1,21 +1,25 @@ -// use crate::components::websocket::WebSocketContext; +use crate::components::websocket::WebSocketContext; use leptos::*; use lib::*; +use serde_json::to_string; #[component] pub fn Game() -> impl IntoView { - // let websocket = expect_context::(); + let websocket = expect_context::(); let game_meta = expect_context::>>(); + let (game_id, set_game_id) = create_signal("".to_string()); let (game_name, set_game_name) = create_signal("".to_string()); let (game_host, set_game_host) = create_signal("".to_string()); let (game_players, set_game_players) = create_signal(vec![]); let (game_czar, set_game_czar) = create_signal("".to_string()); let (game_black, set_game_black) = create_signal(("".to_string(), 0u8)); let (game_white, set_game_white) = create_signal(vec![]); + let (selected_card, set_selected_card) = create_signal("".to_string()); create_effect(move |_| { if let Some(game) = game_meta() { + set_game_id(game.uuid.clone()); set_game_name(game.name.clone()); set_game_host(game.host.clone()); set_game_players(game.players.clone()); @@ -25,6 +29,17 @@ pub fn Game() -> impl IntoView { } }); + create_effect(move |_| { + logging::log!("{:#?}", selected_card()); + websocket.send( + &to_string(&PlayerMoveRequest { + game_id: game_id(), + card_id: selected_card(), + }) + .unwrap(), + ) + }); + view! {
@@ -58,7 +73,15 @@ pub fn Game() -> impl IntoView { .map(|card| { view! {
-

{card}

+

{&card.text}

+
} }) diff --git a/lib/src/lib.rs b/lib/src/lib.rs index 5e6f3dd..b476e15 100644 --- a/lib/src/lib.rs +++ b/lib/src/lib.rs @@ -6,6 +6,20 @@ pub struct GameJoinRequest { pub id: String, } +/// Player Move Request +#[derive(Clone, Debug, Serialize, Deserialize)] +pub struct PlayerMoveRequest { + pub game_id: String, + pub card_id: String, +} + +/// White Card Meta +#[derive(Clone, Debug, Serialize, Deserialize)] +pub struct WhiteCardMeta { + pub uuid: String, + pub text: String, +} + /// Game meta #[derive(Clone, Debug, Serialize, Deserialize)] pub struct GameStateMeta { @@ -15,7 +29,7 @@ pub struct GameStateMeta { pub players: Vec, pub czar: String, pub black: (String, u8), - pub white: Vec, + pub white: Vec, pub packs: Vec, } diff --git a/server/src/game_handler.rs b/server/src/game_handler.rs index 2241781..1d6e4b2 100644 --- a/server/src/game_handler.rs +++ b/server/src/game_handler.rs @@ -25,6 +25,11 @@ pub enum GameHandlerMessage { addr: SocketAddr, id: String, }, + MoveRequest { + card_id: String, + game_id: String, + addr: SocketAddr, + }, } /// Handles game stuff @@ -42,11 +47,62 @@ impl GameHandler { /// Handles incoming messages pub async fn handle(&self, message: GameHandlerMessage) { match message { - NewGame { addr, new_game } => self.new_game(addr, new_game).await, + NewGame { addr, new_game } => self.create_new_game(addr, new_game).await, JoinGame { addr, id } => self.join_game(addr, id).await, + MoveRequest { + card_id, + game_id, + addr, + } => self.handle_player_move(card_id, game_id, addr).await, } } + /// Process player move request + async fn handle_player_move(&self, card_id: String, game_id: String, addr: SocketAddr) { + let this_player_id = self + .state + .online_users + .read() + .unwrap() + .get(&addr) + .unwrap() + .read() + .unwrap() + .uuid + .to_string(); + + let this_game = self + .state + .games + .read() + .unwrap() + .get(&game_id) + .unwrap() + .clone(); + + if this_game + .read() + .unwrap() + .czar + .read() + .unwrap() + .uuid + .to_string() + == this_player_id + { + tracing::error!("No! User id is same as current czar"); + } else { + tracing::error!("Ok, but i have nothing to do"); + } + + tracing::debug!( + "Player move received:\nCard: {}\nGame: {}\nPlayer: {}", + card_id, + game_id, + this_player_id, + ); + } + /// Puts a user in a game async fn join_game(&self, addr: SocketAddr, id: String) { // Get pointers @@ -82,7 +138,14 @@ impl GameHandler { this_game.read().unwrap().current_black.text.clone(), this_game.read().unwrap().current_black.pick, ), - white: player.white.iter().map(|card| card.text.clone()).collect(), + white: player + .white + .iter() + .map(|card| WhiteCardMeta { + uuid: card.uuid.to_string(), + text: card.text.clone(), + }) + .collect(), packs: this_game.read().unwrap().packs.clone(), }; @@ -106,7 +169,7 @@ impl GameHandler { } /// Creates a new game - async fn new_game(&self, addr: SocketAddr, new_game: NewGameRequest) { + async fn create_new_game(&self, addr: SocketAddr, new_game: NewGameRequest) { if new_game.packs.is_empty() { tracing::error!("New game cards are empty!"); return; @@ -148,7 +211,7 @@ impl GameHandler { .iter() .map(|player| { self.state - .user_uuid + .users_by_id .read() .unwrap() .get(player.0) @@ -170,7 +233,10 @@ impl GameHandler { .unwrap() .white .iter() - .map(|card| card.text.clone()) + .map(|card| WhiteCardMeta { + uuid: card.uuid.to_string(), + text: card.text.clone(), + }) .collect(), packs: new_game_object.packs.clone(), }; @@ -301,6 +367,8 @@ pub struct Game { pub host: Arc>, /// White draw pile pub white: Vec>, + /// Current card czar + pub czar: Arc>, /// Black draw pile black: Vec>, pub players: HashMap, @@ -349,6 +417,7 @@ impl Game { uuid: Uuid::now_v7(), name: request.name, host: request.host.clone(), + czar: request.host.clone(), white, black, players: HashMap::new(), diff --git a/server/src/lib.rs b/server/src/lib.rs index e230e65..f1c7d7e 100644 --- a/server/src/lib.rs +++ b/server/src/lib.rs @@ -67,7 +67,7 @@ pub struct AppState { pub first_names: Vec, pub last_names: Vec, pub reserved_names: RwLock>, - pub user_uuid: RwLock>>>, + pub users_by_id: RwLock>>>, pub online_users: RwLock>>>, pub offline_users: RwLock>>>, pub packs: CardPacks, diff --git a/server/src/main.rs b/server/src/main.rs index ef516df..4848368 100644 --- a/server/src/main.rs +++ b/server/src/main.rs @@ -35,7 +35,7 @@ async fn main() -> Result<()> { let first_names = load_names("data/first.txt")?; let last_names = load_names("data/last.txt")?; let reserved_names = RwLock::new(HashSet::::new()); - let user_uuid = RwLock::new(HashMap::>>::new()); + let users_by_id = RwLock::new(HashMap::>>::new()); let online_users = RwLock::new(HashMap::>>::new()); let offline_users = RwLock::new(HashMap::>>::new()); let (packs, packs_meta) = load_cards_from_json("data/cah-cards-full.json")?; @@ -49,7 +49,7 @@ async fn main() -> Result<()> { first_names, last_names, reserved_names, - user_uuid, + users_by_id, online_users, offline_users, packs, diff --git a/server/src/message_handler.rs b/server/src/message_handler.rs index b4de9cb..c9e6b9e 100644 --- a/server/src/message_handler.rs +++ b/server/src/message_handler.rs @@ -1,6 +1,7 @@ use crate::user_handler::*; use crate::AppState; -use crate::GameHandlerMessage; +use crate::GameHandlerMessage::*; +use crate::UserHandlerMessage::*; use axum::extract::ws::{CloseFrame, Message}; use lib::*; use serde_json::{from_str, to_string}; @@ -37,7 +38,7 @@ impl MessageHandler { { self.state .users_tx - .send(UserHandlerMessage::UserLogIn { + .send(UserLogIn { username: user_log_in.username, addr, }) @@ -48,7 +49,7 @@ impl MessageHandler { _new_game_request if let Ok(new_game) = from_str::(&text) => { self.state .games_tx - .send(GameHandlerMessage::NewGame { addr, new_game }) + .send(NewGame { addr, new_game }) .await .unwrap(); } @@ -56,7 +57,7 @@ impl MessageHandler { _join_game_request if let Ok(join_request) = from_str::(&text) => { self.state .games_tx - .send(GameHandlerMessage::JoinGame { + .send(JoinGame { addr, id: join_request.id, }) @@ -64,6 +65,29 @@ impl MessageHandler { .unwrap(); } + _player_move_request + if let Ok(move_request) = from_str::(&text) => + { + if move_request.card_id == "".to_string() { + tracing::error!("Move request card_id is empty! Ignoring..."); + return; + } + if move_request.game_id == "".to_string() { + tracing::error!("Move request game_id is empty! Ignoring..."); + return; + } else { + self.state + .games_tx + .send(MoveRequest { + card_id: move_request.card_id, + game_id: move_request.game_id, + addr, + }) + .await + .unwrap(); + } + } + _ => tracing::debug!("Unhandled text from {}", addr), }, diff --git a/server/src/user_handler.rs b/server/src/user_handler.rs index df5d5f3..bff23a8 100644 --- a/server/src/user_handler.rs +++ b/server/src/user_handler.rs @@ -88,7 +88,7 @@ impl UserHandler { // Register uuid self.state - .user_uuid + .users_by_id .write() .unwrap() .insert(new_user.read().unwrap().uuid.clone(), new_user.clone());