From 960a2bbe08aeb1a9f4b12c55465802857699c11b Mon Sep 17 00:00:00 2001 From: "Tristan Poland (Trident_For_U)" <34868944+tristanpoland@users.noreply.github.com> Date: Thu, 8 Aug 2024 16:54:08 -0400 Subject: [PATCH] Updated JSON parse after merge --- src/main.rs | 186 +++++++++++++++++++++++++++++++------------- src/structs.rs | 85 +++++++++++++------- src/subsystems/core | 2 +- 3 files changed, 192 insertions(+), 81 deletions(-) diff --git a/src/main.rs b/src/main.rs index c7b606d..8bbdedb 100755 --- a/src/main.rs +++ b/src/main.rs @@ -34,8 +34,11 @@ use viz::{future::ok, handler::ServiceHandler, serve, Response, Result, Router, use TerraForge; use PebbleVault; -// WARNING -// Import all structs (when we have a ton of structs this will be very bad but should be fine for now) +////////////////////////////////////////////////////////////// +// !!!! WARNING !!!! // +// Import all structs (when we have a ton of structs this // +// will be very bad but should be fine for now) // +////////////////////////////////////////////////////////////// use structs::*; ///////////////////////////////////// @@ -48,19 +51,56 @@ mod structs; mod subsystems; /////////////////////////////////////////////////////////////// -// WARNING // +// !!!! WARNING !!!! // // on_connect runs every time a new player connects to the // // server avoid putting memory hungry code here if possible! // /////////////////////////////////////////////////////////////// fn on_connect(socket: SocketRef, Data(data): Data, players: Arc>>) { + // Update the parsing functions to handle the data without Object wrappers + + fn parse_rotation(parse: &Value) -> (f64, f64, f64, f64) { + ( + parse_f64(&parse["w"]).unwrap_or(0.0), + parse_f64(&parse["x"]).unwrap_or(0.0), + parse_f64(&parse["y"]).unwrap_or(0.0), + parse_f64(&parse["z"]).unwrap_or(0.0), + ) + } + + fn parse_xyz(parse: &Value) -> (f64, f64, f64) { + ( + parse_f64(&parse["x"]).unwrap_or(0.0), + parse_f64(&parse["y"]).unwrap_or(0.0), + parse_f64(&parse["z"]).unwrap_or(0.0), + ) + } + + fn parse_xy(parse: &Value) -> (f64, f64) { + ( + parse_f64(&parse["x"]).unwrap_or(0.0), + parse_f64(&parse["y"]).unwrap_or(0.0), + ) + } + + fn parse_f64(n: &Value) -> Result { + n.as_f64().ok_or_else(|| std::io::Error::new(std::io::ErrorKind::InvalidData, "Invalid f64 value")) + } + + let id = socket.id.as_str(); println!("Starting subsystems for player: {}", id); + /////////////////////////////////////////////////////////////////// + // + // + /////////////////////////////////////////////////////////////////// + // Authenticate the user - let player = Player { + let player = Player { socket: socket.clone(), - location: None, // Initialize with no location + moveActionValue: None, + transform: None }; players.lock().unwrap().push(player); @@ -70,10 +110,10 @@ fn on_connect(socket: SocketRef, Data(data): Data, players: Arc, players: Arc>> = Arc::clone(&players); + let players_clone: Arc>> = Arc::clone(&players); socket.on( - "UpdatePlayerLocation", + "updatePlayerLocation", move |socket: SocketRef, Data::(data), Bin(bin)| { - info!( + println!( "Received event: UpdatePlayerLocation with data: {:?} and bin: {:?}", data, bin ); + // Extract location from data - match serde_json::from_value::(data.clone()) { - Ok(location) => { - let mut players: std::sync::MutexGuard> = players_clone.lock().unwrap(); - if let Some(player) = players.iter_mut().find(|p: &&mut Player| p.socket.id == socket.id) - { - player.location = Some(location); - info!("Updated player location: {:?}", player); + if let Some(transform) = data.get("transform").and_then(|t| t.as_object()) { + let mut players: std::sync::MutexGuard> = players_clone_updateloc.lock().unwrap(); + if let Some(player) = players.iter_mut().find(|p: &&mut Player| p.socket.id == socket.id) + { + // Do the actual parsing + if let (Some(rotation), Some(translation), Some(scale3d)) = ( + transform.get("rotation"), + transform.get("translation"), + transform.get("scale3D") + ) { + let (rot_w, rot_x, rot_y, rot_z) = parse_rotation(rotation); + let (trans_x, trans_y, trans_z) = parse_xyz(translation); + let (scale3d_x, scale3d_y, scale3d_z) = parse_xyz(scale3d); + + // Create or update the transform + let mut transform = player.transform.take().unwrap_or_else(|| structs::Transform { + rotation: None, + translation: None, + scale3D: structs::Scale3D { x: 1.0, y: 1.0, z: 1.0 }, + location: None, + }); + + // Update rotation + transform.rotation = Some(structs::Rotation { w: rot_w, x: rot_x, y: rot_y, z: rot_z }); + + // Update translation + let new_translation = structs::Translation { x: trans_x, y: trans_y, z: trans_z }; + transform.translation = Some(new_translation.clone()); + transform.location = Some(new_translation); + + // Update scale3D + transform.scale3D = structs::Scale3D { x: scale3d_x, y: scale3d_y, z: scale3d_z }; + + // Update the player's transform + player.transform = Some(transform); + + // Parse player movement axis values + if let Some(move_action) = data.get("move Action Value") { + let (mv_action_value_x, mv_action_value_y) = parse_xy(move_action); + player.moveActionValue = Some(structs::MoveActionValue { x: mv_action_value_x, y: mv_action_value_y }); + } + + // Print a debug statement + println!("Updated player location: {:?}", player); } else { - info!("Player not found: {}", socket.id); + println!("Invalid transform data structure"); } + } else { + println!("Player not found: {}", socket.id); } - Err(err) => { - info!("Failed to parse location: {:?}", err); - } + } else { + println!("Failed to parse location: transform field not found or is not an object"); } + + // Send a reply containing the correct data socket.bin(bin).emit("messageBack", data).ok(); }, ); - socket.on( - "message-with-ack", - move |Data::(data), ack: AckSender, Bin(bin)| { - info!( - "Received event: message-with-ack with data: {:?} and bin: {:?}", - data, bin - ); - ack.bin(bin).send(data).ok(); - }, - ); + ///////////////////////////////////////////////////////////////////////// + // Client sends this message when they need a list of online players // + ///////////////////////////////////////////////////////////////////////// - let players_clone: Arc>> = Arc::clone(&players); socket.on( "getOnlinePlayers", move |socket: SocketRef, _: Data, _: Bin| { @@ -163,17 +231,27 @@ fn on_connect(socket: SocketRef, Data(data): Data, players: Arc, _: Bin| { - println!("Responding with players and locations list"); + move |socket: SocketRef, Data::(data), ack: AckSender, Bin(bin)| { + info!("Responding with players and locations list"); let players: std::sync::MutexGuard> = players_clone.lock().unwrap(); + + match data { + Value::Null => println!("Received event with null data"), + Value::String(s) => println!("Received event with string data: {}", s), + _ => println!("Received event with data: {:?}", data), + } + println!("Binary payload: {:?}", bin); let players_with_locations_json = serde_json::to_value( players .iter() - .map(|player| json!({ "id": player.socket.id, "location": player.location })) + .map(|player| json!({ + "id": player.socket.id, + "transform": player.transform.as_ref().unwrap().location + })) .collect::>(), ) .unwrap(); - println!( + info!( "Players with Locations as JSON: {}", players_with_locations_json ); @@ -266,4 +344,4 @@ async fn main() -> Result<(), Box> { println!("{}", e); } Ok(()) -} +} \ No newline at end of file diff --git a/src/structs.rs b/src/structs.rs index 02e5a00..61853dc 100755 --- a/src/structs.rs +++ b/src/structs.rs @@ -1,3 +1,15 @@ +/////////////////////////////////////////////////////////////// +// INFORMATION // +// This file contains Horizon's global struct definitions. // +// Because of this anything that is public in this file // +// can be imported by any part of Horizon using // +// crate::structs:: // +/////////////////////////////////////////////////////////////// +// !!!! WARNING !!!! // +// Anything made public in this file *WILL* me imported by // +// main.rs // +/////////////////////////////////////////////////////////////// + use serde::{Deserialize, Serialize}; use std::net::SocketAddr; use socketioxide::extract::SocketRef; @@ -104,6 +116,9 @@ impl ChildServer { neighbors } + + // TODO: Finish this implementation and move to its own file + ////////////////////////////////////////////////////////////////////////////////////////////////////// // * Event Transmission: // // - After determining the target neighbors, the child server sends the event to the master server. // @@ -141,12 +156,6 @@ impl ChildServer { // } } -// Define a struct for Player -#[derive(Debug, Clone)] -pub struct Player { - pub socket: SocketRef, - pub location: Option, // Optional to handle players who haven't sent location updates yet -} ///////////////////////////////////////////////////////////////////////////// // World object structs: // @@ -156,36 +165,60 @@ pub struct Player { #[derive(Debug, Clone, Serialize, Deserialize)] pub struct Rotation { - w: f64, - x: f64, - y: f64, - z: f64, + pub x: f64, + pub y: f64, + pub z: f64, + pub w: f64, +} + +#[derive(Debug, Clone, Copy, Serialize, Deserialize)] +pub struct Translation { + pub x: f64, + pub y: f64, + pub z: f64, } -// Define a struct for Scale of objects #[derive(Debug, Clone, Serialize, Deserialize)] -pub struct Scale { - x: f64, - y: f64, - z: f64, +pub struct Location { + pub x: f64, + pub y: f64, + pub z: f64, } -// Define a struct for Translation of objects #[derive(Debug, Clone, Serialize, Deserialize)] -pub struct Translation { - x: f64, - y: f64, - z: f64, +pub struct Scale3D { + pub x: f64, + pub y: f64, + pub z: f64, } -// Define a struct for Location of objects #[derive(Debug, Clone, Serialize, Deserialize)] -pub struct Location { - rotation: Rotation, - scale3D: Scale, // Update field name to match the JSON data - translation: Translation, +pub struct Transform { + pub location: Option, + pub rotation: Option, + pub translation: Option, + pub scale3D: Scale3D, } +#[derive(Debug, Clone, Serialize, Deserialize)] +pub struct MoveActionValue { + pub x: f64, + pub y: f64, +} + + +//////////////////////////////// +// Define the player struct // +//////////////////////////////// + +#[derive(Debug, Clone)] +pub struct Player { + pub socket: SocketRef, + pub moveActionValue: Option, + pub transform: Option +} + + pub struct PlayerManager { players: Mutex>>, } @@ -293,4 +326,4 @@ pub struct Planet { // ], // }; // } -////////////////////////////////////////////////////////////////////////////////// +////////////////////////////////////////////////////////////////////////////////// \ No newline at end of file diff --git a/src/subsystems/core b/src/subsystems/core index 63a2de2..c47bba9 160000 --- a/src/subsystems/core +++ b/src/subsystems/core @@ -1 +1 @@ -Subproject commit 63a2de270cb621b9efb92bb9fc3719dc14a815b5 +Subproject commit c47bba95c48635462243ac17c42ab1d7aaf2ecc2