diff --git a/server/src/connection.rs b/server/src/connection.rs index 47b72e8..19b91dc 100644 --- a/server/src/connection.rs +++ b/server/src/connection.rs @@ -1,5 +1,4 @@ -use crate::events::ClientEvent::*; -use crate::events::{ClientEvent, EventResponse}; +use crate::connection::ClientEvent::*; use futures_util::{SinkExt, StreamExt}; use serde::{Deserialize, Serialize}; use std::collections::{HashMap, VecDeque}; @@ -28,6 +27,28 @@ pub fn new_waiting_queue() -> WaitingQueue { Arc::new(Mutex::new(VecDeque::new())) } +#[derive(Serialize, Deserialize, Debug)] +pub struct Step { + pub from: String, + pub to: String, +} + +#[derive(Serialize, Deserialize, Debug)] +#[serde(tag = "type")] +enum ClientEvent { + Join { username: String }, + FindMatch, + Move { from: String, to: String }, + Resign, + Chat { text: String }, + RequestLegalMoves { fen: String }, +} + +#[derive(Serialize, Deserialize, Debug)] +pub struct EventResponse { + pub response: Result<(), String>, +} + #[derive(Debug)] pub struct PlayerConnection { pub id: Uuid, @@ -45,12 +66,6 @@ pub struct GameMatch { pub move_history: Vec, } -#[derive(Serialize, Deserialize, Debug)] -pub struct Step { - pub from: String, - pub to: String, -} - // Message sending utilities pub async fn send_message_to_player( connections: &ConnectionMap, @@ -104,7 +119,6 @@ pub async fn handle_connection( connections: ConnectionMap, matches: MatchMap, waiting_queue: WaitingQueue, - //event_system: crate::events::EventSystem, ) -> anyhow::Result<()> { use tokio_tungstenite::accept_async; @@ -163,7 +177,6 @@ pub async fn handle_connection( println!("response: {:?}", response); - //event_system.send_event(player_id, &response, &connections); let _ = send_message_to_player( &connections, player_id, diff --git a/server/src/events.rs b/server/src/events.rs deleted file mode 100644 index 94b14ea..0000000 --- a/server/src/events.rs +++ /dev/null @@ -1,132 +0,0 @@ -use serde::{Deserialize, Serialize}; -use serde_json::json; -use std::sync::Arc; -use tokio::sync::Mutex; -use tokio::sync::mpsc; -use uuid::Uuid; - -use crate::connection::ConnectionMap; - -#[derive(Serialize, Deserialize, Debug)] -pub struct Step { - pub from: String, - pub to: String, -} - -#[derive(Serialize, Deserialize, Debug)] -#[serde(tag = "type")] -pub enum ClientEvent { - Join { username: String }, - FindMatch, - Move { from: String, to: String }, - Resign, - Chat { text: String }, - RequestLegalMoves { fen: String }, -} - -#[derive(Serialize, Deserialize, Debug)] -pub struct EventResponse { - pub response: Result<(), String>, -} - -/*pub struct EventSystem { - sender: mpsc::UnboundedSender<(Uuid, EventResponse)>, - receiver: Arc>>, -} - -impl Clone for EventSystem { - fn clone(&self) -> Self { - Self { - sender: self.sender.clone(), - receiver: Arc::clone(&self.receiver), - } - } -} - -impl EventSystem { - pub fn new() -> Self { - let (sender, receiver) = mpsc::unbounded_channel(); - Self { - sender, - receiver: Arc::new(Mutex::new(receiver)), - } - } - - pub async fn send_event( - &self, - player_id: Uuid, - event: &EventResponse, - connections: &ConnectionMap, - ) -> Result<(), Box> { - //self.sender.send((player_id, event))?; - println!("Hellodsjaoidsaid"); - crate::connection::send_message_to_player( - &connections, - player_id, - &serde_json::to_string(&event).unwrap(), - ) - .await; - - Ok(()) - } - - pub async fn next_event(&self) -> Option<(Uuid, EventResponse)> { - let mut receiver = self.receiver.lock().await; - receiver.recv().await - } -} - -impl Default for EventSystem { - fn default() -> Self { - Self::new() - } -} - -#[cfg(test)] -mod tests { - use super::*; - use uuid::Uuid; - - #[tokio::test] - async fn test_event_system_send_and_receive() { - let event_system = EventSystem::new(); - let player_id = Uuid::new_v4(); - - let join_event = ClientEvent::Join { - username: "test_user".to_string(), - }; - - let send_result = event_system.send_event(player_id, join_event).await; - assert!(send_result.is_ok(), "Should send event successfully"); - - let received = event_system.next_event().await; - assert!(received.is_some(), "Should receive sent event"); - - let (received_id, received_event) = received.unwrap(); - assert_eq!(received_id, player_id, "Should receive correct player ID"); - - match received_event { - ClientEvent::Join { username } => { - assert_eq!(username, "test_user", "Should receive correct username"); - } - _ => panic!("Should receive Join event"), - } - } - - #[tokio::test] - async fn test_event_system_clone() { - let event_system1 = EventSystem::new(); - let event_system2 = event_system1.clone(); - - let player_id = Uuid::new_v4(); - let event = ClientEvent::FindMatch; - - event_system1.send_event(player_id, event).await.unwrap(); - - let received = event_system2.next_event().await; - assert!( - received.is_some(), - "Cloned event system should receive events" - ); - } -}*/ diff --git a/server/src/main.rs b/server/src/main.rs index cd50264..ad6ee97 100644 --- a/server/src/main.rs +++ b/server/src/main.rs @@ -1,5 +1,4 @@ mod connection; -mod events; mod matchmaking; mod messages; use tokio::net::TcpListener; @@ -15,15 +14,11 @@ async fn main() -> anyhow::Result<()> { let matches = connection::new_match_map(); let waiting_queue = connection::new_waiting_queue(); - // Event system for communication between components - //let event_system = events::EventSystem::new(); - // Start matchmaking background task let matchmaker = matchmaking::MatchmakingSystem::new( connections.clone(), matches.clone(), waiting_queue.clone(), - //event_system.clone(), ); tokio::spawn(async move { matchmaker.run().await; @@ -34,17 +29,10 @@ async fn main() -> anyhow::Result<()> { let connections = connections.clone(); let matches = matches.clone(); let waiting_queue = waiting_queue.clone(); - //let event_system = event_system.clone(); tokio::spawn(async move { - if let Err(e) = connection::handle_connection( - stream, - connections, - matches, - waiting_queue, - //event_system, - ) - .await + if let Err(e) = + connection::handle_connection(stream, connections, matches, waiting_queue).await { eprintln!("Connection error: {}", e); } diff --git a/server/src/matchmaking.rs b/server/src/matchmaking.rs index 1aae4ea..de7eca0 100644 --- a/server/src/matchmaking.rs +++ b/server/src/matchmaking.rs @@ -1,5 +1,4 @@ use crate::connection::{ConnectionMap, GameMatch, MatchMap, WaitingQueue}; -//use crate::events::EventSystem; use rand::random; use uuid::Uuid; @@ -7,21 +6,14 @@ pub struct MatchmakingSystem { connections: ConnectionMap, matches: MatchMap, waiting_queue: WaitingQueue, - //event_system: EventSystem, } impl MatchmakingSystem { - pub fn new( - connections: ConnectionMap, - matches: MatchMap, - waiting_queue: WaitingQueue, - //event_system: EventSystem, - ) -> Self { + pub fn new(connections: ConnectionMap, matches: MatchMap, waiting_queue: WaitingQueue) -> Self { Self { connections, matches, waiting_queue, - //event_system, } } @@ -114,7 +106,6 @@ impl MatchmakingSystem { #[cfg(test)] mod tests { use super::*; - //use crate::events::EventSystem; use uuid::Uuid; use crate::connection::new_connection_map; @@ -126,14 +117,9 @@ mod tests { let connections = new_connection_map(); let matches = new_match_map(); let waiting_queue = new_waiting_queue(); - //let event_system = EventSystem::new(); - let matchmaking = MatchmakingSystem::new( - connections.clone(), - matches.clone(), - waiting_queue.clone(), - //event_system.clone(), - ); + let matchmaking = + MatchmakingSystem::new(connections.clone(), matches.clone(), waiting_queue.clone()); let player1 = Uuid::new_v4(); let player2 = Uuid::new_v4(); @@ -172,14 +158,9 @@ mod tests { let connections = new_connection_map(); let matches = new_match_map(); let waiting_queue = new_waiting_queue(); - //let event_system = EventSystem::new(); - let matchmaking = MatchmakingSystem::new( - connections.clone(), - matches.clone(), - waiting_queue.clone(), - //event_system.clone(), - ); + let matchmaking = + MatchmakingSystem::new(connections.clone(), matches.clone(), waiting_queue.clone()); let player1 = Uuid::new_v4(); {