ApiClient

Struct ApiClient 

Source
pub struct ApiClient {
    pub session_id: Option<String>,
    pub matchmaker_token: Option<String>,
    /* private fields */
}
Expand description

Statefull, non-blocking nakama client. Works as a state machine - all calls are non-blocking, but may modify some internal ApiClient state and therefore results of other calls in the future.

Fields§

§session_id: Option<String>§matchmaker_token: Option<String>

Implementations§

Source§

impl ApiClient

Source

pub fn new(key: &str, server: &str, port: u32, protocol: &str) -> ApiClient

Examples found in repository?
examples/register.rs (line 5)
4fn main() {
5    let mut client = ApiClient::new("defaultkey", "127.0.0.1", config::DEFAULT_PORT, "http");
6    // Note that the minimum password length is 8 characters!
7    client.register("some@user.com", "password", "Username");
8
9    while client.in_progress() {
10        client.tick()
11    }
12
13    println!("Username: {:?}", client.username());
14}
More examples
Hide additional examples
examples/leaderboard.rs (line 7)
6fn main() {
7    let mut client = ApiClient::new("defaultkey", "127.0.0.1", config::DEFAULT_PORT, "http");
8
9    client.register("email@provider.com", "password", "Leader");
10
11    while client.in_progress() {
12        client.tick()
13    }
14
15    client.write_leaderboard_record("wins", 1);
16
17    while client.in_progress() {
18        client.tick()
19    }
20
21    client.list_leaderboard_records("wins");
22
23    while client.in_progress() {
24        client.tick()
25    }
26
27    if let Some(leaderboard) = client.leaderboard_records("wins") {
28        for record in &leaderboard.records {
29            println!("{:?}", record);
30        }
31    }
32}
examples/matchmaker.rs (line 22)
20fn match_rank_range() {
21    let mut clients: Vec<ApiClient> = (1..5).map(|_| {
22        ApiClient::new("defaultkey", "127.0.0.1", config::DEFAULT_PORT, "http")
23    }).collect();
24
25    clients.iter_mut().enumerate().for_each(|(i, client)| {
26        client.register(&format!("email{}@provider.com", i), "password", &format!("Player{}", i));
27    });
28
29    tick_clients(&mut clients);
30
31    println!("Client 1 with rank 5 searching for a match with rank between 3 and 7");
32    let mut matchmaker = Matchmaker::new();
33    matchmaker
34        .add_numeric_property("rank", 5.0)
35        .add_query_item(&QueryItemBuilder::new("rank").geq(3).required().build())
36        .add_query_item(&QueryItemBuilder::new("rank").leq(7).required().build());
37
38    println!("Client 2 with rank 4 searching for a match with rank between 2 and 6");
39    let mut matchmaker2 = Matchmaker::new();
40    matchmaker2
41        .add_numeric_property("rank", 4.0)
42        .add_query_item(&QueryItemBuilder::new("rank").geq(2).required().build())
43        .add_query_item(&QueryItemBuilder::new("rank").leq(6).required().build());
44
45    println!("Client 3 with rank 10 searching for a match with rank between 8 and 12");
46    let mut matchmaker3 = Matchmaker::new();
47    matchmaker3
48        .add_numeric_property("rank", 10.0)
49        .add_query_item(&QueryItemBuilder::new("rank").geq(8).required().build())
50        .add_query_item(&QueryItemBuilder::new("rank").leq(12).required().build());
51
52    println!("Client 4 with rank 9 searching for a match with rank between 7 and 11");
53    let mut matchmaker4 = Matchmaker::new();
54    matchmaker4
55        .add_numeric_property("rank", 9.0)
56        .add_query_item(&QueryItemBuilder::new("rank").geq(7).required().build())
57        .add_query_item(&QueryItemBuilder::new("rank").leq(11).required().build());
58
59    clients[0].socket_add_matchmaker(&matchmaker);
60    clients[1].socket_add_matchmaker(&matchmaker2);
61    clients[2].socket_add_matchmaker(&matchmaker3);
62    clients[3].socket_add_matchmaker(&matchmaker4);
63
64    wait_tokens(&mut clients) ;
65
66    assert_eq!(clients[0].matchmaker_token, clients[1].matchmaker_token);
67    println!("Successfully ranked the clients with rank 4 and 5");
68    assert_eq!(clients[2].matchmaker_token, clients[3].matchmaker_token);
69    println!("Successfully ranked the clients with rank 9 and 10");
70}
71
72fn match_region() {
73    let mut client = ApiClient::new("defaultkey", "127.0.0.1", config::DEFAULT_PORT, "http");
74    let mut client2 = ApiClient::new("defaultkey", "127.0.0.1", config::DEFAULT_PORT, "http");
75
76    client.register("email1@provider.com", "password", "Player1");
77    client2.register("email2@provider.com", "password", "Player2");
78
79    while client.in_progress() {
80        client.tick()
81    }
82
83    while client2.in_progress() {
84        client2.tick()
85    }
86
87    println!("Both clients Searching for a match in Europe");
88    let mut matchmaker = Matchmaker::new();
89    matchmaker
90        .add_string_property("region", "Europe")
91        .add_query_item(&QueryItemBuilder::new("region").term("Europe").required().build());
92
93    client.socket_add_matchmaker(&matchmaker);
94    client2.socket_add_matchmaker(&matchmaker);
95
96    while client.matchmaker_token.is_none() || client2.matchmaker_token.is_none() {
97        client.tick();
98        client2.tick();
99    }
100
101    assert_eq!(client.matchmaker_token, client2.matchmaker_token);
102    println!("Both clients found a match in Europe");
103}
Source

