using System;
using System.Globalization;
using System.Text.RegularExpressions;
namespace RynthCore.Plugin.RynthAi.Meta;
/// What one chat line is, as far as /myquests goes.
internal enum MyQuestsLineKind
{
/// Not a /myquests line.
None,
/// One quest: .
Quest,
/// "Quest list is empty." - the whole (empty) reply.
Empty,
/// The server has /myquests turned off.
Disabled,
/// GDLe-style "This command may only be run once every ..." (ACE never sends it).
RateLimited,
}
/// One /myquests line. Times are the server's unix seconds.
internal readonly record struct MyQuestsEntry(
string Name, int Solves, long LastSolvedUnix, string Message, int MaxSolves, long MinDeltaSeconds)
{
/// ACE: a quest with a solve limit (MaxSolves > -1) that has reached it can never be solved again.
public bool AtSolveLimit => MaxSolves > -1 && Solves >= MaxSolves;
/// When the timer runs out (last solve + min delta); MaxValue at the solve limit.
public DateTime ReadyAtUtc => AtSolveLimit
? DateTime.MaxValue
: DateTime.UnixEpoch.AddSeconds((double)LastSolvedUnix + MinDeltaSeconds);
/// ACE QuestManager.GetNextSolveTime == MinValue: not at the limit and the timer has run out.
public bool IsReady(DateTime utcNow) => !AtSolveLimit && utcNow >= ReadyAtUtc;
}
///
/// Parses ACE's /myquests reply (ACE.Server PlayerCommands.HandleQuests, "GDLe formatting
/// to match plugin expectations"). One Broadcast line per quest:
/// {name lower} - {solves} solves ({last solved unix})"{quest message}" {max solves} {min delta}
/// e.g. stipendtimer_monthly - 2 solves (1759276800)"Monthly stipend" -1 2592000.
/// The min delta is already scaled by the server's quest_mindelta_rate.
///
internal static class MyQuestsParser
{
// The name is lowercase and has no spaces in practice; allow anything up to " - N solves (".
// The message is greedy up to the last '" ' so a quote inside it can't cut it short.
private static readonly Regex QuestLine = new(
"^(?\\S.*?) - (?-?\\d+) solves \\((?\\d+)\\)\"(?.*)\" (?-?\\d+) (?\\d+)$",
RegexOptions.CultureInvariant);
public const string EmptyText = "Quest list is empty.";
public const string DisabledText = "The command \"myquests\" is not currently enabled on this server.";
public static MyQuestsLineKind Parse(string? text, out MyQuestsEntry entry)
{
entry = default;
if (string.IsNullOrEmpty(text)) return MyQuestsLineKind.None;
string line = text.Trim();
if (line.Length == 0) return MyQuestsLineKind.None;
if (line.Equals(EmptyText, StringComparison.Ordinal)) return MyQuestsLineKind.Empty;
if (line.Equals(DisabledText, StringComparison.Ordinal)) return MyQuestsLineKind.Disabled;
if (line.StartsWith("This command may only be run once every", StringComparison.Ordinal)) return MyQuestsLineKind.RateLimited;
// Cheap pre-check before the regex: every quest line has " solves (".
if (line.IndexOf(" solves (", StringComparison.Ordinal) < 0) return MyQuestsLineKind.None;
Match m = QuestLine.Match(line);
if (!m.Success) return MyQuestsLineKind.None;
if (!int.TryParse(m.Groups["solves"].Value, NumberStyles.AllowLeadingSign, CultureInfo.InvariantCulture, out int solves)
|| !long.TryParse(m.Groups["last"].Value, NumberStyles.None, CultureInfo.InvariantCulture, out long last)
|| !int.TryParse(m.Groups["max"].Value, NumberStyles.AllowLeadingSign, CultureInfo.InvariantCulture, out int max)
|| !long.TryParse(m.Groups["delta"].Value, NumberStyles.None, CultureInfo.InvariantCulture, out long delta))
return MyQuestsLineKind.None;
entry = new MyQuestsEntry(m.Groups["name"].Value.Trim(), solves, last, m.Groups["msg"].Value, max, delta);
return MyQuestsLineKind.Quest;
}
}