Add online user tracking and notification.
This commit is contained in:
parent
c5ce1ec2ad
commit
97a1ba2fc4
@ -74,4 +74,9 @@ impl Session {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn spectators(&self) -> usize
|
||||||
|
{
|
||||||
|
self.connections.len()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -474,6 +474,8 @@ 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;
|
||||||
@ -498,6 +500,16 @@ 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 }
|
||||||
@ -506,8 +518,18 @@ pub async fn thread_system(mut app:App, bus:Bus<protocol::QRPacket>)
|
|||||||
// Join game as spectator.
|
// Join game as spectator.
|
||||||
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, session.spectators() as u32),
|
||||||
|
}),
|
||||||
|
));
|
||||||
|
}
|
||||||
|
|
||||||
true
|
true
|
||||||
} {
|
} {
|
||||||
// Associate session and connection on join
|
// Associate session and connection on join
|
||||||
@ -525,6 +547,11 @@ pub async fn thread_system(mut app:App, bus:Bus<protocol::QRPacket>)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 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)))
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -575,15 +602,47 @@ 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) { session.remove_connection(0, qr.id); }
|
if user_id == Some(session.p_dawn.user) {
|
||||||
else if user_id == Some(session.p_dusk.user) { session.remove_connection(1, 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) {
|
||||||
|
session.remove_connection(1, 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(2, 0),
|
||||||
|
}),
|
||||||
|
));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
else { session.remove_connection(2, qr.id); }
|
else { session.remove_connection(2, qr.id); }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
for packet in packets {
|
||||||
|
app.send_response(packet).await;
|
||||||
|
}
|
||||||
|
|
||||||
Some(QRPacket::new(0, QRPacketData::None))
|
Some(QRPacket::new(0, QRPacketData::None))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -643,7 +643,11 @@ const INTERFACE = {
|
|||||||
if(INTERFACE_DATA.Session.Client.Dawn.handle !== null) {
|
if(INTERFACE_DATA.Session.Client.Dawn.handle !== null) {
|
||||||
let pos = handle_pos[(1 ^ INTERFACE_DATA.player ^ INTERFACE_DATA.rotate) & 1];
|
let pos = handle_pos[(1 ^ INTERFACE_DATA.player ^ INTERFACE_DATA.rotate) & 1];
|
||||||
|
|
||||||
ctx.fillStyle = INTERFACE.Color.Dawn;
|
if(INTERFACE_DATA.Session.Client.Dawn.online) {
|
||||||
|
ctx.fillStyle = INTERFACE.Color.Dawn;
|
||||||
|
} else {
|
||||||
|
ctx.fillStyle = INTERFACE.Color.DawnDark;
|
||||||
|
}
|
||||||
ctx.textBaseline = "middle";
|
ctx.textBaseline = "middle";
|
||||||
ctx.textAlign = "center";
|
ctx.textAlign = "center";
|
||||||
ctx.fillText(INTERFACE_DATA.Session.Client.Dawn.handle, pos.x, pos.y);
|
ctx.fillText(INTERFACE_DATA.Session.Client.Dawn.handle, pos.x, pos.y);
|
||||||
@ -652,7 +656,11 @@ const INTERFACE = {
|
|||||||
if(INTERFACE_DATA.Session.Client.Dusk.handle !== null) {
|
if(INTERFACE_DATA.Session.Client.Dusk.handle !== null) {
|
||||||
let pos = handle_pos[(INTERFACE_DATA.player ^ INTERFACE_DATA.rotate) & 1];
|
let pos = handle_pos[(INTERFACE_DATA.player ^ INTERFACE_DATA.rotate) & 1];
|
||||||
|
|
||||||
ctx.fillStyle = INTERFACE.Color.Dusk;
|
if(INTERFACE_DATA.Session.Client.Dusk.online) {
|
||||||
|
ctx.fillStyle = INTERFACE.Color.Dusk;
|
||||||
|
} else {
|
||||||
|
ctx.fillStyle = INTERFACE.Color.DuskDark;
|
||||||
|
}
|
||||||
ctx.textBaseline = "middle";
|
ctx.textBaseline = "middle";
|
||||||
ctx.textAlign = "center";
|
ctx.textAlign = "center";
|
||||||
ctx.fillText(INTERFACE_DATA.Session.Client.Dusk.handle, pos.x, pos.y);
|
ctx.fillText(INTERFACE_DATA.Session.Client.Dusk.handle, pos.x, pos.y);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user