From 4ec28ddd27b73b66383d2fa85868931c25e011e8 Mon Sep 17 00:00:00 2001 From: Sean Sullivan Date: Wed, 14 Aug 2024 20:08:08 -0400 Subject: [PATCH] Add logs and cleanup for debugging --- apps/server/src/main.rs | 15 +++++++++------ apps/web/src/worker.rs | 12 ++++++++---- plugins/active_game/src/lib.rs | 9 +++++++-- plugins/client/src/lib.rs | 16 ++++++++++++++-- plugins/client/src/transport.rs | 2 +- plugins/game/src/action.rs | 9 ++++++++- plugins/game/src/arena.rs | 1 + plugins/game/src/lib.rs | 10 ++++++++++ plugins/server/src/lib.rs | 27 ++++++++++++++++++--------- plugins/server/src/transport.rs | 2 +- 10 files changed, 77 insertions(+), 26 deletions(-) diff --git a/apps/server/src/main.rs b/apps/server/src/main.rs index 5fc9ab7..19845b1 100644 --- a/apps/server/src/main.rs +++ b/apps/server/src/main.rs @@ -5,7 +5,7 @@ use bevy::{ }; use server::ServerPlugin; -use wordfight::WordFightPlugins; +use wordfight::{ActiveGamePlugin, WordFightPlugins}; fn main() { App::default() @@ -14,11 +14,14 @@ fn main() { // need some wait duration so that async tasks are not entirely outcompeted by the main loop std::time::Duration::from_millis(10), ), - WordFightPlugins.build().set(LogPlugin { - filter: "wgpu=error,naga=warn,h3=error".to_string(), - level: Level::INFO, - ..Default::default() - }), + WordFightPlugins + .build() + .set(LogPlugin { + filter: "wgpu=error,naga=warn,h3=error".to_string(), + level: Level::INFO, + ..Default::default() + }) + .disable::(), )) .add_plugins(ServerPlugin { port: option_env!("SERVER_PORT").unwrap_or("7636").to_string(), diff --git a/apps/web/src/worker.rs b/apps/web/src/worker.rs index 48139df..2efb635 100644 --- a/apps/web/src/worker.rs +++ b/apps/web/src/worker.rs @@ -55,8 +55,6 @@ impl Worker for BevyWorker { } fn update(&mut self, scope: &WorkerScope, message: Self::Message) { - #[cfg(feature = "log")] - log("Update".to_string()); if let Some(app) = self.game.as_mut() { let WorkerUpdateMessage::Update = message else { return; @@ -85,9 +83,9 @@ impl Worker for BevyWorker { } fn received(&mut self, _: &WorkerScope, message: Self::Input, id: HandlerId) { - #[cfg(feature = "log")] - log(format!("Message received! {:?}", message)); if let Some(app) = self.game.as_mut() { + #[cfg(feature = "log")] + log(format!("Message received! {:?}", message)); self.subscriptions.insert(id); let action: wordfight::Action = match message { AppMessage::AddLetter(letter) => wordfight::Action::Append(letter), @@ -104,6 +102,12 @@ impl Worker for BevyWorker { .send_event(action.made_by(my_player, wordfight::PlayerSide::Left)); app.update(); + } else { + #[cfg(feature = "log")] + log(format!( + "Discarding message received before app is ready: {:?}", + message + )); } } } diff --git a/plugins/active_game/src/lib.rs b/plugins/active_game/src/lib.rs index 406d995..93f9d50 100644 --- a/plugins/active_game/src/lib.rs +++ b/plugins/active_game/src/lib.rs @@ -19,8 +19,9 @@ impl Plugin for ActiveGamePlugin { } impl ActiveGamePlugin { - fn set_active_game(mut commands: Commands, spawned_games: Query>) { - for game in spawned_games.iter() { + fn set_active_game(mut commands: Commands, spawned_games: Query>) { + if let Some(game) = spawned_games.iter().next() { + info!("Setting active game: {game}"); commands.insert_resource(ActiveGame(game)); } } @@ -32,6 +33,7 @@ impl ActiveGamePlugin { games: Query<(&GamePlayers, &Arena)>, words: Query<(&Word, &Score)>, updated_words: Query<(), Changed>, + updated_scores: Query<(), Changed>, ) { let Some((game, players, arena)) = games .get(active_game.0) @@ -41,6 +43,8 @@ impl ActiveGamePlugin { game.is_changed() || updated_words.contains(players.left) || updated_words.contains(players.right) + || updated_scores.contains(players.left) + || updated_scores.contains(players.right) }) else { return; @@ -64,6 +68,7 @@ impl ActiveGamePlugin { right_word, right_score, }; + info!("Game update triggered: {event:?}"); commands.trigger(event.clone()); events.send(event); } diff --git a/plugins/client/src/lib.rs b/plugins/client/src/lib.rs index 5951aa6..8e212d7 100644 --- a/plugins/client/src/lib.rs +++ b/plugins/client/src/lib.rs @@ -1,12 +1,14 @@ use bevy::{ ecs::world::Command, - prelude::{App, Commands, Plugin, Startup, World}, + log::info, + prelude::{App, Commands, IntoSystem, Plugin, Startup, Update, World}, }; +use bevy_replicon::core::common_conditions as network_conditions; use bevy_replicon::prelude::RepliconChannels; use bevy_replicon_renet2::{ renet2::{ConnectionConfig, RenetClient}, - RenetChannelsExt, + RenetChannelsExt, RepliconRenetClientPlugin, }; mod transport; @@ -19,6 +21,8 @@ pub struct ClientPlugin { impl Plugin for ClientPlugin { fn build(&self, app: &mut App) { + app.add_plugins(RepliconRenetClientPlugin); + #[cfg(target_family = "wasm")] app.add_plugins(transport::ClientTransportPlugin::new( &self.server_origin, @@ -28,6 +32,14 @@ impl Plugin for ClientPlugin { app.add_systems(Startup, |mut commands: Commands| { commands.add(ClientCommand::Connect); }); + app.add_systems( + Update, + network_conditions::client_just_connected.map(|just_connected| { + if just_connected { + info!("Connected!"); + } + }), + ); } } diff --git a/plugins/client/src/transport.rs b/plugins/client/src/transport.rs index 977fd7c..e483ff2 100644 --- a/plugins/client/src/transport.rs +++ b/plugins/client/src/transport.rs @@ -18,7 +18,7 @@ impl ClientTransportPlugin { .unwrap() } - fn url(host: &str, port: &str, server_token: &str) -> Option { + fn _url(host: &str, port: &str, server_token: &str) -> Option { format!("{host}:{port}") .parse::() .map(|url| Self { diff --git a/plugins/game/src/action.rs b/plugins/game/src/action.rs index dc973bd..9cabf43 100644 --- a/plugins/game/src/action.rs +++ b/plugins/game/src/action.rs @@ -22,10 +22,17 @@ impl Action { let test_string = format!("{}{}", word.to_string(), letter.to_string()); if dictionary.is_word_substring(test_string.as_str()) { word.push(*letter); + info!("Added {letter}, making {word}"); + } else { + info!( + "{word}{} is not in the dictionary", + letter.to_string().to_lowercase() + ); } } Delete => { - word.pop(); + let removed_letter = word.pop(); + info!("Removed {removed_letter:?} from {word}"); } } } diff --git a/plugins/game/src/arena.rs b/plugins/game/src/arena.rs index 6e653c7..b9e7c89 100644 --- a/plugins/game/src/arena.rs +++ b/plugins/game/src/arena.rs @@ -84,6 +84,7 @@ impl Arena { } } +#[derive(Debug)] pub enum Strike { Score(PlayerSide), Parry, diff --git a/plugins/game/src/lib.rs b/plugins/game/src/lib.rs index e8eb94e..0346354 100644 --- a/plugins/game/src/lib.rs +++ b/plugins/game/src/lib.rs @@ -61,6 +61,7 @@ impl WordFightGamePlugin { event: action, } in action_events.read() { + info!("Client {client_id:?} took action: {action:?}"); let ActionEvent { action, side, @@ -71,6 +72,7 @@ impl WordFightGamePlugin { }; if **client == *client_id && *player_side == *side { + info!("Action {action:?} applied to {}", word.clone()); action.apply(&mut word, &dictionary); } } @@ -84,11 +86,13 @@ impl WordFightGamePlugin { let Ok([(left_word, _), (right_word, _)]) = players.get_many([game_players.left, game_players.right]) else { + error!("Failed to find players {game_players:?}"); continue; }; let Ok(strike) = arena.strike(left_word, right_word) else { continue; }; + info!("Strike occurred: {strike:?}"); // contact has occurred! // first determine whether anyone gets a point match strike { @@ -98,9 +102,14 @@ impl WordFightGamePlugin { PlayerSide::Right => game_players.right, }; let Ok((_, mut score)) = players.get_mut(winner) else { + error!("Failed to find winner! {winner}"); continue; }; **score += 1; + info!( + "Player {winner} (side {winning_side:?} gains score! Total: {}", + **score + ); } // both parry conditions result in no score change Strike::OverRange | Strike::Parry => {} @@ -200,6 +209,7 @@ impl SpawnGame { .id(); commands.entity(player_one).insert(InGame(game)); commands.entity(player_two).insert(InGame(game)); + info!("Spawned game {game} with players {player_one}, {player_two}"); } } diff --git a/plugins/server/src/lib.rs b/plugins/server/src/lib.rs index d0fb574..cac19fd 100644 --- a/plugins/server/src/lib.rs +++ b/plugins/server/src/lib.rs @@ -1,12 +1,15 @@ -use bevy::prelude::{ - App, Commands, Entity, EventReader, IntoSystemConfigs, Name, Plugin, Query, Res, ResMut, - Startup, Update, With, Without, +use bevy::{ + log::info, + prelude::{ + App, Commands, Entity, EventReader, IntoSystemConfigs, Name, Plugin, Query, Res, ResMut, + Startup, Update, With, Without, + }, }; use bevy_replicon::prelude::{ConnectedClients, Replicated, RepliconChannels, ServerEvent}; use bevy_replicon_renet2::{ renet2::{ConnectionConfig, RenetServer}, - RenetChannelsExt, + RenetChannelsExt, RepliconRenetServerPlugin, }; use game::{Client, InGame, SpawnGame}; @@ -21,6 +24,8 @@ pub struct ServerPlugin { impl Plugin for ServerPlugin { fn build(&self, app: &mut App) { + app.add_plugins(RepliconRenetServerPlugin); + app.add_plugins(ServerTransportPlugin { port: self.port.clone(), wt_tokens_port: self.wt_tokens_port.clone(), @@ -42,6 +47,11 @@ impl ServerPlugin { let server_channels_config = replicon_channels.get_server_configs(); let client_channels_config = replicon_channels.get_client_configs(); + info!( + "Starting server! Server channels: {} | Client channels: {}", + server_channels_config.len(), + client_channels_config.len() + ); let server = RenetServer::new(ConnectionConfig { server_channels_config, client_channels_config, @@ -54,9 +64,10 @@ impl ServerPlugin { for [client1, client2] in clients .iter() .collect::>() - .chunks(2) + .chunks_exact(2) .map(|chunk| [chunk[0], chunk[1]]) { + info!("Found match: {client1} + {client2}"); commands.trigger(SpawnGame::new(7, client1, client2)); } } @@ -69,8 +80,7 @@ impl ServerPlugin { for event in server_events.read() { match event { ServerEvent::ClientConnected { client_id } => { - #[cfg(feature = "log")] - bevy_log::info!("Player {} connected.", client_id.get()); + info!("Player {} connected.", client_id.get()); // Spawn new player entity commands.spawn(( Replicated, @@ -82,8 +92,7 @@ impl ServerPlugin { if let Some((player_entity, _)) = clients.iter().find(|(_, id)| ***id == *client_id) { - #[cfg(feature = "log")] - bevy_log::debug!("Player disconnected: {}", reason); + info!("Player disconnected: {}", reason); commands.entity(player_entity).despawn(); } } diff --git a/plugins/server/src/transport.rs b/plugins/server/src/transport.rs index babb83c..be6b27e 100644 --- a/plugins/server/src/transport.rs +++ b/plugins/server/src/transport.rs @@ -28,7 +28,7 @@ struct NativeServerTransportPlugin { } impl NativeServerTransportPlugin { - fn url(host: &str, port: &str, tokens_port: &str) -> Self { + fn _url(host: &str, port: &str, tokens_port: &str) -> Self { Self { server_address: WebServerDestination::Url(format!("{host}:{port}").parse().unwrap()), tokens_address: WebServerDestination::Url(