From 2a0a1ee52a1d0ba2f474061defd64ec26f5aaa05 Mon Sep 17 00:00:00 2001 From: ghostlyzsh Date: Sat, 6 Jan 2024 23:44:10 -0600 Subject: [PATCH] save theoretically works --- server/src/component.rs | 14 +++++++++ server/src/main.rs | 55 ++++++++++++++++++++++++++++------ starkingdoms-common/src/lib.rs | 17 +++++++++++ 3 files changed, 77 insertions(+), 9 deletions(-) diff --git a/server/src/component.rs b/server/src/component.rs index 592d83c812267abafe112ffb26aaadbb89ef3922..ca3cc77a85e4c2a420d728b9eb4912cf32beb24a 100644 --- a/server/src/component.rs +++ b/server/src/component.rs @@ -17,6 +17,7 @@ use std::net::SocketAddr; use bevy::prelude::*; use serde::{Deserialize, Serialize}; +use starkingdoms_common::PartType as c_PartType; #[derive(Component, Clone, Copy, Serialize, Deserialize, Debug)] pub enum PlanetType { @@ -27,12 +28,25 @@ pub enum PlanetType { #[derive(Component, Clone, Copy, PartialEq, Serialize, Deserialize, Debug)] pub enum PartType { + Placeholder, Hearty, Cargo, Hub, LandingThruster, LandingThrusterSuspension, } +impl From for starkingdoms_common::PartType { + fn from(value: PartType) -> Self { + match value { + PartType::Placeholder => c_PartType::Placeholder, + PartType::Hearty => c_PartType::Hearty, + PartType::Cargo => c_PartType::Cargo, + PartType::Hub => c_PartType::Hub, + PartType::LandingThruster => c_PartType::LandingThruster, + PartType::LandingThrusterSuspension => c_PartType::LandingThrusterSuspension, + } + } +} #[derive(Component, Clone, Debug)] pub struct Attach { diff --git a/server/src/main.rs b/server/src/main.rs index 1bd82a8966e3bc5a25094572340df2650875d2c6..e06cda9e3f01b8923da261668faa25e2c7c4aea3 100644 --- a/server/src/main.rs +++ b/server/src/main.rs @@ -26,6 +26,7 @@ use component::Input; use component::*; use packet::*; use rand::Rng; +use starkingdoms_common::SaveModule; use starkingdoms_common::{pack_savefile, unpack_savefile, SaveData}; use std::f32::consts::PI; @@ -868,15 +869,20 @@ fn on_message( } } Packet::RequestSave {} => { - // HEY! GHOSTLY! PLEASE FILL THIS STRUCT WITH DATA! - // THANKS! - let save = SaveData {}; - let save_string = pack_savefile(&app_keys.app_key, save); - let packet = Packet::SaveData { - payload: save_string, - }; - let buf = serde_json::to_vec(&packet).unwrap(); - event_queue.push(ServerEvent::Broadcast(MessageType::Text, buf)); + for (_, q_player, _, _, attach) in &mut player_query + { + if q_player.addr == *addr { + // HEY! GHOSTLY! PLEASE FILL THIS STRUCT WITH DATA! + // THANKS! + let save = SaveData { module: construct_save_data(attach.clone(), &attached_query) }; + let save_string = pack_savefile(&app_keys.app_key, save); + let packet = Packet::SaveData { + payload: save_string, + }; + let buf = serde_json::to_vec(&packet).unwrap(); + event_queue.push(ServerEvent::Broadcast(MessageType::Text, buf)); + } + } } _ => continue, } @@ -887,6 +893,37 @@ fn on_message( } } +fn construct_save_data( + attach: Attach, + attached_query: &Query< + ( + Entity, + &PartType, + &mut Transform, + &mut Attach, + &Velocity, + Option<&CanAttach>, + Option<&LooseAttach>, + ), + (Without, Without), + >, +) -> SaveModule { + let mut save_module = SaveModule { + part_type: starkingdoms_common::PartType::Placeholder, + children: vec![None, None, None, None], + }; + for (i, child) in attach.children.iter().enumerate() { + if let Some(child) = child { + let (_, part_type, _, attach, _, _, _) = attached_query.get(*child).unwrap(); + if *part_type == PartType::LandingThrusterSuspension { continue } + let child_save_module = construct_save_data(attach.clone(), attached_query); + save_module.part_type = (*part_type).into(); + save_module.children[i] = Some(child_save_module); + } + } + save_module +} + fn detach_recursive( commands: &mut Commands, attach: Attach, diff --git a/starkingdoms-common/src/lib.rs b/starkingdoms-common/src/lib.rs index d2116cd73da93c1774497df0b2ec1ed24ab458fa..9a50951e8a5649110ba65cea16fae6a65d019912 100644 --- a/starkingdoms-common/src/lib.rs +++ b/starkingdoms-common/src/lib.rs @@ -29,6 +29,23 @@ pub struct SaveData { // FILL THIS WITH STUFF // ---------------------------------------------------------------------- // THANKS! -core + pub module: SaveModule, +} + +#[derive(Serialize, Deserialize, Debug)] +pub struct SaveModule { + pub part_type: PartType, + pub children: Vec>, +} + +#[derive(Clone, Copy, PartialEq, Serialize, Deserialize, Debug)] +pub enum PartType { + Placeholder, + Hearty, + Cargo, + Hub, + LandingThruster, + LandingThrusterSuspension, } // no touchy. this is the struct that savefiles are actually represented in