removed event system file as it was unused and we do not neet it anymore

This commit is contained in:
2025-11-15 19:41:48 +01:00
parent 1c92918692
commit c77e534ed8
4 changed files with 30 additions and 180 deletions

View File

@@ -1,5 +1,4 @@
use crate::events::ClientEvent::*; use crate::connection::ClientEvent::*;
use crate::events::{ClientEvent, EventResponse};
use futures_util::{SinkExt, StreamExt}; use futures_util::{SinkExt, StreamExt};
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use std::collections::{HashMap, VecDeque}; use std::collections::{HashMap, VecDeque};
@@ -28,6 +27,28 @@ pub fn new_waiting_queue() -> WaitingQueue {
Arc::new(Mutex::new(VecDeque::new())) 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)] #[derive(Debug)]
pub struct PlayerConnection { pub struct PlayerConnection {
pub id: Uuid, pub id: Uuid,
@@ -45,12 +66,6 @@ pub struct GameMatch {
pub move_history: Vec<String>, pub move_history: Vec<String>,
} }
#[derive(Serialize, Deserialize, Debug)]
pub struct Step {
pub from: String,
pub to: String,
}
// Message sending utilities // Message sending utilities
pub async fn send_message_to_player( pub async fn send_message_to_player(
connections: &ConnectionMap, connections: &ConnectionMap,
@@ -104,7 +119,6 @@ pub async fn handle_connection(
connections: ConnectionMap, connections: ConnectionMap,
matches: MatchMap, matches: MatchMap,
waiting_queue: WaitingQueue, waiting_queue: WaitingQueue,
//event_system: crate::events::EventSystem,
) -> anyhow::Result<()> { ) -> anyhow::Result<()> {
use tokio_tungstenite::accept_async; use tokio_tungstenite::accept_async;
@@ -163,7 +177,6 @@ pub async fn handle_connection(
println!("response: {:?}", response); println!("response: {:?}", response);
//event_system.send_event(player_id, &response, &connections);
let _ = send_message_to_player( let _ = send_message_to_player(
&connections, &connections,
player_id, player_id,

View File

@@ -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<Mutex<mpsc::UnboundedReceiver<(Uuid, EventResponse)>>>,
}
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<dyn std::error::Error>> {
//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"
);
}
}*/

View File

@@ -1,5 +1,4 @@
mod connection; mod connection;
mod events;
mod matchmaking; mod matchmaking;
mod messages; mod messages;
use tokio::net::TcpListener; use tokio::net::TcpListener;
@@ -15,15 +14,11 @@ async fn main() -> anyhow::Result<()> {
let matches = connection::new_match_map(); let matches = connection::new_match_map();
let waiting_queue = connection::new_waiting_queue(); let waiting_queue = connection::new_waiting_queue();
// Event system for communication between components
//let event_system = events::EventSystem::new();
// Start matchmaking background task // Start matchmaking background task
let matchmaker = matchmaking::MatchmakingSystem::new( let matchmaker = matchmaking::MatchmakingSystem::new(
connections.clone(), connections.clone(),
matches.clone(), matches.clone(),
waiting_queue.clone(), waiting_queue.clone(),
//event_system.clone(),
); );
tokio::spawn(async move { tokio::spawn(async move {
matchmaker.run().await; matchmaker.run().await;
@@ -34,17 +29,10 @@ async fn main() -> anyhow::Result<()> {
let connections = connections.clone(); let connections = connections.clone();
let matches = matches.clone(); let matches = matches.clone();
let waiting_queue = waiting_queue.clone(); let waiting_queue = waiting_queue.clone();
//let event_system = event_system.clone();
tokio::spawn(async move { tokio::spawn(async move {
if let Err(e) = connection::handle_connection( if let Err(e) =
stream, connection::handle_connection(stream, connections, matches, waiting_queue).await
connections,
matches,
waiting_queue,
//event_system,
)
.await
{ {
eprintln!("Connection error: {}", e); eprintln!("Connection error: {}", e);
} }

View File

@@ -1,5 +1,4 @@
use crate::connection::{ConnectionMap, GameMatch, MatchMap, WaitingQueue}; use crate::connection::{ConnectionMap, GameMatch, MatchMap, WaitingQueue};
//use crate::events::EventSystem;
use rand::random; use rand::random;
use uuid::Uuid; use uuid::Uuid;
@@ -7,21 +6,14 @@ pub struct MatchmakingSystem {
connections: ConnectionMap, connections: ConnectionMap,
matches: MatchMap, matches: MatchMap,
waiting_queue: WaitingQueue, waiting_queue: WaitingQueue,
//event_system: EventSystem,
} }
impl MatchmakingSystem { impl MatchmakingSystem {
pub fn new( pub fn new(connections: ConnectionMap, matches: MatchMap, waiting_queue: WaitingQueue) -> Self {
connections: ConnectionMap,
matches: MatchMap,
waiting_queue: WaitingQueue,
//event_system: EventSystem,
) -> Self {
Self { Self {
connections, connections,
matches, matches,
waiting_queue, waiting_queue,
//event_system,
} }
} }
@@ -114,7 +106,6 @@ impl MatchmakingSystem {
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use super::*; use super::*;
//use crate::events::EventSystem;
use uuid::Uuid; use uuid::Uuid;
use crate::connection::new_connection_map; use crate::connection::new_connection_map;
@@ -126,14 +117,9 @@ mod tests {
let connections = new_connection_map(); let connections = new_connection_map();
let matches = new_match_map(); let matches = new_match_map();
let waiting_queue = new_waiting_queue(); let waiting_queue = new_waiting_queue();
//let event_system = EventSystem::new();
let matchmaking = MatchmakingSystem::new( let matchmaking =
connections.clone(), MatchmakingSystem::new(connections.clone(), matches.clone(), waiting_queue.clone());
matches.clone(),
waiting_queue.clone(),
//event_system.clone(),
);
let player1 = Uuid::new_v4(); let player1 = Uuid::new_v4();
let player2 = Uuid::new_v4(); let player2 = Uuid::new_v4();
@@ -172,14 +158,9 @@ mod tests {
let connections = new_connection_map(); let connections = new_connection_map();
let matches = new_match_map(); let matches = new_match_map();
let waiting_queue = new_waiting_queue(); let waiting_queue = new_waiting_queue();
//let event_system = EventSystem::new();
let matchmaking = MatchmakingSystem::new( let matchmaking =
connections.clone(), MatchmakingSystem::new(connections.clone(), matches.clone(), waiting_queue.clone());
matches.clone(),
waiting_queue.clone(),
//event_system.clone(),
);
let player1 = Uuid::new_v4(); let player1 = Uuid::new_v4();
{ {