From d3302478eae5cb6eefe9ad9b1e1438ba79e0b5cd Mon Sep 17 00:00:00 2001 From: Martin Indra Date: Wed, 16 Aug 2023 00:11:27 +0200 Subject: [PATCH] Refactor game name handling in multiplayer menu (#682) --- crates/menu/src/multiplayer/current.rs | 28 ++++++++++++++ crates/menu/src/multiplayer/gamelisting.rs | 7 ++-- crates/menu/src/multiplayer/joining.rs | 45 ++-------------------- crates/menu/src/multiplayer/mod.rs | 8 ++-- crates/menu/src/multiplayer/setup.rs | 6 ++- 5 files changed, 46 insertions(+), 48 deletions(-) create mode 100644 crates/menu/src/multiplayer/current.rs diff --git a/crates/menu/src/multiplayer/current.rs b/crates/menu/src/multiplayer/current.rs new file mode 100644 index 00000000..53034c1d --- /dev/null +++ b/crates/menu/src/multiplayer/current.rs @@ -0,0 +1,28 @@ +use bevy::prelude::*; + +use crate::MenuState; + +pub(super) struct CurrentGamePlugin; + +impl Plugin for CurrentGamePlugin { + fn build(&self, app: &mut App) { + app.add_systems(OnExit(MenuState::Multiplayer), cleanup); + } +} + +#[derive(Resource)] +pub(super) struct GameNameRes(String); + +impl GameNameRes { + pub(super) fn new(name: S) -> Self { + Self(name.to_string()) + } + + pub(super) fn name_owned(&self) -> String { + self.0.to_owned() + } +} + +fn cleanup(mut commands: Commands) { + commands.remove_resource::(); +} diff --git a/crates/menu/src/multiplayer/gamelisting.rs b/crates/menu/src/multiplayer/gamelisting.rs index 1affb7c0..02f98a5c 100644 --- a/crates/menu/src/multiplayer/gamelisting.rs +++ b/crates/menu/src/multiplayer/gamelisting.rs @@ -5,7 +5,7 @@ use de_gui::{ButtonCommands, GuiCommands, LabelCommands, OuterStyle, ToastEvent} use de_lobby_client::{ListGamesRequest, RequestEvent, ResponseEvent}; use de_lobby_model::GamePartial; -use super::{joining::JoinGameEvent, MultiplayerState}; +use super::{current::GameNameRes, MultiplayerState}; use crate::menu::Menu; const REFRESH_INTERVAL: Duration = Duration::from_secs(10); @@ -184,16 +184,17 @@ fn list_games_system( } fn button_system( + mut commands: Commands, mut next_state: ResMut>, interactions: Query<(&Interaction, &ButtonAction), Changed>, - mut joins: EventWriter, ) { for (&interaction, action) in interactions.iter() { if let Interaction::Pressed = interaction { match action { ButtonAction::Create => next_state.set(MultiplayerState::GameCreation), ButtonAction::Join(name) => { - joins.send(JoinGameEvent::new(name.to_owned())); + commands.insert_resource(GameNameRes::new(name)); + next_state.set(MultiplayerState::GameJoining); } } } diff --git a/crates/menu/src/multiplayer/joining.rs b/crates/menu/src/multiplayer/joining.rs index c7aff381..ce5d3b02 100644 --- a/crates/menu/src/multiplayer/joining.rs +++ b/crates/menu/src/multiplayer/joining.rs @@ -7,22 +7,17 @@ use de_multiplayer::{ }; use super::{ + current::GameNameRes, requests::{Receiver, Sender}, MultiplayerState, }; -use crate::MenuState; pub(crate) struct JoiningGamePlugin; impl Plugin for JoiningGamePlugin { fn build(&self, app: &mut App) { - app.add_event::() - .add_systems(OnEnter(MultiplayerState::GameJoining), get_game) + app.add_systems(OnEnter(MultiplayerState::GameJoining), get_game) .add_systems(OnExit(MultiplayerState::GameJoining), cleanup) - .add_systems( - PreUpdate, - handle_join_event.run_if(in_state(MenuState::Multiplayer)), - ) .add_systems( Update, ( @@ -35,49 +30,17 @@ impl Plugin for JoiningGamePlugin { } } -/// Send this event to initiate joining of a multiplayer game. -/// -/// The game will be joined at both DE Lobby and the DE Connector. Once this is -/// done, the menu transitions to [`MultiplayerState::GameJoined`]. -#[derive(Event)] -pub(super) struct JoinGameEvent(String); - -impl JoinGameEvent { - pub(super) fn new(name: String) -> Self { - Self(name) - } -} - -#[derive(Resource)] -pub(crate) struct GameNameRes(String); - -fn handle_join_event( - mut commands: Commands, - mut next_state: ResMut>, - mut events: EventReader, -) { - let Some(event) = events.iter().last() else { - return; - }; - - commands.insert_resource(GameNameRes(event.0.to_owned())); - next_state.set(MultiplayerState::GameJoining); -} - fn cleanup( - mut commands: Commands, state: Res>, mut shutdown: EventWriter, ) { - commands.remove_resource::(); - if state.as_ref() != &MultiplayerState::GameJoined { shutdown.send(ShutdownMultiplayerEvent); } } fn get_game(game_name: Res, mut sender: Sender) { - sender.send(GetGameRequest::new(game_name.0.to_owned())); + sender.send(GetGameRequest::new(game_name.name_owned())); } fn handle_get_response( @@ -113,7 +76,7 @@ fn handle_joined_event( }; sender.send(JoinGameRequest::new( - game_name.0.to_owned(), + game_name.name_owned(), GamePlayerInfo::new(event.player().to_num()), )); } diff --git a/crates/menu/src/multiplayer/mod.rs b/crates/menu/src/multiplayer/mod.rs index badd8275..13533547 100644 --- a/crates/menu/src/multiplayer/mod.rs +++ b/crates/menu/src/multiplayer/mod.rs @@ -6,13 +6,14 @@ use de_lobby_client::{ use de_multiplayer::MultiplayerShuttingDownEvent; use self::{ - create::CreateGamePlugin, gamelisting::GameListingPlugin, joined::JoinedGamePlugin, - joining::JoiningGamePlugin, requests::RequestsPlugin, setup::SetupGamePlugin, - signin::SignInPlugin, + create::CreateGamePlugin, current::CurrentGamePlugin, gamelisting::GameListingPlugin, + joined::JoinedGamePlugin, joining::JoiningGamePlugin, requests::RequestsPlugin, + setup::SetupGamePlugin, signin::SignInPlugin, }; use crate::{menu::ScreenStatePlugin, MenuState}; mod create; +mod current; mod gamelisting; mod joined; mod joining; @@ -32,6 +33,7 @@ impl Plugin for MultiplayerPlugin { RequestsPlugin::::new(), MultiplayerStatePlugin, ScreenStatePlugin::::default(), + CurrentGamePlugin, SignInPlugin, GameListingPlugin, CreateGamePlugin, diff --git a/crates/menu/src/multiplayer/setup.rs b/crates/menu/src/multiplayer/setup.rs index 2f8681e7..3339649a 100644 --- a/crates/menu/src/multiplayer/setup.rs +++ b/crates/menu/src/multiplayer/setup.rs @@ -8,6 +8,7 @@ use de_multiplayer::{ }; use super::{ + current::GameNameRes, requests::{Receiver, Sender}, MultiplayerState, }; @@ -93,6 +94,7 @@ fn setup_network( } fn create_game_in_lobby( + mut commands: Commands, config: Res, mut opened_events: EventReader, mut sender: Sender, @@ -101,7 +103,9 @@ fn create_game_in_lobby( return; }; - let game_setup = GameSetup::new(opened_event.0, config.0.clone()); + let game_config = config.0.clone(); + commands.insert_resource(GameNameRes::new(game_config.name())); + let game_setup = GameSetup::new(opened_event.0, game_config); sender.send(CreateGameRequest::new(game_setup)); }