Give queue review fixes: failed reads, chat attribution, busy loop, counted gives

- F1: stack reads use a -1 sentinel (no clamp); a failed read is no information.
  Unreadable item (no cache entry / Container 0) must repeat on 2 ticks to be gone.
  SendGive records max(1, amount) on a failed read and sends an explicit amount.
- F2: speech/tell lines (chat type 17 or text shape) never answer a give;
  Item not found counts as given only if the item left the pack.
- F3: busy log throttled (1st, then every 10th); chat notice every ~10 s of busy.
- F4: up to 5 unclassified chat lines logged per give.
- F5: single-stack counted gives go through the queue.
- F7/F8: dead fields removed, stale comment fixed, Interlocked verdict handoff,
  _giveRedo snapshotted.
- F9: saved GiveQueueIntervalMs of 150 migrates to 0 on profile load.
- Tests for each.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
This commit is contained in:
Daniel Wallace 2026-10-12 18:56:56 -07:00
commit 620048211b
5 changed files with 271 additions and 49 deletions

View file

@ -34,7 +34,14 @@ internal static class GiveTests
r.Add("give: a permanent refusal skips the stack at once and moves on", PermanentSkips);
r.Add("give: item not found after a re-send counts as given", ItemNotFoundAfterResendCounts);
r.Add("give: the summary counts items, not stacks", SummaryCountsItems);
r.Add("give: no answer at all re-sends once, then skips", NoAnswerSkips); }
r.Add("give: no answer at all re-sends once, then skips", NoAnswerSkips);
r.Add("give: an unreadable stack count is no information, not a shrink", UnreadableStackIsNotAShrink);
r.Add("give: an item unreadable for one tick is not gone; for two it is", OneTickUnreadableIsNotGone);
r.Add("give: speech lines never answer a give; item not found with the stack unchanged is a skip", SpeechIgnoredAndNotFoundNeedsGone);
r.Add("give: busy notice after 10 s, busy logging throttled, unclassified lines logged (max 5)", BusyNoticeAndLogThrottle);
r.Add("give: a counted give from one stack goes through the queue (NPC one per give, player split once)", CountedSingleStack);
r.Add("give: cancel in the middle of a busy wait stops everything", CancelMidBusy);
r.Add("give: a saved interval of 150 migrates to 0, other values stay", IntervalMigration); }
private sealed class World
{
@ -94,7 +101,7 @@ internal static class GiveTests
/// <summary>Advances the clock, then runs one tick of the give queue.</summary>
public void Tick(long advanceMs = 0) { Now += advanceMs; Call("DrainGiveQueue"); }
public void Chat(string text) => Call("NoteGiveChat", text);
public void Chat(string text, int chatType = 0) => Call("NoteGiveChat", text, chatType);
/// <summary>Adds a carried stack the cache knows about.</summary>
public void AddStack(uint id, string name, int count)
@ -262,6 +269,7 @@ internal static class GiveTests
w.Tick();
w.Tick(3001); // no answer: sent once more
Check.Eq(FakeHost.Gives.Count, 2, "re-sent after the confirm time");
FakeHost.Containers[Mote1] = Npc; // the first send did land: the item is gone from the pack
w.Chat("Item not found!");
w.Tick();
Check.Eq(FakeHost.Gives.Count, 2, "no third send");
@ -291,5 +299,147 @@ internal static class GiveTests
Check.True(w.ChatHas("didn't take"), "and says so");
Check.True(w.ChatHas("1 stack(s) skipped"), "the summary counts it as skipped");
}
}
private static void UnreadableStackIsNotAShrink()
{
var w = new World();
FakeHost.GiveHandler = (_, _, _) => { }; // the server has not answered yet
w.AddStack(Carapace, "Olthoi Slayer Carapace", 5);
w.Call("EnqueueGive", Carapace, Npc, 5);
w.Tick();
Check.Eq(FakeHost.Gives.Count, 1, "sent");
FakeHost.Ints.Remove((Carapace, 12)); // the engine read fails for a tick
w.Tick();
Check.Eq(FakeHost.Gives.Count, 1, "a failed read is not a shrink of 4: no second give while the first is in flight");
FakeHost.Ints[(Carapace, 12)] = 5;
w.Tick();
Check.Eq(FakeHost.Gives.Count, 1, "read again, unchanged: still waiting");
Check.False(w.ChatHas("item(s) given"), "nothing credited");
FakeHost.Ints[(Carapace, 12)] = 4; // now a real shrink
w.Tick();
Check.Eq(FakeHost.Gives.Count, 2, "a real shrink gives again");
}
private static void OneTickUnreadableIsNotGone()
{
var w = new World();
FakeHost.GiveHandler = (_, _, _) => { };
w.Call("EnqueueGive", Mote1, Npc, 1);
w.Call("EnqueueGive", Mote2, Npc, 1);
w.Tick();
FakeHost.Containers[Mote1] = 0; // one unreadable tick
w.Tick();
Check.Eq(FakeHost.Gives.Count, 1, "Container 0 for one tick is not gone");
FakeHost.Containers[Mote1] = Player;
w.Tick();
FakeHost.Containers[Mote1] = 0;
w.Tick();
Check.Eq(FakeHost.Gives.Count, 1, "the streak restarted after a good read");
w.Tick();
Check.Eq(FakeHost.Gives.Count, 2, "unreadable on two ticks in a row: gone, the next stack goes");
}
private static void SpeechIgnoredAndNotFoundNeedsGone()
{
var w = new World();
FakeHost.GiveHandler = (_, _, _) => { };
w.Call("EnqueueGive", Mote1, Npc, 1);
w.Call("EnqueueGive", Mote2, Npc, 1);
w.Tick();
w.Chat("<Tell:IIDString:1:Bob>Bob<\\Tell> says, \"Town Crier doesn't want that\"", 17);
w.Chat("Bob says, \"Town Crier doesn't want that\"", 0); // shape alone is enough
w.Chat("Bob tells you, \"not accepting gifts\"", 3);
w.Tick();
Check.Eq(FakeHost.Gives.Count, 1, "other players' words are no answer: still in flight, nothing skipped");
Check.False(w.ChatHas("skipped"), "no summary yet");
w.Tick(3001); // re-sent
w.Chat("Item not found!");
w.Tick(); // the item is still carried, stack unchanged: a skip, not a delivery
Check.Eq(FakeHost.Gives.Count, 3, "item not found with the item still in the pack skips it; the next stack goes");
FakeHost.Containers[Mote2] = Npc;
w.Tick();
Check.True(w.ChatHas("1 item(s) given, 1 stack(s) skipped"), "one skipped as item not found, one given");
}
private static void BusyNoticeAndLogThrottle()
{
var w = new World();
FakeHost.GiveHandler = (_, _, _) => { };
w.Call("EnqueueGive", Mote1, Npc, 1);
w.Tick();
for (int i = 0; i < 6; i++) w.Chat("Something odd " + i);
Check.Eq(FakeHost.Logs.Count(l => l.Contains("[Give] unclassified chat:")), 5, "at most 5 unclassified lines logged per give");
int busyCycles = 0;
long start = w.Now;
while (w.Now - start < 9000)
{
w.Chat("You're too busy!");
w.Tick(0);
w.Tick(300);
busyCycles++;
}
Check.False(w.ChatHas("still retrying"), "no notice before 10 s of busy waiting");
while (w.Now - start < 10700)
{
w.Chat("You're too busy!");
w.Tick(0);
w.Tick(300);
busyCycles++;
}
Check.Eq(FakeHost.Chat.Count(c => c.Contains("Town Crier is busy - still retrying (/ra givea stop to cancel).")), 1, "one notice at about 10 s");
int busyLogs = FakeHost.Logs.Count(l => l.Contains("Give answer: busy"));
Check.True(busyCycles >= 30 && busyLogs <= busyCycles / 10 + 1, $"busy logging throttled: {busyLogs} lines for {busyCycles} busy answers");
Check.True(busyLogs >= 1, "the first busy is logged");
}
private static void CountedSingleStack()
{
var w = new World();
w.AddStack(Carapace, "Olthoi Slayer Carapace", 5);
w.NpcTakesOnePerGive();
w.Run("/ub give 3 Olthoi Slayer Carapace to Town Crier");
Check.Eq(FakeHost.Gives.Count, 3, "3 of 5 to a one-per-give NPC: three gives");
Check.Eq(FakeHost.Ints[(Carapace, 12)], 2, "two left in the pack");
Check.Eq(FakeHost.Containers[Carapace], Player, "the stack stays");
var w2 = new World();
const uint Friend = 0x50000F02;
FakeHost.Names[Friend] = "Friend";
FakeHost.Positions[Friend] = (0x1048002Au, 101f, 101f, 0f);
FakeHost.ItemTypes[Friend] = 0x10;
w2.Cache.OnCreateObject(Friend);
w2.Cache.Tick();
w2.AddStack(Carapace, "Olthoi Slayer Carapace", 5);
FakeHost.GiveHandler = (item, target, amount) =>
{
FakeHost.Ints[(item, 12)] -= amount == 0 ? FakeHost.Ints[(item, 12)] : amount; // a player takes what is asked
};
w2.Run("/ub give 3 Olthoi Slayer Carapace to Friend");
Check.Eq(FakeHost.Gives.Count, 1, "to a player: one give");
Check.Eq(FakeHost.Gives[0].Amount, 3, "carrying the count (the server splits)");
Check.Eq(FakeHost.Ints[(Carapace, 12)], 2, "two left");
}
private static void CancelMidBusy()
{
var w = new World();
FakeHost.GiveHandler = (_, _, _) => { };
w.Call("EnqueueGive", Mote1, Npc, 1);
w.Call("EnqueueGive", Mote2, Npc, 1);
w.Tick();
w.Chat("You're too busy!");
w.Tick(100); // busy seen, re-send armed
w.Call("CancelGiveQueue");
for (int i = 0; i < 20; i++) w.Tick(500);
Check.Eq(FakeHost.Gives.Count, 1, "nothing is sent after the cancel: no re-send, no next stack");
Check.True(w.ChatHas("Give queue cancelled"), "cancel reported");
}
private static void IntervalMigration()
{
Check.Eq(LegacyUiSettings.MigrateGiveQueueIntervalMs(150), 0, "the old default becomes 0");
Check.Eq(LegacyUiSettings.MigrateGiveQueueIntervalMs(400), 400, "a chosen value stays");
Check.Eq(LegacyUiSettings.MigrateGiveQueueIntervalMs(0), 0, "0 stays");
Check.Eq(new LegacyUiSettings().GiveQueueIntervalMs, 0, "the default is 0");
}
}