pub fn in_progress(&self) -> bool

Examples found in repository?
examples/matchmaker.rs (line 5)
4fn tick_clients(clients: &mut Vec<ApiClient>) {
5    while clients.iter().any(|client| client.in_progress()) {
6       clients.iter_mut().for_each(|client| {
7           client.tick();
8       })
9    }
10}
11
12fn wait_tokens(clients: &mut Vec<ApiClient>) {
13    while clients.iter().any(|client| client.matchmaker_token.is_none()) {
14        clients.iter_mut().for_each(|client| {
15            client.tick();
16        })
17    }
18}
19
20fn match_rank_range() {
21    let mut clients: Vec<ApiClient> = (1..5).map(|_| {
22        ApiClient::new("defaultkey", "127.0.0.1", config::DEFAULT_PORT, "http")
23    }).collect();
24
25    clients.iter_mut().enumerate().for_each(|(i, client)| {
26        client.register(&format!("email{}@provider.com", i), "password", &format!("Player{}", i));
27    });
28
29    tick_clients(&mut clients);
30
31    println!("Client 1 with rank 5 searching for a match with rank between 3 and 7");
32    let mut matchmaker = Matchmaker::new();
33    matchmaker
34        .add_numeric_property("rank", 5.0)
35        .add_query_item(&QueryItemBuilder::new("rank").geq(3).required().build())
36        .add_query_item(&QueryItemBuilder::new("rank").leq(7).required().build());
37
38    println!("Client 2 with rank 4 searching for a match with rank between 2 and 6");
39    let mut matchmaker2 = Matchmaker::new();
40    matchmaker2
41        .add_numeric_property("rank", 4.0)
42        .add_query_item(&QueryItemBuilder::new("rank").geq(2).required().build())
43        .add_query_item(&QueryItemBuilder::new("rank").leq(6).required().build());
44
45    println!("Client 3 with rank 10 searching for a match with rank between 8 and 12");
46    let mut matchmaker3 = Matchmaker::new();
47    matchmaker3
48        .add_numeric_property("rank", 10.0)
49        .add_query_item(&QueryItemBuilder::new("rank").geq(8).required().build())
50        .add_query_item(&QueryItemBuilder::new("rank").leq(12).required().build());
51
52    println!("Client 4 with rank 9 searching for a match with rank between 7 and 11");
53    let mut matchmaker4 = Matchmaker::new();
54    matchmaker4
55        .add_numeric_property("rank", 9.0)
56        .add_query_item(&QueryItemBuilder::new("rank").geq(7).required().build())
57        .add_query_item(&QueryItemBuilder::new("rank").leq(11).required().build());
58
59    clients[0].socket_add_matchmaker(&matchmaker);
60    clients[1].socket_add_matchmaker(&matchmaker2);
61    clients[2].socket_add_matchmaker(&matchmaker3);
62    clients[3].socket_add_matchmaker(&matchmaker4);
63
64    wait_tokens(&mut clients) ;
65
66    assert_eq!(clients[0].matchmaker_token, clients[1].matchmaker_token);
67    println!("Successfully ranked the clients with rank 4 and 5");
68    assert_eq!(clients[2].matchmaker_token, clients[3].matchmaker_token);
69    println!("Successfully ranked the clients with rank 9 and 10");
70}
71
72fn match_region() {
73    let mut client = ApiClient::new("defaultkey", "127.0.0.1", config::DEFAULT_PORT, "http");
74    let mut client2 = ApiClient::new("defaultkey", "127.0.0.1", config::DEFAULT_PORT, "http");
75
76    client.register("email1@provider.com", "password", "Player1");
77    client2.register("email2@provider.com", "password", "Player2");
78
79    while client.in_progress() {
80        client.tick()
81    }
82
83    while client2.in_progress() {
84        client2.tick()
85    }
86
87    println!("Both clients Searching for a match in Europe");
88    let mut matchmaker = Matchmaker::new();
89    matchmaker
90        .add_string_property("region", "Europe")
91        .add_query_item(&QueryItemBuilder::new("region").term("Europe").required().build());
92
93    client.socket_add_matchmaker(&matchmaker);
94    client2.socket_add_matchmaker(&matchmaker);
95
96    while client.matchmaker_token.is_none() || client2.matchmaker_token.is_none() {
97        client.tick();
98        client2.tick();
99    }
100
101    assert_eq!(client.matchmaker_token, client2.matchmaker_token);
102    println!("Both clients found a match in Europe");
103}
More examples
Hide additional examples
examples/register.rs (line 9)
4fn main() {
5    let mut client = ApiClient::new("defaultkey", "127.0.0.1", config::DEFAULT_PORT, "http");
6    // Note that the minimum password length is 8 characters!
7    client.register("some@user.com", "password", "Username");
8
9    while client.in_progress() {
10        client.tick()
11    }
12
13    println!("Username: {:?}", client.username());
14}
examples/leaderboard.rs (line 11)
6fn main() {
7    let mut client = ApiClient::new("defaultkey", "127.0.0.1", config::DEFAULT_PORT, "http");
8
9    client.register("email@provider.com", "password", "Leader");
10
11    while client.in_progress() {
12        client.tick()
13    }
14
15    client.write_leaderboard_record("wins", 1);
16
17    while client.in_progress() {
18        client.tick()
19    }
20
21    client.list_leaderboard_records("wins");
22
23    while client.in_progress() {
24        client.tick()
25    }
26
27    if let Some(leaderboard) = client.leaderboard_records("wins") {
28        for record in &leaderboard.records {
29            println!("{:?}", record);
30        }
31    }
32}
Source

