コード例 #1
0
        internal Task JoinLobby(DiscordLobby discordLobby)
        {
            if (_connectedLobbies.ContainsKey((long)discordLobby.Id))
            {
                throw new InvalidOperationException("[Discord] Already connected to lobby.");
            }
            Assert.IsNotNull(discordLobby);
            var future = new TaskCompletionSource <object>();

            _lobbyManager.ConnectLobby((long)discordLobby.Id, discordLobby.Secret,
                                       (DiscordApp.Result result, ref DiscordApp.Lobby lobby) => {
                if (result != DiscordApp.Result.Ok)
                {
                    future.SetException(DiscordUtility.ToError(result));
                    return;
                }
                discordLobby._data = lobby;
                _connectedLobbies.Add(lobby.Id, discordLobby);
                _lobbyManager.ConnectNetwork(lobby.Id);
                _lobbyManager.OpenNetworkChannel(lobby.Id, (byte)Reliability.Reliable, true);
                _lobbyManager.OpenNetworkChannel(lobby.Id, (byte)Reliability.Unreliable, false);
                discordLobby.Members.Refresh();
                future.SetResult(null);
            });
            return(future.Task);
        }
コード例 #2
0
        internal void LeaveLobby(DiscordLobby lobby)
        {
            Assert.IsNotNull(lobby);
            var id = (long)lobby.Id;

            if (!_connectedLobbies.ContainsKey(id))
            {
                throw new InvalidOperationException($"Not connected to lobby: {id}");
            }
            _connectedLobbies.Remove(id);
            _lobbyManager.DisconnectNetwork(id);
            _lobbyManager.DisconnectLobby(id, DiscordUtility.LogIfError);
            lobby.Dispose();
        }
コード例 #3
0
        public Task <Lobby> CreateLobby(LobbyCreateParams createParams)
        {
            var txn = _lobbyManager.GetLobbyCreateTransaction();

            txn.SetCapacity(createParams.Capacity);
            txn.SetType((DiscordApp.LobbyType)createParams.Type);
            if (createParams.Metadata != null)
            {
                foreach (var kvp in createParams.Metadata)
                {
                    if (kvp.Key == null || kvp.Value == null)
                    {
                        continue;
                    }
                    txn.SetMetadata(kvp.Key, kvp.Value.ToString());
                }
            }
            var future = new TaskCompletionSource <Lobby>();

            _lobbyManager.CreateLobby(txn, (DiscordApp.Result result, ref DiscordApp.Lobby lobby) => {
                if (result != DiscordApp.Result.Ok)
                {
                    future.SetException(DiscordUtility.ToError(result));
                    return;
                }
                SetupLobbyChannels(lobby.Id);
                var outputLobby = new DiscordLobby(this, lobby);
                _connectedLobbies.Add(lobby.Id, outputLobby);
                var secret   = _lobbyManager.GetLobbyActivitySecret(lobby.Id);
                var activity = new DiscordApp.Activity {
                    Party =
                    {
                        Id   = lobby.Id.ToString(),
                        Size =
                        {
                            CurrentSize =                   1,
                            MaxSize     = (int)createParams.Capacity
                        }
                    },
                    Secrets = { Join = secret }
                };
                _activityManager.UpdateActivity(activity, res => {});
                future.SetResult(outputLobby);
            });
            return(future.Task);
        }
コード例 #4
0
        public Task <IList <Lobby> > SearchLobbies(Action <ILobbySearchBuilder> builder)
        {
            var queryBuilder = new LobbySearchBuilder(_lobbyManager);

            builder?.Invoke(queryBuilder);
            var future = new TaskCompletionSource <IList <Lobby> >();

            _lobbyManager.Search(queryBuilder.Build(), (result) => {
                if (result != DiscordApp.Result.Ok)
                {
                    future.SetException(DiscordUtility.ToError(result));
                    return;
                }
                var count   = _lobbyManager.LobbyCount();
                var results = new Lobby[count];
                for (var i = 0; i < count; i++)
                {
                    var lobby  = _lobbyManager.GetLobby(_lobbyManager.GetLobbyId(i));
                    results[i] = new DiscordLobby(this, lobby);
                }
                future.SetResult(results);
            });
            return(future.Task);
        }