|
|
|
@ -1,13 +1,13 @@
|
|
|
|
|
use crate::client::{Client, ClientInfo};
|
|
|
|
|
use crate::database::{DatabaseSettings, DatabaseType, DatabaseCreationError};
|
|
|
|
|
use crate::database::{DatabaseCreationError, DatabaseSettings, DatabaseType};
|
|
|
|
|
use crate::response::MessageResponseData;
|
|
|
|
|
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::rc::Rc;
|
|
|
|
|
use std::cell::RefCell;
|
|
|
|
|
use simple_websockets::{Event, Responder, Message, EventHub};
|
|
|
|
|
use derive_more::From;
|
|
|
|
|
|
|
|
|
|
#[derive(From, Debug)]
|
|
|
|
|
pub enum ServerCreationError {
|
|
|
|
@ -66,27 +66,36 @@ impl Server {
|
|
|
|
|
pub fn run(&mut self) {
|
|
|
|
|
loop {
|
|
|
|
|
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::Message(client_id, message) => self.handle_message(client_id, message),
|
|
|
|
|
} {
|
|
|
|
|
Ok(()) => {},
|
|
|
|
|
Ok(()) => {}
|
|
|
|
|
Err(error) => println!("{:?}", error),
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn new_client(&mut self, client_id: u64, responder: Responder) -> &Client {
|
|
|
|
|
let client = Client::new(ClientInfo::Ws {
|
|
|
|
|
id: client_id,
|
|
|
|
|
responder,
|
|
|
|
|
discord_info: None,
|
|
|
|
|
}, self.lobby.clone());
|
|
|
|
|
let client = Client::new(
|
|
|
|
|
ClientInfo::Ws {
|
|
|
|
|
id: client_id,
|
|
|
|
|
responder,
|
|
|
|
|
discord_info: None,
|
|
|
|
|
},
|
|
|
|
|
self.lobby.clone(),
|
|
|
|
|
);
|
|
|
|
|
self.clients.insert(client_id, client);
|
|
|
|
|
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
|
|
|
|
|
println!("A client connected with id #{}", client_id);
|
|
|
|
|
|
|
|
|
@ -98,15 +107,21 @@ impl Server {
|
|
|
|
|
let room = client.room.borrow();
|
|
|
|
|
|
|
|
|
|
// Send client greeting
|
|
|
|
|
client.send(MessageResponseData::Greeting {
|
|
|
|
|
id: client_id,
|
|
|
|
|
next_mora: room.next_mora().clone(),
|
|
|
|
|
}.into_message());
|
|
|
|
|
client.send(
|
|
|
|
|
MessageResponseData::Greeting {
|
|
|
|
|
id: client_id,
|
|
|
|
|
next_mora: room.next_mora().clone(),
|
|
|
|
|
}
|
|
|
|
|
.into_message(),
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
// Sent recent message history
|
|
|
|
|
client.send(MessageResponseData::History {
|
|
|
|
|
words: room.get_history()?,
|
|
|
|
|
}.into_message());
|
|
|
|
|
client.send(
|
|
|
|
|
MessageResponseData::History {
|
|
|
|
|
words: room.get_history()?,
|
|
|
|
|
}
|
|
|
|
|
.into_message(),
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 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) -> ()) {
|
|
|
|
|
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);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
@ -145,7 +164,10 @@ impl Server {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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() {
|
|
|
|
|
client.send(response.to_message());
|
|
|
|
|
}
|
|
|
|
@ -163,13 +185,19 @@ impl Server {
|
|
|
|
|
let message = match message {
|
|
|
|
|
Message::Text(message) => {
|
|
|
|
|
// Debug
|
|
|
|
|
println!("Received a message from client #{}: {:?}", client_id, message);
|
|
|
|
|
println!(
|
|
|
|
|
"Received a message from client #{}: {:?}",
|
|
|
|
|
client_id, message
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
message
|
|
|
|
|
}
|
|
|
|
|
Message::Binary(message) => {
|
|
|
|
|
// Debug
|
|
|
|
|
println!("Received a binary message from client #{}: {:?}", client_id, message);
|
|
|
|
|
println!(
|
|
|
|
|
"Received a binary message from client #{}: {:?}",
|
|
|
|
|
client_id, message
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
return Ok(());
|
|
|
|
|
}
|
|
|
|
@ -182,7 +210,12 @@ impl Server {
|
|
|
|
|
Ok(response) => self.announce_to_room(&client.room, response),
|
|
|
|
|
// Send errors to only this client
|
|
|
|
|
Err(message) => {
|
|
|
|
|
client.send(MessageResponseData::Error { message: message.to_string() }.into_message());
|
|
|
|
|
client.send(
|
|
|
|
|
MessageResponseData::Error {
|
|
|
|
|
message: message.to_string(),
|
|
|
|
|
}
|
|
|
|
|
.into_message(),
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
Ok(())
|
|
|
|
|