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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
//! Provides types and functionality for [Lobbies](https://discord.com/developers/docs/game-sdk/lobbies)

pub mod events;
pub mod search;
pub mod state;

use crate::{types::Snowflake, user::UserId, Command, CommandKind, Error};
use serde::{Deserialize, Serialize};
use serde_repr::{Deserialize_repr, Serialize_repr};

pub type Metadata = std::collections::BTreeMap<String, String>;
pub type LobbyId = Snowflake;

#[derive(Serialize, Deserialize, PartialEq, Debug, Copy, Clone)]
#[serde(rename_all = "kebab-case")]
pub enum Region {
    Amsterdam,
    Brazil,
    Dubai,
    EuCentral,
    EuWest,
    Europe,
    Frankfurt,
    Hongkong,
    India,
    Japan,
    London,
    Russia,
    Singapore,
    Southafrica,
    SouthKorea,
    Stockholm,
    Sydney,
    UsCentral,
    UsEast,
    UsSouth,
    UsWest,
    VipAmsterdam,
    VipUsEast,
    VipUsWest,
    // This isn't in the list returned by /voice/regions but...
    StPete,
}

#[derive(Copy, Clone, Debug, Serialize_repr, Deserialize_repr)]
#[repr(u8)]
pub enum LobbyKind {
    Private = 1,
    Public = 2,
}

/// The voice states that can be attached to each lobby member
#[derive(Deserialize, Debug, Clone)]
#[cfg_attr(test, derive(serde::Serialize))]
pub struct VoiceState {
    pub channel_id: crate::types::ChannelId,
    pub deaf: bool,
    pub mute: bool,
    pub self_deaf: bool,
    pub self_mute: bool,
    pub self_video: bool,
    pub session_id: String,
    pub suppress: bool,
    pub user_id: UserId,
}

#[derive(Deserialize, Debug, Clone)]
#[cfg_attr(test, derive(serde::Serialize))]
pub struct Lobby {
    /// The unique identifier for the lobby.
    pub id: LobbyId,
    /// The maximum number of users that can join the lobby.
    pub capacity: u32,
    /// Whether new members can join the lobby.
    pub locked: bool,
    /// The users and attached metadata that are actually present in the lobby.
    /// This list will be empty if this lobby is deserialized from a
    /// [`LobbyUpdate` event](crate::Event::LobbyUpdate) as that event only
    /// fires for metadata changes on the lobby itself, not its members.
    #[serde(default)]
    pub members: Vec<LobbyMember>,
    /// A set of key value pairs to add arbitrary metadata to the lobby.
    pub metadata: Metadata,
    /// The id of the user who owns this lobby.
    pub owner_id: UserId,
    /// The Discord region that the lobby is located in.
    pub region: Region,
    /// The secret required for other users to be able to join this lobby. This
    /// is autogenerated by Discord itself, unlike activity secrets.
    pub secret: String,
    /// Whether the lobby is public or private.
    #[serde(rename = "type")]
    pub kind: LobbyKind,
    #[serde(default)]
    pub voice_states: Vec<VoiceState>,
}

#[derive(Deserialize, Debug, Clone)]
#[cfg_attr(test, derive(serde::Serialize))]
pub struct LobbyMember {
    pub metadata: Metadata,
    pub user: crate::user::User,
    #[serde(skip)]
    pub speaking: bool,
}

/// Argument used to create or modify a [`Lobby`]
#[derive(Serialize, Clone)]
pub struct LobbyArgs {
    /// The id for a lobby, only set when modifying
    #[serde(skip_serializing_if = "Option::is_none")]
    id: Option<LobbyId>,
    /// The max capacity of the lobby
    capacity: u32,
    /// If the lobby is public or private
    #[serde(rename = "type")]
    kind: LobbyKind,
    /// Whether or not the lobby can be joined
    #[serde(skip_serializing_if = "Option::is_none")]
    locked: Option<bool>,
    /// The ID of the user to make the owner, only set when modifying
    #[serde(skip_serializing_if = "Option::is_none")]
    owner_id: Option<UserId>,
    #[serde(skip_serializing_if = "std::collections::BTreeMap::is_empty")]
    metadata: Metadata,
}

impl LobbyArgs {
    pub fn modify(self, lobby: &mut Lobby) {
        lobby.capacity = self.capacity;
        lobby.kind = self.kind;
        lobby.locked = self.locked.unwrap_or(false);
        if let Some(owner) = self.owner_id {
            lobby.owner_id = owner;
        }
        lobby.metadata = self.metadata;
    }
}

/// Supplies the same defaults as those that Discord (currently) sets if the any
/// of the arguments are not specified, to protect from behavior changes in
/// Discord in the future
impl Default for LobbyArgs {
    fn default() -> Self {
        Self {
            id: None,
            capacity: 16,
            kind: LobbyKind::Private,
            locked: None,
            owner_id: None,
            metadata: Default::default(),
        }
    }
}

#[derive(Default)]
pub struct CreateLobbyBuilder {
    inner: LobbyArgs,
}

impl CreateLobbyBuilder {
    pub fn new() -> Self {
        Self::default()
    }

