From bc4c69cd73999cb067929e38dd19aa3ee8c9bdb0 Mon Sep 17 00:00:00 2001 From: Steph Flower Date: Sun, 3 Mar 2024 00:54:05 +0000 Subject: [PATCH] BE/WebSockets: 'initialised: false' sends random initial coords --- backend/Cargo.toml | 3 ++- backend/src/websocketstructs.rs | 1 - backend/src/ws/gen_json.rs | 19 +++++++++++++++---- 3 files changed, 17 insertions(+), 6 deletions(-) delete mode 100644 backend/src/websocketstructs.rs diff --git a/backend/Cargo.toml b/backend/Cargo.toml index d5f3402..9348de9 100644 --- a/backend/Cargo.toml +++ b/backend/Cargo.toml @@ -18,4 +18,5 @@ warp = "0.3" serde = { version = "1.0", features = ["derive"] } serde_json = "1.0" futures = { version = "0.3", default-features=false} -serde_repr = "0.1" \ No newline at end of file +serde_repr = "0.1" +rand = "0.8.5" diff --git a/backend/src/websocketstructs.rs b/backend/src/websocketstructs.rs deleted file mode 100644 index 8b13789..0000000 --- a/backend/src/websocketstructs.rs +++ /dev/null @@ -1 +0,0 @@ - diff --git a/backend/src/ws/gen_json.rs b/backend/src/ws/gen_json.rs index 3ef19b7..898ba6d 100644 --- a/backend/src/ws/gen_json.rs +++ b/backend/src/ws/gen_json.rs @@ -1,12 +1,14 @@ use core::fmt; use cosmic_kube::kube::Kube; use serde::{Deserialize, Serialize}; -use serde_json::json; +use serde_json::{json, Value}; use serde_repr::{Serialize_repr, Deserialize_repr}; +use rand::Rng; // this is the data we expect to recieve from the player #[derive(Serialize, Deserialize)] pub struct PlayerInfo { + initialised: bool, player: String, //Player, //the player requesting the data coordinates: [u64; 2], //current player coordinates action: Option, // 0, block picked up 1, block placed @@ -43,10 +45,19 @@ fn recalculate_game(state: PlayerInfo) -> String { } //send action to database to get result !!!<---- + let resp: Value; - let resp = json!({ - "grid" : "edited grid" - }); + if state.initialised { + resp = json!({ + "grid" : "edited grid" + }); + } else { + let mut rng = rand::thread_rng(); + resp = json!({ + "coordinates" : [rng.gen_range(0..2048), rng.gen_range(0..2048)] + }); + } + resp.to_string() }