Skip to content

Commit

Permalink
Add UnknownEntitiesRequester to game
Browse files Browse the repository at this point in the history
  • Loading branch information
sorokya committed Mar 28, 2022
1 parent b2d72c5 commit 4b0758b
Show file tree
Hide file tree
Showing 5 changed files with 121 additions and 9 deletions.
2 changes: 2 additions & 0 deletions EOLib/PacketHandlers/MapInfoHandler.cs
Expand Up @@ -53,6 +53,8 @@ public override bool HandlePacket(IPacket packet)
character = existingCharacter.WithAppliedData(character, isRangedWeapon);
}
_currentMapStateRepository.Characters[character.ID] = character;
if (packet.ReadByte() != 255)
throw new MalformedPacketException("Missing 255 byte after character data", packet);
}
}

Expand Down
10 changes: 2 additions & 8 deletions EOLib/PacketHandlers/PlayerWalkHandler.cs
@@ -1,14 +1,11 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Collections.Generic;
using AutomaticTypeMapper;
using EOLib.Domain.Character;
using EOLib.Domain.Extensions;
using EOLib.Domain.Login;
using EOLib.Domain.Map;
using EOLib.Domain.Notifiers;
using EOLib.Net;
using EOLib.Net.Communication;
using EOLib.Net.Handlers;

namespace EOLib.PacketHandlers
Expand All @@ -18,21 +15,18 @@ public class PlayerWalkHandler : InGameOnlyPacketHandler
{
private readonly ICurrentMapStateRepository _currentMapStateRepository;
private readonly IEnumerable<IOtherCharacterAnimationNotifier> _otherCharacterAnimationNotifiers;
private readonly IPacketSendService _packetSendService;

public override PacketFamily Family => PacketFamily.Walk;

public override PacketAction Action => PacketAction.Player;

public PlayerWalkHandler(IPlayerInfoProvider playerInfoProvider,
ICurrentMapStateRepository currentMapStateRepository,
IEnumerable<IOtherCharacterAnimationNotifier> otherCharacterAnimationNotifiers,
IPacketSendService packetSendService)
IEnumerable<IOtherCharacterAnimationNotifier> otherCharacterAnimationNotifiers)
: base(playerInfoProvider)
{
_currentMapStateRepository = currentMapStateRepository;
_otherCharacterAnimationNotifiers = otherCharacterAnimationNotifiers;
_packetSendService = packetSendService;
}

public override bool HandlePacket(IPacket packet)
Expand Down
1 change: 1 addition & 0 deletions EndlessClient/HUD/Controls/HudControlIdentifier.cs
Expand Up @@ -69,6 +69,7 @@ public enum HudControlIdentifier
UserInputHandler,
CharacterAnimator,
NPCAnimator,
UnknownEntitiesRequester,

PreviousUserInputTracker = Int32.MaxValue, //this should always be last!
}
Expand Down
13 changes: 12 additions & 1 deletion EndlessClient/HUD/Controls/HudControlsFactory.cs
Expand Up @@ -10,6 +10,7 @@
using EndlessClient.HUD.Panels;
using EndlessClient.HUD.StatusBars;
using EndlessClient.Input;
using EndlessClient.Network;
using EndlessClient.Rendering;
using EndlessClient.Rendering.Character;
using EndlessClient.Rendering.Factories;
Expand All @@ -19,6 +20,7 @@
using EOLib.Domain.Map;
using EOLib.Graphics;
using EOLib.Localization;
using EOLib.Net.Communication;
using Microsoft.Xna.Framework;
using XNAControls;

Expand Down Expand Up @@ -53,6 +55,7 @@ public class HudControlsFactory : IHudControlsFactory
private readonly IPathFinder _pathFinder;
private readonly ICharacterActions _characterActions;
private readonly IWalkValidationActions _walkValidationActions;
private readonly IPacketSendService _packetSendService;
private IChatController _chatController;

public HudControlsFactory(IHudButtonController hudButtonController,
Expand All @@ -76,7 +79,8 @@ public class HudControlsFactory : IHudControlsFactory
IArrowKeyController arrowKeyController,
IPathFinder pathFinder,
ICharacterActions characterActions,
IWalkValidationActions walkValidationActions)
IWalkValidationActions walkValidationActions,
IPacketSendService packetSendService)
{
_hudButtonController = hudButtonController;
_hudPanelFactory = hudPanelFactory;
Expand All @@ -100,6 +104,7 @@ public class HudControlsFactory : IHudControlsFactory
_pathFinder = pathFinder;
_characterActions = characterActions;
_walkValidationActions = walkValidationActions;
_packetSendService = packetSendService;
}

