Restructuring, prepare for room, Discord support
This commit is contained in:
parent
1fbb0ede50
commit
ed11799bf5
8 changed files with 492 additions and 239 deletions
113
src/client.rs
Normal file
113
src/client.rs
Normal file
|
@ -0,0 +1,113 @@
|
|||
// ---------------
|
||||
// Module overview
|
||||
// ---------------
|
||||
//
|
||||
// DiscordInfo: describes a Discord user's information
|
||||
//
|
||||
// ClientInfo: describes a client's identifying information,
|
||||
// both for websocket and Discord connections.
|
||||
// Webscoket connections have the option to store
|
||||
// DiscordInfo as well for verified connections.
|
||||
//
|
||||
// Client: describes a client, holding ClientInfo and Rc<Room>
|
||||
|
||||
use crate::room::Room;
|
||||
|
||||
use std::cmp::{PartialEq, Eq};
|
||||
use std::hash::{Hash, Hasher};
|
||||
use std::rc::Rc;
|
||||
use std::cell::RefCell;
|
||||
use simple_websockets::{Responder, Message};
|
||||
|
||||
pub struct DiscordInfo {
|
||||
pub username: String,
|
||||
pub discriminator: u16,
|
||||
pub id: u64,
|
||||
}
|
||||
|
||||
impl PartialEq for DiscordInfo {
|
||||
fn eq(&self, other: &Self) -> bool {
|
||||
self.id == other.id
|
||||
}
|
||||
}
|
||||
|
||||
impl Eq for DiscordInfo {}
|
||||
|
||||
pub enum ClientInfo {
|
||||
Ws {
|
||||
id: u64,
|
||||
responder: Responder,
|
||||
discord_info: Option<DiscordInfo>,
|
||||
},
|
||||
Discord {
|
||||
discord_info: DiscordInfo,
|
||||
},
|
||||
}
|
||||
|
||||
impl ClientInfo {
|
||||
fn id(&self) -> u64 {
|
||||
match self {
|
||||
Self::Ws { id, discord_info, .. } => match discord_info {
|
||||
// Discord-verified websocket connection
|
||||
Some(discord_info) => discord_info.id,
|
||||
// Anonymous websocket connection
|
||||
None => *id,
|
||||
},
|
||||
// Discord connection
|
||||
Self::Discord { discord_info } => discord_info.id,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl PartialEq for ClientInfo {
|
||||
fn eq(&self, other: &Self) -> bool {
|
||||
self.id() == other.id()
|
||||
}
|
||||
}
|
||||
|
||||
impl Eq for ClientInfo {}
|
||||
|
||||
impl Hash for ClientInfo {
|
||||
fn hash<H: Hasher>(&self, state: &mut H) {
|
||||
self.id().hash(state)
|
||||
}
|
||||
}
|
||||
|
||||
pub struct Client {
|
||||
info: ClientInfo,
|
||||
pub room: Rc<RefCell<Room>>,
|
||||
}
|
||||
|
||||
impl Client {
|
||||
pub fn new(info: ClientInfo, room: Rc<RefCell<Room>>) -> Self {
|
||||
Self { info, room }
|
||||
}
|
||||
|
||||
pub fn id(&self) -> u64 {
|
||||
match self.info {
|
||||
ClientInfo::Ws { id, .. } => id,
|
||||
ClientInfo::Discord { .. } => unimplemented!("no id for Discord connections"),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn send(&self, message: Message) -> bool {
|
||||
match &self.info {
|
||||
ClientInfo::Ws { responder, .. } => responder.send(message),
|
||||
ClientInfo::Discord { .. } => unimplemented!("no networking implementation for Discord connections"),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl PartialEq for Client {
|
||||
fn eq(&self, other: &Self) -> bool {
|
||||
self.info.eq(&other.info)
|
||||
}
|
||||
}
|
||||
|
||||
impl Eq for Client {}
|
||||
|
||||
impl Hash for Client {
|
||||
fn hash<H: Hasher>(&self, state: &mut H) {
|
||||
self.info.hash(state)
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue