1
0
mirror of https://git.ngram.ca/OpenJam/rc-servers synced 2026-08-23 23:08:52 +00:00

Fix auto-selecting chat channel, add some more config options

This commit is contained in:
NG (Graham)
2025-08-13 22:17:24 -04:00
parent a8692b2839
commit 624fee6d72
20 changed files with 316 additions and 55 deletions

View File

@@ -10,6 +10,8 @@ pub struct PublicMessage {
}
impl PublicMessage {
pub const CODE: u8 = 1;
pub fn as_event_params<C>(&self) -> polariton::operation::ParameterTable<C> {
let mut params = std::collections::HashMap::with_capacity(8);
params.insert(5, polariton::operation::Typed::Str(self.sender_name.clone().into()));
@@ -24,6 +26,32 @@ impl PublicMessage {
}
}
impl <C: Send + 'static> polariton_server::events::IntoEvent<C> for PublicMessage {
const CHANNEL: u8 = 0;
const ENCRYPT: bool = true;
const RELIABLE: bool = true;
fn into_event(self) -> polariton::operation::Event<C> {
polariton::operation::Event {
code: Self::CODE,
params: self.as_event_params().into(),
}
}
}
impl <C: Send + 'static> polariton_server::events::IntoEvent<C> for &PublicMessage {
const CHANNEL: u8 = 0;
const ENCRYPT: bool = true;
const RELIABLE: bool = true;
fn into_event(self) -> polariton::operation::Event<C> {
polariton::operation::Event {
code: PublicMessage::CODE,
params: self.as_event_params().into(),
}
}
}
pub struct PrivateMessage {
pub sender_name: String,
pub sender_display_name: String,
@@ -34,6 +62,8 @@ pub struct PrivateMessage {
}
impl PrivateMessage {
pub const CODE: u8 = 2;
pub fn as_event_params<C>(&self) -> polariton::operation::ParameterTable<C> {
let mut params = std::collections::HashMap::with_capacity(6);
params.insert(5, polariton::operation::Typed::Str(self.sender_name.clone().into()));
@@ -45,3 +75,29 @@ impl PrivateMessage {
params.into()
}
}
impl <C: Send + 'static> polariton_server::events::IntoEvent<C> for PrivateMessage {
const CHANNEL: u8 = 0;
const ENCRYPT: bool = true;
const RELIABLE: bool = true;
fn into_event(self) -> polariton::operation::Event<C> {
polariton::operation::Event {
code: Self::CODE,
params: self.as_event_params().into(),
}
}
}
impl <C: Send + 'static> polariton_server::events::IntoEvent<C> for &PrivateMessage {
const CHANNEL: u8 = 0;
const ENCRYPT: bool = true;
const RELIABLE: bool = true;
fn into_event(self) -> polariton::operation::Event<C> {
polariton::operation::Event {
code: PrivateMessage::CODE,
params: self.as_event_params().into(),
}
}
}

View File

@@ -1 +1,3 @@
pub mod chat_message;
pub mod player_update;
pub mod room_join;

View File

@@ -0,0 +1,44 @@
pub struct PlayerUpdated {
pub channel_name: String,
pub player_name: String,
pub player_state: oj_rc_core::data::channel::ChatPlayerState,
}
impl PlayerUpdated {
pub const CODE: u8 = 6;
pub const CHANNEL: u8 = 0;
pub fn as_event_params<C>(&self) -> polariton::operation::ParameterTable<C> {
let mut params = std::collections::HashMap::with_capacity(3);
params.insert(3, polariton::operation::Typed::Str(self.channel_name.clone().into()));
params.insert(22, polariton::operation::Typed::Str(self.player_name.clone().into()));
params.insert(23, polariton::operation::Typed::Int(self.player_state as _));
params.into()
}
}
impl <C: Send + 'static> polariton_server::events::IntoEvent<C> for PlayerUpdated {
const CHANNEL: u8 = Self::CHANNEL;
const ENCRYPT: bool = true;
const RELIABLE: bool = true;
fn into_event(self) -> polariton::operation::Event<C> {
polariton::operation::Event {
code: Self::CODE,
params: self.as_event_params().into(),
}
}
}
impl <C: Send + 'static> polariton_server::events::IntoEvent<C> for &PlayerUpdated {
const CHANNEL: u8 = PlayerUpdated::CHANNEL;
const ENCRYPT: bool = true;
const RELIABLE: bool = true;
fn into_event(self) -> polariton::operation::Event<C> {
polariton::operation::Event {
code: PlayerUpdated::CODE,
params: self.as_event_params().into(),
}
}
}

View File

