From ffb88c86c3dc16ef923b3df33e12a6f1fc4689b1 Mon Sep 17 00:00:00 2001 From: Klesti Selimaj Date: Mon, 23 Feb 2026 09:09:30 +0100 Subject: [PATCH] Fixed bug and restructured --- src/cosmetics.rs | 7 +++---- src/main.rs | 51 ++++++++++++++++++++++++------------------------ src/methods.rs | 19 +++++------------- src/types.rs | 31 +++++++++++++++++++++++++++++ 4 files changed, 64 insertions(+), 44 deletions(-) create mode 100644 src/types.rs diff --git a/src/cosmetics.rs b/src/cosmetics.rs index 2664f75..c9b998b 100644 --- a/src/cosmetics.rs +++ b/src/cosmetics.rs @@ -1,9 +1,8 @@ use std::sync::Arc; use sqlx::SqlitePool; -use tokio::sync::Mutex; -use crate::user::get_put; +use crate::{types::UUID, user::get_put}; pub const CLOAKS: &[&str] = &[ "mercedes_flow", @@ -26,7 +25,7 @@ pub enum CosmeticKind { // Buy a cosmetic pub async fn buy( kind: CosmeticKind, - uuid: Arc>, + uuid: UUID, item_id: String, pool: Arc, ) -> Result { @@ -65,7 +64,7 @@ pub async fn buy( // Equip a cosmetic pub async fn equip( kind: CosmeticKind, - uuid: Arc>, + uuid: UUID, item_id: String, pool: Arc, ) -> Result { diff --git a/src/main.rs b/src/main.rs index e2752e1..aed9350 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,21 +1,18 @@ mod cosmetics; mod methods; +mod types; mod user; -use std::{ - collections::{HashMap, HashSet}, - pin::Pin, - sync::Arc, -}; +use std::{collections::HashMap, pin::Pin, sync::Arc}; -use serde::{Deserialize, Serialize}; use session_rs::{server::SessionServer, session::Session}; use sqlx::SqlitePool; use tokio::sync::Mutex; -use crate::user::User; - -type SessionMap = Arc>>>; +use crate::{ + types::{SessionMap, UUID}, + user::User, +}; #[tokio::main(flavor = "current_thread")] async fn main() -> session_rs::Result<()> { @@ -33,7 +30,7 @@ async fn main() -> session_rs::Result<()> { Box::pin(async move { println!("Connected"); - let uuid: Arc> = Arc::new(Mutex::new(String::new())); + let uuid = Arc::new(Mutex::new(String::new())); session .on_close({ @@ -202,10 +199,10 @@ async fn main() -> session_rs::Result<()> { async fn send_emote( sessions: SessionMap, - uuid: Arc>, - emote: methods::ClientEmote, + uuid: UUID, + emote: types::EmoteRequest, ) -> Result { - let emote_event = methods::EventEmote { + let emote_event = types::EventEmote { from: uuid.lock().await.clone(), emote: emote.emote, }; @@ -213,28 +210,30 @@ async fn send_emote( for i in emote.targets { if let Some(sessions) = sessions.lock().await.get_mut(&i) { let emote_event = emote_event.clone(); - sessions.retain(|s| { - let notify_result = tokio::runtime::Handle::current() - .block_on(s.notify::(emote_event.clone())); + let mut bad_sessions = Vec::new(); - notify_result.is_ok() - }); + for s in sessions.iter() { + if s.notify::(emote_event.clone()) + .await + .is_err() + { + bad_sessions.push(s.clone()); + } + } + + for s in bad_sessions { + sessions.remove(&s); + } } } Ok(format!("Cool")) } -#[derive(Debug, Deserialize, Serialize)] -struct MinecraftAuthResponse { - pub id: String, - pub name: String, -} - async fn authenticate( sessions: SessionMap, session: Session, - uuid: Arc>, + uuid: UUID, session_token: String, pool: Arc, ) -> Result { @@ -258,7 +257,7 @@ async fn authenticate( )); } - let auth: MinecraftAuthResponse = response + let auth: types::MinecraftAuthResponse = response .json() .await .map_err(|_| "Unable to parse auth response".to_string())?; diff --git a/src/methods.rs b/src/methods.rs index 41e7f02..a347359 100644 --- a/src/methods.rs +++ b/src/methods.rs @@ -1,7 +1,10 @@ use serde::{Deserialize, Serialize}; use session_rs::Method; -use crate::user::User; +use crate::{ + types::{EmoteRequest, EventEmote}, + user::User, +}; #[derive(Debug, Serialize, Deserialize)] pub struct Auth; @@ -53,24 +56,12 @@ impl Method for BuyHat { type Error = String; } -#[derive(Debug, Serialize, Deserialize)] -pub struct ClientEmote { - pub emote: String, - pub targets: Vec, -} - -#[derive(Debug, Clone, Serialize, Deserialize)] -pub struct EventEmote { - pub emote: String, - pub from: String, -} - #[derive(Debug, Serialize, Deserialize)] pub struct Emote; impl Method for Emote { const NAME: &'static str = "emote"; - type Request = ClientEmote; + type Request = EmoteRequest; type Response = String; type Error = String; } diff --git a/src/types.rs b/src/types.rs new file mode 100644 index 0000000..834014d --- /dev/null +++ b/src/types.rs @@ -0,0 +1,31 @@ +use serde::{Deserialize, Serialize}; +use session_rs::session::Session; +use tokio::sync::Mutex; + +use std::{ + collections::{HashMap, HashSet}, + sync::Arc, +}; + +pub type SessionMap = Arc>>>; +pub type UUID = Arc>; + +#[derive(Debug, Deserialize, Serialize)] +pub struct MinecraftAuthResponse { + pub id: String, + pub name: String, +} + +#[derive(Debug, Serialize, Deserialize)] +/// Received from the client to send to the targets +pub struct EmoteRequest { + pub emote: String, + pub targets: Vec, +} + +/// To send to the targets +#[derive(Debug, Clone, Serialize, Deserialize)] +pub struct EventEmote { + pub emote: String, + pub from: String, +}