Skip to content
This repository has been archived by the owner on Jul 11, 2020. It is now read-only.

Added Drop Packet Handler #20

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 62 additions & 0 deletions srcs/Moonlight/Handlers/Maps/DropPacketHandler.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
using Moonlight.Clients;
using Moonlight.Core;
using Moonlight.Core.Enums;
using Moonlight.Core.Logging;
using Moonlight.Event;
using Moonlight.Event.Maps;
using Moonlight.Game.Entities;
using Moonlight.Game.Factory;
using Moonlight.Game.Inventories.Items;
using Moonlight.Game.Maps;
using Moonlight.Packet.Map;
using System;

namespace Moonlight.Handlers.Maps
{
internal class DropPacketHandler : PacketHandler<DropPacket>
{
private readonly IEntityFactory _entityFactory;
private readonly IEventManager _eventManager;
private readonly ILogger _logger;

public DropPacketHandler(ILogger logger, IEntityFactory entityFactory, IEventManager eventManager)
{
_logger = logger;
_entityFactory = entityFactory;
_eventManager = eventManager;
}
protected override void Handle(Client client, DropPacket packet)
{
Map map = client.Character.Map;

if (map == null)
{
_logger.Warn("Handling InPacket but character map is null");
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Change warning message to Handling DropPacket instead of Handling InPacket

return;
}

if(map.Contains(EntityType.GROUND_ITEM, packet.VNum))
{
_logger.Warn($"Entity {EntityType.GROUND_ITEM} {packet.VNum} already on map");
return;
}

Entity entity = _entityFactory.CreateGroundItem(packet.DropId, packet.VNum, packet.Amount);
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Set variable type as GroundItem instead of Entity since CreateGroundItem will always return a GroundItem

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Got it, ill will try to fix this and the rest of the issue maybe later this week. Thank you for your keen eye.

entity.Position = new Position(packet.PositionX, packet.PositionY);

if (entity is GroundItem drop)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

By fixing previous problem, you can also remove this type check/cast.

{
drop.Owner = map.GetEntity<Player>(packet.OwnerId);
}

map.AddEntity(entity);
_logger.Info($"Entity {entity.EntityType} {entity.Id} joined map");
Copy link
Owner

@Roxeez Roxeez Apr 21, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Change info message to Drop with id {entity.Id} added to map instead or something like this


_eventManager.Emit(new EntityJoinEvent(client)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe you can also create a DropEvent or something with GroundItem & Map as property

{
Map = map,
Entity = entity
});
}
}
}