Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Migrate shop packet family to new handlers/actions
- Loading branch information
1 parent
8820813
commit 6aa0ff2
Showing
7 changed files
with
451 additions
and
398 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) | ||
{ | ||
} | ||
} | ||
} |
Oops, something went wrong.