Skip to content

Commit

Permalink
Refactor game name handling in multiplayer menu (#682)
Browse files Browse the repository at this point in the history
  • Loading branch information
Indy2222 authored Aug 15, 2023
1 parent af7c371 commit d330247
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 48 deletions.
28 changes: 28 additions & 0 deletions crates/menu/src/multiplayer/current.rs
Original file line number Diff line number Diff line change
@@ -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<S: ToString>(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::<GameNameRes>();
}
7 changes: 4 additions & 3 deletions crates/menu/src/multiplayer/gamelisting.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -184,16 +184,17 @@ fn list_games_system(
}

fn button_system(
mut commands: Commands,
mut next_state: ResMut<NextState<MultiplayerState>>,
interactions: Query<(&Interaction, &ButtonAction), Changed<Interaction>>,
mut joins: EventWriter<JoinGameEvent>,
) {
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);
}
}
}
Expand Down
45 changes: 4 additions & 41 deletions crates/menu/src/multiplayer/joining.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::<JoinGameEvent>()
.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,
(
Expand All @@ -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<NextState<MultiplayerState>>,
mut events: EventReader<JoinGameEvent>,
) {
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<State<MultiplayerState>>,
mut shutdown: EventWriter<ShutdownMultiplayerEvent>,
) {
commands.remove_resource::<GameNameRes>();

if state.as_ref() != &MultiplayerState::GameJoined {
shutdown.send(ShutdownMultiplayerEvent);
}
}

fn get_game(game_name: Res<GameNameRes>, mut sender: Sender<GetGameRequest>) {
sender.send(GetGameRequest::new(game_name.0.to_owned()));
sender.send(GetGameRequest::new(game_name.name_owned()));
}

fn handle_get_response(
Expand Down Expand Up @@ -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()),
));
}
Expand Down
8 changes: 5 additions & 3 deletions crates/menu/src/multiplayer/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -32,6 +33,7 @@ impl Plugin for MultiplayerPlugin {
RequestsPlugin::<JoinGameRequest>::new(),
MultiplayerStatePlugin,
ScreenStatePlugin::<MultiplayerState>::default(),
CurrentGamePlugin,
SignInPlugin,
GameListingPlugin,
CreateGamePlugin,
Expand Down
6 changes: 5 additions & 1 deletion crates/menu/src/multiplayer/setup.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use de_multiplayer::{
};

use super::{
current::GameNameRes,
requests::{Receiver, Sender},
MultiplayerState,
};
Expand Down Expand Up @@ -93,6 +94,7 @@ fn setup_network(
}

fn create_game_in_lobby(
mut commands: Commands,
config: Res<GameConfigRes>,
mut opened_events: EventReader<GameOpenedEvent>,
mut sender: Sender<CreateGameRequest>,
Expand All @@ -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));
}

Expand Down

0 comments on commit d330247

Please sign in to comment.