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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
mod api_gen;

pub mod config;

pub mod api_client;

pub mod async_client;
pub mod matchmaker;

pub mod api {
    pub use super::api_gen::*;
}

pub mod rt_api {
    use nanoserde::DeJson;
    use quad_net::web_socket::WebSocket;

    #[derive(DeJson, Debug, Clone)]
    pub struct SocketEvent {
        /// Request/response ID.
        /// Request CID will match response CID.
        /// If event was not a response cid will be None.
        pub cid: Option<String>,
        pub match_presence_event: Option<MatchPresenceEvent>,
        pub match_data: Option<MatchData>,
        #[nserde(rename = "match")]
        pub new_match: Option<Match>,
        pub matchmaker_matched: Option<MatchmakerMatched>,
    }

    #[derive(DeJson, Debug, Clone)]
    pub struct Presence {
        pub user_id: String,
        pub session_id: String,
        pub username: String,
    }

    #[derive(DeJson, Debug, Clone)]
    pub struct MatchPresenceEvent {
        pub match_id: String,
        #[nserde(default)]
        pub joins: Vec<Presence>,
        #[nserde(default)]
        pub leaves: Vec<Presence>,
    }

    #[derive(DeJson, Debug, Clone)]
    pub struct MatchData {
        pub match_id: String,
        pub presence: Presence,
        #[nserde(default)]
        #[nserde(proxy = "Base64Encoder")]
        pub data: Vec<u8>,
        pub op_code: String,
        #[nserde(default)]
        pub reliable: bool,
    }

    #[derive(DeJson, Debug, Clone)]
    pub struct Match {
        pub match_id: String,
        #[nserde(default)]
        pub authoritative: bool,
        #[nserde(default)]
        pub label: String,
        #[nserde(rename = "self")]
        pub self_user: Presence,
        #[nserde(default)]
        pub presences: Vec<Presence>,
    }

    #[derive(DeJson, Debug, Clone)]
    pub struct MatchmakerMatched {
        pub ticket: String,
        pub token: String,
    }

    #[derive(DeJson, Clone, Debug)]
    #[nserde(transparent)]
    struct Base64Encoder(String);
    impl From<&Base64Encoder> for Vec<u8> {
        fn from(base64: &Base64Encoder) -> Vec<u8> {
            let mut buffer = Vec::<u8>::new();
            base64::decode_config_buf(&base64.0, base64::STANDARD, &mut buffer).unwrap();
            buffer
        }
    }

    pub struct Socket {
        web_socket: WebSocket,
        cid: u32,
    }

    impl Socket {
        pub fn connect(addr: &str, port: u32, appear_online: bool, token: &str) -> Socket {
            let ws_addr = format!(
                "{}:{}/ws?lang=en&status={}&token={}",
                addr, port, appear_online, token
            );

            Socket {
                web_socket: WebSocket::connect(&ws_addr).unwrap(),
                cid: 1,
            }
        }

        pub fn connected(&self) -> bool {
            self.web_socket.connected()
        }

        pub fn try_recv(&mut self) -> Option<String> {
            self.web_socket
                .try_recv()
                .map(|bytes| String::from_utf8(bytes).unwrap())
        }

        pub fn join_match_by_id(&mut self, match_id: &str) -> u32 {
            let id = self.cid;
            self.web_socket.send_text(&format!(
                r#"{{"match_join":{{"match_id":"{}"}},"cid":"{}"}}"#,
                match_id, id
            ));

            self.cid += 1;
            id
        }

        pub fn join_match_by_token(&mut self, token: &str) -> u32 {
            let id = self.cid;
            self.web_socket.send_text(&format!(
                r#"{{"match_join":{{"token":"{}"}},"cid":"{}"}}"#,
                token, id
            ));

            self.cid += 1;
            id
        }

        pub fn leave_match(&mut self, match_id: &str) -> u32 {
            let id = self.cid;
            self.web_socket.send_text(&format!(
                r#"{{"match_leave":{{"match_id":"{}"}},"cid":"{}"}}"#,
                match_id, id
            ));
            self.cid += 1;
            id
        }

        pub fn match_data_send(&mut self, match_id: &str, opcode: i32, data: &[u8]) {
            let mut buf = String::new();
            base64::encode_config_buf(data, base64::STANDARD, &mut buf);

            self.web_socket
                .send_text(&format!(
                    r#"{{"match_data_send":{{"match_id":"{}","op_code":"{}","data":"{}","presences":[]}}}}"#,
                    match_id, opcode, buf
                ));
        }

        /// usage example: `add_matchmaker(2, 4, "+properties.engine:\\\"macroquad_matchmaking\\\"", "{\"engine\":\"macroquad_matchmaking\"}");`
        pub fn add_matchmaker(
            &mut self,
            min_count: u32,
            max_count: u32,
            query: &str,
            string_properties: &str,
            numeric_properties: &str,
        ) -> u32 {
            let id = self.cid;
            let request = format!(
                r#"{{"matchmaker_add":{{"min_count":{},"max_count":{},"query":"{}","string_properties":{}, "numeric_properties":{}}},"cid":"{}"}}"#,
                min_count, max_count, query, string_properties, numeric_properties, id
            );

            self.web_socket.send_text(&request);
            self.cid += 1;
            id
        }

        pub fn create_match(&mut self) -> u32 {
            let id = self.cid;
            let request = format!(r#"{{"match_create":{{}},"cid":"{}"}}"#, id);
            self.web_socket.send_text(&request);
            self.cid += 1;
            id
        }
    }
}