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) }); }
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(); } }
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(); } }
// 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); }
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; } } } }