ModernUO/Projects/UOContent/Engines/ConPVP/AcceptDuelGump.cs
Kamron Batman a74c7f9d4e
perf: Migrates ConPVP lobby gumps from legacy Gump. (#2423)
## Summary

Migrates 14 ConPVP lobby/tournament gumps from legacy `Gump` to modern `DynamicGump`/`StaticGump<T>`. Layouts move into `BuildLayout(ref DynamicGumpBuilder)`, constructors become private, and validation moves into static `DisplayTo` entry points (empty-gump rule).

**Per-gump base type decisions:**

- `BeginGump` → `StaticGump<BeginGump>`: layout is fully fixed (no dynamic content). All other gumps below are `DynamicGump` because they bake dynamic player names, guild abbreviations, ruleset titles, arena names, tournament participant names, ladder rankings, or per-instance rule modifications. Per the cliloc/dynamic-text rule, dynamic content forces `DynamicGump`.
- `ReadyGump`, `ReadyUpGump` → `DynamicGump` (per-instance participant rosters).
- `AcceptDuelGump`, `AcceptTeamGump`, `ConfirmSignupGump` → `DynamicGump` (challenger/registrar/team names, dynamic rule modifications).
- `PickRulesetGump`, `RulesetGump` → `DynamicGump` (ruleset titles and option labels per instance).
- `ParticipantGump`, `DuelContextGump` → `DynamicGump` (player rosters/team labels).
- `LadderGump` → `DynamicGump` (ladder entries: ranks, levels, guild abbrs, names, wins/losses).
- `ArenaGump` → `DynamicGump` (arena names with active player names).
- `PreferencesGump` → `DynamicGump` (arena name list).
- `TournamentBracketGump` (~1k LOC) → `DynamicGump`. The whole gump is one type-switched view that re-renders on every button press across `Index`, `Rules_Info`, `Participant_List`, `Participant_Info`, `Round_List`, `Round_Info`, `Match_Info`, `Player_Info`. All branches bake per-instance content.

**Refresh-via-this conversions (the big perf wins):**

- `LadderGump`: page +/- now mutates `_page` and calls `from.SendGump(this)` instead of allocating a new `LadderGump`.
- `PickRulesetGump`: ruleset apply / flavor toggle now refreshes via `this`.
- `ParticipantGump`: increase/decrease team size, remove player, target failure all refresh via `this`.
- `DuelContextGump`: failed-start and add-participant refresh via `this`.
- `ConfirmSignupGump`: every signup-validation rejection branch in `OnResponse` and every `AddPlayer_OnTarget` rejection branch refreshes via `this` (was allocating a new gump per branch).
- `TournamentBracketGump`: every navigation button (back/forward, type change, page change, drill-down) mutates `_type`/`_object`/`_list`/`_page` and refreshes via `this`. Previously each click allocated a new 1k LOC gump.

All gumps are `Singleton`, use private constructors with static `DisplayTo` entry points that null-check `NetState` before allocation. External callers in `DuelContext`, `TournamentBracketItem`, `TournamentController`, `TournamentSignupItem`, and the cross-references between `AcceptDuelGump`/`ParticipantGump`/`AcceptTeamGump`/`ConfirmSignupGump` are all updated to use `DisplayTo`. Legacy `m_X` fields renamed to `_x` per coding standards.
2026-05-03 01:06:04 -07:00

322 lines
10 KiB
C#

using System;
using System.Collections.Generic;
using Server.Gumps;
using Server.Mobiles;
using Server.Network;
namespace Server.Engines.ConPVP;
public class AcceptDuelGump : DynamicGump
{
private const int LabelColor32 = 0xFFFFFF;
private const int BlackColor32 = 0x000008;
private static readonly Dictionary<Mobile, List<IgnoreEntry>> _ignoreLists = new();
private readonly Mobile _challenged;
private readonly Mobile _challenger;
private readonly DuelContext _context;
private readonly Participant _participant;
private readonly int _slot;
private bool _active = true;
public override bool Singleton => true;
private AcceptDuelGump(Mobile challenger, Mobile challenged, DuelContext context, Participant p, int slot)
: base(50, 50)
{
_challenger = challenger;
_challenged = challenged;
_context = context;
_participant = p;
_slot = slot;
Timer.StartTimer(TimeSpan.FromSeconds(15.0), AutoReject);
}
public static void DisplayTo(Mobile challenger, Mobile challenged, DuelContext context, Participant p, int slot)
{
if (challenged?.NetState == null || challenger == null || context == null || p == null)
{
return;
}
challenged.SendGump(new AcceptDuelGump(challenger, challenged, context, p, slot));
}
protected override void BuildLayout(ref DynamicGumpBuilder builder)
{
builder.SetNoClose();
builder.AddPage();
builder.AddBackground(1, 1, 398, 218, 3600);
builder.AddImageTiled(16, 15, 369, 189, 3604);
builder.AddAlphaRegion(16, 15, 369, 189);
builder.AddImage(215, -43, 0xEE40);
var duelChallengeBlack = "Duel Challenge".Center(BlackColor32);
builder.AddHtml(22 - 1, 22, 294, 20, duelChallengeBlack);
builder.AddHtml(22 + 1, 22, 294, 20, duelChallengeBlack);
builder.AddHtml(22, 22 - 1, 294, 20, duelChallengeBlack);
builder.AddHtml(22, 22 + 1, 294, 20, duelChallengeBlack);
builder.AddHtml(22, 22, 294, 20, "Duel Challenge".Center(LabelColor32));
var accept = _participant.Contains(_challenger)
? $"You have been asked to join sides with {_challenger.Name} in a duel. Do you accept?"
: $"You have been challenged to a duel from {_challenger.Name}. Do you accept?";
var acceptBlack = accept.Color(BlackColor32);
builder.AddHtml(22 - 1, 50, 294, 40, acceptBlack);
builder.AddHtml(22 + 1, 50, 294, 40, acceptBlack);
builder.AddHtml(22, 50 - 1, 294, 40, acceptBlack);
builder.AddHtml(22, 50 + 1, 294, 40, acceptBlack);
builder.AddHtml(22, 50, 294, 40, accept.Color(0xB0C868));
builder.AddImageTiled(32, 88, 264, 1, 9107);
builder.AddImageTiled(42, 90, 264, 1, 9157);
var yesBlack = "Yes, I will fight this duel.".Color(BlackColor32);
builder.AddRadio(24, 100, 9727, 9730, true, 1);
builder.AddHtml(60 - 1, 105, 250, 20, yesBlack);
builder.AddHtml(60 + 1, 105, 250, 20, yesBlack);
builder.AddHtml(60, 105 - 1, 250, 20, yesBlack);
builder.AddHtml(60, 105 + 1, 250, 20, yesBlack);
builder.AddHtml(60, 105, 250, 20, "Yes, I will fight this duel.".Color(LabelColor32));
var noBlack = "No, I do not wish to fight.".Color(BlackColor32);
builder.AddRadio(24, 135, 9727, 9730, false, 2);
builder.AddHtml(60 - 1, 140, 250, 20, noBlack);
builder.AddHtml(60 + 1, 140, 250, 20, noBlack);
builder.AddHtml(60, 140 - 1, 250, 20, noBlack);
builder.AddHtml(60, 140 + 1, 250, 20, noBlack);
builder.AddHtml(60, 140, 250, 20, "No, I do not wish to fight.".Color(LabelColor32));
var noAskBlack = "No, knave. Do not ask again.".Color(BlackColor32);
builder.AddRadio(24, 170, 9727, 9730, false, 3);
builder.AddHtml(60 - 1, 175, 250, 20, noAskBlack);
builder.AddHtml(60 + 1, 175, 250, 20, noAskBlack);
builder.AddHtml(60, 175 - 1, 250, 20, noAskBlack);
builder.AddHtml(60, 175 + 1, 250, 20, noAskBlack);
builder.AddHtml(60, 175, 250, 20, "No, knave. Do not ask again.".Color(LabelColor32));
builder.AddButton(314, 173, 247, 248, 1);
}
public void AutoReject()
{
if (!_active)
{
return;
}
_active = false;
_challenged.CloseGump<AcceptDuelGump>();
_challenger.SendMessage($"{_challenged.Name} seems unresponsive.");
_challenged.SendMessage("You decline the challenge.");
}
public static void BeginIgnore(Mobile source, Mobile toIgnore)
{
if (!_ignoreLists.TryGetValue(source, out var list))
{
_ignoreLists[source] = list = new List<IgnoreEntry>();
}
for (var i = 0; i < list.Count; ++i)
{
var ie = list[i];
if (ie.Ignored == toIgnore)
{
ie.Refresh();
return;
}
if (ie.Expired)
{
list.RemoveAt(i--);
}
}
list.Add(new IgnoreEntry(toIgnore));
}
public static bool IsIgnored(Mobile source, Mobile check)
{
if (!_ignoreLists.TryGetValue(source, out var list))
{
return false;
}
for (var i = 0; i < list.Count; ++i)
{
var ie = list[i];
if (ie.Expired)
{
list.RemoveAt(i--);
}
else if (ie.Ignored == check)
{
return true;
}
}
return false;
}
public override void OnResponse(NetState sender, in RelayInfo info)
{
if (info.ButtonID != 1 || !_active || !_context.Registered)
{
return;
}
_active = false;
if (!_context.Participants.Contains(_participant))
{
return;
}
if (info.IsSwitched(1))
{
if (_challenged is not PlayerMobile pm)
{
return;
}
if (pm.DuelContext != null)
{
if (pm.DuelContext.Initiator == pm)
{
pm.SendMessage(0x22, "You have already started a duel.");
}
else
{
pm.SendMessage(0x22, "You have already been challenged in a duel.");
}
_challenger.SendMessage($"{pm.Name} cannot fight because they are already assigned to another duel.");
}
else if (DuelContext.CheckCombat(pm))
{
pm.SendMessage(
0x22,
"You have recently been in combat with another player and must wait before starting a duel."
);
_challenger.SendMessage(
$"{pm.Name} cannot fight because they have recently been in combat with another player."
);
}
else if (TournamentController.IsActive)
{
pm.SendMessage(0x22, "A tournament is currently active and you may not duel.");
_challenger.SendMessage(0x22, "A tournament is currently active and you may not duel.");
}
else
{
var added = false;
if (_slot >= 0 && _slot < _participant.Players.Length && _participant.Players[_slot] == null)
{
added = true;
_participant.Players[_slot] = new DuelPlayer(_challenged, _participant);
}
else
{
for (var i = 0; i < _participant.Players.Length; ++i)
{
if (_participant.Players[i] == null)
{
added = true;
_participant.Players[i] = new DuelPlayer(_challenged, _participant);
break;
}
}
}
if (added)
{
_challenger.SendMessage($"{_challenged.Name} has accepted the request.");
_challenged.SendMessage($"You have accepted the request from {_challenger.Name}.");
foreach (var g in _challenger.GetGumps())
{
if (g is ParticipantGump pg && pg.Participant == _participant)
{
ParticipantGump.DisplayTo(_challenger, _context, _participant);
break;
}
if (g is DuelContextGump dcg && dcg.Context == _context)
{
DuelContextGump.DisplayTo(_challenger, _context);
break;
}
}
}
else
{
_challenger.SendMessage(
$"The participant list was full and so {_challenged.Name} could not join."
);
if (_participant.Contains(_challenger))
{
_challenged.SendMessage($"The participant list was full and so you could not join the fight with {_challenger.Name}.");
}
else
{
_challenged.SendMessage($"The participant list was full and so you could not join the fight against {_challenger.Name}.");
}
}
}
}
else
{
if (info.IsSwitched(3))
{
BeginIgnore(_challenged, _challenger);
}
_challenger.SendMessage($"{_challenged.Name} does not wish to fight.");
if (_participant.Contains(_challenger))
{
_challenged.SendMessage($"You chose not to fight with {_challenger.Name}.");
}
else
{
_challenged.SendMessage($"You chose not to fight against {_challenger.Name}.");
}
}
}
private class IgnoreEntry
{
private static readonly TimeSpan ExpireDelay = TimeSpan.FromMinutes(15.0);
private DateTime _expire;
public IgnoreEntry(Mobile ignored)
{
Ignored = ignored;
Refresh();
}
public Mobile Ignored { get; }
public bool Expired => Core.Now >= _expire;
public void Refresh() => _expire = Core.Now + ExpireDelay;
}
}