diff --git a/server/src/app/mod.rs b/server/src/app/mod.rs index ec69695..f3c5141 100644 --- a/server/src/app/mod.rs +++ b/server/src/app/mod.rs @@ -223,6 +223,32 @@ impl App { } } + pub async fn send_session_spectators(&mut self, token:SessionToken) + { + use crate::protocol::*; + + let mut packets = Vec::new(); + + if let Some(session) = self.sessions.get_mut(&token) { + for (cid, _) in session.get_connections() { + packets.push(QRPacket::new( + cid, + QRPacketData::GameMessage(PacketGameMessage { + data: GameMessageData::Online( + session.p_dawn.connections.len() > 0, + session.p_dusk.connections.len() > 0, + session.connections.len() as u32, + ), + }), + )); + } + } + + for packet in packets { + self.send_response(packet).await; + } + } + pub async fn send_status(&mut self, user_id:u32) { use crate::protocol::*; diff --git a/server/src/app/session.rs b/server/src/app/session.rs index 8e921ff..b24d843 100644 --- a/server/src/app/session.rs +++ b/server/src/app/session.rs @@ -74,13 +74,4 @@ impl Session { } } } - - pub fn spectators(&self) -> usize - { - println!("{}", self.connections.len()); - for conn in &self.connections { - print!("{} ", *conn); - } println!(""); - self.connections.len() - } } diff --git a/server/src/manager/data.rs b/server/src/manager/data.rs index 275862e..a016cd4 100644 --- a/server/src/manager/data.rs +++ b/server/src/manager/data.rs @@ -67,8 +67,6 @@ 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() { @@ -77,46 +75,16 @@ pub async fn thread_system(mut app:App, bus:Bus) if let Some(session) = app.sessions.get_mut(&session_token) { 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), - }), - )); - } } } + + app.send_session_spectators(session_token).await; } // Remove connection from chain. @@ -157,10 +125,6 @@ pub async fn thread_system(mut app:App, bus:Bus) app.connections.remove(qr.id as usize).ok(); } - for packet in packets { - app.send_response(packet).await; - } - Some(QRPacket::new(0, QRPacketData::None)) } @@ -519,8 +483,6 @@ pub async fn thread_system(mut app:App, bus:Bus) QRPacketData::QSessionView(request) => { println!("Request: Session Join"); - let mut packets = Vec::::new(); - let mut response = PacketSessionViewResponse::new(); response.status = STATUS_ERROR; response.token = request.token; @@ -545,16 +507,6 @@ pub async fn thread_system(mut app:App, bus:Bus) if Some(session.p_dawn.user) == user_id || Some(session.p_dusk.user) == user_id { println!("User resumes session."); response.status = STATUS_OK; - - for (cid, _) in session.get_connections() { - packets.push(QRPacket::new( - cid, - QRPacketData::GameMessage(PacketGameMessage { - data: GameMessageData::Online(1 + (Some(session.p_dusk.user) == user_id) as u8, 1), - }), - )); - } - true } else { false } } else { response.status = STATUS_NOAUTH; false } @@ -564,17 +516,6 @@ pub async fn thread_system(mut app:App, bus:Bus) else { println!("User spectates session."); response.status = STATUS_OK; - - // Add user online packets. - for (cid, _) in session.get_connections() { - packets.push(QRPacket::new( - cid, - QRPacketData::GameMessage(PacketGameMessage { - data: GameMessageData::Online(0, 1 + session.spectators() as u32), - }), - )); - } - true } { // Associate session and connection on join @@ -589,14 +530,10 @@ pub async fn thread_system(mut app:App, bus:Bus) 2 }; session.add_connection(mode, qr.id); + app.send_session_spectators(request.token).await; } } - // Send notification packets - for packet in packets { - app.send_response(packet).await; - } - Some(QRPacket::new(qr.id, QRPacketData::RSessionView(response))) } @@ -647,57 +584,21 @@ pub async fn thread_system(mut app:App, bus:Bus) QRPacketData::QSessionLeave => { println!("Request: Session Leave"); - let mut packets = Vec::::new(); - // Verify that session exists. if let Some(session_token) = session_id { if let Some(session) = app.sessions.get_mut(&session_token) { 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), - }), - )); - } } } - } - for packet in packets { - app.send_response(packet).await; + app.send_session_spectators(session_token).await; } Some(QRPacket::new(0, QRPacketData::None)) @@ -1032,7 +933,7 @@ fn generate_game_state(app:&App, session:&Session) -> protocol::PacketGameStateR response.dawn_online = session.p_dawn.connections.len() > 0; response.dusk_online = session.p_dusk.connections.len() > 0; - response.spectators = session.spectators() as u32; + response.spectators = session.connections.len() as u32; // Get history response.history = session.game.history.clone(); diff --git a/server/src/protocol/packet/game_message.rs b/server/src/protocol/packet/game_message.rs index e703447..255f10b 100644 --- a/server/src/protocol/packet/game_message.rs +++ b/server/src/protocol/packet/game_message.rs @@ -16,7 +16,7 @@ pub enum GameMessageData { PlayDrop(u16, u8, u8), PlayAlt(u16, u8, u8), - Online(u8, u32), + Online(bool, bool, u32), Undo(u16, u8), Resign, @@ -102,10 +102,11 @@ impl Packet for PacketGameMessage { | ((to as u64) << 30) } - GameMessageData::Online(client, state) => { + GameMessageData::Online(dawn, dusk, spectators) => { GMSG_ONLINE as u64 - | ((client as u64) << 8) - | ((state as u64) << 10) + | ((dawn as u64) << 8) + | ((dusk as u64) << 9) + | ((spectators as u64) << 10) } GameMessageData::Undo(turn, state) => { diff --git a/www/js/interface.js b/www/js/interface.js index 39649f8..21611da 100644 --- a/www/js/interface.js +++ b/www/js/interface.js @@ -1312,22 +1312,9 @@ const INTERFACE = { } break; case GameMessage.Online: { - switch(data.client) { - case 0: { - // Spectator - INTERFACE_DATA.Session.Client.Spectators.count = data.state; - } break; - - case 1: { - // Dawn - INTERFACE_DATA.Session.Client.Dawn.online = (data.state != 0); - } break; - - case 2: { - // Dusk - INTERFACE_DATA.Session.Client.Dusk.online = (data.state != 0); - } break; - } + INTERFACE_DATA.Session.Client.Dawn.online = data.dawn; + INTERFACE_DATA.Session.Client.Dusk.online = data.dusk; + INTERFACE_DATA.Session.Client.Spectators.count = data.spectators; INTERFACE.redraw(); } break; diff --git a/www/js/system.js b/www/js/system.js index c09cc1b..21ddccd 100644 --- a/www/js/system.js +++ b/www/js/system.js @@ -359,9 +359,9 @@ function MESSAGE(event) { } break; case GameMessage.Online: { - data.client = dat & 0x3; - data.state = (dat >> 2) & 0xFFFF_FFFF; - console.log(data.client + " = " + data.state); + data.dawn = (dat & 0x1) != 0; + data.dusk = (dat & 0x2) != 0; + data.spectators = (dat >> 2) & 0xFF_FFFF; } break; case GameMessage.Undo: {