From a0409c676374029f5db85df4cf97c2347c8ae16f Mon Sep 17 00:00:00 2001 From: Klesti Selimaj Date: Mon, 23 Feb 2026 18:42:35 +0100 Subject: [PATCH] Fixed --- src/cosmetics.rs | 44 ++++++++++++++++++++++++++++---------------- src/methods/auth.rs | 15 +++++++++++---- src/methods/user.rs | 13 ++++++++----- src/types.rs | 15 +++++++++++++++ 4 files changed, 62 insertions(+), 25 deletions(-) diff --git a/src/cosmetics.rs b/src/cosmetics.rs index c9b998b..372b505 100644 --- a/src/cosmetics.rs +++ b/src/cosmetics.rs @@ -1,18 +1,20 @@ +use sqlx::SqlitePool; use std::sync::Arc; -use sqlx::SqlitePool; - -use crate::{types::UUID, user::get_put}; +use crate::{types::UUID, user}; pub const CLOAKS: &[&str] = &[ - "mercedes_flow", "glitch", + "mercedes_flow", "crimson_mark", "bmw", "amg", "amg_petronas", "ferrari", "redbull", + "black_hole_amethyst", + "black_hole_flame", + "black_hole_white", ]; pub const HATS: &[&str] = &["horns_black", "horns_white", "halo", "halo_black"]; @@ -29,31 +31,36 @@ pub async fn buy( item_id: String, pool: Arc, ) -> Result { - let mut user = get_put(uuid.lock().await.as_str(), &pool).await?; + // Lock once + let uuid = uuid.lock().await.clone(); + + let mut user = user::get(&uuid, &pool).await?; match kind { CosmeticKind::Hat => { if !HATS.contains(&item_id.as_str()) { return Err("Hat does not exist".into()); } - if !user.hats.contains(&item_id.to_string()) { - user.hats.push(item_id.to_string()); + + if !user.hats.contains(&item_id) { + user.hats.push(item_id.clone()); } } CosmeticKind::Cloak => { if !CLOAKS.contains(&item_id.as_str()) { return Err("Cloak does not exist".into()); } - if !user.cloaks.contains(&item_id.to_string()) { - user.cloaks.push(item_id.to_string()); + + if !user.cloaks.contains(&item_id) { + user.cloaks.push(item_id.clone()); } } } sqlx::query("UPDATE users SET cloaks = ?, hats = ? WHERE uuid = ?") - .bind(serde_json::to_string(&user.cloaks).unwrap()) - .bind(serde_json::to_string(&user.hats).unwrap()) - .bind(uuid.lock().await.as_str()) + .bind(serde_json::to_string(&user.cloaks).map_err(|e| e.to_string())?) + .bind(serde_json::to_string(&user.hats).map_err(|e| e.to_string())?) + .bind(&uuid) .execute(pool.as_ref()) .await .map_err(|e| e.to_string())?; @@ -68,27 +75,32 @@ pub async fn equip( item_id: String, pool: Arc, ) -> Result { - let mut user = get_put(uuid.lock().await.as_str(), &pool).await?; + // Lock once + let uuid = uuid.lock().await.clone(); + + let mut user = user::get(&uuid, &pool).await?; match kind { CosmeticKind::Hat => { if !user.hats.contains(&item_id) { return Err("You don't own this hat".into()); } - user.hat = item_id.to_string(); + + user.hat = item_id.clone(); } CosmeticKind::Cloak => { if !user.cloaks.contains(&item_id) { return Err("You don't own this cloak".into()); } - user.cloak = item_id.to_string(); + + user.cloak = item_id.clone(); } } sqlx::query("UPDATE users SET cloak = ?, hat = ? WHERE uuid = ?") .bind(&user.cloak) .bind(&user.hat) - .bind(uuid.lock().await.as_str()) + .bind(&uuid) .execute(pool.as_ref()) .await .map_err(|e| e.to_string())?; diff --git a/src/methods/auth.rs b/src/methods/auth.rs index 651ba80..265684c 100644 --- a/src/methods/auth.rs +++ b/src/methods/auth.rs @@ -36,10 +36,17 @@ pub async fn authenticate( )); } - let auth: crate::types::MinecraftAuthResponse = response - .json() + let auth = response + .json::() .await - .map_err(|_| "Unable to parse auth response".to_string())?; + .map_err(|_| "Unable to parse auth response".to_string()) + .and_then(|auth| { + let id = crate::types::format_uuid(&auth.id)?; + Ok(crate::types::MinecraftAuthResponse { + name: auth.name, + id, + }) + })?; *uuid.lock().await = auth.id.clone(); *name.lock().await = auth.name.clone(); @@ -51,7 +58,7 @@ pub async fn authenticate( .or_default() .insert(session); - println!("Authenticated as {:?}", auth); + println!("{:?}", auth); crate::user::get_put(&auth.id, &pool).await } diff --git a/src/methods/user.rs b/src/methods/user.rs index 76ed898..a046e3a 100644 --- a/src/methods/user.rs +++ b/src/methods/user.rs @@ -27,15 +27,16 @@ pub async fn send_user( targets: Vec, pool: Arc, ) -> Result<(), String> { + println!("send player {targets:?}"); + let uuid = uuid.lock().await.to_string(); + let user = PlayerStream { - player: crate::user::get(&uuid.lock().await, pool.as_ref()).await?, - uuid: uuid.lock().await.clone(), + player: crate::user::get(&uuid, pool.as_ref()).await?, + uuid: uuid.clone(), name: name.lock().await.clone(), }; - println!("{user:?}"); - - println!("{targets:?}"); + println!("ready {targets:?}"); for target in targets { if let Some(sessions) = sessions.lock().await.get_mut(&target) { @@ -53,5 +54,7 @@ pub async fn send_user( } } + println!("sent"); + Ok(()) } diff --git a/src/types.rs b/src/types.rs index 4eee104..6e6642f 100644 --- a/src/types.rs +++ b/src/types.rs @@ -38,3 +38,18 @@ pub struct PlayerStream { pub uuid: String, pub name: String, } + +pub fn format_uuid(input: &str) -> Result { + if input.len() != 32 { + return Err("Input must be exactly 32 hex characters"); + } + + Ok(format!( + "{}-{}-{}-{}-{}", + &input[0..8], + &input[8..12], + &input[12..16], + &input[16..20], + &input[20..32], + )) +}