Skip to content

Commit

Permalink
Add packets for requesting shop and handling data about shop
Browse files Browse the repository at this point in the history
  • Loading branch information
ethanmoffat committed Apr 3, 2022
1 parent 6eddceb commit 8820813
Show file tree
Hide file tree
Showing 12 changed files with 301 additions and 63 deletions.
18 changes: 18 additions & 0 deletions EOLib/Domain/Character/PaperdollData.cs
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,24 @@ public override bool Equals(object obj)
Guild == other.Guild && Rank == other.Rank && PlayerID == other.PlayerID &&
Class == other.Class && Gender == other.Gender && Icon == other.Icon && Paperdoll.SequenceEqual(other.Paperdoll);
}

public override int GetHashCode()
{
int hashCode = 170256730;
hashCode = hashCode * -1521134295 + Name.GetHashCode();
hashCode = hashCode * -1521134295 + Home.GetHashCode();
hashCode = hashCode * -1521134295 + Partner.GetHashCode();
hashCode = hashCode * -1521134295 + Title.GetHashCode();
hashCode = hashCode * -1521134295 + Guild.GetHashCode();
hashCode = hashCode * -1521134295 + Rank.GetHashCode();
hashCode = hashCode * -1521134295 + PlayerID.GetHashCode();
hashCode = hashCode * -1521134295 + Class.GetHashCode();
hashCode = hashCode * -1521134295 + Gender.GetHashCode();
hashCode = hashCode * -1521134295 + Paperdoll.Select(x => (int)x.Value)
.Aggregate(hashCode * -1521134295, (a, b) => a * -1521134295 + b.GetHashCode());
hashCode = hashCode * -1521134295 + Icon.GetHashCode();
return hashCode;
}
}

public interface IPaperdollData
Expand Down
29 changes: 29 additions & 0 deletions EOLib/Domain/Interact/MapNPCActions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
using EOLib.Net;
using EOLib.Net.Communication;

namespace EOLib.Domain.Interact
{
public class MapNPCActions : IMapNPCActions
{
private readonly IPacketSendService _packetSendService;

public MapNPCActions(IPacketSendService packetSendService)
{
_packetSendService = packetSendService;
}

public void RequestShop(byte index)
{
var packet = new PacketBuilder(PacketFamily.Shop, PacketAction.Open)
.AddShort(index)
.Build();

_packetSendService.SendPacket(packet);
}
}

public interface IMapNPCActions
{
void RequestShop(byte index);
}
}
22 changes: 22 additions & 0 deletions EOLib/Domain/Interact/Shop/ShopCraftIngredient.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
namespace EOLib.Domain.Interact.Shop
{
public class ShopCraftIngredient : IShopCraftIngredient
{
public int ID { get; }

public int Amount { get; }

public ShopCraftIngredient(int id, int amount)
{
ID = id;
Amount = amount;
}
}

public interface IShopCraftIngredient
{
int ID { get; }

int Amount { get; }
}
}
24 changes: 24 additions & 0 deletions EOLib/Domain/Interact/Shop/ShopCraftItem.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
using System.Collections.Generic;

namespace EOLib.Domain.Interact.Shop
{
public class ShopCraftItem : IShopCraftItem
{
public int ID { get; }

public IReadOnlyList<IShopCraftIngredient> Ingredients { get; }

public ShopCraftItem(int id, IEnumerable<IShopCraftIngredient> ingredients)
{
ID = id;
Ingredients = new List<IShopCraftIngredient>(ingredients);
}
}

public interface IShopCraftItem
{
int ID { get; }

IReadOnlyList<IShopCraftIngredient> Ingredients { get; }
}
}
55 changes: 55 additions & 0 deletions EOLib/Domain/Interact/Shop/ShopDataRepository.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
using AutomaticTypeMapper;
using System.Collections.Generic;

