2025-07-06 17:36:52 -04:00
|
|
|
pub(super) struct UserConnection {
|
|
|
|
|
pub(super) user: std::sync::Arc<Box<dyn oj_rc_core::persist::user::MultiplayerUser + Send + Sync + 'static>>,
|
2025-07-12 12:49:27 -04:00
|
|
|
pub(super) connection: UserSender,
|
2025-07-20 18:31:08 -04:00
|
|
|
pub(super) state: std::sync::Arc<UserState>,
|
2025-07-06 17:36:52 -04:00
|
|
|
pub(super) machine: MachineState,
|
2025-07-21 22:33:12 -04:00
|
|
|
pub(super) descriptor: oj_rc_core::persist::user::PlayerDescriptor,
|
2025-07-06 17:36:52 -04:00
|
|
|
}
|
|
|
|
|
|
2025-07-12 12:49:27 -04:00
|
|
|
#[derive(Clone)]
|
|
|
|
|
pub(super) struct UserSender {
|
|
|
|
|
pub(super) connection: std::sync::Arc<literustlib_server::Connection<crate::PacketData>>,
|
|
|
|
|
pub(super) sender: std::sync::Arc<literustlib_server::DataSender<crate::PacketData>>,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl UserSender {
|
|
|
|
|
pub fn rlnl(&self) -> crate::handlers::RlnlSender<'_> {
|
|
|
|
|
crate::handlers::RlnlSender::new(&self.sender)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-07-06 17:36:52 -04:00
|
|
|
pub(super) struct UserState {
|
|
|
|
|
pub(super) mode: std::sync::atomic::AtomicU8,
|
|
|
|
|
pub(super) progress: std::sync::atomic::AtomicU8, // percent
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl UserState {
|
|
|
|
|
fn new() -> Self {
|
|
|
|
|
Self {
|
|
|
|
|
mode: std::sync::atomic::AtomicU8::new(ConnectionMode::Loading.to_u8()),
|
|
|
|
|
progress: std::sync::atomic::AtomicU8::new(0),
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub(super) struct MachineState {
|
|
|
|
|
pub(super) selected_weapon: WeaponInfo,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl MachineState {
|
|
|
|
|
fn new() -> Self {
|
|
|
|
|
Self {
|
|
|
|
|
selected_weapon: WeaponInfo::new(),
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub(super) struct WeaponInfo {
|
|
|
|
|
category: std::sync::atomic::AtomicU32,
|
|
|
|
|
size: std::sync::atomic::AtomicU32,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl WeaponInfo {
|
|
|
|
|
fn new() -> Self {
|
|
|
|
|
Self {
|
|
|
|
|
category: std::sync::atomic::AtomicU32::new(0),
|
|
|
|
|
size: std::sync::atomic::AtomicU32::new(0),
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[repr(u8)]
|
|
|
|
|
#[derive(Debug, Copy, Clone)]
|
|
|
|
|
pub(super) enum ConnectionMode {
|
|
|
|
|
Loading = 0,
|
2025-07-20 18:31:08 -04:00
|
|
|
WaitingForSync = 1,
|
|
|
|
|
Sync = 2,
|
|
|
|
|
WaitingToStart = 3,
|
|
|
|
|
InGame = 4,
|
2025-07-21 22:33:12 -04:00
|
|
|
Disconnected = 5,
|
2025-07-06 17:36:52 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl ConnectionMode {
|
|
|
|
|
#[inline]
|
2025-07-20 18:31:08 -04:00
|
|
|
pub(super) fn from_u8(num: u8) -> Self {
|
2025-07-06 17:36:52 -04:00
|
|
|
match num {
|
|
|
|
|
0 => Self::Loading,
|
2025-07-20 18:31:08 -04:00
|
|
|
1 => Self::WaitingForSync,
|
|
|
|
|
2 => Self::Sync,
|
|
|
|
|
3 => Self::WaitingToStart,
|
|
|
|
|
4 => Self::InGame,
|
2025-07-21 22:33:12 -04:00
|
|
|
5 => Self::Disconnected,
|
2025-07-06 17:36:52 -04:00
|
|
|
x => panic!("Unrecognized ConnectionMode {}", x),
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[inline]
|
2025-07-20 18:31:08 -04:00
|
|
|
pub(super) fn to_u8(self) -> u8 {
|
2025-07-06 17:36:52 -04:00
|
|
|
self as u8
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-07-21 22:33:12 -04:00
|
|
|
pub(super) struct GenericGamemodeEngine<L: super::CustomGameLogic> {
|
2025-07-06 17:36:52 -04:00
|
|
|
pub users: tokio::sync::RwLock<std::collections::HashMap<u8, UserConnection>>,
|
|
|
|
|
pub user_id_map: tokio::sync::RwLock<std::collections::HashMap<i32, u8>>,
|
|
|
|
|
//pub recv: tokio::sync::Mutex<tokio::sync::mpsc::Receiver<super::GameMessage>>,
|
|
|
|
|
//pub send: tokio::sync::mpsc::Sender<super::GameMessage>,
|
|
|
|
|
pub game_guid: String,
|
2025-07-21 22:33:12 -04:00
|
|
|
is_complete: std::sync::atomic::AtomicBool,
|
2025-07-12 12:49:27 -04:00
|
|
|
pub game_start: std::sync::atomic::AtomicI64,
|
2025-07-20 18:31:08 -04:00
|
|
|
pub player_count: std::sync::atomic::AtomicU8,
|
2025-07-21 22:33:12 -04:00
|
|
|
pub custom_logic_handler: L,
|
2025-07-06 17:36:52 -04:00
|
|
|
}
|
|
|
|
|
|
2025-07-21 22:33:12 -04:00
|
|
|
impl <L: super::CustomGameLogic> GenericGamemodeEngine<L> {
|
2025-07-12 12:49:27 -04:00
|
|
|
const END_OF_SYNC_DELAY: std::time::Duration = std::time::Duration::from_millis(100);
|
|
|
|
|
const COUNTDOWN_DURATION: std::time::Duration = std::time::Duration::from_secs(5);
|
|
|
|
|
|
2025-07-21 22:33:12 -04:00
|
|
|
pub fn new(guid: String, custom: L) -> Self {
|
2025-07-06 17:36:52 -04:00
|
|
|
|
|
|
|
|
Self {
|
|
|
|
|
users: tokio::sync::RwLock::new(std::collections::HashMap::new()),
|
|
|
|
|
user_id_map: tokio::sync::RwLock::new(std::collections::HashMap::new()),
|
|
|
|
|
game_guid: guid,
|
|
|
|
|
is_complete: std::sync::atomic::AtomicBool::new(false),
|
2025-07-12 12:49:27 -04:00
|
|
|
game_start: std::sync::atomic::AtomicI64::new(-1),
|
2025-07-20 18:31:08 -04:00
|
|
|
player_count: std::sync::atomic::AtomicU8::new(0),
|
2025-07-21 22:33:12 -04:00
|
|
|
custom_logic_handler: custom,
|
2025-07-06 17:36:52 -04:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub(super) async fn user_key_by_user_id(&self, user_id: i32) -> Option<u8> {
|
|
|
|
|
self.user_id_map.read().await.get(&user_id).map(|x| *x)
|
|
|
|
|
}
|
|
|
|
|
|
2025-07-20 18:31:08 -04:00
|
|
|
pub(super) async fn rebroadcast<T: byteserde::ser_heap::ByteSerializeHeap + ?Sized>(&self, user_id: i32, code: rlnl::event_code::NetworkEvent, property: literustlib::packet::Property, data: &T, in_game: bool) {
|
2025-07-06 17:36:52 -04:00
|
|
|
for conn in self.users.read().await.values() {
|
|
|
|
|
if user_id == conn.user.user_id() { continue; }
|
2025-07-20 18:31:08 -04:00
|
|
|
if in_game {
|
|
|
|
|
let mode = ConnectionMode::from_u8(conn.state.mode.load(std::sync::atomic::Ordering::Relaxed));
|
|
|
|
|
if !matches!(mode, ConnectionMode::InGame) { continue; }
|
|
|
|
|
}
|
2025-07-12 12:49:27 -04:00
|
|
|
let sender = crate::handlers::RlnlSender::new(&conn.connection.sender);
|
2025-07-06 17:36:52 -04:00
|
|
|
crate::events::log_lnl_send_failure(sender.send_data(
|
2025-07-12 12:49:27 -04:00
|
|
|
data,
|
2025-07-06 17:36:52 -04:00
|
|
|
code,
|
|
|
|
|
property,
|
2025-07-12 12:49:27 -04:00
|
|
|
&conn.connection.connection,
|
|
|
|
|
).await);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-07-20 18:31:08 -04:00
|
|
|
pub(super) async fn rebroadcast_dataless(&self, user_id: i32, code: rlnl::event_code::NetworkEvent, property: literustlib::packet::Property, in_game: bool) {
|
2025-07-12 12:49:27 -04:00
|
|
|
for conn in self.users.read().await.values() {
|
|
|
|
|
if user_id == conn.user.user_id() { continue; }
|
2025-07-20 18:31:08 -04:00
|
|
|
if in_game {
|
|
|
|
|
let mode = ConnectionMode::from_u8(conn.state.mode.load(std::sync::atomic::Ordering::Relaxed));
|
|
|
|
|
if !matches!(mode, ConnectionMode::InGame) { continue; }
|
|
|
|
|
}
|
2025-07-12 12:49:27 -04:00
|
|
|
let sender = crate::handlers::RlnlSender::new(&conn.connection.sender);
|
|
|
|
|
crate::events::log_lnl_send_failure(sender.send_empty(
|
|
|
|
|
code,
|
|
|
|
|
property,
|
|
|
|
|
&conn.connection.connection,
|
|
|
|
|
).await);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-07-20 18:31:08 -04:00
|
|
|
pub(super) async fn broadcast<T: byteserde::ser_heap::ByteSerializeHeap + ?Sized>(&self, code: rlnl::event_code::NetworkEvent, property: literustlib::packet::Property, data: &T, in_game: bool) {
|
2025-07-12 12:49:27 -04:00
|
|
|
for conn in self.users.read().await.values() {
|
2025-07-20 18:31:08 -04:00
|
|
|
if in_game {
|
|
|
|
|
let mode = ConnectionMode::from_u8(conn.state.mode.load(std::sync::atomic::Ordering::Relaxed));
|
|
|
|
|
if !matches!(mode, ConnectionMode::InGame) { continue; }
|
|
|
|
|
}
|
2025-07-12 12:49:27 -04:00
|
|
|
let sender = conn.connection.rlnl();
|
|
|
|
|
crate::events::log_lnl_send_failure(sender.send_data(
|
|
|
|
|
data,
|
|
|
|
|
code,
|
|
|
|
|
property,
|
|
|
|
|
&conn.connection.connection,
|
|
|
|
|
).await);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-07-20 18:31:08 -04:00
|
|
|
pub(super) async fn broadcast_dataless(&self, code: rlnl::event_code::NetworkEvent, property: literustlib::packet::Property, in_game: bool) {
|
2025-07-12 12:49:27 -04:00
|
|
|
for conn in self.users.read().await.values() {
|
2025-07-20 18:31:08 -04:00
|
|
|
if in_game {
|
|
|
|
|
let mode = ConnectionMode::from_u8(conn.state.mode.load(std::sync::atomic::Ordering::Relaxed));
|
|
|
|
|
if !matches!(mode, ConnectionMode::InGame) { continue; }
|
|
|
|
|
}
|
2025-07-12 12:49:27 -04:00
|
|
|
let sender = conn.connection.rlnl();
|
|
|
|
|
crate::events::log_lnl_send_failure(sender.send_empty(
|
|
|
|
|
code,
|
|
|
|
|
property,
|
|
|
|
|
&conn.connection.connection,
|
2025-07-06 17:36:52 -04:00
|
|
|
).await);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub(super) fn spawn(self) -> tokio::sync::mpsc::Sender<super::GameMessage> {
|
|
|
|
|
let (tx, rx) = tokio::sync::mpsc::channel(super::CHANNEL_BOUND);
|
|
|
|
|
tokio::spawn(self.run(rx));
|
|
|
|
|
tx
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub(super) async fn run(self, mut recv: tokio::sync::mpsc::Receiver<super::GameMessage>) {
|
2025-07-21 22:33:12 -04:00
|
|
|
let mut is_engaged = true;
|
|
|
|
|
while !recv.is_closed() && is_engaged {
|
2025-07-06 17:36:52 -04:00
|
|
|
if let Some(msg) = recv.recv().await {
|
|
|
|
|
match msg {
|
|
|
|
|
super::GameMessage::NewConnection { user, game_guid, connection, response, sender } => {
|
|
|
|
|
if self.game_guid != game_guid {
|
2025-07-20 18:31:08 -04:00
|
|
|
log::error!("Game guid does not match (got: {}, expected: {})", game_guid, self.game_guid);
|
2025-07-06 17:36:52 -04:00
|
|
|
response.send(Some(super::messages::ErrorMessage {
|
2025-07-20 18:31:08 -04:00
|
|
|
message: format!("Game guid does not match (got: {}, expected: {})", game_guid, self.game_guid),
|
2025-07-06 17:36:52 -04:00
|
|
|
inner: None,
|
|
|
|
|
})).unwrap_or_default();
|
|
|
|
|
return;
|
|
|
|
|
} else {
|
|
|
|
|
let mut users = self.users.write().await;
|
|
|
|
|
//tokio::time::sleep(std::time::Duration::from_secs(1)).await;
|
2025-07-20 18:31:08 -04:00
|
|
|
//let id = users.len() as u8;
|
2025-07-21 22:33:12 -04:00
|
|
|
match user.game_players(&game_guid).await {
|
2025-07-20 18:31:08 -04:00
|
|
|
Ok(players) => {
|
|
|
|
|
if self.player_count.load(std::sync::atomic::Ordering::Relaxed) == 0 {
|
|
|
|
|
self.player_count.store(players.len() as _, std::sync::atomic::Ordering::Relaxed);
|
|
|
|
|
}
|
2025-07-21 22:33:12 -04:00
|
|
|
let user_id = user.user_id();
|
|
|
|
|
let player_info = players.iter().filter(|p| p.user_id == user_id).next().unwrap();
|
|
|
|
|
let id = player_info.player_id;
|
|
|
|
|
let new_user = UserConnection {
|
|
|
|
|
user,
|
|
|
|
|
connection: UserSender {
|
|
|
|
|
connection,
|
|
|
|
|
sender,
|
|
|
|
|
},
|
|
|
|
|
state: std::sync::Arc::new(UserState::new()),
|
|
|
|
|
machine: MachineState::new(),
|
|
|
|
|
descriptor: player_info.to_owned(),
|
|
|
|
|
};
|
|
|
|
|
if self.custom_logic_handler.on_player_join(&self, &new_user, &players).await {
|
|
|
|
|
self.spawn_send_loading_events(&new_user, id, players);
|
|
|
|
|
log::debug!("User {} is validated to play game {}", new_user.user.user_id(), game_guid);
|
|
|
|
|
self.user_id_map.write().await.insert(new_user.user.user_id(), id);
|
|
|
|
|
users.insert(id, new_user);
|
|
|
|
|
}
|
2025-07-20 18:31:08 -04:00
|
|
|
response.send(None).unwrap_or_default();
|
|
|
|
|
},
|
|
|
|
|
Err(e) => {
|
|
|
|
|
log::error!("Failed to retrieve players for game {}: {}", game_guid, e);
|
|
|
|
|
response.send(Some(super::messages::ErrorMessage {
|
|
|
|
|
message: "Failed to retrieve players for game".to_owned(),
|
|
|
|
|
inner: Some(Box::new(e)),
|
|
|
|
|
})).unwrap_or_default();
|
|
|
|
|
}
|
2025-07-06 17:36:52 -04:00
|
|
|
}
|
2025-07-20 18:31:08 -04:00
|
|
|
|
2025-07-06 17:36:52 -04:00
|
|
|
}
|
|
|
|
|
},
|
2025-07-21 22:33:12 -04:00
|
|
|
super::GameMessage::EndConnection { user_id } => {
|
|
|
|
|
if let Some(player_id) = self.user_key_by_user_id(user_id).await {
|
|
|
|
|
let conn_opt = self.users.write().await.remove(&player_id);
|
|
|
|
|
if let Some(conn) = conn_opt {
|
|
|
|
|
if self.custom_logic_handler.on_player_end(&self, &conn).await {
|
|
|
|
|
conn.state.mode.store(ConnectionMode::Disconnected.to_u8(), std::sync::atomic::Ordering::Relaxed);
|
|
|
|
|
if !self.is_complete.load(std::sync::atomic::Ordering::Relaxed) {
|
|
|
|
|
self.rebroadcast(
|
|
|
|
|
user_id,
|
|
|
|
|
rlnl::event_code::NetworkEvent::OnAnotherClientDisconnected,
|
|
|
|
|
literustlib::packet::Property::ReliableOrdered,
|
|
|
|
|
&rlnl::events::ingame::PlayerId { player: player_id },
|
|
|
|
|
true,
|
|
|
|
|
).await;
|
|
|
|
|
} else {
|
|
|
|
|
let mut has_active_connections = false;
|
|
|
|
|
for user in self.users.read().await.values() {
|
|
|
|
|
let mode = ConnectionMode::from_u8(user.state.mode.load(std::sync::atomic::Ordering::Relaxed));
|
|
|
|
|
has_active_connections |= !matches!(mode, ConnectionMode::Disconnected);
|
|
|
|
|
}
|
|
|
|
|
is_engaged = has_active_connections;
|
|
|
|
|
}
|
2025-07-21 23:01:56 -04:00
|
|
|
conn.connection.connection.goodbye(&conn.connection.sender).await;
|
2025-07-21 22:33:12 -04:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
},
|
2025-07-06 17:36:52 -04:00
|
|
|
super::GameMessage::LoadingProgress { user_id, user_name, progress } => {
|
|
|
|
|
let progress_data = rlnl::events::loading::LoadingProgress {
|
|
|
|
|
user_name: rlnl::types::BinaryWriterString(user_name),
|
|
|
|
|
progress,
|
|
|
|
|
};
|
2025-07-12 12:49:27 -04:00
|
|
|
let mut all_users_loading_complete = true;
|
2025-07-06 17:36:52 -04:00
|
|
|
for conn in self.users.read().await.values() {
|
|
|
|
|
if user_id == conn.user.user_id() {
|
2025-07-20 18:31:08 -04:00
|
|
|
let progress_percent = ((progress * 100.0).ceil() as u8).clamp(0, 100);
|
2025-07-06 17:36:52 -04:00
|
|
|
log::debug!("User {} is loaded {}% into game {}", user_id, progress_percent, self.game_guid);
|
|
|
|
|
conn.state.progress.store(progress_percent, std::sync::atomic::Ordering::Relaxed);
|
2025-07-20 18:31:08 -04:00
|
|
|
if progress_percent != 100 {
|
|
|
|
|
all_users_loading_complete = false;
|
|
|
|
|
}
|
2025-07-12 12:49:27 -04:00
|
|
|
} else {
|
|
|
|
|
all_users_loading_complete &= conn.state.progress.load(std::sync::atomic::Ordering::Relaxed) == 100;
|
2025-07-06 17:36:52 -04:00
|
|
|
}
|
|
|
|
|
let mode = ConnectionMode::from_u8(conn.state.mode.load(std::sync::atomic::Ordering::Relaxed));
|
|
|
|
|
match mode {
|
2025-07-21 22:33:12 -04:00
|
|
|
ConnectionMode::Loading | ConnectionMode::Disconnected => {},
|
|
|
|
|
ConnectionMode::WaitingForSync | ConnectionMode::Sync | ConnectionMode::WaitingToStart => {
|
2025-07-06 17:36:52 -04:00
|
|
|
if user_id != conn.user.user_id() {
|
2025-07-12 12:49:27 -04:00
|
|
|
crate::events::log_lnl_send_failure(conn.connection.rlnl()
|
|
|
|
|
.send_data(&progress_data, rlnl::event_code::NetworkEvent::BroadcastLoadingProgress, literustlib::packet::Property::ReliableOrdered, &conn.connection.connection).await);
|
2025-07-06 17:36:52 -04:00
|
|
|
}
|
|
|
|
|
/*if progress > 0.95 {
|
|
|
|
|
log::info!("User {} is ready, ending sync", user_id);
|
|
|
|
|
crate::events::log_lnl_send_failure(crate::handlers::simple_typed::RlnlSender::new(&conn.sender)
|
|
|
|
|
.send_empty(
|
|
|
|
|
rlnl::event_code::NetworkEvent::EndOfSync,
|
|
|
|
|
literustlib::packet::Property::ReliableOrdered,
|
|
|
|
|
&conn.connection,
|
|
|
|
|
)
|
|
|
|
|
.await);
|
|
|
|
|
conn.mode.store(ConnectionMode::InGame.to_u8(), std::sync::atomic::Ordering::Relaxed);
|
|
|
|
|
}*/
|
|
|
|
|
},
|
|
|
|
|
ConnectionMode::InGame => {
|
|
|
|
|
log::warn!("Got loading progress for user {} who is supposed to be already in-game", user_id);
|
|
|
|
|
},
|
|
|
|
|
}
|
2025-07-12 12:49:27 -04:00
|
|
|
}
|
|
|
|
|
if all_users_loading_complete {
|
2025-07-20 18:31:08 -04:00
|
|
|
for (id, conn) in self.users.read().await.iter() {
|
|
|
|
|
if let Err(e) = conn.connection.rlnl().send_empty(
|
|
|
|
|
rlnl::event_code::NetworkEvent::EndOfSync,
|
|
|
|
|
literustlib::packet::Property::ReliableOrdered,
|
|
|
|
|
&conn.connection.connection
|
|
|
|
|
).await {
|
|
|
|
|
log::error!("Failed to send EndOfSync event to user {}: {}", id, e);
|
|
|
|
|
}
|
2025-07-12 12:49:27 -04:00
|
|
|
}
|
2025-07-06 17:36:52 -04:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
super::GameMessage::RequestLoadingProgress { user_id } => {
|
|
|
|
|
let mut user_info = None;
|
|
|
|
|
for conn in self.users.read().await.values() {
|
|
|
|
|
if user_id == conn.user.user_id() {
|
|
|
|
|
user_info = Some((
|
|
|
|
|
conn.connection.to_owned(),
|
|
|
|
|
rlnl::events::loading::LoadingProgress {
|
|
|
|
|
user_name: rlnl::types::BinaryWriterString(conn.user.user_name().to_owned()),
|
|
|
|
|
progress: (conn.state.progress.load(std::sync::atomic::Ordering::Relaxed) as f32) / 100.0,
|
|
|
|
|
},
|
|
|
|
|
));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if let Some(user_info) = user_info {
|
2025-07-12 12:49:27 -04:00
|
|
|
let sender = crate::handlers::RlnlSender::new(&user_info.0.sender);
|
2025-07-06 17:36:52 -04:00
|
|
|
for conn in self.users.read().await.values() {
|
|
|
|
|
if user_id == conn.user.user_id() { continue; }
|
|
|
|
|
crate::events::log_lnl_send_failure(sender.send_data(
|
2025-07-12 12:49:27 -04:00
|
|
|
&user_info.1,
|
2025-07-06 17:36:52 -04:00
|
|
|
rlnl::event_code::NetworkEvent::BroadcastLoadingProgress,
|
|
|
|
|
literustlib::packet::Property::ReliableOrdered,
|
2025-07-12 12:49:27 -04:00
|
|
|
&user_info.0.connection,
|
2025-07-06 17:36:52 -04:00
|
|
|
).await);
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
log::error!("Failed to find user {} in connected users for match {}", user_id, self.game_guid);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
},
|
|
|
|
|
super::GameMessage::WeaponSelect { user_id, machine_id, category, size } => {
|
|
|
|
|
if let Some(conn) = self.users.read().await.get(&machine_id) {
|
|
|
|
|
let category_u32 = category as u32;
|
|
|
|
|
let size_u32 = size as u32;
|
|
|
|
|
conn.machine.selected_weapon.category.store(category_u32, std::sync::atomic::Ordering::Relaxed);
|
|
|
|
|
conn.machine.selected_weapon.size.store(size_u32, std::sync::atomic::Ordering::Relaxed);
|
|
|
|
|
let data = rlnl::events::ingame::SelectWeapon {
|
|
|
|
|
machine_id,
|
|
|
|
|
item_category: category_u32,
|
|
|
|
|
item_size: size_u32,
|
|
|
|
|
};
|
2025-07-12 12:49:27 -04:00
|
|
|
self.rebroadcast(
|
2025-07-06 17:36:52 -04:00
|
|
|
user_id,
|
|
|
|
|
rlnl::event_code::NetworkEvent::BroadcastWeaponSelect,
|
|
|
|
|
literustlib::packet::Property::ReliableOrdered,
|
2025-07-12 12:49:27 -04:00
|
|
|
&data,
|
2025-07-20 23:22:37 -04:00
|
|
|
true
|
2025-07-06 17:36:52 -04:00
|
|
|
).await;
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
super::GameMessage::RequestLoadingSync { user_id } => {
|
2025-07-20 18:31:08 -04:00
|
|
|
// wait for all users to be ready before transitioning to loading sync
|
|
|
|
|
let mut ready_count = 0;
|
|
|
|
|
for user in self.users.read().await.values() {
|
|
|
|
|
if user.user.user_id() == user_id {
|
|
|
|
|
user.state.mode.store(ConnectionMode::WaitingForSync.to_u8(), std::sync::atomic::Ordering::Relaxed);
|
|
|
|
|
ready_count += 1;
|
|
|
|
|
} else {
|
|
|
|
|
if matches!(ConnectionMode::from_u8(user.state.mode.load(std::sync::atomic::Ordering::Relaxed)), ConnectionMode::WaitingForSync) {
|
|
|
|
|
ready_count += 1;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
let player_count = self.player_count.load(std::sync::atomic::Ordering::Relaxed) as usize;
|
|
|
|
|
if ready_count == player_count {
|
|
|
|
|
log::info!("All players ({}) awaiting sync for game {}", player_count, self.game_guid);
|
|
|
|
|
let total_users = self.users.read().await.len() as u8;
|
|
|
|
|
for (user_key, conn) in self.users.read().await.iter() {
|
2025-07-21 22:33:12 -04:00
|
|
|
let extra_packets = self.custom_logic_handler.extra_sync_events(&self, conn).await;
|
|
|
|
|
self.spawn_send_sync_events(conn, conn.user.user_id(), *user_key, total_users, extra_packets);
|
2025-07-06 17:36:52 -04:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
},
|
2025-07-12 12:49:27 -04:00
|
|
|
super::GameMessage::LoadComplete { user_id } => {
|
|
|
|
|
if let Some(user_key) = self.user_key_by_user_id(user_id).await {
|
|
|
|
|
if let Some(conn) = self.users.read().await.get(&user_key) {
|
2025-07-20 18:31:08 -04:00
|
|
|
log::info!("Loading complete for game {}, user {} ({})", self.game_guid, user_id, user_key);
|
|
|
|
|
conn.state.progress.store(100, std::sync::atomic::Ordering::Relaxed);
|
|
|
|
|
conn.state.mode.store(ConnectionMode::WaitingToStart.to_u8(), std::sync::atomic::Ordering::Relaxed);
|
|
|
|
|
/*let game_start = chrono::DateTime::from_timestamp(self.game_start.load(std::sync::atomic::Ordering::Relaxed), 0).unwrap();
|
2025-07-12 12:49:27 -04:00
|
|
|
let payload = super::countdown::time_to_game_start_payload(game_start);
|
|
|
|
|
let sender = conn.connection.rlnl();
|
|
|
|
|
if let Err(e) = sender.send_data(
|
|
|
|
|
&payload,
|
|
|
|
|
rlnl::event_code::NetworkEvent::TimeToGameStart,
|
|
|
|
|
literustlib::packet::Property::ReliableOrdered,
|
|
|
|
|
&conn.connection.connection)
|
|
|
|
|
.await {
|
|
|
|
|
log::error!("Failed to send updated TimeToGameStart to a user: {}", e);
|
2025-07-20 18:31:08 -04:00
|
|
|
}*/
|
2025-07-12 12:49:27 -04:00
|
|
|
self.spawn_initial_ingame_events(conn, user_id);
|
2025-07-20 18:31:08 -04:00
|
|
|
} else {
|
|
|
|
|
log::warn!("Invalid LoadComplete user key {} for game {}", user_key, self.game_guid);
|
|
|
|
|
continue;
|
2025-07-12 12:49:27 -04:00
|
|
|
}
|
2025-07-20 18:31:08 -04:00
|
|
|
} else {
|
|
|
|
|
log::warn!("Unknown LoadComplete user id {} for game {}", user_id, self.game_guid);
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
// wait for all users to be ready for starting game start countdown
|
|
|
|
|
let mut all_users_loading_complete = true;
|
|
|
|
|
for conn in self.users.read().await.values() {
|
|
|
|
|
let mode = ConnectionMode::from_u8(conn.state.mode.load(std::sync::atomic::Ordering::Relaxed));
|
|
|
|
|
all_users_loading_complete &= matches!(mode, ConnectionMode::WaitingToStart);
|
|
|
|
|
}
|
|
|
|
|
// trigger game start
|
|
|
|
|
if all_users_loading_complete {
|
|
|
|
|
let player_count = self.player_count.load(std::sync::atomic::Ordering::Relaxed) as usize;
|
|
|
|
|
log::info!("All players ({}) are ready for game {}", player_count, self.game_guid);
|
|
|
|
|
tokio::time::sleep(Self::END_OF_SYNC_DELAY).await;
|
|
|
|
|
let mut senders = Vec::new();
|
|
|
|
|
for conn in self.users.read().await.values() {
|
|
|
|
|
senders.push((conn.connection.clone(), conn.state.clone()));
|
|
|
|
|
}
|
|
|
|
|
let game_start = chrono::Utc::now() + Self::COUNTDOWN_DURATION;
|
|
|
|
|
self.game_start.store(game_start.timestamp(), std::sync::atomic::Ordering::Relaxed);
|
|
|
|
|
super::countdown::match_countdown(senders, game_start);
|
2025-07-12 12:49:27 -04:00
|
|
|
}
|
2025-07-20 23:22:37 -04:00
|
|
|
},
|
|
|
|
|
super::GameMessage::SpotVehicle { user_id, remote_player } => {
|
|
|
|
|
self.rebroadcast(
|
|
|
|
|
user_id,
|
|
|
|
|
rlnl::event_code::NetworkEvent::RemoteEnemySpotted,
|
|
|
|
|
literustlib::packet::Property::ReliableOrdered,
|
|
|
|
|
&rlnl::events::ingame::PlayerId { player: remote_player },
|
|
|
|
|
true
|
|
|
|
|
).await;
|
|
|
|
|
},
|
|
|
|
|
super::GameMessage::DestroyVehicle { user_id, remote_player, killer_player } => {
|
2025-07-21 22:33:12 -04:00
|
|
|
// FIXME allow custom_logic_handler to override the MachineDestroyedConfirmed send
|
2025-07-20 23:22:37 -04:00
|
|
|
self.rebroadcast(
|
|
|
|
|
user_id,
|
|
|
|
|
rlnl::event_code::NetworkEvent::MachineDestroyedConfirmed,
|
|
|
|
|
literustlib::packet::Property::ReliableOrdered,
|
|
|
|
|
&rlnl::events::ingame::Kill { killee_player_id: remote_player, killer_player_id: killer_player },
|
|
|
|
|
true,
|
|
|
|
|
).await;
|
2025-07-21 22:33:12 -04:00
|
|
|
log::info!("Player {} was destroyed by {} ({}) in game {}", remote_player, killer_player, user_id, self.game_guid);
|
|
|
|
|
self.custom_logic_handler.on_vehicle_destroyed(&self, killer_player, remote_player).await;
|
2025-07-12 12:49:27 -04:00
|
|
|
}
|
|
|
|
|
super::GameMessage::BroadcastRlnl { user_id: _, event, property, data } => {
|
|
|
|
|
if let Some(data) = data {
|
2025-07-20 18:31:08 -04:00
|
|
|
self.broadcast(event, property, &*data, true).await;
|
2025-07-12 12:49:27 -04:00
|
|
|
} else {
|
2025-07-20 18:31:08 -04:00
|
|
|
self.broadcast_dataless(event, property, true).await;
|
2025-07-12 12:49:27 -04:00
|
|
|
}
|
2025-07-20 23:22:37 -04:00
|
|
|
},
|
2025-07-12 12:49:27 -04:00
|
|
|
super::GameMessage::RebroadcastRlnl { skip_user_id, event, property, data } => {
|
|
|
|
|
if let Some(data) = data {
|
2025-07-20 18:31:08 -04:00
|
|
|
self.rebroadcast(skip_user_id, event, property, &*data, true).await;
|
2025-07-12 12:49:27 -04:00
|
|
|
} else {
|
2025-07-20 18:31:08 -04:00
|
|
|
self.rebroadcast_dataless(skip_user_id, event, property, true).await;
|
2025-07-12 12:49:27 -04:00
|
|
|
}
|
|
|
|
|
|
2025-07-20 23:22:37 -04:00
|
|
|
},
|
2025-07-06 17:36:52 -04:00
|
|
|
super::GameMessage::Motion { user_id, data } => {
|
|
|
|
|
for conn in self.users.read().await.values() {
|
|
|
|
|
if conn.user.user_id() == user_id { continue; } // fun fact: the game hard crashes if you omit this
|
2025-07-12 12:49:27 -04:00
|
|
|
crate::events::log_lnl_send_failure(conn.connection.sender.send_data(crate::handler::EventData {
|
2025-07-06 17:36:52 -04:00
|
|
|
message_ty: crate::data::MessageType::RobotMotion,
|
|
|
|
|
variant: 0,
|
|
|
|
|
data_size: data.len() as _,
|
|
|
|
|
data: data.clone(),
|
2025-07-12 12:49:27 -04:00
|
|
|
}, literustlib::packet::Property::Unreliable, &conn.connection.connection).await);
|
2025-07-06 17:36:52 -04:00
|
|
|
}
|
2025-07-20 23:22:37 -04:00
|
|
|
},
|
2025-07-06 17:36:52 -04:00
|
|
|
super::GameMessage::NoOp => {},
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2025-07-21 22:33:12 -04:00
|
|
|
log::info!("Game {} has exited", self.game_guid);
|
2025-07-06 17:36:52 -04:00
|
|
|
}
|
|
|
|
|
|
2025-07-20 18:31:08 -04:00
|
|
|
fn spawn_send_loading_events(&self, user: &UserConnection, player_id: u8, players: Vec<oj_rc_core::persist::user::PlayerDescriptor>) {
|
|
|
|
|
let connection = user.connection.clone();
|
|
|
|
|
let user_id = user.user.user_id();
|
|
|
|
|
tokio::spawn(Self::send_loading_events_wrapper(connection, player_id, user_id, players));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async fn send_loading_events_wrapper(connection: UserSender, player_id: u8, user_id: i32, players: Vec<oj_rc_core::persist::user::PlayerDescriptor>) {
|
|
|
|
|
if let Err(e) = Self::send_loading_events(&connection, player_id, players).await {
|
|
|
|
|
log::error!("Failed to send Loading events for user {} ({}): {}", user_id, player_id, e);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async fn send_loading_events(user: &UserSender, player_id: u8, players: Vec<oj_rc_core::persist::user::PlayerDescriptor>) -> std::io::Result<()> {
|
2025-07-12 12:49:27 -04:00
|
|
|
let sender = user.rlnl();
|
2025-07-06 17:36:52 -04:00
|
|
|
sender.send_data(
|
2025-07-20 18:31:08 -04:00
|
|
|
&rlnl::events::ingame::PlayerId { player: player_id },
|
2025-07-06 17:36:52 -04:00
|
|
|
rlnl::event_code::NetworkEvent::GameGuidValidated,
|
|
|
|
|
literustlib::packet::Property::ReliableOrdered,
|
|
|
|
|
&user.connection
|
|
|
|
|
).await?;
|
|
|
|
|
sender.send_data(
|
|
|
|
|
&rlnl::events::loading::PlayerIDsAndNames {
|
2025-07-20 18:31:08 -04:00
|
|
|
num_players: players.len() as _,
|
|
|
|
|
players: players.into_iter().map(|player| rlnl::events::loading::PlayerIDAndName {
|
|
|
|
|
player_id: player.player_id as _,
|
|
|
|
|
name: rlnl::types::BinaryWriterString(player.public_id),
|
|
|
|
|
display_name: rlnl::types::BinaryWriterString(player.display_name),
|
|
|
|
|
})
|
|
|
|
|
.collect(),
|
2025-07-06 17:36:52 -04:00
|
|
|
},
|
|
|
|
|
rlnl::event_code::NetworkEvent::PlayerIDs,
|
|
|
|
|
literustlib::packet::Property::ReliableOrdered,
|
|
|
|
|
&user.connection
|
|
|
|
|
).await?;
|
|
|
|
|
sender.send_data(
|
|
|
|
|
&rlnl::events::loading::PlayerIDs {
|
|
|
|
|
num_ids: 0,
|
|
|
|
|
players: vec![],
|
|
|
|
|
},
|
|
|
|
|
rlnl::event_code::NetworkEvent::HostAIs,
|
|
|
|
|
literustlib::packet::Property::ReliableOrdered,
|
|
|
|
|
&user.connection
|
|
|
|
|
).await?;
|
|
|
|
|
Ok(())
|
|
|
|
|
}
|
|
|
|
|
|
2025-07-21 22:33:12 -04:00
|
|
|
fn spawn_send_sync_events(&self, user: &UserConnection, user_id: i32, player_id: u8, num_players: u8, extra_packets: Vec<super::RlnlPacket>) {
|
2025-07-06 17:36:52 -04:00
|
|
|
let connection = user.connection.clone();
|
2025-07-21 22:33:12 -04:00
|
|
|
tokio::spawn(Self::send_sync_events_wrapper(connection, user_id, player_id, num_players, extra_packets));
|
2025-07-06 17:36:52 -04:00
|
|
|
user.state.mode.store(ConnectionMode::Sync.to_u8(), std::sync::atomic::Ordering::Relaxed);
|
|
|
|
|
}
|
|
|
|
|
|
2025-07-21 22:33:12 -04:00
|
|
|
async fn send_sync_events_wrapper(connection: UserSender, user_id: i32, player_id: u8, num_players: u8, extra_packets: Vec<super::RlnlPacket>) {
|
|
|
|
|
if let Err(e) = Self::send_sync_events(connection, player_id, num_players, extra_packets).await {
|
2025-07-06 17:36:52 -04:00
|
|
|
log::error!("Failed to send Sync events for user {}: {}", user_id, e);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-07-21 22:33:12 -04:00
|
|
|
async fn send_sync_events(connection: UserSender, _player_id: u8, num_players: u8, extra_packets: Vec<super::RlnlPacket>) -> std::io::Result<()> {
|
2025-07-12 12:49:27 -04:00
|
|
|
let sender = connection.rlnl();
|
2025-07-06 17:36:52 -04:00
|
|
|
sender.send_empty(
|
|
|
|
|
rlnl::event_code::NetworkEvent::BeginSync,
|
|
|
|
|
literustlib::packet::Property::ReliableOrdered,
|
2025-07-12 12:49:27 -04:00
|
|
|
&connection.connection)
|
2025-07-06 17:36:52 -04:00
|
|
|
.await?;
|
2025-07-21 22:33:12 -04:00
|
|
|
|
|
|
|
|
for packet in extra_packets {
|
|
|
|
|
sender.send_data(
|
|
|
|
|
&*packet.data,
|
|
|
|
|
packet.event,
|
|
|
|
|
packet.property,
|
|
|
|
|
&connection.connection)
|
|
|
|
|
.await?;
|
|
|
|
|
}
|
|
|
|
|
|
2025-07-06 17:36:52 -04:00
|
|
|
sender.send_data(
|
|
|
|
|
&rlnl::events::sync::InitialiseGameStats {
|
2025-07-20 18:31:08 -04:00
|
|
|
num_players,
|
|
|
|
|
stats: (0..num_players).into_iter()
|
|
|
|
|
.map(|i| rlnl::types::IngamePlayerStats {
|
|
|
|
|
player_name: i,
|
2025-07-06 17:36:52 -04:00
|
|
|
num_stats: 0,
|
|
|
|
|
stats: vec![],
|
2025-07-20 18:31:08 -04:00
|
|
|
}).collect(),
|
2025-07-06 17:36:52 -04:00
|
|
|
},
|
|
|
|
|
rlnl::event_code::NetworkEvent::InitialiseGameStats,
|
|
|
|
|
literustlib::packet::Property::ReliableOrdered,
|
2025-07-12 12:49:27 -04:00
|
|
|
&connection.connection)
|
2025-07-06 17:36:52 -04:00
|
|
|
.await?;
|
2025-07-20 18:31:08 -04:00
|
|
|
for i in 0..num_players {
|
|
|
|
|
sender.send_data(
|
|
|
|
|
&rlnl::events::sync::SpawnPoint {
|
|
|
|
|
pos: rlnl::types::PosQuatPair {
|
|
|
|
|
pos: rlnl::types::CompressedVec3 { x: i as _, y: 42, z: i as _ },
|
|
|
|
|
rot: rlnl::types::CompressedQuat { x: 0, y: 0, z: 0 },
|
|
|
|
|
},
|
|
|
|
|
owner: i,
|
2025-07-06 17:36:52 -04:00
|
|
|
},
|
2025-07-20 18:31:08 -04:00
|
|
|
rlnl::event_code::NetworkEvent::FreeSpawnPoint,
|
|
|
|
|
literustlib::packet::Property::ReliableOrdered,
|
|
|
|
|
&connection.connection)
|
|
|
|
|
.await?;
|
|
|
|
|
}
|
|
|
|
|
|
2025-07-12 12:49:27 -04:00
|
|
|
// seems to be for reconnecting
|
2025-07-06 17:36:52 -04:00
|
|
|
/*sender.send_data(
|
|
|
|
|
&rlnl::events::sync::SyncMachineCubes {
|
|
|
|
|
machine_id: 0,
|
|
|
|
|
num_cubes: 0,
|
|
|
|
|
events: vec![
|
|
|
|
|
rlnl::types::CubeState {
|
|
|
|
|
loc: rlnl::types::Byte3 { x: 0, y: 0, z: 0 },
|
|
|
|
|
status: rlnl::types::CubeStatus {
|
|
|
|
|
ty: rlnl::types::CubeHistoryEventType::Heal,
|
|
|
|
|
damage: Some(1),
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
],
|
|
|
|
|
},
|
|
|
|
|
rlnl::event_code::NetworkEvent::SyncMachineCubes,
|
|
|
|
|
literustlib::packet::Property::ReliableOrdered,
|
|
|
|
|
&user.connection)
|
|
|
|
|
.await?;*/
|
|
|
|
|
Ok(())
|
|
|
|
|
}
|
|
|
|
|
|
2025-07-12 12:49:27 -04:00
|
|
|
fn spawn_initial_ingame_events(&self, user: &UserConnection, user_id: i32) {
|
|
|
|
|
let connection = user.connection.clone();
|
|
|
|
|
tokio::spawn(Self::send_initial_ingame_events_wrapper(connection, user_id));
|
2025-07-20 18:31:08 -04:00
|
|
|
//user.state.mode.store(ConnectionMode::InGame.to_u8(), std::sync::atomic::Ordering::Relaxed);
|
2025-07-12 12:49:27 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async fn send_initial_ingame_events_wrapper(connection: UserSender, user_id: i32) {
|
|
|
|
|
if let Err(e) = Self::send_initial_ingame_events(connection).await {
|
|
|
|
|
log::error!("Failed to send Sync events for user {}: {}", user_id, e);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async fn send_initial_ingame_events(_connection: UserSender) -> std::io::Result<()> {
|
|
|
|
|
//let sender = connection.rlnl();
|
|
|
|
|
/*sender.send_data(
|
|
|
|
|
&rlnl::events::GameTime(3.0),
|
|
|
|
|
rlnl::event_code::NetworkEvent::TimeToGameStart,
|
|
|
|
|
literustlib::packet::Property::ReliableOrdered,
|
|
|
|
|
&connection.connection)
|
|
|
|
|
.await?;*/
|
|
|
|
|
|
|
|
|
|
// TODO
|
|
|
|
|
Ok(())
|
2025-07-06 17:36:52 -04:00
|
|
|
}
|
2025-07-12 12:49:27 -04:00
|
|
|
|
2025-07-21 22:33:12 -04:00
|
|
|
pub(super) fn game_done(&self) {
|
|
|
|
|
self.is_complete.store(true, std::sync::atomic::Ordering::SeqCst);
|
|
|
|
|
}
|
|
|
|
|
}
|