pub fn authenticate(&mut self, email: &str, password: &str)

Source

pub fn register(&mut self, email: &str, password: &str, username: &str)

Examples found in repository?
examples/register.rs (line 7)
4fn main() {
5    let mut client = ApiClient::new("defaultkey", "127.0.0.1", config::DEFAULT_PORT, "http");
6    // Note that the minimum password length is 8 characters!
7    client.register("some@user.com", "password", "Username");
8
9    while client.in_progress() {
10        client.tick()
11    }
12
13    println!("Username: {:?}", client.username());
14}
More examples
Hide additional examples
examples/leaderboard.rs (line 9)
6fn main() {
7    let mut client = ApiClient::new("defaultkey", "127.0.0.1", config::DEFAULT_PORT, "http");
8
9    client.register("email@provider.com", "password", "Leader");
10
11    while client.in_progress() {
12        client.tick()
13    }
14
15    client.write_leaderboard_record("wins", 1);
16
17    while client.in_progress() {
18        client.tick()
19    }
20
21    client.list_leaderboard_records("wins");
22
23    while client.in_progress() {
24        client.tick()
25    }
26
27    if let Some(leaderboard) = client.leaderboard_records("wins") {
28        for record in &leaderboard.records {
29            println!("{:?}", record);
30        }
31    }
32}
examples/matchmaker.rs (line 26)
20fn match_rank_range() {
21    let mut clients: Vec<ApiClient> = (1..5).map(|_| {
22        ApiClient::new("defaultkey", "127.0.0.1", config::DEFAULT_PORT, "http")
23    }).collect();
24
25    clients.iter_mut().enumerate().for_each(|(i, client)| {
26        client.register(&format!("email{}@provider.com", i), "password", &format!("Player{}", i));
27    });
28
29    tick_clients(&mut clients);
30
31    println!("Client 1 with rank 5 searching for a match with rank between 3 and 7");
32    let mut matchmaker = Matchmaker::new();
33    matchmaker
34        .add_numeric_property("rank", 5.0)
35        .add_query_item(&QueryItemBuilder::new("rank").geq(3).required().build())
36        .add_query_item(&QueryItemBuilder::new("rank").leq(7).required().build());
37
38    println!("Client 2 with rank 4 searching for a match with rank between 2 and 6");
39    let mut matchmaker2 = Matchmaker::new();
40    matchmaker2
41        .add_numeric_property("rank", 4.0)
42        .add_query_item(&QueryItemBuilder::new("rank").geq(2).required().build())
43        .add_query_item(&QueryItemBuilder::new("rank").leq(6).required().build());
44
45    println!("Client 3 with rank 10 searching for a match with rank between 8 and 12");
46    let mut matchmaker3 = Matchmaker::new();
47    matchmaker3
48        .add_numeric_property("rank", 10.0)
49        .add_query_item(&QueryItemBuilder::new("rank").geq(8).required().build())
50        .add_query_item(&QueryItemBuilder::new("rank").leq(12).required().build());
51
52    println!("Client 4 with rank 9 searching for a match with rank between 7 and 11");
53    let mut matchmaker4 = Matchmaker::new();
54    matchmaker4
55        .add_numeric_property("rank", 9.0)
56        .add_query_item(&QueryItemBuilder::new("rank").geq(7).required().build())
57        .add_query_item(&QueryItemBuilder::new("rank").leq(11).required().build());
58
59    clients[0].socket_add_matchmaker(&matchmaker);
60    clients[1].socket_add_matchmaker(&matchmaker2);
61    clients[2].socket_add_matchmaker(&matchmaker3);
62    clients[3].socket_add_matchmaker(&matchmaker4);
63
64    wait_tokens(&mut clients) ;
65
66    assert_eq!(clients[0].matchmaker_token, clients[1].matchmaker_token);
67    println!("Successfully ranked the clients with rank 4 and 5");
68    assert_eq!(clients[2].matchmaker_token, clients[3].matchmaker_token);
69    println!("Successfully ranked the clients with rank 9 and 10");
70}
71
72fn match_region() {
73    let mut client = ApiClient::new("defaultkey", "127.0.0.1", config::DEFAULT_PORT, "http");
74    let mut client2 = ApiClient::new("defaultkey", "127.0.0.1", config::DEFAULT_PORT, "http");
75
76    client.register("email1@provider.com", "password", "Player1");
77    client2.register("email2@provider.com", "password", "Player2");
78
79    while client.in_progress() {
80        client.tick()
81    }
82
83    while client2.in_progress() {
84        client2.tick()
85    }
86
87    println!("Both clients Searching for a match in Europe");
88    let mut matchmaker = Matchmaker::new();
89    matchmaker
90        .add_string_property("region", "Europe")
91        .add_query_item(&QueryItemBuilder::new("region").term("Europe").required().build());
92
93    client.socket_add_matchmaker(&matchmaker);
94    client2.socket_add_matchmaker(&matchmaker);
95
96    while client.matchmaker_token.is_none() || client2.matchmaker_token.is_none() {
97        client.tick();
98        client2.tick();
99    }
100
101    assert_eq!(client.matchmaker_token, client2.matchmaker_token);
102    println!("Both clients found a match in Europe");
103}
Source

