Skip to content

Commit

Permalink
Store unknown Players/NPC in MapState repository
Browse files Browse the repository at this point in the history
  • Loading branch information
sorokya committed Mar 28, 2022
1 parent 278c31f commit b2d72c5
Show file tree
Hide file tree
Showing 16 changed files with 138 additions and 102 deletions.
14 changes: 14 additions & 0 deletions EOLib/Domain/Map/CurrentMapStateRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ public interface ICurrentMapStateRepository
HashSet<MapCoordinate> VisibleSpikeTraps { get; set; }

WarpState MapWarpState { get; set; }

HashSet<short> UnknownPlayerIDs { get; set; }

HashSet<byte> UnknownNPCIndexes { get; set; }
}

public interface ICurrentMapStateProvider
Expand All @@ -45,6 +49,10 @@ public interface ICurrentMapStateProvider
IReadOnlyCollection<MapCoordinate> VisibleSpikeTraps { get; }

WarpState MapWarpState { get; set; }

HashSet<short> UnknownPlayerIDs { get; }

HashSet<byte> UnknownNPCIndexes { get; }
}

[AutoMappedType(IsSingleton = true)]
Expand All @@ -68,6 +76,10 @@ public class CurrentMapStateRepository : ICurrentMapStateRepository, ICurrentMap

public WarpState MapWarpState { get; set; }

public HashSet<short> UnknownPlayerIDs { get; set; }

public HashSet<byte> UnknownNPCIndexes { get; set; }

IReadOnlyDictionary<int, ICharacter> ICurrentMapStateProvider.Characters => Characters;

IReadOnlyCollection<INPC> ICurrentMapStateProvider.NPCs => NPCs;
Expand Down Expand Up @@ -96,6 +108,8 @@ public void ResetState()
OpenDoors = new HashSet<IWarp>();
PendingDoors = new HashSet<IWarp>();
VisibleSpikeTraps = new HashSet<MapCoordinate>();
UnknownPlayerIDs = new HashSet<short>();
UnknownNPCIndexes = new HashSet<byte>();

MapWarpState = WarpState.None;
}
Expand Down
18 changes: 12 additions & 6 deletions EOLib/PacketHandlers/AdminHideHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,18 @@ public override bool HandlePacket(IPacket packet)
_characterRepository.MainCharacter = Hidden(_characterRepository.MainCharacter);
else
{
if (!_currentMapStateRepository.Characters.ContainsKey(id))
return false;
var character = _currentMapStateRepository.Characters[id];

var updatedCharacter = Hidden(character);
_currentMapStateRepository.Characters[id] = updatedCharacter;
if (_currentMapStateRepository.Characters.ContainsKey(id))
{
var character = _currentMapStateRepository.Characters[id];

var updatedCharacter = Hidden(character);
_currentMapStateRepository.Characters[id] = updatedCharacter;
}
else
{
_currentMapStateRepository.UnknownPlayerIDs.Add(id);
}

}

return true;
Expand Down
17 changes: 11 additions & 6 deletions EOLib/PacketHandlers/AdminShowHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,17 @@ public override bool HandlePacket(IPacket packet)
_characterRepository.MainCharacter = Shown(_characterRepository.MainCharacter);
else
{
if (!_currentMapStateRepository.Characters.ContainsKey(id))
return false;
var character = _currentMapStateRepository.Characters[id];

var updatedCharacter = Shown(character);
_currentMapStateRepository.Characters[id] = updatedCharacter;
if (_currentMapStateRepository.Characters.ContainsKey(id))
{
var character = _currentMapStateRepository.Characters[id];

var updatedCharacter = Shown(character);
_currentMapStateRepository.Characters[id] = updatedCharacter;
}
else
{
_currentMapStateRepository.UnknownPlayerIDs.Add(id);
}
}

return true;
Expand Down
19 changes: 12 additions & 7 deletions EOLib/PacketHandlers/Effects/PlayerSpikeDamageHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,16 +35,21 @@ public override bool HandlePacket(IPacket packet)
var isDead = packet.ReadChar() != 0;
var damageTaken = packet.ReadThree();

if (!_currentMapStateRepository.Characters.ContainsKey(characterId))
return false;

var updatedCharacter = _currentMapStateRepository.Characters[characterId].WithDamage(damageTaken, isDead);
_currentMapStateRepository.Characters[characterId] = updatedCharacter;
if (_currentMapStateRepository.Characters.ContainsKey(characterId))
{
var updatedCharacter = _currentMapStateRepository.Characters[characterId].WithDamage(damageTaken, isDead);
_currentMapStateRepository.Characters[characterId] = updatedCharacter;

foreach (var notifier in _otherCharacterEventNotifiers)
foreach (var notifier in _otherCharacterEventNotifiers)
{
notifier.OtherCharacterTakeDamage(characterId, playerPercentHealth, damageTaken);
}
}
else
{
notifier.OtherCharacterTakeDamage(characterId, playerPercentHealth, damageTaken);
_currentMapStateRepository.UnknownPlayerIDs.Add(characterId);
}


