Emote broadcasting

This commit is contained in:
2026-02-23 08:27:36 +01:00
parent 5cc4685588
commit a4b1ce559f
2 changed files with 54 additions and 2 deletions
+40
View File
@@ -180,6 +180,19 @@ async fn main() -> session_rs::Result<()> {
})
.await;
session
.on_request::<methods::Emote, _>({
let sessions = Arc::clone(&sessions);
move |_, emote| {
let sessions = Arc::clone(&sessions);
let uuid = Arc::clone(&uuid);
send_emote(sessions, uuid, emote)
}
})
.await;
Ok::<(), session_rs::Error>(())
}) as Pin<Box<dyn Future<Output = _> + Send>>
}
@@ -187,6 +200,31 @@ async fn main() -> session_rs::Result<()> {
.await
}
async fn send_emote(
sessions: SessionMap,
uuid: Arc<Mutex<String>>,
emote: methods::ClientEmote,
) -> Result<String, String> {
let emote_event = methods::EventEmote {
from: uuid.lock().await.clone(),
emote: emote.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::<methods::EmoteEvent>(emote_event.clone()));
notify_result.is_ok()
});
}
}
Ok(format!("Cool"))
}
#[derive(Debug, Deserialize, Serialize)]
struct MinecraftAuthResponse {
pub id: String,
@@ -234,5 +272,7 @@ async fn authenticate(
.or_default()
.insert(session);
println!("Authenticated as {:?}", auth);
user::get_put(&auth.id, &pool).await
}
+14 -2
View File
@@ -53,12 +53,24 @@ impl Method for BuyHat {
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)]
pub struct Emote;
impl Method for Emote {
const NAME: &'static str = "emote";
type Request = String;
type Request = ClientEmote;
type Response = String;
type Error = String;
}
@@ -68,7 +80,7 @@ pub struct EmoteEvent;
impl Method for EmoteEvent {
const NAME: &'static str = "emote_event";
type Request = String;
type Request = EventEmote;
type Response = String;
type Error = String;
}