-
Notifications
You must be signed in to change notification settings - Fork 16
/
ShopCraftHandler.cs
74 lines (63 loc) · 3.01 KB
/
ShopCraftHandler.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
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
{
[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;
public 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;
}
}
}