return true;
}
Expand Down
5 changes: 5 additions & 0 deletions EOLib/PacketHandlers/ItemEquipHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,11 @@ protected bool HandlePaperdollPacket(IPacket packet, bool itemUnequipped)
}
});

update.MatchNone(() =>
{
_currentMapStateRepository.UnknownPlayerIDs.Add(playerId);
});

return true;
}
}
Expand Down
61 changes: 11 additions & 50 deletions EOLib/PacketHandlers/MainPlayerWalkHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@
using EOLib.Domain.Login;
using EOLib.Domain.Map;
using EOLib.Net;
using EOLib.Net.Communication;
using EOLib.Net.Handlers;
using System.Collections.Generic;
using System.Linq;

namespace EOLib.PacketHandlers
Expand All @@ -13,33 +11,36 @@ namespace EOLib.PacketHandlers
public class MainPlayerWalkHandler : InGameOnlyPacketHandler
{
private readonly ICurrentMapStateRepository _currentMapStateRepository;
private readonly IPacketSendService _packetSendService;

public override PacketFamily Family => PacketFamily.Walk;
public override PacketAction Action => PacketAction.Reply;

public MainPlayerWalkHandler(IPlayerInfoProvider playerInfoProvider,
ICurrentMapStateRepository currentMapStateRepository,
IPacketSendService packetSendService)
ICurrentMapStateRepository currentMapStateRepository)
: base(playerInfoProvider)
{
_currentMapStateRepository = currentMapStateRepository;
_packetSendService = packetSendService;
}

public override bool HandlePacket(IPacket packet)
{
var playerIDs = new List<short>();
while (packet.PeekByte() != 0xFF)
{
playerIDs.Add(packet.ReadShort());
var playerID = packet.ReadShort();
if (!_currentMapStateRepository.Characters.ContainsKey(playerID))
{
_currentMapStateRepository.UnknownPlayerIDs.Add(playerID);
}
}
packet.ReadByte();

var npcIndexes = new List<byte>();
while (packet.PeekByte() != 0xFF)
{
npcIndexes.Add(packet.ReadChar());
var index = packet.ReadChar();
if (!_currentMapStateRepository.NPCs.Any((npc) => npc.Index == index))
{
_currentMapStateRepository.UnknownNPCIndexes.Add(index);
}
}
packet.ReadByte();

Expand All @@ -56,46 +57,6 @@ public override bool HandlePacket(IPacket packet)
_currentMapStateRepository.MapItems.Add(newItem);
}

var newPlayerIDs = playerIDs
.Where(id => !_currentMapStateRepository.Characters.ContainsKey(id))
.ToList();

var newNPCIndxes = npcIndexes
.Where(index => !_currentMapStateRepository.NPCs.Any((npc) => npc.Index == index))
.ToList();

if (newPlayerIDs.Count > 0 || newNPCIndxes.Count > 0)
{
IPacketBuilder builder = new PacketBuilder(PacketFamily.MapInfo, PacketAction.Request);

if (newPlayerIDs.Count > 0)
{
foreach (var playerId in newPlayerIDs)
{
builder = builder.AddShort(playerId);
}
}

if (newNPCIndxes.Count > 0)
{
builder.AddByte(0xFF);
foreach (var npcIndex in newNPCIndxes)
{
builder = builder.AddChar(npcIndex);
}
}

try
{
var request = builder.Build();
_packetSendService.SendPacket(request);
}
catch (NoDataSentException)
{
return false;
}
}

return true;
}
}
Expand Down
10 changes: 9 additions & 1 deletion EOLib/PacketHandlers/NPCActionHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,11 @@ public override bool HandlePacket(IPacket packet)
{
npc = _currentMapStateRepository.NPCs.Single(n => n.Index == index);
}
catch (InvalidOperationException) { return false; }
catch (InvalidOperationException)
{
_currentMapStateRepository.UnknownNPCIndexes.Add(index);
return false;
}

var updatedNpc = Option.None<INPC>();
switch (num255s)
Expand Down Expand Up @@ -142,6 +146,10 @@ private INPC HandleNPCAttack(IPacket packet, INPC npc)
foreach (var notifier in _otherCharacterNotifiers)
notifier.OtherCharacterTakeDamage(characterID, playerPercentHealth, damageTaken);
}
else
{
_currentMapStateRepository.UnknownPlayerIDs.Add(characterID);
}

foreach (var notifier in _npcAnimationNotifiers)
notifier.StartNPCAttackAnimation(npc.Index);
Expand Down
4 changes: 4 additions & 0 deletions EOLib/PacketHandlers/NPCLeaveMapHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,10 @@ private void UpdatePlayerDirection(short playerID, EODirection playerDirection)
var updatedCharacter = _currentMapStateRepository.Characters[playerID].WithRenderProperties(updatedRenderProps);
_currentMapStateRepository.Characters[playerID] = updatedCharacter;
}
else
{
_currentMapStateRepository.UnknownPlayerIDs.Add(playerID);
}
}