pub fn username(&self) -> Option<String>

Examples found in repository?
examples/register.rs (line 13)
4fn main() {
5    let mut client = ApiClient::new("defaultkey", "127.0.0.1", config::DEFAULT_PORT, "http");
6    // Note that the minimum password length is 8 characters!
7    client.register("some@user.com", "password", "Username");
8
9    while client.in_progress() {
10        client.tick()
11    }
12
13    println!("Username: {:?}", client.username());
14}
Source

pub fn rpc(&mut self, name: &str, body: &str)

Source

pub fn logout(&mut self)

Source

pub fn authenticated(&self) -> bool

Source

pub fn write_leaderboard_record(&mut self, leaderboard_id: &str, score: i32)

Examples found in repository?
examples/leaderboard.rs (line 15)
6fn main() {
7    let mut client = ApiClient::new("defaultkey", "127.0.0.1", config::DEFAULT_PORT, "http");
8
9    client.register("email@provider.com", "password", "Leader");
10
11    while client.in_progress() {
12        client.tick()
13    }
14
15    client.write_leaderboard_record("wins", 1);
16
17    while client.in_progress() {
18        client.tick()
19    }
20
21    client.list_leaderboard_records("wins");
22
23    while client.in_progress() {
24        client.tick()
25    }
26
27    if let Some(leaderboard) = client.leaderboard_records("wins") {
28        for record in &leaderboard.records {
29            println!("{:?}", record);
30        }
31    }
32}
Source

