// Ported from Oracle of Dereth by Advis Eveldan (advis61), MIT. See THIRD_PARTY_NOTICES.md.
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Text.RegularExpressions;
using System.Threading;
namespace RynthCore.Plugin.RynthOracle.Data;
/// One quest flag the character holds, as /myquests reports it.
internal sealed class QuestFlag
{
public string Key = "";
public string Description = "";
public int Solves;
/// -1 = no limit (ACE).
public int MaxSolves = -1;
public DateTime CompletedOnUtc = DateTime.MinValue;
public TimeSpan RepeatTime = TimeSpan.Zero;
public TimeSpan NextAvailableTime(DateTime utcNow) => CompletedOnUtc + RepeatTime - utcNow;
/// ACE: a quest with a solve limit that has reached it can never be solved again.
public bool AtSolveLimit => MaxSolves > -1 && Solves >= MaxSolves;
public bool Ready(DateTime utcNow) => NextAvailableTime(utcNow).TotalSeconds <= 0;
public string NextAvailable(DateTime utcNow)
{
TimeSpan d = NextAvailableTime(utcNow);
return d.TotalSeconds > 0 ? TimeText.Friendly(d) : "ready";
}
}
/// What one chat line is, as far as /myquests goes.
internal enum MyQuestsLine { None, Quest, Empty, Disabled }
///
/// The character's quest flags, read from the server's /myquests reply (ACE: one Broadcast line
/// per flag, "name - N solves (unix)"message" maxSolves minDelta"). Every reply is the whole
/// list, so a burst of lines replaces the flags once it goes quiet, whoever asked: RynthOracle,
/// the player, or RynthAi's meta manager (which polls and hides its own replies).
///
/// Threads: everything runs on the plugin pump, except , which the
/// chat bar calls on AC's thread (it only touches interlocked fields).
///
internal sealed class QuestFlagStore
{
// Optional chat timestamp, then the flag line. Anchored so a pasted 'Bob says, "x - 1 solves (0)"'
// can't inject a flag (upstream's hardening). ACE puts no space between ")" and the quote.
private static readonly Regex LineRegex = new(
@"^\s*(?:(?:\d{1,2}:\d{2}(?::\d{2})?(?:\s*[AP]M)?\s+)|(?:\[[^\]]+\][\s:]*))*"
+ @"(?\S+) - (?-?\d+) solves \((?\d{0,11})\)\s*""?(?:(?.*)"" (?-?\d+) (?\d+))?\s*$",
RegexOptions.CultureInvariant | RegexOptions.IgnoreCase);
public const string EmptyText = "Quest list is empty.";
public const string DisabledText = "The command \"myquests\" is not currently enabled on this server.";
private const int QuietMs = 1500; // a reply is complete once no line came for this long
private const int RequestTimeoutMs = 10_000;
private const int MinRequestGapMs = 5_000;
public Dictionary Flags { get; private set; } = new(StringComparer.OrdinalIgnoreCase);
private Dictionary? _pending;
private long _lastLineMs;
public DateTime LastReadUtc { get; private set; } = DateTime.MinValue;
public bool ServerDisabled { get; private set; }
/// Bumps whenever the flags change (views rebuild their caches on it).
public int Revision { get; private set; }
// Our own request: its reply may be hidden from chat.
public bool AwaitingOurs { get; private set; }
private long _requestedMs = long.MinValue / 2;
private int _selfSends; // our "/myquests" sends not yet seen by the chat bar
private int _typedByPlayer; // the player typed /myquests (AC's thread sets it)
public bool Loading => _pending != null || AwaitingOurs;
/// Sends /myquests unless one went out moments ago. Returns false if nothing was sent.
public bool Request(Func invokeChat, long nowMs)
{
if (nowMs - _requestedMs < MinRequestGapMs) return false;
_requestedMs = nowMs;
Interlocked.Increment(ref _selfSends);
if (!invokeChat("/myquests"))
{
Interlocked.Decrement(ref _selfSends);
return false;
}
AwaitingOurs = true;
return true;
}
/// AC's thread (chat bar): a "/myquests" went through it. Ours, or typed by the player?
public void NoteTypedCommand()
{
if (Interlocked.Decrement(ref _selfSends) >= 0) return;
Interlocked.Exchange(ref _selfSends, 0);
Interlocked.Exchange(ref _typedByPlayer, 1);
}
/// Classifies a chat line and takes in its flag.
public MyQuestsLine OnChatLine(string? text, long nowMs)
{
if (string.IsNullOrEmpty(text)) return MyQuestsLine.None;
string line = text.Trim();
if (line.EndsWith(EmptyText, StringComparison.Ordinal))
{
_pending = new Dictionary(StringComparer.OrdinalIgnoreCase);
_lastLineMs = nowMs - QuietMs; // nothing follows it
ServerDisabled = false;
return MyQuestsLine.Empty;
}
if (line.EndsWith(DisabledText, StringComparison.Ordinal))
{
ServerDisabled = true;
AwaitingOurs = false;
Revision++;
return MyQuestsLine.Disabled;
}
if (line.IndexOf(" solves (", StringComparison.Ordinal) < 0) return MyQuestsLine.None;
QuestFlag? flag = Parse(line);
if (flag == null) return MyQuestsLine.None;
_pending ??= new Dictionary(StringComparer.OrdinalIgnoreCase);
_pending[flag.Key] = flag;
_lastLineMs = nowMs;
ServerDisabled = false;
return MyQuestsLine.Quest;
}
/// Pump tick: completes a reply once it has gone quiet.
public void Tick(long nowMs)
{
if (Interlocked.Exchange(ref _typedByPlayer, 0) != 0)
AwaitingOurs = false; // the player asked too: their reply shows in chat
if (_pending != null && nowMs - _lastLineMs >= QuietMs)
{
Flags = _pending;
_pending = null;
LastReadUtc = DateTime.UtcNow;
AwaitingOurs = false;
Revision++;
}
else if (AwaitingOurs && _pending == null && nowMs - _requestedMs > RequestTimeoutMs)
{
AwaitingOurs = false; // no reply (not in world, server busy)
}
}
public void Clear()
{
Flags = new Dictionary(StringComparer.OrdinalIgnoreCase);
_pending = null;
AwaitingOurs = false;
LastReadUtc = DateTime.MinValue;
ServerDisabled = false;
Revision++;
}
public bool TryGet(string flag, out QuestFlag q) => Flags.TryGetValue(flag, out q!);
internal static QuestFlag? Parse(string line)
{
Match m = LineRegex.Match(line);
if (!m.Success) return null;
var q = new QuestFlag
{
Key = m.Groups["key"].Value.ToLowerInvariant(),
Description = m.Groups["desc"].Value.Trim().Trim('"').Trim(),
};
int.TryParse(m.Groups["solves"].Value, NumberStyles.AllowLeadingSign, CultureInfo.InvariantCulture, out q.Solves);
if (m.Groups["max"].Success && int.TryParse(m.Groups["max"].Value, NumberStyles.AllowLeadingSign, CultureInfo.InvariantCulture, out int max))
q.MaxSolves = max;
if (long.TryParse(m.Groups["completed"].Value, NumberStyles.None, CultureInfo.InvariantCulture, out long completed) && completed > 0)
q.CompletedOnUtc = DateTime.UnixEpoch.AddSeconds(completed);
if (m.Groups["delta"].Success && long.TryParse(m.Groups["delta"].Value, NumberStyles.None, CultureInfo.InvariantCulture, out long delta))
q.RepeatTime = TimeSpan.FromSeconds(Math.Min(delta, (long)TimeSpan.MaxValue.TotalSeconds / 2));
return q;
}
}