@@ -0,0 +1,53 @@
pub struct RoomJoined {
pub channel_name: String,
pub player_name: String,
pub player_state: oj_rc_core::data::channel::ChatPlayerState,
pub use_custom_avatar: bool,
pub custom_avatar: Vec<u8>,
pub avatar_id: i32,
}
impl RoomJoined {
pub const CODE: u8 = 4;
pub const CHANNEL: u8 = 0;
pub fn as_event_params<C>(&self) -> polariton::operation::ParameterTable<C> {
let mut params = std::collections::HashMap::with_capacity(8);
params.insert(3, polariton::operation::Typed::Str(self.channel_name.clone().into()));
params.insert(22, polariton::operation::Typed::Str(self.player_name.clone().into()));
params.insert(23, polariton::operation::Typed::Int(self.player_state as _));
params.insert(24, polariton::operation::Typed::Bool(self.use_custom_avatar));
if self.use_custom_avatar {
params.insert(26, polariton::operation::Typed::Bytes(self.custom_avatar.clone().into()));
} else {
params.insert(25, polariton::operation::Typed::Int(self.avatar_id));
}
params.into()
}
}
impl <C: Send + 'static> polariton_server::events::IntoEvent<C> for RoomJoined {
const CHANNEL: u8 = Self::CHANNEL;
const ENCRYPT: bool = true;
const RELIABLE: bool = true;
fn into_event(self) -> polariton::operation::Event<C> {
polariton::operation::Event {
code: Self::CODE,
params: self.as_event_params().into(),
}
}
}
impl <C: Send + 'static> polariton_server::events::IntoEvent<C> for &RoomJoined {
const CHANNEL: u8 = RoomJoined::CHANNEL;
const ENCRYPT: bool = true;
const RELIABLE: bool = true;
fn into_event(self) -> polariton::operation::Event<C> {
polariton::operation::Event {
code: RoomJoined::CODE,
params: self.as_event_params().into(),
}
}
}

View File