    #[inline]
    pub fn capacity(mut self, capacity: Option<std::num::NonZeroU32>) -> Self {
        self.inner.capacity = capacity.map_or(16, |cap| cap.get());
        self
    }

    #[inline]
    pub fn kind(mut self, kind: LobbyKind) -> Self {
        self.inner.kind = kind;
        self
    }

    #[inline]
    pub fn locked(mut self, locked: bool) -> Self {
        self.inner.locked = Some(locked);
        self
    }

    #[inline]
    pub fn add_metadata(mut self, md: impl IntoIterator<Item = (String, String)>) -> Self {
        self.inner.metadata.extend(md);
        self
    }
}

pub struct UpdateLobbyBuilder {
    inner: LobbyArgs,
}

impl UpdateLobbyBuilder {
    pub fn new(to_update: &Lobby) -> Self {
        Self {
            inner: LobbyArgs {
                id: Some(to_update.id),
                capacity: to_update.capacity,
                kind: to_update.kind,
                locked: if to_update.locked { Some(true) } else { None },
                owner_id: Some(to_update.owner_id),
                metadata: to_update.metadata.clone(),
            },
        }
    }

    #[inline]
    pub fn capacity(mut self, capacity: Option<std::num::NonZeroU32>) -> Self {
        self.inner.capacity = capacity.map_or(16, |cap| cap.get());
        self
    }

    #[inline]
    pub fn kind(mut self, kind: LobbyKind) -> Self {
        self.inner.kind = kind;
        self
    }

    #[inline]
    pub fn locked(mut self, locked: bool) -> Self {
        self.inner.locked = Some(locked);
        self
    }

    #[inline]
    pub fn owner(mut self, owner: Option<UserId>) -> Self {
        self.inner.owner_id = owner;
        self
    }

    #[inline]
    pub fn add_metadata(mut self, md: impl IntoIterator<Item = (String, String)>) -> Self {
        self.inner.metadata.extend(md);
        self
    }

    #[inline]
    pub fn delete_metadata<'key>(mut self, to_remove: impl IntoIterator<Item = &'key str>) -> Self {
        for key in to_remove {
            self.inner.metadata.remove(key);
        }
        self
    }
}

#[derive(Serialize)]
pub struct ConnectLobby {
    pub id: LobbyId,
    pub secret: String,
}

impl<'s> std::convert::TryFrom<&'s str> for ConnectLobby {
    type Error = Error;

    fn try_from(s: &'s str) -> Result<Self, Self::Error> {
        s.find(':')
            .and_then(|sep| {
                let id = s[..sep].parse().ok()?;
                let secret = s[sep + 1..].to_owned();

                Some(Self { id, secret })
            })
            .ok_or(Error::NonCanonicalLobbyActivitySecret)
    }
}

/// A message sent by a user to a lobby
#[derive(Debug, PartialEq, Clone)]
pub enum LobbyMessage {
    Binary(Vec<u8>),
    Text(String),
}

impl LobbyMessage {
    pub fn text(text: impl Into<String>) -> Self {
        Self::Text(text.into())
    }

    pub fn binary(bin: impl Into<Vec<u8>>) -> Self {
        Self::Binary(bin.into())
    }
}

impl Serialize for LobbyMessage {
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
    where
        S: serde::Serializer,
    {
        match self {
            Self::Binary(bin) => {
                let mut data = String::from("data:text/plain;base64,");
                base64::encode_config_buf(&bin, base64::STANDARD_NO_PAD, &mut data);

                serializer.serialize_str(&data)
            }
            Self::Text(text) => serializer.serialize_str(text),
        }
    }
}

impl<'de> Deserialize<'de> for LobbyMessage {
    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
    where
        D: serde::Deserializer<'de>,
    {
        use serde::de;
        use std::fmt;

        struct Visitor;

        impl<'de> de::Visitor<'de> for Visitor {
            type Value = LobbyMessage;

            fn expecting(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
                formatter.write_str("a string")
            }

            fn visit_borrowed_str<E>(self, v: &'de str) -> Result<Self::Value, E>
            where
                E: de::Error,
            {
                Ok(match v.strip_prefix("data:text/plain;base64,") {
                    Some(encoded) => {
                        let bin = base64::decode_config(encoded, base64::STANDARD_NO_PAD)
                            .map_err(de::Error::custom)?;
                        LobbyMessage::Binary(bin)
                    }
                    None => LobbyMessage::Text(v.to_owned()),
                })
            }
        }

        deserializer.deserialize_str(Visitor)
    }
}

/// Used by different command types when performing an action on a specific lobby
#[derive(Serialize)]
struct LobbyAction {
    id: LobbyId,
}

impl crate::Discord {
    /// Creates a new [`Lobby`], automatically joining the current
    /// [`User`](crate::user::User) and making them the owner of the [`Lobby`].
    ///
    /// [API docs](https://discord.com/developers/docs/game-sdk/lobbies#createlobby)
    pub async fn create_lobby(&self, args: CreateLobbyBuilder) -> Result<Lobby, Error> {
        let rx = self.send_rpc(CommandKind::CreateLobby, args.inner)?;

        handle_response!(rx, Command::CreateLobby(lobby) => {
            Ok(lobby)
        })
    }