pub fn list_leaderboard_records(&mut self, leaderboard_id: &str)

Examples found in repository?
examples/leaderboard.rs (line 21)
6fn main() {
7    let mut client = ApiClient::new("defaultkey", "127.0.0.1", config::DEFAULT_PORT, "http");
8
9    client.register("email@provider.com", "password", "Leader");
10
11    while client.in_progress() {
12        client.tick()
13    }
14
15    client.write_leaderboard_record("wins", 1);
16
17    while client.in_progress() {
18        client.tick()
19    }
20
21    client.list_leaderboard_records("wins");
22
23    while client.in_progress() {
24        client.tick()
25    }
26
27    if let Some(leaderboard) = client.leaderboard_records("wins") {
28        for record in &leaderboard.records {
29            println!("{:?}", record);
30        }
31    }
32}
Source

pub fn leaderboard_records( &self, leaderboard_id: &str, ) -> Option<Rc<ApiLeaderboardRecordList>>

Examples found in repository?
examples/leaderboard.rs (line 27)
6fn main() {
7    let mut client = ApiClient::new("defaultkey", "127.0.0.1", config::DEFAULT_PORT, "http");
8
9    client.register("email@provider.com", "password", "Leader");
10
11    while client.in_progress() {
12        client.tick()
13    }
14
15    client.write_leaderboard_record("wins", 1);
16
17    while client.in_progress() {
18        client.tick()
19    }
20
21    client.list_leaderboard_records("wins");
22
23    while client.in_progress() {
24        client.tick()
25    }
26
27    if let Some(leaderboard) = client.leaderboard_records("wins") {
28        for record in &leaderboard.records {
29            println!("{:?}", record);
30        }
31    }
32}
Source

pub fn try_recv(&mut self) -> Option<Event>

Source

pub fn tick(&mut self)