@@ -1,25 +1,32 @@
//use oj_rc_core::persist::user::ChatUser;
use polariton::operation::{ParameterTable, OperationResponse};
const CODE: u8 = 11;
const CODE: u8 = 11; // subscribe all
const PARAM_KEY: u8 = 18;
async fn do_handling(params: ParameterTable<()>, user: &crate::UserTy) -> Result<ParameterTable, i16> {
async fn do_handling(params: ParameterTable<()>, user: &crate::UserTy, chat_system: &crate::state::chat::ChatImpl) -> Result<ParameterTable, i16> {
log::info!("Adding joined user to channels");
let mut params = params.to_dict();
let user_info = user.user()?;
let name = user_info.public_id().to_owned();
let channels = user_info.subscribed_channels_strings().await?;
let event_tx = user.event_chann();
chat_system.system_mut().connect_user(name, channels, event_tx);
params.insert(PARAM_KEY, user_info.subscribed_channels().await?);
Ok(params.into())
}
pub struct JoinedChannelsProvider;
pub struct JoinedChannelsProvider {
chat_system: crate::state::chat::ChatImpl,
}
#[async_trait::async_trait]
impl polariton_server::operations::Operation<()> for JoinedChannelsProvider {
type User = crate::UserTy;
async fn handle_async(&self, params: ParameterTable<()>, user: &Self::User) -> OperationResponse<()> {
polariton_server::operations::result_to_op_resp::<CODE, ()>(do_handling(params, user).await)
polariton_server::operations::result_to_op_resp::<CODE, ()>(do_handling(params, user, &self.chat_system).await)
}
}
@@ -30,6 +37,8 @@ impl polariton_server::operations::OperationCode for JoinedChannelsProvider {
}
pub(super) fn all_channels_provider() -> JoinedChannelsProvider {
JoinedChannelsProvider
pub(super) fn all_channels_provider(chat_system: crate::state::chat::ChatImpl) -> JoinedChannelsProvider {
JoinedChannelsProvider {
chat_system,
}
}

View File

@@ -15,10 +15,10 @@ use polariton_server::operations::OperationsHandler;
pub fn handler(chat_system: crate::state::chat::ChatImpl, conf: &oj_rc_core::persist::config::ConfigImpl) -> OperationsHandler<crate::UserTy> {
OperationsHandler::new()
.modify(oj_rc_core::polariton::RcOpModifier)
.add(more_auth::MoreLobbyAuth::new(chat_system.clone()))
.add(more_auth::MoreLobbyAuth::new())
.add(chat_ignores::ignores_provider())
.add(pending_sanctions::pending_sanctions_checker())
.add(all_joined_channels::all_channels_provider())
.add(all_joined_channels::all_channels_provider(chat_system.clone()))
//.add(polariton_server::operations::Ack::<12, _>::default())
.add(send_message::send_public_message_handler(chat_system.clone()))
.add(public_channels::public_channels_provider(conf))

View File

@@ -4,17 +4,14 @@ use polariton_server::operations::{Operation, OperationCode};
//use crate::persist::chat_user::{ChatUser, ChatUserImpl};
//use oj_rc_core::persist::user::ChatUser;
pub struct MoreLobbyAuth {
chat_system: crate::state::chat::ChatImpl,
}
pub struct MoreLobbyAuth;
impl MoreLobbyAuth {
const AUTH_PAYLOAD_KEY: u8 = 245;
pub fn new(chat_system: crate::state::chat::ChatImpl) -> Self {
Self {
chat_system,
}
#[inline]
pub fn new() -> Self {
Self
}
/*fn build_ext_map(&self, token: &oj_rc_core::persist::user::UserToken) -> Option<std::collections::HashMap<std::any::TypeId, Box<dyn std::any::Any + Send + Sync + 'static>>> {
@@ -33,12 +30,12 @@ impl MoreLobbyAuth {
async fn do_auth<C>(&self, params: std::collections::HashMap<u8, Typed<C>>, user: &crate::UserTy) -> Result<polariton::operation::ParameterTable<C>, i16> {
if let Some(Typed::Str(auth_payload)) = params.get(&Self::AUTH_PAYLOAD_KEY) {
if user.update_with_auth(&auth_payload.string).await {
let user_impl = user.user()?;
let name = user_impl.public_id().to_owned();
//let user_impl = user.user()?;
//let name = user_impl.public_id().to_owned();
//let chat_user = super::get_chat_user(user_impl.as_ref().as_ref());
let channels = user_impl.subscribed_channels_strings().await?;
let event_tx = user.event_chann();
self.chat_system.system_mut().connect_user(name, channels, event_tx);
//let channels = user_impl.subscribed_channels_strings().await?;
//let event_tx = user.event_chann();
//self.chat_system.system_mut().connect_user(name, channels, event_tx);
let mut resp_params = std::collections::HashMap::new();
resp_params.insert(Self::AUTH_PAYLOAD_KEY, polariton::operation::Typed::Byte(0));
return Ok(resp_params.into());

View File

@@ -1,11 +1,12 @@
//use oj_rc_core::persist::user::ChatUser;
use polariton::operation::{ParameterTable, OperationResponse};
const CODE: u8 = 12;
const CODE: u8 = 12; // get all subscribed
const PARAM_KEY: u8 = 18;
async fn do_handling(params: ParameterTable<()>, user: &crate::UserTy) -> Result<ParameterTable, i16> {
log::info!("Getting subscribed user's channels");
let mut params = params.to_dict();
let user_info = user.user()?;
params.insert(PARAM_KEY, user_info.subscribed_channels().await?);

View File

@@ -91,7 +91,7 @@ impl ChatSystem {
} else if let Some(room) = self.chats.get(&channel) {
let event_params = crate::events::chat_message::PublicMessage {
sender_name: user.public_id().to_owned(),
sender_display_name: user.public_id().to_owned(),
sender_display_name: user.display_name().to_owned(),
text,
is_dev: user.is_dev(),
is_mod: user.is_mod(),

View File

@@ -1,5 +1,6 @@
pub struct ChatRoom {
name: String,
#[allow(dead_code)]
channel: crate::data::channel::ChatChannelType,
online_users: Vec<super::UserHandle>,
}
@@ -35,11 +36,13 @@ impl ChatRoom {
}
pub fn send_public_message(&self, message: crate::events::chat_message::PublicMessage) {
let user_id = &message.sender_display_name;
let event = polariton::operation::Event {
code: 1,
params: message.as_event_params(),
};
for user in self.online_users.iter() {
if user.name() == user_id { continue; }
user.send(polariton_server::ToSend::Data {
data: polariton::packet::Data::Event(event.clone()),
encrypt: true,
@@ -60,22 +63,20 @@ impl ChatRoom {
pub fn connect_user(&mut self, handle: super::UserHandle) {
self.cleanup();
let event = polariton::operation::Event {
code: 1,
params: crate::events::chat_message::PublicMessage {
sender_name: "system".to_owned(),
sender_display_name: "system".to_owned(),
code: crate::events::room_join::RoomJoined::CODE,
params: crate::events::room_join::RoomJoined {
channel_name: self.name.clone(),
channel_ty: self.channel,
text: "joined".to_owned(),
is_dev: false,
is_mod: false,
is_admin: false,
player_name: handle.name().to_owned(),
player_state: oj_rc_core::data::channel::ChatPlayerState::Idk0,
use_custom_avatar: false,
custom_avatar: Vec::default(),
avatar_id: 0,
}.as_event_params(),
};
handle.send(polariton_server::ToSend::Data {
data: polariton::packet::Data::Event(event),
encrypt: true,
channel: 0,
channel: crate::events::room_join::RoomJoined::CHANNEL,
reliable: true,
});
self.online_users.push(handle);

View File

@@ -24,6 +24,17 @@ impl UserHandle {
}
}
/*pub fn send_later(&self, to_send: polariton_server::ToSend, wait: std::time::Duration) {
tokio::spawn(Self::send_after(self.event_tx.clone(), to_send, wait));
}
async fn send_after(event_tx: tokio::sync::mpsc::WeakUnboundedSender<polariton_server::ToSend>, to_send: polariton_server::ToSend, wait: std::time::Duration) {
tokio::time::sleep(wait).await;
if let Some(event_tx) = event_tx.upgrade() {
event_tx.send(to_send).unwrap_or_default();
}
}*/
pub fn send_private_message(&self, message: crate::events::chat_message::PrivateMessage) {
let event = polariton::operation::Event {
code: 2,