// Server Responses private static void OnDataHandler(object sender, MessageReceivedEventArgs e) { using (var message = e.GetMessage()) { // Check if message is meant for this plugin if (message.Tag < Tags.TagsPerPlugin * Tags.Chat || message.Tag >= Tags.TagsPerPlugin * (Tags.Chat + 1)) { return; } // Private message received switch (message.Tag) { // Private message received case ChatTags.PrivateMessage: { using (var reader = message.GetReader()) { var senderName = reader.ReadString(); var content = reader.ReadString(); var chatMessage = new ChatMessage(senderName, content, MessageType.Private, senderName); Messages.Add(chatMessage); onPrivateMessage?.Invoke(chatMessage); } break; } // Private message sent case ChatTags.SuccessfulPrivateMessage: { using (var reader = message.GetReader()) { var senderName = reader.ReadString(); var receiver = reader.ReadString(); var content = reader.ReadString(); var chatMessage = new ChatMessage(senderName, content, MessageType.Private, receiver, true); Messages.Add(chatMessage); onPrivateMessage?.Invoke(chatMessage); } break; } // Room message received case ChatTags.RoomMessage: { using (var reader = message.GetReader()) { var senderName = reader.ReadString(); var content = reader.ReadString(); var chatMessage = new ChatMessage(senderName, content, MessageType.Room, "Room"); Messages.Add(chatMessage); onRoomMessage?.Invoke(chatMessage); } break; } // Group message received case ChatTags.GroupMessage: { using (var reader = message.GetReader()) { var groupName = reader.ReadString(); var senderName = reader.ReadString(); var content = reader.ReadString(); var chatMessage = new ChatMessage(senderName, content, MessageType.ChatGroup, groupName); Messages.Add(chatMessage); onGroupMessage?.Invoke(chatMessage); } break; } case ChatTags.MessageFailed: { var content = "Failed to send message."; using (var reader = message.GetReader()) { if (reader.Length != 1) { Debug.LogWarning("Invalid Message Failed Error data received."); } else { switch (reader.ReadByte()) { case 0: Debug.Log("Invalid Message data sent!"); break; case 1: Debug.Log("You're not logged in!"); SceneManager.LoadScene("Login"); break; case 2: Debug.Log("You're not part of this chatgroup."); content = "Not connected to this chat channel. Try leaving and rejoining!"; break; case 3: Debug.Log("Failed to send message. Player is offline."); content = "Player is offline."; break; default: Debug.Log("Invalid errorId!"); break; } } } ServerMessage(content, MessageType.Error); break; } case ChatTags.JoinGroup: { using (var reader = message.GetReader()) { var group = reader.ReadSerializable <ChatGroup>(); ServerMessage("You joined the channel: " + group.Name, MessageType.ChatGroup); if (!SavedChatGroups.Contains(group.Name)) { SavedChatGroups.Add(group.Name); ArrayPrefs.SetStringArray("ChatGroups", SavedChatGroups.ToArray()); } onSuccessfulJoinGroup?.Invoke(group.Name); } break; } case ChatTags.JoinGroupFailed: { var content = "Failed to join chat group."; using (var reader = message.GetReader()) { if (reader.Length != 1) { Debug.LogWarning("Invalid Join Group Failed Error data received."); } else { switch (reader.ReadByte()) { case 0: Debug.Log("Invalid Join Group data sent!"); break; case 1: Debug.Log("You're not logged in!"); SceneManager.LoadScene("Login"); break; case 2: Debug.Log("Alreay in this chatgroup."); content = "You are already in this chat group."; break; default: Debug.Log("Invalid errorId!"); break; } } ServerMessage(content, MessageType.Error); } break; } case ChatTags.LeaveGroup: { using (var reader = message.GetReader()) { var groupName = reader.ReadString(); ServerMessage("You left the channel: " + groupName, MessageType.ChatGroup); if (SavedChatGroups.Remove(groupName)) { ArrayPrefs.SetStringArray("ChatGroups", SavedChatGroups.ToArray()); } onSuccessfulLeaveGroup?.Invoke(groupName); } break; } case ChatTags.LeaveGroupFailed: { var content = "Failed to leave chat group."; using (var reader = message.GetReader()) { if (reader.Length != 1) { Debug.LogWarning("Invalid Leave Group Failed Error data received."); } else { switch (reader.ReadByte()) { case 0: Debug.Log("Invalid Leave Group data sent!"); break; case 1: Debug.Log("You're not logged in!"); SceneManager.LoadScene("Login"); break; case 2: Debug.Log("No such chatgroup chatgroup."); content = "There is no chat group with this name."; break; default: Debug.Log("Invalid errorId!"); break; } } } ServerMessage(content, MessageType.Error); break; } case ChatTags.GetActiveGroups: { using (var reader = message.GetReader()) { var groupList = reader.ReadStrings().ToList(); groupList.Sort(string.CompareOrdinal); foreach (var group in groupList) { ServerMessage(group, MessageType.All); } } break; } case ChatTags.GetActiveGroupsFailed: { var content = "Failed to get list of chat groups."; using (var reader = message.GetReader()) { if (reader.Length != 1) { Debug.LogWarning("Invalid Get Active Groups Failed Error data received."); } else { switch (reader.ReadByte()) { case 1: Debug.Log("You're not logged in!"); SceneManager.LoadScene("Login"); break; default: Debug.Log("Invalid errorId!"); break; } } } ServerMessage(content, MessageType.Error); break; } } } }
// Server Responses private void OnDataHandler(object sender, MessageReceivedEventArgs e) { var message = e.Message as TagSubjectMessage; if (message == null || message.Tag != Tags.Chat) { return; } // Private message received if (message.Subject == ChatSubjects.PrivateMessage) { var reader = message.GetReader(); var senderName = reader.ReadString(); var content = reader.ReadString(); var chatMessage = new ChatMessage(senderName, content, MessageType.Private, senderName); Messages.Add(chatMessage); onPrivateMessage?.Invoke(chatMessage); } // Private message sent else if (message.Subject == ChatSubjects.SuccessfulPrivateMessage) { var reader = message.GetReader(); var senderName = reader.ReadString(); var receiver = reader.ReadString(); var content = reader.ReadString(); var chatMessage = new ChatMessage(senderName, content, MessageType.Private, receiver, true); Messages.Add(chatMessage); onPrivateMessage?.Invoke(chatMessage); } // Room message received else if (message.Subject == ChatSubjects.RoomMessage) { var reader = message.GetReader(); var senderName = reader.ReadString(); var content = reader.ReadString(); var chatMessage = new ChatMessage(senderName, content, MessageType.Room, "Lobby"); Messages.Add(chatMessage); onRoomMessage?.Invoke(chatMessage); } // Group message received else if (message.Subject == ChatSubjects.GroupMessage) { var reader = message.GetReader(); var groupName = reader.ReadString(); var senderName = reader.ReadString(); var content = reader.ReadString(); var chatMessage = new ChatMessage(senderName, content, MessageType.ChatGroup, groupName); Messages.Add(chatMessage); onGroupMessage?.Invoke(chatMessage); } // Message failed else if (message.Subject == ChatSubjects.MessageFailed) { var content = "Failed to send message."; var reader = message.GetReader(); if (reader.Length != 1) { Debug.LogWarning("Invalid Message Failed Error data received."); } else { switch (reader.ReadByte()) { case 0: Debug.Log("Invalid Message data sent!"); break; case 1: Debug.Log("You're not logged in!"); SceneManager.LoadScene("Login"); break; case 2: content = "Not connected to this chat channel. Try leaving and rejoining!"; break; case 3: content = "Player is offline."; break; default: Debug.Log("Invalid errorId!"); break; } } ServerMessage(content, MessageType.Error); } // Successfully joined group else if (message.Subject == ChatSubjects.JoinGroup) { var reader = message.GetReader(); var group = reader.ReadSerializable <ChatGroup>(); ServerMessage("You joined the channel: " + group.Name, MessageType.ChatGroup); if (!SavedChatGroups.Contains(group.Name)) { SavedChatGroups.Add(group.Name); ArrayPrefs.SetStringArray("ChatGroups", SavedChatGroups.ToArray()); } onSuccessfulJoinGroup?.Invoke(group.Name); } // Failed to join group else if (message.Subject == ChatSubjects.JoinGroupFailed) { var content = "Failed to join chat group."; var reader = message.GetReader(); if (reader.Length != 1) { Debug.LogWarning("Invalid Join Group Failed Error data received."); } else { switch (reader.ReadByte()) { case 0: Debug.Log("Invalid Join Group data sent!"); break; case 1: Debug.Log("You're not logged in!"); SceneManager.LoadScene("Login"); break; case 2: Debug.Log("Already in this chatgroup."); content = "You are already in this chat group."; break; default: Debug.Log("Invalid errorId!"); break; } } ServerMessage(content, MessageType.Error); } // Successfully left group else if (message.Subject == ChatSubjects.LeaveGroup) { var reader = message.GetReader(); var groupName = reader.ReadString(); ServerMessage("You left the channel: " + groupName, MessageType.ChatGroup); if (SavedChatGroups.Remove(groupName)) { ArrayPrefs.SetStringArray("ChatGroups", SavedChatGroups.ToArray()); } onSuccessfulLeaveGroup?.Invoke(groupName); } // Failed to leave group else if (message.Subject == ChatSubjects.LeaveGroupFailed) { var content = "Failed to leave chat group."; var reader = message.GetReader(); if (reader.Length != 1) { Debug.LogWarning("Invalid Leave Group Failed Error data received."); } else { switch (reader.ReadByte()) { case 0: Debug.Log("Invalid Leave Group data sent!"); break; case 1: Debug.Log("You're not logged in!"); SceneManager.LoadScene("Login"); break; case 2: Debug.Log("No such chatgroup chatgroup."); content = "There is no chat group with this name."; break; default: Debug.Log("Invalid errorId!"); break; } } ServerMessage(content, MessageType.Error); } // Received all active chat groups else if (message.Subject == ChatSubjects.GetActiveGroups) { var reader = message.GetReader(); var groupList = reader.ReadStrings().ToList(); groupList.Sort(string.CompareOrdinal); foreach (var group in groupList) { ServerMessage(group, MessageType.All); } } // Failed to get grouplist else if (message.Subject == ChatSubjects.GetActiveGroupsFailed) { var content = "Failed to get list of chat groups."; var reader = message.GetReader(); if (reader.Length != 1) { Debug.LogWarning("Invalid Get Active Groups Failed Error data received."); } else { switch (reader.ReadByte()) { case 1: Debug.Log("You're not logged in!"); SceneManager.LoadScene("Login"); break; default: Debug.Log("Invalid errorId!"); break; } } ServerMessage(content, MessageType.Error); } }