Examples found in repository?
examples/matchmaker.rs (line 7)
4fn tick_clients(clients: &mut Vec<ApiClient>) {
5    while clients.iter().any(|client| client.in_progress()) {
6       clients.iter_mut().for_each(|client| {
7           client.tick();
8       })
9    }
10}
11
12fn wait_tokens(clients: &mut Vec<ApiClient>) {
13    while clients.iter().any(|client| client.matchmaker_token.is_none()) {
14        clients.iter_mut().for_each(|client| {
15            client.tick();
16        })
17    }
18}
19
20fn match_rank_range() {
21    let mut clients: Vec<ApiClient> = (1..5).map(|_| {
22        ApiClient::new("defaultkey", "127.0.0.1", config::DEFAULT_PORT, "http")
23    }).collect();
24
25    clients.iter_mut().enumerate().for_each(|(i, client)| {
26        client.register(&format!("email{}@provider.com", i), "password", &format!("Player{}", i));
27    });
28
29    tick_clients(&mut clients);
30
31    println!("Client 1 with rank 5 searching for a match with rank between 3 and 7");
32    let mut matchmaker = Matchmaker::new();
33    matchmaker
34        .add_numeric_property("rank", 5.0)
35        .add_query_item(&QueryItemBuilder::new("rank").geq(3).required().build())
36        .add_query_item(&QueryItemBuilder::new("rank").leq(7).required().build());
37
38    println!("Client 2 with rank 4 searching for a match with rank between 2 and 6");
39    let mut matchmaker2 = Matchmaker::new();
40    matchmaker2
41        .add_numeric_property("rank", 4.0)
42        .add_query_item(&QueryItemBuilder::new("rank").geq(2).required().build())
43        .add_query_item(&QueryItemBuilder::new("rank").leq(6).required().build());
44
45    println!("Client 3 with rank 10 searching for a match with rank between 8 and 12");
46    let mut matchmaker3 = Matchmaker::new();
47    matchmaker3
48        .add_numeric_property("rank", 10.0)
49        .add_query_item(&QueryItemBuilder::new("rank").geq(8).required().build())
50        .add_query_item(&QueryItemBuilder::new("rank").leq(12).required().build());
51
52    println!("Client 4 with rank 9 searching for a match with rank between 7 and 11");
53    let mut matchmaker4 = Matchmaker::new();
54    matchmaker4
55        .add_numeric_property("rank", 9.0)
56        .add_query_item(&QueryItemBuilder::new("rank").geq(7).required().build())
57        .add_query_item(&QueryItemBuilder::new("rank").leq(11).required().build());
58
59    clients[0].socket_add_matchmaker(&matchmaker);
60    clients[1].socket_add_matchmaker(&matchmaker2);
61    clients[2].socket_add_matchmaker(&matchmaker3);
62    clients[3].socket_add_matchmaker(&matchmaker4);
63
64    wait_tokens(&mut clients) ;
65
66    assert_eq!(clients[0].matchmaker_token, clients[1].matchmaker_token);
67    println!("Successfully ranked the clients with rank 4 and 5");
68    assert_eq!(clients[2].matchmaker_token, clients[3].matchmaker_token);
69    println!("Successfully ranked the clients with rank 9 and 10");
70}
71
72fn match_region() {
73    let mut client = ApiClient::new("defaultkey", "127.0.0.1", config::DEFAULT_PORT, "http");
74    let mut client2 = ApiClient::new("defaultkey", "127.0.0.1", config::DEFAULT_PORT, "http");
75
76    client.register("email1@provider.com", "password", "Player1");
77    client2.register("email2@provider.com", "password", "Player2");
78
79    while client.in_progress() {
80        client.tick()
81    }
82
83    while client2.in_progress() {
84        client2.tick()
85    }
86
87    println!("Both clients Searching for a match in Europe");
88    let mut matchmaker = Matchmaker::new();
89    matchmaker
90        .add_string_property("region", "Europe")
91        .add_query_item(&QueryItemBuilder::new("region").term("Europe").required().build());
92
93    client.socket_add_matchmaker(&matchmaker);
94    client2.socket_add_matchmaker(&matchmaker);
95
96    while client.matchmaker_token.is_none() || client2.matchmaker_token.is_none() {
97        client.tick();
98        client2.tick();
99    }
100
101    assert_eq!(client.matchmaker_token, client2.matchmaker_token);
102    println!("Both clients found a match in Europe");
103}
More examples
Hide additional examples
examples/register.rs (line 10)
4fn main() {
5    let mut client = ApiClient::new("defaultkey", "127.0.0.1", config::DEFAULT_PORT, "http");
6    // Note that the minimum password length is 8 characters!
7    client.register("some@user.com", "password", "Username");
8
9    while client.in_progress() {
10        client.tick()
11    }
12
13    println!("Username: {:?}", client.username());
14}
examples/leaderboard.rs (line 12)
6fn main() {
7    let mut client = ApiClient::new("defaultkey", "127.0.0.1", config::DEFAULT_PORT, "http");
8
9    client.register("email@provider.com", "password", "Leader");
10
11    while client.in_progress() {
12        client.tick()
13    }
14
15    client.write_leaderboard_record("wins", 1);
16
17    while client.in_progress() {
18        client.tick()
19    }
20
21    client.list_leaderboard_records("wins");
22
23    while client.in_progress() {
24        client.tick()
25    }
26
27    if let Some(leaderboard) = client.leaderboard_records("wins") {
28        for record in &leaderboard.records {
29            println!("{:?}", record);
30        }
31    }
32}
Source

pub fn match_id(&self) -> Option<String>

Source

pub fn rpc_response(&self) -> Option<String>

Source

pub fn socket_add_matchmaker(&mut self, matchmaker: &Matchmaker)

