internal void SetMemberMetadata(AccountHandle handle, string key, string value)
 {
     if (_metadata.SetMemberMetadata(handle, key, value))
     {
         DispatchMemberUpdate(handle);
     }
 }
 internal void DeleteMemberMetadata(AccountHandle handle, string key)
 {
     if (_metadata.DeleteMemberMetadata(handle, key))
     {
         DispatchMemberUpdate(handle);
     }
 }
 /// <summary>
 /// Initializes a lobby member's metadata.
 /// </summary>
 /// <param name="handle">the account handle of the lobby member.</param>
 public void AddMember(AccountHandle handle)
 {
     if (_memberMetadata.ContainsKey(handle))
     {
         return;
     }
     _memberMetadata.Add(handle, new Dictionary <string, string>());
 }
 /// <summary>
 /// Gets a user from the member map by their ID.
 /// </summary>
 /// <param name="handle">the user account handle.</param>
 /// <returns>the LobbyMember with the ID, or null if they are not present.</returns>
 public LobbyMember Get(AccountHandle handle)
 {
     if (TryGetValue(handle, out LobbyMember owner))
     {
         return(owner);
     }
     return(default(LobbyMember));
 }
 /// <summary>
 /// Creates a view of the lobby, as seen from a remote player.
 ///
 /// This does not connect the view to the lobby. LobbyView.Join must be called
 /// to "connect" the view to to the lobby.
 /// </summary>
 /// <param name="id">the user to create the view from</param>
 /// <returns>the Lobby view from the view of that player</returns>
 public LocalLobbyView CreateView(AccountHandle id)
 {
     if (_connectedViews.TryGetValue(id, out LocalLobbyView view))
     {
         return(view);
     }
     return(new LocalLobbyView(id, this));
 }
 void DispatchMemberUpdate(AccountHandle handle)
 {
     foreach (var view in _connectedViews.Values)
     {
         if (view.Members.TryGetValue(handle, out LobbyMember member))
         {
             member.DispatchUpdate();
         }
     }
 }
 internal void SendLobbyMessage(AccountHandle source, ReadOnlySpan <byte> msg)
 {
     foreach (var view in _connectedViews.Values)
     {
         if (view.Members.TryGetValue(source, out LobbyMember member))
         {
             view.DispatchLobbyMessage(member, msg);
         }
     }
 }
 internal void SendLobbyMessage(AccountHandle source, string msg)
 {
     foreach (var view in _connectedViews.Values)
     {
         if (view.Members.TryGetValue(source, out LobbyMember member))
         {
             view.DispatchLobbyMessage(member, Encoding.UTF8.GetBytes(msg));
         }
     }
 }
 internal bool Remove(AccountHandle handle)
 {
     if (_members.TryGetValue(handle, out LobbyMember member))
     {
         _members.Remove(handle);
         OnMemberLeave?.Invoke(member);
         member.Dispose();
         return(true);
     }
     return(false);
 }
 internal LobbyMember Add(AccountHandle handle)
 {
     if (!_members.TryGetValue(handle, out LobbyMember player))
     {
         player = new LobbyMember(_lobby, handle);
         _members.Add(handle, player);
         OnMemberJoin?.Invoke(player);
         return(player);
     }
     throw new Exception("Cannot add a member that already exists in the lobby.");
 }
        /// <summary>
        /// Delete the member level metadata  for a given key.
        ///
        /// Note: this does not do any permissions checking.
        /// </summary>
        /// <param name="key">the metadata key to delete</param>
        /// <returns>true if the value was deleted, false otherwise.</returns>
        public bool DeleteMemberMetadata(AccountHandle handle, string key)
        {
            Dictionary <string, string> memberMetadata;

            if (_memberMetadata.TryGetValue(handle, out memberMetadata))
            {
                bool changed = memberMetadata.ContainsKey(key);
                memberMetadata.Remove(key);
                return(changed);
            }
            return(false);
        }
        internal LobbyMember GetOrAdd(AccountHandle handle)
        {
            LobbyMember player;

            if (!_members.TryGetValue(handle, out player))
            {
                player = new LobbyMember(_lobby, handle);
                _members.Add(handle, player);
                OnMemberJoin?.Invoke(player);
            }
            return(player);
        }
        /// <summary>
        /// Gets a member level metadata value for a key.
        /// </summary>
        /// <param name="handle">the account handle of the lobby member.</param>
        /// <returns>the metadata value for the key, empty string if no value for the key is present</returns>
        public string GetMemberMetadata(AccountHandle handle, string key)
        {
            Dictionary <string, string> memberMetadata;
            string value;

            if (!_memberMetadata.TryGetValue(handle, out memberMetadata) ||
                !memberMetadata.TryGetValue(key, out value))
            {
                return(string.Empty);
            }
            return(value);
        }
        internal void Disconnect(LocalLobbyView view)
        {
            var handle = new AccountHandle(view.UserId);

            if (!_connectedViews.ContainsKey(handle))
            {
                return;
            }
            _metadata.RemoveMember(handle);
            _connectedViews.Remove(handle);
            foreach (var connectedView in _connectedViews.Values)
            {
                connectedView.Members.Remove(handle);
            }
        }
        /// <summary>
        /// Set the member level metadata for a given key.
        ///
        /// Note: this does not do any permissions checking.
        /// </summary>
        /// <param name="handle">the account handle of the lobby member.</param>
        /// <param name="key">the metadata key to set</param>
        /// <param name="value">the metadata value to set</param>
        /// <returns>true if the value has changed, false otherwise.</returns>
        public bool SetMemberMetadata(AccountHandle handle, string key, string value)
        {
            Dictionary <string, string> memberMetadata;

            if (_memberMetadata.TryGetValue(handle, out memberMetadata))
            {
                bool changed = true;
                if (memberMetadata.TryGetValue(key, out string currentValue))
                {
                    changed = currentValue == value;
                }
                memberMetadata[key] = value;
                return(changed);
            }
            return(false);
        }
        internal void SendNetworkMessage(AccountHandle source, AccountHandle target, ReadOnlySpan <byte> msg, Reliability reliability)
        {
            // Simulate unreliablity
            if (reliability == Reliability.Unreliable && _packetLossRng.NextDouble() > _packetLossPercent)
            {
                return;
            }
            LocalLobbyView view;
            LobbyMember    member;

            if (!_connectedViews.TryGetValue(target, out view) ||
                !view.Members.TryGetValue(source, out member))
            {
                return;
            }
            member.DispatchNetworkMessage(msg);
        }
        internal void Connect(LocalLobbyView view)
        {
            if (_connectedViews.Count >= Capacity)
            {
                throw new InvalidOperationException("Cannot join a lobby that is already full");
            }
            var handle = new AccountHandle(view.UserId);

            if (_connectedViews.ContainsKey(handle))
            {
                return;
            }
            _metadata.AddMember(handle);
            _connectedViews[handle] = view;
            foreach (var connectedView in _connectedViews.Values)
            {
                connectedView.Members.Add(handle);
            }
        }
 /// <summary>
 /// Checks if the a user is in the lobby.
 /// </summary>
 /// <param name="handle">the user account handle.</param>
 /// <returns>true if the member is in the lobby, false otherwise.</returns>
 public bool Contains(AccountHandle handle) => Get(handle) == default(LobbyMember);
        // Read-only accessors are public

        /// <summary>
        /// Attempts to get the member
        /// </summary>
        /// <param name="handle">the user account handle.</param>
        /// <param name="member"></param>
        /// <returns></returns>
        public bool TryGetValue(AccountHandle handle, out LobbyMember member) =>
        _members.TryGetValue(handle, out member);
Beispiel #20
0
 internal LobbyMember(Lobby lobby, AccountHandle userId)
 {
     Id    = userId;
     Lobby = lobby;
     State = ConnectionState.Connected;
 }
 /// <summary>
 /// Removes a lobby member's metadata.
 /// </summary>
 /// <param name="handle">the account handle of the lobby member.</param>
 public void RemoveMember(AccountHandle handle) => _memberMetadata.Remove(handle);
 internal string GetMemberMetadata(AccountHandle handle, string key) =>
 _metadata.GetMemberMetadata(handle, key);