From 568d69a25060ab0196091fbda7ac2a8c82866c94 Mon Sep 17 00:00:00 2001 From: CoCo_Sol007 Date: Wed, 20 Mar 2024 17:30:52 +0100 Subject: [PATCH] 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 + } +}