matchmaker/
matchmaker.rs

1use nakama_rs::{api_client::ApiClient, config};
2use nakama_rs::matchmaker::{Matchmaker, QueryItemBuilder};
3
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}
104
105fn main() {
106    match_rank_range();
107    match_region();
108}