Improve handling of spectator game messages.

This commit is contained in:
yukirij 2024-10-05 14:39:47 -07:00
parent d8a33d1c10
commit b848492c5a
6 changed files with 42 additions and 136 deletions

View File

@ -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) pub async fn send_status(&mut self, user_id:u32)
{ {
use crate::protocol::*; use crate::protocol::*;

View File

@ -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()
}
} }

View File

@ -67,8 +67,6 @@ pub async fn thread_system(mut app:App, bus:Bus<protocol::QRPacket>)
QRPacketData::QDisconn => { QRPacketData::QDisconn => {
println!("Disconnect: {}", qr.id); println!("Disconnect: {}", qr.id);
let mut packets = Vec::<QRPacket>::new();
// Uninitialize connection // Uninitialize connection
if if let Some(conn) = app.connections.get(qr.id as usize).cloned() { 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<protocol::QRPacket>)
if let Some(session) = app.sessions.get_mut(&session_token) { if let Some(session) = app.sessions.get_mut(&session_token) {
if user_id == Some(session.p_dawn.user) { if user_id == Some(session.p_dawn.user) {
session.remove_connection(0, qr.id); 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) { else if user_id == Some(session.p_dusk.user) {
session.remove_connection(1, qr.id); 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 { else {
session.remove_connection(2, qr.id); session.remove_connection(2, qr.id);
}
}
// Add user online packets. app.send_session_spectators(session_token).await;
for (cid, _) in session.get_connections() {
packets.push(QRPacket::new(
cid,
QRPacketData::GameMessage(PacketGameMessage {
data: GameMessageData::Online(0, session.spectators() as u32),
}),
));
}
}
}
} }
// Remove connection from chain. // Remove connection from chain.
@ -157,10 +125,6 @@ pub async fn thread_system(mut app:App, bus:Bus<protocol::QRPacket>)
app.connections.remove(qr.id as usize).ok(); app.connections.remove(qr.id as usize).ok();
} }
for packet in packets {
app.send_response(packet).await;
}
Some(QRPacket::new(0, QRPacketData::None)) Some(QRPacket::new(0, QRPacketData::None))
} }
@ -519,8 +483,6 @@ pub async fn thread_system(mut app:App, bus:Bus<protocol::QRPacket>)
QRPacketData::QSessionView(request) => { QRPacketData::QSessionView(request) => {
println!("Request: Session Join"); println!("Request: Session Join");
let mut packets = Vec::<QRPacket>::new();
let mut response = PacketSessionViewResponse::new(); let mut response = PacketSessionViewResponse::new();
response.status = STATUS_ERROR; response.status = STATUS_ERROR;
response.token = request.token; response.token = request.token;
@ -545,16 +507,6 @@ pub async fn thread_system(mut app:App, bus:Bus<protocol::QRPacket>)
if Some(session.p_dawn.user) == user_id || Some(session.p_dusk.user) == user_id { if Some(session.p_dawn.user) == user_id || Some(session.p_dusk.user) == user_id {
println!("User resumes session."); println!("User resumes session.");
response.status = STATUS_OK; 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 true
} else { false } } else { false }
} else { response.status = STATUS_NOAUTH; false } } else { response.status = STATUS_NOAUTH; false }
@ -564,17 +516,6 @@ pub async fn thread_system(mut app:App, bus:Bus<protocol::QRPacket>)
else { else {
println!("User spectates session."); println!("User spectates session.");
response.status = STATUS_OK; 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 true
} { } {
// Associate session and connection on join // Associate session and connection on join
@ -589,14 +530,10 @@ pub async fn thread_system(mut app:App, bus:Bus<protocol::QRPacket>)
2 2
}; };
session.add_connection(mode, qr.id); 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))) Some(QRPacket::new(qr.id, QRPacketData::RSessionView(response)))
} }
@ -647,57 +584,21 @@ pub async fn thread_system(mut app:App, bus:Bus<protocol::QRPacket>)
QRPacketData::QSessionLeave => { QRPacketData::QSessionLeave => {
println!("Request: Session Leave"); println!("Request: Session Leave");
let mut packets = Vec::<QRPacket>::new();
// Verify that session exists. // Verify that session exists.
if let Some(session_token) = session_id { if let Some(session_token) = session_id {
if let Some(session) = app.sessions.get_mut(&session_token) { if let Some(session) = app.sessions.get_mut(&session_token) {
if user_id == Some(session.p_dawn.user) { if user_id == Some(session.p_dawn.user) {
session.remove_connection(0, qr.id); 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) { else if user_id == Some(session.p_dusk.user) {
session.remove_connection(1, qr.id); 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 { else {
session.remove_connection(2, qr.id); 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_session_spectators(session_token).await;
app.send_response(packet).await;
} }
Some(QRPacket::new(0, QRPacketData::None)) 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.dawn_online = session.p_dawn.connections.len() > 0;
response.dusk_online = session.p_dusk.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 // Get history
response.history = session.game.history.clone(); response.history = session.game.history.clone();

View File

@ -16,7 +16,7 @@ pub enum GameMessageData {
PlayDrop(u16, u8, u8), PlayDrop(u16, u8, u8),
PlayAlt(u16, u8, u8), PlayAlt(u16, u8, u8),
Online(u8, u32), Online(bool, bool, u32),
Undo(u16, u8), Undo(u16, u8),
Resign, Resign,
@ -102,10 +102,11 @@ impl Packet for PacketGameMessage {
| ((to as u64) << 30) | ((to as u64) << 30)
} }
GameMessageData::Online(client, state) => { GameMessageData::Online(dawn, dusk, spectators) => {
GMSG_ONLINE as u64 GMSG_ONLINE as u64
| ((client as u64) << 8) | ((dawn as u64) << 8)
| ((state as u64) << 10) | ((dusk as u64) << 9)
| ((spectators as u64) << 10)
} }
GameMessageData::Undo(turn, state) => { GameMessageData::Undo(turn, state) => {

View File

@ -1312,22 +1312,9 @@ const INTERFACE = {
} break; } break;
case GameMessage.Online: { case GameMessage.Online: {
switch(data.client) { INTERFACE_DATA.Session.Client.Dawn.online = data.dawn;
case 0: { INTERFACE_DATA.Session.Client.Dusk.online = data.dusk;
// Spectator INTERFACE_DATA.Session.Client.Spectators.count = data.spectators;
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.redraw(); INTERFACE.redraw();
} break; } break;

View File

@ -359,9 +359,9 @@ function MESSAGE(event) {
} break; } break;
case GameMessage.Online: { case GameMessage.Online: {
data.client = dat & 0x3; data.dawn = (dat & 0x1) != 0;
data.state = (dat >> 2) & 0xFFFF_FFFF; data.dusk = (dat & 0x2) != 0;
console.log(data.client + " = " + data.state); data.spectators = (dat >> 2) & 0xFF_FFFF;
} break; } break;
case GameMessage.Undo: { case GameMessage.Undo: {