    /// Updates a lobby.
    ///
    /// # Errors
    ///
    /// This call has a rate limit of 10 updates per 5 seconds. If you fear you
    /// might hit that, it may be a good idea to batch your lobby updates into
    /// transactions.
    ///
    /// [API docs](https://discord.com/developers/docs/game-sdk/lobbies#updatelobby)
    pub async fn update_lobby(&self, args: UpdateLobbyBuilder) -> Result<LobbyArgs, Error> {
        // The response for the lobby update unfortunately doesn't return any
        // actual data for the lobby, so we store the new state and set it once
        // Discord responds to the update, but only the metadata pieces that can
        // be modified by the update, so no changes to members or their metadata
        let update = args.inner.clone();
        let rx = self.send_rpc(CommandKind::UpdateLobby, args.inner)?;

        handle_response!(rx, Command::UpdateLobby => {
            Ok(update)
        })
    }

    /// Deletes the specified lobby.
    ///
    /// [API docs](https://discord.com/developers/docs/game-sdk/lobbies#deletelobby)
    pub async fn delete_lobby(&self, id: LobbyId) -> Result<(), Error> {
        let rx = self.send_rpc(CommandKind::DeleteLobby, LobbyAction { id })?;

        handle_response!(rx, Command::DeleteLobby => {
            Ok(())
        })
    }

    /// Connects to the specified lobby, which comprises 2 pieces of information,
    /// the lobby identifier, and the lobby secret.
    ///
    /// [API docs](https://discord.com/developers/docs/game-sdk/lobbies#connectlobby)
    pub async fn connect_lobby(&self, lobby: ConnectLobby) -> Result<Lobby, Error> {
        let rx = self.send_rpc(CommandKind::ConnectToLobby, lobby)?;

        handle_response!(rx, Command::ConnectToLobby(lobby) => {
            Ok(lobby)
        })
    }

    /// Disconnects the current user from a lobby.
    ///
    /// [API docs](https://discord.com/developers/docs/game-sdk/lobbies#disconnectlobby)
    pub async fn disconnect_lobby(&self, id: LobbyId) -> Result<(), Error> {
        let rx = self.send_rpc(CommandKind::DisconnectFromLobby, LobbyAction { id })?;

        handle_response!(rx, Command::DisconnectFromLobby => {
            Ok(())
        })
    }

    /// Sends a message to the lobby on behalf of the current user. The
    ///
    /// # Errors
    ///
    /// You must be connected to the lobby you are messaging.
    /// This method has a rate limit of 10 messages per 5 seconds.
    ///
    /// [API docs](https://discord.com/developers/docs/game-sdk/lobbies#sendlobbymessage)
    pub async fn send_lobby_message(
        &self,
        lobby_id: LobbyId,
        data: LobbyMessage,
    ) -> Result<(), Error> {
        #[derive(Serialize)]
        struct SendToLobby {
            lobby_id: LobbyId,
            data: LobbyMessage,
        }

        let rx = self.send_rpc(CommandKind::SendToLobby, SendToLobby { lobby_id, data })?;

        handle_response!(rx, Command::SendToLobby => {
            Ok(())
        })
    }

    /// Connects to the voice channel of the specified lobby.
    ///
    /// # Errors
    ///
    /// The user must be connected to the specified lobby.
    ///
    /// [API docs](https://discord.com/developers/docs/game-sdk/lobbies#connectvoice)
    pub async fn connect_lobby_voice(&self, id: LobbyId) -> Result<(), Error> {
        let rx = self.send_rpc(CommandKind::ConnectToLobbyVoice, LobbyAction { id })?;

        handle_response!(rx, Command::ConnectToLobbyVoice => {
            Ok(())
        })
    }

    /// Disconnects from the voice channel of the specified lobby.
    ///
    /// # Errors
    ///
    /// The user must be connected to the specified lobby, and be connected to
    /// the voice channel already
    ///
    /// [API docs](https://discord.com/developers/docs/game-sdk/lobbies#disconnectvoice)
    pub async fn disconnect_lobby_voice(&self, id: LobbyId) -> Result<(), Error> {
        let rx = self.send_rpc(CommandKind::DisconnectFromLobbyVoice, LobbyAction { id })?;

        handle_response!(rx, Command::DisconnectFromLobbyVoice => {
            Ok(())
        })
    }

    /// Updates the metadata for the specified lobby member.
    ///
    /// [API docs](https://discord.com/developers/docs/game-sdk/lobbies#updatemember)
    pub async fn update_lobby_member(
        &self,
        lobby_id: LobbyId,
        user_id: UserId,
        metadata: Metadata,
    ) -> Result<(), Error> {
        #[derive(Serialize)]
        struct UpdateMember {
            lobby_id: LobbyId,
            user_id: UserId,
            metadata: Metadata,
        }

        let rx = self.send_rpc(
            CommandKind::UpdateLobbyMember,
            UpdateMember {
                lobby_id,
                user_id,
                metadata,
            },
        )?;

        handle_response!(rx, Command::UpdateLobbyMember => {
            Ok(())
        })
    }
}