1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
use axum::extract::ws::{Message, WebSocket};
use futures_util::stream::SplitSink;
use std::sync::Arc;
use tokio::sync::Mutex;

// `Sender` is a type alias for a synchronized WebSocket sender.
//
// This is used to send messages to a WebSocket connection.
type Sender = Arc<Mutex<SplitSink<WebSocket, Message>>>;

/// Struct representing a room of WebSocket clients.
///
/// A `Room` contains a list of WebSocket senders and a room size.
/// The senders are used to send messages to the WebSocket connections,
/// while the room size represents the maximum number of clients allowed in the room.
#[derive(Debug, Clone)]
pub struct Room {
    /// The list of WebSocket senders.
    ///
    /// Each sender is used to send messages to a WebSocket connection.
    pub senders: Vec<Sender>,
    /// The size of the room.
    ///
    /// This represents the maximum number of clients allowed in the room.
    pub size: usize,
}

impl Room {
    /// The default room size.
    ///
    /// This is used as a fallback value when creating a new room.
    pub const DEFAULT_ROOM_SIZE: usize = 2;

    /// Create a new room with the specified size.
    ///
    /// # Arguments
    ///
    /// * `size` - The size of the room.
    ///
    /// # Returns
    ///
    /// A new `Room` instance.
    pub fn new(size: usize) -> Room {
        Room {
            senders: Vec::new(),
            size,
        }
    }
}
#[cfg(test)]
mod tests {
    use super::*;

    #[tokio::test]
    async fn test_room_new() {
        let room = Room::new(5);

        assert_eq!(room.size, 5);

        assert!(room.senders.is_empty());
    }
}