Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
Indy2222 committed Aug 15, 2023
1 parent d5c34eb commit 3b14421
Show file tree
Hide file tree
Showing 6 changed files with 109 additions and 8 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion crates/map/src/hash.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ impl MapHash {
}

/// Constructs the map hash from a hexadecimal string.
pub(crate) fn from_hex(hex: &str) -> Result<Self, HexError> {
pub fn from_hex(hex: &str) -> Result<Self, HexError> {
if hex.len() != 64 {
return Err(HexError::InvalidLenError);
}
Expand Down
1 change: 1 addition & 0 deletions crates/menu/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ de_lobby_client.workspace = true
de_lobby_model.workspace = true
de_map.workspace = true
de_multiplayer.workspace = true
de_net.workspace = true

# Other
async-std.workspace = true
Expand Down
102 changes: 97 additions & 5 deletions crates/menu/src/multiplayer/joined.rs
Original file line number Diff line number Diff line change
@@ -1,19 +1,111 @@
use bevy::prelude::*;
use de_core::state::AppState;
use de_multiplayer::ShutdownMultiplayerEvent;
use de_core::{
assets::asset_path,
gconfig::{GameConfig, LocalPlayers},
player::Player,
state::AppState,
};
use de_gui::ToastEvent;
use de_lobby_client::GetGameRequest;
use de_lobby_model::GameConfig as GameConfigModel;
use de_map::hash::MapHash;
use de_multiplayer::{GameReadinessEvent, ShutdownMultiplayerEvent};
use de_net::Readiness;

use super::MultiplayerState;
use super::{
current::GameNameRes,
requests::{Receiver, Sender},
MultiplayerState,
};

pub(crate) struct JoinedGamePlugin;

impl Plugin for JoinedGamePlugin {
fn build(&self, app: &mut App) {
app.add_systems(OnExit(MultiplayerState::GameJoined), cleanup);
app.add_systems(OnExit(MultiplayerState::GameJoined), cleanup)
.add_systems(
Update,
(
handle_readiness.run_if(on_event::<GameReadinessEvent>()),
handle_get_response,
)
.run_if(in_state(MultiplayerState::GameJoined)),
);
}
}

fn cleanup(state: Res<State<AppState>>, mut shutdown: EventWriter<ShutdownMultiplayerEvent>) {
#[derive(Resource)]
struct GameConfigRes(GameConfigModel);

fn cleanup(
mut commands: Commands,
state: Res<State<AppState>>,
mut shutdown: EventWriter<ShutdownMultiplayerEvent>,
) {
commands.remove_resource::<GameConfigRes>();

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

fn handle_readiness(
mut events: EventReader<GameReadinessEvent>,
game_name: Res<GameNameRes>,
mut sender: Sender<GetGameRequest>,
) {
if events.iter().all(|e| **e != Readiness::Ready) {
return;
}

sender.send(GetGameRequest::new(game_name.name_owned()));
}

fn handle_get_response(
mut commands: Commands,
mut multi_state: ResMut<NextState<MultiplayerState>>,
mut app_state: ResMut<NextState<AppState>>,
mut receiver: Receiver<GetGameRequest>,
mut toasts: EventWriter<ToastEvent>,
) {
while let Some(result) = receiver.receive() {
match result {
Ok(game) => {
let game_conf = game.setup().config();
let map_path = match MapHash::from_hex(game_conf.map().hash()) {
Ok(hash) => hash.construct_path(asset_path("maps")),
Err(error) => {
toasts.send(ToastEvent::new(error));
multi_state.set(MultiplayerState::SignIn);
return;
}
};

// TODO players may be sparse?
let max_player = match Player::try_from(game_conf.max_players()) {
Ok(player) => player,
Err(error) => {
toasts.send(ToastEvent::new(format!(
"Game max players err: {:?}",
error
)));
multi_state.set(MultiplayerState::SignIn);
return;
}
};

commands.insert_resource(GameConfig::new(
map_path,
max_player,
// TODO use real player
LocalPlayers::new(Player::Player1),
));
app_state.set(AppState::InGame);
}
Err(error) => {
toasts.send(ToastEvent::new(error));
multi_state.set(MultiplayerState::SignIn);
}
}
}
}
9 changes: 8 additions & 1 deletion crates/multiplayer/src/game.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use std::net::SocketAddr;

use bevy::prelude::*;
use de_core::{player::Player, schedule::PreMovement};
use de_net::{FromGame, FromServer, GameOpenError, JoinError, ToGame, ToServer};
use de_net::{FromGame, FromServer, GameOpenError, JoinError, Readiness, ToGame, ToServer};

use crate::{
config::ConnectionType,
Expand All @@ -20,6 +20,7 @@ impl Plugin for GamePlugin {
fn build(&self, app: &mut App) {
app.add_event::<GameOpenedEvent>()
.add_event::<GameJoinedEvent>()
.add_event::<GameReadinessEvent>()
.add_systems(OnEnter(NetState::Connected), open_or_join)
.add_systems(
PreMovement,
Expand Down Expand Up @@ -56,6 +57,10 @@ impl GameJoinedEvent {
}
}

/// This event is sent when game readiness stage of the joined game changes.
#[derive(Event, Deref)]
pub struct GameReadinessEvent(Readiness);

fn open_or_join(
conf: Res<NetGameConfRes>,
mut main_server: EventWriter<ToMainServerEvent>,
Expand Down Expand Up @@ -118,6 +123,7 @@ fn process_from_game(
mut fatals: EventWriter<FatalErrorEvent>,
state: Res<State<NetState>>,
mut joined_events: EventWriter<GameJoinedEvent>,
mut readiness_events: EventWriter<GameReadinessEvent>,
mut next_state: ResMut<NextState<NetState>>,
) {
for event in inputs.iter() {
Expand Down Expand Up @@ -175,6 +181,7 @@ fn process_from_game(
}
FromGame::GameReadiness(readiness) => {
info!("Game readiness changed to: {readiness:?}");
readiness_events.send(GameReadinessEvent(*readiness));
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion crates/multiplayer/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ use stats::StatsPlugin;

pub use crate::{
config::{ConnectionType, NetGameConf},
game::{GameJoinedEvent, GameOpenedEvent},
game::{GameJoinedEvent, GameOpenedEvent, GameReadinessEvent},
lifecycle::{MultiplayerShuttingDownEvent, ShutdownMultiplayerEvent, StartMultiplayerEvent},
netstate::NetState,
};
Expand Down

0 comments on commit 3b14421

Please sign in to comment.