mirror of
https://git.ngram.ca/OpenJam/rc-servers
synced 2026-08-23 23:08:52 +00:00
Implement non-multiplayer parts of custom games
This commit is contained in:
@@ -36,6 +36,7 @@ mod clan_invite_decline_all;
|
||||
mod clan_invite_cancel;
|
||||
mod clan_member_rerank;
|
||||
mod clan_experience_poll;
|
||||
mod user_can_be_custom_gamed;
|
||||
|
||||
use polariton_server::operations::OperationsHandler;
|
||||
|
||||
@@ -66,6 +67,7 @@ pub fn handler(init_ctx: &crate::InitConfig) -> OperationsHandler<crate::UserTy,
|
||||
.add(friend_remove::friend_remove_provider(init_ctx))
|
||||
.add(platoon_invite_to::platoon_invite_provider(init_ctx))
|
||||
.add(user_can_be_platooned::can_invite_to_platoon_provider(init_ctx))
|
||||
.add(user_can_be_custom_gamed::can_invite_to_custom_game_provider(init_ctx))
|
||||
.add(platoon_invite_accept::platoon_accepter_provider(init_ctx))
|
||||
.add(platoon_invite_decline::platoon_decliner_provider(init_ctx))
|
||||
.add(platoon_leave::platoon_leave_provider(init_ctx))
|
||||
|
||||
@@ -96,25 +96,26 @@ impl SimpleOperation<crate::data::custom::CustomType> for PlatoonInviter {
|
||||
}
|
||||
} else {
|
||||
// create new platoon
|
||||
let platoon_id = format!("{}-{}", user_info.public_id(), chrono::Utc::now().timestamp());
|
||||
log::debug!("Creating new platoon {} for user {} to invite {}", platoon_id, user_info.public_id(), username.string);
|
||||
let social_infos = user_info.list_social_info(&[
|
||||
username.string.clone(),
|
||||
user_info.public_id().to_owned(),
|
||||
]).await?;
|
||||
if social_infos.len() != 2 {
|
||||
log::debug!("User {} info could not be retrieved while creating platoon {}", username.string, platoon_id);
|
||||
log::debug!("User {} info could not be retrieved while creating new platoon", username.string);
|
||||
return Err(SimpleOpError::with_message(
|
||||
oj_rc_core::data::error_codes::SocialErrorCode::UserDoesNotExist as i16,
|
||||
"User's info could not be retrieved".to_owned(),
|
||||
));
|
||||
}
|
||||
if self.social.create_platoon(&platoon_id, user_info.public_id()).await.is_none() {
|
||||
let platoon_id = if let Some((_, platoon_key)) = self.social.create_platoon(user_info.public_id()).await {
|
||||
log::debug!("Created new platoon {} for user {} to invite {}", platoon_key, user_info.public_id(), username.string);
|
||||
platoon_key
|
||||
} else {
|
||||
return Err(SimpleOpError::with_message(
|
||||
oj_rc_core::data::error_codes::SocialErrorCode::UserNotInPlatoon as i16,
|
||||
"Failed to create platoon".to_owned(),
|
||||
));
|
||||
}
|
||||
};
|
||||
if let Some(timestamp) = self.social.add_user_to_platoon(&username.string, &platoon_id, crate::data::platoon::MemberStatus::Invited).await {
|
||||
(platoon_id, social_infos, timestamp)
|
||||
} else {
|
||||
|
||||
42
rc_social_room/src/operations/user_can_be_custom_gamed.rs
Normal file
42
rc_social_room/src/operations/user_can_be_custom_gamed.rs
Normal file
@@ -0,0 +1,42 @@
|
||||
use polariton_server::operations::{SimpleOpError, SimpleOperation, SimpleOpImpl};
|
||||
use polariton::operation::{ParameterTable, Typed};
|
||||
|
||||
const CODE: u8 = 58;
|
||||
|
||||
const USERNAME_PARAM_KEY: u8 = 65; // str; in
|
||||
const RESPONSE_CODE_PARAM_KEY: u8 = 66; // bool; out
|
||||
|
||||
pub(super) struct UserCanBeInvitedToCustoGameGetter {
|
||||
social: std::sync::Arc<crate::SocialMesh>,
|
||||
}
|
||||
|
||||
#[async_trait::async_trait]
|
||||
impl SimpleOperation<crate::data::custom::CustomType> for UserCanBeInvitedToCustoGameGetter {
|
||||
type User = crate::UserTy;
|
||||
const CODE: u8 = CODE;
|
||||
|
||||
async fn handle(&self, mut params: ParameterTable<crate::data::custom::CustomType>, user: &Self::User) -> Result<ParameterTable<crate::data::custom::CustomType>, SimpleOpError> {
|
||||
if let Some(Typed::Str(username)) = params.remove(&USERNAME_PARAM_KEY) {
|
||||
let _user_info = user.user()?; // just to validate request is authenticated
|
||||
let mut set = std::collections::HashSet::with_capacity(1);
|
||||
set.insert(username.string.clone());
|
||||
self.social.filter_online_only(&mut set).await;
|
||||
if set.is_empty() {
|
||||
params.insert(RESPONSE_CODE_PARAM_KEY, Typed::Bool(false));
|
||||
return Ok(params);
|
||||
}
|
||||
if self.social.platoon_of_user(&username.string).await.is_some() {
|
||||
params.insert(RESPONSE_CODE_PARAM_KEY, Typed::Bool(false));
|
||||
return Ok(params);
|
||||
}
|
||||
params.insert(RESPONSE_CODE_PARAM_KEY, Typed::Bool(true));
|
||||
}
|
||||
Ok(params)
|
||||
}
|
||||
}
|
||||
|
||||
pub(super) fn can_invite_to_custom_game_provider(init_ctx: &crate::InitConfig) -> SimpleOpImpl<crate::data::custom::CustomType, crate::UserTy, UserCanBeInvitedToCustoGameGetter> {
|
||||
SimpleOpImpl::new(UserCanBeInvitedToCustoGameGetter {
|
||||
social: init_ctx.social.clone(),
|
||||
})
|
||||
}
|
||||
@@ -27,6 +27,11 @@ pub struct PlatoonMemberInfo {
|
||||
pub timestamp: i64,
|
||||
}
|
||||
|
||||
fn platoon_key(creator: &str) -> String {
|
||||
let now = chrono::Utc::now().timestamp();
|
||||
format!("{}_{}_p", creator, now)
|
||||
}
|
||||
|
||||
impl SocialMesh {
|
||||
#[allow(clippy::new_without_default)]
|
||||
pub fn new() -> Self {
|
||||
@@ -121,13 +126,14 @@ impl SocialMesh {
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn create_platoon(&self, platoon_id: &str, public_id: &str) -> Option<i64> {
|
||||
pub async fn create_platoon(&self, public_id: &str) -> Option<(i64, String)> {
|
||||
/*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) {
|
||||
let platoon_id = platoon_key(public_id);
|
||||
if self.platoons.platoon_by_id.read().await.contains_key(&platoon_id) {
|
||||
return None;
|
||||
}
|
||||
let mut platoon_members = Vec::with_capacity(5);
|
||||
@@ -140,7 +146,7 @@ impl SocialMesh {
|
||||
});
|
||||
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)
|
||||
Some((timestamp, platoon_id))
|
||||
}
|
||||
|
||||
pub async fn remove_user_from_platoon(&self, public_id: &str) -> bool {
|
||||
|
||||
Reference in New Issue
Block a user