132 lines
4.2 KiB
C#
132 lines
4.2 KiB
C#
using Dalamud.Plugin.Services;
|
|
|
|
namespace QuestShare.Services
|
|
{
|
|
internal class ShareService : IService
|
|
{
|
|
internal static List<Objects.ShareCode> ShareCodes => ConfigurationManager.Instance.KnownShareCodes;
|
|
internal List<Objects.Session> Sessions { get; private set; } = [];
|
|
internal Dictionary<long, string> CharacterLookup { get; private set; } = [];
|
|
|
|
public void Initialize()
|
|
{
|
|
ClientState.Login += OnLogin;
|
|
ClientState.Logout += OnLogout;
|
|
Framework.Update += OnFrameworkUpdate;
|
|
}
|
|
|
|
public void Shutdown()
|
|
{
|
|
OnLogout(0, 0);
|
|
ClientState.Login -= OnLogin;
|
|
ClientState.Logout -= OnLogout;
|
|
Framework.Update -= OnFrameworkUpdate;
|
|
CharacterLookup.Clear();
|
|
Sessions.Clear();
|
|
}
|
|
|
|
private void OnLogin()
|
|
{
|
|
CharacterLookup.Clear();
|
|
CharacterLookup.Add((long)ClientState.LocalContentId, ClientState.LocalPlayer!.Name.TextValue);
|
|
foreach (var character in ConfigurationManager.Instance.KnownCharacters)
|
|
{
|
|
CharacterLookup.Add(character.Key, character.Value);
|
|
}
|
|
}
|
|
|
|
private void OnLogout(int code, int state)
|
|
{
|
|
ConfigurationManager.Save();
|
|
CharacterLookup.Clear();
|
|
Sessions.Clear();
|
|
}
|
|
|
|
private int pCount = 0;
|
|
|
|
private void OnFrameworkUpdate(IFramework framework)
|
|
{
|
|
if (PartyList.Count != pCount)
|
|
{
|
|
foreach (var partyMember in PartyList)
|
|
{
|
|
AddKnownCharacter(partyMember.ContentId, partyMember.Name.TextValue);
|
|
}
|
|
pCount = PartyList.Count;
|
|
}
|
|
}
|
|
|
|
public static void SetActiveQuest(uint questId, byte questStep)
|
|
{
|
|
Log.Debug($"Setting active quest to {questId} - {questStep}");
|
|
HostService.Update((int)questId, questStep);
|
|
}
|
|
|
|
public void AddSession(Objects.Session session)
|
|
{
|
|
if (Sessions.Any(s => s.ShareCode == session.ShareCode))
|
|
{
|
|
return;
|
|
}
|
|
Sessions.Add(session);
|
|
AddKnownShareCode(new Objects.ShareCode() { Code = session.ShareCode, CharacterId = ClientState.LocalContentId.ToString().SaltedHash(session.ShareCode) });
|
|
}
|
|
|
|
public void RemoveSession(Objects.Session session)
|
|
{
|
|
Sessions.Remove(session);
|
|
RemoveKnownShareCode(session.ShareCode);
|
|
}
|
|
|
|
public void RemoveSession(string shareCode)
|
|
{
|
|
var session = Sessions.FirstOrDefault(s => s.ShareCode == shareCode);
|
|
if (session != null)
|
|
{
|
|
RemoveSession(session);
|
|
}
|
|
}
|
|
|
|
public void UpdateSession(Objects.Session session)
|
|
{
|
|
var existing = Sessions.FirstOrDefault(s => s.ShareCode == session.ShareCode);
|
|
if (existing != null)
|
|
{
|
|
Sessions.Remove(existing);
|
|
}
|
|
Sessions.Add(session);
|
|
}
|
|
|
|
public static void AddKnownShareCode(Objects.ShareCode shareCode)
|
|
{
|
|
if (ConfigurationManager.Instance.KnownShareCodes.Any(sc => sc.Code == shareCode.Code))
|
|
{
|
|
return;
|
|
}
|
|
ConfigurationManager.Instance.KnownShareCodes.Add(shareCode);
|
|
}
|
|
|
|
public static void RemoveKnownShareCode(string shareCode)
|
|
{
|
|
ConfigurationManager.Instance.KnownShareCodes.RemoveAll(sc => sc.Code == shareCode);
|
|
}
|
|
|
|
public static void AddKnownCharacter(long contentId, string characterId)
|
|
{
|
|
if (ConfigurationManager.Instance.KnownCharacters.ContainsKey(contentId))
|
|
{
|
|
return;
|
|
}
|
|
ConfigurationManager.Instance.KnownCharacters.Add(contentId, characterId);
|
|
ConfigurationManager.Save();
|
|
}
|
|
|
|
public static void RemoveKnownCharacter(long contentId)
|
|
{
|
|
ConfigurationManager.Instance.KnownCharacters.Remove(contentId);
|
|
ConfigurationManager.Save();
|
|
}
|
|
}
|
|
|
|
}
|