|
|
@ -1,13 +1,13 @@
|
|
|
|
use crate::client::{Client, ClientInfo};
|
|
|
|
use crate::client::{Client, ClientInfo};
|
|
|
|
use crate::database::{DatabaseSettings, DatabaseType, DatabaseCreationError};
|
|
|
|
use crate::database::{DatabaseCreationError, DatabaseSettings, DatabaseType};
|
|
|
|
use crate::response::MessageResponseData;
|
|
|
|
use crate::response::MessageResponseData;
|
|
|
|
use crate::room::{Room, RoomSettings};
|
|
|
|
use crate::room::{Room, RoomSettings};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
use derive_more::From;
|
|
|
|
|
|
|
|
use simple_websockets::{Event, EventHub, Message, Responder};
|
|
|
|
|
|
|
|
use std::cell::RefCell;
|
|
|
|
use std::collections::HashMap;
|
|
|
|
use std::collections::HashMap;
|
|
|
|
use std::rc::Rc;
|
|
|
|
use std::rc::Rc;
|
|
|
|
use std::cell::RefCell;
|
|
|
|
|
|
|
|
use simple_websockets::{Event, Responder, Message, EventHub};
|
|
|
|
|
|
|
|
use derive_more::From;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#[derive(From, Debug)]
|
|
|
|
#[derive(From, Debug)]
|
|
|
|
pub enum ServerCreationError {
|
|
|
|
pub enum ServerCreationError {
|
|
|
@ -66,27 +66,36 @@ impl Server {
|
|
|
|
pub fn run(&mut self) {
|
|
|
|
pub fn run(&mut self) {
|
|
|
|
loop {
|
|
|
|
loop {
|
|
|
|
match match self.event_hub.poll_event() {
|
|
|
|
match match self.event_hub.poll_event() {
|
|
|
|
Event::Connect(client_id, responder) => self.handle_connection(client_id, responder),
|
|
|
|
Event::Connect(client_id, responder) => {
|
|
|
|
|
|
|
|
self.handle_connection(client_id, responder)
|
|
|
|
|
|
|
|
}
|
|
|
|
Event::Disconnect(client_id) => self.handle_disconnection(client_id),
|
|
|
|
Event::Disconnect(client_id) => self.handle_disconnection(client_id),
|
|
|
|
Event::Message(client_id, message) => self.handle_message(client_id, message),
|
|
|
|
Event::Message(client_id, message) => self.handle_message(client_id, message),
|
|
|
|
} {
|
|
|
|
} {
|
|
|
|
Ok(()) => {},
|
|
|
|
Ok(()) => {}
|
|
|
|
Err(error) => println!("{:?}", error),
|
|
|
|
Err(error) => println!("{:?}", error),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
fn new_client(&mut self, client_id: u64, responder: Responder) -> &Client {
|
|
|
|
fn new_client(&mut self, client_id: u64, responder: Responder) -> &Client {
|
|
|
|
let client = Client::new(ClientInfo::Ws {
|
|
|
|
let client = Client::new(
|
|
|
|
id: client_id,
|
|
|
|
ClientInfo::Ws {
|
|
|
|
responder,
|
|
|
|
id: client_id,
|
|
|
|
discord_info: None,
|
|
|
|
responder,
|
|
|
|
}, self.lobby.clone());
|
|
|
|
discord_info: None,
|
|
|
|
|
|
|
|
},
|
|
|
|
|
|
|
|
self.lobby.clone(),
|
|
|
|
|
|
|
|
);
|
|
|
|
self.clients.insert(client_id, client);
|
|
|
|
self.clients.insert(client_id, client);
|
|
|
|
self.clients.get(&client_id).unwrap()
|
|
|
|
self.clients.get(&client_id).unwrap()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
fn handle_connection(&mut self, client_id: u64, responder: Responder) -> Result<(), ServerError> {
|
|
|
|
fn handle_connection(
|
|
|
|
|
|
|
|
&mut self,
|
|
|
|
|
|
|
|
client_id: u64,
|
|
|
|
|
|
|
|
responder: Responder,
|
|
|
|
|
|
|
|
) -> Result<(), ServerError> {
|
|
|
|
// Debug
|
|
|
|
// Debug
|
|
|
|
println!("A client connected with id #{}", client_id);
|
|
|
|
println!("A client connected with id #{}", client_id);
|
|
|
|
|
|
|
|
|
|
|
@ -98,15 +107,21 @@ impl Server {
|
|
|
|
let room = client.room.borrow();
|
|
|
|
let room = client.room.borrow();
|
|
|
|
|
|
|
|
|
|
|
|
// Send client greeting
|
|
|
|
// Send client greeting
|
|
|
|
client.send(MessageResponseData::Greeting {
|
|
|
|
client.send(
|
|
|
|
id: client_id,
|
|
|
|
MessageResponseData::Greeting {
|
|
|
|
next_mora: room.next_mora().clone(),
|
|
|
|
id: client_id,
|
|
|
|
}.into_message());
|
|
|
|
next_mora: room.next_mora().clone(),
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
.into_message(),
|
|
|
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
|
|
// Sent recent message history
|
|
|
|
// Sent recent message history
|
|
|
|
client.send(MessageResponseData::History {
|
|
|
|
client.send(
|
|
|
|
words: room.get_history()?,
|
|
|
|
MessageResponseData::History {
|
|
|
|
}.into_message());
|
|
|
|
words: room.get_history()?,
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
.into_message(),
|
|
|
|
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Number of clients on Rc<RefCell<Room>> reference counter will be one more
|
|
|
|
// Number of clients on Rc<RefCell<Room>> reference counter will be one more
|
|
|
@ -133,7 +148,11 @@ impl Server {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
fn for_client_in_room(&self, room: &Rc<RefCell<Room>>, mut closure: impl FnMut(&Client) -> ()) {
|
|
|
|
fn for_client_in_room(&self, room: &Rc<RefCell<Room>>, mut closure: impl FnMut(&Client) -> ()) {
|
|
|
|
for (_id, client) in self.clients.iter().filter(|(_id, client)| Rc::<RefCell<Room>>::ptr_eq(room, &client.room)) {
|
|
|
|
for (_id, client) in self
|
|
|
|
|
|
|
|
.clients
|
|
|
|
|
|
|
|
.iter()
|
|
|
|
|
|
|
|
.filter(|(_id, client)| Rc::<RefCell<Room>>::ptr_eq(room, &client.room))
|
|
|
|
|
|
|
|
{
|
|
|
|
closure(client);
|
|
|
|
closure(client);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
@ -145,7 +164,10 @@ impl Server {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
fn broadcast_player_count(&self, room: &Rc<RefCell<Room>>) {
|
|
|
|
fn broadcast_player_count(&self, room: &Rc<RefCell<Room>>) {
|
|
|
|
let response = MessageResponseData::PlayerCount { players: self.client_count_in_room(room) as u64 }.into_response();
|
|
|
|
let response = MessageResponseData::PlayerCount {
|
|
|
|
|
|
|
|
players: self.client_count_in_room(room) as u64,
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
.into_response();
|
|
|
|
for (_id, client) in self.clients.iter() {
|
|
|
|
for (_id, client) in self.clients.iter() {
|
|
|
|
client.send(response.to_message());
|
|
|
|
client.send(response.to_message());
|
|
|
|
}
|
|
|
|
}
|
|
|
@ -163,13 +185,19 @@ impl Server {
|
|
|
|
let message = match message {
|
|
|
|
let message = match message {
|
|
|
|
Message::Text(message) => {
|
|
|
|
Message::Text(message) => {
|
|
|
|
// Debug
|
|
|
|
// Debug
|
|
|
|
println!("Received a message from client #{}: {:?}", client_id, message);
|
|
|
|
println!(
|
|
|
|
|
|
|
|
"Received a message from client #{}: {:?}",
|
|
|
|
|
|
|
|
client_id, message
|
|
|
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
|
|
message
|
|
|
|
message
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Message::Binary(message) => {
|
|
|
|
Message::Binary(message) => {
|
|
|
|
// Debug
|
|
|
|
// Debug
|
|
|
|
println!("Received a binary message from client #{}: {:?}", client_id, message);
|
|
|
|
println!(
|
|
|
|
|
|
|
|
"Received a binary message from client #{}: {:?}",
|
|
|
|
|
|
|
|
client_id, message
|
|
|
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
|
|
return Ok(());
|
|
|
|
return Ok(());
|
|
|
|
}
|
|
|
|
}
|
|
|
@ -182,7 +210,12 @@ impl Server {
|
|
|
|
Ok(response) => self.announce_to_room(&client.room, response),
|
|
|
|
Ok(response) => self.announce_to_room(&client.room, response),
|
|
|
|
// Send errors to only this client
|
|
|
|
// Send errors to only this client
|
|
|
|
Err(message) => {
|
|
|
|
Err(message) => {
|
|
|
|
client.send(MessageResponseData::Error { message: message.to_string() }.into_message());
|
|
|
|
client.send(
|
|
|
|
|
|
|
|
MessageResponseData::Error {
|
|
|
|
|
|
|
|
message: message.to_string(),
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
.into_message(),
|
|
|
|
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
};
|
|
|
|
Ok(())
|
|
|
|
Ok(())
|
|
|
|