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:
@@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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)?,
|
||||
})
|
||||
}
|
||||
|
||||
@@ -560,54 +560,6 @@ impl UserData {
|
||||
}
|
||||
Ok(players)
|
||||
}
|
||||
|
||||
async fn get_players_in_current_game(&self) -> Result<Vec<oj_rc_database::schema::multiplayer_game_player::Model>, polariton_server::operations::SimpleOpError> {
|
||||
let current_game = self.db.game_by_user_id_and_completion(self.account.id, false).await
|
||||
.map_err(|e| {
|
||||
log::error!("Failed to retrieve ongoing game for user {}: {}", self.account.id, e);
|
||||
polariton_server::operations::SimpleOpError::with_message(
|
||||
crate::data::error_codes::ChatErrorCodes::UnexpectedError as i16,
|
||||
format!("Failed to retrieve ongoing game for user {}", self.account.id),
|
||||
)
|
||||
})?;
|
||||
if let Some(current_game) = current_game {
|
||||
let guid = current_game.guid;
|
||||
self.db.players_by_game_guid_and_completion(current_game.guid, false).await
|
||||
.map_err(|e| {
|
||||
log::error!("Failed to retrieve players for game {} for user {}: {}", guid, self.account.id, e);
|
||||
polariton_server::operations::SimpleOpError::with_message(
|
||||
crate::data::error_codes::ChatErrorCodes::UnexpectedError as i16,
|
||||
format!("Failed to retrieve ongoing game {} for user {}", guid, self.account.id),
|
||||
)
|
||||
})
|
||||
} else {
|
||||
Ok(Vec::default())
|
||||
}
|
||||
}
|
||||
|
||||
async fn get_teammates_in_current_game(&self) -> Result<Vec<oj_rc_database::schema::multiplayer_game_player::Model>, polariton_server::operations::SimpleOpError> {
|
||||
let current_game_info = self.db.game_and_player_by_user_id_and_completion(self.account.id, false).await
|
||||
.map_err(|e| {
|
||||
log::error!("Failed to retrieve ongoing game for user {}: {}", self.account.id, e);
|
||||
polariton_server::operations::SimpleOpError::with_message(
|
||||
crate::data::error_codes::ChatErrorCodes::UnexpectedError as i16,
|
||||
format!("Failed to retrieve ongoing game for user {}", self.account.id),
|
||||
)
|
||||
})?;
|
||||
if let Some((current_game, current_player)) = current_game_info {
|
||||
let guid = current_game.guid;
|
||||
self.db.players_by_game_guid_and_completion_and_team(current_game.guid, current_player.team, false).await
|
||||
.map_err(|e| {
|
||||
log::error!("Failed to retrieve players for game {} for user {}: {}", guid, self.account.id, e);
|
||||
polariton_server::operations::SimpleOpError::with_message(
|
||||
crate::data::error_codes::ChatErrorCodes::UnexpectedError as i16,
|
||||
format!("Failed to retrieve ongoing game {} for user {}", guid, self.account.id),
|
||||
)
|
||||
})
|
||||
} else {
|
||||
Ok(Vec::default())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const INVALID_ROBOT_ERR: i16 = crate::data::error_codes::WebServicesError::InvalidRobot as i16; // 140
|
||||
@@ -1352,22 +1304,4 @@ impl super::ChatUser for UserData {
|
||||
Err(crate::data::error_codes::ChatErrorCodes::DoesNotExist as i16)
|
||||
}
|
||||
}
|
||||
|
||||
async fn get_teammates(&self) -> Result<Vec<String>, polariton_server::operations::SimpleOpError> {
|
||||
Ok(
|
||||
self.get_teammates_in_current_game().await?
|
||||
.into_iter()
|
||||
.map(|player| player.public_id)
|
||||
.collect()
|
||||
)
|
||||
}
|
||||
|
||||
async fn get_gamemates(&self) -> Result<Vec<String>, polariton_server::operations::SimpleOpError> {
|
||||
Ok(
|
||||
self.get_players_in_current_game().await?
|
||||
.into_iter()
|
||||
.map(|player| player.public_id)
|
||||
.collect()
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -211,9 +211,6 @@ pub trait ChatUser {
|
||||
//async fn has_pending_sanctions(&self) -> Result<bool, i16>;
|
||||
async fn get_sanctions(&self, username: String) -> Result<polariton::operation::Typed<()>, i16>;
|
||||
async fn set_sanction(&self, sanction: SetSanction) -> Result<(), i16>;
|
||||
// multiplayer-related
|
||||
async fn get_teammates(&self) -> Result<Vec<String>, polariton_server::operations::SimpleOpError>;
|
||||
async fn get_gamemates(&self) -> Result<Vec<String>, polariton_server::operations::SimpleOpError>;
|
||||
}
|
||||
|
||||
pub struct SetSanction {
|
||||
|
||||
Reference in New Issue
Block a user