Unofficial CardLife revival project, pronounced like "celery"
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

122 lines
3.6KB

  1. using System;
  2. using System.Collections.Generic;
  3. using ExitGames.Client.Photon;
  4. using ExitGames.Client.Photon.Chat;
  5. namespace CLre_server.Tweaks.Chat
  6. {
  7. public class ChatListener: IChatClientListener
  8. {
  9. internal ChatClient ChatClient;
  10. public static string ChatName;
  11. private Dictionary<ChatCommandAttribute, ChatConnectionEngine.CommandHandler> _handlers;
  12. public ChatListener(Dictionary<ChatCommandAttribute, ChatConnectionEngine.CommandHandler> handlers)
  13. {
  14. _handlers = handlers;
  15. }
  16. public void DebugReturn(DebugLevel level, string message)
  17. {
  18. #if DEBUG
  19. API.Utility.Logging.Log($"ServerChatLog<{level}>: {message}");
  20. #endif
  21. }
  22. public void OnDisconnected()
  23. {
  24. #if DEBUG
  25. API.Utility.Logging.MetaLog("Chat disconnected");
  26. #endif
  27. }
  28. public void OnConnected()
  29. {
  30. API.Utility.Logging.MetaLog("Chat connected");
  31. // autoconnect to server's chat room
  32. Room room = PhotonNetwork.room;
  33. ChatName = "";
  34. if (room != null)
  35. {
  36. ChatName = $"{room.Name}_chat_";
  37. }
  38. else
  39. {
  40. return;
  41. }
  42. bool result = ChatClient.Subscribe(new[] {ChatName}, 10);
  43. #if DEBUG
  44. API.Utility.Logging.MetaLog($"Subscribed to chat: {result}");
  45. #endif
  46. }
  47. public void OnChatStateChange(ChatState state)
  48. {
  49. #if DEBUG
  50. API.Utility.Logging.MetaLog($"Chat state changed to {state}");
  51. #endif
  52. }
  53. public void OnGetMessages(string channelName, string[] senders, object[] messages)
  54. {
  55. if (channelName != ChatName) return; // just in case the server somehow gets subscribed to the wrong chat
  56. for (int i = 0; i < senders.Length; i++)
  57. {
  58. string message = messages[i].ToString();
  59. string username = stripUsernameTag(senders[i]);
  60. #if DEBUG
  61. API.Utility.Logging.MetaLog($"Chat: `{username}` said `{messages[i]}` in `{channelName}`");
  62. #endif
  63. if (message.StartsWith("/"))
  64. {
  65. string command = message.Substring(1);
  66. foreach (ChatCommandAttribute cca in _handlers.Keys)
  67. {
  68. var match = cca.RegexMatch(senders[i], command);
  69. if (match.Success)
  70. {
  71. _handlers[cca](match, ChatClient, username);
  72. }
  73. }
  74. }
  75. }
  76. }
  77. public void OnPrivateMessage(string sender, object message, string channelName)
  78. {
  79. #if DEBUG
  80. API.Utility.Logging.MetaLog($"Chat (private): `{sender}` said `{message}` in `{channelName}`");
  81. #endif
  82. }
  83. public void OnSubscribed(string[] channels, bool[] results)
  84. {
  85. #if DEBUG
  86. API.Utility.Logging.MetaLog($"Subscribed");
  87. #endif
  88. }
  89. public void OnUnsubscribed(string[] channels)
  90. {
  91. #if DEBUG
  92. API.Utility.Logging.MetaLog($"Unsubscribed");
  93. #endif
  94. }
  95. public void OnStatusUpdate(string user, int status, bool gotMessage, object message)
  96. {
  97. #if DEBUG
  98. API.Utility.Logging.MetaLog($"Status update: {user}->{status} ({gotMessage}:{message})");
  99. #endif
  100. }
  101. private string stripUsernameTag(string sender)
  102. {
  103. if (!sender.Contains("]")) return sender;
  104. return sender.Split(']')[1].Trim();
  105. }
  106. }
  107. }