From 6dd6cfc3b715a25c852df4c20ddb594936f41fd3 Mon Sep 17 00:00:00 2001 From: CoCo_Sol007 Date: Wed, 20 Mar 2024 18:40:35 +0100 Subject: [PATCH] save --- crates/border-wars/src/lib.rs | 2 +- crates/border-wars/src/networkinig/connection.rs | 14 ++++++++++---- crates/border-wars/src/networkinig/mod.rs | 7 +++---- 3 files changed, 14 insertions(+), 9 deletions(-) diff --git a/crates/border-wars/src/lib.rs b/crates/border-wars/src/lib.rs index 2c21c48..c768e2d 100644 --- a/crates/border-wars/src/lib.rs +++ b/crates/border-wars/src/lib.rs @@ -27,7 +27,7 @@ pub enum CurrentScene { } /// A player in the game. -#[derive(Serialize, Deserialize, Clone)] +#[derive(Serialize, Deserialize, Clone, Debug)] pub struct Player { /// The name of the player. pub name: String, diff --git a/crates/border-wars/src/networkinig/connection.rs b/crates/border-wars/src/networkinig/connection.rs index 0d9d4fe..5cbc991 100644 --- a/crates/border-wars/src/networkinig/connection.rs +++ b/crates/border-wars/src/networkinig/connection.rs @@ -13,7 +13,11 @@ impl Plugin for ConnectionPuglin { fn build(&self, app: &mut App) { app.add_network_event::() .add_network_event::() - .add_network_event::(); + .add_network_event::() + .add_systems( + Update, + (accept_connection, handle_change_player).run_if(in_state(CurrentScene::Lobby)), + ); } } @@ -27,7 +31,7 @@ pub struct AddPlayer(Uuid, Player); /// An event that is trigger whene a player is removed. #[derive(Event, Serialize, Deserialize)] -pub struct RemovePlayer(Uuid); +pub struct RemovePlayer(pub Uuid); /// A fonction that handle join request and add it. pub fn accept_connection( @@ -49,6 +53,8 @@ pub fn accept_connection( let new_uuid = request_join.0; let new_name = request_join.1.0.clone(); + add_players.send(SendTo(new_uuid, AddPlayer(new_uuid, new_name.clone()))); + for (uuid, name) in all_players.0.iter() { // Link all players add_players.send(SendTo(*uuid, AddPlayer(new_uuid, new_name.clone()))); @@ -59,8 +65,8 @@ pub fn accept_connection( /// A fonction that handle new / remove players events. pub fn handle_change_player( - mut add_players: EventReader>, - mut remove_players: EventReader>, + mut add_players: EventReader>, + mut remove_players: EventReader>, mut all_players: ResMut, connection: Res, mut next_scene: ResMut>, diff --git a/crates/border-wars/src/networkinig/mod.rs b/crates/border-wars/src/networkinig/mod.rs index 5903459..fd91cba 100644 --- a/crates/border-wars/src/networkinig/mod.rs +++ b/crates/border-wars/src/networkinig/mod.rs @@ -14,7 +14,8 @@ pub struct NetworkingPlugin; impl Plugin for NetworkingPlugin { fn build(&self, app: &mut App) { - app.add_plugins(ConnectionPuglin); + app.add_plugins(ConnectionPuglin) + .init_resource::(); } } @@ -34,9 +35,7 @@ pub enum GameRank { impl AllPlayer { /// Get the player by a connection. pub fn get_by_connection(&self, connection: &Connection) -> Option<&Player> { - let Some(uuid) = connection.identifier() else { - return None; - }; + let uuid = connection.identifier()?; self.0.get(&uuid) }