namespace EOLib.Domain.Interact.Shop
{
public interface IShopDataRepository
{
int ShopID { get; set; }

string ShopName { get; set; }

List<IShopItem> TradeItems { get; set; }

List<IShopCraftItem> CraftItems { get; set; }
}

public interface IShopDataProvider
{
int ShopID { get; }

string ShopName { get; }

IReadOnlyList<IShopItem> TradeItems { get; }

IReadOnlyList<IShopCraftItem> CraftItems { get; }
}

[AutoMappedType(IsSingleton = true)]
public class ShopDataRepository : IShopDataProvider, IShopDataRepository, IResettable
{
public int ShopID { get; set; }
public string ShopName { get; set; }

public List<IShopItem> TradeItems { get; set; }

public List<IShopCraftItem> CraftItems { get; set; }

IReadOnlyList<IShopItem> IShopDataProvider.TradeItems => TradeItems;

IReadOnlyList<IShopCraftItem> IShopDataProvider.CraftItems => CraftItems;

public ShopDataRepository()
{
ResetState();
}

public void ResetState()
{
ShopID = 0;
ShopName = string.Empty;
TradeItems = new List<IShopItem>();
CraftItems = new List<IShopCraftItem>();
}
}
}
32 changes: 32 additions & 0 deletions EOLib/Domain/Interact/Shop/ShopItem.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
namespace EOLib.Domain.Interact.Shop
{
public class ShopItem : IShopItem
{
public int ID { get; }

public int Buy { get; }

public int Sell { get; }

public int MaxBuy { get; }

public ShopItem(int id, int buyPrice, int sellPrice, int maxBuy)
{
ID = id;
Buy = buyPrice;
Sell = sellPrice;
MaxBuy = maxBuy;
}
}

public interface IShopItem
{
int ID { get; }

int Buy { get; }

int Sell { get; }

int MaxBuy { get; }
}
}
49 changes: 0 additions & 49 deletions EOLib/Net/API/Shop.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,32 +43,18 @@ public CraftItem(int ID, IEnumerable<Tuple<int, int>> Ingredients)

partial class PacketAPI
{
public delegate void ShopOpenEvent(int shopID, string name, List<ShopItem> tradeItems, List<CraftItem> craftItems);
public delegate void ShopTradeEvent(int goldRemaining, short itemID, int amount, byte weight, byte maxWeight, bool isBuy);
public delegate void ShopCraftEvent(short itemID, byte weight, byte maxWeight, List<InventoryItem> ingredients);
public event ShopOpenEvent OnShopOpen;
public event ShopTradeEvent OnShopTradeItem;
public event ShopCraftEvent OnShopCraftItem;

private void _createShopMembers()
{
m_client.AddPacketHandler(new FamilyActionPair(PacketFamily.Shop, PacketAction.Open), _handleShopOpen, true);
m_client.AddPacketHandler(new FamilyActionPair(PacketFamily.Shop, PacketAction.Buy), _handleShopBuy, true);
m_client.AddPacketHandler(new FamilyActionPair(PacketFamily.Shop, PacketAction.Sell), _handleShopSell, true);
m_client.AddPacketHandler(new FamilyActionPair(PacketFamily.Shop, PacketAction.Create), _handleShopCreate, true);
}

public bool RequestShop(short npcIndex)
{
if (!m_client.ConnectedAndInitialized || !Initialized)
return false;

OldPacket pkt = new OldPacket(PacketFamily.Shop, PacketAction.Open);
pkt.AddShort(npcIndex);

return m_client.SendPacket(pkt);
}

/// <summary>
/// Buy an item from a shopkeeper
/// </summary>
Expand Down Expand Up @@ -112,41 +98,6 @@ public bool CraftItem(short ItemID)

return m_client.SendPacket(pkt);
}

