mirror of
https://git.ngram.ca/OpenJam/rc-servers
synced 2026-08-23 23:08:52 +00:00
Platoons social functionality (#91)
### Description This implements and completes #87 (to the best of my knowledge) ### 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/91 Co-authored-by: NG (Graham) <ngniusness@gmail.com> Co-committed-by: NG (Graham) <ngniusness@gmail.com>
This commit is contained in:
1 parent
a82488ab3a
commit
50b396f34a
49 files changed
+1152
-47
No files matched your search
@@ -1,6 +1,7 @@
|
||||
/// 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>>,
|
||||
users: tokio::sync::RwLock<std::collections::HashMap<String, std::sync::Arc<UserHandle>>>,
|
||||
platoons: Platoons,
|
||||
}
|
||||
|
||||
struct UserHandle {
|
||||
@@ -8,10 +9,33 @@ struct UserHandle {
|
||||
is_alive: std::sync::atomic::AtomicBool,
|
||||
}
|
||||
|
||||
struct Platoons {
|
||||
platoon_by_id: tokio::sync::RwLock<std::collections::HashMap<String, Vec<PlatoonMember>>>,
|
||||
platoon_by_user: tokio::sync::RwLock<std::collections::HashMap<String, String>>, // public_id -> platoon_id
|
||||
}
|
||||
|
||||
struct PlatoonMember {
|
||||
public_id: String,
|
||||
//handle: std::sync::Arc<UserHandle>,
|
||||
status: crate::data::platoon::MemberStatus,
|
||||
timestamp: i64,
|
||||
}
|
||||
|
||||
pub struct PlatoonMemberInfo {
|
||||
pub public_id: String,
|
||||
pub status: crate::data::platoon::MemberStatus,
|
||||
pub timestamp: i64,
|
||||
}
|
||||
|
||||
impl SocialMesh {
|
||||
#[allow(clippy::new_without_default)]
|
||||
pub fn new() -> Self {
|
||||
Self {
|
||||
users: tokio::sync::RwLock::new(std::collections::HashMap::new()),
|
||||
platoons: Platoons {
|
||||
platoon_by_id: tokio::sync::RwLock::new(std::collections::HashMap::new()),
|
||||
platoon_by_user: tokio::sync::RwLock::new(std::collections::HashMap::new()),
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
@@ -33,10 +57,10 @@ impl SocialMesh {
|
||||
) {
|
||||
let mut user_lock = self.users.write().await;
|
||||
Self::cleanup_dead_users(&mut user_lock).await;
|
||||
user_lock.insert(public_id, UserHandle {
|
||||
user_lock.insert(public_id, std::sync::Arc::new(UserHandle {
|
||||
emitter,
|
||||
is_alive: std::sync::atomic::AtomicBool::new(true),
|
||||
});
|
||||
}));
|
||||
}
|
||||
|
||||
/// Filter out offline users
|
||||
@@ -46,7 +70,7 @@ impl SocialMesh {
|
||||
public_ids.retain(|public_id| user_lock.contains_key(public_id));
|
||||
}
|
||||
|
||||
async fn cleanup_dead_users(users: &mut std::collections::HashMap<String, UserHandle>) {
|
||||
async fn cleanup_dead_users(users: &mut std::collections::HashMap<String, std::sync::Arc<UserHandle>>) {
|
||||
users.retain(|_public_id, handle| handle.is_alive.load(std::sync::atomic::Ordering::SeqCst));
|
||||
}
|
||||
|
||||
@@ -56,4 +80,112 @@ impl SocialMesh {
|
||||
.filter(|(_, handle)| handle.is_alive.load(std::sync::atomic::Ordering::SeqCst))
|
||||
.count() as u64
|
||||
}
|
||||
|
||||
pub async fn platoon_of_user(&self, public_id: &str) -> Option<String> {
|
||||
self.platoons.platoon_by_user.read().await
|
||||
.get(public_id)
|
||||
.map(|x| x.to_owned())
|
||||
}
|
||||
|
||||
pub async fn users_of_platoon(&self, platoon_id: &str) -> Vec<PlatoonMemberInfo> {
|
||||
self.platoons.platoon_by_id.read().await
|
||||
.get(platoon_id)
|
||||
.map(|plat| plat.iter()
|
||||
.map(|member| PlatoonMemberInfo {
|
||||
public_id: member.public_id.clone(),
|
||||
status: member.status,
|
||||
timestamp: member.timestamp,
|
||||
})
|
||||
.collect()
|
||||
).unwrap_or_default()
|
||||
}
|
||||
|
||||
pub async fn add_user_to_platoon(&self, public_id: &str, platoon_id: &str, status: crate::data::platoon::MemberStatus) -> Option<i64> {
|
||||
/*let user_handle = if let Some(handle) = self.users.read().await.get(public_id) {
|
||||
handle.to_owned()
|
||||
} else {
|
||||
return None;
|
||||
};*/
|
||||
if let Some(platoon) = self.platoons.platoon_by_id.write().await.get_mut(platoon_id) {
|
||||
let timestamp = chrono::Utc::now().timestamp();
|
||||
platoon.push(PlatoonMember {
|
||||
public_id: public_id.to_owned(),
|
||||
//handle: user_handle,
|
||||
status,
|
||||
timestamp,
|
||||
});
|
||||
self.platoons.platoon_by_user.write().await.insert(public_id.to_owned(), platoon_id.to_owned());
|
||||
Some(timestamp)
|
||||
} else {
|
||||
None
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn create_platoon(&self, platoon_id: &str, public_id: &str) -> Option<i64> {
|
||||
/*let user_handle = if let Some(handle) = self.users.read().await.get(public_id) {
|
||||
handle.to_owned()
|
||||
} else {
|
||||
return None;
|
||||
};*/
|
||||
if self.platoons.platoon_by_id.read().await.contains_key(platoon_id) {
|
||||
return None;
|
||||
}
|
||||
let mut platoon_members = Vec::with_capacity(5);
|
||||
let timestamp = chrono::Utc::now().timestamp();
|
||||
platoon_members.push(PlatoonMember {
|
||||
public_id: public_id.to_owned(),
|
||||
//handle: user_handle,
|
||||
status: crate::data::platoon::MemberStatus::Ready,
|
||||
timestamp,
|
||||
});
|
||||
self.platoons.platoon_by_id.write().await.insert(platoon_id.to_owned(), platoon_members);
|
||||
self.platoons.platoon_by_user.write().await.insert(public_id.to_owned(), platoon_id.to_owned());
|
||||
Some(timestamp)
|
||||
}
|
||||
|
||||
pub async fn remove_user_from_platoon(&self, public_id: &str) -> bool {
|
||||
if let Some(platoon_id) = self.platoons.platoon_by_user.write().await.remove(public_id) {
|
||||
let mut platoon_by_id_lock = self.platoons.platoon_by_id.write().await;
|
||||
if let Some(platoon) = platoon_by_id_lock.get_mut(&platoon_id) {
|
||||
platoon.retain(|member| member.public_id != public_id);
|
||||
if platoon.is_empty() {
|
||||
platoon_by_id_lock.remove(&platoon_id);
|
||||
}
|
||||
true
|
||||
} else {
|
||||
false
|
||||
}
|
||||
} else {
|
||||
false
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn update_user_in_platoon(&self, public_id: &str, status: crate::data::platoon::MemberStatus) -> bool {
|
||||
if let Some(platoon_id) = self.platoons.platoon_by_user.write().await.get_mut(public_id) {
|
||||
if let Some(platoon) = self.platoons.platoon_by_id.write().await.get_mut(platoon_id) {
|
||||
for member in platoon.iter_mut() {
|
||||
if member.public_id != public_id { continue; }
|
||||
member.status = status;
|
||||
break;
|
||||
}
|
||||
true
|
||||
} else {
|
||||
false
|
||||
}
|
||||
} else {
|
||||
false
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn remove_platoon(&self, platoon_id: &str) -> bool {
|
||||
if let Some(platoon) = self.platoons.platoon_by_id.write().await.remove(platoon_id) {
|
||||
let mut platoon_by_user_lock = self.platoons.platoon_by_user.write().await;
|
||||
for member in platoon {
|
||||
platoon_by_user_lock.remove(&member.public_id);
|
||||
}
|
||||
true
|
||||
} else {
|
||||
false
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user