Skip to content

Commit

Permalink
Update private message behavior to be closer to vanilla client
Browse files Browse the repository at this point in the history
  • Loading branch information
ethanmoffat committed May 27, 2022
1 parent fcfd2cd commit 9902262
Show file tree
Hide file tree
Showing 5 changed files with 82 additions and 45 deletions.
2 changes: 1 addition & 1 deletion EOLib.Localization/EOResourceID.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public enum EOResourceID


YOUR_MIND_PREVENTS_YOU_TO_SAY = 14,

STATUS_LABEL_INVALID_INPUT_TRY = 15,
STATUS_LABEL_CHAT_PANEL_NOW_VIEWED = 16,
STATUS_LABEL_STATS_PANEL_NOW_VIEWED = 17,
STATUS_LABEL_ONLINE_PLAYERS_NOW_VIEWED = 18,
Expand Down
34 changes: 27 additions & 7 deletions EOLib/Domain/Chat/ChatActions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,13 @@

namespace EOLib.Domain.Chat
{
public enum ChatResult
{
Ok,
YourMindPrevents,
HideSpeechBubble,
}

[AutoMappedType]
public class ChatActions : IChatActions
{
Expand Down Expand Up @@ -40,14 +47,14 @@ public ChatActions(IChatRepository chatRepository,
_configurationProvider = configurationProvider;
}

public (bool, string) SendChatToServer(string chat, string targetCharacter)
public (ChatResult, string) SendChatToServer(string chat, string targetCharacter)
{
var chatType = _chatTypeCalculator.CalculateChatType(chat);

if (chatType == ChatType.Command)
{
if (HandleCommand(chat))
return (true, chat);
return (ChatResult.HideSpeechBubble, chat);

//treat unhandled command as local chat
chatType = ChatType.Local;
Expand All @@ -65,7 +72,7 @@ public ChatActions(IChatRepository chatRepository,
var (ok, filtered) = _chatProcessor.FilterCurses(chat);
if (!ok)
{
return (ok, filtered);
return (ChatResult.YourMindPrevents, filtered);
}

chat = filtered;
Expand All @@ -78,7 +85,7 @@ public ChatActions(IChatRepository chatRepository,

AddChatForLocalDisplay(chatType, chat, targetCharacter);

return (ok, chat);
return (chatType == ChatType.PM ? ChatResult.HideSpeechBubble : ChatResult.Ok, chat);
}

public void SetHearWhispers(bool whispersEnabled)
Expand All @@ -98,6 +105,19 @@ public void SetGlobalActive(bool active)
_packetSendService.SendPacket(packet);
}

public void ClosePMTab(ChatTab whichTab)
{
if (whichTab != ChatTab.Private1 && whichTab != ChatTab.Private2)
throw new ArgumentException("Tab must be a PM tab", nameof(whichTab));

_chatRepository.AllChat[whichTab].Clear();

if (whichTab == ChatTab.Private1)
_chatRepository.PMTarget1 = null;
else if (whichTab == ChatTab.Private2)
_chatRepository.PMTarget2 = null;
}

/// <summary>
/// Returns true if the text is a command (#) and can be handled as such, false if it should be sent to the server as a public chat string
/// </summary>
Expand Down Expand Up @@ -126,8 +146,6 @@ private void AddChatForLocalDisplay(ChatType chatType, string chat, string targe
_chatRepository.AllChat[ChatTab.Private1].Add(new ChatData(ChatTab.Private1, who, chat, ChatIcon.Note, ChatColor.PM));
else if (targetCharacter == _chatRepository.PMTarget2)
_chatRepository.AllChat[ChatTab.Private2].Add(new ChatData(ChatTab.Private2, who, chat, ChatIcon.Note, ChatColor.PM));
else
throw new ArgumentException("Unexpected target character!", nameof(targetCharacter));

break;
case ChatType.Local:
Expand Down Expand Up @@ -157,10 +175,12 @@ private void AddChatForLocalDisplay(ChatType chatType, string chat, string targe

public interface IChatActions
{
(bool Ok, string Processed) SendChatToServer(string chat, string targetCharacter);
(ChatResult Ok, string Processed) SendChatToServer(string chat, string targetCharacter);

void SetHearWhispers(bool whispersEnabled);

void SetGlobalActive(bool active);

void ClosePMTab(ChatTab tab);
}
}
31 changes: 13 additions & 18 deletions EndlessClient/Controllers/ChatController.cs
Original file line number Diff line number Diff line change
@@ -1,20 +1,15 @@
using System.Threading.Tasks;
using AutomaticTypeMapper;
using AutomaticTypeMapper;
using EndlessClient.ControlSets;
using EndlessClient.Dialogs.Actions;
using EndlessClient.GameExecution;
using EndlessClient.HUD;
using EndlessClient.HUD.Chat;
using EndlessClient.HUD.Controls;
using EndlessClient.UIControls;
using EOLib.Domain.Chat;
using EOLib.Localization;
using EOLib.Net;
using EOLib.Net.Communication;

namespace EndlessClient.Controllers
{
[MappedType(BaseType = typeof(IChatController))]
[AutoMappedType]
public class ChatController : IChatController
{
private readonly IChatTextBoxActions _chatTextBoxActions;
Expand Down Expand Up @@ -42,20 +37,20 @@ public ChatController(IChatTextBoxActions chatTextBoxActions,
public void SendChatAndClearTextBox()
{
var localTypedText = ChatTextBox.Text;
var targetCharacter = _privateMessageActions.GetTargetCharacter(localTypedText);
var (pmCheckOk, targetCharacter) = _privateMessageActions.GetTargetCharacter(localTypedText);

var (ok, updatedChat) = _chatActions.SendChatToServer(localTypedText, targetCharacter);

_chatTextBoxActions.ClearChatText();

if (ok)
if (pmCheckOk)
{
_chatBubbleActions.ShowChatBubbleForMainCharacter(updatedChat);
}
else
{
_statusLabelSetter.SetStatusLabel(EOResourceID.STATUS_LABEL_TYPE_WARNING, EOResourceID.YOUR_MIND_PREVENTS_YOU_TO_SAY);
var (result, updatedChat) = _chatActions.SendChatToServer(localTypedText, targetCharacter);
switch (result)
{
case ChatResult.Ok: _chatBubbleActions.ShowChatBubbleForMainCharacter(updatedChat); break;
case ChatResult.YourMindPrevents: _statusLabelSetter.SetStatusLabel(EOResourceID.STATUS_LABEL_TYPE_WARNING, EOResourceID.YOUR_MIND_PREVENTS_YOU_TO_SAY); break;
case ChatResult.HideSpeechBubble: break; // no-op
}
}

_chatTextBoxActions.ClearChatText();
}

public void SelectChatTextBox()
Expand Down
4 changes: 2 additions & 2 deletions EndlessClient/HUD/Chat/ChatNotificationActions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,9 @@ public void NotifyPrivateMessageRecipientNotFound(string recipientName)
whichTab.MatchSome(tab =>
{
if (tab == ChatTab.Private1)
_chatRepository.PMTarget1 = string.Empty;
_chatRepository.PMTarget1 = null;
else if (tab == ChatTab.Private2)
_chatRepository.PMTarget2 = string.Empty;
_chatRepository.PMTarget2 = null;

_chatRepository.AllChat[tab].Clear();

Expand Down
56 changes: 39 additions & 17 deletions EndlessClient/HUD/Chat/PrivateMessageActions.cs
Original file line number Diff line number Diff line change
@@ -1,48 +1,70 @@
using System.Linq;
using AutomaticTypeMapper;
using AutomaticTypeMapper;
using EndlessClient.ControlSets;
using EndlessClient.HUD.Controls;
using EndlessClient.HUD.Panels;
using EOLib.Domain.Character;
using EOLib.Domain.Chat;
using EOLib.Localization;
using System;

namespace EndlessClient.HUD.Chat
{
[MappedType(BaseType = typeof(IPrivateMessageActions))]
public class PrivateMessageActions : IPrivateMessageActions
{
private readonly IHudControlProvider _hudControlProvider;
private readonly ICharacterProvider _characterProvider;
private readonly IChatProvider _chatProvider;
private readonly IChatTypeCalculator _chatTypeCalculator;
private readonly IStatusLabelSetter _statusLabelSetter;

public PrivateMessageActions(IHudControlProvider hudControlProvider,
ICharacterProvider characterProvider,
IChatProvider chatProvider,
IChatTypeCalculator chatTypeCalculator)
IChatTypeCalculator chatTypeCalculator,
IStatusLabelSetter statusLabelSetter)
{
_hudControlProvider = hudControlProvider;
_characterProvider = characterProvider;
_chatProvider = chatProvider;
_chatTypeCalculator = chatTypeCalculator;
_statusLabelSetter = statusLabelSetter;
}

public string GetTargetCharacter(string localTypedText)
public (bool, string) GetTargetCharacter(string localTypedText)
{
//todo: error status bar message if there is no text following a character name

if (_chatTypeCalculator.CalculateChatType(localTypedText) != ChatType.PM)
return "";
{
return (true, string.Empty);
}

if (localTypedText.Length < 2 || localTypedText[1] == ' ')
{
_statusLabelSetter.SetStatusLabel(EOResourceID.STATUS_LABEL_TYPE_WARNING, EOResourceID.STATUS_LABEL_INVALID_INPUT_TRY);
return (false, string.Empty);
}

if (CurrentTab == ChatTab.Private1)
return _chatProvider.PMTarget1;
{
return (true, _chatProvider.PMTarget1);
}
else if (CurrentTab == ChatTab.Private2)
{
return (true, _chatProvider.PMTarget2);
}

if (CurrentTab == ChatTab.Private2)
return _chatProvider.PMTarget2;
var messageParts = localTypedText[1..].Split(' ', StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries);
if (messageParts.Length <= 1)
{
_statusLabelSetter.SetStatusLabel(EOResourceID.STATUS_LABEL_TYPE_WARNING, EOResourceID.STATUS_LABEL_INVALID_INPUT_TRY);
return (false, string.Empty);
}

var characterArray = localTypedText.Skip(1)
.TakeWhile(x => x != ' ')
.ToArray();
var characterName = new string(characterArray);
if (string.Equals(messageParts[0], _characterProvider.MainCharacter.Name, StringComparison.OrdinalIgnoreCase))
return (false, string.Empty);

ChatPanel.TryStartNewPrivateChat(characterName);
return characterName;
ChatPanel.TryStartNewPrivateChat(messageParts[0]);
return (true, messageParts[0]);
}

private ChatPanel ChatPanel => _hudControlProvider.GetComponent<ChatPanel>(HudControlIdentifier.ChatPanel);
Expand All @@ -52,6 +74,6 @@ public string GetTargetCharacter(string localTypedText)

public interface IPrivateMessageActions
{
string GetTargetCharacter(string localTypedText);
(bool Ok, string TargetCharacter) GetTargetCharacter(string localTypedText);
}
}

0 comments on commit 9902262

Please sign in to comment.