Minor improvements to chat commands

This commit is contained in:
NGnius (Graham) 2021-08-29 13:06:37 -04:00
parent f82eac3ffc
commit 89089c9397
3 changed files with 22 additions and 16 deletions

View file

@ -44,7 +44,7 @@ namespace CLre_server.Tweaks.Chat
auth.UserId = ChatHandler.PublicId;
_chatListener= new ChatListener(_handlers);
_chatClient = new ChatClient(_chatListener, ConnectionProtocol.Udp);
_chatListener._chatClient = _chatClient;
_chatListener.ChatClient = _chatClient;
_chatClient.Connect(Game.Utilities.CardLifePhotonSettings.PhotonChatAppID, "1.0", auth);
// run forever (until server shutsdown)
while (_running)

View file

@ -7,7 +7,7 @@ namespace CLre_server.Tweaks.Chat
{
public class ChatListener: IChatClientListener
{
internal ChatClient _chatClient;
internal ChatClient ChatClient;
public static string ChatName;
private Dictionary<ChatCommandAttribute, ChatConnectionEngine.CommandHandler> _handlers;
@ -18,12 +18,16 @@ namespace CLre_server.Tweaks.Chat
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()
@ -41,13 +45,17 @@ namespace CLre_server.Tweaks.Chat
return;
}
bool result = _chatClient.Subscribe(new[] {ChatName}, 10);
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)
@ -56,28 +64,20 @@ namespace CLre_server.Tweaks.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: `{senders[i]}` said `{messages[i]}` in `{channelName}`");
API.Utility.Logging.MetaLog($"Chat: `{username}` said `{messages[i]}` in `{channelName}`");
#endif
if (messages[i].ToString().ToLower() == "hello server")
{
_chatClient.PublishMessage(channelName, $"Hi {senders[i]}!");
}
else if (messages[i].ToString().ToLower() == "hello world")
{
_chatClient.PublishMessage(channelName, $"Hello fellow programmer {senders[i]}!");
}
if (message.StartsWith("/"))
{
string command = message.Substring(1);
string username = stripUsernameTag(senders[i]);
foreach (ChatCommandAttribute cca in _handlers.Keys)
{
var match = cca.RegexMatch(senders[i], command);
if (match.Success)
{
_handlers[cca](match, _chatClient, username);
_handlers[cca](match, ChatClient, username);
}
}
}
@ -94,17 +94,23 @@ namespace CLre_server.Tweaks.Chat
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)

View file

@ -55,7 +55,7 @@ namespace CLre_server.Tweaks.Chat
}
else
{
connection.PublishMessage(ChatListener.ChatName, "Ban failure: User not found :(");
connection.PublishMessage(ChatListener.ChatName, $"Ban warning: {target} was added to ban list but was not connected to this server...");
}
}