mirror of
https://git.ngram.ca/OpenJam/rc-servers
synced 2026-08-23 23:08:52 +00:00
Add proof-of-concept server intercom service through auth server
This commit is contained in:
20
rc_services_room/src/events/dev_message.rs
Normal file
20
rc_services_room/src/events/dev_message.rs
Normal file
@@ -0,0 +1,20 @@
|
||||
pub struct DevMessage {
|
||||
pub message: String,
|
||||
pub duration: i32,
|
||||
}
|
||||
|
||||
impl <C: Send + Sync + 'static> polariton_server::events::IntoEvent<C> for DevMessage {
|
||||
const CHANNEL: u8 = 0;
|
||||
const ENCRYPT: bool = true;
|
||||
const RELIABLE: bool = true;
|
||||
|
||||
fn into_event(self) -> polariton::operation::Event<C> {
|
||||
let mut params = polariton::operation::ParameterTable::with_capacity(2);
|
||||
params.insert(2, polariton::operation::Typed::Bytes(self.message.as_bytes().to_vec().into()));
|
||||
params.insert(15, polariton::operation::Typed::Int(self.duration));
|
||||
polariton::operation::Event {
|
||||
code: 1,
|
||||
params,
|
||||
}
|
||||
}
|
||||
}
|
||||
58
rc_services_room/src/events/handler.rs
Normal file
58
rc_services_room/src/events/handler.rs
Normal file
@@ -0,0 +1,58 @@
|
||||
use oj_rc_core::persist::user::IntercomListener;
|
||||
use oj_rc_core::persist::user::intercom::IntercomWebServiceUserMessage;
|
||||
|
||||
pub struct IntercomHandler {
|
||||
listener: IntercomListener<IntercomWebServiceUserMessage>,
|
||||
user: std::sync::Weak<Box<dyn oj_rc_core::persist::user::User<()> + Send + Sync>>,
|
||||
emitter: polariton_server::events::EventEmitter<()>,
|
||||
}
|
||||
|
||||
impl IntercomHandler {
|
||||
pub fn new(
|
||||
listener: IntercomListener<IntercomWebServiceUserMessage>,
|
||||
user: &std::sync::Arc<Box<dyn oj_rc_core::persist::user::User<()> + Send + Sync>>,
|
||||
emitter: &polariton_server::events::EventEmitter<()>,
|
||||
) -> Self {
|
||||
Self {
|
||||
listener,
|
||||
user: std::sync::Arc::downgrade(user),
|
||||
emitter: emitter.to_owned(),
|
||||
}
|
||||
}
|
||||
|
||||
async fn run_loop(
|
||||
listener: IntercomListener<IntercomWebServiceUserMessage>,
|
||||
user: std::sync::Weak<Box<dyn oj_rc_core::persist::user::User<()> + Send + Sync>>,
|
||||
emitter: polariton_server::events::EventEmitter<()>
|
||||
) {
|
||||
use futures::StreamExt;
|
||||
let mut listener = listener.listen().await;
|
||||
while let Some(msg) = listener.next().await {
|
||||
match msg {
|
||||
Ok(msg) => {
|
||||
if let Some(_user) = user.upgrade() {
|
||||
match msg {
|
||||
IntercomWebServiceUserMessage::DevMessage(msg) => {
|
||||
let event = super::DevMessage {
|
||||
message: msg.message,
|
||||
duration: msg.duration as i32,
|
||||
};
|
||||
emitter.emit(event);
|
||||
},
|
||||
}
|
||||
} else {
|
||||
break;
|
||||
}
|
||||
},
|
||||
Err(e) => {
|
||||
log::error!("Bad intercom message received: {}", e);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
pub fn run(self) -> tokio::task::JoinHandle<()> {
|
||||
tokio::spawn(Self::run_loop(self.listener, self.user, self.emitter))
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
mod handler;
|
||||
pub use handler::IntercomHandler;
|
||||
|
||||
mod dev_message;
|
||||
pub use dev_message::DevMessage;
|
||||
|
||||
@@ -16,7 +16,8 @@ impl <C: Send + 'static> Operation<C> for MoreLobbyAuth {
|
||||
if let Some(Typed::Str(auth_payload)) = params_dict.get(&Self::AUTH_PAYLOAD_KEY) {
|
||||
//let mut write_lock = user.write().unwrap();
|
||||
if user.update_with_auth(&auth_payload.string).await {
|
||||
if user.user().unwrap().is_banned() {
|
||||
let user_info = user.user().unwrap();
|
||||
if user_info.is_banned() {
|
||||
return polariton::operation::OperationResponse {
|
||||
code: Self::op_code(),
|
||||
return_code: oj_rc_core::data::error_codes::WebServicesError::Banned as i16,
|
||||
@@ -24,13 +25,27 @@ impl <C: Send + 'static> Operation<C> for MoreLobbyAuth {
|
||||
params: polariton::operation::ParameterTable::with_capacity(0),
|
||||
}
|
||||
} else {
|
||||
let mut resp_params = std::collections::HashMap::with_capacity(1);
|
||||
resp_params.insert(Self::AUTH_PAYLOAD_KEY, polariton::operation::Typed::Byte(0));
|
||||
return polariton::operation::OperationResponse {
|
||||
code: Self::op_code(),
|
||||
return_code: 0,
|
||||
message: polariton::operation::Typed::Null,
|
||||
params: resp_params.into(),
|
||||
match user_info.webservice_listener().await {
|
||||
Ok(listener) => {
|
||||
let mut resp_params = std::collections::HashMap::with_capacity(1);
|
||||
resp_params.insert(Self::AUTH_PAYLOAD_KEY, polariton::operation::Typed::Byte(0));
|
||||
crate::events::IntercomHandler::new(listener, &user_info, user.event_sender()).run();
|
||||
return polariton::operation::OperationResponse {
|
||||
code: Self::op_code(),
|
||||
return_code: 0,
|
||||
message: polariton::operation::Typed::Null,
|
||||
params: resp_params.into(),
|
||||
}
|
||||
},
|
||||
Err(e) => {
|
||||
log::error!("Failed to start web service intercom listener for user {}: {}", user_info.public_id(), e.error_msg().map(|x| x.to_owned()).unwrap_or("".to_string()));
|
||||
return polariton::operation::OperationResponse {
|
||||
code: Self::op_code(),
|
||||
return_code: oj_rc_core::data::error_codes::WebServicesError::PlatformFeatureNotAvailable as i16,
|
||||
message: polariton::operation::Typed::Null,
|
||||
params: polariton::operation::ParameterTable::with_capacity(0),
|
||||
}
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user