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
use std::{future::Future, sync::Arc};

use gn_matchmaking_state::prelude::RedisAdapterDefault;
use models::{CreateMatch, CreatedMatch, GameServerCreate, MatchAbrubtClose, MatchResult, Task};

pub mod rabbitmq;
pub mod models;
pub mod healthcheck;

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
{}


pub trait Communicator 
where Self: Sized
{
    async fn on_match_abrupt_close<F, Fut>(&self, callback: F)
    where
        F: MessageHandler<MatchAbrubtClose, Fut>,
        Fut: Future<Output = ()> + Send + Sync + 'static;

    async fn on_match_result<F, Fut>(&self, callback: F)
    where
        F: MessageHandler<MatchResult, Fut>,
        Fut: Future<Output = ()> + Send + Sync + 'static;
    
    async fn on_match_created<F, Fut>(&self, callback: F)
    where
        F: MessageHandler<CreatedMatch, Fut>,
        Fut: Future<Output = ()> + Send + Sync + 'static;
    
    async fn on_game_create<F, Fut>(&self, callback: F)
    where
        F: MessageHandler<GameServerCreate, Fut>,
        Fut: Future<Output = String> + Send + Sync + 'static;
    
    async fn on_health_check<F, Fut>(&self, callback: F)
    where
        F: MessageHandler<String, Fut>,
        Fut: Future<Output = ()> + Send + Sync + 'static;

    async fn on_match_create<F, Fut>(&self, callback: F)
    where
        F: MessageHandler<CreateMatch, Fut>,
        Fut: Future<Output = ()> + Send + Sync + 'static;
    
    async fn create_game(&self, game_server: &GameServerCreate) -> Result<String, Box<dyn std::error::Error>>;
    async fn send_health_check(&self, client_id: String);
    async fn create_match(&self, match_request: &CreateMatch);
    async fn report_match_created(&self, created_match: &CreatedMatch);
    async fn report_match_result(&self, match_result: &MatchResult);
    async fn report_match_abrupt_close(&self, match_close: &MatchAbrubtClose);
    async fn create_ai_task(&self, task: &Task);
}