114 lines
2.7 KiB
Rust
114 lines
2.7 KiB
Rust
use crate::{
|
|
app::session::SessionToken,
|
|
util::pack::{pack_u16, pack_u32, unpack_u16},
|
|
};
|
|
use game::util::mask;
|
|
|
|
use super::Packet;
|
|
|
|
#[derive(Clone)]
|
|
pub struct PacketSessionList {
|
|
pub page:u16,
|
|
pub game_state:u8,
|
|
pub is_player:bool,
|
|
pub is_live:bool,
|
|
}
|
|
impl PacketSessionList {
|
|
pub fn new() -> Self
|
|
{
|
|
Self {
|
|
page:0,
|
|
game_state:0,
|
|
is_player:false,
|
|
is_live:false,
|
|
}
|
|
}
|
|
}
|
|
impl Packet for PacketSessionList {
|
|
type Data = Self;
|
|
|
|
fn decode(data:&Vec<u8>, index:&mut usize) -> Result<Self::Data, ()>
|
|
{
|
|
let mut result = Self::new();
|
|
|
|
/* Read flags
|
|
** 0:[2] - Game state
|
|
** 2:[1] - User is player of session
|
|
** 3:[1] - Both players are online
|
|
*/
|
|
if data.len() - *index == 4 {
|
|
let flags = unpack_u16(data, index);
|
|
result.game_state = (flags & mask(2, 0) as u16) as u8;
|
|
result.is_player = (flags & mask(1, 2) as u16) != 0;
|
|
result.is_live = (flags & mask(1, 3) as u16) != 0;
|
|
|
|
result.page = unpack_u16(data, index);
|
|
|
|
Ok(result)
|
|
} else {
|
|
Err(())
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
#[derive(Clone)]
|
|
pub struct PacketSessionListResponseRecord {
|
|
pub token:SessionToken,
|
|
pub handles:[String; 2],
|
|
pub turn:u16,
|
|
pub last_move:[u8; 3],
|
|
pub viewers:u32,
|
|
pub player:bool,
|
|
}
|
|
|
|
#[derive(Clone)]
|
|
pub struct PacketSessionListResponse {
|
|
pub records:Vec<PacketSessionListResponseRecord>,
|
|
}
|
|
impl PacketSessionListResponse {
|
|
pub fn new() -> Self
|
|
{
|
|
Self {
|
|
records:Vec::new(),
|
|
}
|
|
}
|
|
}
|
|
impl Packet for PacketSessionListResponse {
|
|
type Data = Self;
|
|
|
|
fn encode(&self) -> Vec<u8>
|
|
{
|
|
let mut result = pack_u16(self.records.len() as u16);
|
|
|
|
for record in &self.records {
|
|
let mut chunk = record.token.to_vec();
|
|
|
|
// Dawn handle
|
|
let mut bytes = record.handles[0].as_bytes().to_vec();
|
|
chunk.append(&mut pack_u16(bytes.len() as u16));
|
|
if bytes.len() > 0 { chunk.append(&mut bytes); }
|
|
|
|
// Dusk handle
|
|
let mut bytes = record.handles[1].as_bytes().to_vec();
|
|
chunk.append(&mut pack_u16(bytes.len() as u16));
|
|
if bytes.len() > 0 { chunk.append(&mut bytes); }
|
|
|
|
// Turn number
|
|
chunk.append(&mut pack_u16(record.turn));
|
|
|
|
// Last move
|
|
chunk.append(&mut record.last_move.to_vec());
|
|
|
|
// Spectator count
|
|
chunk.append(&mut pack_u32(record.viewers));
|
|
|
|
// User is player
|
|
chunk.append(&mut vec![record.player as u8]);
|
|
|
|
result.append(&mut chunk);
|
|
}
|
|
result
|
|
}
|
|
}
|