Exemplo n.º 1
0
 internal void SetMemberMetadata(AccountHandle handle, string key, string value)
 {
     if (_metadata.SetMemberMetadata(handle, key, value))
     {
         DispatchMemberUpdate(handle);
     }
 }
Exemplo n.º 2
0
 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>());
 }
Exemplo n.º 4
0
 /// <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));
 }
Exemplo n.º 5
0
 /// <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));
 }
Exemplo n.º 6
0
 void DispatchMemberUpdate(AccountHandle handle)
 {
     foreach (var view in _connectedViews.Values)
     {
         if (view.Members.TryGetValue(handle, out LobbyMember member))
         {
             member.DispatchUpdate();
         }
     }
 }
Exemplo n.º 7
0
 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);
         }
     }
 }
Exemplo n.º 8
0
 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));
         }
     }
 }
Exemplo n.º 9
0
 internal bool Remove(AccountHandle handle)
 {
     if (_members.TryGetValue(handle, out LobbyMember member))
     {
         _members.Remove(handle);
         OnMemberLeave?.Invoke(member);
         member.Dispose();
         return(true);
     }
     return(false);
 }
Exemplo n.º 10
0
 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);
        }
Exemplo n.º 12
0
        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);
        }
Exemplo n.º 14
0
        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);
        }
Exemplo n.º 16
0
        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);
        }
Exemplo n.º 17
0
        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);
            }
        }
Exemplo n.º 18
0
 /// <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);
Exemplo n.º 19
0
        // 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);
Exemplo n.º 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);
Exemplo n.º 22
0
 internal string GetMemberMetadata(AccountHandle handle, string key) =>
 _metadata.GetMemberMetadata(handle, key);