Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add UnknownEntitiesRequester to game
- Loading branch information
Showing
5 changed files
with
121 additions
and
9 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
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 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 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 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,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(); | ||
} | ||
} | ||
} |