gn_communicator/
lib.rs

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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
use std::{future::Future, sync::Arc};

use models::{
    AIPlayerRegister, CreateMatch, CreatedMatch, GameServerCreate, MatchAbrubtClose, MatchResult,
    Task,
};

pub mod models;
pub mod rabbitmq;

pub trait MessageHandler<T, Fut>: Fn(T) -> Fut + Send + Sync + 'static + Clone {}

impl<T, Fut, F> MessageHandler<T, Fut> for F where F: Fn(T) -> Fut + Send + Sync + 'static + Clone {}

/// Handles communication between the game server and the matchmaker.
/// Structs which implement the Communicator trait enable multi-device communication.
/// This means that if a callback is registered with an "on"-function, it may be called when any device calls a "report" or "send" function.
pub trait Communicator
where
    Self: Sized,
{
    /// Registers a callback for when a match is abruptly closed.
    ///
    /// # Arguments
    ///
    /// * `callback` - A function that handles `MatchAbrubtClose` events.
    async fn on_match_abrupt_close<F, Fut>(&self, callback: F)
    where
        F: MessageHandler<MatchAbrubtClose, Fut>,
        Fut: Future<Output = ()> + Send + Sync + 'static;

    /// Registers a callback for when a match result is reported.
    ///
    /// # Arguments
    ///
    /// * `callback` - A function that handles `MatchResult` events.
    async fn on_match_result<F, Fut>(&self, callback: F)
    where
        F: MessageHandler<MatchResult, Fut>,
        Fut: Future<Output = ()> + Send + Sync + 'static;

    /// Registers a callback for when a match is created.
    ///
    /// # Arguments
    ///
    /// * `callback` - A function that handles `CreatedMatch` events.
    async fn on_match_created<F, Fut>(&self, callback: F)
    where
        F: MessageHandler<CreatedMatch, Fut>,
        Fut: Future<Output = ()> + Send + Sync + 'static;

    /// Registers a callback for when a game server is created.
    ///
    /// # Arguments
    ///
    /// * `callback` - A function that handles `GameServerCreate` events.
    async fn on_game_create<F, Fut>(&self, callback: F)
    where
        F: MessageHandler<GameServerCreate, Fut>,
        Fut: Future<Output = String> + Send + Sync + 'static;

    /// Registers a callback for health check events.
    ///
    /// # Arguments
    ///
    /// * `callback` - A function that handles health check events.
    async fn on_health_check<F, Fut>(&self, callback: F)
    where
        F: MessageHandler<String, Fut>,
        Fut: Future<Output = ()> + Send + Sync + 'static;

    /// Registers a callback for when a match creation request is received.
    ///
    /// # Arguments
    ///
    /// * `callback` - A function that handles `CreateMatch` events.
    async fn on_match_create<F, Fut>(&self, callback: F)
    where
        F: MessageHandler<CreateMatch, Fut>,
        Fut: Future<Output = ()> + Send + Sync + 'static;

    /// Registers a callback for when a new ai-player is registered.
    ///
    /// # Arguments
    ///
    /// * `callback` - A function that handles `AIPlayerRegister` events.
    async fn on_ai_register<F, Fut>(&self, callback: F)
    where
        F: MessageHandler<AIPlayerRegister, Fut>,
        Fut: Future<Output = ()> + Send + Sync + 'static;

    /// Creates a game on the game server.
    ///
    /// # Arguments
    ///
    /// * `game_server` - The game server creation request.
    ///
    /// # Returns
    ///
    /// A result containing the game server ID or an error.
    async fn create_game(
        &self,
        game_server: &GameServerCreate,
    ) -> Result<String, Box<dyn std::error::Error>>;

    /// Sends a health check to the specified client.
    ///
    /// # Arguments
    ///
    /// * `client_id` - The ID of the client to send the health check to.
    async fn send_health_check(&self, client_id: String);

    /// Creates a match based on the provided match request.
    ///
    /// # Arguments
    ///
    /// * `match_request` - The match creation request.
    async fn create_match(&self, match_request: &CreateMatch);

    /// Reports that a match has been created.
    ///
    /// # Arguments
    ///
    /// * `created_match` - The created match information.
    async fn report_match_created(&self, created_match: &CreatedMatch);

    /// Reports the result of a match.
    ///
    /// # Arguments
    ///
    /// * `match_result` - The match result information.
    async fn report_match_result(&self, match_result: &MatchResult);

    /// Reports that a match was abruptly closed.
    ///
    /// # Arguments
    ///
    /// * `match_close` - The abrupt match close information.
    async fn report_match_abrupt_close(&self, match_close: &MatchAbrubtClose);

    /// Sends a message to create a new AI-Task.
    ///
    /// # Arguments
    ///
    /// * `task` - The AI task information.
    async fn create_ai_task(&self, task: &Task);
    
    /// Sends a message to create a new AI-Player.
    ///
    /// # Arguments
    ///
    /// * `ai_player` - AI-Player Information.
    async fn register_ai_player(&self, ai_player: &AIPlayerRegister);
}