From 6aa0ff2b7e446a20ace43b54d70e49da5d65a1b0 Mon Sep 17 00:00:00 2001 From: Ethan Moffat Date: Sun, 3 Apr 2022 15:04:03 -0700 Subject: [PATCH] Migrate shop packet family to new handlers/actions --- EOLib/Domain/Interact/Shop/ShopActions.cs | 53 +++ EOLib/Net/API/PacketAPI.cs | 1 - EOLib/Net/API/Shop.cs | 156 ------ .../Interact/Shop/ShopCraftHandler.cs | 75 +++ .../Interact/Shop/ShopTradeHandler.cs | 100 ++++ EndlessClient/Dialogs/Old/ShopDialog.cs | 446 +++++++++--------- EndlessClient/Old/PacketAPICallbackManager.cs | 18 - 7 files changed, 451 insertions(+), 398 deletions(-) create mode 100644 EOLib/Domain/Interact/Shop/ShopActions.cs delete mode 100644 EOLib/Net/API/Shop.cs create mode 100644 EOLib/PacketHandlers/Interact/Shop/ShopCraftHandler.cs create mode 100644 EOLib/PacketHandlers/Interact/Shop/ShopTradeHandler.cs diff --git a/EOLib/Domain/Interact/Shop/ShopActions.cs b/EOLib/Domain/Interact/Shop/ShopActions.cs new file mode 100644 index 000000000..9f8c6f35d --- /dev/null +++ b/EOLib/Domain/Interact/Shop/ShopActions.cs @@ -0,0 +1,53 @@ +using EOLib.Net; +using EOLib.Net.Communication; + +namespace EOLib.Domain.Interact.Shop +{ + public class ShopActions : IShopActions + { + private readonly IPacketSendService _packetSendService; + + public ShopActions(IPacketSendService packetSendService) + { + _packetSendService = packetSendService; + } + + public void BuyItem(short itemId, int amount) + { + var packet = new PacketBuilder(PacketFamily.Shop, PacketAction.Buy) + .AddShort(itemId) + .AddInt(amount) + .Build(); + + _packetSendService.SendPacket(packet); + } + + public void SellItem(short itemId, int amount) + { + var packet = new PacketBuilder(PacketFamily.Shop, PacketAction.Sell) + .AddShort(itemId) + .AddInt(amount) + .Build(); + + _packetSendService.SendPacket(packet); + } + + public void CraftItem(short itemId) + { + var packet = new PacketBuilder(PacketFamily.Shop, PacketAction.Create) + .AddShort(itemId) + .Build(); + + _packetSendService.SendPacket(packet); + } + } + + public interface IShopActions + { + void BuyItem(short itemId, int amount); + + void SellItem(short itemId, int amount); + + void CraftItem(short itemId); + } +} diff --git a/EOLib/Net/API/PacketAPI.cs b/EOLib/Net/API/PacketAPI.cs index 421370cde..f57f3b8cf 100644 --- a/EOLib/Net/API/PacketAPI.cs +++ b/EOLib/Net/API/PacketAPI.cs @@ -29,7 +29,6 @@ public PacketAPI(EOClient client) _createPartyMembers(); _createNPCMembers(); _createQuestMembers(); - _createShopMembers(); _createSpellMembers(); _createStatSkillMembers(); _createTradeMembers(); diff --git a/EOLib/Net/API/Shop.cs b/EOLib/Net/API/Shop.cs deleted file mode 100644 index 57f2c4ce5..000000000 --- a/EOLib/Net/API/Shop.cs +++ /dev/null @@ -1,156 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Collections.ObjectModel; -using System.Linq; -using EOLib.Domain.Character; -using EOLib.Net.Handlers; - -namespace EOLib.Net.API -{ - public struct ShopItem - { - private readonly int m_id, m_buy, m_sell, m_maxBuy; - - public int ID => m_id; - public int Buy => m_buy; - public int Sell => m_sell; - public int MaxBuy => m_maxBuy; - - public ShopItem(int ID, int BuyPrice, int SellPrice, int MaxBuy) - { - m_id = ID; - m_buy = BuyPrice; - m_sell = SellPrice; - m_maxBuy = MaxBuy; - } - } - - public struct CraftItem - { - private readonly int m_id; - public int ID => m_id; - - private readonly List> m_ingreds; - public ReadOnlyCollection> Ingredients => m_ingreds.AsReadOnly(); - - public CraftItem(int ID, IEnumerable> Ingredients) - { - m_ingreds = new List>(); - m_ingreds.AddRange(Ingredients.Where(x => x.Item1 != 0 && x.Item2 != 0)); - m_id = ID; - } - } - - partial class PacketAPI - { - 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 ingredients); - public event ShopTradeEvent OnShopTradeItem; - public event ShopCraftEvent OnShopCraftItem; - - private void _createShopMembers() - { - 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); - } - - /// - /// Buy an item from a shopkeeper - /// - public bool BuyItem(short ItemID, int amount) - { - if (!m_client.ConnectedAndInitialized || !Initialized) - return false; - - OldPacket pkt = new OldPacket(PacketFamily.Shop, PacketAction.Buy); - pkt.AddShort(ItemID); - pkt.AddInt(amount); - - return m_client.SendPacket(pkt); - } - - /// - /// Sell an item to a shopkeeper - /// - public bool SellItem(short ItemID, int amount) - { - if (!m_client.ConnectedAndInitialized || !Initialized) - return false; - - OldPacket pkt = new OldPacket(PacketFamily.Shop, PacketAction.Sell); - pkt.AddShort(ItemID); - pkt.AddInt(amount); - - return m_client.SendPacket(pkt); - } - - /// - /// Craft an item with a shopkeeper - /// - public bool CraftItem(short ItemID) - { - if (!m_client.ConnectedAndInitialized || !Initialized) - return false; - - OldPacket pkt = new OldPacket(PacketFamily.Shop, PacketAction.Create); - pkt.AddShort(ItemID); - - return m_client.SendPacket(pkt); - } - - /// - /// Handles SHOP_BUY from server, response to buying an item - /// - private void _handleShopBuy(OldPacket pkt) - { - if (OnShopTradeItem == null) return; - - int charGoldLeft = pkt.GetInt(); - short itemID = pkt.GetShort(); - int amount = pkt.GetInt(); - byte weight = pkt.GetChar(); - byte maxWeight = pkt.GetChar(); - - OnShopTradeItem(charGoldLeft, itemID, amount, weight, maxWeight, true); - } - - /// - /// Handles SHOP_SELL from server, response to selling an item - /// - private void _handleShopSell(OldPacket pkt) - { - if (OnShopTradeItem == null) return; - - int charNumLeft = pkt.GetInt(); - short itemID = pkt.GetShort(); - int charGold = pkt.GetInt(); - byte weight = pkt.GetChar(); - byte maxWeight = pkt.GetChar(); - - OnShopTradeItem(charGold, itemID, charNumLeft, weight, maxWeight, false); - } - - /// - /// Handles SHOP_CREATE from server, response to crafting an item - /// - private void _handleShopCreate(OldPacket pkt) - { - if (OnShopCraftItem == null) return; - - short itemID = pkt.GetShort(); - byte weight = pkt.GetChar(); - byte maxWeight = pkt.GetChar(); - - List inventoryItems = new List(4); - while (pkt.ReadPos != pkt.Length) - { - if (pkt.PeekShort() <= 0) break; - - inventoryItems.Add(new InventoryItem(pkt.GetShort(), pkt.GetInt())); - } - - OnShopCraftItem(itemID, weight, maxWeight, inventoryItems); - } - } -} diff --git a/EOLib/PacketHandlers/Interact/Shop/ShopCraftHandler.cs b/EOLib/PacketHandlers/Interact/Shop/ShopCraftHandler.cs new file mode 100644 index 000000000..294a7d030 --- /dev/null +++ b/EOLib/PacketHandlers/Interact/Shop/ShopCraftHandler.cs @@ -0,0 +1,75 @@ +using AutomaticTypeMapper; +using EOLib.Domain.Character; +using EOLib.Domain.Login; +using EOLib.Net; +using EOLib.Net.Handlers; +using Optional.Collections; +using System.Collections.Generic; + +namespace EOLib.PacketHandlers.Interact.Shop +{ + [AutoMappedType] + public class ShopCraftHandler : InGameOnlyPacketHandler + { + private readonly ICharacterRepository _characterRepository; + private readonly ICharacterInventoryRepository _characterInventoryRepository; + + public override PacketFamily Family => PacketFamily.Shop; + + public override PacketAction Action => PacketAction.Create; + + protected ShopCraftHandler(IPlayerInfoProvider playerInfoProvider, + ICharacterRepository characterRepository, + ICharacterInventoryRepository characterInventoryRepository) + : base(playerInfoProvider) + { + _characterRepository = characterRepository; + _characterInventoryRepository = characterInventoryRepository; + } + + public override bool HandlePacket(IPacket packet) + { + var itemId = packet.ReadShort(); + var weight = packet.ReadChar(); + var maxWeight = packet.ReadChar(); + + while (packet.ReadPosition < packet.Length) + { + if (packet.PeekShort() == 0) break; + + var nextItemId = packet.ReadShort(); + var nextItemAmount = packet.ReadInt(); + + _characterInventoryRepository.ItemInventory.SingleOrNone(x => x.ItemID == nextItemId) + .Match( + some: existing => + { + _characterInventoryRepository.ItemInventory.Remove(existing); + if (nextItemAmount > 0) + _characterInventoryRepository.ItemInventory.Add(existing.WithAmount(nextItemAmount)); + }, + none: () => + { + if (nextItemAmount > 0) + _characterInventoryRepository.ItemInventory.Add(new InventoryItem(nextItemId, nextItemAmount)); + }); + } + + _characterInventoryRepository.ItemInventory.SingleOrNone(x => x.ItemID == itemId) + .Match( + some: existing => + { + _characterInventoryRepository.ItemInventory.Remove(existing); + _characterInventoryRepository.ItemInventory.Add(existing.WithAmount(existing.Amount + 1)); + }, + none: () => _characterInventoryRepository.ItemInventory.Add(new InventoryItem(itemId, 1))); + + var stats = _characterRepository.MainCharacter.Stats; + stats = stats.WithNewStat(CharacterStat.Weight, weight) + .WithNewStat(CharacterStat.MaxWeight, maxWeight); + _characterRepository.MainCharacter = _characterRepository.MainCharacter.WithStats(stats); + + return true; + } + } +} diff --git a/EOLib/PacketHandlers/Interact/Shop/ShopTradeHandler.cs b/EOLib/PacketHandlers/Interact/Shop/ShopTradeHandler.cs new file mode 100644 index 000000000..009fc7830 --- /dev/null +++ b/EOLib/PacketHandlers/Interact/Shop/ShopTradeHandler.cs @@ -0,0 +1,100 @@ +using AutomaticTypeMapper; +using EOLib.Domain.Character; +using EOLib.Domain.Login; +using EOLib.Net; +using EOLib.Net.Handlers; +using Optional.Collections; + +namespace EOLib.PacketHandlers.Interact.Shop +{ + public abstract class ShopTradeHandler : InGameOnlyPacketHandler + { + private readonly ICharacterRepository _characterRepository; + private readonly ICharacterInventoryRepository _characterInventoryRepository; + + public override PacketFamily Family => PacketFamily.Shop; + + protected ShopTradeHandler(IPlayerInfoProvider playerInfoProvider, + ICharacterRepository characterRepository, + ICharacterInventoryRepository characterInventoryRepository) + : base(playerInfoProvider) + { + _characterRepository = characterRepository; + _characterInventoryRepository = characterInventoryRepository; + } + + public override bool HandlePacket(IPacket packet) + { + var remaining = packet.ReadInt(); // character gold remaining on buy; item amount remaining on sell + var itemId = packet.ReadShort(); + var acquired = packet.ReadInt(); // amount acquired on buy; gold acquired on sell + var weight = packet.ReadChar(); + var maxWeight = packet.ReadChar(); + + if (Action == PacketAction.Buy) + { + var gold = new InventoryItem(1, remaining); + _characterInventoryRepository.ItemInventory.RemoveWhere(x => x.ItemID == 1); + _characterInventoryRepository.ItemInventory.Add(gold); + + var shopBuy = new InventoryItem(itemId, acquired); + _characterInventoryRepository.ItemInventory.SingleOrNone(x => x.ItemID == itemId) + .Match( + some: existing => + { + _characterInventoryRepository.ItemInventory.RemoveWhere(x => x.ItemID == itemId); + _characterInventoryRepository.ItemInventory.Add(shopBuy); + }, + none: () => _characterInventoryRepository.ItemInventory.Add(shopBuy)); + } + else if (Action == PacketAction.Sell) + { + var gold = new InventoryItem(1, acquired); + _characterInventoryRepository.ItemInventory.RemoveWhere(x => x.ItemID == 1); + _characterInventoryRepository.ItemInventory.Add(gold); + + var itemSold = new InventoryItem(itemId, remaining); + _characterInventoryRepository.ItemInventory.RemoveWhere(x => x.ItemID == itemId); + if (itemSold.Amount > 0) + _characterInventoryRepository.ItemInventory.Add(itemSold); + } + else + { + return false; + } + + var stats = _characterRepository.MainCharacter.Stats; + stats = stats.WithNewStat(CharacterStat.Weight, weight) + .WithNewStat(CharacterStat.MaxWeight, maxWeight); + _characterRepository.MainCharacter = _characterRepository.MainCharacter.WithStats(stats); + + return true; + } + } + + [AutoMappedType] + public class ShopBuyHandler : ShopTradeHandler + { + public override PacketAction Action => PacketAction.Buy; + + public ShopBuyHandler(IPlayerInfoProvider playerInfoProvider, + ICharacterRepository characterRepository, + ICharacterInventoryRepository characterInventoryRepository) + : base(playerInfoProvider, characterRepository, characterInventoryRepository) + { + } + } + + [AutoMappedType] + public class ShopSellHandler : ShopTradeHandler + { + public override PacketAction Action => PacketAction.Sell; + + public ShopSellHandler(IPlayerInfoProvider playerInfoProvider, + ICharacterRepository characterRepository, + ICharacterInventoryRepository characterInventoryRepository) + : base(playerInfoProvider, characterRepository, characterInventoryRepository) + { + } + } +} diff --git a/EndlessClient/Dialogs/Old/ShopDialog.cs b/EndlessClient/Dialogs/Old/ShopDialog.cs index 232342bd4..15fd5bed9 100644 --- a/EndlessClient/Dialogs/Old/ShopDialog.cs +++ b/EndlessClient/Dialogs/Old/ShopDialog.cs @@ -51,8 +51,8 @@ private enum ShopState public int ID { get; private set; } private ShopState m_state; - private List m_tradeItems; - private List m_craftItems; + //private List m_tradeItems; + //private List m_craftItems; private static Texture2D BuyIcon, SellIcon, CraftIcon; @@ -88,16 +88,16 @@ private ShopDialog(PacketAPI api, int id) } } - public void SetShopData(int id, string Name, List tradeItems, List craftItems) - { - if (Instance == null || this != Instance || ID != id) return; - Title = Name; + //public void SetShopData(int id, string Name, List tradeItems, List craftItems) + //{ + // if (Instance == null || this != Instance || ID != id) return; + // Title = Name; - m_tradeItems = tradeItems; - m_craftItems = craftItems; + // m_tradeItems = tradeItems; + // m_craftItems = craftItems; - _setState(ShopState.Initial); - } + // _setState(ShopState.Initial); + //} private void _setState(ShopState newState) { @@ -105,68 +105,68 @@ private void _setState(ShopState newState) if (old == newState) return; - int buyNumInt = m_tradeItems.FindAll(x => x.Buy > 0).Count; - int sellNumInt = m_tradeItems.FindAll(x => OldWorld.Instance.MainPlayer.ActiveCharacter.Inventory.FindIndex(item => item.ItemID == x.ID) >= 0 && x.Sell > 0).Count; + //int buyNumInt = m_tradeItems.FindAll(x => x.Buy > 0).Count; + //int sellNumInt = m_tradeItems.FindAll(x => OldWorld.Instance.MainPlayer.ActiveCharacter.Inventory.FindIndex(item => item.ItemID == x.ID) >= 0 && x.Sell > 0).Count; - if (newState == ShopState.Buying && buyNumInt <= 0) - { - EOMessageBox.Show(DialogResourceID.SHOP_NOTHING_IS_FOR_SALE, EODialogButtons.Ok, EOMessageBoxStyle.SmallDialogSmallHeader); - return; - } + //if (newState == ShopState.Buying && buyNumInt <= 0) + //{ + // EOMessageBox.Show(DialogResourceID.SHOP_NOTHING_IS_FOR_SALE, EODialogButtons.Ok, EOMessageBoxStyle.SmallDialogSmallHeader); + // return; + //} - if (newState == ShopState.Selling && sellNumInt <= 0) - { - EOMessageBox.Show(DialogResourceID.SHOP_NOT_BUYING_YOUR_ITEMS, EODialogButtons.Ok, EOMessageBoxStyle.SmallDialogSmallHeader); - return; - } + //if (newState == ShopState.Selling && sellNumInt <= 0) + //{ + // EOMessageBox.Show(DialogResourceID.SHOP_NOT_BUYING_YOUR_ITEMS, EODialogButtons.Ok, EOMessageBoxStyle.SmallDialogSmallHeader); + // return; + //} ClearItemList(); switch (newState) { case ShopState.Initial: { - string buyNum = - $"{m_tradeItems.FindAll(x => x.Buy > 0).Count} {OldWorld.GetString(EOResourceID.DIALOG_SHOP_ITEMS_IN_STORE)}"; - string sellNum = $"{sellNumInt} {OldWorld.GetString(EOResourceID.DIALOG_SHOP_ITEMS_ACCEPTED)}"; - string craftNum = - $"{m_craftItems.Count} {OldWorld.GetString(EOResourceID.DIALOG_SHOP_ITEMS_ACCEPTED)}"; - - OldListDialogItem buy = new OldListDialogItem(this, OldListDialogItem.ListItemStyle.Large, 0) - { - Text = OldWorld.GetString(EOResourceID.DIALOG_SHOP_BUY_ITEMS), - SubText = buyNum, - IconGraphic = BuyIcon, - OffsetY = 45 - }; - buy.OnLeftClick += (o, e) => _setState(ShopState.Buying); - buy.OnRightClick += (o, e) => _setState(ShopState.Buying); - buy.ShowItemBackGround = false; - AddItemToList(buy, false); - OldListDialogItem sell = new OldListDialogItem(this, OldListDialogItem.ListItemStyle.Large, 1) - { - Text = OldWorld.GetString(EOResourceID.DIALOG_SHOP_SELL_ITEMS), - SubText = sellNum, - IconGraphic = SellIcon, - OffsetY = 45 - }; - sell.OnLeftClick += (o, e) => _setState(ShopState.Selling); - sell.OnRightClick += (o, e) => _setState(ShopState.Selling); - sell.ShowItemBackGround = false; - AddItemToList(sell, false); - if (m_craftItems.Count > 0) - { - OldListDialogItem craft = new OldListDialogItem(this, OldListDialogItem.ListItemStyle.Large, 2) - { - Text = OldWorld.GetString(EOResourceID.DIALOG_SHOP_CRAFT_ITEMS), - SubText = craftNum, - IconGraphic = CraftIcon, - OffsetY = 45 - }; - craft.OnLeftClick += (o, e) => _setState(ShopState.Crafting); - craft.OnRightClick += (o, e) => _setState(ShopState.Crafting); - craft.ShowItemBackGround = false; - AddItemToList(craft, false); - } + //string buyNum = + // $"{m_tradeItems.FindAll(x => x.Buy > 0).Count} {OldWorld.GetString(EOResourceID.DIALOG_SHOP_ITEMS_IN_STORE)}"; + //string sellNum = $"{sellNumInt} {OldWorld.GetString(EOResourceID.DIALOG_SHOP_ITEMS_ACCEPTED)}"; + //string craftNum = + // $"{m_craftItems.Count} {OldWorld.GetString(EOResourceID.DIALOG_SHOP_ITEMS_ACCEPTED)}"; + + //OldListDialogItem buy = new OldListDialogItem(this, OldListDialogItem.ListItemStyle.Large, 0) + //{ + // Text = OldWorld.GetString(EOResourceID.DIALOG_SHOP_BUY_ITEMS), + // SubText = buyNum, + // IconGraphic = BuyIcon, + // OffsetY = 45 + //}; + //buy.OnLeftClick += (o, e) => _setState(ShopState.Buying); + //buy.OnRightClick += (o, e) => _setState(ShopState.Buying); + //buy.ShowItemBackGround = false; + //AddItemToList(buy, false); + //OldListDialogItem sell = new OldListDialogItem(this, OldListDialogItem.ListItemStyle.Large, 1) + //{ + // Text = OldWorld.GetString(EOResourceID.DIALOG_SHOP_SELL_ITEMS), + // SubText = sellNum, + // IconGraphic = SellIcon, + // OffsetY = 45 + //}; + //sell.OnLeftClick += (o, e) => _setState(ShopState.Selling); + //sell.OnRightClick += (o, e) => _setState(ShopState.Selling); + //sell.ShowItemBackGround = false; + //AddItemToList(sell, false); + //if (m_craftItems.Count > 0) + //{ + // OldListDialogItem craft = new OldListDialogItem(this, OldListDialogItem.ListItemStyle.Large, 2) + // { + // Text = OldWorld.GetString(EOResourceID.DIALOG_SHOP_CRAFT_ITEMS), + // SubText = craftNum, + // IconGraphic = CraftIcon, + // OffsetY = 45 + // }; + // craft.OnLeftClick += (o, e) => _setState(ShopState.Crafting); + // craft.OnRightClick += (o, e) => _setState(ShopState.Crafting); + // craft.ShowItemBackGround = false; + // AddItemToList(craft, false); + //} _setButtons(ScrollingListDialogButtons.Cancel); } break; @@ -177,60 +177,60 @@ private void _setState(ShopState newState) bool buying = newState == ShopState.Buying; List itemList = new List(); - foreach (ShopItem si in m_tradeItems) - { - if (si.ID <= 0 || (buying && si.Buy <= 0) || - (!buying && (si.Sell <= 0 || OldWorld.Instance.MainPlayer.ActiveCharacter.Inventory.FindIndex(inv => inv.ItemID == si.ID) < 0))) - continue; - - ShopItem localItem = si; - var rec = OldWorld.Instance.EIF[si.ID]; - string secondary = string.Format("{2}: {0} {1}", buying ? si.Buy : si.Sell, - rec.Type == ItemType.Armor ? "(" + (rec.Gender == 0 ? OldWorld.GetString(EOResourceID.FEMALE) : OldWorld.GetString(EOResourceID.MALE)) + ")" : "", - OldWorld.GetString(EOResourceID.DIALOG_SHOP_PRICE)); - - OldListDialogItem nextItem = new OldListDialogItem(this, OldListDialogItem.ListItemStyle.Large) - { - Text = rec.Name, - SubText = secondary, - IconGraphic = ((EOGame)Game).GFXManager.TextureFromResource(GFXTypes.Items, 2 * rec.Graphic - 1, true), - OffsetY = 45 - }; - nextItem.OnLeftClick += (o, e) => _buySellItem(localItem); - nextItem.OnRightClick += (o, e) => _buySellItem(localItem); - - itemList.Add(nextItem); - } + //foreach (ShopItem si in m_tradeItems) + //{ + // if (si.ID <= 0 || (buying && si.Buy <= 0) || + // (!buying && (si.Sell <= 0 || OldWorld.Instance.MainPlayer.ActiveCharacter.Inventory.FindIndex(inv => inv.ItemID == si.ID) < 0))) + // continue; + + // ShopItem localItem = si; + // var rec = OldWorld.Instance.EIF[si.ID]; + // string secondary = string.Format("{2}: {0} {1}", buying ? si.Buy : si.Sell, + // rec.Type == ItemType.Armor ? "(" + (rec.Gender == 0 ? OldWorld.GetString(EOResourceID.FEMALE) : OldWorld.GetString(EOResourceID.MALE)) + ")" : "", + // OldWorld.GetString(EOResourceID.DIALOG_SHOP_PRICE)); + + // OldListDialogItem nextItem = new OldListDialogItem(this, OldListDialogItem.ListItemStyle.Large) + // { + // Text = rec.Name, + // SubText = secondary, + // IconGraphic = ((EOGame)Game).GFXManager.TextureFromResource(GFXTypes.Items, 2 * rec.Graphic - 1, true), + // OffsetY = 45 + // }; + // nextItem.OnLeftClick += (o, e) => _buySellItem(localItem); + // nextItem.OnRightClick += (o, e) => _buySellItem(localItem); + + // itemList.Add(nextItem); + //} SetItemList(itemList); _setButtons(ScrollingListDialogButtons.BackCancel); } break; case ShopState.Crafting: { - List itemList = new List(m_craftItems.Count); - foreach (CraftItem ci in m_craftItems) - { - if (ci.Ingredients.Count <= 0) continue; - - CraftItem localItem = ci; - var rec = OldWorld.Instance.EIF[ci.ID]; - string secondary = string.Format("{2}: {0} {1}", ci.Ingredients.Count, - rec.Type == ItemType.Armor ? "(" + (rec.Gender == 0 ? OldWorld.GetString(EOResourceID.FEMALE) : OldWorld.GetString(EOResourceID.MALE)) + ")" : "", - OldWorld.GetString(EOResourceID.DIALOG_SHOP_CRAFT_INGREDIENTS)); - - OldListDialogItem nextItem = new OldListDialogItem(this, OldListDialogItem.ListItemStyle.Large) - { - Text = rec.Name, - SubText = secondary, - IconGraphic = ((EOGame)Game).GFXManager.TextureFromResource(GFXTypes.Items, 2 * rec.Graphic - 1, true), - OffsetY = 45 - }; - nextItem.OnLeftClick += (o, e) => _craftItem(localItem); - nextItem.OnRightClick += (o, e) => _craftItem(localItem); - - itemList.Add(nextItem); - } - SetItemList(itemList); + //List itemList = new List(m_craftItems.Count); + //foreach (CraftItem ci in m_craftItems) + //{ + // if (ci.Ingredients.Count <= 0) continue; + + // CraftItem localItem = ci; + // var rec = OldWorld.Instance.EIF[ci.ID]; + // string secondary = string.Format("{2}: {0} {1}", ci.Ingredients.Count, + // rec.Type == ItemType.Armor ? "(" + (rec.Gender == 0 ? OldWorld.GetString(EOResourceID.FEMALE) : OldWorld.GetString(EOResourceID.MALE)) + ")" : "", + // OldWorld.GetString(EOResourceID.DIALOG_SHOP_CRAFT_INGREDIENTS)); + + // OldListDialogItem nextItem = new OldListDialogItem(this, OldListDialogItem.ListItemStyle.Large) + // { + // Text = rec.Name, + // SubText = secondary, + // IconGraphic = ((EOGame)Game).GFXManager.TextureFromResource(GFXTypes.Items, 2 * rec.Graphic - 1, true), + // OffsetY = 45 + // }; + // nextItem.OnLeftClick += (o, e) => _craftItem(localItem); + // nextItem.OnRightClick += (o, e) => _craftItem(localItem); + + // itemList.Add(nextItem); + //} + //SetItemList(itemList); _setButtons(ScrollingListDialogButtons.BackCancel); } break; @@ -239,109 +239,109 @@ private void _setState(ShopState newState) m_state = newState; } - private void _buySellItem(ShopItem item) + private void _buySellItem(object item) { - if (m_state != ShopState.Buying && m_state != ShopState.Selling) - return; - bool isBuying = m_state == ShopState.Buying; - - InventoryItem ii = OldWorld.Instance.MainPlayer.ActiveCharacter.Inventory.Find(x => (isBuying ? x.ItemID == 1 : x.ItemID == item.ID)); - var rec = OldWorld.Instance.EIF[item.ID]; - if (isBuying) - { - //if (!EOGame.Instance.Hud.InventoryFits((short)item.ID)) - //{ - // EOMessageBox.Show(OldWorld.GetString(EOResourceID.DIALOG_TRANSFER_NOT_ENOUGH_SPACE), - // OldWorld.GetString(EOResourceID.STATUS_LABEL_TYPE_WARNING), - // EODialogButtons.Ok, EOMessageBoxStyle.SmallDialogSmallHeader); - // return; - //} - - if (rec.Weight + OldWorld.Instance.MainPlayer.ActiveCharacter.Weight > - OldWorld.Instance.MainPlayer.ActiveCharacter.MaxWeight) - { - EOMessageBox.Show(OldWorld.GetString(EOResourceID.DIALOG_TRANSFER_NOT_ENOUGH_WEIGHT), - OldWorld.GetString(EOResourceID.STATUS_LABEL_TYPE_WARNING), - EODialogButtons.Ok, EOMessageBoxStyle.SmallDialogSmallHeader); - return; - } + //if (m_state != ShopState.Buying && m_state != ShopState.Selling) + // return; + //bool isBuying = m_state == ShopState.Buying; - if (ii.Amount < item.Buy) - { - EOMessageBox.Show(DialogResourceID.WARNING_YOU_HAVE_NOT_ENOUGH, " gold.", EODialogButtons.Ok, EOMessageBoxStyle.SmallDialogSmallHeader); - return; - } - } - else if (ii.Amount == 0) - return; //can't sell if amount of item is 0 + //InventoryItem ii = OldWorld.Instance.MainPlayer.ActiveCharacter.Inventory.Find(x => (isBuying ? x.ItemID == 1 : x.ItemID == item.ID)); + //var rec = OldWorld.Instance.EIF[item.ID]; + //if (isBuying) + //{ + // if (!EOGame.Instance.Hud.InventoryFits((short)item.ID)) + // { + // EOMessageBox.Show(OldWorld.GetString(EOResourceID.DIALOG_TRANSFER_NOT_ENOUGH_SPACE), + // OldWorld.GetString(EOResourceID.STATUS_LABEL_TYPE_WARNING), + // EODialogButtons.Ok, EOMessageBoxStyle.SmallDialogSmallHeader); + // return; + // } + + // if (rec.Weight + OldWorld.Instance.MainPlayer.ActiveCharacter.Weight > + // OldWorld.Instance.MainPlayer.ActiveCharacter.MaxWeight) + // { + // EOMessageBox.Show(OldWorld.GetString(EOResourceID.DIALOG_TRANSFER_NOT_ENOUGH_WEIGHT), + // OldWorld.GetString(EOResourceID.STATUS_LABEL_TYPE_WARNING), + // EODialogButtons.Ok, EOMessageBoxStyle.SmallDialogSmallHeader); + // return; + // } + + // if (ii.Amount < item.Buy) + // { + // EOMessageBox.Show(DialogResourceID.WARNING_YOU_HAVE_NOT_ENOUGH, " gold.", EODialogButtons.Ok, EOMessageBoxStyle.SmallDialogSmallHeader); + // return; + // } + //} + //else if (ii.Amount == 0) + // return; //can't sell if amount of item is 0 - //special case: no need for prompting if selling an item with count == 1 in inventory - if (!isBuying && ii.Amount == 1) - { - string _message = - $"{OldWorld.GetString(EOResourceID.DIALOG_WORD_SELL)} 1 {rec.Name} {OldWorld.GetString(EOResourceID.DIALOG_WORD_FOR)} {item.Sell} gold?"; - EOMessageBox.Show(_message, OldWorld.GetString(EOResourceID.DIALOG_SHOP_SELL_ITEMS), EODialogButtons.OkCancel, - EOMessageBoxStyle.SmallDialogSmallHeader, (oo, ee) => - { - if (ee.Result == XNADialogResult.OK && !m_api.SellItem((short)item.ID, 1)) - { - EOGame.Instance.DoShowLostConnectionDialogAndReturnToMainMenu(); - } - }); - } - else - { - ItemTransferDialog dlg = new ItemTransferDialog(rec.Name, ItemTransferDialog.TransferType.ShopTransfer, - isBuying ? item.MaxBuy : ii.Amount, isBuying ? EOResourceID.DIALOG_TRANSFER_BUY : EOResourceID.DIALOG_TRANSFER_SELL); - dlg.DialogClosing += (o, e) => - { - if (e.Result == XNADialogResult.OK) - { - string _message = - $"{OldWorld.GetString(isBuying ? EOResourceID.DIALOG_WORD_BUY : EOResourceID.DIALOG_WORD_SELL)} {dlg.SelectedAmount} {rec.Name} {OldWorld.GetString(EOResourceID.DIALOG_WORD_FOR)} {(isBuying ? item.Buy : item.Sell)*dlg.SelectedAmount} gold?"; - - EOMessageBox.Show(_message, - OldWorld.GetString(isBuying ? EOResourceID.DIALOG_SHOP_BUY_ITEMS : EOResourceID.DIALOG_SHOP_SELL_ITEMS), - EODialogButtons.OkCancel, EOMessageBoxStyle.SmallDialogSmallHeader, (oo, ee) => - { - if (ee.Result == XNADialogResult.OK) - { - //only actually do the buy/sell if the user then clicks "OK" in the second prompt - if (isBuying && !m_api.BuyItem((short)item.ID, dlg.SelectedAmount) || - !isBuying && !m_api.SellItem((short)item.ID, dlg.SelectedAmount)) - { - EOGame.Instance.DoShowLostConnectionDialogAndReturnToMainMenu(); - } - } - }); - } - }; - } + ////special case: no need for prompting if selling an item with count == 1 in inventory + //if (!isBuying && ii.Amount == 1) + //{ + // string _message = + // $"{OldWorld.GetString(EOResourceID.DIALOG_WORD_SELL)} 1 {rec.Name} {OldWorld.GetString(EOResourceID.DIALOG_WORD_FOR)} {item.Sell} gold?"; + // EOMessageBox.Show(_message, OldWorld.GetString(EOResourceID.DIALOG_SHOP_SELL_ITEMS), EODialogButtons.OkCancel, + // EOMessageBoxStyle.SmallDialogSmallHeader, (oo, ee) => + // { + // if (ee.Result == XNADialogResult.OK && !m_api.SellItem((short)item.ID, 1)) + // { + // EOGame.Instance.DoShowLostConnectionDialogAndReturnToMainMenu(); + // } + // }); + //} + //else + //{ + // ItemTransferDialog dlg = new ItemTransferDialog(rec.Name, ItemTransferDialog.TransferType.ShopTransfer, + // isBuying ? item.MaxBuy : ii.Amount, isBuying ? EOResourceID.DIALOG_TRANSFER_BUY : EOResourceID.DIALOG_TRANSFER_SELL); + // dlg.DialogClosing += (o, e) => + // { + // if (e.Result == XNADialogResult.OK) + // { + // string _message = + // $"{OldWorld.GetString(isBuying ? EOResourceID.DIALOG_WORD_BUY : EOResourceID.DIALOG_WORD_SELL)} {dlg.SelectedAmount} {rec.Name} {OldWorld.GetString(EOResourceID.DIALOG_WORD_FOR)} {(isBuying ? item.Buy : item.Sell)*dlg.SelectedAmount} gold?"; + + // EOMessageBox.Show(_message, + // OldWorld.GetString(isBuying ? EOResourceID.DIALOG_SHOP_BUY_ITEMS : EOResourceID.DIALOG_SHOP_SELL_ITEMS), + // EODialogButtons.OkCancel, EOMessageBoxStyle.SmallDialogSmallHeader, (oo, ee) => + // { + // if (ee.Result == XNADialogResult.OK) + // { + // //only actually do the buy/sell if the user then clicks "OK" in the second prompt + // if (isBuying && !m_api.BuyItem((short)item.ID, dlg.SelectedAmount) || + // !isBuying && !m_api.SellItem((short)item.ID, dlg.SelectedAmount)) + // { + // EOGame.Instance.DoShowLostConnectionDialogAndReturnToMainMenu(); + // } + // } + // }); + // } + // }; + //} } - private void _craftItem(CraftItem item) + private void _craftItem(object item) { - if (m_state != ShopState.Crafting) - return; + //if (m_state != ShopState.Crafting) + // return; - var craftItemRec = OldWorld.Instance.EIF[item.ID]; - // ReSharper disable once LoopCanBeConvertedToQuery - foreach (var ingredient in item.Ingredients) - { - if (OldWorld.Instance.MainPlayer.ActiveCharacter.Inventory.FindIndex(_item => _item.ItemID == ingredient.Item1 && _item.Amount >= ingredient.Item2) < 0) - { - string _message = OldWorld.GetString(EOResourceID.DIALOG_SHOP_CRAFT_MISSING_INGREDIENTS) + "\n\n"; - foreach (var ingred in item.Ingredients) - { - var localRec = OldWorld.Instance.EIF[ingred.Item1]; - _message += $"+ {ingred.Item2} {localRec.Name}\n"; - } - string _caption = - $"{OldWorld.GetString(EOResourceID.DIALOG_SHOP_CRAFT_INGREDIENTS)} {OldWorld.GetString(EOResourceID.DIALOG_WORD_FOR)} {craftItemRec.Name}"; - EOMessageBox.Show(_message, _caption, EODialogButtons.Cancel, EOMessageBoxStyle.LargeDialogSmallHeader); - return; - } - } + //var craftItemRec = OldWorld.Instance.EIF[item.ID]; + //// ReSharper disable once LoopCanBeConvertedToQuery + //foreach (var ingredient in item.Ingredients) + //{ + // if (OldWorld.Instance.MainPlayer.ActiveCharacter.Inventory.FindIndex(_item => _item.ItemID == ingredient.Item1 && _item.Amount >= ingredient.Item2) < 0) + // { + // string _message = OldWorld.GetString(EOResourceID.DIALOG_SHOP_CRAFT_MISSING_INGREDIENTS) + "\n\n"; + // foreach (var ingred in item.Ingredients) + // { + // var localRec = OldWorld.Instance.EIF[ingred.Item1]; + // _message += $"+ {ingred.Item2} {localRec.Name}\n"; + // } + // string _caption = + // $"{OldWorld.GetString(EOResourceID.DIALOG_SHOP_CRAFT_INGREDIENTS)} {OldWorld.GetString(EOResourceID.DIALOG_WORD_FOR)} {craftItemRec.Name}"; + // EOMessageBox.Show(_message, _caption, EODialogButtons.Cancel, EOMessageBoxStyle.LargeDialogSmallHeader); + // return; + // } + //} //if (!EOGame.Instance.Hud.InventoryFits((short)item.ID)) //{ @@ -351,21 +351,21 @@ private void _craftItem(CraftItem item) // return; //} - string _message2 = OldWorld.GetString(EOResourceID.DIALOG_SHOP_CRAFT_PUT_INGREDIENTS_TOGETHER) + "\n\n"; - foreach (var ingred in item.Ingredients) - { - var localRec = OldWorld.Instance.EIF[ingred.Item1]; - _message2 += $"+ {ingred.Item2} {localRec.Name}\n"; - } - string _caption2 = - $"{OldWorld.GetString(EOResourceID.DIALOG_SHOP_CRAFT_INGREDIENTS)} {OldWorld.GetString(EOResourceID.DIALOG_WORD_FOR)} {craftItemRec.Name}"; - EOMessageBox.Show(_message2, _caption2, EODialogButtons.OkCancel, EOMessageBoxStyle.LargeDialogSmallHeader, (o, e) => - { - if (e.Result == XNADialogResult.OK && !m_api.CraftItem((short)item.ID)) - { - EOGame.Instance.DoShowLostConnectionDialogAndReturnToMainMenu(); - } - }); + //string _message2 = OldWorld.GetString(EOResourceID.DIALOG_SHOP_CRAFT_PUT_INGREDIENTS_TOGETHER) + "\n\n"; + //foreach (var ingred in item.Ingredients) + //{ + // var localRec = OldWorld.Instance.EIF[ingred.Item1]; + // _message2 += $"+ {ingred.Item2} {localRec.Name}\n"; + //} + //string _caption2 = + // $"{OldWorld.GetString(EOResourceID.DIALOG_SHOP_CRAFT_INGREDIENTS)} {OldWorld.GetString(EOResourceID.DIALOG_WORD_FOR)} {craftItemRec.Name}"; + //EOMessageBox.Show(_message2, _caption2, EODialogButtons.OkCancel, EOMessageBoxStyle.LargeDialogSmallHeader, (o, e) => + //{ + // if (e.Result == XNADialogResult.OK && !m_api.CraftItem((short)item.ID)) + // { + // EOGame.Instance.DoShowLostConnectionDialogAndReturnToMainMenu(); + // } + //}); } } } diff --git a/EndlessClient/Old/PacketAPICallbackManager.cs b/EndlessClient/Old/PacketAPICallbackManager.cs index 859d7510e..f32047686 100644 --- a/EndlessClient/Old/PacketAPICallbackManager.cs +++ b/EndlessClient/Old/PacketAPICallbackManager.cs @@ -41,10 +41,6 @@ public void AssignCallbacks() m_packetAPI.OnBankOpen += _bankOpen; m_packetAPI.OnBankChange += _bankChange; - //shop - m_packetAPI.OnShopTradeItem += _shopTrade; - m_packetAPI.OnShopCraftItem += _shopCraft; - //locker m_packetAPI.OnLockerOpen += _lockerOpen; m_packetAPI.OnLockerItemChange += _lockerItemChange; @@ -149,20 +145,6 @@ private void _bankChange(int gold, int bankGold) BankAccountDialog.Instance.AccountBalance = $"{bankGold}"; } - private void _shopTrade(int gold, short itemID, int amount, byte weight, byte maxWeight, bool isBuy) - { - //OldWorld.Instance.MainPlayer.ActiveCharacter.UpdateInventoryItem(1, gold); - //OldWorld.Instance.MainPlayer.ActiveCharacter.UpdateInventoryItem(itemID, amount, weight, maxWeight, isBuy); - } - - private void _shopCraft(short id, byte weight, byte maxWeight, List ingredients) - { - //OldCharacter c = OldWorld.Instance.MainPlayer.ActiveCharacter; - //c.UpdateInventoryItem(id, 1, weight, maxWeight, true); - //foreach (var ingred in ingredients) - // c.UpdateInventoryItem(ingred.ItemID, ingred.Amount); - } - private void _lockerOpen(byte x, byte y, List items) { if (LockerDialog.Instance == null || LockerDialog.Instance.X != x || LockerDialog.Instance.Y != y)