/// <summary>
/// Handles SHOP_OPEN from server, contains shop data for a shop dialog
/// </summary>
private void _handleShopOpen(OldPacket pkt)
{
if (OnShopOpen == null) return;

int shopKeeperID = pkt.GetShort();
string shopName = pkt.GetBreakString();

List<ShopItem> tradeItems = new List<ShopItem>();
while (pkt.PeekByte() != 255)
{
ShopItem nextItem = new ShopItem(pkt.GetShort(), pkt.GetThree(), pkt.GetThree(), pkt.GetChar());
tradeItems.Add(nextItem);
}
pkt.GetByte();

List<CraftItem> craftItems = new List<CraftItem>();
while (pkt.PeekByte() != 255)
{
int ID = pkt.GetShort();
List<Tuple<int, int>> ingreds = new List<Tuple<int, int>>();

for (int i = 0; i < 4; ++i)
{
ingreds.Add(new Tuple<int, int>(pkt.GetShort(), pkt.GetChar()));
}
craftItems.Add(new CraftItem(ID, ingreds));
}
pkt.GetByte();

OnShopOpen(shopKeeperID, shopName, tradeItems, craftItems);
}

/// <summary>
/// Handles SHOP_BUY from server, response to buying an item
Expand Down
66 changes: 66 additions & 0 deletions EOLib/PacketHandlers/Interact/Shop/ShopOpenHandler.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
using AutomaticTypeMapper;
using EOLib.Domain.Interact.Shop;
using EOLib.Domain.Login;
using EOLib.Net;
using EOLib.Net.Handlers;
using System.Collections.Generic;

namespace EOLib.PacketHandlers.Interact.Shop
{
[AutoMappedType]
public class ShopOpenHandler : InGameOnlyPacketHandler
{
private readonly IShopDataRepository _shopDataRepository;

public override PacketFamily Family => PacketFamily.Shop;

public override PacketAction Action => PacketAction.Open;

public ShopOpenHandler(IPlayerInfoProvider playerInfoProvider,
IShopDataRepository shopDataRepository)
: base(playerInfoProvider)
{
_shopDataRepository = shopDataRepository;
}

public override bool HandlePacket(IPacket packet)
{
_shopDataRepository.ShopID = packet.ReadShort();
_shopDataRepository.ShopName = packet.ReadBreakString();

var tradeItems = new List<IShopItem>();
while (packet.PeekByte() != 255)
{
var nextItem = new ShopItem(
id: packet.ReadShort(),
buyPrice: packet.ReadThree(),
sellPrice: packet.ReadThree(),
maxBuy: packet.ReadChar());
tradeItems.Add(nextItem);
}
packet.ReadByte();

_shopDataRepository.TradeItems = tradeItems;

var craftItems = new List<IShopCraftItem>();
while (packet.PeekByte() != 255)
{
var id = packet.ReadShort();
var ingreds = new List<IShopCraftIngredient>();

for (int i = 0; i < 4; ++i)
{
ingreds.Add(new ShopCraftIngredient(
id: packet.ReadShort(),
amount: packet.ReadChar()));
}
craftItems.Add(new ShopCraftItem(id, ingreds));
}
packet.ReadByte();

_shopDataRepository.CraftItems = craftItems;

return true;
}
}
}
12 changes: 6 additions & 6 deletions EndlessClient/Dialogs/Old/ShopDialog.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,12 @@ public static void Show(PacketAPI api, OldNPCRenderer shopKeeper)
Instance = new ShopDialog(api, shopKeeper.NPC.Data.ID);

//request from server is based on the map index
if (!api.RequestShop(shopKeeper.NPC.Index))
{
Instance.Close();
Instance = null;
EOGame.Instance.DoShowLostConnectionDialogAndReturnToMainMenu();
}
//if (!api.RequestShop(shopKeeper.NPC.Index))
//{
// Instance.Close();
// Instance = null;
// EOGame.Instance.DoShowLostConnectionDialogAndReturnToMainMenu();
//}
}

private enum ShopState
Expand Down
2 changes: 1 addition & 1 deletion EndlessClient/Dialogs/Services/EODialogButtonService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public enum SmallButton
NUM_BUTTONS
}

[MappedType(BaseType = typeof(IEODialogButtonService))]
[AutoMappedType]
public class EODialogButtonService : IEODialogButtonService
{
private readonly INativeGraphicsManager _nativeGraphicsManager;
Expand Down
Loading

0 comments on commit 8820813

Please sign in to comment.