Ejemplo n.º 1
0
        public void AddGroup(string name, int userId)
        {
            var chatGroupEntity    = _db.DbAccess.AddChatGroup(name);
            var chatGroupViewModel = new ChatGroupViewModel(chatGroupEntity);

            ChatGroups.Add(chatGroupViewModel);
            AddUserToGroup(chatGroupEntity.ChatGroupId, userId);
        }
Ejemplo n.º 2
0
        public void AddUserToGroup(int groupId, int appUserId)
        {
            var group = ChatGroups.FirstOrDefault(e => e.Entity.ChatGroupId == groupId);

            _db.DbAccess.EnterChatGroup(groupId, appUserId);
            group.Users.Add(appUserId);
            if (CollectionViews.ContainsKey(appUserId))
            {
                CollectionViews[appUserId].Refresh();
            }
        }
Ejemplo n.º 3
0
        public void AddMessage(ChatMessageEntity message)
        {
            var group             = ChatGroups.FirstOrDefault(e => e.Entity.ChatGroupId == message.IdChatGroup);
            var chatMessageEntity = _db.DbAccess.AddChatMessage(message);

            group.Messages.Add(new ChatMessageEntityViewModel()
            {
                Entity = chatMessageEntity,
                User   = _db.DbAccess.GetUsers().FirstOrDefault(e => e.AppUserId == message.IdUser)
            });
        }
Ejemplo n.º 4
0
 public ChatGroupEntity AddChatGroup(string name)
 {
     lock (ChatGroups)
     {
         ChatGroups.Add(new ChatGroupEntity()
         {
             Name = name
         });
         return(ChatGroups.Last());
     }
 }
Ejemplo n.º 5
0
        public void LeaveGroup(int groupId, int appUserId)
        {
            _db.DbAccess.LeaveGroup(groupId, appUserId);
            var chatGroupViewModel = ChatGroups.FirstOrDefault(e => e.Entity.ChatGroupId == groupId);

            chatGroupViewModel.Users.Remove(appUserId);
            if (!chatGroupViewModel.Users.Any())
            {
                _db.DbAccess.PurgeChatHistory(groupId);
            }
            if (CollectionViews.ContainsKey(appUserId))
            {
                CollectionViews[appUserId].Refresh();
            }
        }
Ejemplo n.º 6
0
            public static List <ChatGroup> GetUserGroups(IPlayer player)
            {
                string[] oxideGroups = Instance.permission.GetUserGroups(player.Id);
                var      groups      = ChatGroups.Where(g => oxideGroups.Any(name => g.GroupName.ToLower() == name)).ToList();

#if RUST
                BasePlayer bPlayer = BasePlayer.Find(player.Id);

                if (bPlayer?.IsDeveloper ?? false)
                {
                    groups.Add(BetterChat.RustDeveloperGroup);
                }
#endif

                return(groups);
            }
Ejemplo n.º 7
0
        private void RemovePlayerFromChatGroups(string username)
        {
            if (!ChatGroupsOfPlayer.ContainsKey(username))
            {
                return;
            }

            foreach (var chatGroup in ChatGroupsOfPlayer[username])
            {
                ChatGroups[chatGroup.Name].RemovePlayer(username);
                if (chatGroup.Users.Count == 0 && chatGroup.Name != "General")
                {
                    ChatGroups.TryRemove(chatGroup.Name, out _);
                }
            }
            ChatGroupsOfPlayer.TryRemove(username, out _);
        }
Ejemplo n.º 8
0
        //  Helper to get and create groups
        ChatGroupViewModel _getGroup(string groupName)
        {
            //  Attempt to get group
            var group = ChatGroups.FirstOrDefault(x => x.Name == groupName);

            if (group == null)
            {
                //  Create group if it doesnt exist
                group = new ChatGroupViewModel()
                {
                    Name = groupName
                };

                Application.Current.Dispatcher.Invoke(() =>
                {
                    ChatGroups.Add(group);
                });
            }
            return(group);
        }
