PalacePal/Pal.Client/DependencyInjection/ChatService.cs

118 lines
4.4 KiB
C#
Raw Normal View History

2023-02-15 22:17:19 +00:00
using System;
using System.Text;
2023-02-15 22:17:19 +00:00
using System.Text.RegularExpressions;
using Dalamud.Data;
using Dalamud.Game.Gui;
using Dalamud.Game.Text;
using Dalamud.Game.Text.SeStringHandling;
using Lumina.Excel.GeneratedSheets;
using Pal.Client.Configuration;
2023-02-22 22:58:05 +00:00
using Pal.Client.Floors;
2023-02-15 22:17:19 +00:00
2023-03-30 20:01:43 +00:00
namespace Pal.Client.DependencyInjection;
internal sealed class ChatService : IDisposable
2023-02-15 22:17:19 +00:00
{
2023-03-30 20:01:43 +00:00
private readonly ChatGui _chatGui;
private readonly TerritoryState _territoryState;
private readonly IPalacePalConfiguration _configuration;
private readonly DataManager _dataManager;
private readonly LocalizedChatMessages _localizedChatMessages;
public ChatService(ChatGui chatGui, TerritoryState territoryState, IPalacePalConfiguration configuration,
DataManager dataManager)
2023-02-15 22:17:19 +00:00
{
2023-03-30 20:01:43 +00:00
_chatGui = chatGui;
_territoryState = territoryState;
_configuration = configuration;
_dataManager = dataManager;
2023-02-15 22:17:19 +00:00
2023-03-30 20:01:43 +00:00
_localizedChatMessages = LoadLanguageStrings();
2023-02-15 22:17:19 +00:00
2023-03-30 20:01:43 +00:00
_chatGui.ChatMessage += OnChatMessage;
}
public void Dispose()
=> _chatGui.ChatMessage -= OnChatMessage;
2023-02-15 22:17:19 +00:00
2023-03-30 20:01:43 +00:00
private void OnChatMessage(XivChatType type, uint senderId, ref SeString sender, ref SeString seMessage,
ref bool isHandled)
{
if (_configuration.FirstUse)
return;
if (type != (XivChatType)2105)
return;
2023-02-15 22:17:19 +00:00
2023-03-30 20:01:43 +00:00
string message = seMessage.ToString();
if (_localizedChatMessages.FloorChanged.IsMatch(message))
2023-02-15 22:17:19 +00:00
{
2023-03-30 20:01:43 +00:00
_territoryState.PomanderOfSight = PomanderState.Inactive;
2023-02-15 22:17:19 +00:00
2023-03-30 20:01:43 +00:00
if (_territoryState.PomanderOfIntuition == PomanderState.FoundOnCurrentFloor)
_territoryState.PomanderOfIntuition = PomanderState.Inactive;
}
else if (message.EndsWith(_localizedChatMessages.MapRevealed))
2023-02-15 22:17:19 +00:00
{
2023-03-30 20:01:43 +00:00
_territoryState.PomanderOfSight = PomanderState.Active;
2023-02-15 22:17:19 +00:00
}
2023-03-30 20:01:43 +00:00
else if (message.EndsWith(_localizedChatMessages.AllTrapsRemoved))
{
_territoryState.PomanderOfSight = PomanderState.PomanderOfSafetyUsed;
}
else if (message.EndsWith(_localizedChatMessages.HoardNotOnCurrentFloor) ||
message.EndsWith(_localizedChatMessages.HoardOnCurrentFloor))
{
// There is no functional difference between these - if you don't open the marked coffer,
// going to higher floors will keep the pomander active.
_territoryState.PomanderOfIntuition = PomanderState.Active;
}
else if (message.EndsWith(_localizedChatMessages.HoardCofferOpened))
2023-02-15 22:17:19 +00:00
{
2023-03-30 20:01:43 +00:00
_territoryState.PomanderOfIntuition = PomanderState.FoundOnCurrentFloor;
2023-02-15 22:17:19 +00:00
}
2023-03-30 20:01:43 +00:00
}
2023-02-15 22:17:19 +00:00
2023-03-30 20:01:43 +00:00
private LocalizedChatMessages LoadLanguageStrings()
{
return new LocalizedChatMessages
2023-02-15 22:17:19 +00:00
{
2023-03-30 20:01:43 +00:00
MapRevealed = GetLocalizedString(7256),
AllTrapsRemoved = GetLocalizedString(7255),
HoardOnCurrentFloor = GetLocalizedString(7272),
HoardNotOnCurrentFloor = GetLocalizedString(7273),
HoardCofferOpened = GetLocalizedString(7274),
FloorChanged =
new Regex("^" + GetLocalizedString(7270, true).Replace("\u0002 \u0003\ufffd\u0002\u0003", @"(\d+)") +
2023-03-30 20:01:43 +00:00
"$"),
};
}
2023-02-15 22:17:19 +00:00
private string GetLocalizedString(uint id, bool asRawData = false)
2023-03-30 20:01:43 +00:00
{
var text = _dataManager.GetExcelSheet<LogMessage>()?.GetRow(id)?.Text;
if (text == null)
return "Unknown";
if (asRawData)
return Encoding.UTF8.GetString(text.RawData);
else
return text.ToString();
2023-03-30 20:01:43 +00:00
}
private sealed class LocalizedChatMessages
{
public string MapRevealed { get; init; } = "???"; //"The map for this floor has been revealed!";
public string AllTrapsRemoved { get; init; } = "???"; // "All the traps on this floor have disappeared!";
public string HoardOnCurrentFloor { get; init; } = "???"; // "You sense the Accursed Hoard calling you...";
2023-02-15 22:17:19 +00:00
2023-03-30 20:01:43 +00:00
public string HoardNotOnCurrentFloor { get; init; } =
"???"; // "You do not sense the call of the Accursed Hoard on this floor...";
2023-02-15 22:17:19 +00:00
2023-03-30 20:01:43 +00:00
public string HoardCofferOpened { get; init; } = "???"; // "You discover a piece of the Accursed Hoard!";
public Regex FloorChanged { get; init; } =
new(@"This isn't a game message, but will be replaced"); // new Regex(@"^Floor (\d+)$");
2023-02-15 22:17:19 +00:00
}
}