Friends functionality (#90)

### Description

Implements and closes #86

### Game

Robocraft

### Please confirm

- [x] I am the legal owner or represent the owner of all work submitted
- [x] I consent to my submission being added to this FOSS project
- [ ] This PR used LLMs to generate some or all of the code changes

Reviewed-on: https://git.ngram.ca/OpenJam/rc-servers/pulls/90
Co-authored-by: NG (Graham) <ngniusness@gmail.com>
Co-committed-by: NG (Graham) <ngniusness@gmail.com>
This commit is contained in:
NG (Graham) authored and NGnius committed 2026-02-16 04:28:14 +00:00
1 parent 872f224af6
commit 9502522ac4
30 files changed
+1108 -35

No files matched your search

+59
View File
@@ -0,0 +1,59 @@
/// Primarily keeps track of who is online so events can be sent to them.
pub struct SocialMesh {
users: tokio::sync::RwLock<std::collections::HashMap<String, UserHandle>>,
}
struct UserHandle {
emitter: polariton_server::events::WeakEventEmitter<crate::data::custom::CustomType>,
is_alive: std::sync::atomic::AtomicBool,
}
impl SocialMesh {
pub fn new() -> Self {
Self {
users: tokio::sync::RwLock::new(std::collections::HashMap::new()),
}
}
pub async fn send_event_to(&self, public_id: &str, event: impl polariton_server::events::IntoEvent<crate::data::custom::CustomType>) -> bool {
let user_lock = self.users.read().await;
if let Some(user_handle) = user_lock.get(public_id) {
let is_success = user_handle.emitter.emit(event);
user_handle.is_alive.swap(is_success, std::sync::atomic::Ordering::SeqCst);
is_success
} else {
false
}
}
pub async fn add_user(
&self,
public_id: String,
emitter: polariton_server::events::WeakEventEmitter<crate::data::custom::CustomType>,
) {
let mut user_lock = self.users.write().await;
Self::cleanup_dead_users(&mut user_lock).await;
user_lock.insert(public_id, UserHandle {
emitter,
is_alive: std::sync::atomic::AtomicBool::new(true),
});
}
/// Filter out offline users
pub async fn filter_online_only(&self, public_ids: &mut std::collections::HashSet<String>) {
let mut user_lock = self.users.write().await;
Self::cleanup_dead_users(&mut user_lock).await;
public_ids.retain(|public_id| user_lock.contains_key(public_id));
}
async fn cleanup_dead_users(users: &mut std::collections::HashMap<String, UserHandle>) {
users.retain(|_public_id, handle| handle.is_alive.load(std::sync::atomic::Ordering::SeqCst));
}
pub async fn online_count_read(&self) -> u64 {
let user_lock = self.users.read().await;
user_lock.iter()
.filter(|(_, handle)| handle.is_alive.load(std::sync::atomic::Ordering::SeqCst))
.count() as u64
}
}