private void UpdateCharacterStat(CharacterStat whichStat, int statValue)
Expand Down
10 changes: 9 additions & 1 deletion EOLib/PacketHandlers/NPCTakeDamageHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,10 @@ public override bool HandlePacket(IPacket packet)
var updatedCharacter = _currentMapStateRepository.Characters[fromPlayerId].WithRenderProperties(renderProps);
_currentMapStateRepository.Characters[fromPlayerId] = updatedCharacter;
}
else
{
_currentMapStateRepository.UnknownPlayerIDs.Add(fromPlayerId);
}

// todo: this has the potential to bug out if the opponent ID is never reset and the player dies/leaves
try
Expand All @@ -79,7 +83,11 @@ public override bool HandlePacket(IPacket packet)
foreach (var notifier in _npcNotifiers)
notifier.NPCTakeDamage(npcIndex, fromPlayerId, damageToNpc, npcPctHealth, spellId);
}
catch (InvalidOperationException) { return false; }
catch (InvalidOperationException)
{
_currentMapStateRepository.UnknownNPCIndexes.Add((byte)npcIndex);
return false;
}

return true;
}
Expand Down
24 changes: 14 additions & 10 deletions EOLib/PacketHandlers/PlayerAttackHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,18 +32,22 @@ public override bool HandlePacket(IPacket packet)
var playerID = packet.ReadShort();
var direction = (EODirection)packet.ReadChar();

if (!_currentMapStateRepository.Characters.ContainsKey(playerID))
return false;

var character = _currentMapStateRepository.Characters[playerID];
if (character.RenderProperties.Direction != direction)
if (_currentMapStateRepository.Characters.ContainsKey(playerID))
{
var renderProperties = character.RenderProperties.WithDirection(direction);
_currentMapStateRepository.Characters[playerID] = character.WithRenderProperties(renderProperties);
var character = _currentMapStateRepository.Characters[playerID];
if (character.RenderProperties.Direction != direction)
{
var renderProperties = character.RenderProperties.WithDirection(direction);
_currentMapStateRepository.Characters[playerID] = character.WithRenderProperties(renderProperties);
}

foreach (var notifier in _otherCharacterAnimationNotifiers)
notifier.StartOtherCharacterAttackAnimation(playerID);
}
else
{
_currentMapStateRepository.UnknownPlayerIDs.Add(playerID);
}

foreach (var notifier in _otherCharacterAnimationNotifiers)
notifier.StartOtherCharacterAttackAnimation(playerID);

return true;
}
Expand Down
1 change: 1 addition & 0 deletions EOLib/PacketHandlers/PlayerAvatarChangeHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ public override bool HandlePacket(IPacket packet)
}
else
{
_currentMapStateRepository.UnknownPlayerIDs.Add(playerID);
return false;
}

Expand Down
16 changes: 10 additions & 6 deletions EOLib/PacketHandlers/PlayerLeaveMapHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,16 @@ public override bool HandlePacket(IPacket packet)
notifier.NotifyWarpLeaveEffect(id, anim);
}

if (!_currentMapStateRepository.Characters.ContainsKey(id))
return false;

var character = _currentMapStateRepository.Characters[id];
_currentMapStateRepository.Characters.Remove(id);
_currentMapStateRepository.VisibleSpikeTraps.Remove(new MapCoordinate(character.RenderProperties.MapX, character.RenderProperties.MapY));
if (_currentMapStateRepository.Characters.ContainsKey(id))
{
var character = _currentMapStateRepository.Characters[id];
_currentMapStateRepository.Characters.Remove(id);
_currentMapStateRepository.VisibleSpikeTraps.Remove(new MapCoordinate(character.RenderProperties.MapX, character.RenderProperties.MapY));
}
else
{
_currentMapStateRepository.UnknownPlayerIDs.Add(id);
}

return true;
}
Expand Down
1 change: 1 addition & 0 deletions EOLib/PacketHandlers/PlayerSitHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ public override bool HandlePacket(IPacket packet)
}
else
{
_currentMapStateRepository.UnknownPlayerIDs.Add(playerId);
return false;
}

Expand Down
1 change: 1 addition & 0 deletions EOLib/PacketHandlers/PlayerStandHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ public override bool HandlePacket(IPacket packet)
}
else
{
_currentMapStateRepository.UnknownPlayerIDs.Add(playerId);
return false;
}

Expand Down
1 change: 1 addition & 0 deletions EOLib/PacketHandlers/PlayerTargetOtherSpellHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ public override bool HandlePacket(IPacket packet)
}
else
{
_currentMapStateRepository.UnknownPlayerIDs.Add(sourcePlayerId);
return false;
}

Expand Down
Loading

0 comments on commit b2d72c5

Please sign in to comment.