Skip to content

Commit

Permalink
Create NPCFromPacketFactory
Browse files Browse the repository at this point in the history
  • Loading branch information
sorokya committed Mar 28, 2022
1 parent 31cc6d9 commit 2803ca9
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 12 deletions.
31 changes: 31 additions & 0 deletions EOLib/Net/Translators/NPCFromPacketFactory.cs
@@ -0,0 +1,31 @@
using AutomaticTypeMapper;
using EOLib.Domain.NPC;

namespace EOLib.Net.Translators
{
[AutoMappedType]
public class NPCFromPacketFactory : INPCFromPacketFactory
{
public NPCFromPacketFactory()
{
}

public INPC CreateNPC(IPacket packet)
{
var index = packet.ReadChar();
var id = packet.ReadShort();
var x = packet.ReadChar();
var y = packet.ReadChar();
var direction = (EODirection)packet.ReadChar();

INPC npc = new NPC(id, index);
npc = npc.WithX(x).WithY(y).WithDirection(direction).WithFrame(NPCFrame.Standing);
return npc;
}
}

public interface INPCFromPacketFactory
{
INPC CreateNPC(IPacket packet);
}
}
17 changes: 5 additions & 12 deletions EOLib/PacketHandlers/MapInfoHandler.cs
Expand Up @@ -2,7 +2,6 @@
using EOLib.Domain.Extensions;
using EOLib.Domain.Login;
using EOLib.Domain.Map;
using EOLib.Domain.NPC;
using EOLib.IO.Extensions;
using EOLib.IO.Repositories;
using EOLib.Net;
Expand All @@ -16,6 +15,7 @@ public class MapInfoHandler : InGameOnlyPacketHandler
{
private readonly ICurrentMapStateRepository _currentMapStateRepository;
private readonly ICharacterFromPacketFactory _characterFromPacketFactory;
private readonly INPCFromPacketFactory _npcFromPacketFactory;
private readonly IEIFFileProvider _eifFileProvider;

public override PacketFamily Family => PacketFamily.MapInfo;
Expand All @@ -25,12 +25,13 @@ public class MapInfoHandler : InGameOnlyPacketHandler
public MapInfoHandler(IPlayerInfoProvider playerInfoProvider,
ICurrentMapStateRepository currentMapStateRepository,
ICharacterFromPacketFactory characterFromPacketFactory,
IEIFFileProvider eifFileProvider
INPCFromPacketFactory npcFromPacketFactory
)
: base(playerInfoProvider)
{
_currentMapStateRepository = currentMapStateRepository;
_characterFromPacketFactory = characterFromPacketFactory;
_npcFromPacketFactory = npcFromPacketFactory;
}

public override bool HandlePacket(IPacket packet)
Expand All @@ -54,16 +55,8 @@ public override bool HandlePacket(IPacket packet)

while (packet.ReadPosition < packet.Length)
{
var index = packet.ReadChar();
var id = packet.ReadShort();
var x = packet.ReadChar();
var y = packet.ReadChar();
var direction = (EODirection)packet.ReadChar();

INPC npc = new NPC(id, index);
npc = npc.WithX(x).WithY(y).WithDirection(direction).WithFrame(NPCFrame.Standing);

_currentMapStateRepository.NPCs.RemoveWhere(n => n.Index == index);
var npc = _npcFromPacketFactory.CreateNPC(packet);
_currentMapStateRepository.NPCs.RemoveWhere(n => n.Index == npc.Index);
_currentMapStateRepository.NPCs.Add(npc);
}

Expand Down

0 comments on commit 2803ca9

Please sign in to comment.