using System; using System.Collections.Generic; using System.IO; namespace RynthCore.Plugin.RynthOracle.Data; /// /// One Legendary Key quest (legendary.csv; list and notes from the "Arwic John Route" sheet). /// The quest's own /myquests flags say whether it was ever done and when it can be run again: /// the first flag is its completion stamp, any others only add their repeat timers (a key pickup /// timer, say). John in Arwic counts 30 different ones (legendaryquestcounter_0913). /// internal sealed class LegendaryQuest { public string Group = ""; public string Name = ""; /// The short name players use ("RTW", "HMD"), or "". public string CommonName = ""; public int Luminance; public string[] Flags = Array.Empty(); public string Url = ""; public string Note = ""; public string Video = ""; /// The short name when the sheet has one, else the quest name. public string Label => CommonName.Length > 0 ? $"{Name} ({CommonName})" : Name; /// Done at least once: any of its flags is on this character. public bool Have(QuestFlagStore flags) { foreach (string f in Flags) if (flags.TryGet(f, out _)) return true; return false; } /// /// Time until it can be run again: the longest wait among its flags that have a repeat timer; /// zero when every timer is up (or it has none). Null when it was never done. /// public TimeSpan? TimeLeft(QuestFlagStore flags, DateTime utcNow) { bool any = false; TimeSpan left = TimeSpan.Zero; foreach (string f in Flags) { if (!flags.TryGet(f, out QuestFlag q)) continue; any = true; if (q.RepeatTime <= TimeSpan.Zero || q.CompletedOnUtc == DateTime.MinValue) continue; TimeSpan d = q.NextAvailableTime(utcNow); if (d > left) left = d; } return any ? left : null; } /// "missing", "ready" or the time left ("14h 3m"). public string Status(QuestFlagStore flags, DateTime utcNow) { TimeSpan? left = TimeLeft(flags, utcNow); if (left == null) return "missing"; return left.Value.TotalSeconds > 0 ? TimeText.Friendly(left.Value) : "ready"; } } internal static class LegendaryList { /// John's count of different Legendary Key quests done (needs 30). public const string CounterFlag = "legendaryquestcounter_0913"; /// Legendary Key quests John asks for. public const int JohnTarget = 30; public static List All { get; } = Load(); /// The groups in list order (Graveyard, Rynthid, ...). public static List Groups { get; } = BuildGroups(); /// John's counter for this character; null when /myquests doesn't have it (not started). public static int? JohnCount(QuestFlagStore flags) => flags.TryGet(CounterFlag, out QuestFlag q) ? q.Solves : null; private static List Load() { try { using StreamReader r = Csv.OpenEmbedded("legendary.csv"); return Parse(r); } catch (Exception) { // A missing or unreadable list must not take the Quests page down with it. return new List(); } } /// /// Reads a legendary.csv (a header row, then one quest a row). A row without a quest name is /// skipped; a missing column or a luminance that isn't a number reads as empty / 0, so one bad /// line never costs the rest of the list. Internal for the tests. /// internal static List Parse(TextReader r) { var list = new List(); Dictionary col = Csv.MapColumns(r.ReadLine() ?? ""); int cGroup = Csv.Column(col, "Group"), cName = Csv.Column(col, "Quest"), cCommon = Csv.Column(col, "CommonName"), cLum = Csv.Column(col, "Luminance"), cFlags = Csv.Column(col, "Flags"), cUrl = Csv.Column(col, "Url"), cNote = Csv.Column(col, "Note"), cVideo = Csv.Column(col, "Video"); while (r.ReadLine() is string line) { if (string.IsNullOrWhiteSpace(line)) continue; string[] f = Csv.ParseLine(line); string name = Csv.Field(f, cName); if (name.Length == 0) continue; list.Add(new LegendaryQuest { Group = Csv.Field(f, cGroup), Name = name, CommonName = Csv.Field(f, cCommon), Luminance = int.TryParse(Csv.Field(f, cLum), out int lum) ? lum : 0, Flags = Csv.Field(f, cFlags).ToLowerInvariant() .Split(';', StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries), Url = Csv.Field(f, cUrl), Note = Csv.Field(f, cNote), Video = Csv.Field(f, cVideo), }); } return list; } private static List BuildGroups() { var groups = new List(); foreach (LegendaryQuest q in All) if (!groups.Contains(q.Group)) groups.Add(q.Group); return groups; } }