Examples found in repository?
examples/matchmaker.rs (line 59)
20fn match_rank_range() {
21    let mut clients: Vec<ApiClient> = (1..5).map(|_| {
22        ApiClient::new("defaultkey", "127.0.0.1", config::DEFAULT_PORT, "http")
23    }).collect();
24
25    clients.iter_mut().enumerate().for_each(|(i, client)| {
26        client.register(&format!("email{}@provider.com", i), "password", &format!("Player{}", i));
27    });
28
29    tick_clients(&mut clients);
30
31    println!("Client 1 with rank 5 searching for a match with rank between 3 and 7");
32    let mut matchmaker = Matchmaker::new();
33    matchmaker
34        .add_numeric_property("rank", 5.0)
35        .add_query_item(&QueryItemBuilder::new("rank").geq(3).required().build())
36        .add_query_item(&QueryItemBuilder::new("rank").leq(7).required().build());
37
38    println!("Client 2 with rank 4 searching for a match with rank between 2 and 6");
39    let mut matchmaker2 = Matchmaker::new();
40    matchmaker2
41        .add_numeric_property("rank", 4.0)
42        .add_query_item(&QueryItemBuilder::new("rank").geq(2).required().build())
43        .add_query_item(&QueryItemBuilder::new("rank").leq(6).required().build());
44
45    println!("Client 3 with rank 10 searching for a match with rank between 8 and 12");
46    let mut matchmaker3 = Matchmaker::new();
47    matchmaker3
48        .add_numeric_property("rank", 10.0)
49        .add_query_item(&QueryItemBuilder::new("rank").geq(8).required().build())
50        .add_query_item(&QueryItemBuilder::new("rank").leq(12).required().build());
51
52    println!("Client 4 with rank 9 searching for a match with rank between 7 and 11");
53    let mut matchmaker4 = Matchmaker::new();
54    matchmaker4
55        .add_numeric_property("rank", 9.0)
56        .add_query_item(&QueryItemBuilder::new("rank").geq(7).required().build())
57        .add_query_item(&QueryItemBuilder::new("rank").leq(11).required().build());
58
59    clients[0].socket_add_matchmaker(&matchmaker);
60    clients[1].socket_add_matchmaker(&matchmaker2);
61    clients[2].socket_add_matchmaker(&matchmaker3);
62    clients[3].socket_add_matchmaker(&matchmaker4);
63
64    wait_tokens(&mut clients) ;
65
66    assert_eq!(clients[0].matchmaker_token, clients[1].matchmaker_token);
67    println!("Successfully ranked the clients with rank 4 and 5");
68    assert_eq!(clients[2].matchmaker_token, clients[3].matchmaker_token);
69    println!("Successfully ranked the clients with rank 9 and 10");
70}
71
72fn match_region() {
73    let mut client = ApiClient::new("defaultkey", "127.0.0.1", config::DEFAULT_PORT, "http");
74    let mut client2 = ApiClient::new("defaultkey", "127.0.0.1", config::DEFAULT_PORT, "http");
75
76    client.register("email1@provider.com", "password", "Player1");
77    client2.register("email2@provider.com", "password", "Player2");
78
79    while client.in_progress() {
80        client.tick()
81    }
82
83    while client2.in_progress() {
84        client2.tick()
85    }
86
87    println!("Both clients Searching for a match in Europe");
88    let mut matchmaker = Matchmaker::new();
89    matchmaker
90        .add_string_property("region", "Europe")
91        .add_query_item(&QueryItemBuilder::new("region").term("Europe").required().build());
92
93    client.socket_add_matchmaker(&matchmaker);
94    client2.socket_add_matchmaker(&matchmaker);
95
96    while client.matchmaker_token.is_none() || client2.matchmaker_token.is_none() {
97        client.tick();
98        client2.tick();
99    }
100
101    assert_eq!(client.matchmaker_token, client2.matchmaker_token);
102    println!("Both clients found a match in Europe");
103}
Source

pub fn socket_create_match(&mut self) -> u32

Source

pub fn socket_join_match_by_id(&mut self, match_id: &str) -> u32

Source

pub fn socket_join_match_by_token(&mut self, token_id: &str) -> u32

Source

pub fn socket_leave_match(&mut self) -> u32

Source

pub fn socket_send<T: SerBin>(&mut self, opcode: i32, data: &T)

Source

pub fn socket_response(&self, cid: u32) -> Option<SocketEvent>

Source

pub fn error(&self) -> Option<String>

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> ErasedDestructor for T
where T: 'static,