From 285987210b970322302b2d57fabb210a771b0a82 Mon Sep 17 00:00:00 2001 From: yukirij Date: Sat, 5 Oct 2024 14:04:50 -0700 Subject: [PATCH] Fix spectator update on disconnect; change spectator symbol. --- server/src/manager/data.rs | 51 +++++++++++++++++++++++++++++++++++--- www/js/interface.js | 2 +- 2 files changed, 49 insertions(+), 4 deletions(-) diff --git a/server/src/manager/data.rs b/server/src/manager/data.rs index 845b946..e5e0b49 100644 --- a/server/src/manager/data.rs +++ b/server/src/manager/data.rs @@ -67,15 +67,55 @@ pub async fn thread_system(mut app:App, bus:Bus) QRPacketData::QDisconn => { println!("Disconnect: {}", qr.id); + let mut packets = Vec::::new(); + // Uninitialize connection if if let Some(conn) = app.connections.get(qr.id as usize).cloned() { // Disassociate session if present if let Some(session_token) = conn.session { if let Some(session) = app.sessions.get_mut(&session_token) { - if user_id == Some(session.p_dawn.user) { session.remove_connection(0, qr.id); } - else if user_id == Some(session.p_dusk.user) { session.remove_connection(1, qr.id); } - else { session.remove_connection(2, qr.id); } + if user_id == Some(session.p_dawn.user) { + session.remove_connection(0, qr.id); + + if session.p_dawn.connections.len() == 0 { + for (cid, _) in session.get_connections() { + packets.push(QRPacket::new( + cid, + QRPacketData::GameMessage(PacketGameMessage { + data: GameMessageData::Online(1, 0), + }), + )); + } + } + } + else if user_id == Some(session.p_dusk.user) { + session.remove_connection(1, qr.id); + + if session.p_dusk.connections.len() == 0 { + for (cid, _) in session.get_connections() { + packets.push(QRPacket::new( + cid, + QRPacketData::GameMessage(PacketGameMessage { + data: GameMessageData::Online(2, 0), + }), + )); + } + } + } + else { + session.remove_connection(2, qr.id); + + // Add user online packets. + for (cid, _) in session.get_connections() { + packets.push(QRPacket::new( + cid, + QRPacketData::GameMessage(PacketGameMessage { + data: GameMessageData::Online(0, session.spectators() as u32), + }), + )); + } + } } } @@ -116,6 +156,11 @@ pub async fn thread_system(mut app:App, bus:Bus) } else { false } { app.connections.remove(qr.id as usize).ok(); } + + for packet in packets { + app.send_response(packet).await; + } + Some(QRPacket::new(0, QRPacketData::None)) } diff --git a/www/js/interface.js b/www/js/interface.js index f2948ca..39649f8 100644 --- a/www/js/interface.js +++ b/www/js/interface.js @@ -710,7 +710,7 @@ const INTERFACE = { ctx.fillStyle = INTERFACE.Color.Text; ctx.textBaseline = "bottom"; ctx.textAlign = "right"; - ctx.fillText("👁" + INTERFACE_DATA.Session.Client.Spectators.count, width - gui_margin.t, height - gui_margin.t); + ctx.fillText("⚇" + INTERFACE_DATA.Session.Client.Spectators.count, width - gui_margin.t, height - gui_margin.t); // Game state message let message = null;