Implement generic online system #82

Merged
CoCo_Sol merged 14 commits from impl-online into main 2024-03-25 05:45:17 +00:00
3 changed files with 68 additions and 9 deletions
Showing only changes of commit 4401b64300 - Show all commits

View file

@ -1,9 +1,12 @@
//! The lobby of the game. //! The lobby of the game.
use bevnet::{Connection, SendTo};
use bevy::prelude::*; use bevy::prelude::*;
use bevy_egui::{egui, EguiContexts}; 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. /// The plugin for the lobby.
pub struct LobbyPlugin; pub struct LobbyPlugin;
@ -15,7 +18,13 @@ impl Plugin for LobbyPlugin {
} }
/// Display the UI of the lobby. /// Display the UI of the lobby.
fn lobby_ui(mut ctx: EguiContexts, mut next_scene: ResMut<NextState<CurrentScene>>) { fn lobby_ui(
mut ctx: EguiContexts,
mut next_scene: ResMut<NextState<CurrentScene>>,
connection: Res<Connection>,
all_players: Res<AllPlayer>,
mut kick_player: EventWriter<SendTo<RemovePlayer>>,
) {
egui::CentralPanel::default().show(ctx.ctx_mut(), |ui| { egui::CentralPanel::default().show(ctx.ctx_mut(), |ui| {
ui.heading("Border Wars"); ui.heading("Border Wars");
@ -23,14 +32,31 @@ fn lobby_ui(mut ctx: EguiContexts, mut next_scene: ResMut<NextState<CurrentScene
ui.label("Game created"); ui.label("Game created");
ui.horizontal(|ui| { ui.horizontal(|ui| {
if all_players.get_by_connection(&connection).map(|p| p.rank) != Some(GameRank::Admin) {
return;
}
ui.label("Game ID: "); ui.label("Game ID: ");
// TODO : get the game ID and display it. // TODO : get the game ID and display it.
ui.label("connection_string"); ui.text_edit_singleline(&mut connection.identifier().unwrap_or_default().to_string());
}); });
ui.separator(); ui.separator();
if ui.button("Run the game").clicked() { for (uuid, player) in all_players.0.iter() {
ui.label(format!("{}: {:?}", player.name, player.rank));
if all_players.get_by_connection(&connection).map(|p| p.rank) == Some(GameRank::Admin)
&& ui.button("Remove").clicked()
{
for sender_id in all_players.0.keys() {
kick_player.send(SendTo(*sender_id, RemovePlayer(*uuid)));
}
}
ui.separator();
}
if all_players.get_by_connection(&connection).map(|p| p.rank) == Some(GameRank::Admin)
&& ui.button("Run the game").clicked()
{
next_scene.set(CurrentScene::Game); next_scene.set(CurrentScene::Game);
// TODO: run the game // TODO: run the game
} }

View file

@ -1,9 +1,12 @@
//! The main menu of the game. //! The main menu of the game.
use bevnet::{Connection, SendTo, Uuid};
use bevy::prelude::*; use bevy::prelude::*;
use bevy_egui::{egui, EguiContexts}; use bevy_egui::{egui, EguiContexts};
use crate::CurrentScene; use crate::networkinig::connection::RequestJoin;
use crate::networkinig::GameRank;
use crate::{AllPlayer, CurrentScene, Player};
/// The plugin for the menu. /// The plugin for the menu.
pub struct MenuPlugin; pub struct MenuPlugin;
@ -18,20 +21,43 @@ fn menu_ui(
mut ctx: EguiContexts, mut ctx: EguiContexts,
mut connection_string: Local<String>, mut connection_string: Local<String>,
mut next_scene: ResMut<NextState<CurrentScene>>, mut next_scene: ResMut<NextState<CurrentScene>>,
mut request_join: EventWriter<SendTo<RequestJoin>>,
mut name: Local<String>,
mut all_player: ResMut<AllPlayer>,
connection: Res<Connection>,
) { ) {
let Some(uuid) = connection.identifier() else {
return;
};
egui::CentralPanel::default().show(ctx.ctx_mut(), |ui| { egui::CentralPanel::default().show(ctx.ctx_mut(), |ui| {
ui.heading("Border Wars"); ui.heading("Border Wars");
ui.separator(); ui.separator();
ui.label("Name");
ui.text_edit_singleline(&mut *name);
ui.separator();
ui.label("Connect to an existing game:"); ui.label("Connect to an existing game:");
ui.horizontal(|ui| { ui.horizontal(|ui| {
ui.label("Game ID: "); ui.label("Game ID: ");
ui.text_edit_singleline(&mut *connection_string); ui.text_edit_singleline(&mut *connection_string);
let Ok(game_id) = Uuid::parse_str(&connection_string) else {
return;
};
if ui.button("Join").clicked() { if ui.button("Join").clicked() {
next_scene.set(CurrentScene::Game); next_scene.set(CurrentScene::Lobby);
// TODO: connect to the game 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() { if ui.button("Create new game").clicked() {
next_scene.set(CurrentScene::Lobby); next_scene.set(CurrentScene::Lobby);
// TODO: create a new game all_player.0.insert(
uuid,
Player {
name: name.clone(),
rank: GameRank::Admin,
},
);
} }
}); });
} }

View file

@ -80,7 +80,8 @@ impl Connection {
path.push(".relay-data"); path.push(".relay-data");
// Check if the file exists. // Check if the file exists.
match path.exists() { match false {
// path.exists()
true => { true => {
// Read the file and parse the identifier and secret key. // Read the file and parse the identifier and secret key.
let contents = fs::read(&path)?; let contents = fs::read(&path)?;