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

Fix battle chats properly (without involving the database)

This commit is contained in:
NG (Graham)
2025-08-16 21:54:16 -04:00
parent 05bf4a097a
commit f63185c2cd
4 changed files with 10 additions and 125 deletions

View File

@@ -31,8 +31,10 @@ impl SimpleOperation<()> for JoinChannelProvider {
}
let user_info = user.user()?;
//let chat_user = super::get_chat_user(user_info.as_ref().as_ref());
self.chat_system.system_mut().await.join_channel(user_info.public_id().to_owned(), chann_name.string.clone());
let response = user_info.add_subscribed_channel(chann_name.string, crate::data::channel::ChatChannelType::from_u8(chann_ty as _)?).await?;
let ty = crate::data::channel::ChatChannelType::from_u8(chann_ty as _)?;
log::info!("User {} wants to join channel {} ({:?})", user_info.display_name().to_owned(), chann_name.string, ty);
self.chat_system.system_mut().await.join_channel(user_info.display_name().to_owned(), chann_name.string.clone(), ty);
let response = user_info.add_subscribed_channel(chann_name.string, ty).await?;
params.insert(CHANNEL_INFO_PARAM_KEY, response);
}
}

View File

@@ -24,7 +24,6 @@ impl ChatProvider {
pub struct ChatSystem {
chats: HashMap<String, super::ChatRoom>,
online_users: HashMap<String, super::UserHandle>,
battle_cache: tokio::sync::RwLock<HashMap<String, Vec<String>>>,
config: super::ChatSystemConfig,
}
@@ -69,10 +68,14 @@ impl ChatSystem {
}
}
pub fn join_channel(&mut self, display_name: String, channel: String) {
pub fn join_channel(&mut self, display_name: String, channel: String, channel_ty: crate::data::channel::ChatChannelType) {
if let Some(user_handle) = self.online_users.get(&display_name) {
if let Some(chat_room) = self.chats.get_mut(&channel) {
chat_room.connect_user(user_handle.to_owned());
} else {
let mut new_room = super::ChatRoom::new(channel.clone(), channel_ty);
new_room.connect_user(user_handle.to_owned());
self.chats.insert(channel, new_room);
}
}
self.cleanup();
@@ -102,57 +105,7 @@ impl ChatSystem {
};
room.send_public_message(event_params);
} else {
match channel_ty {
crate::data::channel::ChatChannelType::Battle | crate::data::channel::ChatChannelType::BattleTeam => {
let relevant_players = if let Some(cached) = self.battle_cache.read().await.get(&channel) {
cached.to_owned()
} else if matches!(channel_ty, crate::data::channel::ChatChannelType::BattleTeam) {
if let Ok(players) = user.get_teammates().await {
self.battle_cache.write().await.insert(channel.clone(), players.clone());
players
} else {
Vec::default()
}
} else {
if let Ok(players) = user.get_gamemates().await {
self.battle_cache.write().await.insert(channel.clone(), players.clone());
players
} else {
Vec::default()
}
};
if !relevant_players.is_empty() {
let event_params = crate::events::chat_message::PublicMessage {
sender_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(),
is_admin: user.is_admin(),
channel_name: channel,
channel_ty,
};
let event = polariton::operation::Event {
code: crate::events::chat_message::PublicMessage::CODE,
params: event_params.as_event_params(),
};
for handle in self.online_users.values() {
if handle.name() == user.public_id() { continue; }
if relevant_players.contains(&handle.name().to_owned()) {
handle.send(polariton_server::ToSend::Data {
data: polariton::packet::Data::Event(event.clone()),
encrypt: true,
channel: 0,
reliable: true,
});
}
}
}
},
_ => {
log::warn!("Got message for non-existent chat room {} (variant: {:?})", channel, channel_ty);
}
}
log::warn!("Got message for non-existent chat room {} (variant: {:?})", channel, channel_ty);
}
}
@@ -218,7 +171,6 @@ impl ChatSystem {
Ok(Self {
chats: HashMap::new(),
online_users: HashMap::new(),
battle_cache: tokio::sync::RwLock::new(HashMap::new()),
config: super::ChatSystemConfig::from_persist(config)?,
})
}