public void InjectChatController(IChatController chatController)
Expand Down Expand Up @@ -158,6 +163,7 @@ public void InjectChatController(IChatController chatController)
{HudControlIdentifier.UserInputHandler, CreateUserInputHandler()},
{HudControlIdentifier.CharacterAnimator, CreateCharacterAnimator()},
{HudControlIdentifier.NPCAnimator, CreateNPCAnimator()},
{HudControlIdentifier.UnknownEntitiesRequester, CreateUnknownEntitiesRequester()},
{HudControlIdentifier.PreviousUserInputTracker, CreatePreviousUserInputTracker()}
};

Expand Down Expand Up @@ -359,6 +365,11 @@ private UsageTrackerComponent CreateUsageTracker()
return new UsageTrackerComponent(_endlessGameProvider, _characterRepository);
}

private UnknownEntitiesRequester CreateUnknownEntitiesRequester()
{
return new UnknownEntitiesRequester(_endlessGameProvider, _currentMapStateRepository, _packetSendService);
}

private StatusBarLabel CreateStatusLabel()
{
return new StatusBarLabel(_clientWindowSizeProvider, _statusLabelTextProvider) { DrawOrder = HUD_CONTROL_LAYER };
Expand Down
104 changes: 104 additions & 0 deletions EndlessClient/Network/UnknownEntitiesRequester.cs
@@ -0,0 +1,104 @@
using EndlessClient.GameExecution;
using EOLib.Domain.Map;
using EOLib.Net;
using EOLib.Net.Communication;
using Microsoft.Xna.Framework;
using System;

namespace EndlessClient.Network
{
public class UnknownEntitiesRequester : GameComponent
{
private readonly ICurrentMapStateRepository _currentMapStateRepository;
private readonly IPacketSendService _packetSendService;
private DateTime _lastRequestTime;
private const double REQUEST_INTERVAL_SECONDS = 1;

public UnknownEntitiesRequester(IEndlessGameProvider gameProvider,
ICurrentMapStateRepository currentMapStateRepository,
IPacketSendService packetSendService)
: base((Game) gameProvider.Game)
{
_currentMapStateRepository = currentMapStateRepository;
_packetSendService = packetSendService;
_lastRequestTime = DateTime.Now;
}

public override void Update(GameTime gameTime)
{
if ((DateTime.Now - _lastRequestTime).TotalSeconds >= REQUEST_INTERVAL_SECONDS)
{
IPacket request = null;
if (_currentMapStateRepository.UnknownNPCIndexes.Count > 0 && _currentMapStateRepository.UnknownPlayerIDs.Count > 0)
{
request = CreateRequestForBoth();
}
else if (_currentMapStateRepository.UnknownNPCIndexes.Count > 0)
{
request = CreateRequestForNPCs();
}
else if (_currentMapStateRepository.UnknownPlayerIDs.Count > 0)
{
request = CreateRequestForPlayers();
}

try
{
if (request != null)
_packetSendService.SendPacket(request);
}
catch (NoDataSentException)
{
// do nothing.. should we log these?
}
finally
{
_currentMapStateRepository.UnknownNPCIndexes.Clear();
_currentMapStateRepository.UnknownPlayerIDs.Clear();
_lastRequestTime = DateTime.Now;
}
}

base.Update(gameTime);
}

private IPacket CreateRequestForBoth()
{
IPacketBuilder builder = new PacketBuilder(PacketFamily.MapInfo, PacketAction.Request);
foreach (var id in _currentMapStateRepository.UnknownPlayerIDs)
{
builder = builder.AddShort(id);
}
builder = builder.AddByte(0xFF);
foreach (var index in _currentMapStateRepository.UnknownNPCIndexes)
{
builder = builder.AddChar(index);
}

return builder.Build();
}

private IPacket CreateRequestForNPCs()
{
IPacketBuilder builder = new PacketBuilder(PacketFamily.NPCMapInfo, PacketAction.Request)
.AddChar((byte)_currentMapStateRepository.UnknownNPCIndexes.Count)
.AddByte(0xFF);

foreach (var index in _currentMapStateRepository.UnknownNPCIndexes)
{
builder = builder.AddChar(index);
}
return builder.Build();
}

private IPacket CreateRequestForPlayers()
{
IPacketBuilder builder = new PacketBuilder(PacketFamily.CharacterMapInfo, PacketAction.Request);
foreach (var id in _currentMapStateRepository.UnknownPlayerIDs)
{
builder = builder.AddShort(id);
}
return builder.Build();
}
}
}

0 comments on commit 4b0758b

Please sign in to comment.