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.

116 lines
3.9KB

  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. API.Utility.Logging.Log($"ServerChatLog<{level}>: {message}");
  19. }
  20. public void OnDisconnected()
  21. {
  22. API.Utility.Logging.MetaLog("Chat disconnected");
  23. }
  24. public void OnConnected()
  25. {
  26. API.Utility.Logging.MetaLog("Chat connected");
  27. // autoconnect to server's chat room
  28. Room room = PhotonNetwork.room;
  29. ChatName = "";
  30. if (room != null)
  31. {
  32. ChatName = $"{room.Name}_chat_";
  33. }
  34. else
  35. {
  36. return;
  37. }
  38. bool result = _chatClient.Subscribe(new[] {ChatName}, 10);
  39. API.Utility.Logging.MetaLog($"Subscribed to chat: {result}");
  40. }
  41. public void OnChatStateChange(ChatState state)
  42. {
  43. API.Utility.Logging.MetaLog($"Chat state changed to {state}");
  44. }
  45. public void OnGetMessages(string channelName, string[] senders, object[] messages)
  46. {
  47. if (channelName != ChatName) return; // just in case the server somehow gets subscribed to the wrong chat
  48. for (int i = 0; i < senders.Length; i++)
  49. {
  50. string message = messages[i].ToString();
  51. #if DEBUG
  52. API.Utility.Logging.MetaLog($"Chat: `{senders[i]}` said `{messages[i]}` in `{channelName}`");
  53. #endif
  54. if (messages[i].ToString().ToLower() == "hello server")
  55. {
  56. _chatClient.PublishMessage(channelName, $"Hi {senders[i]}!");
  57. }
  58. else if (messages[i].ToString().ToLower() == "hello world")
  59. {
  60. _chatClient.PublishMessage(channelName, $"Hello fellow programmer {senders[i]}!");
  61. }
  62. if (message.StartsWith("/"))
  63. {
  64. string command = message.Substring(1);
  65. string username = stripUsernameTag(senders[i]);
  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. API.Utility.Logging.MetaLog($"Subscribed");
  86. }
  87. public void OnUnsubscribed(string[] channels)
  88. {
  89. API.Utility.Logging.MetaLog($"Unsubscribed");
  90. }
  91. public void OnStatusUpdate(string user, int status, bool gotMessage, object message)
  92. {
  93. API.Utility.Logging.MetaLog($"Status update: {user}->{status} ({gotMessage}:{message})");
  94. }
  95. private string stripUsernameTag(string sender)
  96. {
  97. if (!sender.Contains("]")) return sender;
  98. return sender.Split(']')[1].Trim();
  99. }
  100. }
  101. }