using System;
using System.Collections.Generic;
using System.Text;
using RynthCore.Plugin.Shared;
namespace RynthOracle.Tests
{
///
/// The quest guide (2026-11-27; SilentKelpie's PR 12 idea, re-implemented): the "RynthServer.QuestGuide"
/// interface a server plugin serves its guide through, the guide's JSON, and RynthOracle's reader of it.
/// The server side runs in-process (ServerQuestGuideHost's table, as a server plugin would hand it out).
///
internal static partial class Program
{
/// A small guide in the shared format: two quests (one with steps and stamps), a kill task, a contract.
internal static QuestGuide SampleGuide()
{
var g = new QuestGuide { Source = "test wiki", UpdatedUtc = new DateTime(2026, 11, 27, 12, 0, 0, DateTimeKind.Utc) };
g.Quests.Add(new GuideQuest
{
Title = "Test Daily Errand", Url = "https://wiki.example/Test_Daily_Errand", Rev = 11, Level = "50+", Repeat = "20 Hours",
RepeatSec = 20 * 3600, StartsWith = "Sample Questgiver", StartsAt = "Sampletown",
Steps =
{
new GuideStep { Text = "Talk to Sample Questgiver", Coords = { "12.3N, 45.6E" }, Stamps = { "TestErrandStarted" } },
new GuideStep { Text = "Kill the Sample Beast and loot its Sample Fang", Items = { "Sample Fang" }, Stamps = { "TestErrandFang" } },
new GuideStep { Text = "Hand the fang in", Stamps = { "TestErrandDone" } },
},
Loot = { "Sample Fang" }, Rewards = { "Luminance: 1,000" },
});
g.Quests.Add(new GuideQuest { Title = "Test Story Quest", Overview = "A one-time quest." });
g.Quests.Add(new GuideQuest
{
Title = "Sample Beast Kill Task", KillTask = "Yes", RepeatSec = 20 * 3600, Repeat = "20 hours",
Steps = { new GuideStep { Text = "Kill 50 Sample Beasts", Stamps = { "KillTaskSampleBeasts_0101 on first completion" } } },
});
g.KillTasks.Add("Sample Beast Kill Task");
g.Society["Test Story Quest"] = "Initiate";
g.Contracts.Add(new GuideContract { Title = "Contract for Test Daily Errand", Name = "Contract for Test Daily Errand", Npc = "Sample Barkeep", Quests = { "Test Daily Errand" } });
return g;
}
private static unsafe void QuestGuideInterface()
{
// The guide's JSON round trip, stamps cut to their flags on the way in.
QuestGuide g = SampleGuide();
QuestGuide back = QuestGuide.Parse(g.ToJson());
Check(back.Quests.Count == 3 && back.Contracts.Count == 1 && back.KillTasks.Count == 1, "the guide reads back: quests, kill tasks, contracts");
GuideQuest? daily = back.Find("test daily errand");
Check(daily != null && daily.Steps.Count == 3 && daily.Steps[0].Coords[0] == "12.3N, 45.6E" && daily.RepeatSec == 72000 && daily.IsDaily,
"a quest's steps, coordinates and timer read back");
Check(back.Find("Sample Beast Kill Task")!.Steps[0].Stamps[0] == "KillTaskSampleBeasts_0101", "a stamp with words after it is cut to the flag");
Check(back.IsKillTask(back.Find("Sample Beast Kill Task")!) && !back.IsKillTask(daily!), "kill tasks by the summary box and the list");
Check(back.SocietyRankOf(back.Find("Test Story Quest")!) == "Initiate", "society rank from the guide's society list");
Check(QuestGuide.Parse("not json").IsEmpty && QuestGuide.Parse("{\"schema\":9,\"quests\":[{\"title\":\"x\"}]}").IsEmpty,
"junk or another schema is an empty guide");
Check(GuideStamp.Clean("You've stamped TestErrandFang!") == "TestErrandFang" && GuideStamp.Clean("Shiny Red Stone") == "Shiny Red Stone",
"a chat line's stamp, and a stamp of plain words left whole");
Check(GuideStamp.Key("Shiny_Red_Stone") == GuideStamp.Key("shiny red stone"), "stamp names compare alike");
// A server plugin serves it; the reader takes it through the table.
ServerQuestGuideHost.ResetForTests();
var table = (RynthServerQuestGuideApiV1*)ServerQuestGuideHost.Query(1);
Check(table != null && table->Version == 1 && ServerQuestGuideHost.Query(2) == IntPtr.Zero, "the table answers version 1 only");
var client = new ServerQuestGuideClient();
client.Use(table, "RynthTestServer");
Check(!client.Available && client.Guide.IsEmpty, "nothing published: no guide");
ServerQuestGuideHost.Publish("Test Server", "RynthTestServer", null, "reading the wiki...", running: true, RynthServerQuestGuide.CommandRefresh);
client.Use(table, "RynthTestServer");
Check(client.Available && client.Guide.IsEmpty && client.Info!.Running && client.Info.Status == "reading the wiki...",
"a guide still being read: its status, no quests");
ServerQuestGuideHost.Publish("Test Server", "RynthTestServer", g, "3 quests", running: false, RynthServerQuestGuide.CommandRefresh, RynthServerQuestGuide.CommandSettings);
int rev = client.Revision;
client.Use(table, "RynthTestServer");
Check(client.Guide.Quests.Count == 3 && client.Tag == "Test Server" && client.Revision != rev, "the published guide is read");
rev = client.Revision;
client.Use(table, "RynthTestServer");
Check(client.Revision == rev, "unchanged info: nothing read again");
Check(client.Command("refresh") && ServerQuestGuideHost.TryTakeCommand(out string word) && word == "refresh", "a command goes to the plugin's queue");
Check(!client.Command("delete-everything") && !ServerQuestGuideHost.TryTakeCommand(out _), "a word the plugin doesn't list isn't sent");
// A name outside ASCII survives the info JSON's escaping.
ServerQuestGuideHost.Publish("Café \"Q\"", "RynthTestServer", g, "ok", running: false);
client.Use(table, "RynthTestServer");
Check(client.Tag == "Café \"Q\"" && !client.Info!.Takes("refresh"), "the tag's escaping, and no commands listed");
ServerQuestGuideHost.Release();
Check(table->Version == 0, "shutdown: the table says so");
client.Use(table, "RynthTestServer");
Check(!client.Available && client.Guide.IsEmpty, "a shut-down plugin's guide goes away");
ServerQuestGuideHost.ResetForTests();
Check(ServerQuestGuideInfo.Parse("{\"schema\":1,\"bytes\":-5,\"commands\":[\"Refresh\",3]}") is { Bytes: 0 } i && i.Takes("refresh"),
"the info is read leniently");
Check(ServerQuestGuideInfo.Parse("{\"schema\":2}") == null && ServerQuestGuideInfo.Parse("[]") == null, "another schema, or junk, is no info");
}
}
}