From 56cff5d69e6b6eceb58f01b4ce6b0d1147754faf Mon Sep 17 00:00:00 2001 From: CoCo_Sol007 Date: Wed, 20 Mar 2024 16:15:53 +0100 Subject: [PATCH 01/12] save --- Cargo.lock | 10 ++- crates/border-wars/Cargo.toml | 2 + crates/border-wars/src/lib.rs | 7 ++ crates/border-wars/src/main.rs | 4 + .../border-wars/src/networkinig/connection.rs | 79 +++++++++++++++++++ crates/border-wars/src/networkinig/mod.rs | 30 +++++++ 6 files changed, 128 insertions(+), 4 deletions(-) create mode 100644 crates/border-wars/src/networkinig/connection.rs create mode 100644 crates/border-wars/src/networkinig/mod.rs diff --git a/Cargo.lock b/Cargo.lock index 6ac8535..ee4f5e6 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1314,10 +1314,12 @@ dependencies = [ name = "border-wars" version = "0.1.0" dependencies = [ + "bevnet", "bevy", "bevy_egui", "noise", "paste", + "serde", ] [[package]] @@ -3805,18 +3807,18 @@ dependencies = [ [[package]] name = "serde" -version = "1.0.196" +version = "1.0.197" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "870026e60fa08c69f064aa766c10f10b1d62db9ccd4d0abb206472bee0ce3b32" +checksum = "3fb1c873e1b9b056a4dc4c0c198b24c3ffa059243875552b2bd0933b1aee4ce2" dependencies = [ "serde_derive", ] [[package]] name = "serde_derive" -version = "1.0.196" +version = "1.0.197" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "33c85360c95e7d137454dc81d9a4ed2b8efd8fbe19cee57357b32b9771fccb67" +checksum = "7eb0b34b42edc17f6b7cac84a52a1c5f0e1bb2227e997ca9011ea3dd34e8610b" dependencies = [ "proc-macro2", "quote", diff --git a/crates/border-wars/Cargo.toml b/crates/border-wars/Cargo.toml index fc84f96..fabf283 100644 --- a/crates/border-wars/Cargo.toml +++ b/crates/border-wars/Cargo.toml @@ -15,3 +15,5 @@ bevy = "0.12.1" bevy_egui = "0.24.0" noise = "0.8.2" paste = "1.0.14" +bevnet = { path = "../bevnet" } +serde = "1.0.197" diff --git a/crates/border-wars/src/lib.rs b/crates/border-wars/src/lib.rs index 257aefb..ca0b1aa 100644 --- a/crates/border-wars/src/lib.rs +++ b/crates/border-wars/src/lib.rs @@ -1,9 +1,12 @@ //! The file that contains utility functions, enums, structs for the game. +use bevnet::Uuid; use bevy::prelude::*; +use bevy::utils::HashMap; pub mod camera; pub mod map; +pub mod networkinig; pub mod responsive_scale; pub mod scenes; @@ -20,3 +23,7 @@ pub enum CurrentScene { /// When we play this wonderful game. Game, } + +/// TODO +#[derive(Resource, Default)] +pub struct AllPlayer(pub HashMap); diff --git a/crates/border-wars/src/main.rs b/crates/border-wars/src/main.rs index cc2c72e..a999828 100644 --- a/crates/border-wars/src/main.rs +++ b/crates/border-wars/src/main.rs @@ -1,9 +1,11 @@ //! The main entry point of the game. +use bevnet::NetworkPlugin; use bevy::prelude::*; use border_wars::camera::CameraPlugin; use border_wars::map::click_tile::TilesClickable; use border_wars::map::renderer::RendererPlugin; +use border_wars::networkinig::NetworkingPlugin; use border_wars::scenes::ScenesPlugin; fn main() { @@ -13,5 +15,7 @@ fn main() { .add_plugins(RendererPlugin) .add_plugins(CameraPlugin) .add_plugins(TilesClickable) + .add_plugins(NetworkingPlugin) + .add_plugins(NetworkPlugin::new("relay.cocosol.fr".to_string())) .run(); } diff --git a/crates/border-wars/src/networkinig/connection.rs b/crates/border-wars/src/networkinig/connection.rs new file mode 100644 index 0000000..f887099 --- /dev/null +++ b/crates/border-wars/src/networkinig/connection.rs @@ -0,0 +1,79 @@ +//! All the code related to the connection . + +use bevnet::{Connection, NetworkAppExt, Receive, SendTo, Uuid}; +use bevy::prelude::*; +use serde::{Deserialize, Serialize}; + +use super::GameRank; +use crate::{AllPlayer, CurrentScene}; + +/// A plugin that handle, send or remove connections. +pub struct ConnectionPuglin; + +impl Plugin for ConnectionPuglin { + fn build(&self, app: &mut App) { + app.add_network_event::() + .add_network_event::() + .add_network_event::(); + } +} + +/// An event that is trigger whene a new player request to join a game. +#[derive(Event, Serialize, Deserialize)] +pub struct RequestJoin(pub String); + +/// An event that is trigger whene a new player is added. +#[derive(Event, Serialize, Deserialize)] +pub struct AddPlayer(Uuid, String); + +/// An event that is trigger whene a player is removed. +#[derive(Event, Serialize, Deserialize)] +pub struct RemovePlayer(Uuid); + +/// A fonction that handle join request and add it. +pub fn accept_connection( + all_players: Res, + mut requests_join: EventReader>, + game_rank: Res, + mut add_players: EventWriter>, +) { + if *game_rank != GameRank::Admin { + return; + } + for request_join in requests_join.read() { + let new_uuid = request_join.0; + let new_name = request_join.1.0.clone(); + + for (uuid, name) in all_players.0.iter() { + // Link all players + add_players.send(SendTo(*uuid, AddPlayer(new_uuid, new_name.clone()))); + add_players.send(SendTo(new_uuid, AddPlayer(*uuid, name.clone()))); + } + } +} + +/// A fonction that handle new / remove players events. +pub fn handle_change_player( + mut add_players: EventReader>, + mut remove_players: EventReader>, + mut all_players: ResMut, + connection: Res, + mut next_scene: ResMut>, +) { + let Some(connection) = connection.identifier() else { + return; + }; + + for add_player in add_players.read() { + all_players.0.insert(add_player.1.0, add_player.1.1.clone()); + } + + for remove_player in remove_players.read() { + if remove_player.1.0 == connection { + all_players.0.clear(); + next_scene.set(CurrentScene::Menu); + return; + } + all_players.0.remove(&remove_player.1.0); + } +} diff --git a/crates/border-wars/src/networkinig/mod.rs b/crates/border-wars/src/networkinig/mod.rs new file mode 100644 index 0000000..2359d7b --- /dev/null +++ b/crates/border-wars/src/networkinig/mod.rs @@ -0,0 +1,30 @@ +//! TODO + +use bevy::prelude::*; + +use self::connection::ConnectionPuglin; + +pub mod connection; + +/// TODO +pub struct NetworkingPlugin; + +impl Plugin for NetworkingPlugin { + fn build(&self, app: &mut App) { + app.add_plugins(ConnectionPuglin) + .insert_resource(GameRank::Player); + } +} + +/// TODO +#[derive(Resource, PartialEq, Eq)] +pub enum GameRank { + /// TODO + Spectator, + + /// TODO + Admin, + + /// TODO + Player, +} -- 2.43.4 From 568d69a25060ab0196091fbda7ac2a8c82866c94 Mon Sep 17 00:00:00 2001 From: CoCo_Sol007 Date: Wed, 20 Mar 2024 17:30:52 +0100 Subject: [PATCH 02/12] save --- crates/border-wars/src/lib.rs | 16 +++++++- .../border-wars/src/networkinig/connection.rs | 17 ++++++--- crates/border-wars/src/networkinig/mod.rs | 38 ++++++++++++++----- 3 files changed, 54 insertions(+), 17 deletions(-) diff --git a/crates/border-wars/src/lib.rs b/crates/border-wars/src/lib.rs index ca0b1aa..2c21c48 100644 --- a/crates/border-wars/src/lib.rs +++ b/crates/border-wars/src/lib.rs @@ -3,6 +3,8 @@ use bevnet::Uuid; use bevy::prelude::*; use bevy::utils::HashMap; +use networkinig::GameRank; +use serde::{Deserialize, Serialize}; pub mod camera; pub mod map; @@ -24,6 +26,16 @@ pub enum CurrentScene { Game, } -/// TODO +/// A player in the game. +#[derive(Serialize, Deserialize, Clone)] +pub struct Player { + /// The name of the player. + pub name: String, + + /// The rank of the player. + pub rank: GameRank, +} + +/// All the players in the game. #[derive(Resource, Default)] -pub struct AllPlayer(pub HashMap); +pub struct AllPlayer(pub HashMap); diff --git a/crates/border-wars/src/networkinig/connection.rs b/crates/border-wars/src/networkinig/connection.rs index f887099..0d9d4fe 100644 --- a/crates/border-wars/src/networkinig/connection.rs +++ b/crates/border-wars/src/networkinig/connection.rs @@ -4,8 +4,7 @@ use bevnet::{Connection, NetworkAppExt, Receive, SendTo, Uuid}; use bevy::prelude::*; use serde::{Deserialize, Serialize}; -use super::GameRank; -use crate::{AllPlayer, CurrentScene}; +use crate::{AllPlayer, CurrentScene, Player}; /// A plugin that handle, send or remove connections. pub struct ConnectionPuglin; @@ -20,11 +19,11 @@ impl Plugin for ConnectionPuglin { /// An event that is trigger whene a new player request to join a game. #[derive(Event, Serialize, Deserialize)] -pub struct RequestJoin(pub String); +pub struct RequestJoin(pub Player); /// An event that is trigger whene a new player is added. #[derive(Event, Serialize, Deserialize)] -pub struct AddPlayer(Uuid, String); +pub struct AddPlayer(Uuid, Player); /// An event that is trigger whene a player is removed. #[derive(Event, Serialize, Deserialize)] @@ -34,12 +33,18 @@ pub struct RemovePlayer(Uuid); pub fn accept_connection( all_players: Res, mut requests_join: EventReader>, - game_rank: Res, mut add_players: EventWriter>, + connection: Res, ) { - if *game_rank != GameRank::Admin { + // Check if the player is an admin + if all_players + .get_by_connection(&connection) + .map(|player| player.is_admin()) + != Some(true) + { return; } + for request_join in requests_join.read() { let new_uuid = request_join.0; let new_name = request_join.1.0.clone(); diff --git a/crates/border-wars/src/networkinig/mod.rs b/crates/border-wars/src/networkinig/mod.rs index 2359d7b..5903459 100644 --- a/crates/border-wars/src/networkinig/mod.rs +++ b/crates/border-wars/src/networkinig/mod.rs @@ -1,30 +1,50 @@ -//! TODO +//! All the code related to the networking. +use bevnet::Connection; use bevy::prelude::*; +use serde::{Deserialize, Serialize}; use self::connection::ConnectionPuglin; +use crate::{AllPlayer, Player}; pub mod connection; -/// TODO +/// The plugin for the networking. pub struct NetworkingPlugin; impl Plugin for NetworkingPlugin { fn build(&self, app: &mut App) { - app.add_plugins(ConnectionPuglin) - .insert_resource(GameRank::Player); + app.add_plugins(ConnectionPuglin); } } -/// TODO -#[derive(Resource, PartialEq, Eq)] +/// The rank of the player. +#[derive(PartialEq, Eq, Serialize, Deserialize, Clone, Copy, Debug)] pub enum GameRank { - /// TODO + /// The player is a spectator. He does not play the game. Spectator, - /// TODO + /// The player is an admin. He can remove players. Admin, - /// TODO + /// The player is a player. He can join the game and play. Player, } + +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; + }; + + self.0.get(&uuid) + } +} + +impl Player { + /// Create a new player. + pub fn is_admin(&self) -> bool { + self.rank == GameRank::Admin + } +} -- 2.43.4 From 6dd6cfc3b715a25c852df4c20ddb594936f41fd3 Mon Sep 17 00:00:00 2001 From: CoCo_Sol007 Date: Wed, 20 Mar 2024 18:40:35 +0100 Subject: [PATCH 03/12] 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) } -- 2.43.4 From 4401b64300092d7922e9466f5943de2c5bcd174c Mon Sep 17 00:00:00 2001 From: CoCo_Sol007 Date: Thu, 21 Mar 2024 09:09:54 +0100 Subject: [PATCH 04/12] save --- crates/border-wars/src/scenes/lobby.rs | 34 +++++++++++++++++++--- crates/border-wars/src/scenes/menu.rs | 40 +++++++++++++++++++++++--- crates/relay-client/src/lib.rs | 3 +- 3 files changed, 68 insertions(+), 9 deletions(-) diff --git a/crates/border-wars/src/scenes/lobby.rs b/crates/border-wars/src/scenes/lobby.rs index 84a0348..7181767 100644 --- a/crates/border-wars/src/scenes/lobby.rs +++ b/crates/border-wars/src/scenes/lobby.rs @@ -1,9 +1,12 @@ //! The lobby of the game. +use bevnet::{Connection, SendTo}; use bevy::prelude::*; use bevy_egui::{egui, EguiContexts}; -use crate::CurrentScene; +use crate::networkinig::connection::RemovePlayer; +use crate::networkinig::GameRank; +use crate::{AllPlayer, CurrentScene}; /// The plugin for the lobby. pub struct LobbyPlugin; @@ -15,7 +18,13 @@ impl Plugin for LobbyPlugin { } /// Display the UI of the lobby. -fn lobby_ui(mut ctx: EguiContexts, mut next_scene: ResMut>) { +fn lobby_ui( + mut ctx: EguiContexts, + mut next_scene: ResMut>, + connection: Res, + all_players: Res, + mut kick_player: EventWriter>, +) { egui::CentralPanel::default().show(ctx.ctx_mut(), |ui| { ui.heading("Border Wars"); @@ -23,14 +32,31 @@ fn lobby_ui(mut ctx: EguiContexts, mut next_scene: ResMut, mut next_scene: ResMut>, + mut request_join: EventWriter>, + mut name: Local, + mut all_player: ResMut, + connection: Res, ) { + let Some(uuid) = connection.identifier() else { + return; + }; + egui::CentralPanel::default().show(ctx.ctx_mut(), |ui| { ui.heading("Border Wars"); ui.separator(); + ui.label("Name"); + ui.text_edit_singleline(&mut *name); + + ui.separator(); + ui.label("Connect to an existing game:"); ui.horizontal(|ui| { ui.label("Game ID: "); ui.text_edit_singleline(&mut *connection_string); + let Ok(game_id) = Uuid::parse_str(&connection_string) else { + return; + }; + if ui.button("Join").clicked() { - next_scene.set(CurrentScene::Game); - // TODO: connect to the game + next_scene.set(CurrentScene::Lobby); + request_join.send(SendTo( + game_id, + RequestJoin(Player { + name: name.clone(), + rank: GameRank::Player, + }), + )); } }); @@ -39,7 +65,13 @@ fn menu_ui( if ui.button("Create new game").clicked() { next_scene.set(CurrentScene::Lobby); - // TODO: create a new game + all_player.0.insert( + uuid, + Player { + name: name.clone(), + rank: GameRank::Admin, + }, + ); } }); } diff --git a/crates/relay-client/src/lib.rs b/crates/relay-client/src/lib.rs index b052784..05eec18 100644 --- a/crates/relay-client/src/lib.rs +++ b/crates/relay-client/src/lib.rs @@ -80,7 +80,8 @@ impl Connection { path.push(".relay-data"); // Check if the file exists. - match path.exists() { + match false { + // path.exists() true => { // Read the file and parse the identifier and secret key. let contents = fs::read(&path)?; -- 2.43.4 From e4ead79cb1a16bbfe891db35c21adfca7933c9dc Mon Sep 17 00:00:00 2001 From: CoCo_Sol007 Date: Thu, 21 Mar 2024 09:24:37 +0100 Subject: [PATCH 05/12] save --- crates/relay-client/src/lib.rs | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/crates/relay-client/src/lib.rs b/crates/relay-client/src/lib.rs index 05eec18..b052784 100644 --- a/crates/relay-client/src/lib.rs +++ b/crates/relay-client/src/lib.rs @@ -80,8 +80,7 @@ impl Connection { path.push(".relay-data"); // Check if the file exists. - match false { - // path.exists() + match path.exists() { true => { // Read the file and parse the identifier and secret key. let contents = fs::read(&path)?; -- 2.43.4 From 4fe4bdba3f7dd558dfed0f1e198aa19ef63144e2 Mon Sep 17 00:00:00 2001 From: CoCo_Sol007 Date: Thu, 21 Mar 2024 09:26:24 +0100 Subject: [PATCH 06/12] rename --- crates/border-wars/src/lib.rs | 2 +- crates/border-wars/src/networkinig/connection.rs | 6 +++--- crates/border-wars/src/networkinig/mod.rs | 6 +++--- crates/border-wars/src/scenes/lobby.rs | 4 ++-- crates/border-wars/src/scenes/menu.rs | 4 ++-- 5 files changed, 11 insertions(+), 11 deletions(-) diff --git a/crates/border-wars/src/lib.rs b/crates/border-wars/src/lib.rs index c768e2d..2d48358 100644 --- a/crates/border-wars/src/lib.rs +++ b/crates/border-wars/src/lib.rs @@ -38,4 +38,4 @@ pub struct Player { /// All the players in the game. #[derive(Resource, Default)] -pub struct AllPlayer(pub HashMap); +pub struct AllPlayers(pub HashMap); diff --git a/crates/border-wars/src/networkinig/connection.rs b/crates/border-wars/src/networkinig/connection.rs index 5cbc991..19be805 100644 --- a/crates/border-wars/src/networkinig/connection.rs +++ b/crates/border-wars/src/networkinig/connection.rs @@ -4,7 +4,7 @@ use bevnet::{Connection, NetworkAppExt, Receive, SendTo, Uuid}; use bevy::prelude::*; use serde::{Deserialize, Serialize}; -use crate::{AllPlayer, CurrentScene, Player}; +use crate::{AllPlayers, CurrentScene, Player}; /// A plugin that handle, send or remove connections. pub struct ConnectionPuglin; @@ -35,7 +35,7 @@ pub struct RemovePlayer(pub Uuid); /// A fonction that handle join request and add it. pub fn accept_connection( - all_players: Res, + all_players: Res, mut requests_join: EventReader>, mut add_players: EventWriter>, connection: Res, @@ -67,7 +67,7 @@ pub fn accept_connection( pub fn handle_change_player( mut add_players: EventReader>, mut remove_players: EventReader>, - mut all_players: ResMut, + 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 fd91cba..7314019 100644 --- a/crates/border-wars/src/networkinig/mod.rs +++ b/crates/border-wars/src/networkinig/mod.rs @@ -5,7 +5,7 @@ use bevy::prelude::*; use serde::{Deserialize, Serialize}; use self::connection::ConnectionPuglin; -use crate::{AllPlayer, Player}; +use crate::{AllPlayers, Player}; pub mod connection; @@ -15,7 +15,7 @@ pub struct NetworkingPlugin; impl Plugin for NetworkingPlugin { fn build(&self, app: &mut App) { app.add_plugins(ConnectionPuglin) - .init_resource::(); + .init_resource::(); } } @@ -32,7 +32,7 @@ pub enum GameRank { Player, } -impl AllPlayer { +impl AllPlayers { /// Get the player by a connection. pub fn get_by_connection(&self, connection: &Connection) -> Option<&Player> { let uuid = connection.identifier()?; diff --git a/crates/border-wars/src/scenes/lobby.rs b/crates/border-wars/src/scenes/lobby.rs index 7181767..5eadd41 100644 --- a/crates/border-wars/src/scenes/lobby.rs +++ b/crates/border-wars/src/scenes/lobby.rs @@ -6,7 +6,7 @@ use bevy_egui::{egui, EguiContexts}; use crate::networkinig::connection::RemovePlayer; use crate::networkinig::GameRank; -use crate::{AllPlayer, CurrentScene}; +use crate::{AllPlayers, CurrentScene}; /// The plugin for the lobby. pub struct LobbyPlugin; @@ -22,7 +22,7 @@ fn lobby_ui( mut ctx: EguiContexts, mut next_scene: ResMut>, connection: Res, - all_players: Res, + all_players: Res, mut kick_player: EventWriter>, ) { egui::CentralPanel::default().show(ctx.ctx_mut(), |ui| { diff --git a/crates/border-wars/src/scenes/menu.rs b/crates/border-wars/src/scenes/menu.rs index fe29083..7a2bf0b 100644 --- a/crates/border-wars/src/scenes/menu.rs +++ b/crates/border-wars/src/scenes/menu.rs @@ -6,7 +6,7 @@ use bevy_egui::{egui, EguiContexts}; use crate::networkinig::connection::RequestJoin; use crate::networkinig::GameRank; -use crate::{AllPlayer, CurrentScene, Player}; +use crate::{AllPlayers, CurrentScene, Player}; /// The plugin for the menu. pub struct MenuPlugin; @@ -23,7 +23,7 @@ fn menu_ui( mut next_scene: ResMut>, mut request_join: EventWriter>, mut name: Local, - mut all_player: ResMut, + mut all_player: ResMut, connection: Res, ) { let Some(uuid) = connection.identifier() else { -- 2.43.4 From cd8b181f1d56f439af83a763111bb5347012d88d Mon Sep 17 00:00:00 2001 From: CoCo_Sol007 Date: Thu, 21 Mar 2024 09:28:30 +0100 Subject: [PATCH 07/12] opti de la mort qui tue --- crates/border-wars/src/main.rs | 2 -- crates/border-wars/src/networkinig/mod.rs | 5 +++-- 2 files changed, 3 insertions(+), 4 deletions(-) diff --git a/crates/border-wars/src/main.rs b/crates/border-wars/src/main.rs index 3534c66..98de956 100644 --- a/crates/border-wars/src/main.rs +++ b/crates/border-wars/src/main.rs @@ -1,6 +1,5 @@ //! The main entry point of the game. -use bevnet::NetworkPlugin; use bevy::prelude::*; use border_wars::camera::CameraPlugin; use border_wars::map::renderer::RendererPlugin; @@ -16,6 +15,5 @@ fn main() { .add_plugins(CameraPlugin) .add_plugins(SelectTilePlugin) .add_plugins(NetworkingPlugin) - .add_plugins(NetworkPlugin::new("relay.cocosol.fr".to_string())) .run(); } diff --git a/crates/border-wars/src/networkinig/mod.rs b/crates/border-wars/src/networkinig/mod.rs index 7314019..30dd552 100644 --- a/crates/border-wars/src/networkinig/mod.rs +++ b/crates/border-wars/src/networkinig/mod.rs @@ -1,6 +1,6 @@ //! All the code related to the networking. -use bevnet::Connection; +use bevnet::{Connection, NetworkPlugin}; use bevy::prelude::*; use serde::{Deserialize, Serialize}; @@ -14,7 +14,8 @@ pub struct NetworkingPlugin; impl Plugin for NetworkingPlugin { fn build(&self, app: &mut App) { - app.add_plugins(ConnectionPuglin) + app.add_plugins(NetworkPlugin::new("relay.cocosol.fr".to_string())) + .add_plugins(ConnectionPuglin) .init_resource::(); } } -- 2.43.4 From 49ee6daa8ed24087315d2cdb39cf447fb1fed3c5 Mon Sep 17 00:00:00 2001 From: CoCo_Sol007 Date: Thu, 21 Mar 2024 09:53:11 +0100 Subject: [PATCH 08/12] SAVE --- .../border-wars/src/networkinig/connection.rs | 29 ++++++++++++------- 1 file changed, 18 insertions(+), 11 deletions(-) diff --git a/crates/border-wars/src/networkinig/connection.rs b/crates/border-wars/src/networkinig/connection.rs index 19be805..1db57cd 100644 --- a/crates/border-wars/src/networkinig/connection.rs +++ b/crates/border-wars/src/networkinig/connection.rs @@ -1,12 +1,13 @@ -//! All the code related to the connection . +//! All the code related to the connection. use bevnet::{Connection, NetworkAppExt, Receive, SendTo, Uuid}; use bevy::prelude::*; use serde::{Deserialize, Serialize}; +use super::GameRank; use crate::{AllPlayers, CurrentScene, Player}; -/// A plugin that handle, send or remove connections. +/// A plugin that manage connections (add, remove). pub struct ConnectionPuglin; impl Plugin for ConnectionPuglin { @@ -14,10 +15,7 @@ impl Plugin for ConnectionPuglin { app.add_network_event::() .add_network_event::() .add_network_event::() - .add_systems( - Update, - (accept_connection, handle_change_player).run_if(in_state(CurrentScene::Lobby)), - ); + .add_systems(Update, (accept_connection, handle_change_player)); } } @@ -39,6 +37,7 @@ pub fn accept_connection( mut requests_join: EventReader>, mut add_players: EventWriter>, connection: Res, + state: Res>, ) { // Check if the player is an admin if all_players @@ -51,14 +50,22 @@ pub fn accept_connection( for request_join in requests_join.read() { let new_uuid = request_join.0; - let new_name = request_join.1.0.clone(); + let mut new_player = request_join.1.0.clone(); - add_players.send(SendTo(new_uuid, AddPlayer(new_uuid, new_name.clone()))); + let current_state = *state.get(); - for (uuid, name) in all_players.0.iter() { + if current_state == CurrentScene::Menu { + return; + } else if current_state == CurrentScene::Game { + new_player.rank = GameRank::Spectator; + } + + add_players.send(SendTo(new_uuid, AddPlayer(new_uuid, new_player.clone()))); + + for (uuid, player) in all_players.0.iter() { // Link all players - add_players.send(SendTo(*uuid, AddPlayer(new_uuid, new_name.clone()))); - add_players.send(SendTo(new_uuid, AddPlayer(*uuid, name.clone()))); + add_players.send(SendTo(*uuid, AddPlayer(new_uuid, new_player.clone()))); + add_players.send(SendTo(new_uuid, AddPlayer(*uuid, player.clone()))); } } } -- 2.43.4 From 0d048b3a576b30618d2b62d46dd05ea129abbd4e Mon Sep 17 00:00:00 2001 From: CoCo_Sol007 Date: Thu, 21 Mar 2024 10:07:46 +0100 Subject: [PATCH 09/12] save --- crates/border-wars/src/lib.rs | 4 +-- crates/border-wars/src/main.rs | 2 +- .../{networkinig => networking}/connection.rs | 27 ++++++++++--------- .../src/{networkinig => networking}/mod.rs | 4 +-- crates/border-wars/src/scenes/lobby.rs | 4 +-- crates/border-wars/src/scenes/menu.rs | 4 +-- 6 files changed, 23 insertions(+), 22 deletions(-) rename crates/border-wars/src/{networkinig => networking}/connection.rs (72%) rename crates/border-wars/src/{networkinig => networking}/mod.rs (93%) diff --git a/crates/border-wars/src/lib.rs b/crates/border-wars/src/lib.rs index 2d48358..0b72f15 100644 --- a/crates/border-wars/src/lib.rs +++ b/crates/border-wars/src/lib.rs @@ -3,12 +3,12 @@ use bevnet::Uuid; use bevy::prelude::*; use bevy::utils::HashMap; -use networkinig::GameRank; +use networking::GameRank; use serde::{Deserialize, Serialize}; pub mod camera; pub mod map; -pub mod networkinig; +pub mod networking; pub mod responsive_scale; pub mod scenes; diff --git a/crates/border-wars/src/main.rs b/crates/border-wars/src/main.rs index 98de956..205a9d6 100644 --- a/crates/border-wars/src/main.rs +++ b/crates/border-wars/src/main.rs @@ -4,7 +4,7 @@ use bevy::prelude::*; use border_wars::camera::CameraPlugin; use border_wars::map::renderer::RendererPlugin; use border_wars::map::selected_tile::SelectTilePlugin; -use border_wars::networkinig::NetworkingPlugin; +use border_wars::networking::NetworkingPlugin; use border_wars::scenes::ScenesPlugin; fn main() { diff --git a/crates/border-wars/src/networkinig/connection.rs b/crates/border-wars/src/networking/connection.rs similarity index 72% rename from crates/border-wars/src/networkinig/connection.rs rename to crates/border-wars/src/networking/connection.rs index 1db57cd..a73df1c 100644 --- a/crates/border-wars/src/networkinig/connection.rs +++ b/crates/border-wars/src/networking/connection.rs @@ -8,9 +8,9 @@ use super::GameRank; use crate::{AllPlayers, CurrentScene, Player}; /// A plugin that manage connections (add, remove). -pub struct ConnectionPuglin; +pub struct ConnectionPlugin; -impl Plugin for ConnectionPuglin { +impl Plugin for ConnectionPlugin { fn build(&self, app: &mut App) { app.add_network_event::() .add_network_event::() @@ -19,23 +19,24 @@ impl Plugin for ConnectionPuglin { } } -/// An event that is trigger whene a new player request to join a game. +/// An event that is trigger when a new player request to join a game. #[derive(Event, Serialize, Deserialize)] pub struct RequestJoin(pub Player); -/// An event that is trigger whene a new player is added. +/// An event that is trigger when a new player is added. #[derive(Event, Serialize, Deserialize)] pub struct AddPlayer(Uuid, Player); -/// An event that is trigger whene a player is removed. +/// An event that is trigger when a player is removed. #[derive(Event, Serialize, Deserialize)] pub struct RemovePlayer(pub Uuid); -/// A fonction that handle join request and add it. +/// A fonction that accept new connection. +/// It add the player to the list of all players. pub fn accept_connection( all_players: Res, - mut requests_join: EventReader>, - mut add_players: EventWriter>, + mut requests_join_event: EventReader>, + mut add_players_event: EventWriter>, connection: Res, state: Res>, ) { @@ -48,7 +49,7 @@ pub fn accept_connection( return; } - for request_join in requests_join.read() { + for request_join in requests_join_event.read() { let new_uuid = request_join.0; let mut new_player = request_join.1.0.clone(); @@ -60,17 +61,17 @@ pub fn accept_connection( new_player.rank = GameRank::Spectator; } - add_players.send(SendTo(new_uuid, AddPlayer(new_uuid, new_player.clone()))); + add_players_event.send(SendTo(new_uuid, AddPlayer(new_uuid, new_player.clone()))); for (uuid, player) in all_players.0.iter() { // Link all players - add_players.send(SendTo(*uuid, AddPlayer(new_uuid, new_player.clone()))); - add_players.send(SendTo(new_uuid, AddPlayer(*uuid, player.clone()))); + add_players_event.send(SendTo(*uuid, AddPlayer(new_uuid, new_player.clone()))); + add_players_event.send(SendTo(new_uuid, AddPlayer(*uuid, player.clone()))); } } } -/// A fonction that handle new / remove players events. +/// A fonction that handle new / remove players when a events. pub fn handle_change_player( mut add_players: EventReader>, mut remove_players: EventReader>, diff --git a/crates/border-wars/src/networkinig/mod.rs b/crates/border-wars/src/networking/mod.rs similarity index 93% rename from crates/border-wars/src/networkinig/mod.rs rename to crates/border-wars/src/networking/mod.rs index 30dd552..bfc607f 100644 --- a/crates/border-wars/src/networkinig/mod.rs +++ b/crates/border-wars/src/networking/mod.rs @@ -4,7 +4,7 @@ use bevnet::{Connection, NetworkPlugin}; use bevy::prelude::*; use serde::{Deserialize, Serialize}; -use self::connection::ConnectionPuglin; +use self::connection::ConnectionPlugin; use crate::{AllPlayers, Player}; pub mod connection; @@ -15,7 +15,7 @@ pub struct NetworkingPlugin; impl Plugin for NetworkingPlugin { fn build(&self, app: &mut App) { app.add_plugins(NetworkPlugin::new("relay.cocosol.fr".to_string())) - .add_plugins(ConnectionPuglin) + .add_plugins(ConnectionPlugin) .init_resource::(); } } diff --git a/crates/border-wars/src/scenes/lobby.rs b/crates/border-wars/src/scenes/lobby.rs index 5eadd41..88ca4f2 100644 --- a/crates/border-wars/src/scenes/lobby.rs +++ b/crates/border-wars/src/scenes/lobby.rs @@ -4,8 +4,8 @@ use bevnet::{Connection, SendTo}; use bevy::prelude::*; use bevy_egui::{egui, EguiContexts}; -use crate::networkinig::connection::RemovePlayer; -use crate::networkinig::GameRank; +use crate::networking::connection::RemovePlayer; +use crate::networking::GameRank; use crate::{AllPlayers, CurrentScene}; /// The plugin for the lobby. diff --git a/crates/border-wars/src/scenes/menu.rs b/crates/border-wars/src/scenes/menu.rs index 7a2bf0b..fe16511 100644 --- a/crates/border-wars/src/scenes/menu.rs +++ b/crates/border-wars/src/scenes/menu.rs @@ -4,8 +4,8 @@ use bevnet::{Connection, SendTo, Uuid}; use bevy::prelude::*; use bevy_egui::{egui, EguiContexts}; -use crate::networkinig::connection::RequestJoin; -use crate::networkinig::GameRank; +use crate::networking::connection::RequestJoin; +use crate::networking::GameRank; use crate::{AllPlayers, CurrentScene, Player}; /// The plugin for the menu. -- 2.43.4 From 1bcb81be0b521476843e22a1c4cbec1c75dc2ec4 Mon Sep 17 00:00:00 2001 From: CoCo_Sol007 Date: Thu, 21 Mar 2024 22:01:10 +0100 Subject: [PATCH 10/12] remove fonction --- crates/border-wars/src/networking/connection.rs | 2 +- crates/border-wars/src/networking/mod.rs | 8 -------- 2 files changed, 1 insertion(+), 9 deletions(-) diff --git a/crates/border-wars/src/networking/connection.rs b/crates/border-wars/src/networking/connection.rs index a73df1c..11180f6 100644 --- a/crates/border-wars/src/networking/connection.rs +++ b/crates/border-wars/src/networking/connection.rs @@ -43,7 +43,7 @@ pub fn accept_connection( // Check if the player is an admin if all_players .get_by_connection(&connection) - .map(|player| player.is_admin()) + .map(|player| player.rank == GameRank::Admin) != Some(true) { return; diff --git a/crates/border-wars/src/networking/mod.rs b/crates/border-wars/src/networking/mod.rs index bfc607f..2983659 100644 --- a/crates/border-wars/src/networking/mod.rs +++ b/crates/border-wars/src/networking/mod.rs @@ -37,14 +37,6 @@ impl AllPlayers { /// Get the player by a connection. pub fn get_by_connection(&self, connection: &Connection) -> Option<&Player> { let uuid = connection.identifier()?; - self.0.get(&uuid) } } - -impl Player { - /// Create a new player. - pub fn is_admin(&self) -> bool { - self.rank == GameRank::Admin - } -} -- 2.43.4 From 417825d0387c5f459edc719bbdb7cf02f4f406c1 Mon Sep 17 00:00:00 2001 From: CoCo_Sol007 Date: Thu, 21 Mar 2024 22:18:24 +0100 Subject: [PATCH 11/12] update --- crates/border-wars/src/networking/connection.rs | 1 + crates/border-wars/src/scenes/lobby.rs | 7 ++++--- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/crates/border-wars/src/networking/connection.rs b/crates/border-wars/src/networking/connection.rs index 11180f6..3850d62 100644 --- a/crates/border-wars/src/networking/connection.rs +++ b/crates/border-wars/src/networking/connection.rs @@ -84,6 +84,7 @@ pub fn handle_change_player( }; for add_player in add_players.read() { + println!("{:?}", add_player.1.1); all_players.0.insert(add_player.1.0, add_player.1.1.clone()); } diff --git a/crates/border-wars/src/scenes/lobby.rs b/crates/border-wars/src/scenes/lobby.rs index 88ca4f2..840ac6e 100644 --- a/crates/border-wars/src/scenes/lobby.rs +++ b/crates/border-wars/src/scenes/lobby.rs @@ -42,13 +42,14 @@ fn lobby_ui( ui.separator(); - for (uuid, player) in all_players.0.iter() { - ui.label(format!("{}: {:?}", player.name, player.rank)); + for (connected_player_id, connected_player) in all_players.0.iter() { + ui.label(connected_player.name.to_string()); if all_players.get_by_connection(&connection).map(|p| p.rank) == Some(GameRank::Admin) + && connected_player.rank != GameRank::Admin && ui.button("Remove").clicked() { for sender_id in all_players.0.keys() { - kick_player.send(SendTo(*sender_id, RemovePlayer(*uuid))); + kick_player.send(SendTo(*sender_id, RemovePlayer(*connected_player_id))); } } ui.separator(); -- 2.43.4 From 89861980faa19b0cd4ac914750cc46fea53cb7b2 Mon Sep 17 00:00:00 2001 From: CoCo_Sol Date: Sat, 23 Mar 2024 23:05:13 +0100 Subject: [PATCH 12/12] edit struct --- crates/border-wars/src/lib.rs | 14 ++-- .../border-wars/src/networking/connection.rs | 74 +++++++++---------- crates/border-wars/src/networking/mod.rs | 22 ++---- crates/border-wars/src/scenes/lobby.rs | 32 ++++---- crates/border-wars/src/scenes/menu.rs | 20 ++--- crates/relay-client/src/lib.rs | 2 +- 6 files changed, 77 insertions(+), 87 deletions(-) diff --git a/crates/border-wars/src/lib.rs b/crates/border-wars/src/lib.rs index 0b72f15..a97d7ec 100644 --- a/crates/border-wars/src/lib.rs +++ b/crates/border-wars/src/lib.rs @@ -2,8 +2,7 @@ use bevnet::Uuid; use bevy::prelude::*; -use bevy::utils::HashMap; -use networking::GameRank; +use networking::PlayerRank; use serde::{Deserialize, Serialize}; pub mod camera; @@ -27,15 +26,14 @@ pub enum CurrentScene { } /// A player in the game. -#[derive(Serialize, Deserialize, Clone, Debug)] +#[derive(Serialize, Deserialize, Clone, Debug, Component, Resource)] pub struct Player { /// The name of the player. pub name: String, /// The rank of the player. - pub rank: GameRank, -} + pub rank: PlayerRank, -/// All the players in the game. -#[derive(Resource, Default)] -pub struct AllPlayers(pub HashMap); + /// The uuid of the player. + pub uuid: Uuid, +} diff --git a/crates/border-wars/src/networking/connection.rs b/crates/border-wars/src/networking/connection.rs index 3850d62..ee47512 100644 --- a/crates/border-wars/src/networking/connection.rs +++ b/crates/border-wars/src/networking/connection.rs @@ -1,11 +1,11 @@ //! All the code related to the connection. -use bevnet::{Connection, NetworkAppExt, Receive, SendTo, Uuid}; +use bevnet::{Connection, NetworkAppExt, Receive, SendTo}; use bevy::prelude::*; use serde::{Deserialize, Serialize}; -use super::GameRank; -use crate::{AllPlayers, CurrentScene, Player}; +use super::PlayerRank; +use crate::{CurrentScene, Player}; /// A plugin that manage connections (add, remove). pub struct ConnectionPlugin; @@ -15,7 +15,10 @@ impl Plugin for ConnectionPlugin { app.add_network_event::() .add_network_event::() .add_network_event::() - .add_systems(Update, (accept_connection, handle_change_player)); + .add_systems( + Update, + (accept_connection, handle_new_player, handle_remove_player), + ); } } @@ -25,32 +28,21 @@ pub struct RequestJoin(pub Player); /// An event that is trigger when a new player is added. #[derive(Event, Serialize, Deserialize)] -pub struct AddPlayer(Uuid, Player); +pub struct AddPlayer(Player); /// An event that is trigger when a player is removed. #[derive(Event, Serialize, Deserialize)] -pub struct RemovePlayer(pub Uuid); +pub struct RemovePlayer(pub Player); /// A fonction that accept new connection. /// It add the player to the list of all players. pub fn accept_connection( - all_players: Res, + all_players_query: Query<&Player>, mut requests_join_event: EventReader>, mut add_players_event: EventWriter>, - connection: Res, state: Res>, ) { - // Check if the player is an admin - if all_players - .get_by_connection(&connection) - .map(|player| player.rank == GameRank::Admin) - != Some(true) - { - return; - } - for request_join in requests_join_event.read() { - let new_uuid = request_join.0; let mut new_player = request_join.1.0.clone(); let current_state = *state.get(); @@ -58,42 +50,46 @@ pub fn accept_connection( if current_state == CurrentScene::Menu { return; } else if current_state == CurrentScene::Game { - new_player.rank = GameRank::Spectator; + new_player.rank = PlayerRank::Spectator; } - add_players_event.send(SendTo(new_uuid, AddPlayer(new_uuid, new_player.clone()))); + add_players_event.send(SendTo(new_player.uuid, AddPlayer(new_player.clone()))); - for (uuid, player) in all_players.0.iter() { + for old_player in all_players_query.iter() { // Link all players - add_players_event.send(SendTo(*uuid, AddPlayer(new_uuid, new_player.clone()))); - add_players_event.send(SendTo(new_uuid, AddPlayer(*uuid, player.clone()))); + add_players_event.send(SendTo(old_player.uuid, AddPlayer(new_player.clone()))); + add_players_event.send(SendTo(new_player.uuid, AddPlayer(old_player.clone()))); } } } -/// A fonction that handle new / remove players when a events. -pub fn handle_change_player( - mut add_players: EventReader>, +/// A fonction that handle new players when a events is received. +pub fn handle_new_player(mut add_players: EventReader>, mut commands: Commands) { + for add_player in add_players.read() { + commands.spawn(add_player.1.0.clone()); + } +} + +/// A fonction that handle remove players when a events is received. +pub fn handle_remove_player( mut remove_players: EventReader>, - mut all_players: ResMut, + mut commands: Commands, + all_players_query: Query<(Entity, &Player)>, connection: Res, mut next_scene: ResMut>, ) { - let Some(connection) = connection.identifier() else { - return; - }; - - for add_player in add_players.read() { - println!("{:?}", add_player.1.1); - all_players.0.insert(add_player.1.0, add_player.1.1.clone()); - } - for remove_player in remove_players.read() { - if remove_player.1.0 == connection { - all_players.0.clear(); + if Some(remove_player.1.0.uuid) == connection.identifier() { next_scene.set(CurrentScene::Menu); + all_players_query.iter().for_each(|(entity, _)| { + commands.entity(entity).despawn(); + }); return; } - all_players.0.remove(&remove_player.1.0); + for (entity, player) in all_players_query.iter() { + if remove_player.1.0.uuid == player.uuid { + commands.entity(entity).despawn(); + } + } } } diff --git a/crates/border-wars/src/networking/mod.rs b/crates/border-wars/src/networking/mod.rs index 2983659..e3f4f8a 100644 --- a/crates/border-wars/src/networking/mod.rs +++ b/crates/border-wars/src/networking/mod.rs @@ -1,11 +1,10 @@ //! All the code related to the networking. -use bevnet::{Connection, NetworkPlugin}; +use bevnet::NetworkPlugin; use bevy::prelude::*; use serde::{Deserialize, Serialize}; use self::connection::ConnectionPlugin; -use crate::{AllPlayers, Player}; pub mod connection; @@ -15,28 +14,19 @@ pub struct NetworkingPlugin; impl Plugin for NetworkingPlugin { fn build(&self, app: &mut App) { app.add_plugins(NetworkPlugin::new("relay.cocosol.fr".to_string())) - .add_plugins(ConnectionPlugin) - .init_resource::(); + .add_plugins(ConnectionPlugin); } } /// The rank of the player. #[derive(PartialEq, Eq, Serialize, Deserialize, Clone, Copy, Debug)] -pub enum GameRank { - /// The player is a spectator. He does not play the game. +pub enum PlayerRank { + /// A spectator. He does not play the game, just renderer the game. Spectator, - /// The player is an admin. He can remove players. + /// An admin. He manages the game and play the game. Admin, - /// The player is a player. He can join the game and play. + /// The player. He can join the game and play. Player, } - -impl AllPlayers { - /// Get the player by a connection. - pub fn get_by_connection(&self, connection: &Connection) -> Option<&Player> { - let uuid = connection.identifier()?; - self.0.get(&uuid) - } -} diff --git a/crates/border-wars/src/scenes/lobby.rs b/crates/border-wars/src/scenes/lobby.rs index 840ac6e..89201c9 100644 --- a/crates/border-wars/src/scenes/lobby.rs +++ b/crates/border-wars/src/scenes/lobby.rs @@ -5,8 +5,8 @@ use bevy::prelude::*; use bevy_egui::{egui, EguiContexts}; use crate::networking::connection::RemovePlayer; -use crate::networking::GameRank; -use crate::{AllPlayers, CurrentScene}; +use crate::networking::PlayerRank; +use crate::{CurrentScene, Player}; /// The plugin for the lobby. pub struct LobbyPlugin; @@ -22,9 +22,17 @@ fn lobby_ui( mut ctx: EguiContexts, mut next_scene: ResMut>, connection: Res, - all_players: Res, + all_players_query: Query<&Player>, mut kick_player: EventWriter>, ) { + // Get our player info. + let Some(self_player) = all_players_query + .iter() + .find(|player| connection.identifier() == Some(player.uuid)) + else { + return; + }; + egui::CentralPanel::default().show(ctx.ctx_mut(), |ui| { ui.heading("Border Wars"); @@ -32,7 +40,7 @@ fn lobby_ui( ui.label("Game created"); ui.horizontal(|ui| { - if all_players.get_by_connection(&connection).map(|p| p.rank) != Some(GameRank::Admin) { + if self_player.rank != PlayerRank::Admin { return; } ui.label("Game ID: "); @@ -42,22 +50,20 @@ fn lobby_ui( ui.separator(); - for (connected_player_id, connected_player) in all_players.0.iter() { - ui.label(connected_player.name.to_string()); - if all_players.get_by_connection(&connection).map(|p| p.rank) == Some(GameRank::Admin) - && connected_player.rank != GameRank::Admin + for player in all_players_query.iter() { + ui.label(player.name.to_string()); + if self_player.rank == PlayerRank::Admin + && player.rank != PlayerRank::Admin && ui.button("Remove").clicked() { - for sender_id in all_players.0.keys() { - kick_player.send(SendTo(*sender_id, RemovePlayer(*connected_player_id))); + for sender_id in all_players_query.iter() { + kick_player.send(SendTo(sender_id.uuid, RemovePlayer(player.clone()))); } } ui.separator(); } - if all_players.get_by_connection(&connection).map(|p| p.rank) == Some(GameRank::Admin) - && ui.button("Run the game").clicked() - { + if self_player.rank == PlayerRank::Admin && ui.button("Run the game").clicked() { next_scene.set(CurrentScene::Game); // TODO: run the game } diff --git a/crates/border-wars/src/scenes/menu.rs b/crates/border-wars/src/scenes/menu.rs index fe16511..39a2239 100644 --- a/crates/border-wars/src/scenes/menu.rs +++ b/crates/border-wars/src/scenes/menu.rs @@ -5,8 +5,8 @@ use bevy::prelude::*; use bevy_egui::{egui, EguiContexts}; use crate::networking::connection::RequestJoin; -use crate::networking::GameRank; -use crate::{AllPlayers, CurrentScene, Player}; +use crate::networking::PlayerRank; +use crate::{CurrentScene, Player}; /// The plugin for the menu. pub struct MenuPlugin; @@ -16,6 +16,7 @@ impl Plugin for MenuPlugin { app.add_systems(Update, menu_ui.run_if(in_state(CurrentScene::Menu))); } } + /// Display the UI of the menu to host a game or join one. fn menu_ui( mut ctx: EguiContexts, @@ -23,8 +24,8 @@ fn menu_ui( mut next_scene: ResMut>, mut request_join: EventWriter>, mut name: Local, - mut all_player: ResMut, connection: Res, + mut commands: Commands, ) { let Some(uuid) = connection.identifier() else { return; @@ -55,7 +56,8 @@ fn menu_ui( game_id, RequestJoin(Player { name: name.clone(), - rank: GameRank::Player, + rank: PlayerRank::Player, + uuid, }), )); } @@ -65,13 +67,11 @@ fn menu_ui( if ui.button("Create new game").clicked() { next_scene.set(CurrentScene::Lobby); - all_player.0.insert( + commands.spawn(Player { + name: name.clone(), + rank: PlayerRank::Admin, uuid, - Player { - name: name.clone(), - rank: GameRank::Admin, - }, - ); + }); } }); } diff --git a/crates/relay-client/src/lib.rs b/crates/relay-client/src/lib.rs index b052784..b390d21 100644 --- a/crates/relay-client/src/lib.rs +++ b/crates/relay-client/src/lib.rs @@ -80,7 +80,7 @@ impl Connection { path.push(".relay-data"); // Check if the file exists. - match path.exists() { + match false { true => { // Read the file and parse the identifier and secret key. let contents = fs::read(&path)?; -- 2.43.4