From 6f1526b7fa3a7086405d025f03c6461748486453 Mon Sep 17 00:00:00 2001 From: Jonni Liljamo Date: Fri, 17 Mar 2023 15:51:20 +0200 Subject: [PATCH] wip!(client, shared): re-enable browsing games, sota --- client/src/plugins/menu/ui/play/mod.rs | 12 ++++ client/src/plugins/menu/ui/play/ui.rs | 60 ++++++++++--------- client/src/plugins/networking/mod.rs | 31 ++++++---- .../plugins/networking/systems/events/mod.rs | 10 ++++ .../{events.rs => events/receive/mod.rs} | 33 ++++++++-- .../networking/systems/events/send/game.rs | 33 ++++++++++ .../networking/systems/events/send/mod.rs | 22 +++++++ shared/src/types/game.rs | 2 +- 8 files changed, 160 insertions(+), 43 deletions(-) create mode 100644 client/src/plugins/networking/systems/events/mod.rs rename client/src/plugins/networking/systems/{events.rs => events/receive/mod.rs} (61%) create mode 100644 client/src/plugins/networking/systems/events/send/game.rs create mode 100644 client/src/plugins/networking/systems/events/send/mod.rs diff --git a/client/src/plugins/menu/ui/play/mod.rs b/client/src/plugins/menu/ui/play/mod.rs index 100cdcf..a09ceeb 100644 --- a/client/src/plugins/menu/ui/play/mod.rs +++ b/client/src/plugins/menu/ui/play/mod.rs @@ -12,6 +12,7 @@ use bevy::{ }; mod ui; +use laurelin_shared::types::game::Game; pub use ui::*; #[derive(Default, Resource, Reflect)] @@ -19,6 +20,17 @@ pub use ui::*; pub struct PlayScreenData { pub state: PlayScreenState, pub browse_state: PlayScreenBrowseState, + + pub waiting_for_create_game: bool, + pub waiting_for_all_forming: bool, + pub waiting_for_my_games: bool, + + #[reflect(ignore)] + pub cur_game: Option, + #[reflect(ignore)] + pub all_forming: Vec, + #[reflect(ignore)] + pub my_games: Vec, } #[derive(Default, PartialEq, Clone, Reflect)] diff --git a/client/src/plugins/menu/ui/play/ui.rs b/client/src/plugins/menu/ui/play/ui.rs index 5f54b04..ed0a418 100644 --- a/client/src/plugins/menu/ui/play/ui.rs +++ b/client/src/plugins/menu/ui/play/ui.rs @@ -9,7 +9,14 @@ use bevy::prelude::*; use bevy_egui::{egui, EguiContexts}; -use crate::{cfg::CfgUser, plugins::menu::MenuState, util::egui::menuwindow}; +use crate::{ + cfg::CfgUser, + plugins::{ + menu::MenuState, + networking::send::game::{GameAllFormingEvent, GameCreateEvent, GameMyGamesEvent}, + }, + util::egui::menuwindow, +}; use super::{PlayScreenBrowseState, PlayScreenData, PlayScreenState}; @@ -18,6 +25,9 @@ pub fn ui( mut egui_contexts: EguiContexts, mut data: ResMut, cfg_user: Res, + mut creategame_ev_w: EventWriter, + mut allforming_ev_w: EventWriter, + mut mygames_ev_w: EventWriter, ) { menuwindow( egui_contexts.ctx_mut(), @@ -67,19 +77,19 @@ pub fn ui( if ui.button("Refresh").clicked() { match data.browse_state { PlayScreenBrowseState::Forming => { - //if !rtdmenu.waiting_for_all_forming_call { - // allforming_ev_w.send(AllFormingEvent); - //} + if !data.waiting_for_all_forming { + allforming_ev_w.send(GameAllFormingEvent); + } } PlayScreenBrowseState::InProgress => { - //if !rtdmenu.waiting_for_my_games_call { - // mygames_ev_w.send(MyGamesEvent); - //} + if !data.waiting_for_my_games { + mygames_ev_w.send(GameMyGamesEvent); + } } PlayScreenBrowseState::Finished => { - //if !rtdmenu.waiting_for_my_games_call { - // mygames_ev_w.send(MyGamesEvent); - //} + if !data.waiting_for_my_games { + mygames_ev_w.send(GameMyGamesEvent); + } } } } @@ -104,18 +114,16 @@ pub fn ui( } PlayScreenState::CreateGame => { ui.vertical_centered(|ui| { - ui.add_enabled_ui( - /* !rtdmenu.waiting_for_create_game_call */ false, - |ui| { - if ui.button("Confirm").clicked() { - //creategame_ev_w.send(CreateGameEvent); - } + ui.add_enabled_ui(!data.waiting_for_create_game, |ui| { + if ui.button("Confirm").clicked() { + data.waiting_for_create_game = true; + creategame_ev_w.send(GameCreateEvent); + } - if ui.button("Cancel").clicked() { - data.state = PlayScreenState::Main; - } - }, - ); + if ui.button("Cancel").clicked() { + data.state = PlayScreenState::Main; + } + }); }); } PlayScreenState::InLobbyHost => { @@ -141,18 +149,17 @@ pub fn ui( ); } -fn browse_forming(ui: &mut egui::Ui, rtdmenu: &mut PlayScreenData, cfg_user: &CfgUser) { - /* - if rtdmenu.waiting_for_all_forming_call { +fn browse_forming(ui: &mut egui::Ui, data: &mut PlayScreenData, cfg_user: &CfgUser) { + if data.waiting_for_all_forming { ui.horizontal(|ui| { ui.spinner(); ui.label("loading..."); }); } else { - if rtdmenu.all_forming_games.is_empty() { + if data.all_forming.is_empty() { ui.label("No forming games found."); } else { - for game in rtdmenu.all_forming_games.clone() { + for game in data.all_forming.clone() { egui::Frame::none() .fill(egui::Color32::BLACK) .rounding(4.) @@ -191,7 +198,6 @@ fn browse_forming(ui: &mut egui::Ui, rtdmenu: &mut PlayScreenData, cfg_user: &Cf } } } - */ } fn browse_inprogress(ui: &mut egui::Ui, rtdmenu: &mut PlayScreenData) { diff --git a/client/src/plugins/networking/mod.rs b/client/src/plugins/networking/mod.rs index 8b095cc..351de10 100644 --- a/client/src/plugins/networking/mod.rs +++ b/client/src/plugins/networking/mod.rs @@ -10,21 +10,30 @@ use bevy::prelude::*; use naia_bevy_client::ReceiveEvents; mod systems; +pub use systems::events::{receive, send}; pub struct NetworkingPlugin; impl Plugin for NetworkingPlugin { fn build(&self, app: &mut App) { - app.add_systems( - ( - systems::events::connect_events, - systems::events::reject_events, - systems::events::disconnect_events, - systems::events::message_events, - systems::events::tick_events, - ) - .chain() - .in_set(ReceiveEvents), - ); + app.add_event::() + .add_event::() + .add_event::() + .add_systems(( + send::game::create_event, + send::game::all_forming_event, + send::game::my_games_event, + )) + .add_systems( + ( + receive::connect_events, + receive::reject_events, + receive::disconnect_events, + receive::message_events, + receive::tick_events, + ) + .chain() + .in_set(ReceiveEvents), + ); } } diff --git a/client/src/plugins/networking/systems/events/mod.rs b/client/src/plugins/networking/systems/events/mod.rs new file mode 100644 index 0000000..e8b30cb --- /dev/null +++ b/client/src/plugins/networking/systems/events/mod.rs @@ -0,0 +1,10 @@ +/* + * This file is part of laurelin/client + * Copyright (C) 2023 Jonni Liljamo + * + * Licensed under GPL-3.0-only. + * See LICENSE for licensing information. + */ + +pub mod receive; +pub mod send; diff --git a/client/src/plugins/networking/systems/events.rs b/client/src/plugins/networking/systems/events/receive/mod.rs similarity index 61% rename from client/src/plugins/networking/systems/events.rs rename to client/src/plugins/networking/systems/events/receive/mod.rs index ab2d877..d5f2710 100644 --- a/client/src/plugins/networking/systems/events.rs +++ b/client/src/plugins/networking/systems/events/receive/mod.rs @@ -7,7 +7,10 @@ */ use bevy::prelude::*; -use laurelin_shared::server::{channels::AfterAuthChannel, messages::AfterAuth}; +use laurelin_shared::server::{ + channels::{AfterAuthChannel, DataRequestChannel}, + messages::{AfterAuth, DataRequestResponse, DataRequestType}, +}; use naia_bevy_client::{ events::{ClientTickEvent, ConnectEvent, DisconnectEvent, MessageEvents, RejectEvent}, Client, @@ -16,7 +19,10 @@ use naia_bevy_client::{ use crate::{ cfg::CfgUser, plugins::menu::{ - ui::connect::{ConnectScreenData, ConnectState}, + ui::{ + connect::{ConnectScreenData, ConnectState}, + play::PlayScreenData, + }, MenuState, }, }; @@ -52,7 +58,8 @@ pub fn message_events( mut commands: Commands, mut ev: EventReader, mut cfg_user: ResMut, - mut data: ResMut, + mut connect_data: ResMut, + mut play_data: ResMut, ) { for events in ev.iter() { for aa_message in events.read::() { @@ -63,11 +70,29 @@ pub fn message_events( cfg_user.cookie = aa_message.cookie; // reset the connection screen to login - data.state = ConnectState::Login; + connect_data.state = ConnectState::Login; // take us to the main menu commands.insert_resource(NextState(Some(MenuState::Menu))); } + + for response in events.read::() { + match DataRequestType::from_u8(&response.r#type) { + DataRequestType::GameCreate => { + // TODO: set cur_game, send player to lobby screen + } + DataRequestType::GameAllForming => { + // TODO: handle possible error + play_data.all_forming = serde_json::from_str(&response.data).unwrap(); + play_data.waiting_for_all_forming = false; + } + DataRequestType::GameMyGames => { + // TODO: handle possible error + play_data.my_games = serde_json::from_str(&response.data).unwrap(); + play_data.waiting_for_my_games = false; + } + } + } } } diff --git a/client/src/plugins/networking/systems/events/send/game.rs b/client/src/plugins/networking/systems/events/send/game.rs new file mode 100644 index 0000000..6f92495 --- /dev/null +++ b/client/src/plugins/networking/systems/events/send/game.rs @@ -0,0 +1,33 @@ +/* + * This file is part of laurelin/client + * Copyright (C) 2023 Jonni Liljamo + * + * Licensed under GPL-3.0-only. + * See LICENSE for licensing information. + */ + +use bevy::prelude::EventReader; +use laurelin_shared::server::{ + channels::DataRequestChannel, + messages::{DataRequest, DataRequestType}, +}; +use naia_bevy_client::Client; + +use super::data_request; + +pub struct GameCreateEvent; +data_request!(create_event, GameCreateEvent, DataRequestType::GameCreate); + +pub struct GameAllFormingEvent; +data_request!( + all_forming_event, + GameAllFormingEvent, + DataRequestType::GameAllForming +); + +pub struct GameMyGamesEvent; +data_request!( + my_games_event, + GameMyGamesEvent, + DataRequestType::GameMyGames +); diff --git a/client/src/plugins/networking/systems/events/send/mod.rs b/client/src/plugins/networking/systems/events/send/mod.rs new file mode 100644 index 0000000..8464d62 --- /dev/null +++ b/client/src/plugins/networking/systems/events/send/mod.rs @@ -0,0 +1,22 @@ +/* + * This file is part of laurelin/client + * Copyright (C) 2023 Jonni Liljamo + * + * Licensed under GPL-3.0-only. + * See LICENSE for licensing information. + */ + +pub mod game; + +macro_rules! data_request { + ($fni:ident, $event:ident, $reqtype:expr) => { + pub fn $fni(mut ev: EventReader<$event>, mut client: Client) { + for _ in ev.iter() { + client.send_message::(&DataRequest::new( + $reqtype as u8, + )); + } + } + }; +} +use data_request; diff --git a/shared/src/types/game.rs b/shared/src/types/game.rs index 77dac9e..8051e54 100644 --- a/shared/src/types/game.rs +++ b/shared/src/types/game.rs @@ -21,7 +21,7 @@ pub const GAMESTATE_CANCELLED: i16 = 3; pub const GAMETURN_HOST: i16 = 0; pub const GAMETURN_GUEST: i16 = 1; -#[derive(Serialize, Deserialize, Queryable)] +#[derive(Serialize, Deserialize, Queryable, Clone)] pub struct Game { pub id: Uuid, pub created_at: NaiveDateTime, -- 2.44.1