generated from tipragot/rust
Implement generic online system #82
|
@ -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<Uuid, String>);
|
||||
pub struct AllPlayer(pub HashMap<Uuid, Player>);
|
||||
|
|
|
@ -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<AllPlayer>,
|
||||
mut requests_join: EventReader<Receive<RequestJoin>>,
|
||||
game_rank: Res<GameRank>,
|
||||
mut add_players: EventWriter<SendTo<AddPlayer>>,
|
||||
connection: Res<Connection>,
|
||||
) {
|
||||
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();
|
||||
|
|
|
@ -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
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue