Fixed bug and restructured

This commit is contained in:
2026-02-23 09:09:30 +01:00
parent a4b1ce559f
commit ffb88c86c3
4 changed files with 64 additions and 44 deletions
+3 -4
View File
@@ -1,9 +1,8 @@
use std::sync::Arc; use std::sync::Arc;
use sqlx::SqlitePool; use sqlx::SqlitePool;
use tokio::sync::Mutex;
use crate::user::get_put; use crate::{types::UUID, user::get_put};
pub const CLOAKS: &[&str] = &[ pub const CLOAKS: &[&str] = &[
"mercedes_flow", "mercedes_flow",
@@ -26,7 +25,7 @@ pub enum CosmeticKind {
// Buy a cosmetic // Buy a cosmetic
pub async fn buy( pub async fn buy(
kind: CosmeticKind, kind: CosmeticKind,
uuid: Arc<Mutex<String>>, uuid: UUID,
item_id: String, item_id: String,
pool: Arc<SqlitePool>, pool: Arc<SqlitePool>,
) -> Result<String, String> { ) -> Result<String, String> {
@@ -65,7 +64,7 @@ pub async fn buy(
// Equip a cosmetic // Equip a cosmetic
pub async fn equip( pub async fn equip(
kind: CosmeticKind, kind: CosmeticKind,
uuid: Arc<Mutex<String>>, uuid: UUID,
item_id: String, item_id: String,
pool: Arc<SqlitePool>, pool: Arc<SqlitePool>,
) -> Result<String, String> { ) -> Result<String, String> {
+25 -26
View File
@@ -1,21 +1,18 @@
mod cosmetics; mod cosmetics;
mod methods; mod methods;
mod types;
mod user; mod user;
use std::{ use std::{collections::HashMap, pin::Pin, sync::Arc};
collections::{HashMap, HashSet},
pin::Pin,
sync::Arc,
};
use serde::{Deserialize, Serialize};
use session_rs::{server::SessionServer, session::Session}; use session_rs::{server::SessionServer, session::Session};
use sqlx::SqlitePool; use sqlx::SqlitePool;
use tokio::sync::Mutex; use tokio::sync::Mutex;
use crate::user::User; use crate::{
types::{SessionMap, UUID},
type SessionMap = Arc<Mutex<HashMap<String, HashSet<Session>>>>; user::User,
};
#[tokio::main(flavor = "current_thread")] #[tokio::main(flavor = "current_thread")]
async fn main() -> session_rs::Result<()> { async fn main() -> session_rs::Result<()> {
@@ -33,7 +30,7 @@ async fn main() -> session_rs::Result<()> {
Box::pin(async move { Box::pin(async move {
println!("Connected"); println!("Connected");
let uuid: Arc<Mutex<String>> = Arc::new(Mutex::new(String::new())); let uuid = Arc::new(Mutex::new(String::new()));
session session
.on_close({ .on_close({
@@ -202,10 +199,10 @@ async fn main() -> session_rs::Result<()> {
async fn send_emote( async fn send_emote(
sessions: SessionMap, sessions: SessionMap,
uuid: Arc<Mutex<String>>, uuid: UUID,
emote: methods::ClientEmote, emote: types::EmoteRequest,
) -> Result<String, String> { ) -> Result<String, String> {
let emote_event = methods::EventEmote { let emote_event = types::EventEmote {
from: uuid.lock().await.clone(), from: uuid.lock().await.clone(),
emote: emote.emote, emote: emote.emote,
}; };
@@ -213,28 +210,30 @@ async fn send_emote(
for i in emote.targets { for i in emote.targets {
if let Some(sessions) = sessions.lock().await.get_mut(&i) { if let Some(sessions) = sessions.lock().await.get_mut(&i) {
let emote_event = emote_event.clone(); let emote_event = emote_event.clone();
sessions.retain(|s| { let mut bad_sessions = Vec::new();
let notify_result = tokio::runtime::Handle::current()
.block_on(s.notify::<methods::EmoteEvent>(emote_event.clone()));
notify_result.is_ok() for s in sessions.iter() {
}); if s.notify::<methods::EmoteEvent>(emote_event.clone())
.await
.is_err()
{
bad_sessions.push(s.clone());
}
}
for s in bad_sessions {
sessions.remove(&s);
}
} }
} }
Ok(format!("Cool")) Ok(format!("Cool"))
} }
#[derive(Debug, Deserialize, Serialize)]
struct MinecraftAuthResponse {
pub id: String,
pub name: String,
}
async fn authenticate( async fn authenticate(
sessions: SessionMap, sessions: SessionMap,
session: Session, session: Session,
uuid: Arc<Mutex<String>>, uuid: UUID,
session_token: String, session_token: String,
pool: Arc<SqlitePool>, pool: Arc<SqlitePool>,
) -> Result<User, String> { ) -> Result<User, String> {
@@ -258,7 +257,7 @@ async fn authenticate(
)); ));
} }
let auth: MinecraftAuthResponse = response let auth: types::MinecraftAuthResponse = response
.json() .json()
.await .await
.map_err(|_| "Unable to parse auth response".to_string())?; .map_err(|_| "Unable to parse auth response".to_string())?;
+5 -14
View File
@@ -1,7 +1,10 @@
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use session_rs::Method; use session_rs::Method;
use crate::user::User; use crate::{
types::{EmoteRequest, EventEmote},
user::User,
};
#[derive(Debug, Serialize, Deserialize)] #[derive(Debug, Serialize, Deserialize)]
pub struct Auth; pub struct Auth;
@@ -53,24 +56,12 @@ impl Method for BuyHat {
type Error = String; type Error = String;
} }
#[derive(Debug, Serialize, Deserialize)]
pub struct ClientEmote {
pub emote: String,
pub targets: Vec<String>,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct EventEmote {
pub emote: String,
pub from: String,
}
#[derive(Debug, Serialize, Deserialize)] #[derive(Debug, Serialize, Deserialize)]
pub struct Emote; pub struct Emote;
impl Method for Emote { impl Method for Emote {
const NAME: &'static str = "emote"; const NAME: &'static str = "emote";
type Request = ClientEmote; type Request = EmoteRequest;
type Response = String; type Response = String;
type Error = String; type Error = String;
} }
+31
View File
@@ -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<Mutex<HashMap<String, HashSet<Session>>>>;
pub type UUID = Arc<Mutex<String>>;
#[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<String>,
}
/// To send to the targets
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct EventEmote {
pub emote: String,
pub from: String,
}