From face8bc68a55ac41b5d9c3d44cfbfeef182af8c6 Mon Sep 17 00:00:00 2001 From: Jonni Liljamo Date: Wed, 22 Mar 2023 12:24:08 +0200 Subject: [PATCH] =?UTF-8?q?feat(client,=20server,=20shared):=20vittumainen?= =?UTF-8?q?=20bandage=20p=C3=A4=C3=A4lle,=20olkoot=20stna.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- client/src/plugins/menu/ui/play/mod.rs | 8 ++-- client/src/plugins/menu/ui/play/ui.rs | 3 +- .../networking/systems/events/receive/mod.rs | 6 +-- server/src/systems/event/message/mod.rs | 45 +++++++++++-------- shared/src/server/messages/datarequest.rs | 10 +++-- shared/src/types/game.rs | 40 +++++++++++++++++ shared/src/types/user.rs | 19 +++++--- 7 files changed, 97 insertions(+), 34 deletions(-) diff --git a/client/src/plugins/menu/ui/play/mod.rs b/client/src/plugins/menu/ui/play/mod.rs index a09ceeb..8b1c9c8 100644 --- a/client/src/plugins/menu/ui/play/mod.rs +++ b/client/src/plugins/menu/ui/play/mod.rs @@ -12,7 +12,7 @@ use bevy::{ }; mod ui; -use laurelin_shared::types::game::Game; +use laurelin_shared::types::game::GamePub; pub use ui::*; #[derive(Default, Resource, Reflect)] @@ -26,11 +26,11 @@ pub struct PlayScreenData { pub waiting_for_my_games: bool, #[reflect(ignore)] - pub cur_game: Option, + pub cur_game: Option, #[reflect(ignore)] - pub all_forming: Vec, + pub all_forming: Vec, #[reflect(ignore)] - pub my_games: Vec, + 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 f77def1..2b074b6 100644 --- a/client/src/plugins/menu/ui/play/ui.rs +++ b/client/src/plugins/menu/ui/play/ui.rs @@ -173,7 +173,8 @@ fn browse_forming(ui: &mut egui::Ui, data: &mut PlayScreenData, cfg_user: &CfgUs ui.with_layout( egui::Layout::right_to_left(egui::Align::Center), |ui| { - if game.guest_id.unwrap_or_default().to_string() == cfg_user.id + if game.guest_id.clone().unwrap_or_default().to_string() + == cfg_user.id { ui.add_enabled(false, egui::Button::new("Joined")); if ui.button("Inspect").clicked() { diff --git a/client/src/plugins/networking/systems/events/receive/mod.rs b/client/src/plugins/networking/systems/events/receive/mod.rs index 14efbd9..e468144 100644 --- a/client/src/plugins/networking/systems/events/receive/mod.rs +++ b/client/src/plugins/networking/systems/events/receive/mod.rs @@ -86,17 +86,17 @@ pub fn message_events( DataRequestType::GameCreate => { // TODO: handle possible error (unwrap, // and if the response data is an error) - play_data.cur_game = serde_json::from_str(&response.data).unwrap(); + play_data.cur_game = response.games.unwrap().get(0).cloned(); play_data.state = PlayScreenState::InLobbyHost; } DataRequestType::GameAllForming => { // TODO: handle possible error - play_data.all_forming = serde_json::from_str(&response.data).unwrap(); + play_data.all_forming = response.games.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.my_games = response.games.unwrap(); play_data.waiting_for_my_games = false; } } diff --git a/server/src/systems/event/message/mod.rs b/server/src/systems/event/message/mod.rs index 889044f..51a9a6d 100644 --- a/server/src/systems/event/message/mod.rs +++ b/server/src/systems/event/message/mod.rs @@ -6,6 +6,8 @@ * See LICENSE for licensing information. */ +use std::vec; + use bevy_ecs::{ event::EventReader, system::{Res, ResMut}, @@ -18,6 +20,7 @@ use laurelin_shared::{ channels::{CookieRefreshChannel, DataRequestChannel}, messages::{CookieRefresh, DataRequest, DataRequestResponse, DataRequestType}, }, + types::game::GamePub, }; use naia_bevy_server::{events::MessageEvents, Server}; @@ -36,15 +39,17 @@ pub(crate) fn message_events( // TODO: handle let cookie = global.user_to_session_map.get(&user_key).unwrap(); let wrapped = create(&config.api_address, &cookie); - let json = match wrapped.response { - ResponseCreateGame::Error(err) => serde_json::to_string(&err).unwrap(), // TODO: handle - ResponseCreateGame::Valid(result) => { - serde_json::to_string(&result).unwrap() // TODO: handle - } + let game = match wrapped.response { + ResponseCreateGame::Error(_err) => None, // TODO: handle + ResponseCreateGame::Valid(result) => Some(result), }; + let mut games_pub = vec![]; + if let Some(game) = game { + games_pub.push(GamePub::from_game(&game)); + } server.send_message::( &user_key, - &DataRequestResponse::new(request.r#type, &json), + &DataRequestResponse::new(request.r#type, None, Some(games_pub)), ); // update cookie @@ -60,15 +65,17 @@ pub(crate) fn message_events( // TODO: handle let cookie = global.user_to_session_map.get(&user_key).unwrap(); let wrapped = all_forming(&config.api_address, &cookie); - let json = match wrapped.response { - ResponseAllForming::Error(err) => serde_json::to_string(&err).unwrap(), // TODO: handle - ResponseAllForming::Valid(result) => { - serde_json::to_string(&result).unwrap() // TODO: handle - } + let games = match wrapped.response { + ResponseAllForming::Error(_err) => vec![], // TODO: handle + ResponseAllForming::Valid(result) => result, }; + let mut games_pub = vec![]; + for game in games { + games_pub.push(GamePub::from_game(&game)); + } server.send_message::( &user_key, - &DataRequestResponse::new(request.r#type, &json), + &DataRequestResponse::new(request.r#type, None, Some(games_pub)), ); // update cookie @@ -84,15 +91,17 @@ pub(crate) fn message_events( // TODO: handle let cookie = global.user_to_session_map.get(&user_key).unwrap(); let wrapped = my_games(&config.api_address, &cookie); - let json = match wrapped.response { - ResponseMyGames::Error(err) => serde_json::to_string(&err).unwrap(), // TODO: handle - ResponseMyGames::Valid(result) => { - serde_json::to_string(&result).unwrap() // TODO: handle - } + let games = match wrapped.response { + ResponseMyGames::Error(_err) => vec![], // TODO: handle + ResponseMyGames::Valid(result) => result, }; + let mut games_pub = vec![]; + for game in games { + games_pub.push(GamePub::from_game(&game)); + } server.send_message::( &user_key, - &DataRequestResponse::new(request.r#type, &json), + &DataRequestResponse::new(request.r#type, None, Some(games_pub)), ); // update cookie diff --git a/shared/src/server/messages/datarequest.rs b/shared/src/server/messages/datarequest.rs index 59eedc3..388ca8b 100644 --- a/shared/src/server/messages/datarequest.rs +++ b/shared/src/server/messages/datarequest.rs @@ -8,6 +8,8 @@ use naia_bevy_shared::Message; +use crate::types::{game::GamePub, user::UserPub}; + #[derive(Message)] pub struct DataRequest { pub r#type: u8, @@ -22,14 +24,16 @@ impl DataRequest { #[derive(Message)] pub struct DataRequestResponse { pub r#type: u8, - pub data: String, + pub users: Option>, + pub games: Option>, } impl DataRequestResponse { - pub fn new(r#type: u8, data: &str) -> Self { + pub fn new(r#type: u8, users: Option>, games: Option>) -> Self { Self { r#type, - data: data.to_string(), + users, + games, } } } diff --git a/shared/src/types/game.rs b/shared/src/types/game.rs index 8051e54..50486fe 100644 --- a/shared/src/types/game.rs +++ b/shared/src/types/game.rs @@ -8,6 +8,7 @@ use chrono::NaiveDateTime; use diesel::{Insertable, Queryable}; +use naia_bevy_shared::Serde; use serde::{Deserialize, Serialize}; use uuid::Uuid; @@ -33,6 +34,45 @@ pub struct Game { pub game_data_id: Uuid, } +#[derive(Serialize, Deserialize, Clone, Serde, PartialEq)] +pub struct GamePub { + pub id: String, + pub created_at: String, + pub updated_at: String, + pub host_id: String, + pub guest_id: Option, + pub state: i16, + pub ended_at: Option, + pub game_data_id: String, +} + +impl GamePub { + pub fn from_game(game: &Game) -> Self { + Self { + id: game.id.to_string(), + created_at: game.created_at.to_string(), + updated_at: game.updated_at.to_string(), + host_id: game.host_id.to_string(), + guest_id: { + if let Some(guest_id) = game.guest_id { + Some(guest_id.to_string()) + } else { + None + } + }, + state: game.state, + ended_at: { + if let Some(ended_at) = game.ended_at { + Some(ended_at.to_string()) + } else { + None + } + }, + game_data_id: game.game_data_id.to_string(), + } + } +} + #[derive(Deserialize, Insertable)] #[diesel(table_name=games)] pub struct InsertableGame { diff --git a/shared/src/types/user.rs b/shared/src/types/user.rs index 8a3697d..258a880 100644 --- a/shared/src/types/user.rs +++ b/shared/src/types/user.rs @@ -8,6 +8,7 @@ use chrono::NaiveDateTime; use diesel::{Insertable, Queryable}; +use naia_bevy_shared::Serde; use serde::{Deserialize, Serialize}; use uuid::Uuid; @@ -24,13 +25,21 @@ pub struct User { pub password: String, } -#[derive(Deserialize)] +#[derive(Serialize, Deserialize, Clone, Serde, PartialEq)] pub struct UserPub { - pub id: Uuid, - pub created_at: NaiveDateTime, - pub updated_at: NaiveDateTime, + pub id: String, + pub created_at: String, pub username: String, - pub email: String, +} + +impl UserPub { + pub fn from_user(user: &User) -> Self { + Self { + id: user.id.to_string(), + created_at: user.created_at.to_string(), + username: user.username.to_string(), + } + } } #[derive(Deserialize, Insertable)] -- 2.44.1