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

Implement lobby parts of custom games #38

This commit is contained in:
NG (Graham)
2026-03-17 21:52:15 -04:00
parent af04c4093c
commit 6a953bc613
32 changed files with 1045 additions and 61 deletions

View File

@@ -0,0 +1,74 @@
//use oj_rc_core::persist::user::IntercomListener;
use oj_rc_core::persist::user::intercom::IntercomLobbyStateMessage;
const RETRY_TIMEOUT: std::time::Duration = std::time::Duration::from_secs(15);
pub struct IntercomHandler<U: oj_rc_core::persist::user::Userless> {
userless: std::sync::Arc<U>,
lobby: std::sync::Arc<crate::QueueHandler>,
}
impl <U: oj_rc_core::persist::user::Userless + 'static> IntercomHandler<U> {
pub fn new(
userless: std::sync::Arc<U>,
lobby: std::sync::Arc<crate::QueueHandler>,
) -> Self {
Self {
userless,
lobby,
}
}
async fn run_loop(
userless: std::sync::Arc<U>,
lobby: std::sync::Arc<crate::QueueHandler>,
) {
loop {
let listener = match userless.lobby_state_listener().await {
Ok(listener) => {
log::debug!("Connected lobby state intercom listener");
listener
},
Err(e) => {
log::error!("Failed to connect to lobby state intercom: {} (retrying in {}s)", e, RETRY_TIMEOUT.as_secs());
tokio::time::sleep(RETRY_TIMEOUT).await;
continue;
}
};
use futures::StreamExt;
let mut listener = listener.listen().await;
while let Some(msg) = listener.next().await {
match msg {
Ok(msg) => {
match msg {
IntercomLobbyStateMessage::CustomGame(state) => {
if state.users.is_empty() {
// disband
lobby.remove_custom_queue(&state.session_id).await;
} else {
let is_create = lobby.update_custom_queue(
&state.session_id,
state.users.iter().map(|user| (user.public_id.clone(), user.team)),
state.config,
).await;
if is_create {
log::debug!("Created custom game {} lobby data to {} members", state.session_id, state.users.len());
} else {
log::debug!("Updated custom game {} lobby data to {} members", state.session_id, state.users.len());
}
}
},
}
},
Err(e) => {
log::error!("Bad intercom message received: {}", e);
}
}
}
}
}
pub fn run(self) -> tokio::task::JoinHandle<()> {
tokio::spawn(Self::run_loop(self.userless, self.lobby))
}
}

View File

@@ -1,3 +1,6 @@
pub mod battle_found;
pub mod battle_enter;
pub mod enqueue_error;
mod handler;
pub use handler::IntercomHandler;