ModernUO/Projects/UOContent/Engines/Bulk Orders/LargeBOD.cs
Kamron Batman ecbee17690
fix: Optimizes OPL using string interpolation (#1041)
## Breaking Changes (New API)
ObjectPropertyList supports the following API:
```cs
list.Add(500000);
list.Add(500001, stringArgument);
list.Add("Some text");
list.Add($"Some text with {argument}");
list.Add(500002, $"{arg1}\t{arg2}");
```

## Notes
1. All API uses that require a formatter like this:
    ```cs
    list.Add(500002, "{0}\t{1}", arg1, arg2);
    ```
    Should be changed to use string interpolation, for example:
    ```cs
    list.Add(500002, $"{arg1}\t{arg2}");
    ```
2. The following paradigm should no longer be used:
    ```cs
    list.Add(1061170, prop.ToString()); // strength requirement ~1_val~
    ```
    The new string interpolation API will avoid having to convert the argument to a string before writing it to the packet. Instead use the following:
    ```cs
    list.Add(1061170, $"{prop}"); // strength requirement ~1_val~
    ```

### Benchmarks
```cs
|                         Method |     Mean |   Error |  StdDev |  Gen 0 | Allocated |
|------------------------------- |---------:|--------:|--------:|-------:|----------:|
|                BenchmarkOldOPL | 241.0 ns | 0.56 ns | 0.47 ns | 0.0105 |      88 B |
| BenchmarkStringInterpolatedOPL | 199.9 ns | 2.44 ns | 2.39 ns |      - |         - |
```

### Changes
- [X] Removes crash in STArray.Return when array is null.
- [X] Fixes NPE in OPL when entity is null. Serial in packet will be 0 when entity is null.
- [X] Fixes NPE in AosAttributes when Parent is null.
- [X] Changes OPL to use string interpolation.
- [X] Introduces `IPropertyList` to allow extending PropertyList for other uses.
2022-06-02 10:09:53 -07:00

163 lines
5.2 KiB
C#

using ModernUO.Serialization;
using Server.Mobiles;
namespace Server.Engines.BulkOrders
{
[SerializationGenerator(1)]
public abstract partial class LargeBOD : BaseBOD
{
[InvalidateProperties]
[SerializableField(0)]
private LargeBulkEntry[] _entries;
public LargeBOD(
int hue, int amountMax, bool requireExeptional, BulkMaterialType material, LargeBulkEntry[] entries
) : base(hue, amountMax, requireExeptional, material) =>
_entries = entries;
public LargeBOD()
{
}
[CommandProperty(AccessLevel.GameMaster)]
public override bool Complete
{
get
{
for (var i = 0; i < _entries.Length; ++i)
{
if (_entries[i].Amount < AmountMax)
{
return false;
}
}
return true;
}
}
public override int LabelNumber => 1045151; // a bulk order deed
public override void GetProperties(IPropertyList list)
{
base.GetProperties(list);
list.Add(1060655); // large bulk order
if (RequireExceptional)
{
list.Add(1045141); // All items must be exceptional.
}
if (Material != BulkMaterialType.None)
{
list.Add(LargeBODGump.GetMaterialNumberFor(Material)); // All items must be made with x material.
}
list.Add(1060656, $"{AmountMax}"); // amount to make: ~1_val~
for (var i = 0; i < _entries.Length; ++i)
{
var entry = _entries[i];
list.Add(1060658 + i, $"#{entry.Details.Number}\t{entry.Amount}"); // ~1_val~: ~2_val~
}
}
public override void OnDoubleClickNotAccessible(Mobile from)
{
OnDoubleClick(from);
}
public override void OnDoubleClickSecureTrade(Mobile from)
{
OnDoubleClick(from);
}
public override void OnDoubleClick(Mobile from)
{
if (IsChildOf(from.Backpack) || InSecureTrade || RootParent is PlayerVendor)
{
from.SendGump(new LargeBODGump(from, this));
}
else
{
from.SendLocalizedMessage(1045156); // You must have the deed in your backpack to use it.
}
}
public override void EndCombine(Mobile from, Item item)
{
if (item is not SmallBOD small)
{
from.SendLocalizedMessage(1045159); // That is not a bulk order.
return;
}
LargeBulkEntry entry = null;
for (var i = 0; i < _entries.Length; ++i)
{
if (_entries[i].Details.Type == small.Type)
{
entry = _entries[i];
break;
}
}
if (entry == null)
{
from.SendLocalizedMessage(1045160); // That is not a bulk order for this large request.
}
else if (RequireExceptional && !small.RequireExceptional)
{
from.SendLocalizedMessage(1045161); // Both orders must be of exceptional quality.
}
else if (Material >= BulkMaterialType.DullCopper && Material <= BulkMaterialType.Valorite &&
small.Material != Material)
{
from.SendLocalizedMessage(1045162); // Both orders must use the same ore type.
}
else if (Material >= BulkMaterialType.Spined && Material <= BulkMaterialType.Barbed &&
small.Material != Material)
{
from.SendLocalizedMessage(1049351); // Both orders must use the same leather type.
}
else if (AmountMax != small.AmountMax)
{
// The two orders have different requested amounts and cannot be combined.
from.SendLocalizedMessage(1045163);
}
else if (small.AmountCur < small.AmountMax)
{
from.SendLocalizedMessage(1045164); // The order to combine with is not completed.
}
else if (entry.Amount >= AmountMax)
{
// The maximum amount of requested items have already been combined to this deed.
from.SendLocalizedMessage(1045166);
}
else
{
entry.Amount += small.AmountCur;
small.Delete();
from.SendLocalizedMessage(1045165); // The orders have been combined.
from.SendGump(new LargeBODGump(from, this));
if (!Complete)
{
BeginCombine(from);
}
}
}
private void Deserialize(IGenericReader reader, int version)
{
_entries = new LargeBulkEntry[reader.ReadInt()];
for (var i = 0; i < _entries.Length; i++)
{
_entries[i] = new LargeBulkEntry(reader, this);
}
}
}
}