ModernUO/Projects/Server.Tests/Network/Packets/Old/Outgoing/VendorBuyPacketTests.cs
Kamron Batman 1ca8655fc1
Updates Serialization (#292)
- [X] Removes all save strategies
- [X] Removes duplicate file writer that won't be used
- [X] Removes persistence (it will become a duplicate system)
- [X] Add a save position variable to skip serializing a clean item/mobile
- [X] Update Guilds/Accounts to be IEntity types
    - Because guilds are abstract, this may make serialization tricky.
- [X] Update the generalized IEntity writing
- [X] Update the load/save to write to buffers then to files in background


Bumps release version
2020-10-31 17:38:29 -07:00

198 lines
6.8 KiB
C#

using System;
using System.Buffers;
using System.Collections.Generic;
using System.Linq;
using Server.Items;
using Server.Network;
using Xunit;
namespace Server.Tests.Network
{
public class VendorBuyPacketTests : IClassFixture<ServerFixture>
{
[Fact]
public void TestVendorBuyContent()
{
var cont = new Container(World.NewItem);
var buyStates = new List<BuyItemState>
{
new BuyItemState("First Item", cont.Serial, World.NewItem, 10, 1, 0x01, 0),
new BuyItemState("Second Item", cont.Serial, World.NewItem, 20, 2, 0x0A, 0),
new BuyItemState("Third Item", cont.Serial, World.NewItem, 30, 10, 0x0F, 0)
};
var data = new VendorBuyContent(buyStates).Compile();
Span<byte> expectedData = stackalloc byte[5 + buyStates.Count * 19];
var pos = 0;
expectedData.Write(ref pos, (byte)0x3C); // Packet ID
expectedData.Write(ref pos, (ushort)expectedData.Length); // Length
expectedData.Write(ref pos, (ushort)buyStates.Count); // Count
for (var i = buyStates.Count - 1; i >= 0; i--)
{
var buyState = buyStates[i];
expectedData.Write(ref pos, buyState.MySerial);
expectedData.Write(ref pos, (ushort)buyState.ItemID);
pos++; // ItemID Offset
expectedData.Write(ref pos, (ushort)buyState.Amount);
expectedData.Write(ref pos, (ushort)(i + 1)); // X
expectedData.Write(ref pos, (ushort)1); // Y
expectedData.Write(ref pos, buyState.ContainerSerial);
expectedData.Write(ref pos, (ushort)buyState.Hue);
}
AssertThat.Equal(data, expectedData);
}
[Fact]
public void TestVendorBuyContent6017()
{
var cont = new Container(World.NewItem);
var buyStates = new List<BuyItemState>
{
new BuyItemState("First Item", cont.Serial, World.NewItem, 10, 1, 0x01, 0),
new BuyItemState("Second Item", cont.Serial, World.NewItem, 20, 2, 0x0A, 0),
new BuyItemState("Third Item", cont.Serial, World.NewItem, 30, 10, 0x0F, 0)
};
var data = new VendorBuyContent6017(buyStates).Compile();
Span<byte> expectedData = stackalloc byte[5 + buyStates.Count * 20];
var pos = 0;
expectedData.Write(ref pos, (byte)0x3C); // Packet ID
expectedData.Write(ref pos, (ushort)expectedData.Length); // Length
expectedData.Write(ref pos, (ushort)buyStates.Count); // Count
for (var i = buyStates.Count - 1; i >= 0; i--)
{
var buyState = buyStates[i];
expectedData.Write(ref pos, buyState.MySerial);
expectedData.Write(ref pos, (ushort)buyState.ItemID);
pos++; // ItemID Offset
expectedData.Write(ref pos, (ushort)buyState.Amount);
expectedData.Write(ref pos, (ushort)(i + 1)); // X
expectedData.Write(ref pos, (ushort)1); // Y
#if NO_LOCAL_INIT
expectedData.Write(ref pos, (byte)0); // Grid Location?
#else
pos++;
#endif
expectedData.Write(ref pos, buyState.ContainerSerial);
expectedData.Write(ref pos, (ushort)buyState.Hue);
}
AssertThat.Equal(data, expectedData);
}
[Fact]
public void TestDisplayBuyList()
{
var vendor = new Mobile(0x1);
vendor.DefaultMobileInit();
var data = new DisplayBuyList(vendor).Compile();
Span<byte> expectedData = stackalloc byte[7];
var pos = 0;
expectedData.Write(ref pos, (byte)0x24); // Packet ID
expectedData.Write(ref pos, vendor.Serial);
expectedData.Write(ref pos, (ushort)0x30); // Buy gump
AssertThat.Equal(data, expectedData);
}
[Fact]
public void TestDisplayBuyListHS()
{
var vendor = new Mobile(0x1);
vendor.DefaultMobileInit();
var data = new DisplayBuyListHS(vendor).Compile();
Span<byte> expectedData = stackalloc byte[9];
var pos = 0;
expectedData.Write(ref pos, (byte)0x24); // Packet ID
expectedData.Write(ref pos, vendor.Serial);
expectedData.Write(ref pos, (ushort)0x30); // Buy gump
#if NO_LOCAL_INIT
expectedData.Write(ref pos, (ushort)0);
#endif
AssertThat.Equal(data, expectedData);
}
[Fact]
public void TestVendorBuyList()
{
var vendor = new Mobile(0x1);
vendor.DefaultMobileInit();
var cont = new Container(World.NewItem);
var buyStates = new List<BuyItemState>
{
new BuyItemState("First Item", cont.Serial, World.NewItem, 10, 1, 0x01, 0),
new BuyItemState("Second Item", cont.Serial, World.NewItem, 20, 2, 0x0A, 0),
new BuyItemState("Third Item", cont.Serial, World.NewItem, 30, 10, 0x0F, 0)
};
var data = new VendorBuyList(vendor, buyStates).Compile();
var length = 8 + buyStates.Sum(state => 6 + state.Description.Length);
Span<byte> expectedData = stackalloc byte[length];
var pos = 0;
expectedData.Write(ref pos, (byte)0x74); // Packet ID
expectedData.Write(ref pos, (ushort)expectedData.Length); // Length
expectedData.Write(ref pos, Serial.MinusOne); // Vendor Buy Pack Serial or -1
expectedData.Write(ref pos, (byte)buyStates.Count);
for (var i = 0; i < buyStates.Count; i++)
{
var state = buyStates[i];
expectedData.Write(ref pos, state.Price);
var description = state.Description ?? "";
expectedData.Write(ref pos, (byte)Math.Min(255, description.Length + 1));
expectedData.WriteAsciiNull(ref pos, description, 255);
}
AssertThat.Equal(data, expectedData);
}
[Fact]
public void TestEndVendorBuy()
{
var vendor = new Mobile(0x1);
vendor.DefaultMobileInit();
var data = new EndVendorBuy(vendor).Compile();
Span<byte> expectedData = stackalloc byte[8];
var pos = 0;
expectedData.Write(ref pos, (byte)0x3B); // Packet ID
expectedData.Write(ref pos, (ushort)0x8); // Length
expectedData.Write(ref pos, vendor.Serial);
#if NO_LOCAL_INIT
expectedData.Write(ref pos, (byte)-);
#endif
AssertThat.Equal(data, expectedData);
}
}
}