Skip to main content

gn_communicator/
lib.rs

1use std::{future::Future, sync::Arc};
2
3use models::{
4    AIPlayerRegister, CreateMatch, CreatedMatch, GameServerCreate, MatchAbrubtClose, MatchResult,
5    Task,
6};
7
8pub mod models;
9pub mod rabbitmq;
10
11pub trait MessageHandler<T, Fut>: Fn(T) -> Fut + Send + Sync + 'static + Clone {}
12
13impl<T, Fut, F> MessageHandler<T, Fut> for F where F: Fn(T) -> Fut + Send + Sync + 'static + Clone {}
14
15/// Handles communication between the game server and the matchmaker.
16/// Structs which implement the Communicator trait enable multi-device communication.
17/// 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.
18pub trait Communicator
19where
20    Self: Sized,
21{
22    /// Registers a callback for when a match is abruptly closed.
23    ///
24    /// # Arguments
25    ///
26    /// * `callback` - A function that handles `MatchAbrubtClose` events.
27    async fn on_match_abrupt_close<F, Fut>(&self, callback: F)
28    where
29        F: MessageHandler<MatchAbrubtClose, Fut>,
30        Fut: Future<Output = ()> + Send + 'static;
31
32    /// Registers a callback for when a match result is reported.
33    ///
34    /// # Arguments
35    ///
36    /// * `callback` - A function that handles `MatchResult` events.
37    async fn on_match_result<F, Fut>(&self, callback: F)
38    where
39        F: MessageHandler<MatchResult, Fut>,
40        Fut: Future<Output = ()> + Send + 'static;
41
42    /// Registers a callback for when a match is created.
43    ///
44    /// # Arguments
45    ///
46    /// * `callback` - A function that handles `CreatedMatch` events.
47    async fn on_match_created<F, Fut>(&self, callback: F)
48    where
49        F: MessageHandler<CreatedMatch, Fut>,
50        Fut: Future<Output = ()> + Send + 'static;
51
52    /// Registers a callback for when a game server is created.
53    ///
54    /// # Arguments
55    ///
56    /// * `callback` - A function that handles `GameServerCreate` events.
57    async fn on_game_create<F, Fut>(&self, callback: F)
58    where
59        F: MessageHandler<GameServerCreate, Fut>,
60        Fut: Future<Output = String> + Send + 'static;
61
62    /// Registers a callback for health check events.
63    ///
64    /// # Arguments
65    ///
66    /// * `callback` - A function that handles health check events.
67    async fn on_health_check<F, Fut>(&self, callback: F)
68    where
69        F: MessageHandler<String, Fut>,
70        Fut: Future<Output = ()> + Send + 'static;
71
72    /// Registers a callback for when a match creation request is received.
73    ///
74    /// # Arguments
75    ///
76    /// * `callback` - A function that handles `CreateMatch` events.
77    async fn on_match_create<F, Fut>(&self, callback: F)
78    where
79        F: MessageHandler<CreateMatch, Fut>,
80        Fut: Future<Output = ()> + Send + 'static;
81
82    /// Registers a callback for when a new ai-player is registered.
83    ///
84    /// # Arguments
85    ///
86    /// * `callback` - A function that handles `AIPlayerRegister` events.
87    async fn on_ai_register<F, Fut>(&self, callback: F)
88    where
89        F: MessageHandler<AIPlayerRegister, Fut>,
90        Fut: Future<Output = ()> + Send + 'static;
91
92    /// Creates a game on the game server.
93    ///
94    /// # Arguments
95    ///
96    /// * `game_server` - The game server creation request.
97    ///
98    /// # Returns
99    ///
100    /// A result containing the game server ID or an error.
101    async fn create_game(
102        &self,
103        game_server: &GameServerCreate,
104    ) -> Result<(), Box<dyn std::error::Error>>;
105
106    /// Sends a health check to the specified client.
107    ///
108    /// # Arguments
109    ///
110    /// * `client_id` - The ID of the client to send the health check to.
111    async fn send_health_check(&self, client_id: String);
112
113    /// Creates a match based on the provided match request.
114    ///
115    /// # Arguments
116    ///
117    /// * `match_request` - The match creation request.
118    async fn create_match(&self, match_request: &CreateMatch);
119
120    /// Reports that a match has been created.
121    ///
122    /// # Arguments
123    ///
124    /// * `created_match` - The created match information.
125    async fn report_match_created(&self, created_match: &CreatedMatch);
126
127    /// Reports the result of a match.
128    ///
129    /// # Arguments
130    ///
131    /// * `match_result` - The match result information.
132    async fn report_match_result(&self, match_result: &MatchResult);
133
134    /// Reports that a match was abruptly closed.
135    ///
136    /// # Arguments
137    ///
138    /// * `match_close` - The abrupt match close information.
139    async fn report_match_abrupt_close(&self, match_close: &MatchAbrubtClose);
140
141    /// Sends a message to create a new AI-Task.
142    ///
143    /// # Arguments
144    ///
145    /// * `task` - The AI task information.
146    async fn create_ai_task(&self, task: &Task);
147    
148    /// Sends a message to create a new AI-Player.
149    ///
150    /// # Arguments
151    ///
152    /// * `ai_player` - AI-Player Information.
153    async fn register_ai_player(&self, ai_player: &AIPlayerRegister);
154}