|
- using System;
- using System.Collections.Generic;
- using ExitGames.Client.Photon;
- using ExitGames.Client.Photon.Chat;
-
- namespace CLre_server.Tweaks.Chat
- {
- public class ChatListener: IChatClientListener
- {
- internal ChatClient ChatClient;
- public static string ChatName;
- private Dictionary<ChatCommandAttribute, ChatConnectionEngine.CommandHandler> _handlers;
-
- public ChatListener(Dictionary<ChatCommandAttribute, ChatConnectionEngine.CommandHandler> handlers)
- {
- _handlers = handlers;
- }
-
- public void DebugReturn(DebugLevel level, string message)
- {
- #if DEBUG
- API.Utility.Logging.Log($"ServerChatLog<{level}>: {message}");
- #endif
- }
-
- public void OnDisconnected()
- {
- #if DEBUG
- API.Utility.Logging.MetaLog("Chat disconnected");
- #endif
- }
-
- public void OnConnected()
- {
- API.Utility.Logging.MetaLog("Chat connected");
- // autoconnect to server's chat room
- Room room = PhotonNetwork.room;
- ChatName = "";
- if (room != null)
- {
- ChatName = $"{room.Name}_chat_";
- }
- else
- {
- return;
- }
-
- bool result = ChatClient.Subscribe(new[] {ChatName}, 10);
- #if DEBUG
- API.Utility.Logging.MetaLog($"Subscribed to chat: {result}");
- #endif
- }
-
- public void OnChatStateChange(ChatState state)
- {
- #if DEBUG
- API.Utility.Logging.MetaLog($"Chat state changed to {state}");
- #endif
- }
-
- public void OnGetMessages(string channelName, string[] senders, object[] messages)
- {
- if (channelName != ChatName) return; // just in case the server somehow gets subscribed to the wrong chat
- for (int i = 0; i < senders.Length; i++)
- {
- string message = messages[i].ToString();
- string username = stripUsernameTag(senders[i]);
- #if DEBUG
- API.Utility.Logging.MetaLog($"Chat: `{username}` said `{messages[i]}` in `{channelName}`");
- #endif
- if (message.StartsWith("/"))
- {
- string command = message.Substring(1);
-
- foreach (ChatCommandAttribute cca in _handlers.Keys)
- {
- var match = cca.RegexMatch(senders[i], command);
- if (match.Success)
- {
- _handlers[cca](match, ChatClient, username);
- }
- }
- }
-
- }
- }
-
- public void OnPrivateMessage(string sender, object message, string channelName)
- {
- #if DEBUG
- API.Utility.Logging.MetaLog($"Chat (private): `{sender}` said `{message}` in `{channelName}`");
- #endif
- }
-
- public void OnSubscribed(string[] channels, bool[] results)
- {
- #if DEBUG
- API.Utility.Logging.MetaLog($"Subscribed");
- #endif
- }
-
- public void OnUnsubscribed(string[] channels)
- {
- #if DEBUG
- API.Utility.Logging.MetaLog($"Unsubscribed");
- #endif
- }
-
- public void OnStatusUpdate(string user, int status, bool gotMessage, object message)
- {
- #if DEBUG
- API.Utility.Logging.MetaLog($"Status update: {user}->{status} ({gotMessage}:{message})");
- #endif
- }
-
- private string stripUsernameTag(string sender)
- {
- if (!sender.Contains("]")) return sender;
- return sender.Split(']')[1].Trim();
- }
- }
- }
|