Ejemplo n.º 9
0
        private void NamesReceived(object sender, NamesReceivedEventArgs e)
        {
            if (e.Channel == ChannelName)
            {
                //  Add names to general viewer group
                var group = _getGroup("Viewers");

                Application.Current.Dispatcher.Invoke(() =>
                {
                    foreach (var name in e.Names)
                    {
                        if (!ChatGroups.SelectMany(x => x.Members).Any(x => x.Name.ToLower() == name.ToLower()))
                        {
                            group.Members.Add(new ChatMemberViewModel()
                            {
                                Name = name
                            });
                        }
                    }
                });
            }
        }
Ejemplo n.º 10
0
 public static ChatGroup Find(string name) => ChatGroups.Find(g => g.GroupName == name);
Ejemplo n.º 11
0
 public ChatGroupEntity[] GetChatGroups()
 {
     return(ChatGroups.ToArray());
 }
Ejemplo n.º 12
0
        private void OnMessageReceived(object sender, MessageReceivedEventArgs e)
        {
            using (var message = e.GetMessage())
            {
                // Check if message is meant for this plugin
                if (message.Tag < Login.TagsPerPlugin * GameTag || message.Tag >= Login.TagsPerPlugin * (GameTag + 1))
                {
                    return;
                }

                var client = e.Client;

                // Private Message
                switch (message.Tag)
                {
                case PrivateMessage:
                {
                    // If player isn't logged in -> return error 1
                    if (!_loginPlugin.PlayerLoggedIn(client, MessageFailed, "Private Message failed."))
                    {
                        return;
                    }

                    var    senderName = _loginPlugin.UsersLoggedIn[client];
                    string receiver;
                    string content;

                    try
                    {
                        using (var reader = message.GetReader())
                        {
                            receiver = reader.ReadString();
                            content  = reader.ReadString();
                        }
                    }
                    catch (Exception ex)
                    {
                        // Return Error 0 for Invalid Data Packages Recieved
                        _loginPlugin.InvalidData(client, MessageFailed, ex, "Send Message failed! ");
                        return;
                    }

                    if (!_loginPlugin.Clients.ContainsKey(receiver))
                    {
                        // If receiver isn't logged in -> return error 3
                        using (var writer = DarkRiftWriter.Create())
                        {
                            writer.Write((byte)3);

                            using (var msg = Message.Create(MessageFailed, writer))
                            {
                                client.SendMessage(msg, SendMode.Reliable);
                            }
                        }

                        if (_debug)
                        {
                            WriteEvent("Send Message failed. Receiver wasn't logged in.", LogType.Info);
                        }
                        return;
                    }

                    var receivingClient = _loginPlugin.Clients[receiver];

                    // Let sender know message got transmitted
                    using (var writer = DarkRiftWriter.Create())
                    {
                        writer.Write(senderName);
                        writer.Write(receiver);
                        writer.Write(content);

                        using (var msg = Message.Create(SuccessfulPrivateMessage, writer))
                        {
                            client.SendMessage(msg, SendMode.Reliable);
                        }
                    }

                    // Let receiver know about the new message
                    using (var writer = DarkRiftWriter.Create())
                    {
                        writer.Write(senderName);
                        writer.Write(content);

                        using (var msg = Message.Create(PrivateMessage, writer))
                        {
                            receivingClient.SendMessage(msg, SendMode.Reliable);
                        }
                    }
                    break;
                }

                case RoomMessage:
                {
                    // If player isn't logged in -> return error 1
                    if (!_loginPlugin.PlayerLoggedIn(client, MessageFailed, "Group/Room Message failed."))
                    {
                        return;
                    }

                    var    senderName = _loginPlugin.UsersLoggedIn[client];
                    ushort roomId;
                    string content;

                    try
                    {
                        using (var reader = message.GetReader())
                        {
                            roomId  = reader.ReadUInt16();
                            content = reader.ReadString();
                        }
                    }
                    catch (Exception ex)
                    {
                        // Return Error 0 for Invalid Data Packages Recieved
                        _loginPlugin.InvalidData(client, MessageFailed, ex, "Send Message failed! ");
                        return;
                    }

                    if (!_roomSystem.RoomList[roomId].Clients.Contains(client))
                    {
                        // If player isn't actually in the room -> return error 2
                        using (var writer = DarkRiftWriter.Create())
                        {
                            writer.Write((byte)2);

                            using (var msg = Message.Create(MessageFailed, writer))
                            {
                                client.SendMessage(msg, SendMode.Reliable);
                            }
                        }

                        WriteEvent("Send Message failed. Player wasn't part of the room.", LogType.Warning);
                        return;
                    }

                    using (var writer = DarkRiftWriter.Create())
                    {
                        writer.Write(senderName);
                        writer.Write(content);

                        using (var msg = Message.Create(RoomMessage, writer))
                        {
                            foreach (var cl in _roomSystem.RoomList[roomId].Clients)
                            {
                                cl.SendMessage(msg, SendMode.Reliable);
                            }
                        }
                    }
                    break;
                }

                case GroupMessage:
                {
                    // If player isn't logged in -> return error 1
                    if (!_loginPlugin.PlayerLoggedIn(client, MessageFailed, "Group/Room Message failed."))
                    {
                        return;
                    }

                    var    senderName = _loginPlugin.UsersLoggedIn[client];
                    string groupName;
                    string content;

                    try
                    {
                        using (var reader = message.GetReader())
                        {
                            groupName = reader.ReadString();
                            content   = reader.ReadString();
                        }
                    }
                    catch (Exception ex)
                    {
                        // Return Error 0 for Invalid Data Packages Recieved
                        _loginPlugin.InvalidData(client, MessageFailed, ex, "Send Message failed! ");
                        return;
                    }

                    if (!ChatGroups[groupName].Users.Values.Contains(client))
                    {
                        // If player isn't actually in the chatgroup -> return error 2
                        using (var writer = DarkRiftWriter.Create())
                        {
                            writer.Write((byte)2);

                            using (var msg = Message.Create(MessageFailed, writer))
                            {
                                client.SendMessage(msg, SendMode.Reliable);
                            }
                        }

                        WriteEvent("Send Message failed. Player wasn't part of the chat group.", LogType.Warning);
                        return;
                    }

                    using (var writer = DarkRiftWriter.Create())
                    {
                        writer.Write(groupName);
                        writer.Write(senderName);
                        writer.Write(content);

                        using (var msg = Message.Create(GroupMessage, writer))
                        {
                            foreach (var cl in ChatGroups[groupName].Users.Values)
                            {
                                cl.SendMessage(msg, SendMode.Reliable);
                            }
                        }
                    }
                    break;
                }

                case JoinGroup:
                {
                    // If player isn't logged in -> return error 1
                    if (!_loginPlugin.PlayerLoggedIn(client, JoinGroupFailed, "Join ChatGroup failed."))
                    {
                        return;
                    }

                    var    playerName = _loginPlugin.UsersLoggedIn[client];
                    string groupName;

                    try
                    {
                        using (var reader = message.GetReader())
                        {
                            groupName = reader.ReadString();
                        }
                    }
                    catch (Exception ex)
                    {
                        // Return Error 0 for Invalid Data Packages Recieved
                        _loginPlugin.InvalidData(client, JoinGroupFailed, ex, "Join Chatgroup failed! ");
                        return;
                    }

                    // Create chatgroup if necessary and add player to it
                    var chatGroup = ChatGroups.FirstOrDefault(x =>
                                                              string.Equals(x.Key, groupName, StringComparison.CurrentCultureIgnoreCase)).Value;
                    if (chatGroup == null)
                    {
                        chatGroup             = new ChatGroup(groupName);
                        ChatGroups[groupName] = chatGroup;
                    }

                    if (!chatGroup.AddPlayer(playerName, client))
                    {
                        // Already in Chatgroup -> return error 2
                        using (var writer = DarkRiftWriter.Create())
                        {
                            writer.Write((byte)2);

                            using (var msg = Message.Create(JoinGroupFailed, writer))
                            {
                                client.SendMessage(msg, SendMode.Reliable);
                            }
                        }
                        return;
                    }
                    if (!ChatGroupsOfPlayer.ContainsKey(playerName))
                    {
                        ChatGroupsOfPlayer[playerName] = new List <ChatGroup>();
                    }
                    ChatGroupsOfPlayer[playerName].Add(chatGroup);

                    using (var writer = DarkRiftWriter.Create())
                    {
                        writer.Write(chatGroup);

                        using (var msg = Message.Create(JoinGroup, writer))
                        {
                            client.SendMessage(msg, SendMode.Reliable);
                        }
                    }

                    if (_debug)
                    {
                        WriteEvent("Player joined ChatGroup: " + groupName, LogType.Info);
                    }
                    break;
                }

                case LeaveGroup:
                {
                    // If player isn't logged in -> return error 1
                    if (!_loginPlugin.PlayerLoggedIn(client, JoinGroupFailed, "Leave ChatGroup failed."))
                    {
                        return;
                    }

                    var    playerName = _loginPlugin.UsersLoggedIn[client];
                    string groupName;

                    try
                    {
                        using (var reader = message.GetReader())
                        {
                            groupName = reader.ReadString();
                        }
                    }
                    catch (Exception ex)
                    {
                        // Return Error 0 for Invalid Data Packages Recieved
                        _loginPlugin.InvalidData(client, JoinGroupFailed, ex, "Leave ChatGroup failed! ");
                        return;
                    }

                    // get chatgroup if necessary and remove player from it
                    var chatGroup = ChatGroups.FirstOrDefault(x =>
                                                              string.Equals(x.Key, groupName, StringComparison.CurrentCultureIgnoreCase)).Value;
                    if (chatGroup == null)
                    {
                        // No such Chatgroup -> return error 2
                        using (var writer = DarkRiftWriter.Create())
                        {
                            writer.Write((byte)2);

                            using (var msg = Message.Create(LeaveGroupFailed, writer))
                            {
                                client.SendMessage(msg, SendMode.Reliable);
                            }
                        }
                        return;
                    }
                    chatGroup.RemovePlayer(playerName);

                    // Remove Chatgroup if he was the last player in it
                    if (chatGroup.Users.Count == 0 && chatGroup.Name != "General")
                    {
                        ChatGroups.TryRemove(chatGroup.Name, out _);
                    }

                    // Remove chatgroup from the players groups
                    if (ChatGroupsOfPlayer[playerName].Count == 0)
                    {
                        ChatGroupsOfPlayer.TryRemove(playerName, out _);
                    }
                    else
                    {
                        ChatGroupsOfPlayer[playerName].Remove(chatGroup);
                    }

                    using (var writer = DarkRiftWriter.Create())
                    {
                        writer.Write(chatGroup.Name);

                        using (var msg = Message.Create(LeaveGroup, writer))
                        {
                            client.SendMessage(msg, SendMode.Reliable);
                        }
                    }

                    if (_debug)
                    {
                        WriteEvent("Player left ChatGroup: " + groupName, LogType.Info);
                    }
                    break;
                }

                case GetActiveGroups:
                {
                    // If player isn't logged in -> return error 1
                    if (!_loginPlugin.PlayerLoggedIn(client, GetActiveGroupsFailed, "Get ChatGroups failed."))
                    {
                        return;
                    }

                    var groupNames = ChatGroups.Values.Select(chatGroup => chatGroup.Name).ToArray();

                    using (var writer = DarkRiftWriter.Create())
                    {
                        writer.Write(groupNames);

                        using (var msg = Message.Create(GetActiveGroups, writer))
                        {
                            client.SendMessage(msg, SendMode.Reliable);
                        }
                    }
                    break;
                }
                }
            }
        }